34
$\begingroup$

I have been looking everywhere trying to find out how to convert an angle in radians (expressed as -Pi to Pi) to a heading vector.

The only [x,y] answer I have found is, [cos(angle), sin(angle)] , however, this doesn't work! Or am I missing something?

I just want a vector pointing at a direction of a specified angle, and for it to have a magnitude of 1, such is called a "heading vector" I believe. At least it is in the various game code I look at.

CLARIFICATION:

A heading vector is a vector with a magnitude of 1 with the start at 0, and the end (the arrowhead) at some value within a unit circle. A heading vector is a way of showing direction as a vector. I want to take an angle and express it as a vector, however, people seem to just be telling me how to do unit conversions.

I appreciate you trying to be helpful, however, hopefully these clarifications will guide others to giving more fitting responses.

Thanks.

$\endgroup$
10
  • $\begingroup$ An angle in standard position has one side on the positive x-axis, and is measured counter-clockwise. The angle for heading/bearing is measured from the positive y-axis, clockwise. $\endgroup$ Aug 9, 2012 at 23:56
  • $\begingroup$ This does not answer my question. $\endgroup$
    – José
    Aug 10, 2012 at 2:04
  • 2
    $\begingroup$ "...this doesn't work!" - you haven't said why... $\endgroup$ Aug 10, 2012 at 2:10
  • $\begingroup$ Let's stop using the word "heading". See the picture in the top right for visuals of "initial" and "terminal" sides of an angle. If you want a point that is one unit away from the origin that makes an angle $\theta$ with the positive x-axis when connected to the origin, that point is (cos $\theta$, sin $\theta$). If you want a vector, use brackets instead of parenthesis. $\endgroup$ Aug 10, 2012 at 2:18
  • 1
    $\begingroup$ When you finally show the problem, it is clear it is a numerical problem. Unless you use a symbolic package, $\sin \pi$ does not exactly equal $0$ in part because $\pi$ is not exact. $\endgroup$ Feb 5, 2013 at 22:43

3 Answers 3

57
$\begingroup$

Your information is correct.

If you have an angle (A), in radians, in the range -Pi to Pi, then convert it to a vector (V) with:

V.x = cos(A)
V.y = sin(A)

The inverse is A = atan2(V.y, V.x)

If it doesn't work in your games code you should be looking for a typing error, or other silly little bug. Its not the maths.

$\endgroup$
4
  • 1
    $\begingroup$ ${\tt atan2}$ is from the horror shop of mathematical misconceptions. It should not be used on a site devoted to mathematics $\ldots$ $\endgroup$ Jul 20, 2017 at 8:15
  • 9
    $\begingroup$ @ChristianBlatter can you share reasons for not using atan2? I'd like to hear more about this as I don't see anything special here. Everything looks fine from atan2 on wikipedia, it's included in many modern programming languages and commonly used in science and engineering. Wiki's description seems to indicate that it solves a few problems compared to the single parameter version. $\endgroup$
    – GabLeRoux
    Jul 20, 2017 at 15:06
  • 2
    $\begingroup$ @GabLeRoux: The established function is ${\rm arg}:\>\dot{\mathbb R}^2\to{\mathbb R}/(2\pi)$, or its principal value ${\rm Arg}$, which is defined in the plane minus the negative $x$-axis, and takes values in $(-\pi,\pi)$. When $x>0$ then ${\rm Arg}(x,y)=\arctan{y\over x}$, whereby the order of the variables is reversed in comparison to ${\tt atan2}$. In mathematics we don't use fullsize digits in names of objects. $\endgroup$ Jul 20, 2017 at 15:54
  • $\begingroup$ Also see Wikipedia on Argument_(complex_analysis) - Computing_from_the_real_and_imaginary_part ("... the function that calculates the principal value Arg is called the two-argument arctangent function atan2:") $\endgroup$
    – handle
    Jan 11, 2023 at 8:52
4
$\begingroup$

To convert radians to degrees, you multiply by $\frac {180}{\pi}$. The standard positions for angles and headings are different, as The Chaz has commented. To go from a regular angle of $\theta$ to a heading, the heading is $\frac {\pi}2 - \theta$ in radians or $90^\circ -\theta$ in degrees.

$\endgroup$
0
$\begingroup$

If you have an angle between north and vertical axis (which equals to negative the bearing) where North ($0°$) is at $(0,1)$, then your vector would be

$u = sin(\theta)$

$v = cos(\theta)$

for any degree $\theta$.

To think of it visually, just consider what $sin(\theta)$ and $cos(\theta)$ look like as $\theta$ changes, and compare them to how you expect $u$ and $v$ to change.

enter image description here

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .