Code Example for Detecting if b2Body is Resting On Top?

Does anyone have such a code example?

I need to check this to see if a character is allowed to jump for a platformer game. This should only be possible if the character is resting on top of something, and not already in the air.

I will write it myself if I have to, but maybe someone already accomplished this?
If your character has feet as separate bodies you can do something like

LFoot->GetContactList()!=NULL || RFoot->GetContactList()!=NULL

Otherwise.... I'm not sure, look at properties of the contact object, I'm sure you can find where the contact point is and make sure it's underneath your character so you know he's standing on top of something, and not just touching a wall.
Hmmm. Good idea there with the feet!

I started looking at the position of the Shapes found in the Contacts, found in the ContactNodes, found in the list.
Is that the right route? I got a position that looked local, but I guess I can local to global it.

Anyhow I like your idea of seperate bodies for feet, it's very simple.
What I would do is make a capsule for the player using a box and two circles, then you can check for contacts between the bottom circle and the ground to see if they player should be able to jump.
You could also check the position of the contact or the normal to make sure the contact isn't right at the edge (So you can't jump on walls or climb slopes that are too steep).

Haven't tried it myself but I think it would work.
-Matt
I created a Torso, Left foot and Right foot.

They are all defined as Shapes and added to a Body.

So far so good.

The problem is that the contactlist belongs to the Body and not the Shapes. So the contactnode doesn't have any information about what shape is involved.

I was hoping to take the body, get the foot shape from the shape-list, and check the contact list of the foot.

At this point I feel like a total idiot, and don't know where to go next.
Ok, now re-reading I think I should be making the parts as seperate Bodies, and connect them with joints, so that they DO have their own contactlists.
Ygryggil,

I believe you can get the shapes from a bodies contact list, like so:

body.m_contactList.contact.GetShape1();
(And GetShape2())

-Matt
Thanks!

For future reference, this was the solution.

I created the character as one body with two shapes. One for the feet and one for the rest of the body. My character is very small, so two shapes are enough.

I make sure to set the userdata of the shape to a String so that it's easy to recognize.

In AS3 this is what I use to define the character

Code:
public function createActor():b2Body{
       // define torso shape
       var w:Number = 0.7;
      var h:Number = 0.35;
      var torsoBox:b2BoxDef = new b2BoxDef();         
      torsoBox.density = 1;
      torsoBox.friction = 0.5;      
      torsoBox.extents.Set(w, h);
      torsoBox.userData = "torso";
      // define feet shape
      var feetBox:b2BoxDef = new b2BoxDef();
      feetBox.extents.Set(w*0.7, w*0.5);
      feetBox.localPosition.Set( 0, h);
      feetBox.density = 1.0;
      feetBox.friction = 0.5;
      feetBox.userData = "feet";
      // define body
      var bodyDef:b2BodyDef = new b2BodyDef();   
      bodyDef.position.Set((Math.random() * 400 + 120) / m_physScale, (Math.random() * 150 + 50) / m_physScale);
      bodyDef.preventRotation = true;         
      bodyDef.AddShape(torsoBox);
      bodyDef.AddShape(feetBox);
      // create body
      var character:b2Body = m_world.CreateBody(bodyDef);   
      return character;
   }


Now this is a function that you can use to determine if the body is resting.

Code:
public static function isResting(body:b2Body, shapeName:String):Boolean{      
      // loop the contactlist
      for (var cn:b2ContactNode = body.m_contactList; cn != null; cn = cn.next){
         // determine which shape is ours
         var shape:b2Shape;
         if(cn.contact.GetShape1().GetBody()==body){
            shape = cn.contact.GetShape1();
         }else{
            shape = cn.contact.GetShape2();
         }
         // determine if it's the shape we are looking for
         if(shape.m_userData == shapeName){return true;}
      }
      
      return false;
   }      


In my case I would use it like this to determine if my character is resting on it's feet:
Code:
trace("Is my character resting? " + isResting(character, "feet") );


Essentially I have a custom Actor class that has the isResting() function, but that's something you can implement yourself.
Or you could loop through all the contacts, and if the collision normal is pointing up, then the player is on the ground 😉
Do you have a code sample?

Original thread: https://box2d.org/forum/viewtopic.php?t=187