Libgdx Box2D - 检测碰撞

时间:2012-04-09 15:06:22

标签: box2d libgdx

我正在尝试检测玩家与传感器主体的碰撞。想象一下平台游戏中的收藏品......我想在玩家与收藏品发生碰撞时举办活动。

正如您将从我的代码中看到的那样,我正在将一个动态的主体附加到可收集的对象以及一个传感器主体,我希望将玩家碰撞'事件'附加到玩家与传感器发生碰撞时。似乎有一个“ContactListener”接口,但实现这些方法似乎没有做任何事情。我该怎么做呢?

如果有更好的方法可以执行以下任何操作,我们将不胜感激任何建议:)

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactImpulse;
import com.badlogic.gdx.physics.box2d.ContactListener;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.Manifold;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;

public class CoinCollectible extends GameObject{

    public CoinCollectible(Vector2 position, float angle){
        bodyDef.type = BodyType.DynamicBody;
        bodyDef.position.x = position.x;
        bodyDef.position.y = position.y;
        bodyDef.angle = angle;

        PolygonShape poly = new PolygonShape();
        poly.setAsBox(1, 1);
        FixtureDef itemFixture = new FixtureDef();
        itemFixture.shape = poly;
        itemFixture.density = 1;
        itemFixture.filter.categoryBits = PhysicsLayers.LAYER_ITEM;
        itemFixture.filter.maskBits = PhysicsLayers.MASK_PLAYER;
        fixtureArray.add(itemFixture);

        massData.mass = 1f;

        // Attach a collision sensor
        FixtureDef sensorFixture = new FixtureDef();
        sensorFixture.shape = poly;
        sensorFixture.isSensor = true;
        sensorFixture.filter.maskBits = PhysicsLayers.LAYER_PLAYER;

        fixtureArray.add(sensorFixture);

        // Clean up
        poly.dispose();

    }
}

import java.util.ArrayList;
import java.util.List;

import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.MassData;
import com.badlogic.gdx.physics.box2d.World;

public class GameObject{

    public Body body = null;
    public BodyDef bodyDef = new BodyDef();
    public MassData massData = new MassData();

    protected List<FixtureDef> fixtureArray = new ArrayList<FixtureDef>();

    public GameObject(){}

    public Body addToWorld(World world){
        if(bodyDef == null)
            return null;

        body = world.createBody(bodyDef);

        while(fixtureArray.size() > 0){
            body.createFixture(fixtureArray.remove(0));
        }

        body.setMassData(massData);

        return body;
    }

}

3 个答案:

答案 0 :(得分:0)

尝试在代码中添加此行(在硬币构造函数中):

bodyDef.userData = this;

您的联系人听众get userdata并投射到GameObject,然后,例如,调用事件冲突。

问候

答案 1 :(得分:0)

你必须检查{2}碰撞中的Sensor身体碰撞或者盒子碰撞方法的结束联系

答案 2 :(得分:0)

有两件事可能是错的:
1)OP实例化box2d.World后没有正确设置ContactListener 2)类别和掩码位未正确设置,因此不会触发ContactListener

解决方案:
1)在实例化物理世界后把它放好 这个例子中缺少了。这将为你的box2d.World设置ContactListener,因此它将触发一些东西。

physicsWorld = new World(new Vector2(0,-9),true);
physicsWorld.setContactListener(
    new ContactListener() {
        @Override
        public void beginContact(Contact contact) {
            Fixture fA = contact.getFixtureA();
            Fixture fB = contact.getFixtureB();
            Gdx.app.log("beginContact", "between " + fA.toString() + " and " + fB.toString());
        }

        @Override
        public void endContact(Contact contact) {
            Gdx.app.log("endContact", "between " );
        }

        @Override
        public void preSolve(Contact contact, Manifold oldManifold) {
            //you can leave this empty
        }

        @Override
        public void postSolve(Contact contact, ContactImpulse impulse) {
            //you can leave this empty
        }
    });

2)以这种方式设置类别和掩码位,这样你可以测试它是否有效(OP没有显示他的PhysicsLayers中使用的特定值)

public CoinCollectible(Vector2 position, float angle){
    // ...
    itemFixture.filter.categoryBits = 1;
    itemFixture.filter.maskBits =  2;
    sensorFixture.filter.categoryBits = 2; 
    sensorFixture.filter.maskBits = 1;
}