Collision Events

  ... discussions about development with the GLES2 branch of AndEngine.

Collision Events

Postby grixtanian » Mon Mar 05, 2012 7:17 pm

I've been going through the forum searching how to generate an event after a collision has occured. Say we have two types objects, A and B, and when they collide an event is triggered. For example, when an A collides with another object of type A the score is incremented, whereas when A collides with another another object of type B the score is decremented.

Can anyone give me some clues about this? Thanks :)
grixtanian
 
Posts: 13
Joined: Tue Feb 14, 2012 11:07 pm

Re: Collision Events

Postby RealMayo » Mon Mar 05, 2012 8:57 pm

Here's how you handle collision detection. Note in the code below that you never know if BodyA will be theBall or if BodyB will be theBall, so you need to include both possibilities.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2. public Scene onLoadScene() {   
  3.  
  4.         this.mPhysicsWorld.setContactListener( new ContactListener() {
  5.             @Override
  6.             public void beginContact(final Contact pContact) {
  7.                 final Body BodyA = pContact.getFixtureA().getBody();
  8.                 final Body BodyB = pContact.getFixtureB().getBody();
  9.                        
  10.                 if(BodyA.getUserData() == "theBall" && BodyB.getUserData() == "theGround"){
  11.                         // DO SOMETHING
  12.                 }else if(BodyA.getUserData() == "theGround" && BodyB.getUserData() == "theBall"){
  13.                         // DO SOMETHING
  14.                 }
  15.             }
  16.  
  17.                 @Override
  18.                         public void preSolve(Contact contact, Manifold oldManifold) {
  19.                 }
  20.  
  21.                 @Override
  22.                         public void postSolve(Contact contact, ContactImpulse impulse) {
  23.                 }
  24.                
  25.                 @Override
  26.                         public void endContact(final Contact pContact) {
  27.                 }
  28.         });
  29. }
  30.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
User avatar
RealMayo
 
Posts: 1683
Joined: Sat Sep 03, 2011 9:25 pm
Location: Chicago, IL

Re: Collision Events

Postby grixtanian » Mon Mar 05, 2012 9:21 pm

Cheers! :) Thanks a lot RealMayo, it's greatly appreciated :)
grixtanian
 
Posts: 13
Joined: Tue Feb 14, 2012 11:07 pm

Re: Collision Events

Postby grixtanian » Mon Mar 05, 2012 9:42 pm

Just to make sure I'm doing things right, where there is "theBall" or "theGround" I just replace it with the body's name right? In my case if I have a sprite called ball and I've made it a body, and it collided with another sprite"box", BodyA.getUserData() == "theBall" && BodyB.getUserData()== "theBox" right?
grixtanian
 
Posts: 13
Joined: Tue Feb 14, 2012 11:07 pm

Re: Collision Events

Postby RealMayo » Mon Mar 05, 2012 10:46 pm

Actually you replace "theBall" and "theGround" with whatever userData you set on your bodies.

Let's say you create a body called myBody1
You then need to do...
myBody1.setUserData("theBall");

That allows you to later call myBody1.getUserData();

So its not actually the name of your body, but rather the name of the setUserData that you applied to your body at the time of creating the body.
User avatar
RealMayo
 
Posts: 1683
Joined: Sat Sep 03, 2011 9:25 pm
Location: Chicago, IL

Re: Collision Events

Postby grixtanian » Tue Mar 06, 2012 12:26 am

Thanks once again. Works perfectly. One last question if you don't mind.
I am using a class which extends Sprite, and I have an instance of it with a body attached. I created the class because I need to have a sprite object, but I need some functionality related with each and every instance of the sprite.

So I have:
CustomSprite image;
and in CustomSprite I have a method returnNumber(); which returns a desired int value.
and I created a body using this object, therefore:
PhysicsFactory.createCircleBody(this.mPhysicsWorld, image,BodyType.StaticBody, PLAYER_FIXTURE_DEF);

My question is, is there a way to access returnNumber at collision time? Something like this:

if(BodyA.getUserData() == "theBox" && BodyB.getUserData() == "theSphere"){
//ACCESS returnNumber() NOW
}
grixtanian
 
Posts: 13
Joined: Tue Feb 14, 2012 11:07 pm

Re: Collision Events

Postby RealMayo » Tue Mar 06, 2012 1:28 am

In that case you need to get a little fancier like this...
http://www.andengine.org/forums/physics-box2d-extension/how-do-you-setuserdata-with-contactlistener-t6803-10.html?hilit=setuserdata#p29934
You need to use a JSONObject in conjunction with setUserData so that the JSONObject contains a reference to the Sprite itself.
User avatar
RealMayo
 
Posts: 1683
Joined: Sat Sep 03, 2011 9:25 pm
Location: Chicago, IL

Re: Collision Events

Postby Johannes Borchardt » Tue Mar 06, 2012 2:22 pm

My question is, is there a way to access returnNumber at collision time? Something like this:

if(BodyA.getUserData() == "theBox" && BodyB.getUserData() == "theSphere"){
//ACCESS returnNumber() NOW
}


Why don't you just set image as your userData? And then do something like
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. if(BodyA.getUserData().equals(image) && BodyB.getUserData().equals("theSphere")){
  2.   final int returnNumber = ((CustomSprite) BodyA.getUserData()).returnNumber();
  3. }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4

Btw: In Java you should use the equals method instead of == for objects, because "string" == "string" can be true and false, depending on the internal optimization of the virtual machine.
Android Development: http://www.andlabs.eu
Android Blog: http://www.Droid-Blog.net
Johannes Borchardt
 
Posts: 18
Joined: Wed Jun 09, 2010 12:19 pm

Re: Collision Events

Postby grixtanian » Tue Mar 06, 2012 8:51 pm

Thanks for your replies guys :) I'm trying it right now, however I'm getting an out of memory error :/ Thanks again :)
grixtanian
 
Posts: 13
Joined: Tue Feb 14, 2012 11:07 pm

Re: Collision Events

Postby baladewa88 » Sat Mar 17, 2012 2:46 am

I'm sorry. I need to ask, where i can find PhysicsWorld class. What class do i implement?

i use extend BaseGameActivity but there is no PhysichWorld, only Physics Handler. In Andengine example, PhysicsExample.java i see the PhysicsWorld Class. I try to implement IOSceneTouchListener and IAccelerometerLIstener but that class still missing. Anyone can help me?

any response appreciated. thanx
baladewa88
 
Posts: 3
Joined: Thu Mar 15, 2012 9:34 am

Next

Return to GLES2

Who is online

Users browsing this forum: Google Feedfetcher, Yahoo [Bot] and 28 guests