Relationship between Box2D and Cocos2dx

I’m having trouble getting Box2D to do simple collisions and debug drawing.

I have Debug Drawing up and running, but the problem is that it doesn’t show up where the actual b2Fixture is located in the Box2D world.

As a result I can see two Fixtures colliding when looking at the output of the debug draw layer, but no contacts are fired from Box2D, so obviously something is wrong.

I think all my problems are contingent upon misunderstanding the Box2D API.
Here are my questions:

  1. What coordinate system does Box2D use?

  2. How should Fixtures be positioned in world space, to what is part of a b2Body is a position relative to (center, bottom-left etc.)?

  1. If needed, how to correctly continuously update Box2D positions using SetTransform() in the update() function?

Thank you!

I’d try on Box2D forums for better results. I don’t use Box2D. I tend to use our built in physics (based on Chipmunk) and if my needs are ever more advanced, I just use Chipmunk. This has happened 1 time.

There are also a number of examples in cpp-tests.

Hi. we use Box2D for collision detection and debug draw is work properly.
1- Box2d coordinate system is like cocos2d-x, if you add your debugdraw node to a scene, (0,0) Box2d world is (0,0) scene.

2- center

3-In your update method, first you need a world position of your node: auto p = yourNode->convertToWorldSpace(Vec2(0, 0));
second you need transform your b2body: yourBody->SetTransform(b2Vec2(p.x / PTM_RATIO, p.y / PTM_RATIO), CC_DEGREES_TO_RADIANS(-yourNode->getRotaion()));

PTM_RATIO is the point to meter ratio. if you use Box2d then you know about it!

1 Like

If I have anchor points on my node that is not at Vec2(0.5,0.5) do I have use SetTransform() differently to account for the node that has non standard anchor points?

Thanks alot!

You need to change Vec2(0,0) in convertToWorldSpace method. if you have a sprite with (0,1) anchor point you might use Vec2(-yourSprite->getContentSize().x/2,yourSprite->getContentSize().y/2).
I am not sure about the negative or positive sign but I assume you get the idea.

Thanks for sorting this out for me!
Everything works :slight_smile: