Box2d on iPhone (again)

Hi,

I have Box2d working in XCode on the iPhone Simulator. With this target it "just works", although the test harness (mostly in Render.cpp) is not compatible with OpenGL ES 1.1 and it requires GLUT, which isn't present on that platform. I hacked the DebugDraw class enough to compile and draw polygons and circles (using glDrawArray, which means its allocating a fair bit, but it seems fast enough). I haven't tried all the test cases but CCDTest works more or less as expected. I haven't quite got DebugDraw::DrawString function working. It's very platform-specific - I'm not sure anyone wants to see Objective-C code in there - and I haven't looked at the glui library yet.

My application's rendering code is working however, and I wanted to try it on the device, but the compile fails in b2Math.h ("finite is not declared in this scope").

The main difference between compiling for the device and the simulator is that the device is ARM and the simulator is i386. I'm not sure which library I should be looking for to resolve this symbol.

Any suggestions?
I forget what the solution to this particular problem is, but in general, we should probably put together an iPhone base project for Box2d, since everyone that tries usually has to hack away for a while before gettng it to work.

Does anyone have a simple base to work from? Otherwise I'll try to put one together next week, as I'm about to start an iphone game and I'll have to figure it out anyways.

Also, has anyone figures out a series of source edits that we could integrate into the main line that allow compilation both normally and for iPhone sdk without further changes? The finite issue can probably be worked around with a preprocessor macro of some sort.
It seems that C++ support for IEEE NaN and Infinity is complicated and inconsistently implemented.

C99 deprecates finite() and replaces it with isfinite(), but ISO C++ doesn't address that. While Apple's i386 compiler has legacy support (it has comments indicating the function is deprecated), the ARM compiler apparently doesn't.

I changed the include from <cmath> to "math.h" and modified the definition of b2IsValid and it seems to have solved my compilation problem (although I haven't yet tested on my device).

Code:
/// This function is used to ensure that a floating point number is
/// not a NaN or infinity.
inline bool b2IsValid(float32 x)
{
#ifdef _MSC_VER
   return _finite(x) != 0;
#else
   return isfinite(x);
#endif
}


I think it would be nice to have some #defines to deal with GLES and GLUT, rather than tagging to specific platforms, but I'm not sure I will have time to do that this week.
swart wrote:
I think it would be nice to have some #defines to deal with GLES and GLUT, rather than tagging to specific platforms, but I'm not sure I will have time to do that this week.

I'm not sure I follow - are you thinking of trying to get the whole testbed to compile as-is on iPhone, as well? I think that might be difficult, as to my knowledge nobody has managed to get GLUT (even the ES version) working on the iPhone, and the testbed also has a significant glui dependency which will be impossible to translate, as well.

To me, it seems that a better approach would be to have an iPhone-specific project in the Build or Examples directory that has a customized version of the testbed files (at least the framework ones) and then get the rest of the engine in line so that it can compile from that project with no problems.

BTW - ran into an infuriating bug the other day with Box2d + iPhone where things were not acting right on the phone, though they were fine in the simulator, and contacts would occasionally explode and everything was generally going nuts. It turned out to be because I had checked -O3 optimization; once I switched to -Os instead, things went back to normal. To my knowledge the optimization level should almost never affect the actual behavior of a program, so something with the ARM optimizer is clearly messed up. I'm not sure what, but if anyone else runs into trouble, try playing with the optimization settings (both per-project and per-target).
I just ran across this same problem last night. I changed b2math.h to include math.h and float.h instead of cmath and cfloat, and I didn't have any other problems.

The optimization problem is actually noted in math.h. It says that by specificying -fast-math you are essentially telling the compiler that NaN doesn't exist (or at least won't happen).
change cmath to math.h and in b2Math.h the bsIsValid() function to

inline bool b2IsValid(float32 x)
{
return !isnan(x) && !isinf(x);
}
I just checked out the latest version from the SVN, and tried to compile the iPhone build using Xcode (I've tried targetting the simulator and the actual device). I'm getting a lot of errors and warnings:

Code:
Line Location b2Distance.cpp:43: warning: comparison is always true due to limited range of data type
Line Location b2Distance.cpp:43: warning: comparison is always true due to limited range of data type
Line Location b2Distance.cpp:43: warning: comparison is always true due to limited range of data type
Line Location b2Distance.cpp:43: warning: comparison is always true due to limited range of data type
Line Location b2Distance.cpp:43: warning: comparison is always true due to limited range of data type
Line Location b2Distance.cpp:43: warning: comparison is always true due to limited range of data type
Line Location b2Distance.cpp:43: warning: comparison is always true due to limited range of data type
Line Location b2Distance.cpp:43: warning: comparison is always true due to limited range of data type
Line Location b2Distance.cpp:43: warning: comparison is always true due to limited range of data type
Line Location b2World.h:126: note: candidates are: int32 b2World::Query(const b2AABB&, b2Fixture**, int32)
Line Location b2WorldCallbacks.h:30: error: forward declaration of 'const struct b2ContactPoint'
Line Location b2WorldCallbacks.h:30: error: forward declaration of 'const struct b2ContactPoint'
Line Location b2WorldCallbacks.h:30: error: forward declaration of 'const struct b2ContactPoint'
Line Location b2WorldCallbacks.h:30: error: forward declaration of 'const struct b2ContactPoint'
Line Location b2WorldCallbacks.h:30: error: forward declaration of 'const struct b2ContactPoint'
Line Location b2WorldCallbacks.h:30: error: forward declaration of 'const struct b2ContactPoint'
Line Location b2WorldCallbacks.h:30: error: forward declaration of 'const struct b2ContactPoint'
Line Location b2WorldCallbacks.h:30: error: forward declaration of 'const struct b2ContactPoint'
Line Location b2WorldCallbacks.h:30: error: forward declaration of 'const struct b2ContactPoint'
Line Location b2WorldCallbacks.h:30: error: forward declaration of 'const struct b2ContactPoint'
Line Location b2WorldCallbacks.h:30: error: forward declaration of 'const struct b2ContactPoint'
Line Location b2WorldCallbacks.h:30: error: forward declaration of 'const struct b2ContactPoint'
Line Location b2WorldCallbacks.h:30: error: forward declaration of 'const struct b2ContactPoint'
Line Location b2WorldCallbacks.h:30: error: forward declaration of 'const struct b2ContactPoint'
Line Location b2WorldCallbacks.h:30: error: forward declaration of 'const struct b2ContactPoint'
Line Location b2WorldCallbacks.h:48: note:  virtual void b2DestructionListener::SayGoodbye(b2Fixture*)
Line Location b2WorldCallbacks.h:48: note:  virtual void b2DestructionListener::SayGoodbye(b2Fixture*)
Line Location b2WorldCallbacks.h:48: note:  virtual void b2DestructionListener::SayGoodbye(b2Fixture*)
Line Location b2WorldCallbacks.h:48: note:  virtual void b2DestructionListener::SayGoodbye(b2Fixture*)
Line Location b2WorldCallbacks.h:48: note:  virtual void b2DestructionListener::SayGoodbye(b2Fixture*)
Line Location CCDTest.h:224: error: 'm_stepCount' was not declared in this scope
Line Location CollisionProcessing.h:130: error: 'struct ContactPoint' has no member named 'fixtureA'
Line Location CollisionProcessing.h:131: error: 'struct ContactPoint' has no member named 'fixtureB'
Line Location ContactCallbackTest.h:32: error: 'const struct ContactPoint' has no member named 'fixtureA'
Line Location ContactCallbackTest.h:32: error: 'const struct ContactPoint' has no member named 'fixtureA'
Line Location ContactCallbackTest.h:33: error: 'const struct ContactPoint' has no member named 'fixtureA'
Line Location ContactCallbackTest.h:33: error: 'const struct ContactPoint' has no member named 'fixtureA'
Line Location ContactCallbackTest.h:33: error: 'const struct ContactPoint' has no member named 'fixtureB'
Line Location ContactCallbackTest.h:33: error: 'const struct ContactPoint' has no member named 'fixtureB'
Line Location ContactCallbackTest.h:34: error: 'const struct ContactPoint' has no member named 'fixtureA'
Line Location ContactCallbackTest.h:34: error: 'const struct ContactPoint' has no member named 'fixtureA'
Line Location ContactCallbackTest.h:34: error: 'const struct ContactPoint' has no member named 'fixtureB'
Line Location ContactCallbackTest.h:34: error: 'const struct ContactPoint' has no member named 'fixtureB'
Line Location ContactCallbackTest.h:199: error: 'struct ContactPoint' has no member named 'fixtureA'
Line Location ContactCallbackTest.h:199: error: 'struct ContactPoint' has no member named 'fixtureB'
Line Location ContactCallbackTest.h:215: error: 'struct ContactPoint' has no member named 'fixtureA'
Line Location ContactCallbackTest.h:215: error: 'struct ContactPoint' has no member named 'fixtureB'
Line Location ContactCallbackTest.h:226: error: 'struct ContactPoint' has no member named 'fixtureA'
Line Location ContactCallbackTest.h:227: error: 'struct ContactPoint' has no member named 'fixtureB'
Line Location iPhoneTest.h:113: note:   because the following virtual functions are pure within 'DestructionListener':
Line Location iPhoneTest.h:113: note:   because the following virtual functions are pure within 'DestructionListener':
Line Location iPhoneTest.h:113: note:   because the following virtual functions are pure within 'DestructionListener':
Line Location iPhoneTest.h:113: note:   because the following virtual functions are pure within 'DestructionListener':
Line Location iPhoneTest.h:113: note:   because the following virtual functions are pure within 'DestructionListener':
Line Location iPhoneTest.h:195: error: cannot declare field 'Test::m_destructionListener' to be of abstract type 'DestructionListener'
Line Location iPhoneTest.h:195: error: cannot declare field 'Test::m_destructionListener' to be of abstract type 'DestructionListener'
Line Location iPhoneTest.h:195: error: cannot declare field 'Test::m_destructionListener' to be of abstract type 'DestructionListener'
Line Location iPhoneTest.h:195: error: cannot declare field 'Test::m_destructionListener' to be of abstract type 'DestructionListener'
Line Location iPhoneTest.h:195: error: cannot declare field 'Test::m_destructionListener' to be of abstract type 'DestructionListener'
Line Location iPhoneTest.mm:54: error: invalid use of undefined type 'const struct b2ContactPoint'
Line Location iPhoneTest.mm:55: error: invalid use of undefined type 'const struct b2ContactPoint'
Line Location iPhoneTest.mm:56: error: invalid use of undefined type 'const struct b2ContactPoint'
Line Location iPhoneTest.mm:57: error: invalid use of undefined type 'const struct b2ContactPoint'
Line Location iPhoneTest.mm:58: error: invalid use of undefined type 'const struct b2ContactPoint'
Line Location iPhoneTest.mm:72: error: invalid use of undefined type 'const struct b2ContactPoint'
Line Location iPhoneTest.mm:73: error: invalid use of undefined type 'const struct b2ContactPoint'
Line Location iPhoneTest.mm:74: error: invalid use of undefined type 'const struct b2ContactPoint'
Line Location iPhoneTest.mm:75: error: invalid use of undefined type 'const struct b2ContactPoint'
Line Location iPhoneTest.mm:76: error: invalid use of undefined type 'const struct b2ContactPoint'
Line Location iPhoneTest.mm:90: error: invalid use of undefined type 'const struct b2ContactPoint'
Line Location iPhoneTest.mm:91: error: invalid use of undefined type 'const struct b2ContactPoint'
Line Location iPhoneTest.mm:92: error: invalid use of undefined type 'const struct b2ContactPoint'
Line Location iPhoneTest.mm:93: error: invalid use of undefined type 'const struct b2ContactPoint'
Line Location iPhoneTest.mm:94: error: invalid use of undefined type 'const struct b2ContactPoint'
Line Location iPhoneTest.mm:163: error: no matching function for call to 'b2World::Query(b2AABB&, b2Shape* [10], const int32&)'
Line Location iPhoneTest.mm:167: error: 'class b2Shape' has no member named 'GetBody'
Line Location iPhoneTest.mm:173: error: 'class b2Shape' has no member named 'GetBody'
Line Location iPhoneTest.mm:287: error: 'class b2Body' has no member named 'CreateShape'
Yes, I got this as well when I tried to compile the iphone build this morning.

Certainly following a quick look it appears there is no definition for b2ContactPoint.


TheSamuraiElephant wrote:
I just checked out the latest version from the SVN, and tried to compile the iPhone build using Xcode (I've tried targetting the simulator and the actual device). I'm getting a lot of errors and warnings:

Code:
Line Location b2Distance.cpp:43: warning: comparison is always true due to limited range of data type
Line Location b2Distance.cpp:43: warning: comparison is always true due to limited range of data type
Line Location b2Distance.cpp:43: warning: comparison is always true due to limited range of data type
Line Location b2Distance.cpp:43: warning: comparison is always true due to limited range of data type
Line Location b2Distance.cpp:43: warning: comparison is always true due to limited range of data type
Line Location b2Distance.cpp:43: warning: comparison is always true due to limited range of data type
Line Location b2Distance.cpp:43: warning: comparison is always true due to limited range of data type
Line Location b2Distance.cpp:43: warning: comparison is always true due to limited range of data type
Line Location b2Distance.cpp:43: warning: comparison is always true due to limited range of data type
Line Location b2World.h:126: note: candidates are: int32 b2World::Query(const b2AABB&, b2Fixture**, int32)
Line Location b2WorldCallbacks.h:30: error: forward declaration of 'const struct b2ContactPoint'
Line Location b2WorldCallbacks.h:30: error: forward declaration of 'const struct b2ContactPoint'
Line Location b2WorldCallbacks.h:30: error: forward declaration of 'const struct b2ContactPoint'
Line Location b2WorldCallbacks.h:30: error: forward declaration of 'const struct b2ContactPoint'
Line Location b2WorldCallbacks.h:30: error: forward declaration of 'const struct b2ContactPoint'
Line Location b2WorldCallbacks.h:30: error: forward declaration of 'const struct b2ContactPoint'
Line Location b2WorldCallbacks.h:30: error: forward declaration of 'const struct b2ContactPoint'
Line Location b2WorldCallbacks.h:30: error: forward declaration of 'const struct b2ContactPoint'
Line Location b2WorldCallbacks.h:30: error: forward declaration of 'const struct b2ContactPoint'
Line Location b2WorldCallbacks.h:30: error: forward declaration of 'const struct b2ContactPoint'
Line Location b2WorldCallbacks.h:30: error: forward declaration of 'const struct b2ContactPoint'
Line Location b2WorldCallbacks.h:30: error: forward declaration of 'const struct b2ContactPoint'
Line Location b2WorldCallbacks.h:30: error: forward declaration of 'const struct b2ContactPoint'
Line Location b2WorldCallbacks.h:30: error: forward declaration of 'const struct b2ContactPoint'
Line Location b2WorldCallbacks.h:30: error: forward declaration of 'const struct b2ContactPoint'
Line Location b2WorldCallbacks.h:48: note:  virtual void b2DestructionListener::SayGoodbye(b2Fixture*)
Line Location b2WorldCallbacks.h:48: note:  virtual void b2DestructionListener::SayGoodbye(b2Fixture*)
Line Location b2WorldCallbacks.h:48: note:  virtual void b2DestructionListener::SayGoodbye(b2Fixture*)
Line Location b2WorldCallbacks.h:48: note:  virtual void b2DestructionListener::SayGoodbye(b2Fixture*)
Line Location b2WorldCallbacks.h:48: note:  virtual void b2DestructionListener::SayGoodbye(b2Fixture*)
Line Location CCDTest.h:224: error: 'm_stepCount' was not declared in this scope
Line Location CollisionProcessing.h:130: error: 'struct ContactPoint' has no member named 'fixtureA'
Line Location CollisionProcessing.h:131: error: 'struct ContactPoint' has no member named 'fixtureB'
Line Location ContactCallbackTest.h:32: error: 'const struct ContactPoint' has no member named 'fixtureA'
Line Location ContactCallbackTest.h:32: error: 'const struct ContactPoint' has no member named 'fixtureA'
Line Location ContactCallbackTest.h:33: error: 'const struct ContactPoint' has no member named 'fixtureA'
Line Location ContactCallbackTest.h:33: error: 'const struct ContactPoint' has no member named 'fixtureA'
Line Location ContactCallbackTest.h:33: error: 'const struct ContactPoint' has no member named 'fixtureB'
Line Location ContactCallbackTest.h:33: error: 'const struct ContactPoint' has no member named 'fixtureB'
Line Location ContactCallbackTest.h:34: error: 'const struct ContactPoint' has no member named 'fixtureA'
Line Location ContactCallbackTest.h:34: error: 'const struct ContactPoint' has no member named 'fixtureA'
Line Location ContactCallbackTest.h:34: error: 'const struct ContactPoint' has no member named 'fixtureB'
Line Location ContactCallbackTest.h:34: error: 'const struct ContactPoint' has no member named 'fixtureB'
Line Location ContactCallbackTest.h:199: error: 'struct ContactPoint' has no member named 'fixtureA'
Line Location ContactCallbackTest.h:199: error: 'struct ContactPoint' has no member named 'fixtureB'
Line Location ContactCallbackTest.h:215: error: 'struct ContactPoint' has no member named 'fixtureA'
Line Location ContactCallbackTest.h:215: error: 'struct ContactPoint' has no member named 'fixtureB'
Line Location ContactCallbackTest.h:226: error: 'struct ContactPoint' has no member named 'fixtureA'
Line Location ContactCallbackTest.h:227: error: 'struct ContactPoint' has no member named 'fixtureB'
Line Location iPhoneTest.h:113: note:   because the following virtual functions are pure within 'DestructionListener':
Line Location iPhoneTest.h:113: note:   because the following virtual functions are pure within 'DestructionListener':
Line Location iPhoneTest.h:113: note:   because the following virtual functions are pure within 'DestructionListener':
Line Location iPhoneTest.h:113: note:   because the following virtual functions are pure within 'DestructionListener':
Line Location iPhoneTest.h:113: note:   because the following virtual functions are pure within 'DestructionListener':
Line Location iPhoneTest.h:195: error: cannot declare field 'Test::m_destructionListener' to be of abstract type 'DestructionListener'
Line Location iPhoneTest.h:195: error: cannot declare field 'Test::m_destructionListener' to be of abstract type 'DestructionListener'
Line Location iPhoneTest.h:195: error: cannot declare field 'Test::m_destructionListener' to be of abstract type 'DestructionListener'
Line Location iPhoneTest.h:195: error: cannot declare field 'Test::m_destructionListener' to be of abstract type 'DestructionListener'
Line Location iPhoneTest.h:195: error: cannot declare field 'Test::m_destructionListener' to be of abstract type 'DestructionListener'
Line Location iPhoneTest.mm:54: error: invalid use of undefined type 'const struct b2ContactPoint'
Line Location iPhoneTest.mm:55: error: invalid use of undefined type 'const struct b2ContactPoint'
Line Location iPhoneTest.mm:56: error: invalid use of undefined type 'const struct b2ContactPoint'
Line Location iPhoneTest.mm:57: error: invalid use of undefined type 'const struct b2ContactPoint'
Line Location iPhoneTest.mm:58: error: invalid use of undefined type 'const struct b2ContactPoint'
Line Location iPhoneTest.mm:72: error: invalid use of undefined type 'const struct b2ContactPoint'
Line Location iPhoneTest.mm:73: error: invalid use of undefined type 'const struct b2ContactPoint'
Line Location iPhoneTest.mm:74: error: invalid use of undefined type 'const struct b2ContactPoint'
Line Location iPhoneTest.mm:75: error: invalid use of undefined type 'const struct b2ContactPoint'
Line Location iPhoneTest.mm:76: error: invalid use of undefined type 'const struct b2ContactPoint'
Line Location iPhoneTest.mm:90: error: invalid use of undefined type 'const struct b2ContactPoint'
Line Location iPhoneTest.mm:91: error: invalid use of undefined type 'const struct b2ContactPoint'
Line Location iPhoneTest.mm:92: error: invalid use of undefined type 'const struct b2ContactPoint'
Line Location iPhoneTest.mm:93: error: invalid use of undefined type 'const struct b2ContactPoint'
Line Location iPhoneTest.mm:94: error: invalid use of undefined type 'const struct b2ContactPoint'
Line Location iPhoneTest.mm:163: error: no matching function for call to 'b2World::Query(b2AABB&, b2Shape* [10], const int32&)'
Line Location iPhoneTest.mm:167: error: 'class b2Shape' has no member named 'GetBody'
Line Location iPhoneTest.mm:173: error: 'class b2Shape' has no member named 'GetBody'
Line Location iPhoneTest.mm:287: error: 'class b2Body' has no member named 'CreateShape'
I compiled the version in the "Tag" directory later, rather than trunk - and the version there works fine!
Hopefully this post isn't out of place here. I've managed to compile box2D on the iphone but, how can I make a b2World an Objective-C instance variable?


In my objective-c header, I have something like :
Code:
#import <UIKit/UIKit.h>
#import "Box2D.h"

@interface MyViewController : UIViewController {
   b2World *worldPointer;
}

@property b2World *worldPointer;

@end


I then try to setup the pointer to the b2World object like this in my implementation:
Code:
        //setup the world
   b2World world(worldAABB, gravity, doSleep);
   
   //setup a pointer to the world object
   worldPointer = &world;


That compiles fine with no problems, but later, when I want to call the Step function on the b2World the iphone crashes.
Here is how I'm calling it:

Code:
float32 timeStep =  1.0f / 60.0f;
   int32 iterations = 8;
   worldPointer->Step(timeStep, iterations, iterations);


After this is called the program just crashes.

Am I setting up the world pointer incorrectly? How should I set up the b2World as an instance variable in Objective-C?

Cheers!

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