Public Group

# How to follow a path?

This topic is 3998 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Example: Node1(50, 15) Node2(200, 100), Node3(10, 122) Player moves from Node1 to Node2, once he reaches Node2 he moves on to Node3 Pseudo code is enough, but please keep in mind my understanding of radian, sin and cos stuff is horrible.

##### Share on other sites
You don't need any trigonometric functions. First calculate a delta vector:

delta.x = node2.x - node1.x
delta.y = node2.y - node1.y

And then decide on the number of steps you want to take on the path

steps = 100

Then you can iterate over the steps and calculate a new position each time:

for i from 0 to steps do
position.x = node1.x + delta.x * i / steps
position.y = node1.y + delta.y * i / steps

If you don't want a constant number of steps but a constand velocity, you can set

steps = (int) (sqrt(delta.x*delta.x +delta.y*delta.y) / velocity);

##### Share on other sites
Quote:
 Original post by pascalostiExample: Node1(50, 15) Node2(200, 100), Node3(10, 122)Player moves from Node1 to Node2, once he reaches Node2 he moves on to Node3Pseudo code is enough, but please keep in mind my understanding of radian, sin and cos stuff is horrible.

Do you mean that you need some kind of artificial intelligence which is able to follow the path or just a simple thing such as camera following a predefined path ?

 as DevFred pointed out, for the position interpolation you don't need trigonometry, but if you want your player to head to the right direction, you'll need to calculate the rotation angle from the heading vector.

##### Share on other sites
Once you have the piecewise linear interpolation up and running, you could smooth things out with very little work by using Catmull-Rom splines.

##### Share on other sites

Im not sure how steps (const velocity) is implemented, it either stays or I get super high number. Its not working yet Ill try again tomorrow

        public void P_Moving()        {            //Pathway is a List < nodes >            // plus ( sign doesnt show for some reaon)            if (PathWay.Count > (Counter plus 1))            {                Vector2 delta = new Vector2();                delta.X = PathWay[Counter plus 1].Data.Position.X - PathWay[Counter].Data.Position.X;                delta.Y = PathWay[Counter plus 1].Data.Position.Y - PathWay[Counter].Data.Position.Y;                int steps = (int)(Math.Sqrt(delta.X * delta.X plus delta.Y * delta.Y) / this.P_PawnState.Speed);// I divided steps, not sure if thats correct (for constant velocity)                this.SG_Position plus= new Vector2((PathWay[Counter].Data.Position.X * delta.X) / steps,                                                    (PathWay[Counter].Data.Position.Y * delta.Y) / steps);                                // if player is close give player new position and next target                if (this.VacinityNode(30, PathWay[Counter plus 1]))                {                    this.SG_Position = PathWay[Counter plus 1].Data.Position;                    Counter plus= 1;                                    } // if 2            }// if 1            if (Counter == PathWay.Count)                this.P_PawnState.State = 1;// reached target/ turn player state to "still"         }// end moving function

##### Share on other sites
Here is a much shorter code.
the question is about the last line of this code
divide steps? (doesnt work for me)

// from devfred's help trying to get constant velocity// moving from Pathway[0] to Pathway[1]// this.SG_Position is the player/moverdelta.X = PathWay[1].Data.Position.X - PathWay[0].Data.Position.X;delta.Y = PathWay[1].Data.Position.Y - PathWay[0].Data.Position.Y;int steps = (int)(Math.Sqrt(delta.X * delta.X + delta.Y * delta.Y) / 5);// 5 is the speedthis.SG_Position += new Vector2((this.SG_Position.X + delta.X) / steps,                                                   (this.SG_Position.Y + delta.Y) / steps);

##### Share on other sites
[EDIT - Sorry, completely changed this post while you were replying below, your point below is, of course, correct]

Surely if you have your delta vector (destination vector minus origin vector), you can just divide it by the desired number of steps to get a vector representing a single step?

Assuming you want a constant speed regardless of the length of each vector, you could just divide the lenght of the vector by the speed to get the step.

Then you can just add that to the position each turn to move the correct distance towards the destination?

delta.x=dest.x-orig.x;delta.y=dest.y-orig.y;float length=sqrt(delta.x*delta.x+delta.y*delta.y);float steps=length/speed;step.x=delta.x/steps;step.y=delta.y/steps;position.x+=step.x;position.y+=step.y;

##### Share on other sites
edit: ill give it a try, thanks (i think post was edited or i missed the some parts) nevermind the stuff below.

edit 2: Okay so it was edited (im not loosing my mind :-) ) thanks again.

Yeah that would work, but your velocity would change, from one path to the next.

example:
n1 take 100 steps to n2

n2 take 100 steps to n3

if the distance from n2,n3 is longer then n1,n2 there will be a change of velocity.

Correct me if im wrong or missed your point.

##### Share on other sites
My fault. Sorry for the confusion. Should have thought about my post more before I hit the button [smile].

##### Share on other sites
I tried it and it worked like a charm. I think ill implement your neater suggestion also.
cheers

1. 1
Rutin
33
2. 2
3. 3
4. 4
5. 5

• 9
• 13
• 82
• 11
• 10
• ### Forum Statistics

• Total Topics
632969
• Total Posts
3009595
• ### Who's Online (See full list)

There are no registered users currently online

×