0 Favourites

Moving variables along an angle.

  • 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

    <img src="http://www.mathsisfun.com/images/adjacent-opposite-hypotenuse.gif" border="0">

    • (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)

  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • Thanks, think I'll bookmark this thread so I can refer back to it next time this crops up :)

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)