When I run this simulation, none of the dynamic bodies collide with each other. They collide with the static body (floor). They won't sleep either; they just kind of shake around endlessly.
What am I missing?
Here's my init code:
What am I missing?
Here's my init code:
Code:
protected function setupWorld():void {
// bounding box
var AABB:b2AABB = new b2AABB();
AABB.lowerBound.Set(-1000, -1000);
AABB.upperBound.Set(1000, 1000);
// Define the gravity vector
var gravity:b2Vec2 = new b2Vec2(0, 15);
// Allow bodies to sleep
var sleep:Boolean = true;
// Construct a world object
world = new b2World(AABB, gravity, sleep);
}
protected function setupBodies():void {
var floorShapeDef:b2PolygonDef = new b2PolygonDef();
floorShapeDef.SetAsBox((stage.stageWidth/2) / boxScale, 100 / boxScale);
var floorBodyDef:b2BodyDef = new b2BodyDef();
floorBodyDef.position.Set((stage.stageWidth/2) / boxScale, stage.stageHeight / boxScale);
var floorBody:b2Body = world.CreateStaticBody(floorBodyDef);
floorBody.CreateShape(floorShapeDef);
floorBody.SetMassFromShapes();
floorBody.m_userData = "Floor";
var shapeDef:b2PolygonDef = new b2PolygonDef();
var bodyDef:b2BodyDef = new b2BodyDef();
var boxWidth:Number = 100;
var boxHeight:Number = 100;
var shape:Shape;
var body:b2Body;
var col:int;
var row:int;
for (col = 0; col < 5; col++) {
for (row = 0; row < 5; row++) {
shapeDef.SetAsBox((boxWidth/2) / boxScale, (boxHeight/2) / boxScale);
shapeDef.density = 20.0;
shapeDef.friction = 0.2;
shapeDef.restitution = 0.5;
bodyDef.position.Set((200*col + 300) / boxScale, (200*row - 500) / boxScale);
body = world.CreateDynamicBody(bodyDef);
body.CreateShape(shapeDef);
body.SetMassFromShapes();
}
}
}
// bounding box
var AABB:b2AABB = new b2AABB();
AABB.lowerBound.Set(-1000, -1000);
AABB.upperBound.Set(1000, 1000);
// Define the gravity vector
var gravity:b2Vec2 = new b2Vec2(0, 15);
// Allow bodies to sleep
var sleep:Boolean = true;
// Construct a world object
world = new b2World(AABB, gravity, sleep);
}
protected function setupBodies():void {
var floorShapeDef:b2PolygonDef = new b2PolygonDef();
floorShapeDef.SetAsBox((stage.stageWidth/2) / boxScale, 100 / boxScale);
var floorBodyDef:b2BodyDef = new b2BodyDef();
floorBodyDef.position.Set((stage.stageWidth/2) / boxScale, stage.stageHeight / boxScale);
var floorBody:b2Body = world.CreateStaticBody(floorBodyDef);
floorBody.CreateShape(floorShapeDef);
floorBody.SetMassFromShapes();
floorBody.m_userData = "Floor";
var shapeDef:b2PolygonDef = new b2PolygonDef();
var bodyDef:b2BodyDef = new b2BodyDef();
var boxWidth:Number = 100;
var boxHeight:Number = 100;
var shape:Shape;
var body:b2Body;
var col:int;
var row:int;
for (col = 0; col < 5; col++) {
for (row = 0; row < 5; row++) {
shapeDef.SetAsBox((boxWidth/2) / boxScale, (boxHeight/2) / boxScale);
shapeDef.density = 20.0;
shapeDef.friction = 0.2;
shapeDef.restitution = 0.5;
bodyDef.position.Set((200*col + 300) / boxScale, (200*row - 500) / boxScale);
body = world.CreateDynamicBody(bodyDef);
body.CreateShape(shapeDef);
body.SetMassFromShapes();
}
}
}