Advertisement

a to b movement

Started by January 25, 2009 02:36 PM
3 comments, last by freddan007 15 years, 10 months ago
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.
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.
Advertisement
Quote: Original post by JonConley
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.
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.
Magnitude is the magnitude of the vector that you are normalizing.
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?

This topic is closed to new replies.

Advertisement