# Angles

## Angles

While the direction of a vector is useful in many ways, we can’t tell much from the directions of two vectors, not like we could from their lengths. Nor can we simply linearly interpolate between two directions. For this we need a conversion to something we are more accustomed too, namely angles. Instead of degrees, we are going to use radians. While degrees are handy for display or editing values, converting from and to degrees would waste a lot of unnecessary CPU cycles. Degrees go from 0 to 360, radians from 0 to $2\pi$.

function deg2rad(d)
return d*math.pi/180
end

return r * 180 / math.pi
end


The number $\pi$ used in trigonometric functions is not an arbitrarily chosen value. When we work with normalized vectors, vectors with a length of 1, their destination lies on a circle with a radius of 1. If you measure the circumference of that circle, you arrive at exactly $2\pi$ or 6.28318530718. You might remember that if you need the circumference of a circle with radius r, that you need to calculate $2\pi r$. The $r$ in this case is nothing but a scale factor by which you scale the unit circle which has a radius of 1 and a circumference of $2\pi$.

Some game engines use angles from 0 to 360 degrees or from 0 to 1 percent instead. Some even use a clockwise direction for angles rather than the counterclockwise direction used in mathematics to compensate for the inverted y-axis (many games use a positive y-axis going down instead of up). However be aware that this may require changes in mathematical calculations everywhere. The order of vectors in cross products may need to be switched, derivatives may need additional multipliers and angles might need to be adapted.

### Angle to vector

Lets look at some vectors and the angles they represent.

vector angle
$\langle 1, 0\rangle$ 0 or $\pi$
$\langle 0, 1\rangle$ $\frac{\pi}{2}$
$\langle -1, 0\rangle$ $\pi$
$\langle 0, -1\rangle$ $\frac{3\pi}{2}$

Unfortunately, there is no easy way to go from an angle to a direction and back. We need trigonometric functions to do this, which are rather slow compared to normal operations. To go from an angle to a direction vector, we use cosine and sine. These give the x and y of the normalized vector representing the direction.

function vectorFromAngle(a)
return math.cos(a), math.sin(a)
end


### Vector to angle

To get the angle represented by a given normalized vector, we might be tempted to use the reverse calculation, namely arc cosine or arc sine. However if we use $acos(x)$, we get an angle between $0$ and $\pi$ radians. This is because without using y, we don’t know whether the vector is pointing up or down, it only tells us left or right. Similarly, $asin(y)$ gives us an angle between $-\frac{\pi}{2}$ and $\frac{\pi}{2}$ radians as it can’t tell whether the vector is pointing right or left. Lucky for us, we don’t have to do the necessary tests ourselves, as most mathematics modules have an atan2 function taking both x and y to return an angle between $0$ and $\pi$ radians. The tangent of an angle is the slope of the vector, or how many y units for every x unit. If we express this in angles instead of coordinates, we get

Using atan on both sides we get

If we would use the tan function, we would get the same problem as before, as with only the slope, the result of $\frac{y}{x}$, it will return an angle between $-\frac{\pi}{2}$ and $\frac{\pi}{2}$ radians. The previously mentioned atan2 taking both x and y as input will give us the correct angle.

function angleFromVector(x, y)
return math.atan2(y, x)
end


### Angle between 2 vectors

We could convert both vectors separately to angles and subtract these, but we would be doing more work than is needed. If only we could obtain the sine and cosine of the angle, we could, like before, use the arc tangent to get the angle. This is where the dot and cross products come in handy, as they have the following properties

and

The cross product divided by the product of the length of the vectors gives us the sine of the angle, while the dot product does the same for the cosine. Since we actually need the slope, thus sine divided by cosine, we can discard the divisors and obtain

This means we can use the cross and dot products of the two vectors together with atan2 to obtain $\theta$

function angleBetweenVectors(x1, y1, x2, y2)
return math.atan2(cross(x1, y1, x2, y2), dot(x1, y1, x2, y2))
end


Note that we can now explain why the length formula is the square root of the dot product. The angle between a vector and itself is 0 so we get

The cosine of 0 radians is 1

Multiplying both sides with $\left\vert\vec{a}\right\vert^2$ gives

Taking the square root of both sides gives us that the length of a vector is the square root of the dot product of the vector with itself.

### Determining whether 2 vectors point in the same direction

Since we can calculate the angle between two vectors now, you might think of looking at the angle. If the angle between the vectors is between $-\frac{\pi}{2}$ and $\frac{\pi}{2}$ radians, the vectors are pointing in the same direction. However, we can simplify that. If we calculate just the cosine of the angle we can look whether that cosine is greater than 0. For the cosine we just need the dot product of the vectors and their lengths.

But since we are only interested in the sign, we can disregard the lengths. So if $\vec{a}.\vec{b} > 0$ we know that the vectors point in the same direction. An application of this would be for example to draw or attack enemy units only if they are in front of us.

function drawEnemy(x, y)
local toEnemyX, toEnemyY = sub(x, y, ox, oy)
local cosine = dot(toEnemyY, playerDir)
if cosine > 0 then
// actual drawing procedure
else
// nothing to do, enemy is behind our back
end
end


## Rotation

### Rotation around the origin

We will start by trying to rotate the normalized vector $\vec{b}$ by $\alpha$ radians. If we have the normalized vector $\hat{b}$ pointing in the direction of $\beta$ radians, it can be written as $\hat{b}=(cos(\beta), sin(\beta))$. Similarly, we can write the angle $\alpha$ as the normalized vector $\hat{a} =(cos(\alpha), sin(\alpha))$ pointing in the direction of $\alpha$. As we saw before, for normalized vectors, the cosine of the angle between two vectors was equal to the dot product of the two vectors, while the sine of the angle between two vectors was equal to the cross product. The angle between the vectors $\hat{a}$ and $\hat{b}$ is equal to $\alpha-\beta$, so we can write

Since multiplication is commutative we can write this as

What we are looking for, in order to rotate, are the cosine and sine of $\alpha+\beta$. So we need to replace $\beta$ with $-\beta$

The cosine can discard the minus, as $cos(-\beta)=cos(\beta)$, the sine has to switch signs, as $sin(-\beta)=-sin(\beta)$. So we arrive at

These formula’s might look familiar, as they should be featured in every mathematics book on trigonometry. They can be easily derived from the dot and cross products as shown above.

We rotated the normalized vector pointing towards $\beta$ by $\alpha$ degrees. We would like to do that for any vector, not just normalized vectors, so that we can apply it to points. This step is easy, as we can multiply both sides by the length of the vector

Since $(l*cos(\beta), l*sin(\beta))$ is our point (x, y) to be rotated, we can write

Similarly $(l*cos(\alpha+\beta), l*sin(\alpha+\beta))$ is the rotated point (x’, y’). Note that as a vector, the length has been preserved by the rotation.

This formula might also look familiar if you’ve played with rotation matrices before.

### Rotation around a given center

Like scaling from a given center, the center of rotation can be selected by translating the desired center to the origin, doing the rotation and translating back.

Of course unless this rotation anchor is animated, it is better to offset the geometry once in order to save on calculations. For example instead of drawing a sprite as {0, 0, w, h}, it is better drawn as {-w0.5, -h0.5, w0.5, h0.5} if the anchor is always in the center of the sprite. The same goes for scaling. If a sprite has a fixed scale, incorporate it into the geometry, like {0, 0, w2, h2}.

function rotate(x, y, angle, cx, cy)
cx = cx or 0
cy = cy or 0
local c = math.cos(angle)
local s = math.sin(angle)
x = x - cx
y = y - cy
return x*c - y*s + cx, x*s + y*c + cy
end


### Rotation using a normalized vector

Note that while we seemingly use an angle to rotate, this is not really the case. Rotation is actually done using a vector. If we look at the formula for rotating around $(0, 0)$

We can replace the sine and cosine of $\alpha$ with the normalized vector $\hat{v}$

giving

This insight has several implications. If we want to rotate for example a sprite given a direction, we don’t need to calculate an angle from the direction, to convert it again to a vector to do the rotation. We can just use the normalized vector directly. If we apply the rotation of a static object every frame, we might want to store this vector rather than the angle, since we can use it directly without the need of trigonometric functions.

function followTarget(x, y, tx, ty, points)
-- Build direction vector
local vx, vy = tx-x, ty-y
-- Get angle
local angle = math.atan2(vy, vx)
-- Get vector
local c = math.cos(angle)
local s = math.sin(angle)
-- Rotate points
for _, point in ipairs(points) do
point.x, point.y = point.x*c - point.y*s, point.x*s + point.y*c
end
end


compare this with

function followTarget(x, y, tx, ty, points)
-- Build direction vector
local vx, vy = normalize(tx-x, ty-y)
-- Rotate points
for _, point in ipairs(points) do
point.x, point.y = point.x*vx - point.y*vy, point.x*vy + point.y*vx
end
end