Cardinal Rotation

When working with grid-based video games, programmers will inevitably discover the need to represent discrete rotations.

In this series, we derive and implement a theoretical “rotation object”. The goal here is to create an API for performing common grid-based operations.

Nomenclature

Let the sign domain $ \mathbb{S} $ describe the 3 integer signs. $$ \boxed{\mathbb{S} = {-1, 0, 1}} \tag{Definition 1.1} $$

Theory

2D directions are most commonly described with cardinal directions:

  • North
  • East
  • South
  • West

If we require more precision, we can use intercardinal directions to describe the “in between” directions:

  • Northeast
  • Southeast
  • Southwest
  • Northwest

Informally, each cardinal/intercardinal direction can be thought of as a vector which “points” in a certain direction in the XY plane. For example, “southeast” can be expressed as $ \langle 1, -1 \rangle $. In other words, it has a component in the “east” direction (positive X) and a component in the “south” direction (negative Y).

image

2D Cardinal Vector

Let’s formally define this type of vector: for some dimension $ N $, let the vector $ \vec{d} $ represent an $ N $-dimensional cardinal vector.

$$ \boxed{\vec{d} \in \mathbb{S} ^ N} \tag{Definition 1.2} $$ where $$ N \in \mathbb{Z} ^ + $$

All cardinal vectors for $ N = 2 $ are listed in Table 1.3. Notice that most 2D cardinal vectors correspond with a familiar cardinal/intercardinal direction.

VectorCardinal direction
$ \langle -1, -1 \rangle $Southwest
$ \langle -1, 0 \rangle $West
$ \langle -1, 1 \rangle $Northwest
$ \langle 0, -1 \rangle $South
$ \langle 0, 0 \rangle $
$ \langle 0, 1 \rangle $North
$ \langle 1, -1 \rangle $Southeast
$ \langle 1, 0 \rangle $East
$ \langle 1, 1 \rangle $Northeast

Table 1.3 - Cardinal Vectors in 2D


All cardinal vectors for $ N = 3 $ are listed in Table 1.4. Notice that in 3D, it is less appropriate to rely on cardinal/intercardinal directions. With cardinal vector notation, we eliminate the need to create distinct labels for the 27 cardinal directions. Instead, we express each direction numerically.

VectorCardinal direction
$ \langle -1, -1, -1 \rangle $
$ \langle -1, -1, 0 \rangle $
$ \langle -1, -1, 1 \rangle $
$ \langle -1, 0, -1 \rangle $
$ \langle -1, 0, 0 \rangle $West
$ \langle -1, 0, 1 \rangle $
$ \langle -1, 1, -1 \rangle $
$ \langle -1, 1, 0 \rangle $
$ \langle -1, 1, 1 \rangle $
$ \langle 0, -1, -1 \rangle $
$ \langle 0, -1, 0 \rangle $South?
$ \langle 0, -1, 1 \rangle $
$ \langle 0, 0, -1 \rangle $South?
$ \langle 0, 0, 0 \rangle $
$ \langle 0, 0, 1 \rangle $North?
$ \langle 0, 1, -1 \rangle $
$ \langle 0, 1, 0 \rangle $North?
$ \langle 0, 1, 1 \rangle $
$ \langle 1, -1, -1 \rangle $
$ \langle 1, -1, 0 \rangle $
$ \langle 1, -1, 1 \rangle $
$ \langle 1, 0, -1 \rangle $
$ \langle 1, 0, 0 \rangle $East
$ \langle 1, 0, 1 \rangle $
$ \langle 1, 1, -1 \rangle $
$ \langle 1, 1, 0 \rangle $
$ \langle 1, 1, 1 \rangle $

Table 1.4 - Cardinal Vectors in 3D


For example, consider the direction left-up-back. Using cardinal vector notation, this direction can be written as $ \langle -1, 1, -1 \rangle $.

image

2D Cardinal Vector


Conclusion

With the concept of a cardinal vector, we are ready to implement our rotation object. The implementation will be revealed in Part II of this series.


604 Words

2021-02-07 09:49 +0000