Hi all,
I've made a copy of the TestStack example and stripped it down to the four walls, and the circle that is supposed to drop onto the ramp. From there, I have applied keyboard-based movement to the circle, but for some reason, occasionally my walls are disappearing.
I'm 99% sure it is a reference error of some kind where I'm trying to apply an impulse to the walls accidentally. I don't see how that could happen, though, unless box2d is mixing up the references.
Here's the gist of what is going on:
I suspect there is something wrong with creating a pointer to the b2Body object in my _oHero object, but I don't know enough about the inner workings of Box2d to determine exactly what is going on.
Can anyone offer an explanation?
I've made a copy of the TestStack example and stripped it down to the four walls, and the circle that is supposed to drop onto the ramp. From there, I have applied keyboard-based movement to the circle, but for some reason, occasionally my walls are disappearing.
I'm 99% sure it is a reference error of some kind where I'm trying to apply an impulse to the walls accidentally. I don't see how that could happen, though, unless box2d is mixing up the references.
Here's the gist of what is going on:
Code:
// Create ball
var cd:b2CircleDef = new b2CircleDef();
cd.radius = 20/m_physScale;
cd.density = 2;
cd.restitution = 0.5;
cd.friction = 0.1;
var bodyDef:b2BodyDef = new b2BodyDef();
bodyDef.position.Set(100/m_physScale, 100 / m_physScale);
var body:b2Body = m_world.CreateBody(bodyDef);
body.CreateShape(cd);
body.SetMassFromShapes();
_oHero = new Object();
_oHero.body = body;
//... then, later, in my update loop:
override public function Update():void{
super.Update();// calls Update in Test.as
var oDirection:b2Vec2 = new b2Vec2();
var bHeroUpdateRequired:Boolean = false;
if (Input.isKeyDown(65)) // A
{
bHeroUpdateRequired = true;
oDirection.Set(-5,0);
}
else if (Input.isKeyDown(68)) // D
{
bHeroUpdateRequired = true;
oDirection.Set(5,0);
}
if (Input.isKeyDown(87)) // W
{
//if( hero is touching ground ){
bHeroUpdateRequired = true;
oDirection.Set(0, -5);
//}
}
if (bHeroUpdateRequired)
{
// for some reason the left or top wall will disappear when this block gets called! :(
_oHero.body.WakeUp();
_oHero.body.ApplyImpulse(oDirection, _oHero.body.GetPosition())
}
}
var cd:b2CircleDef = new b2CircleDef();
cd.radius = 20/m_physScale;
cd.density = 2;
cd.restitution = 0.5;
cd.friction = 0.1;
var bodyDef:b2BodyDef = new b2BodyDef();
bodyDef.position.Set(100/m_physScale, 100 / m_physScale);
var body:b2Body = m_world.CreateBody(bodyDef);
body.CreateShape(cd);
body.SetMassFromShapes();
_oHero = new Object();
_oHero.body = body;
//... then, later, in my update loop:
override public function Update():void{
super.Update();// calls Update in Test.as
var oDirection:b2Vec2 = new b2Vec2();
var bHeroUpdateRequired:Boolean = false;
if (Input.isKeyDown(65)) // A
{
bHeroUpdateRequired = true;
oDirection.Set(-5,0);
}
else if (Input.isKeyDown(68)) // D
{
bHeroUpdateRequired = true;
oDirection.Set(5,0);
}
if (Input.isKeyDown(87)) // W
{
//if( hero is touching ground ){
bHeroUpdateRequired = true;
oDirection.Set(0, -5);
//}
}
if (bHeroUpdateRequired)
{
// for some reason the left or top wall will disappear when this block gets called! :(
_oHero.body.WakeUp();
_oHero.body.ApplyImpulse(oDirection, _oHero.body.GetPosition())
}
}
I suspect there is something wrong with creating a pointer to the b2Body object in my _oHero object, but I don't know enough about the inner workings of Box2d to determine exactly what is going on.
Can anyone offer an explanation?