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

Pathfinding and Farseer

Topics: User Forum
Mar 8, 2010 at 1:01 AM
Edited Mar 8, 2010 at 1:38 AM


How can I make a body move from node to node?

I've got the pathfinding to work :D but I can't figure out how i can make my object move by x and y. I've been using ApplyForce until I got to this.

Any ideas?



Edit: I did think of using Body.Position but would that affect anything negatively? I need to be sure before implementing :(.

Mar 8, 2010 at 11:40 AM

I use ApplyImpulse... well not exactly, I have a function called WalkTo(Sector node) and in there I apply the needed impulses to make the character move to the appropiate node.

What type of game is yours? Top down, sidescroller? It depends on that on how you should apply the impulses.

Mar 8, 2010 at 11:58 AM

You can try changing the body's position directly. I'm trying to do the same, but the body does not move like I expect. I modify directly the body position in order to move the object, because in my case is an enemy that is wandering, but the movements are unexpected.

If someone can help me too will be great!!


Mar 8, 2010 at 12:02 PM

Changing the body's position manually doesn't seem like a good idea, as you can confront weird reactions when colliding. For your enemies, ¿don't you have some kind of function for moving them around your map?

Mar 8, 2010 at 12:11 PM

Yes I have functions for that, and I change the body's position there. The problem is that I don't know how to do the same applying forces or impulses. Can you help me or clarifying me how can I do applying forces??

Thank you very much.

Mar 8, 2010 at 3:18 PM
Edited Mar 8, 2010 at 3:19 PM

I'm developing a top down action RPG at this time. If you can clarify how to calculate the needed force, it would be great :).

Mar 10, 2010 at 1:39 PM

You can always apply an impulse (just once) towards the direction you want to move your character, and when you want to make it stop (when it reaches its goal), apply the same impulse to the opposite direction (assuming that there is no friction involved).

Top-Down Action RPG? This sounds like Gauntlet, very nice :)

You can make a function WalkTo(Vector2 point), where at each timestep checks the speed of the character, and then applying impulses according to that. For example (I don't think this code will compile or work but maybe will give you a clue):


private bool WalkTo(Vector2 point)
if((body.Position - point).Length() < 10) // If it is 10 units from the destination, we consider it has arrived
    return true;

Vector2 currentSpeed = body.LinearVelocity; Vector2 impulseNeeded = Vector2.Normalize(body.Position - point) * maxSpeed; body.ApplyImpulse(currentSpeed - impulseNeeded); return false; }



Mar 10, 2010 at 11:58 PM
Edited Mar 11, 2010 at 12:07 AM


Thanks a lot! I modified it and this is what I got:

bool MoveTo(ref Vector2 point) { if ((Position - point).Length() < 10) // If it is 10 units from the destination, we consider it has arrived return true; Vector2 amount = new Vector2(0, 0); amount.X = (float)Math.Round(Math.Cos(MathHelper.ToRadians(angle)), 2) * Force; amount.Y = (float)Math.Round(Math.Sin(MathHelper.ToRadians(angle)), 2) * Force; geom.Body.ApplyForce(ref amount); return false; } Instead of impulse I used force because impulse was acting too unpredictable.

The angle is not necessary, the amount of force can be calculated other ways.

edit: Removed unnecessary code.


Mar 11, 2010 at 8:23 AM

Glad to see it helped :)

Hope to see some screenshots or videos soon, I love this type of games :P