pymunk.vec2d Module

This module contain the Vec2d class that is used in all of pymunk when a vector is needed.

The Vec2d class is used almost everywhere in pymunk for 2d coordinates and vectors, for example to define gravity vector in a space. However, pymunk is smart enough to convert tuples or tuple like objects to Vec2ds so you usually do not need to explicitly do conversions if you happen to have a tuple:

>>> import pymunk
>>> space = pymunk.Space()
>>> space.gravity
Vec2d(0.0, 0.0)
>>> space.gravity = 3,5
>>> space.gravity
Vec2d(3.0, 5.0)
>>> space.gravity += 2,6
>>> space.gravity
Vec2d(5.0, 11.0)

More examples:

>>> from pymunk.vec2d import Vec2d
>>> Vec2d(7.3, 4.2)
Vec2d(7.3, 4.2)
>>> Vec2d(7.3, 4.2) + Vec2d(1, 2)
Vec2d(8.3, 6.2)
class pymunk.vec2d.Vec2d(x: float, y: float)[source]

Bases: tuple

2d vector class, supports vector and scalar operators, and also provides some high level functions.

__abs__() float[source]

Return the length of the Vec2d

>>> abs(Vec2d(3,4))
__add__(other: Tuple[float, float]) pymunk.vec2d.Vec2d[source]

Add a Vec2d with another Vec2d or Tuple of size 2

>>> Vec2d(3,4) + Vec2d(1,2)
Vec2d(4, 6)
>>> Vec2d(3,4) + (1,2)
Vec2d(4, 6)
__floordiv__(other: float) pymunk.vec2d.Vec2d[source]

Floor division by a float (also known as integer division)

>>> Vec2d(3,6) // 2.0
Vec2d(1.0, 3.0)
__mul__(other: float) pymunk.vec2d.Vec2d[source]

Multiply with a float

>>> Vec2d(3,6) * 2.5
Vec2d(7.5, 15.0)
__neg__() pymunk.vec2d.Vec2d[source]

Return the negated version of the Vec2d

>>> -Vec2d(1,-2)
Vec2d(-1, 2)
__pos__() pymunk.vec2d.Vec2d[source]

Return the unary pos of the Vec2d.

>>> +Vec2d(1,-2)
Vec2d(1, -2)
__sub__(other: Tuple[float, float]) pymunk.vec2d.Vec2d[source]

Subtract a Vec2d with another Vec2d or Tuple of size 2

>>> Vec2d(3,4) - Vec2d(1,2)
Vec2d(2, 2)
>>> Vec2d(3,4) - (1,2)
Vec2d(2, 2)
__truediv__(other: float) pymunk.vec2d.Vec2d[source]

Division by a float

>>> Vec2d(3,6) / 2.0
Vec2d(1.5, 3.0)
property angle: float

The angle (in radians) of the vector

property angle_degrees: float

Gets the angle (in degrees) of a vector

convert_to_basis(x_vector: Tuple[float, float], y_vector: Tuple[float, float]) pymunk.vec2d.Vec2d[source]
count(value, /)

Return number of occurrences of value.

cpvrotate(other: Tuple[float, float]) pymunk.vec2d.Vec2d[source]

Uses complex multiplication to rotate this vector by the other.

cpvunrotate(other: Tuple[float, float]) pymunk.vec2d.Vec2d[source]

The inverse of cpvrotate

cross(other: Tuple[float, float]) float[source]
The cross product between the vector and other vector

v1.cross(v2) -> v1.x*v2.y - v1.y*v2.x


The cross product

dot(other: Tuple[float, float]) float[source]
The dot product between the vector and other vector -> v1.x*v2.x + v1.y*v2.y


The dot product

get_angle_between(other: Tuple[float, float]) float[source]

Get the angle between the vector and the other in radians


The angle

get_angle_degrees_between(other: pymunk.vec2d.Vec2d) float[source]

Get the angle between the vector and the other in degrees


The angle (in degrees)

get_dist_sqrd(other: Tuple[float, float]) float[source]

The squared distance between the vector and other vector It is more efficent to use this method than to call get_distance() first and then do a sqrt() on the result.


The squared distance

get_distance(other: Tuple[float, float]) float[source]

The distance between the vector and other vector


The distance

get_length_sqrd() float[source]

Get the squared length of the vector. If the squared length is enough it is more efficient to use this method instead of first calling get_length() or access .length and then do a x**2.

>>> v = Vec2d(3,4)
>>> v.get_length_sqrd() == v.length**2

The squared length

index(value, start=0, stop=9223372036854775807, /)

Return first index of value.

Raises ValueError if the value is not present.

property int_tuple: Tuple[int, int]

The x and y values of this vector as a tuple of ints. Uses round() to round to closest int.

>>> Vec2d(0.9, 2.4).int_tuple
(1, 2)
interpolate_to(other: Tuple[float, float], range: float) pymunk.vec2d.Vec2d[source]
property length: float

Get the length of the vector.

>>> Vec2d(10, 0).length
>>> '%.2f' % Vec2d(10, 20).length

The length

normalized() pymunk.vec2d.Vec2d[source]

Get a normalized copy of the vector Note: This function will return 0 if the length of the vector is 0.


A normalized vector

normalized_and_length() Tuple[pymunk.vec2d.Vec2d, float][source]

Normalize the vector and return its length before the normalization


The length before the normalization

static ones() pymunk.vec2d.Vec2d[source]

A vector where both x and y is 1

>>> Vec2d.ones()
Vec2d(1, 1)
perpendicular() pymunk.vec2d.Vec2d[source]
perpendicular_normal() pymunk.vec2d.Vec2d[source]
projection(other: Tuple[float, float]) pymunk.vec2d.Vec2d[source]

Project this vector on top of other vector

rotated(angle_radians: float) pymunk.vec2d.Vec2d[source]

Create and return a new vector by rotating this vector by angle_radians radians.


Rotated vector

rotated_degrees(angle_degrees: float) pymunk.vec2d.Vec2d[source]

Create and return a new vector by rotating this vector by angle_degrees degrees.


Rotade vector

scale_to_length(length: float) pymunk.vec2d.Vec2d[source]

Return a copy of this vector scaled to the given length.

>>> '%.2f, %.2f' % Vec2d(10, 20).scale_to_length(20)
'8.94, 17.89'
static unit() pymunk.vec2d.Vec2d[source]

A unit vector pointing up

>>> Vec2d.unit()
Vec2d(0, 1)
property x

Alias for field number 0

property y

Alias for field number 1

static zero() pymunk.vec2d.Vec2d[source]

A vector of zero length.

Vec2d(0, 0)