pyglet.math
Matrix and Vector math.
This module provides Vector and Matrix objects, including Vec2, Vec3,
Vec4, Mat3, and Mat4. Most common matrix and vector operations are
supported. Helper methods are included for rotating, scaling, and
transforming. The Mat4
includes class methods
for creating orthographic and perspective projection matrixes.
Matrices behave just like they do in GLSL: they are specified in column-major order and multiply on the left of vectors, which are treated as columns.
All objects are immutable and hashable.
- class Mat3
A 3x3 Matrix.
Mat3 is an immutable 3x3 Matrix, which includes most common operators.
A Matrix can be created with a list or tuple of 9 values. If no values are provided, an “identity matrix” will be created (1.0 on the main diagonal). Because Mat3 objects are immutable, all operations return a new Mat3 object.
Note
Matrix multiplication is performed using the “@” operator.
- class Mat4
A 4x4 Matrix.
Mat4
is an immutable 4x4 Matrix object with most common operators. This includes class methods for creating orthogonal and perspective projection matrixes, which can be used directly by OpenGL.You can create a Matrix with 16 initial values (floats), or no values at all. If no values are provided, an “identity matrix” will be created (1.0 on the main diagonal).
Mat4
objects are immutable, so all operations will return a new Mat4 object.Note
Matrix multiplication is performed using the “@” operator.
- classmethod from_rotation(angle: float, vector: Vec3) Mat4
Create a rotation matrix from an angle and Vec3.
- classmethod from_translation(vector: Vec3) Mat4
Create a translation matrix from a Vec3.
- Return type:
- classmethod look_at( ) Mat4
Create a viewing matrix that points toward a target.
This method takes three Vec3s, describing the viewer’s position, where they are looking, and the upward axis (typically positive on the Y axis). The resulting Mat4 can be used as the projection matrix.
- classmethod orthogonal_projection( ) Mat4
Create a Mat4 orthographic projection matrix for use with OpenGL.
Given left, right, bottom, top values, and near/far z planes, create a 4x4 Projection Matrix. This is useful for setting
projection
.- Return type:
- classmethod perspective_projection( ) Mat4
Create a Mat4 perspective projection matrix for use with OpenGL.
Given a desired aspect ratio, near/far planes, and fov (field of view), create a 4x4 Projection Matrix. This is useful for setting
projection
.- Return type:
- class Quaternion
Quaternion.
- conjugate() Quaternion
- Return type:
- dot(other: Quaternion) float
- Return type:
- classmethod from_mat3() Quaternion
- Return type:
- classmethod from_mat4() Quaternion
- Return type:
- length() float
Calculate the length of the Quaternion.
The distance between the coordinates and the origin.
- Return type:
- normalize() Quaternion
- Return type:
- class Vec2
A two-dimensional vector represented as an X Y coordinate pair.
Vec2 is an immutable 2D Vector, including most common operators. As an immutable type, all operations return a new object.
Note
The Python
len
operator returns the number of elements in the vector. For the vector length, use the length() method.Note
Python’s
sum()
requires the first item to be aVec2
.After that, you can mix
Vec2
-liketuple
andVec2
instances freely in the iterable.If you do not, you will see a
TypeError
about being unable to add atuple
and aint
.- clamp( ) Vec2
Restrict the value of the X and Y components of the vector to be within the given values.
If a single value is provided, it will be used for both X and Y. If a tuple or Vec2 is provided, the first value will be used for X and the second for Y.
- distance(other: Vec2 | tuple[int, int]) float
Calculate the distance between this vector and another vector.
- dot(other: Vec2 | tuple[float, float]) float
Calculate the dot product of this vector and another 2D vector.
- Return type:
- static from_heading(heading: float, length: float = 1.0) Vec2
Create a new vector from the given heading and length.
- static from_polar(angle: float, length: float = 1.0) Vec2
Create a new vector from the given polar coordinates.
- heading() float
Calculate the heading of the vector in radians.
Shortcut for atan2(y, x) meaning it returns a value between -pi and pi.
Vec2(1, 0)
will have a heading of 0. Counter-clockwise is positive moving towards pi, and clockwise is negative moving towards -pi.- Return type:
- index(*args: Any) int
Return first index of value.
Raises ValueError if the value is not present.
- Return type:
- length_squared() float
Calculate the squared length of the vector.
This is simply shortcut for x ** 2 + y ** 2 and can be used for faster comparisons without the need for a square root.
- Return type:
- lerp( ) Vec2
Create a new Vec2 linearly interpolated between this vector and another Vec2.
The equivalent in GLSL is mix.
- normalize() Vec2
Return a normalized version of the vector.
This simply means the vector will have a length of 1.0. If the vector has a length of 0, the original vector will be returned.
- Return type:
- reflect(vector: Vec2 | tuple[float, float]) Vec2
Create a new Vec2 reflected (ricochet) from the given normalized vector.
- step(edge: Vec2 | tuple[float, float]) Vec2
A step function that returns 0.0 for a component if it is less than the edge, and 1.0 otherwise.
This can be used enable and disable some behavior based on a condition.
Example:
# First component is less than 1.0, second component is greater than 1.0 Vec2(0.5, 1.5).step((1.0, 1.0)) Vec2(1.0, 0.0)
- class Vec3
A three-dimensional vector represented as X Y Z coordinates.
Vec3 is an immutable 3D Vector, including most common operators. As an immutable type, all operations return a new object.
Note
The Python
len
operator returns the number of elements in the vector. For the vector length, use the length() method.- clamp(
- min_val: float | Vec3 | tuple[float, float, float],
- max_val: float | Vec3 | tuple[float, float, float],
Restrict the value of the X, Y and Z components of the vector to be within the given values.
If a single value is provided, it will be used for all components. If a tuple or Vec3 is provided, the first value will be used for X, the second for Y, and the third for Z.
- distance(other: Vec3 | tuple[float, float, float]) float
Calculate the distance between this vector and another 3D vector.
- dot(other: Vec3 | tuple[float, float, float]) float
Calculate the dot product of this vector and another 3D vector.
- classmethod from_pitch_yaw(pitch: float, yaw: float) Vec3
Create a unit vector from pitch and yaw in radians.
- index(*args: Any) int
Return first index of value.
Raises ValueError if the value is not present.
- Return type:
- length_squared() float
Calculate the squared length of the vector.
This is simply shortcut for x ** 2 + y ** 2 + z ** 2 and can be used for faster comparisons without the need for a square root.
- Return type:
- lerp( ) Vec3
Create a new Vec3 linearly interpolated between this vector and another Vec3-like.
- Parameters:
- Return type:
- class Vec4
A four-dimensional vector represented as X Y Z W coordinates.
Vec4 is an immutable 4D Vector, including most common operators. As an immutable type, all operations return a new object.
Note
The Python ``len` operator returns the number of elements in the vector. For the vector length, use the length() method.
- clamp(
- min_val: float | tuple[float, float, float, float],
- max_val: float | tuple[float, float, float, float],
Restrict the value of the X, Y, Z and W components of the vector to be within the given values.
The minimum and maximum values can be a single value that will be applied to all components, or a tuple of 4 values that will be applied to each component respectively.
- distance(other: Vec4 | tuple[float, float, float, float]) float
Calculate the distance between this vector and another 4D vector.
- dot(other: Vec4 | tuple[float, float, float, float]) float
Calculate the dot product of this vector and another 4D vector.
- index(*args: Any) int
Return first index of value.
Raises ValueError if the value is not present.
- Return type:
- length() float
Calculate the length of the vector:
sqrt(x ** 2 + y ** 2 + z ** 2 + w ** 2)
.- Return type:
- length_squared() float
Calculate the squared length of the vector.
This is simply shortcut for x ** 2 + y ** 2 + z ** 2 + w ** 2 and can be used for faster comparisons without the need for a square root.
- Return type: