pygame documentation |
Pygame Home || Help Contents || Reference Index BufferProxy | camera | cdrom | Color | cursors | display | draw | event | examples | font | freetype | gfxdraw | image | joystick | key | locals | mask | math | midi | mixer | mouse | movie | music | Overlay | PixelArray | pixelcopy | pygame | Rect | scrap | sndarray | sprite | Surface | surfarray | tests | time | transform | version |
pygame.math.enable_swizzling | — | globally enables swizzling for vectors. |
pygame.math.disable_swizzling | — | globally disables swizzling for vectors. |
pygame.math.Vector2 | — | a 2-Dimensional Vector |
pygame.math.Vector3 | — | a 3-Dimensional Vector |
!!!EXPERIMENTAL!!! Note: This Modul is still in development and the API might change. Please report bug and suggestions to pygame-users@seul.org
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.10
Enables swizzling for all vectors until disable_swizzling() is called. By default swizzling is disabled.
Disables swizzling for all vectors until enable_swizzling() is called. By default swizzling is disabled.
pygame.math.Vector2.dot | — | calculates the dot- or scalar-product with the other vector |
pygame.math.Vector2.cross | — | calculates the cross- or vector-product |
pygame.math.Vector2.length | — | returns the euclidic length of the vector. |
pygame.math.Vector2.length_squared | — | returns the squared euclidic length of the vector. |
pygame.math.Vector2.normalize | — | returns a vector with the same direction but length 1. |
pygame.math.Vector2.normalize_ip | — | normalizes the vector in place so that its length is 1. |
pygame.math.Vector2.is_normalized | — | tests if the vector is normalized i.e. has length == 1. |
pygame.math.Vector2.scale_to_length | — | scales the vector to a given length. |
pygame.math.Vector2.reflect | — | returns a vector reflected of a given normal. |
pygame.math.Vector2.reflect_ip | — | reflect the vector of a given normal in place. |
pygame.math.Vector2.distance_to | — | calculates the euclidic distance to a given vector. |
pygame.math.Vector2.distance_squared_to | — | calculates the squared euclidic distance to a given vector. |
pygame.math.Vector2.lerp | — | returns a linear interpolation to the given vector. |
pygame.math.Vector2.slerp | — | returns a spherical interpolation to the given vector. |
pygame.math.Vector2.elementwise | — | The next operation will be performed elementwize. |
pygame.math.Vector2.rotate | — | rotates a vector by a given angle in degrees. |
pygame.math.Vector2.rotate_ip | — | rotates the vector by a given angle in degrees in place. |
pygame.math.Vector2.angle_to | — | calculates the angle to a given vector in degrees. |
pygame.math.Vector2.as_polar | — | returns a tuple with radial distance and azimuthal angle. |
pygame.math.Vector2.from_polar | — | Sets x and y from a polar coordinates tuple. |
Some general information about the Vector2 class.
calculates the third component of the cross-product.
calculates the euclidic length of the vector which follows from the Pythagorean theorem: vec.length() == math.sqrt(vec.x**2 + vec.y**2)
calculates the euclidic 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.
Returns a new vector that has length == 1 and the same direction as self.
Normalizes the vector so that it has length == 1. The direction of the vector is not changed.
Returns True if the vector has length == 1. Otherwise it returns False.
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.
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.
Changes the direction of self as if it would have been reflected of a surface with the given surface normal.
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.
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.
Applies the following operation to each element of the vector.
Returns a vector which has the same length as self but is rotated counterclockwise by the given angle in degrees.
Rotates the vector counterclockwise by the given angle in degrees. The length of the vector is not changed.
Returns the angle between self and the given vector.
Returns a tuple (r, phi) where r is the radial distance, and phi is the azimuthal angle.
Sets x and y from a tuple (r, phi) where r is the radial distance, and phi is the azimuthal angle.
pygame.math.Vector3.dot | — | calculates the dot- or scalar-product with the other vector |
pygame.math.Vector3.cross | — | calculates the cross- or vector-product |
pygame.math.Vector3.length | — | returns the euclidic length of the vector. |
pygame.math.Vector3.length_squared | — | returns the squared euclidic length of the vector. |
pygame.math.Vector3.normalize | — | returns a vector with the same direction but length 1. |
pygame.math.Vector3.normalize_ip | — | normalizes the vector in place so that its length is 1. |
pygame.math.Vector3.is_normalized | — | tests if the vector is normalized i.e. has length == 1. |
pygame.math.Vector3.scale_to_length | — | scales the vector to a given length. |
pygame.math.Vector3.reflect | — | returns a vector reflected of a given normal. |
pygame.math.Vector3.reflect_ip | — | reflect the vector of a given normal in place. |
pygame.math.Vector3.distance_to | — | calculates the euclidic distance to a given vector. |
pygame.math.Vector3.distance_squared_to | — | calculates the squared euclidic distance to a given vector. |
pygame.math.Vector3.lerp | — | returns a linear interpolation to the given vector. |
pygame.math.Vector3.slerp | — | returns a spherical interpolation to the given vector. |
pygame.math.Vector3.elementwise | — | The next operation will be performed elementwize. |
pygame.math.Vector3.rotate | — | rotates a vector by a given angle in degrees. |
pygame.math.Vector3.rotate_ip | — | rotates the vector by a given angle in degrees in place. |
pygame.math.Vector3.rotate_x | — | rotates a vector around the x-axis by the angle in degrees. |
pygame.math.Vector3.rotate_x_ip | — | rotates the vector around the x-axis by the angle in degrees in place. |
pygame.math.Vector3.rotate_y | — | rotates a vector around the y-axis by the angle in degrees. |
pygame.math.Vector3.rotate_y_ip | — | rotates the vector around the y-axis by the angle in degrees in place. |
pygame.math.Vector3.rotate_z | — | rotates a vector around the z-axis by the angle in degrees. |
pygame.math.Vector3.rotate_z_ip | — | rotates the vector around the z-axis by the angle in degrees in place. |
pygame.math.Vector3.angle_to | — | calculates the angle to a given vector in degrees. |
pygame.math.Vector3.as_spherical | — | returns a tuple with radial distance, inclination and azimuthal angle. |
pygame.math.Vector3.from_spherical | — | Sets x, y and z from a spherical coordinates 3-tuple. |
Some general information about the Vector3 class.
calculates the cross-product.
calculates the euclidic length of the vector which follows from the Pythagorean theorem: vec.length() == math.sqrt(vec.x**2 + vec.y**2 + vec.z**2)
calculates the euclidic 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.
Returns a new vector that has length == 1 and the same direction as self.
Normalizes the vector so that it has length == 1. The direction of the vector is not changed.
Returns True if the vector has length == 1. Otherwise it returns False.
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.
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.
Changes the direction of self as if it would have been reflected of a surface with the given surface normal.
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.
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.
Applies the following operation to each element of the vector.
Returns a vector which has the same length as self but is rotated counterclockwise by the given angle in degrees around the given axis.
Rotates the vector counterclockwise around the given axis by the given angle in degrees. The length of the vector is not changed.
Returns a vector which has the same length as self but is rotated counterclockwise around the x-axis by the given angle in degrees.
Rotates the vector counterclockwise around the x-axis by the given angle in degrees. The length of the vector is not changed.
Returns a vector which has the same length as self but is rotated counterclockwise around the y-axis by the given angle in degrees.
Rotates the vector counterclockwise around the y-axis by the given angle in degrees. The length of the vector is not changed.
Returns a vector which has the same length as self but is rotated counterclockwise around the z-axis by the given angle in degrees.
Rotates the vector counterclockwise around the z-axis by the given angle in degrees. The length of the vector is not changed.
Returns the angle between self and the given vector.
Returns a tuple (r, theta, phi) where r is the radial distance, theta is the inclination angle and phi is the azimuthal angle.
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.