# Rotation

The model with all its objects is rotated during the game play. Objects itself are rotated within the model. This page gives an overview about the rotation used in Mario Kart Wii.

## Rotation Vectors and Matrices

In Mario Kart Wii, all rotations are defined as 3D vectors. 3D rotation vectors are found in the KMP sections AREA, CAME, CNPT, GOBJ, JGPT, KTPT, MSPT and in the MDL0 section bones.

For transformations, the rotation vector are used together with a scaling and a translation vector to create a transformation matrix and its inverse. The advantage of a transformation matrix is the simple and fast arithmetic (multiplication and addition only, but no Sinus or Cosinus operation) to get the transformed vectors. Sinus or Cosinus are only needed once to create the matrix.

## Kind of Rotation

A rotation in Mario Kart Wii is done as right-handed rotation with Euler angles (in degree) and x-y-z convention. The x-y-z convention means that the rotation is logical done in 3 independent steps:

1. Right-handed rotation around the X-axis using the x-coordinate of the 3D vector.
2. Right-handed rotation around the Y-axis using the y-coordinate of the 3D vector.
3. Right-handed rotation around the Z-axis using the z-coordinate of the 3D vector.

If a rotation is combined with scaling and translation, it is executed in the following 3 steps:

1. Scaling is done by doing 3 separated multiplications, one for each axis.
2. 3 Rotations are done in x, y and z order (see above).
3. Translation is done by doing 3 separated additions, one for each axis.

### Coding Example

In this coding example we have a position vector pos, which should be scaled by scale, rotated by rotate and moved by translate:

``` # scale the position vector by multiplying
pos.x *= scale.x
pos.y *= scale.y
pos.z *= scale.z

# rotate the position vector by special and complex rotation functions
pos = X_ROTATE(pos,rotate.x)
pos = Y_ROTATE(pos,rotate.y)
pos = Z_ROTATE(pos,rotate.z)

# translate the position vector by adding
pos.x += translate.x
pos.y += translate.y
pos.z += translate.z
```

### Roll, Pitch and Yaw

The roll–pitch–yaw model used in flight dynamics has a different mathematical model. In the Cartesian coordinate system used by Mario Kart Wii, the vertical axis is the Y-axis, but in the roll–pitch–yaw model, it is the Z-axis. Adapted into the Mario Kart coordinate system, the roll–pitch–yaw model uses the x-z-y convention instead of the x-y-z convention. This difference is important, because the order of rotation calculations is changed.

In Mario Kart Wii, all rotation values are stored as single float (32 bit) in degrees (360° for a full circle). Programming languages normally use radians (2π for a full circle).

Before calling the library functions sin(), cos() or tan(), the degree values must be transformed into radians. The radian results of the library functions asin(), acos(), atan() and atan2() must be converted into degrees:

```radians = degrees * (π/180.0)