# a to b movement

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

## Recommended Posts

Hi! I am starting to learn SDL and c++ by making a little game and I need a function that tells me the percent of xvel or the percent of yvel needed to move from one point to another. For example if I want a dot to move from my character to my cursor I give the function character.x, character.y, cursor.x, cursor.y and it should return the percent of xvel needed. I tried making the function on my own before but I failed.

##### Share on other sites
Normalize the vector between the 2 points (x2-x1)/(Magnitude), (y2-y1)/(Magnitude). Then say you have a velocity of 5, to get the velocity in the X direction you take 5* Normal.X. To find the velocity in the Y direction it will be 5* Normal.Y.

##### Share on other sites
Quote:
 Original post by JonConleyNormalize the vector between the 2 points (x2-x1)/(Magnitude), (y2-y1)/(Magnitude). Then say you have a velocity of 5, to get the velocity in the X direction you take 5* Normal.X. To find the velocity in the Y direction it will be 5* Normal.Y.
I noticed I posted this in the wrong forum. It was ment to be in the Alternative Game Libraries forum and not the Artificial intelligence one. Anyways thanks for the help but I don't understand what magnitude's value should be.

##### Share on other sites
Magnitude is the magnitude of the vector that you are normalizing.

##### Share on other sites
Okay, I came up with this function and I use it to make a picture move to my mouse but sometimes it uses a higher speed then it should.

float btoa(float x1,float y1,float x2,float y2)
{
float xdist, ydist;
float magnitude;
xdist = x1-x2;
ydist = y1-y2;
magnitude = xdist + ydist * -1;
if( magnitude < 0)
{
return ydist/magnitude* -1;
}
return ydist/magnitude;
}

When i call it i use

xvel = -1 * atob(curx,cury,event.button.x,event.button.y);
yvel = -1 * btoa(curx,cury,event.button.x,event.button.y);

Have I done anything wrong in the function?

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• 9
• 34
• 16
• 11
• 11
• ### Forum Statistics

• Total Topics
634122
• Total Posts
3015646
×