This project has moved and is read-only. For the latest updates, please go here.

Testing for line of sight

Topics: User Forum
Aug 21, 2010 at 9:56 PM

I'm trying to figure out how the World.RayCast method works. What I need is a simple line-of-sight test from one Body to another. In this case, I need to know if there is a clear line-of-sight between an AI and the player with no walls between them (though some other objects, like bullets, should not break up the ray cast). I haven't quite understood how one must use the RayCastCallback for this sort of thing. Could someone show me with a bit of pseudocode how it would be done?

Aug 22, 2010 at 9:29 PM
Edited Aug 22, 2010 at 9:30 PM

This is how I do it in my top down shooter. 

The Engine.Physics.PositionToWorld() is my function to convert from game cords to physics cords.

The Globals.CollisionGroup[Group.Map] is just an enum for categories.

If you have anymore questions let me know.


//used for ray casting
static bool m_CanSee = false;

/// <summary>
/// Checks to see if a position is visible from another position
/// </summary>
/// <param name="start">start position</param>
/// <param name="end">position to see</param>
/// <returns>returns true if clear LOS else false</returns>
public static bool CanSeePosition(Vector2 start, Vector2 end)
    Engine.Physics.World.RayCast(RayCallBack, Engine.Physics.PositionToWorld(start), Engine.Physics.PositionToWorld(end));

    return m_CanSee;

private static float RayCallBack(Fixture fixture, Vector2 point, Vector2 normal, float fraction)
    if (fixture.CollisionCategories == Globals.CollisionGroup[Group.Map])
        m_CanSee = false;
        return 0f;
        m_CanSee = true;
        return -1f;



Aug 22, 2010 at 9:51 PM