JSFiddle - React, Tailwind, and code Playground

by PiereDome

JavaScript

//collision detection test
function Sprite_Collide(object1, object2) {
  
        var left1, left2;
        var right1, right2;
        var top1, top2;
        var bottom1, bottom2;

        left1 = object1.x;
        left2 = object2.x;
        right1 = object1.x + object1.width;
        right2 = object2.x + object2.width;
        top1 = object1.y;
        top2 = object2.y;
        bottom1 = object1.y + object1.height;
        bottom2 = object2.y + object2.height;

        if (bottom1 < top2) return false;
        if (top1 > bottom2) return false;

        if (right1 < left2) return false;
        if (left1 > right2) return false;

        return !!1;

};

var object = function(x,y,height,width){
    this.x = x;
    this.y = y;
    this.height = height;
    this.width = width;
};
var shapes = [];
ball = new object(10,10,5,5);
shape1 = new object(10,15,5,5);
shapes.push(shape1);
shape2 = new object(25,15,10,10);
shapes.push(shape2);

for(i=0;i<shapes.length;i++){
console.log(Sprite_Collide(ball,shapes[i]));
}