Permalink
Browse files

Add support for smaller bounding box collisions.

  • Loading branch information...
1 parent 78b6804 commit 0cb011ad2b7cdadb3418d85d03b4508b7651ade4 @batiste committed Jan 17, 2012
Showing with 34 additions and 18 deletions.
  1. +34 −18 lib/collision.js
View
@@ -18,7 +18,7 @@ function pointInPoly(polyCords, x, y)
&& (x < (polyCords[j][0] - polyCords[i][0]) *
(y - polyCords[i][1]) / (polyCords[j][1] - polyCords[i][1]) + polyCords[i][0])) {
c = !c;
- }
+ }
}
return c;
}
@@ -74,29 +74,45 @@ function rectangleEdges(rect) {
function shapeCenter(s) {
return {
- x:(s.x + s.w / 2),
+ x:(s.x + s.w / 2),
y:(s.y + s.h / 2)
};
}
+function makeBoundingBox(a) {
+ if(!a.shape) {
+ return a;
+ }
+ return {
+ x:a.x + a.shape.x,
+ y:a.y + a.shape.y,
+ type: a.shape.type || a.type,
+ w:a.shape.w,
+ h:a.shape.h,
+ };
+}
+
function shapeCollides(a, b) {
-
+
+ a = makeBoundingBox(a);
+ b = makeBoundingBox(b);
+
// quick shortcut
if(Math.abs(a.x - b.x) > a.w + b.w)
return false;
-
+
if(Math.abs(a.y - b.y) > a.h + b.h)
return false;
-
+
if(a.type == "rectangle") {
if(b.type == "rectangle") {
return rectangleCollides(a, b);
}
if(b.type == "circle") {
return rectangleCircleCollides(a, b);
}
- }
-
+ }
+
if(a.type == "circle") {
if(b.type == "circle") {
return circleCollides(a, b)
@@ -108,7 +124,7 @@ function shapeCollides(a, b) {
}
function rectangleCollides(a, b) {
-
+
// shortcut for unrotated rectangles.
if(a.angle == 0 && b.angle == 0) {
if(a.x > b.x) {
@@ -126,11 +142,11 @@ function rectangleCollides(a, b) {
}
return y_inter;
};
-
+
// cache the expensive edges function
a.edges = rectangleEdges(a);
b.edges = rectangleEdges(b);
-
+
for(var i=0; i<4; i++) {
if(isPointInRectangle(b.edges[i][0], b.edges[i][1], a))
return true
@@ -146,8 +162,8 @@ function rectangleCircleCollides(r, c) {
var edges = rectangleEdges(r);
var c1 = shapeCenter(c);
for(var i=0; i < 4; i++) {
- if(pointToLineDistance(c1.x, c1.y,
- edges[i][0], edges[i][1],
+ if(pointToLineDistance(c1.x, c1.y,
+ edges[i][0], edges[i][1],
edges[(i+1) % 4][0], edges[(i+1) % 4][1])
< c.w / 2) {
return true;
@@ -210,10 +226,10 @@ function isPointInImage(x, y, w, h, img) {
} else {
imgd = image_data_cache[img.src];
}
-
+
var index = (y * img.width * 4 + x * 4) + 3;
var alpha = imgd.data[index];
-
+
return alpha > 50;
}
@@ -237,11 +253,11 @@ Sprite.prototype.collidesWith = function collidesWith(sprite) {
return true;
return shapeCollides(this, sprite);
};
-
+
Sprite.prototype.collidesWithArray = function collidesWithArray(sprites) {
// Return a sprite if the current sprite has any collision with the Array provided
// a sprite cannot collides with itself.
-
+
// Make the List works
if(sprites.list)
sprites = sprites.list;
@@ -256,7 +272,7 @@ Sprite.prototype.collidesWithArray = function collidesWithArray(sprites) {
function resolveCollisions() {
// return each pair of collision
- var collisions = {};
+ var collisions = {};
// the function accept any amount of array of elements
if(arguments.length == 1)
var elements = arguments[0];
@@ -269,7 +285,7 @@ function resolveCollisions() {
for(var j=i+1; j<elements.length; j++) {
if(el.collidesWith(elements[j])) {
collisions[el] = elements[j];
- collisions[elements[j]] = el;
+ collisions[elements[j]] = el;
}
}
}

0 comments on commit 0cb011a

Please sign in to comment.