检测Box2d和libgdx(android)中两个物体之间的碰撞

时间:2022-09-10 21:23:29

I am new to libgdx and Box2d engine and I'm developing a game using same .I want to detect collision between two bodies to perform some function.But I'm not aware about the optimum way to do that and also want get point of collision.Kindly provide some suggestion with code.I have already implemented ContactListener but to no avail.

我是libgdx和Box2d引擎的新手,我正在开发一个使用它的游戏。我想检测两个物体之间的碰撞以执行某些功能。但我不知道这样做的最佳方法,也希望得到点使用code.Kindly提供一些建议。我已经实现了ContactListener,但无济于事。

I'm using this code as a reference.

我正在使用此代码作为参考。

Thanks

谢谢

1 个解决方案

#1


5  

You already did it the right way to create and set a ContactListener... (for a general setup, the libgdx wiki is great: https://github.com/libgdx/libgdx/wiki/box2d#contact-listeners)

您已经以正确的方式创建并设置了ContactListener ...(对于一般设置,libgdx wiki很棒:https://github.com/libgdx/libgdx/wiki/box2d#contact-listeners)

If you now want to handle the specific contacts, you for exmaple need to add some implementation in the beginContact(); method of your listener. The beginContact(); method contains a Contact instance, which holds all you need:

如果您现在想要处理特定的联系人,那么您需要在beginContact()中添加一些实现;你的听众的方法。 beginContact(); method包含一个Contact实例,它可以满足您的所有需求:

  • FixtureA - the first fixture of a contact
  • FixtureA - 联系人的第一个夹具
  • FixtureB - the fixture, which FixtureA has collided with
  • FixtureB - FixtureA碰撞的夹具
  • WorldManifold - an object that holds the collision points etc
  • WorldManifold - 一个保持碰撞点等的对象

Through the fixtures you can access the bodies and the actors which you are drawing. The connection to your Actor can be done through the body.setUserData(actor); method.

通过灯具,您可以访问您正在绘制的实体和演员。可以通过body.setUserData(actor)完成与Actor的连接;方法。

Now you need to decide on how to find out the correct collisions. You can either work with sensors, which are box2d fixtures that just act as a sensor. That means that when an object collides with a sensor, it won't bounce, but fall through it instead. But you then are able to detect this contact within the listener.

现在您需要决定如何找出正确的碰撞。您可以使用传感器,这些传感器只是作为传感器的box2d灯具。这意味着当一个物体与一个传感器发生碰撞时,它不会反弹,而是会反过来掉落。但是,您可以在侦听器中检测到此联系人。

Also, it might be a good idea to add some kind of GameObjectType to your actors. Imagine you create a jumping game where the player jumps from platform to platform with water below. Then you would create your actors with types like PLAYER, WATER, PLATFORM ... through the getUserData() method of the box2d bodies yu can now access the Actors and compare their types.

此外,向您的actor添加某种GameObjectType可能是个好主意。想象一下,你创造了一个跳跃游戏,玩家从平台跳到平台,下面有水。然后你会用PLAYER,WATER,PLATFORM等类型创建你的演员......通过box2d体的getUserData()方法你现在可以访问Actors并比较它们的类型。

E.g. when an Actor of type PLAYER collides with one of type WATER, he will drown...

例如。当一个类型PLAYER的演员与一个类型的水碰撞时,他会淹死......

Hope it helps...

希望能帮助到你...

#1


5  

You already did it the right way to create and set a ContactListener... (for a general setup, the libgdx wiki is great: https://github.com/libgdx/libgdx/wiki/box2d#contact-listeners)

您已经以正确的方式创建并设置了ContactListener ...(对于一般设置,libgdx wiki很棒:https://github.com/libgdx/libgdx/wiki/box2d#contact-listeners)

If you now want to handle the specific contacts, you for exmaple need to add some implementation in the beginContact(); method of your listener. The beginContact(); method contains a Contact instance, which holds all you need:

如果您现在想要处理特定的联系人,那么您需要在beginContact()中添加一些实现;你的听众的方法。 beginContact(); method包含一个Contact实例,它可以满足您的所有需求:

  • FixtureA - the first fixture of a contact
  • FixtureA - 联系人的第一个夹具
  • FixtureB - the fixture, which FixtureA has collided with
  • FixtureB - FixtureA碰撞的夹具
  • WorldManifold - an object that holds the collision points etc
  • WorldManifold - 一个保持碰撞点等的对象

Through the fixtures you can access the bodies and the actors which you are drawing. The connection to your Actor can be done through the body.setUserData(actor); method.

通过灯具,您可以访问您正在绘制的实体和演员。可以通过body.setUserData(actor)完成与Actor的连接;方法。

Now you need to decide on how to find out the correct collisions. You can either work with sensors, which are box2d fixtures that just act as a sensor. That means that when an object collides with a sensor, it won't bounce, but fall through it instead. But you then are able to detect this contact within the listener.

现在您需要决定如何找出正确的碰撞。您可以使用传感器,这些传感器只是作为传感器的box2d灯具。这意味着当一个物体与一个传感器发生碰撞时,它不会反弹,而是会反过来掉落。但是,您可以在侦听器中检测到此联系人。

Also, it might be a good idea to add some kind of GameObjectType to your actors. Imagine you create a jumping game where the player jumps from platform to platform with water below. Then you would create your actors with types like PLAYER, WATER, PLATFORM ... through the getUserData() method of the box2d bodies yu can now access the Actors and compare their types.

此外,向您的actor添加某种GameObjectType可能是个好主意。想象一下,你创造了一个跳跃游戏,玩家从平台跳到平台,下面有水。然后你会用PLAYER,WATER,PLATFORM等类型创建你的演员......通过box2d体的getUserData()方法你现在可以访问Actors并比较它们的类型。

E.g. when an Actor of type PLAYER collides with one of type WATER, he will drown...

例如。当一个类型PLAYER的演员与一个类型的水碰撞时,他会淹死......

Hope it helps...

希望能帮助到你...