# Thread: Incrementing along a vector

1. ## Incrementing along a vector

Hi again,

Im making a program in c++ using openGL (glut) but having a little trouble figuring out how i would increment along a certain path...

I have quite a few functions that require it like the one below, where i have the Point (contains x,y,z components) of the waypoint i need to head towards, and i have the angle to that waypoint from the current x,y,z position. Plus i can get the current position Point by just calling body->Get_Position(); if i need too.

But im just not sure how i would set the incrementation Point's x,y,z to send to the Set_Position function.

I would take a guess and say that if its walking and the angle rotates the view so its facing the waypoint, i would just increment the -z value of the current Point position by WALK_SPEED ? (WALK_SPEED is a #define)

but im not sure. Could anyone please give me some advice?

Thanks,

Code:
```void AI::Patrol() {

body->set_Walking(true);
body->set_Running(false);
body->set_Still(false);

Point increment;
Point wp = Next_Waypoint();
float angle = Get_Position_Angle(wp);
float range = Get_Range(wp);

//How to get the incrementation in step?

//increment.x = ?
//increment.y = ?
//increment.z = ?

//WALK_SPEED ?
//RUN_SPEED ?

body->Set_Position(increment);
body->Set_Yaw(angle);
}```

(i get the angle through this function, which i hope is correct)
Code:
```float AI::Get_Position_Angle(Point pos) {

float angle;

//Get the float angle between NPC position and parametre position
angle = atan2(body->Get_Position().z-pos.z, body->Get_Position().x-pos.x)*(180 / PI);
angle -= body->Get_Yaw();

return angle;
}```

2. please could someone give me some advice on this? :P
/beg

3. Okay, here's some code for how you handle things; note of course you'd need to define the operators +, -, and *, and the normalize function:

Vector position; // This should be set with the current position
Vector destination; // The x,y,z, coordinates of the destination

Vector path = destination - position; // Subtract to get a vector representing the path we are walking
double stepsize = 10.0; // How many units we should move in one step
Vector step = path.normalize() * stepsize; // Here, normalize() returns a new vector that is the normalized (i.e. length 1) version of path.

position = position + step; // Update the location

Popular pages Recent additions