Rotation

From Custom Mario Kart
Jump to: navigation, search

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 vector. 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

3 axes of the coordinate system

A rotation in Mario Kart Wii is done as right-handed rotation with Euler angles (in degree) and x-y-z convention[1]. 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 multiplication, 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[2] has a different mathematical model. In the Cartesian coordinate system used by Mario Kart Wii, the vertical axis is the y-axis, but in roll–pitch–yaw model it is the z-axis. Adapated into 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.

Degrees and Radians

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)
degrees = radians * (180.0/π)

Most desk calculators allow to switch between degree and radian calculations. You can test the active mode by entering 'acos(0)'. It either returns 1.5708 (π/2, radian mode) or 90° (degree mode).

Links

Wiiki
References
  1. Wikipedia: Matrix Rotation
  2. Wikipedia: Flight dynamics