The Rapier physics plugin
The easiest way to setup Rapier-based physics into Bevy is to use the RapierPhysicsPlugin
:
- Example 2D
- Example 3D
Adding this plugin will result in the registration of all the Rapier resources necessary for the physics simulation. The following structures are added as individual resources by this plugin:
- Example 2D
- Example 3D
Resource type | Description |
---|---|
rapier2d::geometry::BroadPhase | for the broad-phase stage of collision detection. |
rapier2d::geometry::NarrowPhase | for the narrow-phase stage of collision detection. |
rapier2d::geometry::ColliderSet | for the set of colliders. |
rapier2d::dynamics::IntegrationParameters | for configuring the constraint solver. |
rapier2d::dynamics::RigidBodySet | for the set of rigid-bodies. |
rapier2d::dynamics::JointSet | for the set of joint constraints. |
rapier2d::pipeline::PhysicsPipeline | for combining all the other elements to run the simulation step by step. |
bevy_rapier2d::physics::Gravity | for configuring the gravity. |
bevy_rapier2d::physics::EventQueue | for collecting the physics events (collision/intersection started/ended). |
Resource type | Description |
---|---|
rapier3d::geometry::BroadPhase | for the broad-phase stage of collision detection. |
rapier3d::geometry::NarrowPhase | for the narrow-phase stage of collision detection. |
rapier3d::geometry::ColliderSet | for the set of colliders. |
rapier3d::dynamics::IntegrationParameters | for configuring the constraint solver. |
rapier3d::dynamics::RigidBodySet | for the set of rigid-bodies. |
rapier3d::dynamics::JointSet | for the set of joint constraints. |
rapier3d::pipeline::PhysicsPipeline | for combining all the other elements to run the simulation step by step. |
bevy_rapier3d::physics::Gravity | for configuring the gravity. |
bevy_rapier3d::physics::EventQueue | for collecting the physics events (collision/intersection started/ended). |
Each resource can be accessed by any system using the usual Bevy resource wrappers:
The RapierPhysicsPlugin
will actually register two other resources, namely EntityToBody
and RapierPhysicsScale
. However they
will likely disappear in the future.
Besides these resources, the RapierPhysicsPlugin
will register a few systems responsible for:
- Building rigid-bodies, colliders, and joints based some transient components.
- Performing one timestep at each update executed by the Bevy game loop.
- Writing the Rapier rigid-bodies positions back into the
Translation
andRotation
components of the entities they are attached to.
Once the plugin is registered, the next step is to create rigid-bodies an colliders.
#
Creating rigid-bodies and collidersCreating rigid-bodies and colliders using the Bevy plugin is mostly the same as when using Rapier directly as described in that section of the user-guide. You will need to:
- Create a
RigidBodyBuilder
which is responsible for building a rigid-body. - Create a
ColliderBuilder
which is responsible for building a collider.
However, using the bevy_rapier
plugin, the .build()
method of these builders must not be called directly. Instead
these builders are components to be attached to a Bevy entity:
- Example 2D
- Example 3D
Once these components are added, three things will happen during the next step of the Bevy game loop:
- One of the systems automatically registered by the
RapierPhysicsPlugin
will read these builder components, call their.build()
method, and add the newly built rigid-body/collider to theRigidBodySet
andColliderSet
accessible through Bevy resources. - If the rigid-body/collider creation succeeded, the builder components are removed from the entity.
- One
RigidBodyHandleComponent
and oneColliderHandleComponent
will be created and attached to the entity being processed. These components contain the Rapier handles needed to retrieve the rigid-body or collider from theRigidBodySet
andColliderSet
.RigidBodySet
warning
The current version of our bevy_rapier
plugin is limited to one rigid-body and one collider per entity. An entity
with a rigid-body but no collider is not supported. Nor is an entity with no rigid-body and a collider. Entities with
multiple rigid-bodies or multiple colliders attached to them are not supported either. This will be improved in future
versions of our plugins.
#
Creating jointsThe creation of joints is slightly different from the creation of rigid-bodies and colliders because a joint affects
two rigid-bodies at the same time. First joint parameters like BallJoint
, FixedJoint
, etc. have to be created and
initialized, as usual. However instead of calling JointSet::insert_joint(...)
directly to create the joint, we
need to create a JointBuilderComponent
which is responsible for letting the Rapier plugin know that a joint has to
be created between two Bevy entities containing a rigid-body each:
- Example 2D
- Example 3D
This JointBuilderComponent
is a transient component that will cause one of the Rapier plugin systems to:
- Create the corresponding
Joint
and add it to theJointSet
resource. - Remove this
JointBuilderComponent
from the processed entity. - Add a
JointHandleComponent
to the processed entity. This new component will contain an handle necessary to get a reference to the joint from theJointSet
. It also contains the identifiers of the entity this joint is attached to.
#
Handling contact/intersection eventsThe RapierPhysicsPlugin
automatically creates and add the EventQueue
resource to the Bevy app. This event queue
can be used to read and handle events generated by the physics pipeline:
- Example 2D
- Example 3D
#
Going furtherThis is as far as the current capabilities of the bevy rapier plugin go. Keep in mind that you can still access directly
all the Rapier structures, rigid-bodies, contact graph, etc. through the resources added by the RapierBevyPlugin
. This
gives you the same level of flexibility as using the original Rapier crate directly.print_events
In the next section, we are presenting a second plugin we provide in order to easily render the content of the Rapier resources.