Extending b2ContactListener in 2.1

Discuss issues specific to the Flash/AS3 port of Box2D
st33d
Posts: 6
Joined: Tue Jul 14, 2009 6:26 am

Extending b2ContactListener in 2.1

Postby st33d » Mon Mar 22, 2010 5:14 am

I've just downloaded 2.1 and I'm completely lost on the new Contact Listener and the documentation hasn't caught up on the new system.

Before I would do this:

Code: Select all

override public function Add(point:b2ContactPoint):void {
   add_list.push(new ContactPoint(point.shape1, point.shape2, point.separation, point.position.Copy(), point.normal.Copy()));
}


But b2ContactPoint and Add no longer exist.

And I need all of the information in there. I need both shapes, the contact point and the collision normal.

I'm assuming that I would override the new BeginContact method and try to extract data from the Contact object:

Code: Select all

public virtual function BeginContact(contact:b2Contact):void { }


But how do I get my shapes, contact point and collision normal?

sketchbookgames
Posts: 564
Joined: Tue Feb 24, 2009 4:10 pm
Location: Michigan
Contact:

Re: Extending b2ContactListener in 2.1

Postby sketchbookgames » Mon Mar 22, 2010 8:20 pm

you might try the PreSolve event instead of BeginContact, seems like it might contain more info about the collision data, instead of just the contact pair.

im not too well versed on the new box2d yet, but thats where i would look.

VengantMjolnir
Posts: 356
Joined: Tue Feb 03, 2009 2:05 pm
Location: Canada
Contact:

Re: Extending b2ContactListener in 2.1

Postby VengantMjolnir » Tue Mar 23, 2010 9:59 am

You can override the BeginContact method in order to get the contact. From there you can pass in a new instance of b2WorldManifold to the GetWorldManifold method of the contact( see example below. ) The b2WorldManifold includes things like the normal and the collision points.

As for the shapes/bodies involved. The contact also has references to the two fixtures involved in the collision. Remember, in 2.1 shapes don't exist within the simulation on their own. They are attached to bodies by a fixture.

Code: Select all

package 
{
   import Box2D.Collision.b2WorldManifold;
   import Box2D.Collision.Shapes.b2Shape;
   import Box2D.Common.Math.b2Vec2;
   import Box2D.Dynamics.b2Body;
   import Box2D.Dynamics.b2DefaultContactListener;
   import Box2D.Dynamics.b2Fixture;
   import Box2D.Dynamics.b2World;
   import Box2D.Dynamics.Contacts.b2Contact;
   
   public class CustomContactManager extends b2DefaultContactListener
   {
      
      public function CustomContactManager(world:b2World)
      {
         super(world);
         
      }
      
      override public function BeginContact(contact:b2Contact):void
      {
         super.BeginContact(contact);
         
         var mw:b2WorldManifold = contact.GetWorldManifold(mw);
         var firstPoint:b2Vec2 = mw.m_points[0];
         var normal:b2Vec2 = mw.m_normal;
         
         var fixture:b2Fixture = contact.GetFixtureA();
         var shape:b2Shape = fixture.GetShape();
         var body:b2Body = fixture.GetBody();
      }
      
      
   }

}

knightley
Posts: 24
Joined: Sun Mar 14, 2010 10:57 am

Re: Extending b2ContactListener in 2.1

Postby knightley » Tue Mar 23, 2010 4:12 pm

does the normal vector has length 1? or it has a length
according to impulse or velocity?

vitaLee
Posts: 43
Joined: Fri Jan 08, 2010 3:13 am

Re: Extending b2ContactListener in 2.1

Postby vitaLee » Wed Mar 24, 2010 6:11 am

well i guess if it's called normal then it's length equals 1 :)

VengantMjolnir
Posts: 356
Joined: Tue Feb 03, 2009 2:05 pm
Location: Canada
Contact:

Re: Extending b2ContactListener in 2.1

Postby VengantMjolnir » Wed Mar 24, 2010 11:16 am

I took a quick look through the code that generates m_Normal and it looks like it is in fact length 1. In the case of circles its being normalized and as a plane the transform.R matrix is being multiplied by the plane normal.

VengantMjolnir
Posts: 356
Joined: Tue Feb 03, 2009 2:05 pm
Location: Canada
Contact:

Re: Extending b2ContactListener in 2.1

Postby VengantMjolnir » Wed Mar 24, 2010 11:54 am

Looking deeper into the b2DefaulContactListener revealed what you need. The PostSolve method includes the impulse.


Return to “Flash”



Who is online

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