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?
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]);
}
// 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;
}
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;
}