• Advertisement
Sign in to follow this  

a to b movement

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement