问题描述
请建议有关如何可以检测一些解释或code 一个身体的特定部位之间的碰撞与另一个身体中的Box2D与libgdx.I是能够检测两个机构之间简单的碰撞用联系监听这里提到但我也想检查身体的哪个部分都ovelapping
Kindly suggest some explanation or code regarding how can i detect collision between a specific part of one body with another body in box2d with libgdx.I am able to detect simple collision between two bodies using Contact Listener as mentioned hereBut I also want to check which part of bodies are ovelapping.
谢谢,
推荐答案
的 ContactListener
为您提供联系
作为回调参数。这些接触会告诉你哪些灯具
并通过 contact.getFixtureA()
和接触碰撞。 getFixtureB()
。
The ContactListener
provides you with Contact
as a callback parameter. Those contacts will tell you which Fixtures
did collide via contact.getFixtureA()
and contact.getFixtureB()
.
什么人通常以找出他们的身体的一部分相撞,是有几个灯具
通过 body.createFixture(编译它们.. 。)
。
What people usually do to find out which part of their bodies collided, is to build them with several Fixtures
via body.createFixture(...)
.
您可以用灯具设置
和灯具
,以及对机身
用户数据。 setUserData来() body.setUserData()
。你既可以保存您的夹具别的地方,并通过比较contact.getFixtureA()== xxx.savedFixture
。
You can set user data on Fixture
as well as on Body
with fixture.setUserData()
and body.setUserData()
. You could either save your fixture somewhere else and compare via contact.getFixtureA() == xxx.savedFixture
.
这可能对你比如像下面的实体:
That might be in your entity for example like the following:
public class Player {
public Fixture arm;
// create the player body and store the arm fixture
body.setUserData(this);
arm = body.createFixture(...);
}
后来的后来,你可以在你的联系人听众做到这一点:
Then later you can do this in your contact listener:
public void beginContact(Contact contact) {
if (contact.getFixtureA().getBody().getUserData().getClass().equals(Player.class)) {
if (contact.getFixtureA() == ((Player)contact.getFixtureA().getBody().getUserData()).arm == contact.getFixtureA()) {
// the arm collided with something
}
}
}
或者,你可能只是添加像某些用户数据fixture.setUserData(ARM)
然后你可以很容易地检查。在您的联系人回调处理程序。
Or you might just add some user data like fixture.setUserData("arm")
which you can then easily check. In your contact callback handler.
这篇关于检测身体的特定部分是否在box2d的与另一个体相撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!