DebugDraw issues in Box2D 2.1

Hi everyone,
I have recently upgraded my Box2d library in my Cocos2d project file. Everything works great, though, I haven't been able to get the debug draw to work. I have included all the Box2D files, including Render.cpp from the testbed. I am not entirely sure how to instantiate the debug draw class; looks like it's contains only static functions. Not sure what to do. Here is what I am trying:

Code: Select all

// Debug Draw functions
      m_debugDraw = DebugDraw();
      world->SetDebugDraw(&m_debugDraw);
      
      uint32 flags = 0;
      flags += b2Draw::e_shapeBit;
        //      flags += b2DebugDraw::e_jointBit;
        //      flags += b2DebugDraw::e_aabbBit;
        //      flags += b2DebugDraw::e_pairBit;
        //      flags += b2DebugDraw::e_centerOfMassBit;
      m_debugDraw.SetFlags(flags);   


Code: Select all

Undefined symbols for architecture i386:
  "vtable for DebugDraw", referenced from:
      DebugDraw::~DebugDraw()in Game.o
      DebugDraw::DebugDraw()in Game.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture i386
collect2: ld returned 1 exit status


Bit of a cryptic error message; for me, anyway. Any help would be greatly appreciated.

Thanks,

-G. ❓
Check that your class has implemented every pure virtual function declared in the parent class's header. Pure virtual functions are those with a = 0; at the end like:

Code: Select all

/// Draw a closed polygon provided in CCW order.
virtual void DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color) = 0;
Good call, I will give that a try and report back. Thanks.
Hmm, okay, so I took a look and there doesn't appear to be any pure virtual functions... I think seeing an example of the new debug draw in action would be helpful, but hours of searching have revealed nothing. I had a look at the one that comes with the Testbed, but haven't quite figured out how to apply that to my project.
Okay! So I've gotten it working to a point. I knuckled down on it last night, and have realized that I needed to implement the pure virtual methods, like you said. I had been looking at Render.h, but realised that that just inherits from b2Draw, where the pure virtual methods exist, so that threw me off.

Now, just a few OGL issue.... Which is string, because I am including both the OpenGLES framework and the OpenGL framework that came with the Box2d sample project for XCode. It seems like there are a few issues regarding linking... Perhaps the debug draw methods that came with the sample were not made for OpenGLES, but instead for solely OpenGL?

In any case, here are the linking issues from the compiler:

Code: Select all

Ld /Users/Garrett/Library/Developer/Xcode/DerivedData/ForceFieldApp-genccvsubmiuwwdxpvoziloisibp/Build/Products/Debug-iphoneos/ForceFieldApp.app/ForceFieldApp normal armv7
    cd /Users/Garrett/ForceFieldApp
    setenv IPHONEOS_DEPLOYMENT_TARGET 4.3
    setenv PATH "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch armv7 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.1.sdk -L/Users/Garrett/Library/Developer/Xcode/DerivedData/ForceFieldApp-genccvsubmiuwwdxpvoziloisibp/Build/Products/Debug-iphoneos -L/Users/Garrett/ForceFieldApp -F/Users/Garrett/Library/Developer/Xcode/DerivedData/ForceFieldApp-genccvsubmiuwwdxpvoziloisibp/Build/Products/Debug-iphoneos -F/Users/Garrett/ForceFieldApp -filelist /Users/Garrett/Library/Developer/Xcode/DerivedData/ForceFieldApp-genccvsubmiuwwdxpvoziloisibp/Build/Intermediates/ForceFieldApp.build/Debug-iphoneos/ForceFieldApp.build/Objects-normal/armv7/ForceFieldApp.LinkFileList -dead_strip -lz -cxx -gcc_flags -miphoneos-version-min=4.3 -framework OpenGLES -framework GLKit -framework QuartzCore -framework OpenAL -framework AudioToolbox -framework AVFoundation -framework UIKit -framework Foundation -framework CoreGraphics -lGLUI -framework OpenGL -o /Users/Garrett/Library/Developer/Xcode/DerivedData/ForceFieldApp-genccvsubmiuwwdxpvoziloisibp/Build/Products/Debug-iphoneos/ForceFieldApp.app/ForceFieldApp

clang: warning: argument unused during compilation: '-cxx'
clang: warning: argument unused during compilation: '-gcc_flags'
ld: warning: ignoring file /Users/Garrett/ForceFieldApp/libGLUI.a, file was built for archive which is not the architecture being linked (armv7)
ld: warning: ignoring file /Users/Garrett/ForceFieldApp/OpenGL.framework/OpenGL, missing required architecture armv7 in file
Undefined symbols for architecture armv7:
  "_glEnd", referenced from:
      __ZN9DebugDraw11DrawPolygonEPK6b2Vec2iRK7b2Color in Game.o
      __ZN9DebugDraw16DrawSolidPolygonEPK6b2Vec2iRK7b2Color in Game.o
      __ZN9DebugDraw10DrawCircleERK6b2Vec2fRK7b2Color in Game.o
      __ZN9DebugDraw15DrawSolidCircleERK6b2Vec2fS2_RK7b2Color in Game.o
      __ZN9DebugDraw11DrawSegmentERK6b2Vec2S2_RK7b2Color in Game.o
      __ZN9DebugDraw13DrawTransformERK11b2Transform in Game.o
  "_glColor3f", referenced from:
      __ZN9DebugDraw11DrawPolygonEPK6b2Vec2iRK7b2Color in Game.o
      __ZN9DebugDraw10DrawCircleERK6b2Vec2fRK7b2Color in Game.o
      __ZN9DebugDraw11DrawSegmentERK6b2Vec2S2_RK7b2Color in Game.o
      __ZN9DebugDraw13DrawTransformERK11b2Transform in Game.o
  "_glBegin", referenced from:
      __ZN9DebugDraw11DrawPolygonEPK6b2Vec2iRK7b2Color in Game.o
      __ZN9DebugDraw16DrawSolidPolygonEPK6b2Vec2iRK7b2Color in Game.o
      __ZN9DebugDraw10DrawCircleERK6b2Vec2fRK7b2Color in Game.o
      __ZN9DebugDraw15DrawSolidCircleERK6b2Vec2fS2_RK7b2Color in Game.o
      __ZN9DebugDraw11DrawSegmentERK6b2Vec2S2_RK7b2Color in Game.o
      __ZN9DebugDraw13DrawTransformERK11b2Transform in Game.o
  "_glVertex2f", referenced from:
      __ZN9DebugDraw11DrawPolygonEPK6b2Vec2iRK7b2Color in Game.o
      __ZN9DebugDraw16DrawSolidPolygonEPK6b2Vec2iRK7b2Color in Game.o
      __ZN9DebugDraw10DrawCircleERK6b2Vec2fRK7b2Color in Game.o
      __ZN9DebugDraw15DrawSolidCircleERK6b2Vec2fS2_RK7b2Color in Game.o
      __ZN9DebugDraw11DrawSegmentERK6b2Vec2S2_RK7b2Color in Game.o
      __ZN9DebugDraw13DrawTransformERK11b2Transform in Game.o
ld: symbol(s) not found for architecture armv7
clang: error: linker command failed with exit code 1 (use -v to see invocation)



Any help would be awesome!
The iPhone uses OpenGL ES which does not have glBegin/glEnd/glVertex functions. You need to put the vertices in an array and call glVertexPointer and glDrawArrays. Take a look at the debug draw implementation for iPhone in the contributions folder of Box2D's source code:
http://code.google.com/p/box2d/source/b ... -Render.mm

See also:
http://maniacdev.com/2009/05/big-list-o ... opengl-es/
Thanks for the tip, this is exactly what I needed. So, I'm at the point where it's compiling, but it doesn't draw the debug data. I compared my code to the testbed, and honestly I can't find anywhere in the testbed files where m_debugData (or b2Draw class, technically) is instantiated.

I checked the b2World.cpp file, and it looks like I am setting a null b2Draw object to the world.

Code: Select all

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



Mine just returns at that point. I put a break at the same point in the testbed file, and it passes the null test just fine; but like I say, I cant see where that object is being instantiated, or how because it is an abstract class isn't it?

Here is the relevant code in "Game.mm":

Code: Select all

// Game.mm - init
- (void) init {
. . .
      // Debug Draw functions
        DebugDraw d;
      world->SetDebugDraw(&d);
      
      uint32 flags = 0;
      flags += DebugDraw::e_shapeBit;
      flags += DebugDraw::e_jointBit;
        flags += DebugDraw::e_aabbBit;
        flags += DebugDraw::e_pairBit;
        flags += DebugDraw::e_centerOfMassBit;
      d.SetFlags(flags);
. . .
}



// Game.h

#import "cocos2d.h"
#import "Box2D.h"
#import "Render.h"
#import "Globals.h"
#import "MyContactListener.h"
#import "Points.h"
#import "UI.h"
#import "Items.h"
#import "Matrix.h"
#import "VirusOrbit.h"

@class UI;
@class Items;

@interface Game : CCLayer
{
   b2World* world;
   DebugDraw m_debugDraw;
        . . .
}

. . .

void DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color);

void DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color);

void DrawCircle(const b2Vec2& center, float32 radius, const b2Color& color);

void DrawSolidCircle(const b2Vec2& center, float32 radius, const b2Vec2& axis, const b2Color& color);

void DrawSegment(const b2Vec2& p1, const b2Vec2& p2, const b2Color& color);

void DrawTransform(const b2Transform& xf);

void DrawPoint(const b2Vec2& p, float32 size, const b2Color& color);

void DrawString(int x, int y, const char* string, ...);

void DrawAABB(b2AABB* aabb, const b2Color& color);

@end

This makes a temporary debug draw object on the stack:

Code: Select all

- (void) init {
      DebugDraw d;
      world->SetDebugDraw(&d);

      // d goes out of scope here and is destructed
}

Try this...

Code: Select all

//global scope
DebugDraw* d;

- (void) init {
      world->SetDebugDraw( &d );
}

or this...

Code: Select all

- (void) init {
      DebugDraw* d = new DebugDraw();
      world->SetDebugDraw( d );
}

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