Skip to content

Constraints

abstract class CP::Constraint
inherits Reference #

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.

Direct known subclasses

CP::Constraint::DampedRotarySpring CP::Constraint::DampedSpring CP::Constraint::GearJoint CP::Constraint::GrooveJoint CP::Constraint::PinJoint CP::Constraint::PivotJoint CP::Constraint::RatchetJoint CP::Constraint::RotaryLimitJoint CP::Constraint::SimpleMotor CP::Constraint::SlideJoint

Methods#

#bodies : ::Tuple(Body, Body)#

Get the bodies the constraint is attached to.

View source

#body_a : Body#

Get the first body the constraint is attached to.

View source

#body_b : Body#

Get the second body the constraint is attached to.

View source

#collide_bodies=(collide_bodies : Bool)#

View source

#collide_bodies? : Bool#

Are the two bodies connected by the constraint allowed to collide or not?

(defaults to false)

View source

#error_bias : Float64#

Rate at which joint error is corrected.

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

View source

#error_bias=(error_bias : Number)#

View source

#impulse : Float64#

Get the most recent impulse applied by this constraint.

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.

View source

#max_bias : Float64#

The maximum rate at which joint error is corrected.

(defaults to INFINITY)

View source

#max_bias=(max_bias : Number)#

View source

#max_force : Float64#

The maximum force that this constraint is allowed to use.

(defaults to INFINITY)

View source

#max_force=(max_force : Number)#

View source

#post_solve(space : Space)#

The post-solve method that is called before the solver runs (can be overridden in a subclass).

View source

#pre_solve(space : Space)#

The pre-solve method that is called before the solver runs (can be overridden in a subclass).

View source

#space : Space | ::Nil#

Get the Space this constraint is added to.

View source

class CP::Constraint::DampedRotarySpring
inherits CP::Constraint #

Like a damped spring, but works in an angular fashion

Constructors#

.new(a : Body, b : Body, rest_angle : Number, stiffness : Number, damping : Number)#

View source

Methods#

#damping : Float64#

How soft to make the damping of the spring.

View source

#damping=(damping : Number)#

View source

#rest_angle : Float64#

The relative angle in radians that the bodies want to have

View source

#rest_angle=(rest_angle : Number)#

View source

#spring_torque(relative_angle : Float64) : Number#

(can be overridden in a subclass)

View source

#stiffness : Float64#

The stiffness of the spring in force/distance.

View source

#stiffness=(stiffness : Number)#

View source

class CP::Constraint::DampedSpring
inherits CP::Constraint #

Constructors#

.new(a : Body, b : Body, anchor_a : Vect, anchor_b : Vect, rest_length : Number, stiffness : Number, damping : Number)#

Defined much like a slide joint.

  • anchor_a: Anchor point a, relative to body a
  • anchor_b: Anchor point b, relative to body b
  • rest_length: The distance the spring wants to be at
  • stiffness: The spring constant (Young's modulus)
  • damping: How soft to make the damping of the spring
View source

Methods#

#anchor_a : Vect#

The location of the first anchor relative to the first body.

View source

#anchor_a=(anchor_a : Vect)#

View source

#anchor_b : Vect#

The location of the second anchor relative to the second body.

View source

#anchor_b=(anchor_b : Vect)#

View source

#damping : Float64#

How soft to make the damping of the spring.

View source

#damping=(damping : Number)#

View source

#rest_length : Float64#

The distance the spring wants to be at.

View source

#rest_length=(rest_length : Number)#

View source

#spring_force(dist : Float64) : Number#

(can be overridden in a subclass)

View source

#stiffness : Float64#

The stiffness of the spring in force/distance.

View source

#stiffness=(stiffness : Number)#

View source

class CP::Constraint::GearJoint
inherits CP::Constraint #

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

Constructors#

.new(a : Body, b : Body, phase : Number, ratio : Number)#

View source

Methods#

#phase : Float64#

The phase offset of the gears.

View source

#phase=(phase : Number)#

View source

#ratio : Float64#

The angular distance of each ratchet.

View source

#ratio=(ratio : Number)#

View source

class CP::Constraint::GrooveJoint
inherits CP::Constraint #

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

Constructors#

.new(a : Body, b : Body, groove_a : Vect, groove_b : Vect, anchor_b : Vect)#

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.

View source

Methods#

#anchor_b : Vect#

The location of the second anchor relative to the second body.

View source

#anchor_b=(anchor_b : Vect)#

View source

#groove_a : Vect#

The first endpoint of the groove relative to the first body.

View source

#groove_a=(groove_a : Vect)#

View source

#groove_b : Vect#

The second endpoint of the groove relative to the second body.

View source

#groove_b=(groove_b : Vect)#

View source

class CP::Constraint::PinJoint
inherits CP::Constraint #

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

Constructors#

.new(a : Body, b : Body, anchor_a : Vect, anchor_b : Vect)#

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

View source

Methods#

#anchor_a : Vect#

The location of the first anchor relative to the first body.

View source

#anchor_a=(anchor_a : Vect)#

View source

#anchor_b : Vect#

The location of the second anchor relative to the second body.

View source

#anchor_b=(anchor_b : Vect)#

View source

#dist : Float64#

The distance the joint will maintain between the two anchors.

View source

#dist=(dist : Number)#

View source

class CP::Constraint::PivotJoint
inherits CP::Constraint #

Allows two objects to pivot about a single point.

Constructors#

.new(a : Body, b : Body, anchor_a : Vect, anchor_b : Vect)#

a and b are the two bodies to connect, and anchor_a and anchor_b are the points in local coordinates where the pivot is located.

View source

.new(a : Body, b : Body, pivot : Vect) : self#

a and b are the two bodies to connect, and pivot is the point in world coordinates of the pivot.

View source

Methods#

#anchor_a : Vect#

The location of the first anchor relative to the first body.

View source

#anchor_a=(anchor_a : Vect)#

View source

#anchor_b : Vect#

The location of the second anchor relative to the second body.

View source

#anchor_b=(anchor_b : Vect)#

View source

class CP::Constraint::RatchetJoint
inherits CP::Constraint #

Works like a socket wrench.

Constructors#

.new(a : Body, b : Body, phase : Number, ratchet : Number)#

ratchet is the distance between "clicks", phase is the initial offset to use when deciding where the ratchet angles are.

View source

Methods#

#angle : Float64#

The angle of the current ratchet tooth.

View source

#angle=(angle : Number)#

View source

#phase : Float64#

The phase offset of the ratchet.

View source

#phase=(phase : Number)#

View source

#ratchet : Float64#

The angular distance of each ratchet.

View source

#ratchet=(ratchet : Number)#

View source

class CP::Constraint::RotaryLimitJoint
inherits CP::Constraint #

Constrains the relative rotations of two bodies.

Constructors#

.new(a : Body, b : Body, min : Number, max : Number)#

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.

View source

Methods#

#max : Float64#

The maximum distance the joint will maintain between the two anchors.

View source

#max=(max : Number)#

View source

#min : Float64#

The minimum distance the joint will maintain between the two anchors.

View source

#min=(min : Number)#

View source

class CP::Constraint::SimpleMotor
inherits CP::Constraint #

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

Constructors#

.new(a : Body, b : Body, rate : Number)#

View source

Methods#

#rate : Float64#

The desired relative angular velocity of the motor.

You will usually want to set a force (torque) maximum for motors as otherwise they will be able to apply a nearly infinite torque to keep the bodies moving.

View source

#rate=(rate : Number)#

View source

class CP::Constraint::SlideJoint
inherits CP::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 too far apart, but will allow them to get closer together.

Constructors#

.new(a : Body, b : Body, anchor_a : Vect, anchor_b : Vect, min : Number, max : Number)#

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.

View source

Methods#

#anchor_a : Vect#

The location of the first anchor relative to the first body.

View source

#anchor_a=(anchor_a : Vect)#

View source

#anchor_b : Vect#

The location of the second anchor relative to the second body.

View source

#anchor_b=(anchor_b : Vect)#

View source

#max : Float64#

The maximum distance the joint will maintain between the two anchors.

View source

#max=(max : Number)#

View source

#min : Float64#

The minimum distance the joint will maintain between the two anchors.

View source

#min=(min : Number)#

View source