Math

Overview

There are a lot of math helpers available in the engine that we can use to simplify computation of game logic. To start, see the table below for a few common ones (we’ll go over more complex types such as vectors and matrices further into the chapter).

Coordinate System

CRYENGINE uses a right-handed coordinate system where positive X-axis points to the right, positive Y-axis away from the viewer and positive Z-axis points up. In the context of characters this means that positive X is right, positive Y is forward, and positive Z is up:

Fun fact: The engine's coordinate system originates from the terrain being a 2D plane with dimensions X and Y being looked at from an aerial perspective. The height of/above the terrain then becomes the Z-axis coordinate.

Constants

The engine exposes several "globals" to denote common constants:

NameDescription
gf_PIThe 32-bit floating point value of Pi (π)
g_PIThe 64-bit floating point value of Pi (π)
gf_PI2The 32-bit floating point value of Tau (τ / )
g_PI2The 64-bit floating point value of Tau (τ / 2π)
gf_ln2The 32-bit floating point value of ln(2)
sqrt2The 64-bit value of sqrt(2)
sqrt3The 64-bit value of sqrt(3)

Radians & Degrees

Angles are typically represented as radians in the engine. To convert back and forth between radians and degrees, use the DEG2RAD and RAD2DEG helpers.

Additionally, the CryTransform::CAngle class can be used to simplify conversion.

Common Math Helpers

For a reference of common math helper functions, see the crymath namespace.

Vectors

2D vectors are represented by the Vec2_tpl template, and are most commonly used under the name of Vec2 - containing two 32-bit floats. Alternatively, Vec2i (32-bit integer) or Vec2d (64-bit double) can be used.

Similarly, 3D vectors are represented by the Vec3_tpl template, usually used as Vec3 containing three 32-bit floats. Alternatively, Vec3i (32-bit integer) or Vec3d (64-bit double) can be used.

Quaternions

Rotations in the engine are commonly represented by quaternions, implemented by the Quat_tpl template in code. Most commonly, we will be referring to Quaternions using the Quat type - containing 32-bit values.

Matrices

Several matrix structures are also provided to handle the rotations and transformations of elements in the world. The two most common when developing games are Matrix33_tpl (Matrix33 in most cases) for rotations, and Matrix34_tpl (Matrix34 in most cases) for representing scale, translation (position) and rotation.

The types are interchangeable for the most part, and can also be converted to Quaternions if needed. The vast majority of functions present in the Quat structure are also available for matrices. To create a transformation matrix:

Matrix34 myTransform = Matrix34::Create(Vec3(1, 1, 1), IDENTITY, ZERO);

Where the first parameter is the scale, the second being the rotation as a Quaternion, and the last being the translation.

Angle Vectors (Euler Angles)

Similarly to Vec3_tpl, Ang3_tpl (see: Ang3) represents a set of euler angles and provides constructors that can convert back and forth between Quaternions for simplicity’s sake. Keep in mind that these are typically managed as radians internally, and can easily suffer from gimbal lock.

The Ang3_tpl structure is also sometimes used for other contexts, for example the CCamera::CreateAnglesYPR static function allows for converting a rotation stored in a matrix to yaw, pitch and roll respectively. This can then be changed back with CCamera::CreateOrientationYPR.

Conclusion

This concludes this article on the CRYENGINE math library. You may be interested in: