David
David

Reputation: 3814

Calculate trajectory given a coordinate and angle

Given the following:
- starting point (coordinate)
- angle (degree)
- velocity

.. I'd like to calculate the given trajectory.
For example for the image below w/ velocity 1: (10,10) (9,9) (8,8) (7,7) ..

It should be able to move in all directions.
How can I calculate it?

example

Upvotes: 0

Views: 5067

Answers (1)

duffymo
duffymo

Reputation: 309008

If you have an angle and a speed (scalar), the components in the x- and y-directions are easy:

vx = (speed)*cos(angle)

vy = (speed)*sin(angle)

Angle needs to be in radians for most languages, not degrees. Make sure you convert it.

So if you have a point (ux, uy) at time t1, then the position at time t2 is:

ux(t2) = ux(t1) + vx*(t2-t1)

uy(t2) = uy(t1) + vy*(t2-t1)

Let's see what it looks like in Java:

/**
 * Method for updating a position giving speed, angle, and time step
 * @param original coordinates in x, y plane
 * @param speed magnitude; units have to be consistent with coordinates and time
 * @param angle in radians
 * @param dtime increment in time 
 * @return new coordinate in x, y plane
 */
public Point2D.Double updatePosition(Point2D.Double original, double speed, double angle, double dtime) {
    Point2D.Double updated = new Point2D.Double();    
    updated.x = original.x + speed*Math.cos(angle)*dtime;
    updated.y = original.y + speed*Math.sin(angle)*dtime;    
    return updated;
}

Upvotes: 4

Related Questions