Ray goes through body

I've been thinking about this for a week or so but still can't figure out what's causing the issue. It's happening on arbitrary times, locations, angles.. for no apparent reason.

Image
There it only happened on that marked angle.

The code is simply as follows,

Code: Select all

private function onEnterFrame(e:Event):void
{
// raycast from enemy to player
var enemyPos:b2Vec2 = enemy.body.GetWorldCenter();
var playerPos:b2Vec2 = player.body.GetWorldCenter();
            
world.RayCast(callback, enemyPos, playerPos);
}

private function callback(fixture:b2Fixture, point:b2Vec2, normal:b2Vec2, fraction:Number):Number
{   
   _fixture = fixture;
   _intersection = point;
   return fraction;
}


Here's how I draw the ray, though I'm sure that it's nothing to do with this issue.

Code: Select all

// draw rays
rays.graphics.lineStyle(1, Color.RED);
rays.graphics.moveTo(To.px(enemyPos.x), To.px(enemyPos.y));
rays.graphics.lineTo(To.px(_intersection.x) , To.px(_intersection.y));


Note: Game has no gravity
Returning the fraction from the callback will often cause hits to be missed because the fractions are not returned in order. You need to always return 1 so it finds everything I'm afraid.

In your callback:
Record the first hit, then if another occurs check if its fraction is lower. If it is, replace the record.

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