Getting contact points on bodies in Cocos2d & Box2d

醉酒当歌 提交于 2019-12-10 19:11:48

问题


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.


回答1:


check out this. Take a look at b2Body::GetLocalPoint(const b2Vec2 &worldPoint)




回答2:


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.



来源:https://stackoverflow.com/questions/5033498/getting-contact-points-on-bodies-in-cocos2d-box2d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!