检测身体的特定部分是否在box2d的与另一个体相撞 [英] Detect whether a specific part of body collided with another body in box2d

查看:156
本文介绍了检测身体的特定部分是否在box2d的与另一个体相撞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请建议有关如何可以检测一些解释或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的与另一个体相撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆