I’m having some trouble in that it seems like some of the contacts that getContactList is returning aren’t actually contacts.
Relevant code:
List < Contact > contacts = theBall.body.getWorld().getContactList(); //get all contacts in current world
if (contacts.size() == 0) selected = -1; // if there are no contacts, set selected to -1, meaning no menu item is selected
/* loop through all contacts */
for (Contact con: contacts) {
Fixture fixtureA = con.getFixtureA();
Fixture fixtureB = con.getFixtureB();
LogapPlatform lplat = LogapUtils.cast(fixtureA.getBody().getUserData(), fixtureB.getBody().getUserData(), LogapPlatform.class);
LogapBall lball = LogapUtils.cast(fixtureA.getBody().getUserData(), fixtureB.getBody().getUserData(), LogapBall.class);
/* check if the contact is between a LogapBall object and a LogapPlatform object */
if (lball != null && lplat != null) {
/* if true, determine which of the five menu platforms the ball has been placed on top of */
for (int i = 0; i < LogapLevel.ctrPlat.size(); i++) {
if (lplat.equals(LogapLevel.ctrPlat.get(i)) && (lball.getPos().y > lplat.getPos().y)) {
selected = (int) Math.floor(i / 5); // set selected to the "id" of the selected menu platform
}
}
} else {
selected = -1; // if false, set selected to -1
}
}
/* "NEW GAME" is the only menu item currently implemented. its "id" = 2 */
if (selected == 2)
LogapGame.font.setColor(Color.YELLOW); // if the ball is placed on top of the menu platform for NEW GAME, set the font color of the text to yellow
else
LogapGame.font.setColor(Color.PINK); // else, keep the text pink
/* draw the text on the screen */
if (logLev.menuLevel) {
LogapGame.font.draw(batch, "NEW GAME", 135, 150);
LogapGame.font.setColor(LogapGame.GRAY_192);
}
To see this code in action (and the trouble I’m having), here’s a video sample to illustrate (I have Box2DDebugRenderer enabled btw):
RcHwt4b4lt0
So, as you may already see, the problem I’m having is that the text turns to yellow even though the ball has not yet come in contact with the platform. What’s supposed to happen is that the text should only turn yellow when the ball comes in contact with the platform. If there’s any doubt whether or not they aren’t in fact, in contact, here’s a closeup:
http://static.rateyourmusic.com/lk/l/w/02413fc8b4aef5d9f37c3b12420d4a2d/4870735.png
Any idea as to why the text is changing color even though the two objects aren’t in contact yet?
P.S. - For anyone wondering why I’m not using a collision listener, well, I am. Just not for this specific logic. I ran into some quirks with collision listener so I decided that for some of the behavior of objects in the game, I’d rather manually query the contacts like this.