Pathfinding with Farseer

Nov 13, 2011 at 6:31 PM

I've been searching through the discussions on codeplex as well as google but cannot find anything on Farseer Physics and Pathfinding.  I was wondering if anyone has done any pathfinding (Using A* or any other Algorithm) with Farseer.  My game is a 2d sidescroller/shooter and I would like my enemies to be able to avoid obsticles (Static Bodies) in order to reach the Hero.  Does the Farseer Engine have functionality built in to help with this?  If not, can anyone point me to any tutorials or samples using farseer and pathfinding?  Many thanks in advance.

Nov 13, 2011 at 7:06 PM

thanks to MaskedPixel for showing me this blog:

Nov 15, 2011 at 9:02 AM

The Raycasting seems a good idea. Another option is to decompose your map in small squares (squares that repesent walls, and squares that represent empty space where the enemies can move through) and implement an A* algorithm. Another option is to create your "path map" in the map design. I mean, when you create your map, you add a layer or something else indicating where are the sectors where the enemies can move around, and which sectors connect to which ones, then it would be quite easy to imlement an A* algorithm to move the enemies to the sector your Hero is. This last approach is the one I'm using, here you can see a quick explanation.

Nov 15, 2011 at 9:50 PM

Thanks Guys, I like the idea of using a RayCast.  I havent dealt with RayCasts too much and cant figure out the below: 

public void RayCast(Vector2 position, Vector2 heroPosition)



float _fraction = 0;
const float
l = 25.0f;
Vector2 d = new Vector2(l * (float)Math.Cos(Body.Rotation), -l * Math.Abs((float)Math.Sin(Body.Rotation)));
point2 = position + d;
Engine.World.RayCast((fixture, point, normal, fraction) =>
_fraction = fraction;
Debug.WriteLine(fixture.UserData + "-");
return fraction;
}, position, point2);

I am using the code above which I followed from TestBed sample.  I call this RayCast method from my update but it seems like nothing from within Engine.World.RayCast is being called.  My points are all defined and debugging/stepping through this looks to skip anything from inside the RayCast.

Nov 16, 2011 at 1:23 PM
Edited Nov 16, 2011 at 1:24 PM

Engine.World.Raycast( (fixture,point,normal,fraction) =>
      if (fraction < minfraction)
}, position, point2);

or set a boolean to false to check if the path os free