math2dcoordinatestrigonometry

How to move a point (x,y) to a new position (p,q) at a specified distance and angle in 2D space?


I am working on a 2D game where I need to move a point-sized object from its current position (x, y) to a new position (p, q). The new position should be exactly d units away from (x, y) in a specified direction given by an angle a (in degrees or radians).

In other words, I want to calculate the new coordinates (p, q) such that the distance between (x, y) and (p, q) is d units, and the direction of movement is a degrees/radians.

How can I calculate the new coordinates (p, q) programmatically?

Here's a brief summary of everything:

I'm familiar with trigonometry but I'm not sure how to apply it in this context. Any help or code examples would be greatly appreciated!


Solution

  • Assuming a is measured from the positive x-axis:

    p = x + d * cos(a)

    q = y + d * sin(a)