# Calculating a velocity based on two coordinates.

• 02-17-2011
~Kyo~
Calculating a velocity based on two coordinates.
I am trying to code velocity for a player at some position to move to another position. The velocity will be based off a maximum move speed.

My thought process is to make a ratio then take that ratio and multiply it by the difference between the two points on the x-axis then take the move speed and multiply it to get the final move speed in the y-axis. Repeat for y-axis. The code I have at the moment will not move me at all and I am unsure why.
move_speed is 15. In case you all are wondering.

Code:

```void Player::update() {     if((clock() - Last_Update) > 50)     {         float ratio = (abs(grapple_x-x_coord)+abs(grapple_y-y_coord))/(abs(grapple_x-x_coord)*abs(grapple_y-y_coord));         x_vel = move_speed*ratio*(grapple_x-x_coord);         y_vel = move_speed*ratio*(grapple_y-y_coord);         Last_Update = clock();         x_coord += x_vel;         y_coord += y_vel;         if(x_coord < 0)x_coord = 0;         if(x_coord > 960)x_coord = 960;         if(y_coord < 0)y_coord = 0;         if(y_coord > 704)y_coord = 704;     } }```
• 02-17-2011
GReaper
Use 2D vectors. They're easier to implement and understand.
• 02-17-2011
~Kyo~
Ahh I forgot to cast the abs() into floats now it is working. Too bad it does have a slight glitch with jumping over a spot that is between two movements, but that doesn't seem as bad.
• 02-17-2011
~Kyo~
The final code... Works like a charm too.

Code:

```void Player::update() {     if((clock() - Last_Update) > 50)     {         Last_Update = clock();         if(grapple_x != -1 && grapple_y != -1)         {             if(x_coord != grapple_x)x_vel = move_speed*(double)(grapple_x-x_coord)/((double)abs(grapple_y-y_coord)+(double)abs(grapple_x-x_coord));             else x_vel = 0;             if(y_coord != grapple_y)y_vel = move_speed*(double)(grapple_y-y_coord)/((double)abs(grapple_y-y_coord)+(double)abs(grapple_x-x_coord));             else y_vel = 0;             x_coord += x_vel;             y_coord += y_vel;             if(abs(x_coord-grapple_x) < move_speed+1 && abs(y_coord-grapple_y) < move_speed+1){grapple_x = -1;grapple_y = -1;}//correcting for being close, but not on top of a coordinate keeps player from spasming.         }         if(x_coord < 0)x_coord = 0;         if(x_coord > 960)x_coord = 960;         if(y_coord < 0)y_coord = 0;         if(y_coord > 704)y_coord = 704;     } }```
• 02-17-2011
Sebastiani
Just FYI, it might simplify things a bit to encapsulate common operations into (preferably generic) functions, eg:

Code:

```template < typename Type > inline void clamp( Type& value, Type const& low, Type const& high ) {         if( value < low )                 value = low;         else if( value > high )                 value = high; }```
...then you could rewrite that bit like so:

Code:

```void Player::update() {     if((clock() - Last_Update) > 50)     {         Last_Update = clock();         if(grapple_x != -1 && grapple_y != -1)         {             if(x_coord != grapple_x)x_vel = move_speed*(double)(grapple_x-x_coord)/((double)abs(grapple_y-y_coord)+(double)abs(grapple_x-x_coord));             else x_vel = 0;             if(y_coord != grapple_y)y_vel = move_speed*(double)(grapple_y-y_coord)/((double)abs(grapple_y-y_coord)+(double)abs(grapple_x-x_coord));             else y_vel = 0;             clamp< double >( x_coord += x_vel, 0, 960 );             clamp< double >( y_coord += y_vel, 0, 704 );             if(abs(x_coord-grapple_x) < move_speed+1 && abs(x_coord-grapple_y) < move_speed+1){grapple_x = -1;grapple_y = -1;}//correcting for being close, but not on top of a coordinate keeps player from spasming.         }                    } }```
Just a suggestion. :)
• 02-17-2011
~Kyo~
Yea that does work well ended up changing from double to int for that part though. Only really need the doubles for the abs() calls. First time ever using a template I understand whats going on except with the keyword inline not sure exactly what it is telling the compiler.

Don't really need the & on the const vars do we? Not changing emm hence const...