Problems with existing project and Box2D 2.2.0.

Hello! I'm sure this issue has been addressed in the forums recently, but I can't seem to craft a search query to ferret out the information.

I have an iPad project for which I was using Box2D 2.1.2 (I think). Today, I downloaded Box2D 2.2.0, and am trying to incorporate the new library into my project.

One of the first hiccups I hit was in GLES-Render.h. The class from which it was trying to derive GLESDebugDraw was b2DebugDraw. I *think* this needs to be b2Draw now. When I make that change, it seems happy.

The next area that I'm having trouble with is in GLES-Render.mm with the GLESDebugDraw::DrawTransform method. The b2Transform structure seems quite a bit different with the newer Box2D library. I see that the member variable position is now p. But there was an old bMath22 member variable called R, which seems to have been replaced with a b2Rot variable called q. It's not entirely clear to me how I would refactor this code to conform to the new structures:

Code: Select all

void GLESDebugDraw::DrawTransform(const b2Transform& xf)
{
   b2Vec2 p1 = xf.p, p2;
   const float32 k_axisScale = 0.4f;

   p2 = p1 + k_axisScale * xf.R.col1;
   DrawSegment(p1,p2,b2Color(1,0,0));
   
   p2 = p1 + k_axisScale * xf.R.col2;
   DrawSegment(p1,p2,b2Color(0,1,0));
}


I tried to see if there were any new template GLES-Render.h/mm files provided with the latest Box2D library, but I couldn't find any. Maybe this is more of a Cocos2D issue?

If anyone has any suggestions, I'd be much obliged!
The testbed implements all these for regular OpenGL, take a look at Render.cpp to see how the updates affected it.
The source code utilities on google code are extremely useful for this:
http://code.google.com/p/box2d/source/d ... side&r=186
That's awesome! I had no idea that GLES-Render was a subset of Render.cpp from the testbed. Thanks for the heads up! 🙂
I'm also having a problem with GLESDebugDraw. I'm using Box2D 2.2.1. I have updated the GLES-Render files to use b2Draw and have patched the
GLESDebugDraw::DrawTranform method. I have setup and registered GLESDebugDraw with my world but not matter what I do, I cannot get it to draw any shapes around my bodies. Has anyone found a fix for this? What am I missing? Box2D 2.1.0 worked with no problems. I'm using Cocos2d 1.0

DrawTranform method:

void GLESDebugDraw::DrawTransform(const b2Transform& xf)
{
b2Vec2 p1 = xf.p, p2;
const float32 k_axisScale = 0.4f;

p2 = p1 + k_axisScale * xf.q.GetXAxis();
DrawSegment(p1,p2,b2Color(1,0,0));

p2 = p1 + k_axisScale * xf.q.GetYAxis();
DrawSegment(p1,p2,b2Color(0,1,0));
}

- (void) setupDebugDraw {
_debugDraw = new GLESDebugDraw();
_debugDraw->SetFlags(b2Draw::e_shapeBit);
_world->SetDebugDraw(_debugDraw);
}

- (void) draw {
glDisable(GL_TEXTURE_2D);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);

if (_world) {
_world->DrawDebugData();
}

glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

}
Probably because the transforms are not shapes, I think you need:
SetFlags( b2Draw::e_centerOfMassBit );

Take a look at the b2World::DrawDebugData() function in b2World.cpp to see how each thing is drawn.
The e_shapeBit should be sufficient to draw all shapes. Here is a snippet or b2World::DrawDebugData:

Code: Select all

void b2World::DrawDebugData()
{
   if (m_debugDraw == NULL)
   {
   //   return;
   }

   uint32 flags = m_debugDraw->GetFlags();

   if (flags & b2Draw::e_shapeBit)
   {
      for (b2Body* b = m_bodyList; b; b = b->GetNext())
      {
         const b2Transform& xf = b->GetTransform();
         for (b2Fixture* f = b->GetFixtureList(); f; f = f->GetNext())
         {
            if (b->IsActive() == false)
            {
               DrawShape(f, xf, b2Color(0.5f, 0.5f, 0.3f));
            }
            else if (b->GetType() == b2_staticBody)
            {
               DrawShape(f, xf, b2Color(0.5f, 0.9f, 0.5f));
            }
            else if (b->GetType() == b2_kinematicBody)
            {
               DrawShape(f, xf, b2Color(0.5f, 0.5f, 0.9f));
            }
            else if (b->IsAwake() == false)
            {
               DrawShape(f, xf, b2Color(0.6f, 0.6f, 0.6f));
            }
            else
            {
               DrawShape(f, xf, b2Color(0.9f, 0.7f, 0.7f));
            }
         }
      }
   }...



I'm testing with very basic shapes which draw correctly under Box2D 2.1.x.

Code: Select all

    _playerBodyDef.type = b2_dynamicBody;
    _playerBodyDef.position.Set(100/PTM_RATIO, 100/PTM_RATIO);
    _playerBodyDef.userData = _player;
    _playerBodyDef.fixedRotation = true;
    _playerBody = _world->CreateBody(&_playerBodyDef);
   
    b2PolygonShape boxShape;
 
    boxShape.SetAsBox(_player.contentSize.width/2/PTM_RATIO, _player.contentSize.height/2/PTM_RATIO);
   
    b2FixtureDef ballShapeDef;
    ballShapeDef.shape = &boxShape;
    ballShapeDef.density = 0.5f;
    ballShapeDef.friction = 0.0f;
    ballShapeDef.restitution = 0.0f;
    _playerBody->CreateFixture(&ballShapeDef);


Has anyone been able to use GLES-Render successfully to draw debug shapes in 2.2.x?
ah... I got a bit confused with all that talk about DrawTransform. Are you saying that DrawTransform works now after you patched it? If so then wouldn't it be more appropriate to show your DrawPolygon function or something...
The only thing that looks a bit odd is those glDisable/glEnable calls, usually it would be safer for each individual DrawXXX function to do this so they can be sure the state has not been altered somewhere else.

I don't know what version of GLES-Render you started with, but just looking over the current svn version of this file it doesn't have any glVertexPointer calls...
http://code.google.com/p/box2d/source/b ... -Render.mm
If you started with the cocos2d version though I think it should have them, just sayin' 🙂

You could also try the debug draw in this xcode project, it works with Box2D 2.2.0
http://www.iforce2d.net/blog/2011-07-30
I with Bohemio is the same problems, for a solution 😎

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