简体   繁体   English

Libgdx中的碰撞检测无物理响应

[英]Collision Detection in Libgdx without physical response

I my game on android I would like to let my player collide with an invinsible object. 我在android上玩游戏,我想让我的玩家与一个无敌的对象碰撞。 This collision should be detected, but it shouldnt actually stop the player. 应该检测到此碰撞,但实际上不应停止播放器。 He should just be able to "go through". 他应该只能“经历”。 I'm sure this is possilbe in libgdx but i cant get it to work. 我确定这是libgdx中的possilbe,但我无法使其正常工作。 I used the tutorial here 我在这里使用了教程

I just want to use the trigger. 我只想使用触发器。 Setting the Flags with btCollisionObject.CollisionFlags.CF_NO_CONTACT_RESPONSE does work, which means that my Character goes through, but I dont know where this is saved. 使用btCollisionObject.CollisionFlags.CF_NO_CONTACT_RESPONSE设置标志确实可行,这意味着我的角色通过了,但是我不知道将其保存在何处。 So where can I find the Event which has this information, does anyone know? 那么,在哪里可以找到具有此信息的活动,有人知道吗?

I my game on android I would like to let my player collide with an invinsible >object. 我在android上玩游戏,我想让玩家与一个无敌的>>对象碰撞。 This collision should be detected, but it shouldnt actually stop the >player. 应该检测到此冲突,但实际上不应停止> player。

What you need is a custom ContactListener paired with a Box2D sensor body that it can detect. 您需要的是一个自定义的ContactListener以及可以检测到的Box2D 传感器主体 Your player would also be a Box2D body. 您的播放器也将是Box2D实体。 Give your two bodies identifier in the form of UserData (eg a simple string). 以UserData的形式给您的两个主体标识(例如,一个简单的字符串)。 You can then check for these UserDatas inside your ContactListener. 然后,您可以在ContactListener中检查这些UserData。

Fixture.setUserData(...)

Your ContactListener would implement the Box2D ContactListener and override its methods: 您的ContactListener将实现Box2D ContactListener并覆盖其方法:

public class MyContactListener implements ContactListener{
@Override
public void beginContact(Contact contact) {
    Fixture fa = contact.getFixtureA();
    Fixture fb = contact.getFixtureB();
    if(fa == null || fb == null) return;
    // ...
}

@Override
public void endContact(Contact contact) {
    Fixture fa = contact.getFixtureA();
    Fixture fb = contact.getFixtureB();
    if(fa == null || fb == null) return;
    // ...
}

@Override
public void preSolve(Contact contact, Manifold oldManifold) {}
@Override
public void postSolve(Contact contact, ContactImpulse impulse) {}

}

The Box2D body and sensor declarations are left as an exercise for the reader. Box2D主体和传感器声明留给读者练习。

A beginner tutorial for Box2D can be found here: http://rotatingcanvas.com/using-box2d-in-libgdx-game-part-i/ 可在此处找到Box2D的初学者教程: http : //rotatingcanvas.com/using-box2d-in-libgdx-game-part-i/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM