Skip to content

Body

class CP::Body
inherits Reference #

Chipmunk's rigid body type.

Rigid bodies hold the physical properties of an object like its mass, and position and velocity of its center of gravity. They don't have an shape on their own. They are given a shape by creating collision shapes (Shape) that point to the body.

Use forces to modify the rigid bodies if possible. This is likely to be the most stable.

Modifying a body's velocity shouldn't necessarily be avoided, but applying large changes can cause strange results in the simulation. Experiment freely, but be warned.

Don't modify a body's position every step unless you really know what you are doing. Otherwise you're likely to get the position/velocity badly out of sync.

Constructors#

.new(mass : Number = 0, moment : Number = 0)#

Create a new dynamic Body.

Guessing the mass for a body is usually fine, but guessing a moment of inertia can lead to a very poor simulation so it's recommended to use Chipmunk's moment calculations to estimate the moment for you.

There are two ways to set up a dynamic body. The easiest option is to create a body with a mass and moment of 0, and set the mass or density of each collision shape added to the body. Chipmunk will automatically calculate the mass, moment of inertia, and center of gravity for you. This is probably preferred in most cases.

The other option is to set the mass of the body when it's created, and leave the mass of the shapes added to it as 0.0. This approach is more flexible, but is not as easy to use. Don't set the mass of both the body and the shapes. If you do so, it will recalculate and overwite your custom mass value when the shapes are added to the body.

View source

.new_kinematic : self#

Create a Body, and set it as a kinematic body.

View source

.new_static : self#

Create a Body, and set it as a static body.

View source

Methods#

#activate#

Wake up a sleeping or idle body.

View source

#activate_static(filter : Shape | Nil)#

Wake up any sleeping or idle bodies touching a static body.

View source

#angle : Float64#

Rotation of the body in radians.

When changing the rotation you may also want to call Space.reindex_shapes_for(body) to update the collision detection information for the attached shapes if you plan to make any queries against the space. A body rotates around its center of gravity, not its position.

View source

#angle=(angle : Number)#

View source

#angular_velocity : Float64#

The angular velocity of the body in radians per second.

View source

#angular_velocity=(angular_velocity : Number)#

View source

#apply_force_at_local_point(force : Vect, point : Vect)#

Apply a force to a body. Both the force and point are expressed in body local coordinates.

View source

#apply_force_at_world_point(force : Vect, point : Vect)#

Apply a force to a body. Both the force and point are expressed in world coordinates.

View source

#apply_impulse_at_local_point(impulse : Vect, point : Vect)#

Apply an impulse to a body. Both the impulse and point are expressed in body local coordinates.

View source

#apply_impulse_at_world_point(impulse : Vect, point : Vect)#

Apply an impulse to a body. Both the impulse and point are expressed in world coordinates.

An impulse is a very large force applied over a very short period of time. Some examples are a ball hitting a wall or cannon firing. Chipmunk treats impulses as if they occur instantaneously by adding directly to the velocity of an object. Both impulses and forces are affected the mass of an object. Doubling the mass of the object will halve the effect.

View source

#arbiters : Array(Arbiter)#

Get each arbiter associated with this body.

#center_of_gravity : Vect#

The offset of the center of gravity in body local coordinates.

The default value is (0, 0), meaning the center of gravity is the same as the position of the body.

View source

#center_of_gravity=(center_of_gravity : Vect)#

View source

#constraints : Array(Constraint)#

Get each constraint associated with this body.

#each_arbiter#

Get each arbiter associated with this body.

#each_constraint#

Get each constraint associated with this body.

#each_shape#

Get each shape associated with this body.

#force : Vect#

Force applied to the center of gravity of the body.

This value is reset for every time step.

View source

#force=(force : Vect)#

View source

#kinetic_energy : Float64#

Get the amount of kinetic energy contained by the body.

View source

#local_to_world(point : Vect) : Vect#

Convert body relative/local coordinates to absolute/world coordinates.

View source

#mass : Float64#

The mass of the body.

View source

#mass=(mass : Number)#

View source

#moment : Float64#

The moment of inertia of the body.

The moment is like the rotational mass of a body.

View source

#moment=(moment : Number)#

View source

#position : Vect#

The position of the body.

When changing the position you may also want to call Space#reindex_shapes_for(body) to update the collision detection information for the attached shapes if you plan to make any queries against the space.

View source

#position=(position : Vect)#

View source

#rotation : Vect#

Get the rotation vector of the body. (The x basis vector of its transform.)

View source

#shapes : Array(Shape)#

Get each shape associated with this body.

#sleep#

Force a body to fall asleep immediately.

View source

#sleep_with_group(group : Body)#

Force a body to fall asleep immediately along with other bodies in a 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 its group are woken up. sleep_with_group allows you group sleeping objects together. If you pass a sleeping body for group, body will be awoken when group is awoken. You can use this to initialize levels and start stacks of objects in a pre-sleeping state.

View source

#sleeping? : Bool#

Returns true if the body is sleeping.

View source

#space : Space | ::Nil#

Get the space this body is added to.

View source

#torque : Float64#

The torque applied to the body.

This value is reset for every time step.

View source

#torque=(torque : Number)#

View source

#type : Type#

The type of the body.

When changing a body to a dynamic body, the mass and moment of inertia are recalculated from the shapes added to the body. Custom calculated moments of inertia are not preseved when changing types. This function cannot be called directly in a collision callback.

View source

#type=(type : Type)#

View source

#update_position(dt : Number)#

Called each time step to update a body's position (can be overridden in a subclass).

Updates the position of the body using Euler integration.

It's not generally recommended to override this unless you call super.

View source

#update_velocity(gravity : Vect, damping : Number, dt : Number)#

Called each time step to update a body's velocity (can be overridden in a subclass).

Updates the velocity of the body using Euler integration.

View source

#velocity : Vect#

Linear velocity of the center of gravity of the body.

View source

#velocity=(velocity : Vect)#

View source

#velocity_at_local_point(point : Vect) : Vect#

Get the velocity on a body (in world units) at a point on the body in local coordinates.

View source

#velocity_at_world_point(point : Vect) : Vect#

Get the velocity on a body (in world units) at a point on the body in world coordinates.

It's often useful to know the absolute velocity of a point on the surface of a body since the angular velocity affects everything except the center of gravity.

View source

#world_to_local(point : Vect) : Vect#

Convert body absolute/world coordinates to relative/local coordinates.

View source