Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (580)
games submitted by our members
Games in WIP (499)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [Solved (but requesting clarification)] Having trouble with Box2D getContactList  (Read 459 times)
0 Members and 1 Guest are viewing this topic.
Offline heisenbergman

JGO Coder


Medals: 14
Projects: 1


L___ o_ G___ a__ P___


« Posted 2013-07-26 16:43:42 »

I'm having some trouble in that it seems like some of the contacts that getContactList is returning aren't actually contacts.

Relevant code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
       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):

<a href="http://www.youtube.com/v/RcHwt4b4lt0?version=3&amp;hl=en_US&amp;start=" target="_blank">http://www.youtube.com/v/RcHwt4b4lt0?version=3&amp;hl=en_US&amp;start=</a>

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.

Offline sirkarpfen

Junior Member


Medals: 2



« Reply #1 - Posted 2013-07-26 17:22:07 »

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.

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

JGO Coder


Medals: 14
Projects: 1


L___ o_ G___ a__ P___


« Reply #2 - Posted 2013-07-26 17:33:09 »

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.


Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline heisenbergman

JGO Coder


Medals: 14
Projects: 1


L___ o_ G___ a__ P___


« Reply #3 - Posted 2013-07-26 17:57:02 »

Solved by a fellow on StackExchange who suggested I use isTouching() Grin 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 (int i = 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.

Would appreciate some further advice. Thanks!

Offline sirkarpfen

Junior Member


Medals: 2



« Reply #4 - Posted 2013-07-26 18:28:31 »

I'm really not sure, why your code should not work properly...

The only thing i can imagine, is that getContactList() also returns some Pre-Contacts, with that i mean Contacts that "might" occur in a future step...

Your Ball is very close to the Platform. You could try if the problem still occurs if the Ball is farther away from the Platform...
Offline heisenbergman

JGO Coder


Medals: 14
Projects: 1


L___ o_ G___ a__ P___


« Reply #5 - Posted 2013-07-26 19:06:10 »

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.

It's pretty weird Tongue

Offline sirkarpfen

Junior Member


Medals: 2



« Reply #6 - Posted 2013-07-26 21:23:08 »

Yeah quite strange... Maybe this is something for the Issue Tracker Smiley
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (44 views)
2014-04-15 18:08:23

BurntPizza (40 views)
2014-04-15 03:46:01

UprightPath (56 views)
2014-04-14 17:39:50

UprightPath (38 views)
2014-04-14 17:35:47

Porlus (54 views)
2014-04-14 15:48:38

tom_mai78101 (77 views)
2014-04-10 04:04:31

BurntPizza (136 views)
2014-04-08 23:06:04

tom_mai78101 (236 views)
2014-04-05 13:34:39

trollwarrior1 (197 views)
2014-04-04 12:06:45

CJLetsGame (205 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!