class

CP::Body

Inherits Reference / Object

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 overwrite your custom mass value when the shapes are added to the body.

Source
new_kinematic

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

Source
new_static

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

Source

Instance methods

activate

Wake up a sleeping or idle body.

Source
activate_static(filter : Shape | Nil)

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

Source
angle

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.

Source
angle=(angle : Number)
Source
angular_velocity

The angular velocity of the body in radians per second.

Source
angular_velocity=(angular_velocity : Number)
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.

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.

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.

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.

Source
arbiters

Get each arbiter associated with this body.

center_of_gravity

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.

Source
center_of_gravity=(center_of_gravity : Vect)
Source
constraints

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

Force applied to the center of gravity of the body.

This value is reset for every time step.

Source
force=(force : Vect)
Source
kinetic_energy

Get the amount of kinetic energy contained by the body.

Source
local_to_world(point : Vect) : Vect

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

Source
mass

The mass of the body.

Source
mass=(mass : Number)
Source
moment

The moment of inertia of the body.

The moment is like the rotational mass of a body.

Source
moment=(moment : Number)
Source
position

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.

Source
position=(position : Vect)
Source
rotation

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

Source
shapes

Get each shape associated with this body.

sleep

Force a body to fall asleep immediately.

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.

Source
sleeping?

Returns true if the body is sleeping.

Source
space

Get the space this body is added to.

Source
torque

The torque applied to the body.

This value is reset for every time step.

Source
torque=(torque : Number)
Source
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 preserved when changing types. This function cannot be called directly in a collision callback.

Source
type=(type : Type)
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.

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.

Source
velocity

Linear velocity of the center of gravity of the body.

Source
velocity=(velocity : Vect)
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.

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.

Source
world_to_local(point : Vect) : Vect

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

Source

Nested types