Getting contact points on bodies in Cocos2d & Box2d

I am very new to Cocos2d and Box2d, I am after lessons and usually hack. However, I have one problem that I cannot solve.

I create 2 bodies and fixtures (in the Box2d world) and create a Contact Listener object. This object stores the contact list along with the contact point.

When two bodies collide, the contact point communicates, but this (I think) is in the world coordinate system.

My problem is that I cannot convert the contact point to a useful coordinate on both bodies.

I want to add a crack graphic in the sprite (associated with the body) to the contact point on both bodies / fixtures.

Has anyone solved this? I can keep a "contact point" relative to the "world", this is not the way to go at all.

+3
source share
2 answers

check it out . Take a look atb2Body::GetLocalPoint(const b2Vec2 &worldPoint)

+3
source

Here's how to get the world point (or points, maximum 2 points) where the collision occurred. Inside an object B2ContactListenerin BeginContactor EndContactfunction:

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 use b2Body::GetLocalPoint(const b2Vec2 &worldPoint)to transform this point of the world into a local space of objects. I just wanted to add my own answer to include all the information on how to get contact lenses in the world, as this seems to be completely excluded in the accepted answer.

+6

Source: https://habr.com/ru/post/1792889/


All Articles