Walls in the test bed keep disappearing

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:

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())
   }
}


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?
Well, after a bit of digging, I discovered why the walls were disappearing!

This is a testbed problem, not any sort of bug. In the Test.as file, you will find this block of code:

Code:
public function MouseDestroy():void{
   // mouse press
   if (!Input.mouseDown && Input.isKeyPressed(68/*D*/)){
      
      var body:b2Body = GetBodyAtMouse(true);
      
      if (body)
      {
         m_world.DestroyBody(body);
         return;
      }
   }
}


It just so happened that in my keyboard movement test i was putting together, i mapped right to the D key 😀

I bet this has happened to other new people just starting out, as the D key functionality is not really listed anywhere very clearly, and I think a lot of people will want to map their movement keys to the standard asdw.
Good spot, but I cannot help but feel you got what you deserved 😉 for building off the testbed without even reviewing the code, or stripping it. The TestBed is meant to be a framework for building from, or else it would be much more feature poor.
Quote:
I cannot help but feel you got what you deserved


Probably 🙂

I assumed all the keys that were mapped for the testbed were already mapped and listed in the gui, as is the arrow keys to switch demos, r to reload.

Oh well. Now I know how to destroy objects.

Original thread: https://box2d.org/forum/viewtopic.php?t=3408