Permalink
Browse files

Fixed areaForPoly, centroidForPoly, recenterPoly

  • Loading branch information...
1 parent 47dd9c7 commit 9b167c5daaa380e537bd3fe4ea14ec46425d0b64 @josephg josephg committed Feb 19, 2012
Showing with 9 additions and 11 deletions.
  1. +9 −11 lib/chipmunk.js
View
@@ -141,24 +141,22 @@ var momentForPoly = cp.momentForPoly = function(m, verts, offset)
var areaForPoly = cp.areaForPoly = function(verts)
{
- throw new Error('Not updated for flat verts');
var area = 0;
- for(var i=0, len=verts.length; i<len; i++){
- area += vcross(verts[i], verts[(i+1)%len]);
+ for(var i=0, len=verts.length; i<len; i+=2){
+ area += vcross(new Vect(verts[i], verts[i+1]), new Vect(verts[(i+2)%len], verts[(i+3)%len]));
}
return -area/2;
};
var centroidForPoly = cp.centroidForPoly = function(verts)
{
- throw new Error('Not updated for flat verts');
var sum = 0;
- var vsum = [0,0];
+ var vsum = new Vect(0,0);
- for(var i=0, len=verts.length; i<len; i++){
- var v1 = verts[i];
- var v2 = verts[(i+1)%len];
+ for(var i=0, len=verts.length; i<len; i+=2){
+ var v1 = new Vect(verts[i], verts[i+1]);
+ var v2 = new Vect(verts[(i+2)%len], verts[(i+3)%len]);
var cross = vcross(v1, v2);
sum += cross;
@@ -170,11 +168,11 @@ var centroidForPoly = cp.centroidForPoly = function(verts)
var recenterPoly = cp.recenterPoly = function(verts)
{
- throw new Error('Not updated for flat verts');
var centroid = centroidForPoly(verts);
- for(var i=0; i<verts.length; i++){
- verts[i] = vsub(verts[i], centroid);
+ for(var i=0; i<verts.length; i+=2){
+ verts[i] -= centroid.x;
+ verts[i+1] -= centroid.y;
}
};

0 comments on commit 9b167c5

Please sign in to comment.