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

Detecting if an object is visible from other

Feb 24, 2010 at 8:06 PM

Hello, I'm doing a project with the Farseer engine, I'm newbie and I have one question that I don not know how to figure out. I have a 2D platform game and two character. I want to detect when one of them is visible from the other, for example if one character is behind an object the other character can't see it.

It is possible to do with the engine? Sorry if the question is too obvious.

Regards and thank you in advance.

Feb 25, 2010 at 8:10 AM

Raycasting is what you need. Raycasting traces a line between two points and returns all the geoms that intersect with it. Use the RayHelper class, I can't remember the exact method right now, but is't something like getallgeomsintersect.

Feb 25, 2010 at 10:20 AM

Thank your pnikosis, I was trying with the rayhelper, but I do not know how it works really. Is suposed that the collisions points are stored in the last attribute of the method?


Thank you very much.

Feb 25, 2010 at 5:10 PM

Here's the method (now I'm in front of my code :P):

List<GeomPointPair> lg = RayHelper.LineSegmentAllGeomsIntersect(ref p1,
                ref p2,

p1 and p2 are the two points you want to check, then you get a list of geoms with all the points where they intesrsect with the segment (lg in the code)

Then you can iterate all the geoms:

foreach (GeomPointPair gpp in lg){
    Geom geom = gpp.Geom;
    List<Vector2> points = gpp.Points;
Hope this helps :)