Introduction
libGDX has several linear algebra classes for dealing with common tasks in physics and applied math. These include:
A full explanation of these concepts is beyond the current scope, but the above links may provide a starting point for further understanding. What follows is an overview of their use and implementation in Libgdx.
Vectors
two (Vector2) (code) and 3-dimensional (Vector3) (code) spaces. Each contains common operations for working with vector quantities such as addition, subtraction, normalization, and the cross and dot products. There are also methods for linear and spherical linear interpolation between two vectors.
Method Chaining
(x1, y1, z1) to another point (x2, y2, z2):
Vector3 vec = new Vector3( x2, y2, z2 ).sub( x1, y1, z1 ).nor();
A new Vector3 is instantiated with the second point coordinates, the first point is subtracted from this, and the result is normalized. This is of course equivalent to:
Vector3 vec = new Vector3( x2, y2, z2 ); // new vector at (x2, y2, z2)vec.sub( x1, y1, z1 ); // subtract point (x1, y1, z1)vec.nor(); // normalize result
Matrices
3x3 (Matrix3) (code) and 4x4 (Matrix4) (code) varieties with convenient methods for moving values between the two. libGDX includes many common operations for working with matrices such as building translation and scaling transformations, building rotations from Euler angles, axis-angle pairs or quaternions, building projections, and performing multiplication with other matrices and vectors. Camera(code) class which are used to control how geometry is rendered to the screen. Cameras, which come in OrthographicCamera (code) and PerspectiveCamera (code) varieties, provide a convenient and intuitive way to control the view in a rendered scene through a position and viewing direction, yet underneath are a simple group of 4x4 matrices which are used to tell OpenGL how to process geometry for render.
Method Chaining
(x, y, z) and rotation described by an axis-angle pair:
Matrix4 mat = new Matrix4().setToRotation( axis, angle ).trn( x, y, z );
This is of course equivalent to:
Matrix4 mat = new Matrix4(); // new identity matrixmat.setToRotation( axis, angle ); // set rotation from axis-angle pairmat.trn( x, y, z ); // translate by x, y, z
Native Methods
The matrix classes have a number of their operations available in static methods backed by fast native code. While member syntax is often easier to read and more convenient to write, these static methods should be used in areas where performance is a concern. The following example uses one of these methods to perform a multiplication between two 4x4 matrices:
Matrix4 matA;Matrix4 matB;Matrix4.mul( matA.val, matB.val ); // the result is stored in matA
.val to access the underlying float array which backs each matrix. The native methods work directly on these arrays. The above is functionally equivalent to the member syntax:
matA.mul( matB );
Quaternions
gimbal lock making them preferable often to other methods of rotational representation which may variously fall short in these areas. ** Libgdx also provide methods for converting between quaternions and the various other rotational representations such as Euler angles, rotation matrices, and axis-angle pairs.
