Contact listener problem with sensors [SOLVED]

contact points have two shapes, shape1 and shape2... but they are not guaranteed to be the shapes you expect them to be. for instance, lets say you have a ball and a ground, and say another static ball to bounce off. On first collision the ball hits the static ball. A contact is generated and shape1 is the ball, shape2 is the static ball. However, when it hits the ground, shape1 is the ground, shape2 is the ball.
This is arbitrary and while it 'could' be random, I suspect it is a little more deterministic than that. It may just be that the processing of contacts between sphere and sphere allways returns one thing, while contact with a sphere and a poly might return a different order. There are a lot of ways you can deal with this but the simplest( and albiet a bit messy ) is:
Code:
if( shape1.GetBody().getUserData() is Player && shape1.GetBody().getUserData() is Obstacle )
{
... do something with player as shape1 an obstacle as shape2;
}
else if( shape1.GetBody().getUserData() is Obstacle && shape1.GetBody().getUserData() is Player )
{
... do the same thing but with player as shape2 and obstacle as shape1;
}


I'm not fond of this myself since its duplicating code, but it should serve to help point out where I think the problem is.
True, but again; when I loop through each Added contact point and strictly look at just its position, points fail to plot between dynamic polygon and static circle sensor, when dynamic polygon is in contact with static circle as well.
Hey,

Code:
   public class ContactListener extends b2ContactListener {
      public function ContactListener():void {
      }

      public override function Add(point:b2ContactPoint):void {
         var m_world:b2World;
         var Shape1:b2Body = point.shape1.GetBody();
         var Shape2:b2Body = point.shape2.GetBody();
         if (Shape1.GetUserData() == "Ground") {
            m_world = Shape2.GetWorld();
            m_world.DestroyBody(Shape2);
         }
      }


I use the above code to in the ContactListener.
I want to destroy the bodies which hits the "Ground", but it is not work.
You should really search the forums for an existing thread with your answer in it, or at the very least start a new thread with your question. Jacking someone elses thread for an different question is poor taste and just muddles up the forum. With that said, the box2D documentation( which you should read ) states that you should not alter the world inside a contact listener.

Now back to Redshift, I'm going to throw together a modified testbed to see if I can spot what you are having happen. I reccomend that you try and cut out the problem code into a testbed example so more people can help you in a controlled environment.
Here's a quick demo of the problem.

http://flashtangent.com/stuff/test.html

On the left side we have circular sensors, and on the right we have rectangular sensors.

Notice that every rectangular sensor accumulates at least 1 added contact point regardless whether the box is touching the large circle or not.

However, on the left side there are 5 circular sensors in which did not accumulate any contact points during the time that the box was sliding against the large circle. If you look carefully, you'll notice that contact points for the circular sensors begin appearing as soon as the box just barely loses contact with the large circle.
In your listener, are you simply storing a reference to the ContactPoint / Position, or are you making a copy of that data? If you're just making a reference, I'm pretty sure by the time you get around to using that data, it'll be invalid.

If you are drawing those points directly in the listener though... then nevermind.
mayobutter wrote:
In your listener, are you simply storing a reference to the ContactPoint / Position, or are you making a copy of that data? If you're just making a reference, I'm pretty sure by the time you get around to using that data, it'll be invalid.

If you are drawing those points directly in the listener though... then nevermind.



Oh wow, I can't believe I overlooked that. Thanks! Problem solved. 🙂

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