class CP::Body
- CP::Body
- Reference
- Object
Overview
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.
Defined in:
chipmunk/body.crConstructors
-
.new(mass : Number = 0, moment : Number = 0)
Create a new dynamic
Body
. -
.new_kinematic : self
Create a
Body
, and set it as a kinematic body. -
.new_static : self
Create a
Body
, and set it as a static body.
Instance Method Summary
-
#activate
Wake up a sleeping or idle body.
-
#activate_static(filter : Shape | Nil)
Wake up any sleeping or idle bodies touching a static body.
-
#angle : Float64
Rotation of the body in radians.
- #angle=(angle : Number)
-
#angular_velocity : Float64
The angular velocity of the body in radians per second.
- #angular_velocity=(angular_velocity : Number)
-
#apply_force_at_local_point(force : Vect, point : Vect)
Apply a force to a body.
-
#apply_force_at_world_point(force : Vect, point : Vect)
Apply a force to a body.
-
#apply_impulse_at_local_point(impulse : Vect, point : Vect)
Apply an impulse to a body.
-
#apply_impulse_at_world_point(impulse : Vect, point : Vect)
Apply an impulse to a body.
-
#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.
- #center_of_gravity=(center_of_gravity : Vect)
-
#constraints : Array(Constraint)
Get each constraint associated with this body.
-
#each_arbiter(&block : Arbiter -> )
Get each arbiter associated with this body.
-
#each_constraint(&block : Constraint -> )
Get each constraint associated with this body.
-
#each_shape(&block : Shape -> )
Get each shape associated with this body.
-
#force : Vect
Force applied to the center of gravity of the body.
- #force=(force : Vect)
-
#kinetic_energy : Float64
Get the amount of kinetic energy contained by the body.
-
#local_to_world(point : Vect) : Vect
Convert body relative/local coordinates to absolute/world coordinates.
-
#mass : Float64
The mass of the body.
- #mass=(mass : Number)
-
#moment : Float64
The moment of inertia of the body.
- #moment=(moment : Number)
-
#position : Vect
The position of the body.
- #position=(position : Vect)
-
#rotation : Vect
Get the rotation vector of the body.
-
#shapes : Array(Shape)
Get each shape associated with this body.
-
#sleep
Force a body to fall asleep immediately.
-
#sleep_with_group(group : Body)
Force a body to fall asleep immediately along with other bodies in a group.
-
#sleeping? : Bool
Returns true if the body is sleeping.
-
#space : Space | Nil
Get the space this body is added to.
-
#torque : Float64
The torque applied to the body.
- #torque=(torque : Number)
-
#type : Type
The type of the body.
- #type=(type : Type)
-
#update_position(dt : Number)
Called each time step to update a body's position (can be overridden in a subclass).
-
#update_velocity(gravity : Vect, damping : Number, dt : Number)
Called each time step to update a body's velocity (can be overridden in a subclass).
-
#velocity : Vect
Linear velocity of the center of gravity of the body.
- #velocity=(velocity : Vect)
-
#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.
-
#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.
-
#world_to_local(point : Vect) : Vect
Convert body absolute/world coordinates to relative/local coordinates.
Constructor Detail
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.
Instance Method Detail
Wake up any sleeping or idle bodies touching a static body.
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.
Apply a force to a body. Both the force and point are expressed in body local coordinates.
Apply a force to a body. Both the force and point are expressed in world coordinates.
Apply an impulse to a body. Both the impulse and point are expressed in body local coordinates.
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.
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.
Force applied to the center of gravity of the body.
This value is reset for every time step.
Convert body relative/local coordinates to absolute/world coordinates.
The moment of inertia of the body.
The moment is like the rotational mass of a body.
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.
Get the rotation vector of the body. (The x basis vector of its transform.)
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.
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.
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
.
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.
Get the velocity on a body (in world units) at a point on the body in local coordinates.
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.
Convert body absolute/world coordinates to relative/local coordinates.