Custom Collision detection problem

Discuss issues specific to the Flash/AS3 port of Box2D
clouting
Posts: 6
Joined: Thu Jul 08, 2010 7:37 am

Custom Collision detection problem

Postby clouting » Fri Nov 26, 2010 7:03 am

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: Select all

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


Here is my listener class

Code: Select all

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

SticksStones
Posts: 10
Joined: Mon Dec 06, 2010 12:17 pm

Re: Custom Collision detection problem

Postby SticksStones » Tue Dec 07, 2010 10:03 am

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: Select all

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!

SticksStones
Posts: 10
Joined: Mon Dec 06, 2010 12:17 pm

Re: Custom Collision detection problem

Postby SticksStones » Tue Dec 07, 2010 5:48 pm

......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 1243 times

drkucho
Posts: 3
Joined: Thu Dec 30, 2010 7:55 am

Re: Custom Collision detection problem

Postby drkucho » Thu Dec 30, 2010 8:14 am

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 :?


Return to “Flash”



Who is online

Users browsing this forum: Yahoo [Bot] and 2 guests