[Guide] Contact Listener

  ... tutorials on how to use AndEngine.

[Guide] Contact Listener

Postby Mathew » Mon Dec 05, 2011 9:10 pm

Contact Listener

Lately many people keep asking about how to make collision/contact check's between bodies, it was explained couple of times - more or less, I will create this brief guide, to keep most important informations about this subject in one thread. * If it there is already similar guide, please remove this one (sorry in advance)

Short description: as we know (or some of you don't) we can check collisions between shapes (for example sprites), with simple check:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. if (x.collidesWith(y)
  2. {
  3.      // action
  4. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


But, its not enough for bodies, that's why we can use Contact Listener.

1. Creating new contact listener:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         private ContactListener contactListener2()
  2.         {
  3.                 ContactListener contactListener = new ContactListener()
  4.                 {
  5.                         @Override
  6.                         public void beginContact(Contact contact)
  7.                         {
  8.                                
  9.                         }
  10.  
  11.                         @Override
  12.                         public void endContact(Contact contact)
  13.                         {
  14.                                
  15.                         }
  16.  
  17.                         @Override
  18.                         public void preSolve(Contact contact, Manifold oldManifold)
  19.                         {
  20.                                
  21.                         }
  22.  
  23.                         @Override
  24.                         public void postSolve(Contact contact, ContactImpulse impulse)
  25.                         {
  26.                                
  27.                         }
  28.                 };
  29.                 return contactListener;
  30.         }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


There are implemented, two most important functions (beginContact and endContact)

Also we have to register this contact listener, to our physic world.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 mPhysicsWorld.setContactListener(contactListener());
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Those give us possibilities to do certain actions, when there is contact between 2 bodies (or contact is ended)

Obviously, we have to 'receive' those 2 bodies somehow, to reference to them

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         Fixture x1 = contact.getFixtureA();
  2.         Fixture x2 = contact.getFixtureB();
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


And now lets say we want to Log info while there is contact between two bodies, so we have to add this check in beginContact

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. if (x2.getUserData().equals("player") || x1.getUserData().equals("monster"))
  2. {                                              
  3.         Log.i("CONTACT", "BETWEEN PLAYER AND MONSTER!");
  4. }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


as you most likely noticed, we used getUserData() function, to retrieve "data/reference name of the object/body/shape"

It means, you can use setUserData() function, to 'name' somehow your object.

For example:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Body b = PhysicsFactory.createBoxBody(mPhysicsWorld, testSprite, BodyType.KinematicBody, wallFixtureDef);
  2. b.setUserData("monster");
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Then we can reference to those names, using contact listener (with way posted above)

Hopefully its easy to understand, thanks!
Last edited by Mathew on Mon Dec 05, 2011 11:00 pm, edited 1 time in total.
User avatar
Mathew
 
Posts: 1073
Joined: Sun Jul 31, 2011 2:49 pm
Location: Tarnów, Poland

Re: [Guide] Contact Listener

Postby nivangerow » Mon Dec 05, 2011 9:29 pm

Can more than one body have the same userdata? Because I have some blocks that are loaded from a text file, into an ArrayList. Also, will the method contactListener2() be updated automatically?
nivangerow
 
Posts: 54
Joined: Fri Dec 02, 2011 8:25 pm

Re: [Guide] Contact Listener

Postby Mathew » Mon Dec 05, 2011 9:45 pm

For both of your question is yes.
User avatar
Mathew
 
Posts: 1073
Joined: Sun Jul 31, 2011 2:49 pm
Location: Tarnów, Poland

Re: [Guide] Contact Listener

Postby nivangerow » Mon Dec 05, 2011 10:09 pm

Ok, I have tried it, and I dont get any output.... Here is my code (all linked up to the scene)

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Problem solved.
  2.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Am I doing something wrong?
Last edited by nivangerow on Mon Dec 05, 2011 11:08 pm, edited 1 time in total.
nivangerow
 
Posts: 54
Joined: Fri Dec 02, 2011 8:25 pm

Re: [Guide] Contact Listener

Postby Mathew » Mon Dec 05, 2011 10:21 pm

You just set user data for player, I couldn't see any object with "block" data.

Also take into consideration, that x1 OR x2 might be player and x1 or x2 might be 2nd object (Ex. block)
User avatar
Mathew
 
Posts: 1073
Joined: Sun Jul 31, 2011 2:49 pm
Location: Tarnów, Poland

Re: [Guide] Contact Listener

Postby nivangerow » Mon Dec 05, 2011 10:48 pm

Mathew wrote:You just set user data for player, I couldn't see any object with "block" data.

Also take into consideration, that x1 OR x2 might be player and x1 or x2 might be 2nd object (Ex. block)

That is in a different class. DO i have to refer to that class or can i run it all over the Main class with the scene object?
nivangerow
 
Posts: 54
Joined: Fri Dec 02, 2011 8:25 pm

Re: [Guide] Contact Listener

Postby Mathew » Mon Dec 05, 2011 11:00 pm

Oh, I'm an idiot, forget to mention that ... we have to register this contact listener to our physic world (...)

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. mPhysicsWorld.setContactListener(contactListener());
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
User avatar
Mathew
 
Posts: 1073
Joined: Sun Jul 31, 2011 2:49 pm
Location: Tarnów, Poland

Re: [Guide] Contact Listener

Postby nivangerow » Mon Dec 05, 2011 11:11 pm

You need to change your if function inside the beginContact method to something like this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. if(x2.getBody().getUserData().equals("player") || x1.getBody().getUserData().equals("monster"))
Parsed in 0.036 seconds, using GeSHi 1.0.8.4

I got an error, because you forgot to add the .getBody() after the x2 or x1
nivangerow
 
Posts: 54
Joined: Fri Dec 02, 2011 8:25 pm

Re: [Guide] Contact Listener

Postby nivangerow » Mon Dec 05, 2011 11:33 pm

The problem is also fixed...
Last edited by nivangerow on Tue Dec 06, 2011 6:40 pm, edited 1 time in total.
nivangerow
 
Posts: 54
Joined: Fri Dec 02, 2011 8:25 pm

Re: [Guide] Contact Listener

Postby spaine » Tue Dec 06, 2011 12:09 pm

Where do you place this code?
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Fixture x1 = contact.getFixtureA();
  2. Fixture x2 = contact.getFixtureB();
  3.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


I tried doing this code in my game but it closes the intent and the game restarts.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ContactListener cl = new ContactListener() {
  2.                                
  3.                         @Override
  4.                         public void preSolve(Contact contact, Manifold oldManifold) {
  5.                                 // TODO Auto-generated method stub
  6.                                
  7.                         }
  8.                        
  9.                         @Override
  10.                         public void postSolve(Contact contact, ContactImpulse impulse) {
  11.                                 // TODO Auto-generated method stub
  12.                                
  13.                         }
  14.                        
  15.                         @Override
  16.                         public void endContact(Contact contact) {
  17.                                 // TODO Auto-generated method stub
  18.                                
  19.                         }
  20.                        
  21.                         @Override
  22.                         public void beginContact(Contact contact) {
  23.                                 // TODO Auto-generated method stub
  24.                                 Fixture x1 = contact.getFixtureA();
  25.                         Fixture x2 = contact.getFixtureB();
  26.                         if (x2.getUserData().equals("angel") || x1.getUserData().equals("enemy"))
  27.                         {                                              
  28.                                 Log.i("CONTACT", "BETWEEN PLAYER AND MONSTER!");
  29.                            
  30.                                
  31.                         }
  32.                                
  33.                         }
  34.                 };
  35.  
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


What am I doing wrong?
spaine
 
Posts: 58
Joined: Tue Aug 30, 2011 10:26 am

Next

Return to Tutorials

Who is online

Users browsing this forum: Alexa [Bot], Exabot [Bot], Google Feedfetcher, sibhe7, Yahoo [Bot] and 30 guests