Search code examples
cocos2d-iphonebox2d

Box2D and Cocos2d-x coordinates


I'm using Box2D and cocos2d-x but I can't figure the coordinate system out.

I know cocos2d-x uses P(0,0) as the bottom left part of the screen. And Box2D uses P(0, 0) as the center of it's world.

I'm using debug draw for Box2D.

When creating a body with a polygon shape attached to it at P(0,0) in Box2D coordinates it shows up on the bottom left part of the screen.

Why doesn't it show up in the center?


Solution

  • When positioning any b2Body, use Sprite::getBoundingBox().getMidX() for the horizontal center, and for the vertical center use Sprite::getBoundingBox().getMidY().

    You might want to wrap those calls in a Node::convertToWorldSpace(midX, midY) to make sure that your center coordinates are expanded into the cocos2d-x world coordinate system and hence the Box2D world coordinate space as it has to be consistent.