A body within another body.

Hello there
How to Check if a body is completely within another body that has the isSensor set to true?
The code below only checks if a those 2 bodies have contact.



Code: Select all

package 
{
   import Box2D.Dynamics.b2ContactListener;
   import Box2D.Collision.b2ContactPoint;
   
   public class ContactListener extends b2ContactListener
   {

      public function ContactListener()
      {
         
      }
      
      override public function Persist(point:b2ContactPoint):void
      {
         
         if(point.shape1.GetBody().GetUserData() is CrateActor && point.shape2.GetBody().GetUserData() is BoxSensorActor)
         {
            trace("Crate is where it should be!");
         }
         else if(point.shape1.GetBody().GetUserData() is BoxSensorActor && point.shape2.GetBody().GetUserData() is CrateActor)
         {
            trace("Crate is where it should be!");
         }
            
         
         
         super.Add(point);
      }

   }
   
}
I don't think there is an easy way to check if a shape is within another shape. You could do it by having some long thin sensor shapes (perhaps a chain shape) to enclose the goal area. The crate would be in the goal when it was not touching any of these sensors, and its position was within a certain distance of the middle of the goal.
Yeah exactly what i was thinking. Im working on it now.
theres also another method with distance betwen 2 bodies i think

Like this:

var distance:Number=Math.sqrt( ( (redGoalActor.redGoalBody.GetPosition().x * Vals.RATIO) - (boxSensorActor.boxSensorBody.GetPosition().x * Vals.RATIO)) * ( (redGoalActor.redGoalBody.GetPosition().x * Vals.RATIO) - (boxSensorActor.boxSensorBody.GetPosition().x * Vals.RATIO) ) + ( (redGoalActor.redGoalBody.GetPosition().y * Vals.RATIO) - (boxSensorActor.boxSensorBody.GetPosition().y * Vals.RATIO) ) * ( (redGoalActor.redGoalBody.GetPosition().y * Vals.RATIO) - (boxSensorActor.boxSensorBody.GetPosition().y * Vals.RATIO) ) );

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