using the collisions module in wade physics
Shri

Gio,

I am a little unclear on how to use wade.physics.collisions for getting contact information ?

Can you post a snippet on how to get contact information for a scene object's fixture ?

thanks - shri

All 3 Comments
Gio

Hi Shri

There are two ways:

1. If your SceneObjects have a PhysicsObject behavior, they should receive onCollision events, in which case you can do

myObject.onCollision = function(data)
{
    // data.otherObject is the SceneObject we just collided with
    // data.contact is a b2PolygonContact or similar object (depending on the shapes involved)
    // data.manifold is a b2Manifold object with detailed contact information
    // bodyIndex is a string, either A or B, defining whether is object is body A or B in the manifold
};

Note however that this won't happen for Sensors. But there is the other way:

2. Each instance of the PhysicsObject behavior contains a list of objects that are colliding with the current object:

var collidingWith = myObject.getBehavior('PhysicsObject').collidingWith;
// check for example if collidingWith.someOtherObject == true
// or for (var x in collidingWith) { ... }

If you want to get technical, using box2d terminology, the onCollision event is fired in the PreSolve step. You can return true from the onCollision event to effectively cancel the collision resolution.

On the other hand, the collidingWith object is updated on BeginContact and EndContact.

Shri

Gio,

I am trying to add/improve jumping to the stickman demo. In the case of the stickman, it can jump on player input if it is on the ground, and sometimes is jumping through the air because of the uneven terrain. From looking on the web, this seems to be the favored solution.

So, I did the following

  1. added a second fixture as a foot sensor
  2. add an attribute player.grounded to the scene object
  3. in the global player.onUpdate function check the collidingWith attribute to see if colliding with the ground and then set the grounded attribute appropriately.
  4. then based on the grounded attribute, set the players state (jumping, not jumping).

 

-- update

OK, I think I got it figured out. I added the foot sensor under the stickman, and then I just loop through the collidingWith object fields to see if any of them are true. If any are true, then I know the player is "grounded". I should have another demo up in a couple of days.

cheers - shri

 

Gio

Glad you figured it out, looking forward to the new demo.

I was just going to say that, depending on your cirumstances, you may want to do different this with the collidingWith object. If looking for collisions with a specific object or two, it makes sense to do 

var collidingWith = myObject.getBehavior('PhysicsObject').collidingWith;
if (collidingWith['platform'] || collidingWith['otherPlatform'])
{
     // .....
}

On the other hand, if you have lots of platforms to check, I would add an isPlatform property to each platform, so you then test for collisions with any platform like this:

var collidingWith = myObject.getBehavior('PhysicsObject').collidingWith;
for (var x in collidingWith)
{
    if (_[x].isPlatform)
    {
         // ....
    }
}

 

Post a reply
Add Attachment
Submit Reply
Login to Reply