Box2D Forums

It is currently Mon Jul 28, 2014 7:20 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Fri Nov 26, 2010 7:03 am 
Offline

Joined: Thu Jul 08, 2010 7:37 am
Posts: 6
Hi,

I am trying to customise how my b2body's react to collisions.
I found this tutorial: http://blog.allanbishop.com/box2d-2-1a-tutorial-–-part-4-collision-detection/ about over riding the box2d collision handler.

It is basically a new class which extends the b2ContactListener class. It seems to work perfectly except that I need to run this on multiple objects.
When I call the customEventListener it registers a collision but them seems to affect all of the objects I attached the listener to rather than the object that is having the collision.

Any ideas?

Here is how I add the listener (this is added every time I create a new object

Code:
plateContactListener = new game_plateContactListener();
plateContactListener.eventDispatcher.addEventListener(game_plateContactListener.SPROUT_START_CONTACT, onSproutHitPlate);
world.SetContactListener(plateContactListener);


Here is my listener class
Code:
public class game_plateContactListener extends b2ContactListener
   {
      public var eventDispatcher:EventDispatcher;
      public static const SPROUT_START_CONTACT:String = "sproutStartContact";
      
      public function game_plateContactListener():void {
         eventDispatcher = new EventDispatcher();
      }
      
      override public function BeginContact(contact:b2Contact):void {
         if ((contact.GetFixtureA().GetBody().GetUserData().type == doc_class.BRUSSEL_SPROUT && contact.GetFixtureB().GetBody().GetUserData().type == doc_class.PLATE_SENSOR) || (contact.GetFixtureA().GetBody().GetUserData().type == doc_class.PLATE_SENSOR && contact.GetFixtureB().GetBody().GetUserData().type == doc_class.BRUSSEL_SPROUT)) {
            
            eventDispatcher.dispatchEvent(new Event(SPROUT_START_CONTACT));
         }
      }
      
   }


What I want this code to do is only call a response on the b2body that has had a collision rather than on all of them...

Any ideas?

Thanks in advance


Top
 Profile  
 
PostPosted: Tue Dec 07, 2010 10:03 am 
Offline

Joined: Mon Dec 06, 2010 12:17 pm
Posts: 10
This is roughly what's happening....

All the objects listen for an event.
If the if_statement is true, fire the event.
Any object's listening for the event react.
Since all the objects are listening for the event, they all react.

....what else do you expect?!

Ahh...ok I see why you're getting confused. I've had a closer look at the tutorial.

I've been following Allan's tutorials too and mostly they're really good, but personally I think he's taken an overcomplicated and rather stupid approach on this one...I suspect that if you have more than one red ball, then if any red ball hit's the ground - all red balls will react. Likewise with blue balls.

Personally I see absolutely no reason to use event dispatcher unless you want several objects to react to one event, or you can't get a reference to the object from the b2Contact.

You need to do something like this...

Code:
override public function BeginContact(contact:b2Contact):void
{

  if (contact.GetFixtureA().GetBody().GetUserData().type == doc_class.BRUSSEL_SPROUT && contact.GetFixtureB().GetBody().GetUserData().type == doc_class.PLATE_SENSOR)
  {
     contact.GetFixtureA().GetBody().nameOfFunctionToRun(); // Brussel_sprout reaction
     contact.GetFixtureB().GetBody().nameOfFunctionToRun(); // Plate sensor reaction
  }

  if (contact.GetFixtureA().GetBody().GetUserData().type == doc_class.PLATE_SENSOR && contact.GetFixtureB().GetBody().GetUserData().type == doc_class.BRUSSEL_SPROUT)
  {
     contact.GetFixtureA().GetBody().nameOfFunctionToRun(); // Plate sensor reaction
     contact.GetFixtureB().GetBody().nameOfFunctionToRun(); // Brussel_sprout reaction
  }

}


....since nobody seems to want to answer my question on this forum, also about the contact listener, I'm going to have a go at this tonight and see if I can get it working properly. I'll probably learn a lot. Watch this space!


Top
 Profile  
 
PostPosted: Tue Dec 07, 2010 5:48 pm 
Offline

Joined: Mon Dec 06, 2010 12:17 pm
Posts: 10
......and it's done!

Allan Bishop's Box2D 2.1a Tutorial – Part 4 (Collision Detection) - rewritten without event dispatcher and made easy to add multiple objects.


Attachments:
ForumBalls.zip [144.66 KiB]
Downloaded 1075 times
Top
 Profile  
 
PostPosted: Thu Dec 30, 2010 8:14 am 
Offline

Joined: Thu Dec 30, 2010 7:55 am
Posts: 3
hey StickStones thanks for this tutorial remake, i just downloaded it and hope to get some detecting collisions understanding finally, im new to flash and all the documentation i find about box2d is so confusing :?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: Google [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group