I've managed create simulation without multiplicating positions of bodies by 30 each step.
My solution:
open b2Settings.as. Add this constant:
then multiplicate all constants in this file related to linear(not angular!) velosity and distances:
then in your code you can create objects using pixel size instead meters:
Note! use density/(b2Settings.b2_pixelScale*b2Settings.b2_pixelScale) instead of density, for example:
So in your update function you don't need multiplicate positions by b2Settings.b2_pixelScale:
Compare 2 swf's in attachment - helloworld and my test.
My solution:
open b2Settings.as. Add this constant:
Code:
static public const b2_pixelScale:Number = 30;
then multiplicate all constants in this file related to linear(not angular!) velosity and distances:
Code:
static public const b2_linearSlop:Number = 0.005 * b2_pixelScale; // 0.5 cm
static public const b2_velocityThreshold:Number = 1.0 * b2_pixelScale; // 1 m/s
static public const b2_maxLinearCorrection:Number = 0.2 * b2_pixelScale; // 20 cm
static public const b2_maxTranslation:Number = 2.0 * b2_pixelScale;
static public const b2_linearSleepTolerance:Number = 0.01 * b2_pixelScale; // 1 cm/s
static public const b2_velocityThreshold:Number = 1.0 * b2_pixelScale; // 1 m/s
static public const b2_maxLinearCorrection:Number = 0.2 * b2_pixelScale; // 20 cm
static public const b2_maxTranslation:Number = 2.0 * b2_pixelScale;
static public const b2_linearSleepTolerance:Number = 0.01 * b2_pixelScale; // 1 cm/s
then in your code you can create objects using pixel size instead meters:
Code:
// Create world AABB
var worldAABB:b2AABB = new b2AABB();
worldAABB.lowerBound.Set(-100.0*b2Settings.b2_pixelScale, -100.0*b2Settings.b2_pixelScale);
worldAABB.upperBound.Set(100.0*b2Settings.b2_pixelScale, 100.0*b2Settings.b2_pixelScale);
// Define the gravity vector
var gravity:b2Vec2 = new b2Vec2(0.0, 10.0*b2Settings.b2_pixelScale);
...
bodyDef = new b2BodyDef();
bodyDef.position.Set(10*b2Settings.b2_pixelScale, 12*b2Settings.b2_pixelScale);
boxDef = new b2PolygonDef();
boxDef.SetAsBox(30*b2Settings.b2_pixelScale, 3*b2Settings.b2_pixelScale);
boxDef.friction = 0.3;
boxDef.density = 0;
bodyDef.userData = new Ground();
bodyDef.userData.width = 2 * 30 * b2Settings.b2_pixelScale;
bodyDef.userData.height = 2 * 3 * b2Settings.b2_pixelScale;
var worldAABB:b2AABB = new b2AABB();
worldAABB.lowerBound.Set(-100.0*b2Settings.b2_pixelScale, -100.0*b2Settings.b2_pixelScale);
worldAABB.upperBound.Set(100.0*b2Settings.b2_pixelScale, 100.0*b2Settings.b2_pixelScale);
// Define the gravity vector
var gravity:b2Vec2 = new b2Vec2(0.0, 10.0*b2Settings.b2_pixelScale);
...
bodyDef = new b2BodyDef();
bodyDef.position.Set(10*b2Settings.b2_pixelScale, 12*b2Settings.b2_pixelScale);
boxDef = new b2PolygonDef();
boxDef.SetAsBox(30*b2Settings.b2_pixelScale, 3*b2Settings.b2_pixelScale);
boxDef.friction = 0.3;
boxDef.density = 0;
bodyDef.userData = new Ground();
bodyDef.userData.width = 2 * 30 * b2Settings.b2_pixelScale;
bodyDef.userData.height = 2 * 3 * b2Settings.b2_pixelScale;
Note! use density/(b2Settings.b2_pixelScale*b2Settings.b2_pixelScale) instead of density, for example:
Code:
boxDef.SetAsBox(rX, rY);
boxDef.density = 1.0/(b2Settings.b2_pixelScale*b2Settings.b2_pixelScale);
boxDef.friction = 0.5;
boxDef.density = 1.0/(b2Settings.b2_pixelScale*b2Settings.b2_pixelScale);
boxDef.friction = 0.5;
So in your update function you don't need multiplicate positions by b2Settings.b2_pixelScale:
Code:
// Go through body list and update sprite positions/rotations
for (var bb:b2Body = world.GetBodyList(); bb; bb = bb.GetNext()){
if (bb.GetUserData() is Sprite){
var sprite:Sprite = bb.GetUserData() as Sprite;
sprite.x = bb.GetPosition().x;
sprite.y = bb.GetPosition().y;
sprite.rotation = bb.GetAngle() * (180/Math.PI);
}
}
for (var bb:b2Body = world.GetBodyList(); bb; bb = bb.GetNext()){
if (bb.GetUserData() is Sprite){
var sprite:Sprite = bb.GetUserData() as Sprite;
sprite.x = bb.GetPosition().x;
sprite.y = bb.GetPosition().y;
sprite.rotation = bb.GetAngle() * (180/Math.PI);
}
}
Compare 2 swf's in attachment - helloworld and my test.