html5 - Javascript canvas collision side detection -
hey i'm trying side 2 objects in canvas collide. here's i'm using collision detection, checks collision, without specific side.
where o1 , o2 objects taht have properties:
x - position on x axis
 y - position on y axis
 w - width of rectangle
 h - height of rectangle
var collideswith = function (o2) {     var o1 = this;     if ((o1.y + o1.h) < o2.y) {         return 0;     }     if (o1.y > (o2.y + o2.h)) {         return 0;     }     if ((o1.x + o1.w) < o2.x) {         return 0;     }     if (o1.x > (o2.x + o2.w)) {         return 0;     }     return 1; }; edit: here's code came collision detection on top of element:
if (     (o1.y - o1.dy >= o2.y) &&     (o1.y - o1.dy <= o2.y + o2.h) &&     (o1.x + o1.w >= o2.x) &&     (o1.x <= o2.x + o2.w) ) {     // have collision @ top end } 
you need double-conditions this:
if ((o1.y > o2.y) && (o1.y < o2.y + o2.h)) {   return 'top'; // o1's top border collided o2's bottom border } similarily other sides.
Comments
Post a Comment