Dartbox2d Collision Detection

39 views
Skip to first unread message

Sebastiaan Hendriks

unread,
May 16, 2012, 9:56:01 AM5/16/12
to Dartbox2d discuss
I'm building a small demo in Box2DWeb and then the same demo
translated into Google Dart using the Dartbox2D implementation.

As part of the demo, I'm creating an collisionListener in Javascript
that goes off when 2 or more Polygons have a collision with eachother,
being the following.

//Collision Detection listener
var contactListener = new
Box2D.Dynamics.b2ContactListener();
contactListener.BeginContact = function(contact) {
if( contact instanceof
Box2D.Dynamics.Contacts.b2PolygonContact) {
COLLISION = true;
// collision of polygon to polygon
}
};

I have problems however producing the same in Dart using Dartbox2D. My
question is, how can I create a collisionListener and use that
collisionListener to find out if a collision between 2 polygons have
occured in a timestep in Dartbox2D?

Dominic Hamon

unread,
May 16, 2012, 12:55:45 PM5/16/12
to dartbox2...@googlegroups.com
Hello

First, thanks for being interested in DartBox2D.

You'll need to create a ContactListener class that implements callbacks/ContactListener.dart and set it as the ContactManager's contactListener field. Once you do that, your listener will get callbacks as expected.



Dominic Hamon      | Software Engineer | Google

Sebastiaan Hendriks

unread,
May 21, 2012, 9:06:32 AM5/21/12
to Dartbox2d discuss
Dominic, thank you for responding.

I've created a CollisionListener class, as shown here;

class CollisionListener implements ContactListener
{
void beginContact(Contact contact)
{
print("begin contact");
}
void endContact(Contact contact)
{
print("end contact");
}
~Code snipped~
}

Then in the main class, where the world is created and the objects are
created/placed, I set my CollisionListener class as the value of the
contactListener field in ContactManager. As shown here;
ContactManager contactman;
contactman = new ContactManager(world);
contactman.contactListener = new CollisionListener();

After working nearly a full day on getting a response from the engine
that 2 polygons have collided which eachother, I'm kind of blanking
out here.

On 16 mei, 18:55, Dominic Hamon <domi...@google.com> wrote:
> Hello
>
> First, thanks for being interested in DartBox2D.
>
> You'll need to create a ContactListener class that implements
> callbacks/ContactListener.dart and set it as the ContactManager's
> contactListener field. Once you do that, your listener will get callbacks
> as expected.
>
> Dominic Hamon      | Software Engineer | Google
> domi...@google.com | +1 (650) 394-6366
>
> On Wed, May 16, 2012 at 6:56 AM, Sebastiaan Hendriks <asaelnath...@gmail.com

Dominic Hamon

unread,
May 21, 2012, 10:50:01 AM5/21/12
to dartbox2...@googlegroups.com
On Mon, May 21, 2012 at 6:06 AM, Sebastiaan Hendriks <asaeln...@gmail.com> wrote:
Dominic, thank you for responding.

I've created a CollisionListener class, as shown here;

class CollisionListener implements ContactListener
{
 void beginContact(Contact contact)
 {
   print("begin contact");
 }
 void endContact(Contact contact)
 {
   print("end contact");
 }
 ~Code snipped~
}

Then in the main class, where the world is created and the objects are
created/placed, I set my CollisionListener class as the value of the
contactListener field in ContactManager. As shown here;
ContactManager contactman;
contactman = new ContactManager(world);
contactman.contactListener = new CollisionListener();

The world already has a ContactManager. Try:

world.contactListener = new CollisionListener();

Sebastiaan Hendriks

unread,
May 21, 2012, 10:58:34 AM5/21/12
to Dartbox2d discuss
That does the trick. Thank you!

On 21 mei, 16:50, Dominic Hamon <domin...@google.com> wrote:
> On Mon, May 21, 2012 at 6:06 AM, Sebastiaan Hendriks <asaelnath...@gmail.com
Reply all
Reply to author
Forward
0 new messages