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, round(vec,ndigits=0).
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
The coordinates of a vector can be retrieved or set using attributes or
subscripts
Linearly interpolates between a and b by weight using the formula a+(b-a)*weight.
If weight is 0.5, lerp will return the value half-way between a
and b. When a=10 and b=20, lerp(a,b,0.5) will return 15. You
can think of weight as the percentage of interpolation from a to b, 0.0
being 0% and 1.0 being 100%.
lerp can be used for many things. You could rotate a sprite by a weight with
angle=lerp(0,360,weight). You could even scale an enemy's attack value
based on the level you're playing:
If you're on level 0, attack will be 10, if you're on level 10,
attack will be 50. If you're on level 5, the
result of current_level/MAX_LEVEL will be 0.5
which represents 50%, therefore attack will be 30, which is the midpoint of 10 and 50.
Raises a ValueError if weight is outside the range of [0,1].
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.
returns the squared Euclidean length of the vector.
length_squared() -> float
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.
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) a
ValueError 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.
Returns a Vector which is moved towards the given Vector by a given
distance and does not overshoot past its target Vector.
The first parameter determines the target Vector, while the second
parameter determines the delta distance. If the distance is in the
negatives, then it will move away from the target Vector.
Moves itself toward the given Vector at a given distance and does not
overshoot past its target Vector.
The first parameter determines the target Vector, while the second
parameter determines the delta distance. If the distance is in the
negatives, then it will move away from the target Vector.
returns a linear interpolation to the given vector.
lerp(Vector2, float) -> Vector2
Returns a Vector which is a linear interpolation between self and the
given Vector. The second parameter determines how far between self and
other the result is going to be. It must be a value between 0 and 1
where 0 means self and 1 means other will be returned.
returns a spherical interpolation to the given vector.
slerp(Vector2, float) -> Vector2
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.
Returns a vector which has the same length as self but is rotated
counterclockwise by the given angle in degrees.
(Note that due to pygame's inverted y coordinate system, the rotation
will look clockwise if displayed).
Returns a vector which has the same length as self but is rotated
counterclockwise by the given angle in radians.
(Note that due to pygame's inverted y coordinate system, the rotation
will look clockwise if displayed).
rotates the vector by a given angle in degrees in place.
rotate_ip(angle) -> None
Rotates the vector counterclockwise by the given angle in degrees. The
length of the vector is not changed.
(Note that due to pygame's inverted y coordinate system, the rotation
will look clockwise if displayed).
rotates the vector by a given angle in radians in place.
rotate_rad_ip(angle) -> None
Rotates the vector counterclockwise by the given angle in radians. The
length of the vector is not changed.
(Note that due to pygame's inverted y coordinate system, the rotation
will look clockwise if displayed).
calculates the angle to a given vector in degrees.
angle_to(Vector2) -> float
Returns the angle from self to the passed Vector2 that would rotate self
to be aligned with the passed Vector2 without crossing over the negative
x-axis.
Creates a Vector2(x, y) or sets x and y from a polar coordinates tuple.
Vector2.from_polar((r, phi)) -> Vector2
Vector2().from_polar((r, phi)) -> None
If used from the class creates a Vector2(x,y), else sets x and y.
The values of x and y are defined from a tuple (r,phi) where r
is the radial distance, and phi is the azimuthal angle.
Returns the projected vector. This is useful for collision detection in finding the components in a certain direction (e.g. in direction of the wall).
For a more detailed explanation see Wikipedia.
Returns a new copy of a vector with the magnitude clamped between
max_length and min_length. If only one argument is passed, it is
taken to be the max_length
This function raises ValueError if min_length is greater than
max_length, or if either of these values are negative.
Both Vector classes have a value named epsilon that defaults to 1e-6.
This value acts as a numerical margin in various methods to account for floating point
arithmetic errors. Specifically, epsilon is used in the following places:
comparing Vectors (== and !=)
the is_normalized method (if the square of the length is within epsilon of 1, it's normalized)
slerping (a Vector with a length of <epsilon is considered a zero vector, and can't slerp with that)
reflection (can't reflect over the zero vector)
projection (can't project onto the zero vector)
rotation (only used when rotating by a multiple of 90 degrees)
While it's possible to change epsilon for a specific instance of a Vector, all the other Vectors
will retain the default value. Changing epsilon on a specific instance however could lead to some
asymmetric behavior where symmetry would be expected, such as
u=pygame.Vector2(0,1)v=pygame.Vector2(0,1.2)u.epsilon=0.5# don't set it nearly this largeprint(u==v)# >> Trueprint(v==u)# >> False
You'll probably never have to change epsilon from the default value, but in rare situations you might
find that either the margin is too large or too small, in which case changing epsilon slightly
might help you out.
returns the squared Euclidean magnitude of the vector.
magnitude_squared() -> float
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.
returns the squared Euclidean length of the vector.
length_squared() -> float
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.
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) a
ValueError 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.
Returns a Vector which is moved towards the given Vector by a given
distance and does not overshoot past its target Vector.
The first parameter determines the target Vector, while the second
parameter determines the delta distance. If the distance is in the
negatives, then it will move away from the target Vector.
Moves itself toward the given Vector at a given distance and does not
overshoot past its target Vector.
The first parameter determines the target Vector, while the second
parameter determines the delta distance. If the distance is in the
negatives, then it will move away from the target Vector.
returns a linear interpolation to the given vector.
lerp(Vector3, float) -> Vector3
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 and 1 means other will be returned.
returns a spherical interpolation to the given vector.
slerp(Vector3, float) -> Vector3
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.
Returns a vector which has the same length as self but is rotated
counterclockwise by the given angle in degrees around the given axis.
(Note that due to pygame's inverted y coordinate system, the rotation
will look clockwise if displayed).
Returns a vector which has the same length as self but is rotated
counterclockwise by the given angle in radians around the given axis.
(Note that due to pygame's inverted y coordinate system, the rotation
will look clockwise if displayed).
rotates the vector by a given angle in degrees in place.
rotate_ip(angle, Vector3) -> None
Rotates the vector counterclockwise around the given axis by the given
angle in degrees. The length of the vector is not changed.
(Note that due to pygame's inverted y coordinate system, the rotation
will look clockwise if displayed).
rotates the vector by a given angle in radians in place.
rotate_rad_ip(angle, Vector3) -> None
Rotates the vector counterclockwise around the given axis by the given
angle in radians. The length of the vector is not changed.
(Note that due to pygame's inverted y coordinate system, the rotation
will look clockwise if displayed).
rotates a vector around the x-axis by the angle in degrees.
rotate_x(angle) -> Vector3
Returns a vector which has the same length as self but is rotated
counterclockwise around the x-axis by the given angle in degrees.
(Note that due to pygame's inverted y coordinate system, the rotation
will look clockwise if displayed).
rotates a vector around the x-axis by the angle in radians.
rotate_x_rad(angle) -> Vector3
Returns a vector which has the same length as self but is rotated
counterclockwise around the x-axis by the given angle in radians.
(Note that due to pygame's inverted y coordinate system, the rotation
will look clockwise if displayed).
rotates the vector around the x-axis by the angle in degrees in place.
rotate_x_ip(angle) -> None
Rotates the vector counterclockwise around the x-axis by the given angle
in degrees. The length of the vector is not changed.
(Note that due to pygame's inverted y coordinate system, the rotation
will look clockwise if displayed).
rotates the vector around the x-axis by the angle in radians in place.
rotate_x_rad_ip(angle) -> None
Rotates the vector counterclockwise around the x-axis by the given angle
in radians. The length of the vector is not changed.
(Note that due to pygame's inverted y coordinate system, the rotation
will look clockwise if displayed).
rotates a vector around the y-axis by the angle in degrees.
rotate_y(angle) -> Vector3
Returns a vector which has the same length as self but is rotated
counterclockwise around the y-axis by the given angle in degrees.
(Note that due to pygame's inverted y coordinate system, the rotation
will look clockwise if displayed).
rotates a vector around the y-axis by the angle in radians.
rotate_y_rad(angle) -> Vector3
Returns a vector which has the same length as self but is rotated
counterclockwise around the y-axis by the given angle in radians.
(Note that due to pygame's inverted y coordinate system, the rotation
will look clockwise if displayed).
rotates the vector around the y-axis by the angle in degrees in place.
rotate_y_ip(angle) -> None
Rotates the vector counterclockwise around the y-axis by the given angle
in degrees. The length of the vector is not changed.
(Note that due to pygame's inverted y coordinate system, the rotation
will look clockwise if displayed).
rotates the vector around the y-axis by the angle in radians in place.
rotate_y_rad_ip(angle) -> None
Rotates the vector counterclockwise around the y-axis by the given angle
in radians. The length of the vector is not changed.
(Note that due to pygame's inverted y coordinate system, the rotation
will look clockwise if displayed).
rotates a vector around the z-axis by the angle in degrees.
rotate_z(angle) -> Vector3
Returns a vector which has the same length as self but is rotated
counterclockwise around the z-axis by the given angle in degrees.
(Note that due to pygame's inverted y coordinate system, the rotation
will look clockwise if displayed).
rotates a vector around the z-axis by the angle in radians.
rotate_z_rad(angle) -> Vector3
Returns a vector which has the same length as self but is rotated
counterclockwise around the z-axis by the given angle in radians.
(Note that due to pygame's inverted y coordinate system, the rotation
will look clockwise if displayed).
rotates the vector around the z-axis by the angle in degrees in place.
rotate_z_ip(angle) -> None
Rotates the vector counterclockwise around the z-axis by the given angle
in degrees. The length of the vector is not changed.
(Note that due to pygame's inverted y coordinate system, the rotation
will look clockwise if displayed).
rotates the vector around the z-axis by the angle in radians in place.
rotate_z_rad_ip(angle) -> None
Rotates the vector counterclockwise around the z-axis by the given angle
in radians. The length of the vector is not changed.
(Note that due to pygame's inverted y coordinate system, the rotation
will look clockwise if displayed).
If used from the class creates a Vector3(x, y, z), else sets x, y, and z.
The values of x, y, and z are from a tuple (r,theta,phi) where r is the radial
distance, theta is the inclination angle and phi is the azimuthal angle.
Returns the projected vector. This is useful for collision detection in finding the components in a certain direction (e.g. in direction of the wall).
For a more detailed explanation see Wikipedia.
Returns a new copy of a vector with the magnitude clamped between
max_length and min_length. If only one argument is passed, it is
taken to be the max_length
This function raises ValueError if min_length is greater than
max_length, or if either of these values are negative.