a to b movement
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.
Quote: Original post by JonConleyI 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.
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.
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?
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
Popular Topics
Advertisement