W3cubDocs

/Pygame

pygame.math

pygame module for vector classes

The pygame math module currently provides Vector classes in two and three dimensions, Vector2 and Vector3 respectively.

They support the following numerical operations: vec+vec, vec-vec, vec*number, number*vec, vec/number, vec//number, vec+=vec, vec-=vec, vec*=number, vec/=number, vec//=number. All these operations will be performed elementwise. In addition vec*vec will perform a scalar-product (a.k.a. dot-product). If you want to multiply every element from vector v with every element from vector w you can use the elementwise method: v.elementwise() \* w

New in pygame 1.9.2pre. 1.9.4 removed experimental notice. 1.9.4 changed constructors to require 2, or 3 elements rather than assigning 0 default. 1.9.4 allowed scalar construction like GLSL Vector2(2) == Vector2(2.0, 2.0) 1.9.4 pygame.math required import. more convienient pygame.Vector2 and pygame.Vector3.

pygame.math.Vector2

a 2-Dimensional Vector
Vector2() -> Vector2
Vector2(int) -> Vector2
Vector2(float) -> Vector2
Vector2(Vector2) -> Vector2
Vector2(x, y) -> Vector2
Vector2((x, y)) -> Vector2

Some general information about the Vector2 class.

dot(Vector2) -> float

calculates the dot- or scalar-product with the other vector

cross(Vector2) -> float

calculates the cross- or vector-product

calculates the third component of the cross-product.

magnitude() -> float

returns the Euclidean magnitude of the vector.

calculates the magnitude of the vector which follows from the theorem: vec.magnitude() == math.sqrt(vec.x**2 + vec.y**2)

magnitude_squared() -> float

returns the squared magnitude of the vector.

calculates the magnitude of the vector which follows from the theorem: vec.magnitude_squared() == vec.x**2 + vec.y**2 This is faster than vec.magnitude() because it avoids the square root.

length() -> float

returns the Euclidean length of the vector.

calculates the Euclidean length of the vector which follows from the Pythagorean theorem: vec.length() == math.sqrt(vec.x**2 + vec.y**2)

length_squared() -> float

returns the squared Euclidean length of the vector.

calculates the Euclidean length of the vector which follows from the Pythagorean theorem: vec.length_squared() == vec.x**2 + vec.y**2 This is faster than vec.length() because it avoids the square root.

normalize() -> Vector2

returns a vector with the same direction but length 1.

Returns a new vector that has length == 1 and the same direction as self.

normalize_ip() -> None

normalizes the vector in place so that its length is 1.

Normalizes the vector so that it has length == 1. The direction of the vector is not changed.

is_normalized() -> Bool

tests if the vector is normalized i.e. has length == 1.

Returns True if the vector has length == 1. Otherwise it returns False.

scale_to_length(float) -> None

scales the vector to a given length.

Scales the vector so that it has the given length. The direction of the vector is not changed. You can also scale to length 0. If the vector is the zero vector (i.e. has length 0 thus no direction) an ZeroDivisionError is raised.

reflect(Vector2) -> Vector2

returns a vector reflected of a given normal.

Returns a new vector that points in the direction as if self would bounce of a surface characterized by the given surface normal. The length of the new vector is the same as self's.

reflect_ip(Vector2) -> None

reflect the vector of a given normal in place.

Changes the direction of self as if it would have been reflected of a surface with the given surface normal.

distance_to(Vector2) -> float

calculates the Euclidean distance to a given vector.

distance_squared_to(Vector2) -> float

calculates the squared Euclidean distance to a given vector.

lerp(Vector2, float) -> Vector2

returns a linear interpolation to the given vector.

Returns a Vector which is a linear interpolation between self and the given Vector. The second parameter determines how far between self an other the result is going to be. It must be a value between 0 and 1 where 0 means self an 1 means other will be returned.

slerp(Vector2, float) -> Vector2

returns a spherical interpolation to the given vector.

Calculates the spherical interpolation from self to the given Vector. The second argument - often called t - must be in the range [-1, 1]. It parametrizes where - in between the two vectors - the result should be. If a negative value is given the interpolation will not take the complement of the shortest path.

elementwise() -> VectorElementwiseProxy

The next operation will be performed elementwise.

Applies the following operation to each element of the vector.

rotate(float) -> Vector2

rotates a vector by a given angle in degrees.

Returns a vector which has the same length as self but is rotated counterclockwise by the given angle in degrees.

rotate_ip(float) -> None

rotates the vector by a given angle in degrees in place.

Rotates the vector counterclockwise by the given angle in degrees. The length of the vector is not changed.

angle_to(Vector2) -> float

calculates the angle to a given vector in degrees.

Returns the angle between self and the given vector.

as_polar() -> (r, phi)

returns a tuple with radial distance and azimuthal angle.

Returns a tuple (r, phi) where r is the radial distance, and phi is the azimuthal angle.

from_polar((r, phi)) -> None

Sets x and y from a polar coordinates tuple.

Sets x and y from a tuple (r, phi) where r is the radial distance, and phi is the azimuthal angle.

pygame.math.Vector3

a 3-Dimensional Vector
Vector3() -> Vector3
Vector3(int) -> Vector2
Vector3(float) -> Vector2
Vector3(Vector3) -> Vector3
Vector3(x, y, z) -> Vector3
Vector3((x, y, z)) -> Vector3

Some general information about the Vector3 class.

dot(Vector3) -> float

calculates the dot- or scalar-product with the other vector

cross(Vector3) -> float

calculates the cross- or vector-product

calculates the cross-product.

magnitude() -> float

returns the Euclidean magnitude of the vector.

calculates the magnitude of the vector which follows from the theorem: vec.magnitude() == math.sqrt(vec.x**2 + vec.y**2 + vec.z**2)

magnitude_squared() -> float

returns the squared Euclidean magnitude of the vector.

calculates the magnitude of the vector which follows from the theorem: vec.magnitude_squared() == vec.x**2 + vec.y**2 + vec.z**2 This is faster than vec.magnitude() because it avoids the square root.

length() -> float

returns the Euclidean length of the vector.

calculates the Euclidean length of the vector which follows from the Pythagorean theorem: vec.length() == math.sqrt(vec.x**2 + vec.y**2 + vec.z**2)

length_squared() -> float

returns the squared Euclidean length of the vector.

calculates the Euclidean length of the vector which follows from the Pythagorean theorem: vec.length_squared() == vec.x**2 + vec.y**2 + vec.z**2 This is faster than vec.length() because it avoids the square root.

normalize() -> Vector3

returns a vector with the same direction but length 1.

Returns a new vector that has length == 1 and the same direction as self.

normalize_ip() -> None

normalizes the vector in place so that its length is 1.

Normalizes the vector so that it has length == 1. The direction of the vector is not changed.

is_normalized() -> Bool

tests if the vector is normalized i.e. has length == 1.

Returns True if the vector has length == 1. Otherwise it returns False.

scale_to_length(float) -> None

scales the vector to a given length.

Scales the vector so that it has the given length. The direction of the vector is not changed. You can also scale to length 0. If the vector is the zero vector (i.e. has length 0 thus no direction) an ZeroDivisionError is raised.

reflect(Vector3) -> Vector3

returns a vector reflected of a given normal.

Returns a new vector that points in the direction as if self would bounce of a surface characterized by the given surface normal. The length of the new vector is the same as self's.

reflect_ip(Vector3) -> None

reflect the vector of a given normal in place.

Changes the direction of self as if it would have been reflected of a surface with the given surface normal.

distance_to(Vector3) -> float

calculates the Euclidean distance to a given vector.

distance_squared_to(Vector3) -> float

calculates the squared Euclidean distance to a given vector.

lerp(Vector3, float) -> Vector3

returns a linear interpolation to the given vector.

Returns a Vector which is a linear interpolation between self and the given Vector. The second parameter determines how far between self an other the result is going to be. It must be a value between 0 and 1 where 0 means self an 1 means other will be returned.

slerp(Vector3, float) -> Vector3

returns a spherical interpolation to the given vector.

Calculates the spherical interpolation from self to the given Vector. The second argument - often called t - must be in the range [-1, 1]. It parametrizes where - in between the two vectors - the result should be. If a negative value is given the interpolation will not take the complement of the shortest path.

elementwise() -> VectorElementwiseProxy

The next operation will be performed elementwise.

Applies the following operation to each element of the vector.

rotate(Vector3, float) -> Vector3

rotates a vector by a given angle in degrees.

Returns a vector which has the same length as self but is rotated counterclockwise by the given angle in degrees around the given axis.

rotate_ip(Vector3, float) -> None

rotates the vector by a given angle in degrees in place.

Rotates the vector counterclockwise around the given axis by the given angle in degrees. The length of the vector is not changed.

rotate_x(float) -> Vector3

rotates a vector around the x-axis by the angle in degrees.

Returns a vector which has the same length as self but is rotated counterclockwise around the x-axis by the given angle in degrees.

rotate_x_ip(float) -> None

rotates the vector around the x-axis by the angle in degrees in place.

Rotates the vector counterclockwise around the x-axis by the given angle in degrees. The length of the vector is not changed.

rotate_y(float) -> Vector3

rotates a vector around the y-axis by the angle in degrees.

Returns a vector which has the same length as self but is rotated counterclockwise around the y-axis by the given angle in degrees.

rotate_y_ip(float) -> None

rotates the vector around the y-axis by the angle in degrees in place.

Rotates the vector counterclockwise around the y-axis by the given angle in degrees. The length of the vector is not changed.

rotate_z(float) -> Vector3

rotates a vector around the z-axis by the angle in degrees.

Returns a vector which has the same length as self but is rotated counterclockwise around the z-axis by the given angle in degrees.

rotate_z_ip(float) -> None

rotates the vector around the z-axis by the angle in degrees in place.

Rotates the vector counterclockwise around the z-axis by the given angle in degrees. The length of the vector is not changed.

angle_to(Vector3) -> float

calculates the angle to a given vector in degrees.

Returns the angle between self and the given vector.

as_spherical() -> (r, theta, phi)

returns a tuple with radial distance, inclination and azimuthal angle.

Returns a tuple (r, theta, phi) where r is the radial distance, theta is the inclination angle and phi is the azimuthal angle.

from_spherical((r, theta, phi)) -> None

Sets x, y and z from a spherical coordinates 3-tuple.

Sets x, y and z from a tuple (r, theta, phi) where r is the radial distance, theta is the inclination angle and phi is the azimuthal angle.

enable_swizzling() -> None

globally enables swizzling for vectors.

DEPRECATED: Not needed anymore. Will be removed in a later version.

Enables swizzling for all vectors until disable_swizzling() is called. By default swizzling is disabled.

disable_swizzling() -> None

globally disables swizzling for vectors.

DEPRECATED: Not needed anymore. Will be removed in a later version.

Disables swizzling for all vectors until enable_swizzling() is called. By default swizzling is disabled.

© Pygame Developpers.
Licensed under the GNU LGPL License version 2.1.
https://www.pygame.org/docs/ref/math.html