I'm very new to Cocos2d and Box2d, I
have been following tutorials and
generally hacking. However, I have one
problem I cannot solve.
I create 2 bodies and fixtures (in the Box2d world) and create a "Contact Listener" object. This object stores a list of contacts along with the "contact point".
When the two bodies collide a contact point is reported but this (I think) is in the world co-ordinate system.
My problem is I can't seem to convert the contact point to a usable co-ordinate on both of the bodies.
I want to add a crack graphic to the
sprite (connected to the body) at the
point of contact on both
bodies/fixtures.
Has anyone solved this? I may be storing the "contact point" relative to the "world" is completely the wrong way to go.
check out this. Take a look at b2Body::GetLocalPoint(const b2Vec2 &worldPoint)
Here is how to get the world-point (or points, max of 2 points are returned) where the collision took place. Within your B2ContactListener
object, in BeginContact
or EndContact
functions:
b2WorldManifold worldManifold;
contact->GetWorldManifold(&worldManifold);
std::cout << "Contact point X: " << worldManifold.points[0].x * Ascengine::Physics::PIXEL_TO_METER_RATIO << " Contact point Y: " << worldManifold.points[0].y * Ascengine::Physics::PIXEL_TO_METER_RATIO << std::endl;
From here, as Jason F mentioned, you can then use b2Body::GetLocalPoint(const b2Vec2 &worldPoint)
to convert this world point to local object space. I just wanted to add my own answer to include the whole part about getting the world contact points, since this seems to be omitted altogether in the accepted answer.