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

Example:

>>> import pymunk
>>> s = pymunk.Space()
>>> a,b = pymunk.Body(10,10), pymunk.Body(10,10)
>>> c = pymunk.PivotJoint(a, b, (0,0))
>>> s.add(c)
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

collide_bodies

Constraints can be used for filtering collisions too.

When two bodies collide, Pymunk ignores the collisions if this property is set to False on any constraint that connects the two bodies. Defaults to True. This can be used to create a chain that self collides, but adjacent links in the chain do not collide.

error_bias

The percentage of joint error that remains unfixed after a second.

This works exactly the same as the collision bias property of a space, but applies to fixing error (stretching) of joints instead of overlapping collisions.

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

The most recent impulse that constraint applied.

To convert this to a force, divide by the timestep passed to space.step(). You can use this to implement breakable joints to check if the force they attempted to apply exceeded a certain threshold.

max_bias

The maximum speed at which the constraint can apply error correction.

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:
  • a (Body) – Body a
  • b (Body) – Body b
  • rest_angle (float) – The relative angle in radians that the bodies want to have
  • 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

b

The second of the two bodies constrained

collide_bodies

Constraints can be used for filtering collisions too.

When two bodies collide, Pymunk ignores the collisions if this property is set to False on any constraint that connects the two bodies. Defaults to True. This can be used to create a chain that self collides, but adjacent links in the chain do not collide.

damping

How soft to make the damping of the spring.

error_bias

The percentage of joint error that remains unfixed after a second.

This works exactly the same as the collision bias property of a space, but applies to fixing error (stretching) of joints instead of overlapping collisions.

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

The most recent impulse that constraint applied.

To convert this to a force, divide by the timestep passed to space.step(). You can use this to implement breakable joints to check if the force they attempted to apply exceeded a certain threshold.

max_bias

The maximum speed at which the constraint can apply error correction.

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).

class pymunk.constraint.DampedSpring(a, b, anchor_a, anchor_b, rest_length, stiffness, damping)[source]

Bases: pymunk.constraint.Constraint

A damped spring

__init__(a, b, anchor_a, anchor_b, rest_length, stiffness, damping)[source]

Defined much like a slide joint.

Parameters:
  • a (Body) – Body a
  • b (Body) – Body b
  • anchor_a ((float,float)) – Anchor point a, relative to body a
  • anchor_b ((float,float)) – Anchor point b, 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

anchor_a
anchor_b
b

The second of the two bodies constrained

collide_bodies

Constraints can be used for filtering collisions too.

When two bodies collide, Pymunk ignores the collisions if this property is set to False on any constraint that connects the two bodies. Defaults to True. This can be used to create a chain that self collides, but adjacent links in the chain do not collide.

damping

How soft to make the damping of the spring.

error_bias

The percentage of joint error that remains unfixed after a second.

This works exactly the same as the collision bias property of a space, but applies to fixing error (stretching) of joints instead of overlapping collisions.

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

The most recent impulse that constraint applied.

To convert this to a force, divide by the timestep passed to space.step(). You can use this to implement breakable joints to check if the force they attempted to apply exceeded a certain threshold.

max_bias

The maximum speed at which the constraint can apply error correction.

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

collide_bodies

Constraints can be used for filtering collisions too.

When two bodies collide, Pymunk ignores the collisions if this property is set to False on any constraint that connects the two bodies. Defaults to True. This can be used to create a chain that self collides, but adjacent links in the chain do not collide.

error_bias

The percentage of joint error that remains unfixed after a second.

This works exactly the same as the collision bias property of a space, but applies to fixing error (stretching) of joints instead of overlapping collisions.

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

The most recent impulse that constraint applied.

To convert this to a force, divide by the timestep passed to space.step(). You can use this to implement breakable joints to check if the force they attempted to apply exceeded a certain threshold.

max_bias

The maximum speed at which the constraint can apply error correction.

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, anchor_b)[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, anchor_b)[source]

The groove goes from groove_a to groove_b on body a, and the pivot is attached to anchor_b 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

anchor_b
b

The second of the two bodies constrained

collide_bodies

Constraints can be used for filtering collisions too.

When two bodies collide, Pymunk ignores the collisions if this property is set to False on any constraint that connects the two bodies. Defaults to True. This can be used to create a chain that self collides, but adjacent links in the chain do not collide.

error_bias

The percentage of joint error that remains unfixed after a second.

This works exactly the same as the collision bias property of a space, but applies to fixing error (stretching) of joints instead of overlapping collisions.

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

The most recent impulse that constraint applied.

To convert this to a force, divide by the timestep passed to space.step(). You can use this to implement breakable joints to check if the force they attempted to apply exceeded a certain threshold.

max_bias

The maximum speed at which the constraint can apply error correction.

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, anchor_a=(0, 0), anchor_b=(0, 0))[source]

Bases: pymunk.constraint.Constraint

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

__init__(a, b, anchor_a=(0, 0), anchor_b=(0, 0))[source]

a and b are the two bodies to connect, and anchor_a and anchor_b are the anchor points on those bodies.

The distance between the two anchor points is measured when the joint is created. If you want to set a specific distance, use the setter function to override it.

a

The first of the two bodies constrained

activate_bodies()

Activate the bodies this constraint is attached to

anchor_a
anchor_b
b

The second of the two bodies constrained

collide_bodies

Constraints can be used for filtering collisions too.

When two bodies collide, Pymunk ignores the collisions if this property is set to False on any constraint that connects the two bodies. Defaults to True. This can be used to create a chain that self collides, but adjacent links in the chain do not collide.

distance
error_bias

The percentage of joint error that remains unfixed after a second.

This works exactly the same as the collision bias property of a space, but applies to fixing error (stretching) of joints instead of overlapping collisions.

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

The most recent impulse that constraint applied.

To convert this to a force, divide by the timestep passed to space.step(). You can use this to implement breakable joints to check if the force they attempted to apply exceeded a certain threshold.

max_bias

The maximum speed at which the constraint can apply error correction.

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, anchor_a, anchor_b).

Parameters:
  • a (Body) – The first of the two bodies
  • b (Body) – The second of the two bodies
  • args ((float,float) or (float,float) (float,float)) – 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

anchor_a
anchor_b
b

The second of the two bodies constrained

collide_bodies

Constraints can be used for filtering collisions too.

When two bodies collide, Pymunk ignores the collisions if this property is set to False on any constraint that connects the two bodies. Defaults to True. This can be used to create a chain that self collides, but adjacent links in the chain do not collide.

error_bias

The percentage of joint error that remains unfixed after a second.

This works exactly the same as the collision bias property of a space, but applies to fixing error (stretching) of joints instead of overlapping collisions.

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

The most recent impulse that constraint applied.

To convert this to a force, divide by the timestep passed to space.step(). You can use this to implement breakable joints to check if the force they attempted to apply exceeded a certain threshold.

max_bias

The maximum speed at which the constraint can apply error correction.

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

collide_bodies

Constraints can be used for filtering collisions too.

When two bodies collide, Pymunk ignores the collisions if this property is set to False on any constraint that connects the two bodies. Defaults to True. This can be used to create a chain that self collides, but adjacent links in the chain do not collide.

error_bias

The percentage of joint error that remains unfixed after a second.

This works exactly the same as the collision bias property of a space, but applies to fixing error (stretching) of joints instead of overlapping collisions.

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

The most recent impulse that constraint applied.

To convert this to a force, divide by the timestep passed to space.step(). You can use this to implement breakable joints to check if the force they attempted to apply exceeded a certain threshold.

max_bias

The maximum speed at which the constraint can apply error correction.

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

collide_bodies

Constraints can be used for filtering collisions too.

When two bodies collide, Pymunk ignores the collisions if this property is set to False on any constraint that connects the two bodies. Defaults to True. This can be used to create a chain that self collides, but adjacent links in the chain do not collide.

error_bias

The percentage of joint error that remains unfixed after a second.

This works exactly the same as the collision bias property of a space, but applies to fixing error (stretching) of joints instead of overlapping collisions.

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

The most recent impulse that constraint applied.

To convert this to a force, divide by the timestep passed to space.step(). You can use this to implement breakable joints to check if the force they attempted to apply exceeded a certain threshold.

max
max_bias

The maximum speed at which the constraint can apply error correction.

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

collide_bodies

Constraints can be used for filtering collisions too.

When two bodies collide, Pymunk ignores the collisions if this property is set to False on any constraint that connects the two bodies. Defaults to True. This can be used to create a chain that self collides, but adjacent links in the chain do not collide.

error_bias

The percentage of joint error that remains unfixed after a second.

This works exactly the same as the collision bias property of a space, but applies to fixing error (stretching) of joints instead of overlapping collisions.

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

The most recent impulse that constraint applied.

To convert this to a force, divide by the timestep passed to space.step(). You can use this to implement breakable joints to check if the force they attempted to apply exceeded a certain threshold.

max_bias

The maximum speed at which the constraint can apply error correction.

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, anchor_a, anchor_b, 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, anchor_a, anchor_b, min, max)[source]

a and b are the two bodies to connect, anchor_a and anchor_b 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

anchor_a
anchor_b
b

The second of the two bodies constrained

collide_bodies

Constraints can be used for filtering collisions too.

When two bodies collide, Pymunk ignores the collisions if this property is set to False on any constraint that connects the two bodies. Defaults to True. This can be used to create a chain that self collides, but adjacent links in the chain do not collide.

error_bias

The percentage of joint error that remains unfixed after a second.

This works exactly the same as the collision bias property of a space, but applies to fixing error (stretching) of joints instead of overlapping collisions.

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

The most recent impulse that constraint applied.

To convert this to a force, divide by the timestep passed to space.step(). You can use this to implement breakable joints to check if the force they attempted to apply exceeded a certain threshold.

max
max_bias

The maximum speed at which the constraint can apply error correction.

Defaults to infinity

max_force

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

Defaults to infinity

min