Porting iOS Cocos2dx Box2d code to Android

Porting a Box2d project from iOS to Android in Cocos2dx isn't as straight forward as it could be.  If you've based your code in the default iOS template you'll probably get a few compilation errors.

Firstly, make sure you used the --Box2d option when running Next make sure you've actually got the Box2d files (I copy them from the iOS project and put it in the folder with Cocos2dx and CoocsDenshion.

You'll probably still be left with a couple of problems - Box2d.h can't be found, and if you've used the PhysicsSprite these errors:

jni/../../Classes/HelloWorldScene.cpp:48:14: error: 'm_tAnchorPointInPoints' was not declared in this scope
jni/../../Classes/HelloWorldScene.cpp:57:11: error: 'm_tAnchorPointInPoints' was not declared in this scope
jni/../../Classes/HelloWorldScene.cpp:63:5: error: 'm_tTransform' was not declared in this scope

If you get this you'll need to correct PhysicsSprite.cpp as follows:

CCAffineTransform PhysicsSprite::nodeToParentTransform(void) {
CCAffineTransform  m2;
b2Vec2 pos  = m_pBody->GetPosition();
CCPoint p = this->getAnchorPointInPoints();
float x = pos.x * PTM_RATIO;
    float y = pos.y * PTM_RATIO;
if ( isIgnoreAnchorPointForPosition() ) {
        x += p.x;
        y += p.y;
// Make matrix
    float radians = m_pBody->GetAngle();
    float c = cosf(radians);
    float s = sinf(radians);
if( ! p.equals(CCPointZero) ){
        x += c*-p.x + -s*-p.y;
        y += s*-p.x + c*-p.y;
m2 = CCAffineTransformMake( c,  s,
                                         -s,    c,
                                         x,    y );
return m2;

The changes are that I've defined CCAffineTransform and used getAnchorPointInPoints to get the anchor points directly rather than trying to access the member variables.

Check out my latest game: Brickstop on Android

1 comment: