[Guide] Contact Listener

  ... tutorials on how to use AndEngine.

Re: [Guide] Contact Listener

Postby nivangerow » Tue Dec 06, 2011 6:41 pm

Now I have a jump function, I found out that the player can still "stick" underneath the block. Is there any way to detect where the two bodies are colliding?
nivangerow
 
Posts: 54
Joined: Fri Dec 02, 2011 8:25 pm

Re: [Guide] Contact Listener

Postby spaine » Mon Dec 26, 2011 11:57 am

Up. For my question on previous page.

Still don't know why my game closes on contact. It doesn't "force close" it just shuts down.

Thanks.
spaine
 
Posts: 58
Joined: Tue Aug 30, 2011 10:26 am

Re: [Guide] Contact Listener

Postby ttttkk » Tue Jan 03, 2012 8:12 pm

yes, it did shut down, but without error messages...

the problem is this line

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. (x2.getUserData().equals("angel") || x1.getUserData().equals("enemy")
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
ttttkk
 
Posts: 54
Joined: Thu Dec 22, 2011 4:27 am

Re: [Guide] Contact Listener

Postby spaine » Thu Jan 05, 2012 12:13 pm

ttttkk wrote:yes, it did shut down, but without error messages...

the problem is this line

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. (x2.getUserData().equals("angel") || x1.getUserData().equals("enemy")
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


I still confused about contact listener, how should I go on with it?... do i need to use getBody() before getUserData()?
spaine
 
Posts: 58
Joined: Tue Aug 30, 2011 10:26 am

Re: [Guide] Contact Listener

Postby MANISH KUMAR » Tue May 01, 2012 7:12 pm

(x2.getUserData().equals("angel") && x1.getUserData().equals("enemy")

Solved
MANISH KUMAR
 
Posts: 14
Joined: Wed Apr 18, 2012 12:47 pm

Re: [Guide] Contact Listener

Postby Molkobain » Sat May 05, 2012 4:13 pm

Hi, thanks for the tutorial. I followed it to handle jumps through "cloud" platform. :)

I'll put my code just in case someone wonders :
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 final ContactListener contact = new ContactListener(){
  2.                         @Override
  3.                         public void preSolve(Contact contact, Manifold oldManifold){
  4.                                 final Fixture x1 = contact.getFixtureA();
  5.                         final Fixture x2 = contact.getFixtureB();
  6.                         final float entityY;
  7.                         final float obstacleY;
  8.                        
  9.                         if ( (x2.getBody().getUserData().equals(PLAYER_ID) && x1.getBody().getUserData().equals(CLOUD_ID)) ||
  10.                                  (x1.getBody().getUserData().equals(PLAYER_ID) && x2.getBody().getUserData().equals(CLOUD_ID)) ){
  11.                                 //We save player & block Y coordinates to see how contact is made
  12.                                 if(x1.getBody().getUserData().equals(PLAYER_ID)){
  13.                                         entityY = x1.getBody().getPosition().y;
  14.                                         obstacleY = x2.getBody().getPosition().y;
  15.                                 }else{
  16.                                         entityY = x2.getBody().getPosition().y;
  17.                                         obstacleY = x1.getBody().getPosition().y;
  18.                                 }
  19.                                 //If player jump though a "cloud" block, we let him pass only if he comes from below
  20.                                 if(entityY >= obstacleY){
  21.                                         contact.setEnabled(false);
  22.                                 }
  23.                         }
  24.  
  25.                         }
  26.                        
  27.                         @Override
  28.                         public void postSolve(Contact contact, ContactImpulse impulse) {
  29.                         }
  30.                         @Override
  31.                         public void endContact(Contact contact) {
  32.                         }
  33.                         @Override
  34.                         public void beginContact(Contact contact) {
  35.                         }
  36.                 };
  37.                 this.mPhysicsWorld.setContactListener(contact);
  38.  
Parsed in 0.042 seconds, using GeSHi 1.0.8.4
Molkobain
 
Posts: 106
Joined: Wed Apr 18, 2012 4:16 pm
Location: France

Re: [Guide] Contact Listener

Postby JimBadger » Tue May 08, 2012 12:21 am

This was the tutorial that got me to understand ContactListener, so thanks for that!

However, I have a problem...I can't seem to get my preSolve (or postSolve) methods to be called.

I'm using GLES2, and all the rest of my physics stuff is working perfectly, including stuff in beginContact and endContact.

I know there used to be a bug in GLES1 regarding preSolve, but I also know that was fixed. Is there anything special I need to to with GLES2 to get preSolve "working"? An updated library, or something?

Or, should I not be setting the contact listener on the actual physicsworld? Should I set contactlisteners on the actual objects? (Bearing in mind that the rest of my physics is working great....).
JimBadger
 
Posts: 22
Joined: Tue Jul 26, 2011 3:19 pm

Re: [Guide] Contact Listener

Postby ctlayon » Thu Jun 21, 2012 4:13 pm

Thanks for a great guide on Contact Listener, I also found the last post by none other than skyuzo here, http://gamedev.stackexchange.com/questions/16943/how-to-remove-a-box2d-body-when-collision-happens, very helpful in addition to this guide!
ctlayon
 
Posts: 155
Joined: Thu Jun 14, 2012 9:41 pm
Location: Champaign, Illinois

Re: [Guide] Contact Listener

Postby swatirawat » Thu Jul 12, 2012 7:04 am

can anyone give me an example of collision detection implemented between two dynamic bodies. I implemented the contact listener but i don't know how to check for collision. If i check collision between two sprites (using x.collideswith(y) ) , the collision is detected but the transparent space around the sprites is also considered as part of the collision. Can anyone help?
swatirawat
 
Posts: 8
Joined: Wed Jun 06, 2012 3:58 pm

Re: [Guide] Contact Listener

Postby OzLark » Thu Jul 12, 2012 7:09 am

@swatirawat this thread itself explains most of what you'll need.

This has been discussed here many many times, a little searching will find you lots of threads covering this topic.
Did this post help you? Feel free to return the gesture by downloading Ninja Bees, creating a level (or more) and sharing it to the Ninja Bees website!

Ninja Bees - Our latest Android game, check it out on Google Play (free) or paid with no ads
OzLark
 
Posts: 1130
Joined: Sun Feb 05, 2012 3:19 am

PreviousNext

Return to Tutorials

Who is online

Users browsing this forum: Alexa [Bot] and 25 guests