Here's the problem: A player starts the game in the 0 coordinate (x=0,y=0). When the user clicks on the screen, it returns the coordinates of the destination. Now the player has to move from its current position A(0.0) to B(x,y). Take into account that B is in the first Quadrant so x>0 and y>0.
How will the player move? I know that I will have to use this: atan2(positionY-destinationY, positionX-destinationX)
to get the angle.
Now what? How will the player move with a fixed velocity (which by the way will be a variable, so a player may move faster while using some "items")?
Whose answer is better? What is more accurate? Trigonometry or Linear Algebra?
Answer
Basic trigonometry example
cosine for x axis
sine for y axis
speed is whatever you would like the movement each time to increment by... Obviously the higher the number the more pixels it will move at any given time so the quicker it will be.
dx = (double) (Math.cos(angle) * speed);
dy = (double) (Math.sin(angle) * speed);
A.x += dx;
A.y += dy;
Linear algebra example
Check Sidar's answer :).. Explains it way better than I could.
This is in java... You can easily convert it to C++ though.
No comments:
Post a Comment