Sensor Difficulties [resolved]

I finally decided to try to come to grips with a sensor. After a day of scratching my head I think I've narrowed down my problem:

First I go creating my objects, this is in my main object creation:
Code:
         shapeDef.isSensor = true;
         shapeDef.userData = "GroundSensor";
         var theshape = body.CreateShape(shapeDef);
         trace("it is:",theshape.GetUserData()); // Returns GroundSensor


I've confirmed my geometry is correct and debugdraw shows the GroundSensor poking into the ground at various times. However, in contactlistener:

Code:
      public override function Add(point:b2ContactPoint) : void {
         if (point.shape1.GetUserData()) {
            if (point.shape1.GetUserData() == "GroundSensor") {
               point.shape1.GetBody().GetUserData().setGrounded(true);
               trace("grounded");
            } else trace("no:",point.shape1.GetUserData());
         } else trace("no userdata");


No matter what happens in the simulation, this block of code only ever traces "No Userdata". I also repeat for Shape2 with similar results. I'm also getting this in the overriden "Persist" and "Remove" functions. 🙁

What's funny is I have this working perfectly for non-sensor objects. *sighs*

Can anyone give me a pointer as to where I'm going wrong?
Try shape2. Are you assuming it would only be shape1?
I've used sensors against static and dynamic bodies before and it worked fine. What I would do first, before assuming its box2d not detecting it, is make sure your comparison is turning out how you expect. Make sure your sensors are getting the user data set correctly. After that, trace out if the object is a sensor or not( using IsSensor() should work ) plus its userdata. That at the very least will tell you more about the problem.
Yep, I mentioned up above that I do an equivalent check on shape 2 as well.

Also in my first example, after I build all my shapes I trace the values to make sure they "stuck," so that's working allright. But just to be sure I just now put this in my main loop:

Code:
               if (b.GetUserData().getClass() == "Player") {
                  var s:b2Shape = b.GetShapeList();
                  do {
                     trace("isSensor:",s.IsSensor());
                     if (s.GetUserData()) trace("shapeData:",s.GetUserData());
                  } while (s = s.GetNext());


My player object is two shapes (for now, just two squares) and each is reporting properly. One sensor, one not, sensor has the correct "GroundSensor" userdata.

It looks like the sensor simply isn't firing when it intersects with other objects. :/

I put this in my contactListener for more clarity:

Code:
         if (point.shape1.GetBody().GetUserData().getClass() == "Player") {
            trace("player body touched somewhere");
            if (point.shape1.GetUserData()) trace("Shape that touched has userdata:",point.shape1.GetUserData());
               else trace("Point that touched does not have userdata.");
         }
                        (.. and same for shape2)


It looks like my player is indeed touching the ground, as this block of code returns "Player touched" and then the userdata is returning that of the non-sensor object. I'm verifying visually that the sensor object is protruding from the bottom of the character and is deep into a static object via debugdraw. 🙁
Try if ( (point.shape1.GetUserData() as String) == "GroundSensor" )

Or try and put "GroundSensor" into a static constant variable and then use that for assignment/comparisons instead explicitly typing "GroundSensor" each time.

I had a similar problem with String equalities once and doing the latter fixed my problem. Don't ask me why. I really don't know if either of the above will work or why it would or why your way wouldn't work for that matter, but these are some things to try.

I just have a feeling it's the String comparison that is failing you. Have you tried the same code but with a uint instead of a String?
Thanks for the suggestion Jive - I will indeed have a proper class for my userData once I get it working. For sake of simplicity I actually removed it during my head-on-wall frustrations.

In this particular case, my last block of code would have revealed that as the error - right now it's reporting there's a contact, the contact is from the "Player" body, and the userData for the shape is "PlayerBox". This makes sense, as the player should be resting on the ground as well.

The sensor is not triggering the contact result at all - and if it was a casting error it would have said so in my "else trace no userdata" or some such. I'm simply getting a single hit - on the player's box, not on the sensor box.

When I create the sensor maybe I'm missing some configuration somewhere? The manual and API are very sparse on sensor details. Here's what I've got for construction of the sensor:
Code:
         shapeDef.SetAsOrientedBox(width/4, height/10, new b2Vec2(0,height/2));
         shapeDef.isSensor = true;
         shapeDef.userData = "GroundSensor";
         body.CreateShape(shapeDef);

I've tried moving around the starting position of the box, even place it well outside of my player, and I'm getting the same results.
I'm using a sensor shape to track targetting information in my shooter. I don't see anything you're missing in your shape definition. Really the only thing that makes it a sensor is the isSensor boolean. You don't have any masking bits or groupIndex set that would prevent your Ground shape from registering a hit with the sensor? Also I don't see any explicit use of a physics scale. That's not an issue is it?

Oh yea. I remember at one point I actually had to resort to using circle shapes instead of polys. I don't know if it was a bug in the release of Box2D I've been using (I'm pretty lazy about keeping current with that) or some problem with the way I set things up but switching to circles fixed my problem so I didn't care to look further. It was probably something I was doing wrong, but maybe try your code with a circle shape def and see if that registers a hit. Again these are vague and nebulous suggestions, but easy enough to plug in and try.

But again I can't see why what you're doing wouldn't work.
Turn off the sensor flag, and turn on debug draw, so you can verify that this shape is where you think it is, and would ordinarily collide with other shapes (i.e. it is not filtered out).
BorisTheBrave wrote:
Turn off the sensor flag, and turn on debug draw, so you can verify that this shape is where you think it is, and would ordinarily collide with other shapes (i.e. it is not filtered out).


*slaps forehead*

*slaps forehead really really hard*

Yeah I forgot to apply my masking scheme. Damnit. Thanks for the tip to turn off the sensor and see if it collides normally - that was my moment of realization. I had *thought* the masking was an attribute of the Body, not the Shape, so it never occured to me.

*sighs heavily*

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