There are 4 basic objects you will use in Chipmunk.
There is often confusion between rigid bodies and their collision shapes in Chipmunk and how they relate to sprites. A sprite would be a visual representation of an object, while a collision shape is an invisible property that defines how objects should collide. Both the sprite’s and the collision shape’s position and rotation are controlled by a rigid body.
require 'chipmunk'
Ruby is garbage collected, so normallly you don't have to worry about this.
One important exception are objects of the Arbiter
class, which
may only be referenced inside a collision callback. Any data inside the Arbiter
has to be copied out, and no reference to the Arbiter should be held, since
Chipmunk erases the arbiters after calling the collision callback.
Most methods will raise a Ruby exception when used incorrectly. Otherwise, methods that may fail to do what they should do, will return self on success and nil on failure.
Some methods in these wrappers, if used incorrectly, may crash Ruby. This occurs especially when you're using these methods in ways that Chipmunk does not allow. The underlying C library will sometimes throw an assertion, which may crash the bindings and Ruby with it. Yes, I know this is annoying, but I hope to reduce the occurrence of such problems in later versions.
These bindings commonly use Ruby Float for calculations.
There are a few functions in the CP module you will probably find very useful:
CP.clamp(f, min, max)
f
to be between min
and max
.CP.flerp(f1, f2, t)
f1
and f2
.CP.flerpconst(f1, f2, d)
f1
towards f2
by no more than d
.Floating point infinity is defined for you as CP::INFINITY
,
and also as Float::INFINITY
if your version of Ruby doesn’t define
it for you.
To represent vectors, Chipmunk defines the Vec2 type and a set of operators for working with them.
CP::Vec2
CP::Vec2
CP::Vec2.new(x, y)
x
and y
coordinates.vec2(x, y)
x
and y
coordinates.CP::Vec2.for_angle?(a)
a
with the x axis.
CP::Vec2::ZERO
CP::Vec2#x
CP::Vec2#y
CP::Vec2#==(vec)
CP::Vec2#+(vec)
CP::Vec22#-(vec)
CP::Vec2#@-
CP::Vec22#@+
CP::Vec2#*(scalar)
CP::Vec22#/(scalar)
CP::Vec2#dot(vect)
CP::Vec2#cross(vect)
CP::Vec2#perp
CP::Vec2#nperp
CP::Vec2#project(vec)
self
onto vec
.CP::Vec2#rotate(vec)
self
by vec
. Scaling will occur if self
is not a unit vector.CP::Vec2#unrotate(vec)
CP::Vec2#rotate(vec)
CP::Vec2#length()
self
.CP::Vec2#lengthsq()
self
. Faster than Vec2#length
when you only need to compare lengths.CP::Vec2#lerp(vec, t)
self
and vec
.CP::Vec2#lerpconst(vec, d)
self
towards vec
by
distance d
.CP::Vec2#slerp(vec, t)
self
and vec
.CP::Vec2#slerpconst(vec, a)
self
towards vec
by no more than angle a
in radians.CP::Vec2#normalize
self
.CP::Vec2#normalize_safe
self
. Protects against
divide by zero errors. Returns CP::Vec2::ZERO
if there vector was already zero.CP::Vec2#clamp(len)
self
to length len
.CP::Vec2#dist(vec)
self
and vec
.CP::Vec2#distsq(vec)
self
and vec
. Faster than Vec2#dist(vec)
when you only need to compare distances.CP::Vec2#near?(vec, dist)
self
and vec
is less than dist
.CP::Vec2#to_angle
self
is pointing in (in radians).CP::Vec2#to_s
self
. Intended mostly for debugging purposes.class CP::BB
class CP::BB
BB
classes. CP::BB#l, CP::BB#b, CP::BB#r, CP::BB#t
CP::BB#intersect(other)
CP::BB#contain_bb?(other)
self
completely contains other
.CP::BB#contain_vect?(vect)
self
contains vec
.CP::BB#contain?(other)
self
contains other
.
Convenience method that works for both CP::Vec2 and CP::BB.
CP::BB#merge(other)
self
and other
.CP::BB#expand(vec)
self
and vec
.CP::BB#clamp_vect(vec)
vec
clamped to the bounding box.CP::BB#wrap_vect(vec)
vec
wrapped to the bounding box.CP::Body
CP::Body#velocity_func() { |body, gravity, damping, dt| ... }
CP::Body#position_func() { |body,dt| ... }
m
– Float
: Mass of the body.i
– Float
: Moment of inertia (MoI or
sometimes just moment) of the body. The moment is like the rotational
mass of a body. See below for function to help calculate the moment.p
– CP::Vec2
: Position of the body.v
– CP::Vec2
: Velocity of the body.f
– CP::Vec2
: Current force being applied to the body. Note: does not reset automatically as in some physics engines.a
– Float
: Current rotation angle of the body in radians.w
– Float
: Current rotational velocity of the body.t
– Float
: Current torque being applied to the body. Note: does not reset automatically as in some physics engines.rot
– CP::Vec2
: Cached unit length rotation vector.v_limit
– Float
: Maximum speed a body may have after updating it’s velocity.w_limit
– Float
: Maximum rotational speed a body may have after updating it’s velocity.data
– Object
: The Ruby bindings use this field internally, so it is not available for you use. It returns self
, but that is not guaranteed.object
– Object
: A user definable
data object. If you set this to point at the game object the shapes is
for, then you can access your game object from Chipmunk callbacks.When changing any of a body’s properties, you should also call CP::Body#activate()
to make sure that it is not stuck sleeping when you’ve changed a property that should make it move again.
CP::Body.new(m, i)
m
and i
are the mass and moment of inertia for the body. Guessing the mass for a
body is usually fine, but guessing a moment of inertia can lead to a
very poor simulation.CP::Body.new_static()
CP::_static_body.new()
Creates static bodies with infinite mass and moment of inertia.
While every CP::Space
has a built in static body, this is not
usable from the Ruby API. With this constructor you can make your own static bodies. One potential use is in a level editor. By attaching chunks of your
level to static bodies, you can still move and rotate the chunks independently
of each other. Then all you have to do is call CP::Space.rehash_static()
to rebuild the static collision detection data when you are done.
For more information on rogue and static bodies, see Chipmunk Spaces.
Use the following functions to approximate the moment of inertia for your body, adding the results together if you want to use more than one.
CP.moment_for_circle(m, r1, r2, offset)
r1
and r2
are the inner and outer diameters in no particular order. (A solid circle has an inner diameter of 0)CP.moment_for_segment(m, a, b)
a
and b
are relative to the body.CP.moment_for_poly(m, verts, offset)
CP.moment_for_box(m, width, height)
Use the following functions to get the area for common Chipmunk shapes if you want to approximate masses.
CP.area_for_circle(r1, r2)
CP.area_for_segment(a, b, r)
CP.area_for_poly(verts)
Because several rigid body values are linked to others, (m_inv
, i_inv
, rot
), they cannot be set explicitly.
void CP::Body#slew(pos, dt)
void CP::Body#update_velocity(body, gravity, damping, dt)
void CP::Body#update_position(body, dt)
CP::Body#local2world(vec)
CP::Body#world2local(body, vex)
CP::Body#apply_impulse(j, r)
j
to body
at a relative offset r
from the center of gravity. Both r
and j
are in world coordinates. r
is relative to the position of the body, but not the rotation. Many people get tripped up by this.CP::Body#reset_forces
self
.CP::Body#apply_force(f, r)
f
on body
at a relative offset (important!) r
from the center of gravity. Both r
and f
are in world coordinates.See Chipmunk Spaces CP::Space for more information on Chipmunk’s sleeping feature.
CP::Body#sleeping?
body
is sleeping, false if not.CP::Body#sleep_with_group(group)
When objects in Chipmunk sleep, they sleep as a group of
all objects that are touching or jointed together. When an object is
woken up, all of the objects in it’s group are woken up. Calling
CP::Body#sleep_with_group(group) forces a body to fall asleep immediately. If group
is nil
,
a new group will be created. If group is another sleeping Body, it will
be added to that body’s group. It is an error to specify a non-sleeping
Body for group
. Make sure everything is set up before
calling this method. Calling a setter function or adding/removing a
shape or constraint will cause the body to be woken up again. Also, this
function must not be called from a collision handler or query callback.
Use a post-step callback instead.
An example of how this could be used is to set up a piles of boxes that a player can knock over. Creating the piles and letting them fall asleep normally would work, but it means that all of the boxes would need to be simulated until they fell asleep. This could be slow if you had a lot of piles. Instead you can force them to sleep and start the game with the boxes already sleeping.
Another example would be collapsing platforms. You could create a sleeping object that is suspended in the air. As soon as the player comes along to jump on it, it wakes up and starts falling. In this case, it would be impossible to have the platform fall asleep naturally.
CP::Body#sleep_alone
CP::Body#sleep_with_group(nil)
.
It forces body
to fall asleep and creates a new group for it.
The name sleep_alone
was chosen because sleep
is part
of Ruby's Kernel module...
void CP::Body#activate
self
up so that it starts actively
simulating again if it’s sleeping, or reset the idle timer if it’s
active. In Chipmunk 5.3.4 and up, you can call this function from a
collision or query callback. In previous versions this was an error.CP::Body#static?
self
is a static body. (CP::_static_body or a
static rogue body)CP::Body#rogue?()
self
has never been added
to a space. Though shapes attached to this body may still be added to a
space. For more information on rogue and static bodies, see Chipmunk Spaces.CP::Bool CP::Body#sleeping?()
self
is sleeping.CP::Shape
There are currently 3 collision shape types:
You can add multiple shapes to a body. This should give you the flexibility to make any shape you want as well providing different areas of the same object with different friction, elasticity or callback values.
All collision shapes include the CP::Shape module. Chipmunk shapes are meant to be opaque types.
body
– CP::Body
: The rigid body the shape is attached to.bb_raw
– CP::BB
: The bounding box of the shape. Only guaranteed to be valid after CP::Shape#bb()
or CP::Space#step()
is called. Moving a body that a shape is connected to does not update it’s bounding box.sensor
– true or false
: A Boolean value if this shape is a sensor or not. Sensors only call collision callbacks, and never generate real collisions.e
– Float
: Elasticity of the shape. A
value of 0.0 gives no bounce, while a value of 1.0 will give a “perfect”
bounce. However due to inaccuracies in the simulation using 1.0 or
greater is not recommended however. See the notes at the end of the section.u
– Float
: Friction coefficient. Chipmunk uses the Coulomb friction model, a value of 0.0 is frictionless. Tables of friction coefficients. See the notes at the end of the section.surface_v
– CP::Vec2
: The surface velocity
of the object. Useful for creating conveyor belts or players that move
around. This value is only used when calculating friction, not resolving
the collision.collision_type
– Symbol
: You can assign symbols as collision types types to Chipmunk collision shapes that trigger callbacks when objects of certain types touch. See the callbacks section or an example for more information.group
– Fixnum
: Shapes in the
same non-zero group do not generate collisions. Useful when creating an
object out of many shapes that you don’t want to self collide. Defaults
to CP::NO_GROUP
.layers
– Fixnum
: Shapes only collide if they are in the same bit-planes. i.e. (a->layers & b->layers) != 0
By default, a shape occupies all bit-planes. Wikipedia has a nice article on bit masks if you are unfamiliar with how to use them. Defaults to CP::ALL_LAYERS
.data
– Object
: The Ruby bindings use this field internally, so it is not available for you use. It returns self
, but that is not guaranteed.object
– Object
: A user definable
data object. If you set this to point at the game object the shapes is
for, then you can access your game object from Chipmunk callbacks.Chipmunk has two primary means of ignoring collisions: groups and layers.
Groups are meant to ignore collisions between parts on a complex object. A rag doll is a good example. When jointing an arm onto the torso, you’ll want them to allow them to overlap. Groups allow you to do exactly that. Shapes that have the same group don’t generate collisions. So by placing all of the shapes in a rag doll in the same group, you’ll prevent it from colliding against other parts of itself.
Layers allow you to separate collision shapes into mutually exclusive planes. Shapes can be in more than one layer, and shapes only collide with other shapes that are in at least one of the same layers. As a simple example, say shape A is in layer 1, shape B is in layer 2, and shape C is in layer 1 and 2. Shape A and B won’t collide with each other, but shape C will collide with both A and B.
Layers can also be used to set up rule based collisions. Say you have four types of shapes in your game. The player, the enemies, player bullets and enemy bullets. The are that the player should collide with enemies, and bullets shouldn’t collide with the type (player or enemy) that fired them. Making a chart would look like this:
Player | Enemy | Player Bullet | Enemy Bullet | |
Player | – | (1) | (2) | |
Enemy | – | – | (3) | |
Player Bullet | – | – | – | |
Enemy Bullet | – | – | – | – |
The ‘-’s are for redundant spots in the chart, and the numbers are
spots where types should collide. You can use a layer for rule that you
want to define. Then add the layers to each type: The player should be
in layers 1 and 2, the enemy should be in layers 1 and 3, the player
bullets should be in layer 3, and the enemy bullets should be in layer
2. Treating layers as rules this way, you can define up to 32 rules. The
default layer
type has a resolution of 32 bits on most systems.
There is one last way of filtering collisions using collision handlers. See the section on callbacks for more information. While collision handlers can be more flexible, they are also the slowest method. So you try to use groups or layers first.
CP::Shape#cache_bb()
, CP::Shape#bb()
shape
and returns it.void CP::Shape.reset_id_counter
CP::Shape::Circle.new(body, radius, offset)
body
is the body to attach the circle to, offset
is the offset from the body’s center of gravity in body local coordinates.
Note: Due to a bug in the upstream Chipmunk 5.3.4, calling
CP::Shape::Circle.new
, or any of the CP::Shape::XXX.new
constructors with incorrect arguments will crash your program.
This will be fixed in a later version of Chipmunk.
CP::Shape::Circle#offset, CP::Shape::Circle#radius
CP::Shape::Segment.new(body, veca, vecb, radius)
body
is the body to attach the segment to, veca
and vecb
are the endpoints, and radius
is the thickness of the segment.CP::Shape::Segment#a(CP::Shape *shape), CP::Shape::Segment#b(CP::Shape *shape), CP::Shape::Segment#normal(CP::Shape *shape), CP::Shape::Segment#radius(CP::Shape *shape)
CP::Shape::Poly.valid?(verts)
verts
, which should be an array of CP::Vec2 points,
will form a valid, closed polygon array. Useful in conjunction with
CP::Shape::Poly.new
CP::Shape::Poly.new(body, verts, offset)
body
is the body to attach the poly to, verts
is an array of CP::Vec2
objects defining a convex hull with a clockwise winding, offset
is the offset from the body’s center of gravity in body local
coordinates. An exception will be thrown the vertexes are not convex or
do not have a clockwise winding. Note: Due to an upstream bug,
using incorrect verts
will cause a crash.CP::Shape::Poly.num_verts, CP::Shape::Poly.vert(index)
CP::centroid_for_poly(verts) TODO!
void CP::recenter_poly(verts)
The short answer is that you can’t because the changes would be only picked up as a change to the position of the shape’s surface. The long answer is that with the following functions, since version 5.3.4.1 of the bindings, you can, but the results will probably not be what you expect.
The following operations are "unsafe", because they may reduce the physical accuracy or numerical stability of the simulation, but will not cause crashes. The prime example is mutating collision shapes. Chipmunk does not support this directly. Mutating shapes using this API will caused objects in contact to be pushed apart using Chipmunk's overlap solver, but not using real persistent velocities. Probably not what you meant, but perhaps close enough.
CP::Shape::Circle#set_radius!(r)
r
.CP::Shape::Circle#set_offset!(offset)
offset
.CP::Shape::Segment#set_radius!(r)
r
.CP::Shape::Segment#set_endpoints!(veca, vecb)
veca
and
vecb
.CP::Shape::Poly#set_verts!(verts, offset)
verts
is
an Array of CP::Vec2
objects defining a convex hull with a
clockwise winding, offset
is the offset from the body’s center
of gravity in body local coordinates. An ArgumentError will be raised if the
vertexes are not convex or do not have a clockwise winding.CP::Space
Spaces in Chipmunk are the basic unit of simulation. You add rigid bodies, shapes and constraints to it and then step them forward through time.
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.
Rogue bodies are bodies that have not been added to the space, but are referenced from shapes or joints. Rogue bodies are a common way of controlling moving elements in Chipmunk such as platforms. As long as the body’s velocity matches the changes to it’s position, there is no problem with doing this. Most games will not need rogue bodies however.
In previous versions, Chipmunk used infinite mass rogue bodies to attach static shapes to. Creating and maintaining your own body for this is no longer necessary in C as each space has it’s own body for attaching static shapes to.
This body is marked as being a static body. A static body allows other objects resting on or jointed to it to fall asleep even though it does not fall asleep itself.
In Ruby, however, it might be a good idea to use your own static shapes,
as the built-in static Shape of the Space is not so useful. Additional static
bodies can be created using CP::StaticBody.new()
or CP::Body.new_static()
.
Objects resting on or jointed to rogue bodies can never fall asleep.
This is because the purpose of rogue bodies are to allow the programmer
to move or modify them at any time. Without knowing when they will move,
Chipmunk has to assume that objects cannot fall asleep when touching
them.
Chipmunk optimizes collision detection against shapes that do not move. There are two ways that Chipmunk knows a shape is static.
The first is automatic. When adding or removing shapes attached to
static bodies, Chipmunk knows to handle them specially and you just use
the same functions as adding regular dynamic shapes, CP::Space.add_shape()
and CP::Space.remove_shape()
.
The second way is to explicitly tag shapes as static using
CP::Space.add_static_shape()
.
This is rarely needed, but say for instance that you have a rogue body
that seldomly moves. In this case, adding a shape attached to it as a
static shape and updating it only when the body moves can increase
performance. Shapes added this way must be removed by calling
CP::Space.remove_static_shape()
.
Because static shapes aren’t updated automatically, you must let
Chipmunk know that it needs to update the static shapes. You can update
all of the static shapes at once using CP::Space#rehash_static()
or individual shapes using CP::Space#rehash_shape
.
If you find yourself rehashing static bodies often, you should be adding them
normally using CP::Space#add_shape
instead.
New in Chipmunk 5.3 is the ability of spaces to disable entire groups of
objects that have stopped moving to save CPU
time as well as battery life. In order to use this feature you must do 2
things. The first is that you must attach all your static geometry to
static bodies. Objects cannot fall asleep if they are touching a
non-static rogue body even if it’s shapes were added as static shapes.
The second is that you must enable sleeping explicitly by choosing a
time threshold value for CP::Space#sleep_time_threshold
. If you do not set CP::Space#idle_speed_threshold
explicitly, a value will be chosen automatically based on the current amount of gravity.
iterations
– Fixnum
: Allow you to control the accuracy of the solver. Defaults to 10. See the section on iterations above for an explanation.gravity
– CP::Vec2
: Global gravity applied to the space. Defaults to CP::vzero
. Can be overridden on a per body basis by writing custom integration functions.damping
– Float
: Amount of viscous
damping to apply to the space. A value of 0.9 means that each body will
lose 10% of it’s velocity per second. Defaults to 1. Like gravity
can be overridden on a per body basis.idle_speed
– Float
: 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.sleep_time
– Float
: Time a group of bodies must remain idle in order to fall asleep. The default value of INFINITY
disables the sleeping algorithm.CP::Space.new()
CP::Space#add_shape(shape)
void CP::Space#add_static_shape(shape)
void CP::Space#add_body(body)
void CP::Space#add_constraint(constraint)
void CP::Space#remove_shape(shape)
void CP::Space#remove_static_shape(shape)
void CP::Space#remove_body(body)
void CP::Space#remove_constraint(constraint)
space
. They return the object removed from the space, or
nil
if the object in question was already removed from or never had been
added to the space. See the section on Static Shapes above for an explanation
of what a static shape is and how it differs from a normal shape. Also, you
cannot call the any of these functions from within a callback other than a
post-step callback (which is different than a post-solve callback!).
Attempting to add or remove objects from the space while CP::Space#step
is still executing will most likely crash your application!
See the callbacks section for more information.void CP::Space#activate_touching(shape)
shape
and call
CP::Body#activate
on them. This is useful when you need to move a shape explicitly.
Waking shapes touching the moved shape prevents objects from sleeping
when the floor is moved or something similar. You only need to do this
when changing the shape of an existing shape or moving a static shape.
Most other methods of moving a shape automatically would wake up other
touching objects.Chipmunk uses a spatial hash to accelerate it’s collision detection. While it’s not necessary to interact with the hash directly. The current API does expose some of this at the space level to allow you to tune it’s performance.
void CP::Space#resize_static_hash(dim, count)
CP::Space#resize_active_hash(dim, count)
The spatial hash data structures used by Chipmunk’s collision detection are fairly size sensitive. dim
is the size of the hash cells. Setting dim
to the average collision shape size is likely to give the best performance. Setting dim
too small will cause the shape to be inserted into many cells, setting
it too low will cause too many objects into the same hash slot.
count
is the suggested minimum number of cells
in the hash table. If there are too few cells, the spatial hash will
return many false positives. Too many cells will be hard on the cache
and waste memory. the Setting count
to ~10x the number of objects in the space is probably a good starting point. Tune from there if necessary. By default, dim
is 100.0, and count
is 1000. The new demo program has a visualizer for the static hash. You
can use this to get a feel for how to size things up against the
spatial hash.
Using the spatial has visualization in the demo program you can see
what I mean. The grey squares represent cells in the spatial hash. The
darker the cell, the more objects have been mapped into that cell. A
good dim
size is when your objects fit nicely into the grid:
Notice the light grey meaning that each cell doesn’t have too many objects mapped onto it.
When you use too small a size, Chipmunk has to insert each object into a lot of cells. This can get expensive.
Notice that the grey cells are very small compared to the collision shapes.
When you use too big of a size, a lot of shapes will fit into each cell. Each shape has to be checked against every other shape in the cell, so this makes for a lot of unnecessary collision checks.
Notice the dark grey cells meaning that many objects are mapped onto them.
CP::Space#rehash_static()
CP::Space#rehash_shape(shape)
void CP::Space#step(dt)
count
argument to CP::Space.resize_static_hash(dim, count)
than to CP::Space.resize_active_hash(dom, count)
. Doing so will use more memory but can improve performance if you have a lot of static shapes.CP::Constraint
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.
Constraints in Chipmunk are all velocity based constraints. This means that they act primarily by synchronizing the velocity of two bodies. A pivot joint holds two anchor points on two separate bodies together by defining equations that say that the velocity of the anchor points must be the same and calculating impulses to apply to the bodies to try and keep it that way. A constraint takes a velocity as it’s primary input and produces a velocity change as it’s output. Some constraints, (joints in particular) apply velocity changes to correct differences in positions. More about this in the next section.
A spring connected between two bodies is not a constraint. It’s very constraint-like as it creates forces that affect the velocities of the two bodies, but a spring takes distances as input and produces forces as it’s output. If a spring is not a constraint, then why do I have two varieties of spring constraints you ask? The reason is because they are damped springs. The damping associated with the spring is a true constraint that creates velocity changes based on the relative velocities of the two bodies it links. As it is convenient to put a damper and a spring together most of the time, I figured I might as well just apply the spring force as part of the constraint instead of having a damper constraint and having the user calculate and apply their own spring forces separately.
a
– CP::Body
: The first body that the constraint acts on.b
– CP::Body
: The second body that the constraint acts on.maxForce
– Float
: is the maximum force that the constraint can use to act on the two bodies. Defaults to INFINITY.biasCoef
– Float
: is the percentage of
error corrected each step of the space. (Can cause issues if you don’t
use a constant time step) Defaults to 0.1.maxBias
– Float
: is the maximum speed at which the constraint can apply error correction. Defaults to INFINITY.data
– Object
: The Ruby bindings use this field internally, so it is not available for you use. It returns self
, but that is not guaranteed.object
– Object
: A user definable
data object. If you set this to point at the game object the shapes is
for, then you can access your game object from Chipmunk callbacks.To access properties of specific joint types, use the getter and setter functions provided (ex: CP::PinJoint.anchr1
). See the lists of properties for more information.
Joints in Chipmunk are not perfect. A pin joint can’t maintain the exact correct distance between it’s anchor points, nor can a pivot joint hold it’s anchor points completely together. Instead, they are designed to deal with this by correcting themselves over time. In Chipmunk 5, you have a fair amount of extra control over how joints correct themselves and can even use this ability to create physical effects that allow you to use joints in unique ways:
There are three public fields of CP::Constraint structs that control the error correction, max_force
, max_bias
, and bias_coef
. max_force
is pretty self explanatory, a joint or constraint will not be able to
use more than this amount of force in order to function. If it needs
more force to be able to hold itself together, it will fall apart. max_bias
is the maximum speed at which error correction can be applied. If you
change a property on a joint so that the joint will have to correct
itself, it normally does so very quickly. By setting a max_speed
you can make the joint work like a servo, correcting itself at a
constant rate over a longer period of time.
Lastly, bias_coef
is the percentage of error corrected every
step before clamping to a maximum speed. You can use this to make joints
correct themselves smoothly instead of at a constant speed, but is probably
the least useful of the three properties by far.
Neither constraints or collision shapes have any knowledge of the other. When connecting joints to a body the anchor points don’t need to be inside of any shapes attached to the body and it often makes sense that they shouldn’t. Also, adding a constraint between two bodies doesn’t prevent their collision shapes from colliding. In fact, this is the primary reason that the collision group property exists.
CP::Constraint#impulse
constraint
applied. To convert this to a force, divide by the time step passed to CP::Space#step()
.CP::PinJoint.new(a, b, anchr1, anchr2)
a
and b
are the two bodies to connect, and anchr1
and anchr2
are the anchor points (Vect2) 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.Name | Type |
---|---|
anchr1 | CP::Vec2 |
anchr2 | CP::Vec2 |
dist | Float |
CP::SlideJoint.new(a, b, anchr1, anchr2, min, max)
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.Name | Type |
---|---|
anchr1 | CP::Vec2 |
anchr2 | CP::Vec2 |
Min | Float |
Max | Float |
CP::_pivot_joint *CP::_pivot_joint_alloc(void)
CP::_pivot_joint *CP::_pivot_joint_init(CP::_pivot_joint *joint, CP::Body *a, CP::Body *b, CP::Vec2 pivot)
CP::Constraint *CP::_pivot_joint_new(CP::Body *a, CP::Body *b, CP::Vec2 pivot)
CP::Constraint *CP::_pivot_joint_new2(CP::Body *a, CP::Body *b, CP::Vec2 anchr1, CP::Vec2 anchr2)
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.Name | Type |
---|---|
anchr1 | CP::Vec2 |
anchr2 | CP::Vec2 |
CP::GrooveJoint.new(a, *b, groove_a, groove_b, anchr2)
groov_a
to groove_b
on body a
, and the pivot is attached to anchr2
on body b
. All coordinates are body local.Name | Type |
---|---|
anchr2 | CP::Vec2 |
groove_a | CP::Vec2 |
groove_b | CP::Vec2 |
CP::DampedSpring.new(a, b, anchr1, anchr2, restLength, stiffness,
damping)
restLength
is the distance the spring wants to be, stiffness
is the spring constant (>Young’s modulus), and damping
is how soft to make the damping of the spring.Name | Type |
---|---|
anchr1 | CP::Vec2 |
anchr2 | CP::Vec2 |
rest_length | Float |
stiffness | Float |
damping | Float |
CP::DampedRotarySpring.new(a, b, restAngle, stiffness,
damping)
restAngle
is the relative angle in radians that the bodies want to have, stiffness
and damping
work basically the same as on a damped spring.Name | Type |
---|---|
rest_angle | Float |
stiffness | Float |
damping | Float |
CP::RotaryLimitJoint.new(a, b, min,max)
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.Name | Type |
---|---|
min | Float |
max | Float |
CCP::RatchetJoint.new(a, b, phase, ratchet);
ratchet
is the distance between “clicks”, phase
is the initial offset to use when deciding where the ratchet angles are.Name | Type |
---|---|
angle | Float |
phase | Float |
ratchet | Float |
CP::GearJoint.new(a, b, phase, ratio);
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.Name | Type |
---|---|
phase | Float |
ratio | Float |
CP::SimpleMotor.new(a, b, Float rate);
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.Name | Type |
---|---|
rate | Float |
In order to make collision detection in Chipmunk as fast as possible, the process is broken down into several stages. While I’ve tried to keep it conceptually simple, the implementation can be a bit daunting. Fortunately as a user of the library, you don’t need to understand everything about how it works. If you are trying to squeeze every ounce of performance out of Chipmunk, understanding this section is crucial.
The Chipmunk module CP contains variables that control how collisions are solved. They can’t be set on a per space basis, but most games wont need to change them at all.
CP.bias_coef
CP::Space#step()
Chipmunk fixes a percentage of the overlap to push the shapes apart. By
default, this value is set to 0.1. With this setting, after 15 steps
the overlap will be reduced to 20%, after 30 it will be reduced to 4%.
This is usually satisfactory for most games. Setting CP::_bias_coef to 1.0
is not recommended as it will reduce the stability of stacked objects.CP.collision_slop
CP.contact_persistence
Chipmunk uses a spatial hash for its broad phase culling. Spatial hashes are very efficient for a scene made up of consistently sized objects. It basically works by taking the axis aligned bounding boxes for all the objects in the scene, mapping them onto an infinite sized grid, then mapping those grid cells onto a finite sized hash table. This way, you only have to check collisions between objects in the same hash table cells, and mapping the objects onto the grid can be done fairly quickly. Objects in the same hash table cells tend to be very close together, and therefore more likely to be colliding. The downside is that in order to get the best performance out of a spatial hash, you have to tune the size of the grid cells and the size of the hash table so you don’t get too many false positives. For more information about tuning the hash see the CP::Space section.
Things to keep in mind:
For more information on spatial hashing in general, Optimized Spatial Hashing for Collision Detection of Deformable Objects is a good paper that covers all the basics.
After the spatial hash figures out pairs of shapes that are likely to be near each other, it passes them back to the space to perform some additional filtering on the pairs. If the pairs pass all the filters, then Chipmunk will test if the shapes are actually overlapping. If the shape to shape collision check passes, then the collision handler callbacks are called. These tests are much faster to try than the shape to shape collision checks, so use these if you can to reject collisions early instead of rejecting them from callbacks if you can.
The most expensive test that you can do to see if shapes should collide is to actually check based on their geometry. Circle to circle and circle to line collisions are pretty quick. Poly to poly collisions get more expensive as the number of vertexes increases. Simpler shapes make for faster collisions (and more importantly fewer collision points for the solver to run).
After checking if two shapes overlap Chipmunk will look to see if you have defined a collision handler for the collision types of the shapes. This gives you a large amount of flexibility to process collisions events, but also gives you a very flexible way to filter out collisions.
The return value of the begin and pre_solve callbacks determines whether or not the colliding pair of shapes is discarded or not. Returning true will keep the pair, false will discard it. If you don’t define a handler for the given collision_types, Chipmunk will call the space’s default handler, which by default is defined to simply accept all collisions.
While using callbacks to filter collisions is the most flexible way, keep in mind that by the time your callback is called all of the most expensive collision detection has already been done. For simulations with a lot of colliding objects each frame, the time spent finding collisions is small compared to the time spent solving the physics for them so it may not be a big deal. Still, use layers or groups first if you can.
A physics library without any events or feedback would not be very useful for games. How would you know when the player bumped into an enemy so that you could take some health points away? How would you know how hard the car hit something so you don’t play a loud crash noise when a pebble hits it? What if you need to decide if a collision should be ignored based on specific conditions, like implementing one way platforms? Chipmunk has a number of powerful callback systems that you can plug into to accomplish all of that.
The Ruby bindings support 2 types of collision handlers, namely a simple block, or a collision handler object.
A collision handler block is a block that takes either 0, 1, 2, or 3 arguments. These arguments may include the two colliding shapes, and an arbiter that describes the details of the collision. SeeCP::Arbiter for more info on arbiters.
Collision handler blocks are installed by calling Space#add_collision_func(collision_type1, collision_type2) do ... end
. Depending on the arity of the handler block, different arguments will be passed:
Arity | Signature | Passed |
---|---|---|
0 | do ... end | No arguments passed. |
1 | do |arbiter| ... end | Arbiter passed. |
2 | do |a,b|... end | Colliding shapes a and b passed. |
3 | do |a,b,arbiter|... end | Colliding shapes a and b, and arbiter passed. |
A collision handler block will be called in the pre_solve step of the collision detection. Return false from the callback to make Chipmunk ignore the collision this step or true to process it normally.
A collision handler object is an Object that responds_to? one or more of 4 distinct messages. Or, in other words, it's any object that has the one or more of 4 following methods for the different collision events that Chipmunk recognizes. The event types are:
begin
Two shapes just started touching for the
first time this step. Return true from the callback to process the
collision normally or false to cause Chipmunk to ignore the collision
entirely. If you return false, the pre-solve and post-solve callbacks
will never be run, but you will still receive a separate event when the
shapes stop overlapping.Depending on the arity of the mentioned methods, different arguments will be passed to them:
Arity | Signature | Passed |
---|---|---|
0 | () | No arguments passed. |
1 | (arbiter) | Arbiter passed. |
2 | (a,b) | Colliding shapes a and b passed. |
3 | (a,b,arbiter) | Colliding shapes a and b, and arbiter passed. |
The following is and example of a valid Collision Handler Object:
class CollisionHandler
def begin(a, b, arbiter)
end
def pre_solve(a, b)
end
def post_solve(arbiter)
end
def separate
end
end
Collision callbacks are closely associated with CP::Arbiter structs. You should familiarize yourself with those as well.
Note: Shapes tagged as sensors (CP::Shape.sensor == true
)
never generate collisions that get processed so collisions between
sensors shapes and other shapes will never call the post-solve callback.
They still generate begin, and separate callbacks, and the pre solve
callback is also called every frame even though there is no real
collision.
CP::Space#add_collision_handler(collision_type_a, collision_type_b,
collision_handler_object)
Symbol
) a
and collision type b
collide, the callbacks defined on collision_handler_object
will be used to process the collision.CP::Space#add_collision_handler(collision_type_a, collision_type_b, &block)
Symbol
) a
and collision type b
collide, the block will be called to process the collision.
CP::Space#remove_collision_handler(collision_type a, collision_type b)
CP::Space.default_collision_handler(collision_type_a, collision_type_b,collision_handler_object), CP::Space.default_collision_handler(collision_type_a, collision_type_b,&collision_handler_block)
begin
and pre_solve
and does nothing in the post_solve()
and separate()
callbacks.Post-step callbacks are the one place where you can break the rules about adding or removing objects from within a callback. In fact, their primary function is to help you safely remove objects from the space that you wanted to disable or destroy in a collision callback.
Post step callbacks are registered as a function and a pointer that is used as a key. You can only register one post step callback per key. This prevents you from accidentally removing an object more than once. For instance, say that you get a collision callback between a bullet and object A. You want to destroy both the bullet and object A, so you register a post-step callback to safely remove them from your game. Then you get a second collision callback between the bullet and object B. You register a post-step callback to remove object B, and a second post-step callback to remove the bullet. Because you can only register one callback per key, the post-step callback for the bullet will only be called once and you can’t accidentally register to have it removed twice.
Note: Additional callbacks registered to the same key are ignored even if they use a different callback function or data pointer.
do |space, key| ... end
space
is the space the callback was registered on, key
is the pointer value you supplied as the keyCP::Space.add_post_step_callback(key, &block)
block
to be called before CP::Space#step()
returns. self (CP::Space)
and key (Object)
will be passed to your block, which must have the signature do |space, key| ... end
. Only the last callback registered for any unique value of key
will be recorded. You can add post-step callbacks from outside of other callback functions, but they won’t be called until CP::Space#step()
is called again.CP::Arbiter
First of all, why are they called arbiters? The short answer is that Box2D called them that way back in 2006 when Scott Lembcke was looking at the source for it’s solver. An arbiter is like a judge, a person that has authority to settle disputes between two people. It was a fun, fitting name and was shorter to type than CollisionPair which he had been using. :p
Originally arbiters were going to be an internal data type that Chipmunk used that wouldn’t ever be sent to outside code. In Chipmunk 4.x and earlier, only a single callback hook was provided for handling collision events. It was triggered every step that to shapes were touching. This made it non-trivial to track when objects started and stopped touching as the user would have to record and process this themselves. Many people, including myself, wanted to get collision begin/separate events and eventually I realized that information was already being stored in the arbiter cache that the Chipmunk maintains. With some changes to the collision callback API, that information is now exposed to the user. Internally, arbiters are used primarily for solving collision impulses. To external users, you can simply think of them as a weird type used in collision callbacks.
You should never need to create an arbiter, nor will you ever need to free one as they are handled by the space. More importantly, because they are managed by the space you should never store a reference to an arbiter as you don’t know when they will be destroyed. Use them within the callback where they are given to you and then forget about them or copy out the information you need. This is even so in these Ruby bindings!
CP::Arbiter#shapes
CP::Space#add_collision_handler(space, 1, 2, ...)
,
you you will find that a.collision_type == 1
and
b.collision_type == 2
.CP::Arbiter#first_contact?
pre_solve()
and post_solve()
callbacks to know if a collision between two shapes is new without needing to
flag a Boolean in your begin()
callback.CP::Arbiter#points
separate()
callback is not allowed.
CP::ContactPoint#point,
CP::ContactPoint#normal,
CP::ContactPoint#dist
CP::Arbiter.count
CP::Arbiter.contacts
CP::Arbiter.normal(i)
CP::Arbiter.normal(0)
and not have to check each contact point. Note: calling this function from the separate()
callback is undefined.CP::Vec2 CP::Arbiter.point(i)
separate()
callback is undefined.CP::Vec2 CP::Arbiter.depth(i)
CP::Vec2 CP::Arbiter.impulse(friction = nil)
post_step()
callback,
otherwise the result is undefined. Note:
If you are using the deprecated elastic iterations setting on your
space, it will cause you to get incorrect results. Elastic iterations
should no longer be needed, and you should be able to safely turn them
off.It’s unlikely that you’ll need to interact with a CP::Arbiter
struct directly as the collision helper functions should provide most
functionality that people will need. One exception is the e
,
and u
attributes. By default, Chipmunk multiplies the friction
and elasticity values of to shapes together to determine what values to use when
solving the collision. This mostly works, but in many cases is simply
not flexible enough. If you are running into problems with that, you can
change the values calculated for e
and u
in a
preSolve callback. This is likely to change in the next major version of
Chipmunk.
contacts
– Fixnum
: Number of contact points for this collision.e
– Float
: Calculated amount of elasticity to apply for this collision. Can be overridden from a preSolve()
callback.u
– Float
: Calculated amount of friction to apply for this collision. Can be overridden from a preSolve()
callback.surface_v
– CP::Vec2
: Calculated amount of surface velocity to apply for this collision. Can be overridden from a pre_solve()
callback. (Very likely to change in the next major version of Chipmunk).Chipmunk spaces currently support three kinds
of spatial queries, point, segment and bounding box. Any type can be
done efficiently against an entire space, or against individual shapes.
All types of queries take a collision group and layer that are used to
filter matches out using the same rules used for filtering collisions
between shapes. See CP::Shape for more information. If you don’t want to filter out any matches, use CP::ALL_LAYERS
for the layers and CP::NO_GROUP
as the group.
Point queries are useful for things like mouse picking and simple sensors.
CP::Shape.point_query(p)
CP::Space.point_query(point,layers, group) do |shape| ... end
space
at point
filtering out matches with the given layers
and group
. The given block is called for each shape found with shape
as argument. Sensor shapes are included.CP::Space.point_query_first(point, layers, group)
space
at point
and return the first shape found matching the given layers
and group
. Returns nil
if no shape was found. Sensor shapes are ignored.Segment queries are like ray casting, but because Chipmunk uses a spatial hash to process collisions, it cannot process infinitely long queries like a ray. In practice this is still very fast and you don’t need to worry too much about the performance as long as you aren’t using extremely long segments for your queries.
CP::SegmentQueryInfo is a Ruby Struct with the following members:
shape: shape that was hit, nil if no collision.
t: Distance along query segment, will always be in the range 0..1 .
n: Normal of hit surface.
t
is the percentage
between the query start and end points. If you need the hit point in
world space or the absolute distance from start, see the segment query
helper functions farther down.CP::Shape.segment_query(a, b)
a
to b
intersects the shape. Returns a CP::SegmentQueryInfo struct with the results of the query.
CP::Space#segment_query(start, stop, layers, group) do |shape, t, n|
space
along the line segment from start
to stop
filtering out matches with the given layers
and group
. The block is called with the shape found, the normalized distance along the line and surface normal for each shape found. Sensor shapes are included.CP::Space#segment_query_first(start, stop, layers, group)
space
along the line segment from start
to stop
filtering out matches with the given layers
and group
. Only the first shape encountered is returned and the search is short circuited. Returns nil
if no shape was found. Otherwise returns a CP::SegmentQueryInfo struct with the results of the query. Sensor shapes are ignored.CP::SegmentQueryInfo#hit_point(start, stop)
CP::SegmentQueryInfo#hit_dist(start, stop)