0 Favourites

# Moving variables along an angle.

• 3 posts
• I know how to move a sprite along an angle, but I want to move some variables (X & Y) along an angle and distance that I already know (the angle between the player and the mouse cursor). So basically I want to know the math function to update the X and Y position of my sprite with an angle/distance without actually moving the sprite.

I understand that I could move the sprite, update the values, then move the sprite back, or similar tricks. But I want to know the actual mathematical solution (trig functions I think?).

• ``````x = x + distance * cos(angle)
y = y + distance * sin(angle)
``````

---

http://www.mathsisfun.com/sine-cosine-tangent.html

• (x,y) the starting position, is the left-most point of the triangle
• you know the hypotenuse - that's the distance you want to travel
• you know the angle ?
• you want to find the length of the adjacent side which is the how far you need to travel in the x direction
• and the length of the opposite side which is how far to travel in the y direction
``````Sine Function: sin(?) = Opposite / Hypotenuse
Cosine Function: cos(?) = Adjacent / Hypotenuse
``````

Sine Function (to find Opposite side):

=> sin(?) = Opposite / Hypotenuse

=> Opposite = Hypotenuse * sin(?)

=> y amount = distance * sin(angle)

Cosine Function (to find Adjacent side):

=> cos(?) = Adjacent / Hypotenuse

=> Adjacent = Hypotenuse * cos(?)

=> x amount = distance * cos(angle)