我在我的LIBGDX游戏中使用短值来检测碰撞,但是我放置了太多元素,以至于用完了所有值。我目前在32768,被认为是整数。我该怎么办?
这些是我的碰撞位:
public static final short NOTHING_BIT = 0;
public static final short GROUND_BIT = 1;
public static final short FRAN_BIT = 2;
public static final short BRICK_BIT = 4;
public static final short SECONDS_BIT = 8;
public static final short DESTROYED_BIT = 16;
public static final short HIDDEN_BIT = 32;
public static final short ENEMY_BIT = 64;
public static final short ENEMY_HEAD_BIT = 128;
public static final short ENEMY_FRONT_BIT = 256;
public static final short FRAN_HEAD_BIT = 512;
public static final short FRAN_BODY_SENSOR_BIT = 1024;
public static final short ENEMY_LIMIT = 2048;
public static final short WATER_BIT = 4096;
public static final short FIREBALL_BIT = 8192;
public static final short GROUND_ESCAPE = 16384;
这是检测到冲突的代码:
@Override
public void beginContact(Contact contact) {
Fixture fixA = contact.getFixtureA();
Fixture fixB = contact.getFixtureB();
int cDef = fixA.getFilterData().categoryBits | fixB.getFilterData().categoryBits;
switch (cDef){
case JungleMasters.FRAN_HEAD_BIT | JungleMasters.BRICK_BIT:
if (fixA.getFilterData().categoryBits == JungleMasters.FRAN_HEAD_BIT){
((InteractiveTIleObject)fixB.getUserData()).onHeadHit((Franfran) fixA.getUserData());
} else {
((InteractiveTIleObject) fixA.getUserData()).onHeadHit((Franfran) fixB.getUserData());
}
sound = screen.assetManager.manager.get(screen.assetManager.brickBreaking);
sound.play();
break;
case JungleMasters.FRAN_HEAD_BIT | JungleMasters.HIDDEN_BIT:
if (fixA.getFilterData().categoryBits == JungleMasters.FRAN_HEAD_BIT){
((InteractiveTIleObject)fixB.getUserData()).onHeadHit((Franfran) fixA.getUserData());
} else {
((InteractiveTIleObject) fixA.getUserData()).onHeadHit((Franfran) fixB.getUserData());
}
sound = screen.assetManager.manager.get(screen.assetManager.brickBreaking);
sound.play();
break; ...}
答案 0 :(得分:1)
categoryBits
和maskBits
主要用于box2d来说明哪个灯具与另一个灯具发生碰撞。
因此,当您的fixtures
中的一个与全部fixture
或全部都不碰撞时,您就不需要category
-或maskBits
。
如果灯具与所有其他灯具发生碰撞,则不需要category
-或maskBits
。
如果灯具与其他灯具均不发生碰撞,则可以设置isSensor = true;
而不是使用category
-和maskBits
。
如果您知道哪个对象与另一个对象发生碰撞,可以使用UserData
的灯具。
为您拥有的所有类型创建一个枚举:
public enum Type {
NOTHING,
GROUND,
FRAN,
FRAN_BODY_SENSOR,
ENEMY_FRONT,
BRICK,
etc...
}
然后通过创建固定装置,可以将Type设置为UserData:
Fixture fixture = body.createFixture(fixtureDef);
fixture.setUserData(Type.GROUND);
//short:
body.createFixture(fixtureDef).setUserData(Type.GROUND);
最后,在您的ContactListener
中,您可以检查灯具的UserData来找出哪个对象与其他对象发生碰撞:
if(fixA.getUserData() == Type.FRAN_HEAD && fixB.getUserData() == Type.BRICK){
//FRAN_HEAD and BRICK has collided
}