updateAABB: function(test){ var b = new Body(); b.updateAABB(); var b = new Body(), s = new Circle({ radius: 1 }); b.addShape(s); b.updateAABB(); test.equal(b.aabb.lowerBound[0], -1, 'Lower AABB bound should be -1'); test.equal(b.aabb.upperBound[0], 1, 'Upper AABB bound should be 1'); test.equal(b.aabb.lowerBound[1], -1, 'Lower AABB bound should be -1'); test.equal(b.aabb.upperBound[1], 1, 'Upper AABB bound should be 1'); var b = new Body(), s = new Circle({ radius: 1 }), offset = [-2,3]; b.addShape(s,offset,Math.PI/2); b.updateAABB(); test.equal(b.aabb.lowerBound[0], -s.radius + offset[0]); test.equal(b.aabb.upperBound[0], s.radius + offset[0]); test.equal(b.aabb.lowerBound[1], -s.radius + offset[1]); test.equal(b.aabb.upperBound[1], s.radius + offset[1]); test.done(); },
collisionResponse: function(test){ var bodyA = new Body({ mass: 1, position: [1, 0] }); bodyA.addShape(new Circle({ radius: 1 })); var bodyB = new Body({ mass: 1, position: [-1, 0] }); bodyB.addShape(new Circle({ radius: 1 })); var world = new World(); world.addBody(bodyA); world.addBody(bodyB); world.step(1 / 60); test.ok(world.narrowphase.contactEquations[0].enabled); bodyA.collisionResponse = false; world.step(1 / 60); test.ok(!world.narrowphase.contactEquations[0].enabled); bodyA.collisionResponse = true; bodyA.shapes[0].collisionResponse = false; world.step(1 / 60); test.ok(!world.narrowphase.contactEquations[0].enabled); test.done(); },
overlaps: function(test){ var bodyA = new Body({ mass: 1 }); var bodyB = new Body({ mass: 1 }); bodyA.addShape(new Circle()); bodyB.addShape(new Circle()); var world = new World(); world.addBody(bodyA); world.addBody(bodyB); world.step(1/60); test.ok(bodyA.overlaps(bodyB)); test.done(); },
normal: function(test){ var body = new Body(); var shape = new Circle({ radius: 1 }); body.addShape(shape); test.deepEqual(body.shapes, [shape]); test.done(); },
canRemove: function(test){ var body = new Body(); body.addShape(new Circle({ radius: 1 })); test.ok(body.removeShape(body.shapes[0])); test.ok(!body.removeShape(new Circle({ radius: 1 }))); test.equal(body.shapes.length, 0); test.done(); },
exports.removeShape = function(test){ var body = new Body(); body.addShape(new Circle(1)); test.ok(body.removeShape(body.shapes[0])); test.ok(!body.removeShape(new Circle(1))); test.equal(body.shapes.length, 0); test.done(); };
withoutPoint: function(test){ var bodyA = new Body({ mass: 1, position: [2,3] }); bodyA.addShape(new Circle({ radius: 1 })); bodyA.applyImpulse([-1,0]); test.equal(bodyA.angularVelocity, 0); test.ok(bodyA.velocity[0] !== 0); test.equal(bodyA.velocity[1], 0); test.done(); }
adjustCenterOfMass: function(test){ var body = new Body(); var shape = new Circle({ radius: 1 }); body.addShape(shape, [1, 0], 0); body.adjustCenterOfMass(); test.deepEqual(body.position, vec2.fromValues(1,0)); test.deepEqual(body.shapes[0].position, vec2.fromValues(0,0)); test.done(); },
setDensity: function(test){ var body = new Body({ mass: 1 }); body.addShape(new Circle({ radius: 1 })); var inertiaBefore = body.inertia; body.setDensity(10); test.equal(body.mass, body.getArea() * 10); test.ok(inertiaBefore !== body.inertia); test.done(); },
updateBoundingRadius: function(test){ var body = new Body({ mass: 1 }); var shape = new Circle({ radius: 1 }); body.addShape(shape); test.equal(body.boundingRadius, 1); shape.radius = 2; shape.updateBoundingRadius(); body.updateBoundingRadius(); test.equal(body.boundingRadius, 2); test.done(); },
withCCDAndObstacle: function(test){ var world = new World({ gravity: [0,0] }); var body = new Body({ velocity: [2,0], position: [-1,0], mass: 1, ccdSpeedThreshold: 0, ccdIterations: 10, }); body.addShape(new Circle({ radius: 0.01 })); world.addBody(body); var planeBody = new Body({ mass: 0, angle: Math.PI / 2 }); planeBody.addShape(new Plane()); world.addBody(planeBody); world.step(1); // Need to use world.step() instead of body.integrate() test.ok(vec2.distance(body.position, [0,0]) < 0.1); test.done(); }
withoutPoint: function(test){ var bodyA = new Body({ mass: 1, position: [2,3], angle: Math.PI // rotated 180 degrees }); bodyA.addShape(new Circle({ radius: 1 })); bodyA.applyImpulseLocal([-1,0]); test.equal(bodyA.angularVelocity, 0); test.ok(bodyA.velocity[0] > 0); test.ok(Math.abs(bodyA.velocity[1]) < 0.001); test.done(); }
withPoint: function(test){ var bodyA = new Body({ mass: 1, position: [2,3], angle: Math.PI // rotated 180 degrees }); bodyA.addShape(new Circle({ radius: 1 })); bodyA.applyForceLocal([-1,0],[0,1]); test.ok(bodyA.angularForce > 0); test.ok(bodyA.force[0] > 0); test.ok(Math.abs(bodyA.force[1]) < 0.001); test.done(); },
duringStep: function(test){ var world = new World(); var body = new Body(); var shape = new Circle(); world.addBody(body); body.addShape(shape); world.on('postBroadphase', function(){ test.throws(function(){ body.removeShape(shape); }, 'should throw on removing shapes during step'); test.done(); }); world.step(1); }
getAABB: function(test){ var body = new Body(); body.addShape(new Box({ width: 1, height: 1 })); test.deepEqual(body.getAABB(), new AABB({ lowerBound: [-0.5, -0.5], upperBound: [0.5, 0.5] })); test.done(); },
test.throws(function(){ body.addShape(new Circle()); }, 'should throw on adding shapes during step');
getArea: function(test){ var body = new Body(); body.addShape(new Box({ width: 1, height: 1 })); test.equal(body.getArea(), 1); test.done(); },