Moving over from APE: Solid and Collidable flags?

Gah! You're right. For some reason I felt the need to attach the radar as a separate body to another body with a joint. I just moved the radar shape to the body I actually wanted it attached to and now it's working wonderfully.

Thanks!
@NintendoBen:

I was just wondering how the sensor code you posted is working out for you. You said you hadn't done a lot of testing, but that it seemed to work. I followed the instructions you gave in your post and while other shapes pass through my sensor, it doesn't appear to be showing up on the contact list. I added a "name" property to all the shapes, and named them things like "Box", "Polygon", "Circle", and the sensor is named "Sensor". I had it trace out a list of the names for each contact, but Sensor never showed up on the list, while everything else seemed to show correctly.

I was just wondering if you could explain a little more precisely what modifications you made if everything is confirmed working correctly for you. I think your instructions were pretty good, but I never underestimate the possibility of misinterpreting even the most basic of instructions 🙂

I created the variables as you said in b2ShapeDef and b2Shape, making sure to set "sensor" to false in the constructor of b2ShapeDef. For "m_sensor" I put the following code in the constructor:

Code:
m_sensor = def.sensor;


This is what the beginning of my Collide() function looks like now:
Code:
   public function Collide():void {
      var body1:b2Body;
      var body2:b2Body;
      var node1:b2ContactNode;
      var node2:b2ContactNode;
      
      for (var c:b2Contact = m_world.m_contactList; c != null; c = c.m_next)
      {
         if (c.m_shape1.m_body.IsSleeping() &&
            c.m_shape2.m_body.IsSleeping())
         {
            continue;
         }
         
         if (c.m_shape1.m_sensor ||
            c.m_shape2.m_sensor)
         {
            continue;
         }


Please let me know if I messed anything up!

I am wondering if anyone else has made this modification with this method or another. I have been studying the code trying to understand where the b2Contact is added to the b2ContactSolver but the code is very complicated to understand because I am not familiar with it yet that I think it will probably take a while. So in the meantime I would greatly appreciate if someone could help out. Thanks!
Ok so I figured it out finally. Actually I figured it out a few hours ago and have been too busy playing with it to share 😀 I didn't think very many people would be up to notice yet anyhow though. So here's what I did to get sensors working. Basically the beginning part is exactly as NintendoBen said with creating the two boolean "sensor" and "m_sensor" variables and setting the one to false in the constructor as I repeated in my previous post.

For the second part though I got rid of what I had added to b2ContactManager in the "Collide" function since this seemed to make the sensors not show up on my contact list at all. I found what I believe is the correct spot in b2World.as, in the Step() function. You have to scroll down a little in the function to around the comment that says "Search all contacts connected to this body." This is what the code in the section where I changed looks like:
Code:
            // Search all contacts connected to this body.
            for (var cn:b2ContactNode = b.m_contactList; cn != null; cn = cn.next)
            {
               if (cn.contact.m_flags & b2Contact.e_islandFlag)
               {
                  continue;
               }

               if (cn.contact.m_shape1.m_sensor == false && cn.contact.m_shape2.m_sensor == false)
               {
                  island.AddContact(cn.contact);               
                  cn.contact.m_flags |= b2Contact.e_islandFlag;
               }

               
               other = cn.other;
               if (other.m_flags & b2Body.e_islandFlag)
               {
                  continue;
               }


The only thing I changed was to add the second "if" statement, but the two lines inside it are there to begin with, just without the if. Keep in mind this is working from version 1.4.2. I have tested it and it is working with two separate tests. One is deleting all objects that come into contact with the sensor, and the other is applying an upward force to objects within the sensor.

All I know is that this works visually in that objects can pass through the sensor, and the sensor has the desired effect on those objects. I'm not absolutely sure that there isn't some hidden bug effect at work here, or that I'm coding this in the most efficient way. Actually I wasn't even sure of what needed to be in the "if" statement except AddContact seems to be what puts the contacts into the list for the b2ContactSolver. Maybe that should be a continue statement inside the "if" instead, I'm not really sure. Erin or maybe Skatehead probably would know better 😉
Sensors are coming for 2.0. Soon ...
Erin Catto wrote:
Sensors are coming for 2.0. Soon ...


Erin, can you comment on whether the suggested work-around provided by brainclog is along the right lines? I need to implement this feature ASAP, and unless 2.0 is coming this week need to get something stable in today/tomorrow.

Just from a casual reading of the code, it appears as though this will allow contacts for a sensor to be determined but will not pass them to the solver to be "resolved." Is there more to sensors than that?
I would also like to hear a comment from Erin about whether I've done it correctly, but I realize he's busy from the incredible rate of progress on the TODO list (many updates last night!). I can tell you that IT DOES WORK though! I am just not sure of the stability or possible memory leak problems. All tests so far seem to be completely stable, even with dynamic creation/deletion of sensor projectiles. According to the TODO list in the SVN, sensors are completed, but I'm having trouble finding references to "sensor", except for a variable in b2Shape. If you hunt through the source you could probably figure out how their implemented, but no luck here so far.
I think your solution will work fine. The only downside is that you are growing the simulation island across the sensor contact. Here's what I have in SVN:

Code:
// Search all contacts connected to this body.
for (b2ContactNode* cn = b->m_contactList; cn; cn = cn->next)
{
   // Has this contact already been added to an island?
   if (cn->contact->m_flags & (b2Contact::e_islandFlag | b2Contact::e_nonSolidFlag))
   {
      continue;
   }

   // Is this contact touching?
   if (cn->contact->GetManifoldCount() == 0)
   {
      continue;
   }

   island.Add(cn->contact);
   cn->contact->m_flags |= b2Contact::e_islandFlag;

   b2Body* other = cn->other;

   // Was the other body already added to this island?
   if (other->m_flags & b2Body::e_islandFlag)
   {
      continue;
   }

   b2Assert(stackCount < stackSize);
   stack[stackCount++] = other;
   other->m_flags |= b2Body::e_islandFlag;
}


A contact that has a sensor is marked as non-solid. So it is not added to the island and not traversed. You will still get updates for the sensor contacts as long as a at least one of the bodies is dynamic.
Clever. I like the way you do it, Erin (though I like NintendoBen/brainclog's approach because it's so quick to implement!). Doesn't sound like your approach is but a few more lines, though...

I guess yours accounts for the situations where you have two islands that touch only at sensors... yours doesn't merge them into one island whereas the earlier solution would merge them into a single island?
Quote:
I guess yours accounts for the situations where you have two islands that touch only at sensors... yours doesn't merge them into one island whereas the earlier solution would merge them into a single island?


Correct.
Hello I'm just new here sorry if my questions are quite dumb or something :p

anyways here goes,
I've finished doing the sensor and m_sensor stuff.
I've managed to make the b2ShapeDef objects with their sensor set to true
move like ghosts visually.
But I can't seem to find out how to code when the sensor shape is colliding with an ordinary shape

I'm using the TestBed as of now.
I was wondering if I should be seeing the contact points when I overlap the shape sensor and the ordinary shape when the CONTACTS check box at the right is enabled. Because at the moment I'm not seeing them >_<' hmmm

Can somebody tell me where did I go wrong? :p

thanks ^_^

*EDIT: I've tried brainclog's solution though what happened was
I was getting a runtime error when shapes with sensor set to false collides
and when a shape wish a sensor on collides with a normal shape it has the physical properties again hmmmm I'm so lost >_<'

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