Math Overview

XNA Game Studio 3.0

The XNA Framework Math Libraries can be found in the Microsoft.Xna.Framework namespace, alongside a number of additional types that deal with the XNA Framework Application model.

Coordinate system

The XNA Framework uses a right-handed coordinate system, with the positive z-axis pointing toward the observer when the positive x-axis is pointing to the right, and the positive y-axis is pointing up.

Mathematical Constants and Scalar Manipulation

The XNA Framework provides the MathHelper Members class for manipulating scalar values and retrieving some common mathematical constants. This includes methods such as the ToDegrees and ToRadians utility methods for converting between degrees and radians.

Basic Geometric Types

The XNA Framework Math library has multiple basic geometric types that can be used to manipulate objects in 2D or 3D space. The primitive objects in this library represent the data required to represent a geometric object or an operation on that object. Each geometric type has a number of mathematical operations that are supported for the type.

Vectors

The XNA Framework provides the Vector2, Vector3 and Vector4 classes for representing and manipulating vectors. A vector is typically used to represent a direction and magnitude. However, in the XNA framework it might also be used to store a coordinate or some other data type with the same storage requirements.

Each vector class has methods for performing standard vector operations such as:

Matrices

The XNA Framework provides the Matrix class for transformation of geometry. The Matrix class uses a row major order to address matrices, which means that the row is specified before the column when describing an element of a two-dimensional matrix. The Matrix class provides methods for performing standard matrix operations such as calculating the determinate or inverse of a matrix, in addition to helper methods for creating scale, translation, and rotation matrices.

Quaternions

The XNA Framework provides the Quaternion structure to represent and calculate the efficient rotation about a vector around a specified angle.

Curves

The Curve class represents a hermite curve that allows you to interpolate varying positions at different times without having to explicitly define each position. The curve is defined by a collection of CurveKey points representing each varying position at different times.

This class can be used not only for spatial motion, but also to represent any response that changes over time.

Bounding Volumes

The XNA Framework provides the BoundingBox, BoundingFrustum, BoundingSphere, Plane, and Ray classes for representing simplified versions of geometry for the purpose of efficient collision and hit-testing. These classes have methods for checking for intersection and containment with each other.

Precision and Performance

The XNA Framework Math libraries are single-precision. This means that the primitives and operations contained in this library use 32-bits to represent floating-point numbers in order to achieve a balance between precision and efficiency when performing large numbers of calculations.

The 32-bit floating-point numbers can represent a range of values from negative 3.402823e38 to positive 3.402823e38. The 32-bits of a floating-point number are used to store the sign, mantissa, and exponent of the number. The result is seven digits of floating-point precision. Some numbers, such as π, 1/3, or the square root of two, can only be approximated with a limited amount of memory. Because the number of bits used to represent a decimal number limit the range of numbers it can represent, floating-point numbers only approximate decimal numbers that require more than seven digits of precision. Consequently, rounding errors can occur under different conditions through the use of any binary-representation approximating a floating point number.

You can find more information about single-precision numbers in the documentation for the Single data type.

Best Practice

Depending on the hardware platform your game is targeting, you may have multiple processors available to perform floating-point calculations. For example, on the Xbox 360 platform and on many Windows-based computers, you have a CPU, on which your game code is running, but you also have GPU, on which your shader code is running. Therefore, you can also perform floating point calculations on the GPU using shaders and HLSL intrinsic functions. GPUs are very fast at performing large numbers of floating-point operations. This can be very useful, for example, if you need to perform a large number of floating point operations to animate a particle system.

However, you need to remember that the Zune platform does not have a GPU and does not support shaders. For Zune, you can use a conditional compilation directive #if ZUNE to write downlevel-code that behaves gracefully on this platform while still taking advantage of the performance gains on other types of hardware.