pymunk.constraint Module

A constraint is something that describes how two bodies interact with each other. (how they constrain each other). Constraints can be simple joints that allow bodies to pivot around each other like the bones in your body, or they can be more abstract like the gear joint or motors.

This submodule contain all the constraints that are supported by pymunk.

Chipmunk has a good overview of the different constraint on youtube which works fine to showcase them in pymunk as well. http://www.youtube.com/watch?v=ZgJJZTS0aMM

class pymunk.constraint.Constraint(constraint=None)[source]

Bases: object

Base class of all constraints.

You usually don’t want to create instances of this class directly, but instead use one of the specific constraints such as the PinJoint.

__init__(constraint=None)[source]
a

The first of the two bodies constrained

activate_bodies()[source]

Activate the bodies this constraint is attached to

b

The second of the two bodies constrained

error_bias

The rate at which joint error is corrected.

Defaults to pow(1.0 - 0.1, 60.0) meaning that it will correct 10% of the error every 1/60th of a second.

impulse

Get the last impulse applied by this constraint.

max_bias

The maximum rate at which joint error is corrected. Defaults to infinity

max_force

The maximum force that the constraint can use to act on the two bodies. Defaults to infinity

class pymunk.constraint.DampedRotarySpring(a, b, rest_angle, stiffness, damping)[source]

Bases: pymunk.constraint.Constraint

Like a damped spring, but works in an angular fashion

__init__(a, b, rest_angle, stiffness, damping)[source]

Like a damped spring, but works in an angular fashion.

Parameters :
rest_angle

The relative angle in radians that the bodies want to have

stiffness

The spring constant (Young’s modulus).

damping

How soft to make the damping of the spring.

a

The first of the two bodies constrained

activate_bodies()

Activate the bodies this constraint is attached to

b

The second of the two bodies constrained

damping

How soft to make the damping of the spring.

error_bias

The rate at which joint error is corrected.

Defaults to pow(1.0 - 0.1, 60.0) meaning that it will correct 10% of the error every 1/60th of a second.

impulse

Get the last impulse applied by this constraint.

max_bias

The maximum rate at which joint error is corrected. Defaults to infinity

max_force

The maximum force that the constraint can use to act on the two bodies. Defaults to infinity

rest_angle

The relative angle in radians that the bodies want to have

stiffness

The spring constant (Young’s modulus).

torque_func

Set the torque function

func(self, relative_angle) -> torque

Callback Parameters
relative_angle : float
The relative angle
class pymunk.constraint.DampedSpring(a, b, anchr1, anchr2, rest_length, stiffness, damping)[source]

Bases: pymunk.constraint.Constraint

A damped spring

__init__(a, b, anchr1, anchr2, rest_length, stiffness, damping)[source]

Defined much like a slide joint.

Parameters :
anchr1 : Vec2d or (x,y)

Anchor point 1, relative to body a

anchr2 : Vec2d or (x,y)

Anchor point 2, relative to body b

rest_length : float

The distance the spring wants to be.

stiffness : float

The spring constant (Young’s modulus).

damping : float

How soft to make the damping of the spring.

a

The first of the two bodies constrained

activate_bodies()

Activate the bodies this constraint is attached to

anchr1
anchr2
b

The second of the two bodies constrained

damping

How soft to make the damping of the spring.

error_bias

The rate at which joint error is corrected.

Defaults to pow(1.0 - 0.1, 60.0) meaning that it will correct 10% of the error every 1/60th of a second.

impulse

Get the last impulse applied by this constraint.

max_bias

The maximum rate at which joint error is corrected. Defaults to infinity

max_force

The maximum force that the constraint can use to act on the two bodies. Defaults to infinity

rest_length

The distance the spring wants to be.

stiffness

The spring constant (Young’s modulus).

class pymunk.constraint.GearJoint(a, b, phase, ratio)[source]

Bases: pymunk.constraint.Constraint

Keeps the angular velocity ratio of a pair of bodies constant.

__init__(a, b, phase, ratio)[source]

Keeps the angular velocity ratio of a pair of bodies constant. ratio is always measured in absolute terms. It is currently not possible to set the ratio in relation to a third body’s angular velocity. phase is the initial angular offset of the two bodies.

a

The first of the two bodies constrained

activate_bodies()

Activate the bodies this constraint is attached to

b

The second of the two bodies constrained

error_bias

The rate at which joint error is corrected.

Defaults to pow(1.0 - 0.1, 60.0) meaning that it will correct 10% of the error every 1/60th of a second.

impulse

Get the last impulse applied by this constraint.

max_bias

The maximum rate at which joint error is corrected. Defaults to infinity

max_force

The maximum force that the constraint can use to act on the two bodies. Defaults to infinity

phase
ratio
class pymunk.constraint.GrooveJoint(a, b, groove_a, groove_b, anchr2)[source]

Bases: pymunk.constraint.Constraint

Similar to a pivot joint, but one of the anchors is on a linear slide instead of being fixed.

__init__(a, b, groove_a, groove_b, anchr2)[source]

The groove goes from groove_a to groove_b on body a, and the pivot is attached to anchr2 on body b. All coordinates are body local.

a

The first of the two bodies constrained

activate_bodies()

Activate the bodies this constraint is attached to

anchr2
b

The second of the two bodies constrained

error_bias

The rate at which joint error is corrected.

Defaults to pow(1.0 - 0.1, 60.0) meaning that it will correct 10% of the error every 1/60th of a second.

groove_a
groove_b
impulse

Get the last impulse applied by this constraint.

max_bias

The maximum rate at which joint error is corrected. Defaults to infinity

max_force

The maximum force that the constraint can use to act on the two bodies. Defaults to infinity

class pymunk.constraint.PinJoint(a, b, anchr1=(0, 0), anchr2=(0, 0))[source]

Bases: pymunk.constraint.Constraint

Keeps the anchor points at a set distance from one another.

__init__(a, b, anchr1=(0, 0), anchr2=(0, 0))[source]

a and b are the two bodies to connect, and anchr1 and anchr2 are the anchor points on those bodies.

a

The first of the two bodies constrained

activate_bodies()

Activate the bodies this constraint is attached to

anchr1
anchr2
b

The second of the two bodies constrained

distance
error_bias

The rate at which joint error is corrected.

Defaults to pow(1.0 - 0.1, 60.0) meaning that it will correct 10% of the error every 1/60th of a second.

impulse

Get the last impulse applied by this constraint.

max_bias

The maximum rate at which joint error is corrected. Defaults to infinity

max_force

The maximum force that the constraint can use to act on the two bodies. Defaults to infinity

class pymunk.constraint.PivotJoint(a, b, *args)[source]

Bases: pymunk.constraint.Constraint

Simply allow two objects to pivot about a single point.

__init__(a, b, *args)[source]

a and b are the two bodies to connect, and pivot is the point in world coordinates of the pivot. Because the pivot location is given in world coordinates, you must have the bodies moved into the correct positions already. Alternatively you can specify the joint based on a pair of anchor points, but make sure you have the bodies in the right place as the joint will fix itself as soon as you start simulating the space.

That is, either create the joint with PivotJoint(a, b, pivot) or PivotJoint(a, b, anchr1, anchr2).

a : Body
The first of the two bodies
b : Body
The second of the two bodies
args : [Vec2d] or [Vec2d,Vec2d]
Either one pivot point, or two anchor points
a

The first of the two bodies constrained

activate_bodies()

Activate the bodies this constraint is attached to

anchr1
anchr2
b

The second of the two bodies constrained

error_bias

The rate at which joint error is corrected.

Defaults to pow(1.0 - 0.1, 60.0) meaning that it will correct 10% of the error every 1/60th of a second.

impulse

Get the last impulse applied by this constraint.

max_bias

The maximum rate at which joint error is corrected. Defaults to infinity

max_force

The maximum force that the constraint can use to act on the two bodies. Defaults to infinity

class pymunk.constraint.RatchetJoint(a, b, phase, ratchet)[source]

Bases: pymunk.constraint.Constraint

Works like a socket wrench.

__init__(a, b, phase, ratchet)[source]

Works like a socket wrench. ratchet is the distance between “clicks”, phase is the initial offset to use when deciding where the ratchet angles are.

a

The first of the two bodies constrained

activate_bodies()

Activate the bodies this constraint is attached to

angle
b

The second of the two bodies constrained

error_bias

The rate at which joint error is corrected.

Defaults to pow(1.0 - 0.1, 60.0) meaning that it will correct 10% of the error every 1/60th of a second.

impulse

Get the last impulse applied by this constraint.

max_bias

The maximum rate at which joint error is corrected. Defaults to infinity

max_force

The maximum force that the constraint can use to act on the two bodies. Defaults to infinity

phase
ratchet
class pymunk.constraint.RotaryLimitJoint(a, b, min, max)[source]

Bases: pymunk.constraint.Constraint

Constrains the relative rotations of two bodies.

__init__(a, b, min, max)[source]

Constrains the relative rotations of two bodies. min and max are the angular limits in radians. It is implemented so that it’s possible to for the range to be greater than a full revolution.

a

The first of the two bodies constrained

activate_bodies()

Activate the bodies this constraint is attached to

b

The second of the two bodies constrained

error_bias

The rate at which joint error is corrected.

Defaults to pow(1.0 - 0.1, 60.0) meaning that it will correct 10% of the error every 1/60th of a second.

impulse

Get the last impulse applied by this constraint.

max
max_bias

The maximum rate at which joint error is corrected. Defaults to infinity

max_force

The maximum force that the constraint can use to act on the two bodies. Defaults to infinity

min
class pymunk.constraint.SimpleMotor(a, b, rate)[source]

Bases: pymunk.constraint.Constraint

Keeps the relative angular velocity of a pair of bodies constant.

__init__(a, b, rate)[source]

Keeps the relative angular velocity of a pair of bodies constant. rate is the desired relative angular velocity. You will usually want to set an force (torque) maximum for motors as otherwise they will be able to apply a nearly infinite torque to keep the bodies moving.

a

The first of the two bodies constrained

activate_bodies()

Activate the bodies this constraint is attached to

b

The second of the two bodies constrained

error_bias

The rate at which joint error is corrected.

Defaults to pow(1.0 - 0.1, 60.0) meaning that it will correct 10% of the error every 1/60th of a second.

impulse

Get the last impulse applied by this constraint.

max_bias

The maximum rate at which joint error is corrected. Defaults to infinity

max_force

The maximum force that the constraint can use to act on the two bodies. Defaults to infinity

rate

The desired relative angular velocity

class pymunk.constraint.SlideJoint(a, b, anchr1, anchr2, min, max)[source]

Bases: pymunk.constraint.Constraint

Like pin joints, but have a minimum and maximum distance. A chain could be modeled using this joint. It keeps the anchor points from getting to far apart, but will allow them to get closer together.

__init__(a, b, anchr1, anchr2, min, max)[source]

a and b are the two bodies to connect, anchr1 and anchr2 are the anchor points on those bodies, and min and max define the allowed distances of the anchor points.

a

The first of the two bodies constrained

activate_bodies()

Activate the bodies this constraint is attached to

anchr1
anchr2
b

The second of the two bodies constrained

error_bias

The rate at which joint error is corrected.

Defaults to pow(1.0 - 0.1, 60.0) meaning that it will correct 10% of the error every 1/60th of a second.

impulse

Get the last impulse applied by this constraint.

max
max_bias

The maximum rate at which joint error is corrected. Defaults to infinity

max_force

The maximum force that the constraint can use to act on the two bodies. Defaults to infinity

min