/* 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 (inti = 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 yellowelseLogapGame.font.setColor(Color.PINK); // else, keep the text pink

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:

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.

In the Libgdx Box2D Documentation is clarified, that this list does not contain propper contact information:

"Returns the list of Contact instances produced by the last call to step(float, int, int). Note that the returned list will have O(1) access times when using indexing. contacts are created and destroyed in the middle of a time step. Use ContactListener to avoid missing contacts"

This is just a guess by me, but it might be the solution to your problem if you would use the ContactListener instead of getContactList, especially because the contactList could actually be empty (in very rare cases but still...) after World.step() hast finished.

In the Libgdx Box2D Documentation is clarified, that this list does not contain propper contact information:

"Returns the list of Contact instances produced by the last call to step(float, int, int). Note that the returned list will have O(1) access times when using indexing. contacts are created and destroyed in the middle of a time step. Use ContactListener to avoid missing contacts"

Thanks. I'm also aware of this, but I was thinking that that note only applies to a situation where maybe there was a contact, but by the time you called getContactList(), the contact was ended already --- or something like that.

However, in the video I posted there never is any prior or future contact between the ball and the platform... so I find it really odd that the text becomes yellow.

This is just a guess by me, but it might be the solution to your problem if you would use the ContactListener instead of getContactList, especially because the contactList could actually be empty (in very rare cases but still...) after World.step() hast finished.

What were the the problems you had with the contact listener?

If you notice the close-up image I posted, the platform is actually made up of smaller squares. My problem with contactListener happens when the ball is rolling off of one square onto another square in the same platform... endContact() happens between the ball and square #2 for example, before beginContact() happens between the ball and square #3. So the result is that from the program's perspective, the ball is not in contact with the platform, when in fact, it is.

Solved by a fellow on StackExchange who suggested I use isTouching() So this works like a charm now:

1 2 3 4 5 6 7 8 9 10 11

/* 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 (inti = 0; i < LogapLevel.ctrPlat.size(); i++) {if (lplat.equals(LogapLevel.ctrPlat.get(i)) && (lball.getPos().y > lplat.getPos().y) && con.isTouching()) {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 }

But, still... I'd love to hear further clarification from Nate or some other knowledgeable people out there regarding this. Why was my original code a problem in the first place?

I know that I solved it using isTouching(), which is extra checking to see if the fixtures are actually touching... but based on what the Box2DDebugRenderer was showing, the AABB weren't touching in the first place anyway, so I still don't get why the text was turning yellow.

There's only a certain distance it gets when it approaches the platform when the text turns yellow. But that said, I've never heard of Box2D trying to predict or anticipate when possible contacts might occur.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org