class

CP::Space

Inherits Reference / Object

Spaces are the basic unit of simulation. You add rigid bodies, shapes and joints to it and then step them all forward together through time.

Constructors

new(*, threaded : Bool = true)
Source

Instance methods

add(body : Body) : Body

Add a body to the simulation.

If this method is called during a simulation step, the addition will be delayed until the step is finished.

Returns the same Body, for convenience.

Source
add(shape : Shape) : Shape

Add a shape to the simulation.

If the collision shape is attached to a static body, it will be added as a static shape.

If this method is called during a simulation step, the addition will be delayed until the step is finished.

Returns the same Shape, for convenience.

Source
add(constraint : Constraint) : Constraint

Add a constraint to the simulation.

If this method is called during a simulation step, the addition will be delayed until the step is finished.

Returns the same Constraint, for convenience.

Source
add(*items : Shape | Body | Constraint)

Add multiple items

Source
add_collision_handler(a : Int, b : Int, handler : CollisionHandler) : CollisionHandler

Whenever shapes with collision types a and b collide, this handler will be used to process the collision events.

If wildcard handlers are used with either of the collision types, it's the responibility of the custom handler to invoke the wildcard handlers.

Source
add_collision_handler(type : Int, handler : CollisionHandler) : CollisionHandler

Set a wildcard collision handler for the specified type.

This handler will be used any time an object with this type collides with another object, regardless of its type.

Source
add_collision_handler(handler : CollisionHandler) : CollisionHandler

Set a collision handler that is called for all collisions that are not handled by a more specific collision handler.

Source
bb_query(bb : BB, filter : ShapeFilter = ShapeFilter::ALL, &block : Shape -> )

Perform a fast rectangle query on the space, yielding each shape found.

Only the shapes' bounding boxes are checked for overlap, not their full shape.

Source
bb_query(bb : BB, filter : ShapeFilter = ShapeFilter::ALL) : Array(Shape)

Perform a fast rectangle query on the space, yielding each shape found.

Only the shapes' bounding boxes are checked for overlap, not their full shape.

Source
bodies

Get all the bodies that are added to the space.

collision_bias

Determines how fast overlapping shapes are pushed apart.

Expressed as a fraction of the error remaining after each second. Defaults to (1.0 - 0.1)**60.0 meaning that Chipmunk fixes 10% of overlap each frame at 60Hz.

Source
collision_bias=(collision_bias : Number)
Source
collision_persistence

Number of frames that contact information should persist.

Defaults to 3. There is probably never a reason to change this value.

Source
collision_persistence=(collision_persistence : Timestamp)
Source
collision_slop

Amount of encouraged penetration between colliding shapes.

Used to reduce oscillating contacts and keep the collision cache warm. Defaults to 0.1. If you have poor simulation quality, increase this number as much as possible without allowing visible amounts of overlap.

Source
collision_slop=(collision_slop : Number)
Source
constraints

Get all the constraints that are added to the space.

contains?(body : Body) : Bool

Test if a body has been added to the space.

Source
contains?(shape : Shape) : Bool

Test if a shape has been added to the space.

Source
contains?(constraint : Constraint) : Bool

Test if a constraint has been added to the space.

Source
current_time_step

Returns the current (or most recent) time step used with the given space.

Useful from callbacks if your time step is not a compile-time global.

Source
damping

Damping rate expressed as the fraction of velocity bodies retain each second.

A value of 0.9 would mean that each body's velocity will drop 10% per second. The default value is 1.0, meaning no damping is applied.

Note: This damping value is different than those of DampedSpring and DampedRotarySpring.

Source
damping=(damping : Number)
Source
each_body

Yield each body in the space.

Source
each_constraint

Yield each constraint in the space.

Source
each_shape

Yield each shape in the space.

Source
gravity

Gravity to pass to rigid bodies when integrating velocity.

Defaults to (0,0).

Source
gravity=(gravity : Vect)
Source
idle_speed_threshold

Speed threshold for a body to be considered idle.

The default value of 0 means to let the space guess a good threshold based on gravity.

Source
idle_speed_threshold=(idle_speed_threshold : Number)
Source
iterations

Number of iterations to use in the impulse solver to solve contacts and other constraints.

Chipmunk uses an iterative solver to figure out the forces between objects in the space. What this means is that it builds a big list of all of the collisions, joints, and other constraints between the bodies and makes several passes over the list considering each one individually. The number of passes it makes is the iteration count, and each iteration makes the solution more accurate. If you use too many iterations, the physics should look nice and solid, but may use up too much CPU time. If you use too few iterations, the simulation may seem mushy or bouncy when the objects should be solid. Setting the number of iterations lets you balance between CPU usage and the accuracy of the physics. Chipmunk's default of 10 iterations is sufficient for most simple games.

Source
iterations=(iterations : Int)
Source
locked?

Returns true from inside a callback when objects cannot be added/removed.

Source
point_query(point : Vect, max_distance : Number = 0, filter : ShapeFilter = ShapeFilter::ALL, &block : PointQueryInfo -> )

Query the space at a point for shapes within the given distance range.

The filter is applied to the query and follows the same rules as the collision detection. Sensor shapes are included. If a max_distance of 0 is used, the point must lie inside a shape. Negative max_distance is also allowed meaning that the point must be a under a certain depth within a shape to be considered a match.

Source
point_query(point : Vect, max_distance : Number = 0, filter : ShapeFilter = ShapeFilter::ALL) : Array(PointQueryInfo)

Query the space at a point for shapes within the given distance range.

The filter is applied to the query and follows the same rules as the collision detection. Sensor shapes are included. If a max_distance of 0 is used, the point must lie inside a shape. Negative max_distance is also allowed meaning that the point must be a under a certain depth within a shape to be considered a match.

Source
point_query_nearest(point : Vect, max_distance : Number = 0, filter : ShapeFilter = ShapeFilter::ALL) : PointQueryInfo | Nil

Query the space at a point and return the nearest shape found.

Returns nil if no shapes were found.

Source
reindex(shape : Shape)

Update the collision detection data for a specific shape in the space.

Source
reindex_shapes_for(body : Body)

Update the collision detection data for all shapes attached to a body.

Source
reindex_static

Update the collision detection info for the static shapes in the space.

Source
remove(body : Body)

Remove a body from the simulation.

If this method is called during a simulation step, the removal will be delayed until the step is finished.

Source
remove(shape : Shape)

Remove a shape from the simulation.

If this method is called during a simulation step, the removal will be delayed until the step is finished.

Source
remove(constraint : Constraint)

Remove a constraint from the simulation.

If this method is called during a simulation step, the removal will be delayed until the step is finished.

Source
remove(*items : Shape | Body | Constraint)

Remove multiple items

Source
segment_query(start : Vect, end end_ : Vect, radius : Number = 0, filter : ShapeFilter = ShapeFilter::ALL, &block : SegmentQueryInfo -> )

Perform a directed line segment query (like a raycast) against the space and yield each shape intersected.

The filter is applied to the query and follows the same rules as the collision detection. Sensor shapes are included.

Source
segment_query(start : Vect, end end_ : Vect, radius : Number = 0, filter : ShapeFilter = ShapeFilter::ALL) : Array(SegmentQueryInfo)

Perform a directed line segment query (like a raycast) against the space and yield each shape intersected.

The filter is applied to the query and follows the same rules as the collision detection. Sensor shapes are included.

Source
segment_query_first(start : Vect, end end_ : Vect, radius : Number = 0, filter : ShapeFilter = ShapeFilter::ALL) : SegmentQueryInfo | Nil

Perform a directed line segment query (like a raycast) against the space and return the first shape hit.

Returns nil if no shapes were hit.

Source
shape_query(shape : Shape, &block : Shape, ContactPointSet -> )

Query a space for any shapes overlapping the given shape and yield each shape found.

Source
shape_query(shape : Shape) : Array(Shape)

Query a space for any shapes overlapping the given shape and yield each shape found.

Source
shapes

Get all the shapes that are added to the space.

sleep_time_threshold

Time a group of bodies must remain idle in order to fall asleep.

Enabling sleeping also implicitly enables the the contact graph. The default value of INFINITY disables the sleeping algorithm.

Source
sleep_time_threshold=(sleep_time_threshold : Number)
Source
static_body

The Space provided static body for a given Space.

This is merely provided for convenience and you are not required to use it.

Source
step(dt : Number)

Step the space forward in time by dt seconds.

Source
threads

Returns number of threads for multithreaded physics solver or 0 if the space isn't hasty

Source
threads=(nthreads : Int)

Set number of threads for multithreaded physics solver

Source
use_spatial_hash(dim : Number, count : Int)

Switch the space to use a spatial has as its spatial index.

Source

Nested types