56
$\begingroup$

Excuse my ignorance and use of incorrect terms, but...

I have x and y coordinates, and the angle that the entity is facing on a 2D plane. I want to find the correct point, say 5 units in front of the point I have.

Examples:

If my entity is at 0, 0 and is facing east (0 degrees), my point would be 5, 0.

If my entity is at 0, 0 and is facing north (90 degrees), my point would be 0, 5.

If my entity is at 0, 0 and is facing north-east (45 degrees), my point would be ???.

I can't even figure it out in my head, let alone figure out the formula I need. I assume I need trigonometry, but I'm old and haven't used it since 1997.

$\endgroup$
5
  • 3
    $\begingroup$ $x=5\cos\theta$, $y=5\sin\theta$. $\theta$ is the angle between the line of sight from the entity to the point and the positive $x$ axis. $\endgroup$ Commented May 11, 2012 at 16:17
  • $\begingroup$ @DavidMitra Again, my ignorance, what am I doing wrong? I'm my browser's console to test it... 0 works 5*Math.cos(0)= 5, 5*.Math.sin(0)=0, but 90 does not 5*Math.cos(90) = -2.2403680806458506, 5*Math.sin(90) = 4.4699833180027895... I tried converting degrees to radians but that didn't help either. $\endgroup$
    – Langdon
    Commented May 11, 2012 at 16:48
  • $\begingroup$ When you converted to radians, what did you get? You should have obtained $90^\circ$ is $\pi/2$ radians. $\endgroup$ Commented May 11, 2012 at 17:04
  • $\begingroup$ I did, I think I'm now having issues with not understand how JavaScript handles floats... 90 * Math.PI / 180 does give me 1.5707963267948966, as does Math.PI / 2, but when I say Math.cos(Math.PI / 2), I get 6.123233995736766e-17, which may very well be close to 1, but I don't know how to cast that into a usable integer now. =[ $\endgroup$
    – Langdon
    Commented May 11, 2012 at 17:07
  • $\begingroup$ Yes, the "e-17" and the end means "times $10^{-17}$, which in effect shifts the decimal point to the left 17 times, introducing zeroes (so it's $.0000000000000000612...$). $\cos(\pi/2)$ is exactly $0$. $\endgroup$ Commented May 11, 2012 at 17:10

2 Answers 2

48
$\begingroup$

In general, if $\theta$ is the angle between the line of sight from the entity to the point and the positive $x$ axis, then $$ x=5\cos\theta,\quad\text{and}\quad y=5\sin\theta. $$ Here $\cos$ is the cosine function and $\sin$ is the sine function.

When calculating values of these, it is important to realize that the angle can be measured in various ways, the most common being degrees and radians. $360$ degrees is $2\pi$ radians. In general to convert $x$ degrees to radians, multiply $x$ by $\pi/180$.

You can use either measurement system for the angle, but when calculating $\sin$ and $\cos$ using a device, make sure you measure the angle as needed by that device.

In your example, with an angle of $45$ degrees, if you find $\sin(45^\circ)$ and $\cos(45^\circ)$ from a calculator, make sure the calculator is set to use degrees as the measure. Using Google's calculator (which by default uses radians), we must input $\sin(45\ \text{ degrees})$ and $\cos(45\ \text{ degrees})$. This returns

$$\sin(45^\circ)\approx.707\quad\text{and}\quad\cos(45^\circ)\approx.707.$$ Your point would then have $x$ coordinate

$\ \ \ \ \ x\approx5\cdot (0.707)=3.535$

and $y$-coordinate

$\ \ \ \ \ y\approx5\cdot( 0.707)=3.535$.

In radians, $45$ degrees is $45\cdot{\pi\over 180}={\pi\over 4}$ radians; and you could compute $\cos(\pi/4)$ and $\sin(\pi/4)$ using a device where angles are measured in radians. This of course will give approximately $.707$ in both cases as before.

$\endgroup$
8
  • $\begingroup$ Why would I get sin(45) = 0.8509035245341184? I am executing this in actionscript 3. That is a significant difference and nowhere near .707. $\endgroup$
    – IAbstract
    Commented Nov 2, 2016 at 13:58
  • $\begingroup$ @IAbstract You need to specify degrees, I would guess. $\endgroup$ Commented Nov 2, 2016 at 14:18
  • $\begingroup$ That is in degrees. You mean radians? Yes, when I converted to radians I received the same result as you: ~ .707 $\endgroup$
    – IAbstract
    Commented Nov 2, 2016 at 14:35
  • $\begingroup$ @IAbstract I mean your program interprets "sin(45)" as sin(45 radians). You want to type sin(45 degrees) or sin(pi/4). $\endgroup$ Commented Nov 2, 2016 at 14:39
  • 1
    $\begingroup$ This defines a vector in direction and magnitude, but to actually get the new position, you need to add the origin position. $\endgroup$
    – JonoRR
    Commented Jul 30, 2021 at 4:39
27
$\begingroup$

If you are at point (x,y) and you want to move d unit in alpha angle (in radian), then formula for destination point will be:

xx = x + (d * cos(alpha))
yy = y + (d * sin(alpha))

Note: If angle is given in degree:

angle in radian = angle in degree * Pi / 180
$\endgroup$
2
  • $\begingroup$ can you tell me here d will be in km /m ? $\endgroup$
    – Iva
    Commented Aug 12, 2022 at 7:05
  • 2
    $\begingroup$ @Iva the calculation is unit agnostic, d and x/y are in whatever unit you are using $\endgroup$
    – Nathanael
    Commented Sep 15, 2022 at 9:19

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.