pygame2.math – Vector math features used by pygame2

Vector math features used by pygame2

Module Functions

pygame2.math.vector_from_polar(p1, p2) → Vector2
Creates a 2D vector from the passed polar coordinates.
pygame2.math.vector_from_spherical(p1, p2, p3) → Vector2
Creates a 3D vector from the passed spherical coordinates.

Vector

class pygame2.math.Vector(elements) → Vector

Creates a new Vector with the specified elements or dimension.

Creates a new n-dimensional Vector. elements can be either a sequence of numbers representing the values for each dimension or a positive integer to create an empty vector with elements dimensions.

Example:

from pygame2.math import Vector

# Create a 3D vector with x=1, y=2 and z=3
v1 = Vector ([1, 2, 3])

# Create a vector with 15 dimensions, where each component is set to 0.
v2 = Vector (15)

Attributes

Vector.dimension
Gets the dimensions of the Vector.
Vector.length
Gets the length of the Vector.
Vector.length_squared
Gets the length of the Vector.
Vector.epsilon

Gets or sets the exactness delta of the Vector.

epsilon represents a small fractional value to come around rounding issues on floating point calculations. It is usually used to soothe rounding differences and thus to allow the system to return an appropriate state, with a small tradeoff for the exactness.

Example:

(0.0000000000001 - 0.0000000000009) == 0.0

Given that epsilon is large enough, the above calculation will be true. To get over rounding issues on vector operations with small fractional parts or to make them more exact, the concrete epsilon value can be freely adjusted.

Vector.elements

Gets or sets the elements of the Vector.

This will only set a maximum of dimension values.

Vector.normalized
Gets whether the Vector is normalized.

Methods

Vector.normalize() → Vector
Creates a normalized representation of this Vector.
Vector.normalize_ip() → None
Normalizes the Vector in place.
Vector.slerp() → Vector
TODO
Vector.lerp() → Vector
TODO
Vector.dot(v) → float
Calculates the dot product of the Vector and the passed argument.
Vector.scale_to(length) → Vector
Scales the Vector to match the passed length.
Vector.reflect() → Vector
Creates a reflected representation of the Vector.
Vector.reflect_ip() → None
Reflects the Vector in place.
Vector.distance(v) → float
Calculates the distance between two vectors.
Vector.distance_squared() → None
Calculates the squared distance between two vectors.

Vector2

class pygame2.math.Vector2 → Vector2

Creates a new Vector2.

A simple 2D vector containing only two elements.

Attributes

Vector2.dimension
Gets the dimensions of the Vector2.
Vector2.elements

Gets or sets the elements of the Vector2.

This will only set a maximum of two values.

Vector2.x
Gets or sets first element of the Vector2.
Vector2.y
Gets or sets second element of the Vector2.

Methods

Vector2.rotate(angle) → Vector2
Rotates the Vector2 by the passed degrees.
Vector2.rotate_ip(angle) → Vector2
Rotates the Vector2 by the passed degrees in place.
Vector2.as_polar() → float, float
Gets the polar coordinate representation of the Vector2.
Vector2.angle_to(v) → float
Calculates the angle (in degrees) between two Vector2 objects.
Vector2.cross(v) → Vector2
Creates the cross product of the two Vector2 objects.

Vector3

class pygame2.math.Vector3 → Vector3

Creates a new Vector3.

A simple 3D vector containing only three elements.

Attributes

Vector3.dimension
Gets the dimensions of the Vector3.
Vector3.elements

Gets or sets the elements of the Vector3.

This will only set a maximum of three values.

Vector3.x
Gets or sets first element of the Vector3.
Vector3.y
Gets or sets second element of the Vector3.
Vector3.z
Gets or sets third element of the Vector3.

Methods

Vector3.rotate_x(angle) → Vector3
Rotates the Vector3 by the passed degrees around the x-axis.
Vector3.rotate_x_ip(angle) → None
Rotates the Vector3 by the passed degrees in place around the x-axis.
Vector3.rotate_y(angle) → Vector3
Rotates the Vector3 by the passed degrees around the y-axis.
Vector3.rotate_y_ip(angle) → None
Rotates the Vector3 by the passed degrees in place around the y-axis.
Vector3.rotate_z(angle) → Vector3
Rotates the Vector3 by the passed degrees around the z-axis.
Vector3.rotate_z_ip(angle) → None
Rotates the Vector3 by the passed degrees in place around the z-axis.
Vector3.rotate(angle, axis) → Vector3
Rotates the Vector3 by the passed degrees around the passed axis vector.
Vector3.rotate_ip(angle, axis) → Vector3
Rotates the Vector3 by the passed degrees in place around the passed axis vector.
Vector3.angle_to(v) → float
Calculates the angle between two vectors in degrees
Vector3.cross(v) → Vector3
Calculates the cross product between the two Vector3 instances.
Vector3.as_spherical() → float, float, float
Gets the sperhical coordinate representation of the Vector3.