I'm currently developing an Endless Runner in LibGDX. I have 4 sensors (upper-far, upper-close, lower-far, lower-close, will be used for machine learning later). I need to detect collisions between each sensor and object called Enemy. Right now it detects collisions between object of type Sensor and Enemy and that's correct but I don't know which individual Sensor is actually triggered. Here's the code responsible for collision detecting (in GameStage method):
@Override
public void beginContact(Contact contact) {
Body a = contact.getFixtureA().getBody();
Body b = contact.getFixtureB().getBody();
if ((BodyUtils.bodyIsRunner(a) && BodyUtils.bodyIsEnemy(b)) ||
(BodyUtils.bodyIsEnemy(a) && BodyUtils.bodyIsRunner(b))) {
runner.hit();
}else if ((BodyUtils.bodyIsEnemy(a) && BodyUtils.bodyIsSensor(b)) ||
(BodyUtils.bodyIsSensor(a) && BodyUtils.bodyIsEnemy(b))) {
} else if ((BodyUtils.bodyIsRunner(a) && BodyUtils.bodyIsGround(b)) ||
(BodyUtils.bodyIsGround(a) && BodyUtils.bodyIsRunner(b))) {
runner.landed();
}
}
BodyUtils: public class BodyUtils {
public static boolean bodyInBounds(Body body) {
UserData userData = (UserData) body.getUserData();
switch (userData.getUserDataType()) {
case RUNNER:
case ENEMY:
return body.getPosition().x + userData.getWidth() / 2 > 0;
}
return true;
}
public static boolean bodyIsEnemy(Body body) {
UserData userData = (UserData) body.getUserData();
return userData != null && userData.getUserDataType() == UserDataType.ENEMY;
}
public static boolean bodyIsRunner(Body body) {
UserData userData = (UserData) body.getUserData();
return userData != null && userData.getUserDataType() == UserDataType.RUNNER;
}
public static boolean bodyIsGround(Body body) {
UserData userData = (UserData) body.getUserData();
return userData != null && userData.getUserDataType() == UserDataType.GROUND;
}
public static boolean bodyIsSensor(Body body) {
UserData userData = (UserData) body.getUserData();
return userData != null && userData.getUserDataType() == UserDataType.SENSOR;
}
}
and here is the whole code (most important part about userData in box2 package): https://github.com/SzymonGalazka/Machine-Learning-Runner/tree/master/core/src/com/pl/runner
Aucun commentaire:
Enregistrer un commentaire