Turn a Sensor into actual Collision before imminent hit

Hey guys, thanks in advance for reading this and possibly helping out. I'm using Box2D 2.1a (Flash).

I've got a game object (a Bullet, so with CCD) that I conditionally need to simulate the physics of. Regularly the object will not be simulated but there is a special case that depends on how it hits the other object. Specifically, it depends on whether it's going to be a deflection or a dead-on hit. This will be calculated using the difference of the angle between the centers and the angle in which the object was moving.

The way I'm doing this is making it a sensor until it's about to hit, then removing that property (SetSensor(false)).
That's all fine and dandy but the problem is that I'm getting weird results when I simulate. I'm guessing this is because I've changed it too late and the physics are being computed when the object is INSIDE the other object.

I've used 2 methods to toggle the sensor-ness before the collision.
1) Using the ContactListener class to modify the contact (which is Locked for the duration of the World.Step() so I buffer the change till after).
2) Using a Raycast to check for collision 1 frame early and then change the body's fixture to be a non-sensor.

Both of these methods simulate unsatisfactorily. So my question is: Is there any better way to do this?
Code:
// Method 1
// called by collision listener on beginContact
             override public function collisionStart(enemy:Enemy, contact:b2Contact):void {
                       Engine.bufferActionAfterPhysics(contact.SetSensor, [false]);
             }

Code:
// Method 2
       override protected function enterFrameHandler(e:Event):void {
         super.enterFrameHandler(e);
         
         handleImminentCollisions();
         
         if(isInbounds() == false) {
            // remove the object
            remove();
         }         
      }
      
      // Raycasts to where it'll be next and checks for hit
      protected function handleImminentCollisions():void {
         Engine.world.RayCast(raycastCallback, body.GetPosition(), new b2Vec2(body.GetPosition().x + body.GetLinearVelocity().x,body.GetPosition().y + body.GetLinearVelocity().y) );
      }
      
      protected function raycastCallback(fixture:b2Fixture, point:b2Vec2, normal:b2Vec2, fraction:Number):Number {
         var hitActor:Actor = fixture.GetBody().GetUserData();
         braceForCollision(hitActor, point);
         return 0;// immediately terminate
      }
      
      // Override this method in the subclasses
      protected function braceForCollision(hitActor:Actor, hitPoint:b2Vec2):void {
                        // remove sensor-ness: body.GetFixtureList().SetSensor(false); (only has 1 fixture btw)
         return;
      }
I actually read about something along the lines of what I was doing on the forum. It involved having 2 fixtures, 1 sensor, 1 non-sensor. The fixture had a very low density (so it would hardly affect the colliding object) and the sensor would cause the second fixture to change density to it's normal value in the special case.

I'd like to try this as a last resort because manipulating densities to get collision/no collision sounds like a hack job to me. :/
if i understand this right, I would say you are going about this the wrong way.

from the use of the word "fixture" i can assume you are using 2.1 (which is required for my suggestion)

what i would strongly recommend is not using sensors at all.
non-sensors send a PRE_SOLVE event, which gives you information about the contact that is about to take action.

if you want your body to "act like a sensor" and ignore the collision, this is the last possible moment to make that decision, and it only happens on real collisions, rather than trying to determine what will be a collision.

here is a tutorial on making one what platforms by disabling collisions of certain angles.
http://www.emanueleferonato.com/2010/03 ... ka-clouds/

that should be similar to what you are trying to do.
Perfect. That's exactly what I wanted to do. Thanks
Actually, there's a problem now. It's annoying.
I'm tracing whether I disable or enable the contact at Presolve, and whether it is enabled or disabled on beginContact.

disabled // okay I've disabled the contact (Presolve) | Frame 1
disabled // still disabled | Frame 2
disabled | ...
disabled
disabled
true // (beginContact) it collides, but it says the contact is enabled now? WTF
disabled // It's IN (overlapping) the other object now because my code to destroy it if it's disabled didn't work because it was enabled
disabled
disabled
disabled
disabled
disabled
disabled
disabled
disabled
enabled // because it goes through the object the condition changes so it gets enabled. This is correct, problem is it shouldn't exist at this point!
enabled // It's inside, a collision causes the object to jump out of the other one (jitter)
enabled
enabled

Okay so I'm thinking this is what happens:
disabled // I disable it, BUT according to documentation "The contact is only disabled for the current time step (or sub-step in continuous collision)." so it's disabled for this step
true // but then beginContact() is called on the NEXT time step so the contact is enabled or something like that?

I'm confused, please help clear this up for me. Thanks in advance for any contribution.
not sure what your calculating at begin contact.

and begin-contact should not happen if the contact was disabled at pre-solve. i think.
i usually don't use begin-contact if im using pre-solve
Here's the dode (basically a glancing shot from a bullet or a direct shot). It's annoying how flash uses various methods to measure angles.

Code:
override public function preSolve(contact:b2Contact, manifold:b2Manifold):void {
         var enemyBody:b2Body = (contact.GetFixtureA().GetBody().GetUserData() is Enemy) ? contact.GetFixtureA().GetBody() : contact.GetFixtureB().GetBody();
         var body:b2Body = (contact.GetFixtureA().GetBody().GetUserData() is Bullet) ? contact.GetFixtureA().GetBody() : contact.GetFixtureB().GetBody();
         var angleToObject:Number = -180*(Math.atan2(enemyBody.GetPosition().x - body.GetPosition().x, enemyBody.GetPosition().y - body.GetPosition().y) - Math.PI/2)/Math.PI;
         if (angleToObject > 180) {
            angleToObject -= 360;
         }
         var direction:Number = 180*body.GetAngle()/Math.PI;
         
         var offset:Number = angleToObject - direction;
         if (offset < -180) {
            offset += 360;
         }
         
         if (Math.abs(offset) < 40) {
            // Not a deflection (no physics needed)
            // The contact is only disabled for the current time step (or sub-step in continuous collision).
            contact.SetEnabled(false);
            disabled = true;
         } else {
            // Physics needed
            contact.SetEnabled(true);
            disabled = false;
         }         
      }


So I added a boolean that persists (unlike SetEnabled) after world.Step(). This seems to have fixed the issue. I thought that beginContact shouldn't fire either if it was disabled (infact I spent an hour on that theory) but I decided to trace and indeed it does get fired! I feel stupid though.

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