soft_body_attachments
A particle can also be attached directly to a rigid-body (the
SoftBodyAttachments component), which is
the simplest way of combining the two kinds of bodies: a rope tied to a swinging ball, a flag attached to its pole,
etc. Unlike pinning, an attachment is a two-way point-to-point constraint: the rigid-body holds the particle, and the
particle pulls the rigid-body back. The anchor is the position the particle has when the attachment is created,
expressed in the local frame of the rigid-body. Note that a particle attached twice keeps both of its attachments, and
that an attachment is undone with
its removal from the SoftBodyAttachments component (removing the
component detaches every particle):
Each entry of the SoftBodyAttachments component attaches one particle to the entity of a
rigid-body (or of a cluster). Each time the component changes, the attachments of the
soft-body are updated to match it: the attachments that didn't change keep their anchor, and the ones targeting an
entity which rigid-body isn't created yet are retried on the next frames:
- Example 2D
- Example 3D
// Attach the last particle of a rope to a rigid box, at the particle's position.
let weight = commands
.spawn((
Transform::from_xyz(12.0, 8.6, 0.0),
RigidBody::Dynamic,
Collider::cuboid(0.3, 0.3),
ColliderMassProperties::Density(2.0),
))
.id();
commands.spawn((
Rope,
Transform::from_xyz(8.0, 9.0, 0.0),
SoftBody::rope(Vec2::ZERO, Vec2::new(4.0, 0.0), 25).map(|builder| {
builder
.pinned_particles([0])
.softness(SpringCoefficients::new(40.0, 1.0))
}),
SoftBodyAttachments(vec![SoftBodyAttachment {
particle: 24,
body: weight,
}]),
));
// Attach the last particle of a rope to a rigid ball, at the particle's position.
let ball = commands
.spawn((
Transform::from_xyz(2.5, 4.7, 3.0),
RigidBody::Dynamic,
Collider::ball(0.25),
ColliderMassProperties::Density(2.0),
))
.id();
commands.spawn((
Rope,
Transform::from_xyz(-0.5, 5.0, 3.0),
SoftBody::rope(Vec3::ZERO, Vec3::new(3.0, 0.0, 0.0), 30).map(|builder| {
builder
.pinned_particles([0])
.softness(SpringCoefficients::new(40.0, 1.0))
}),
SoftBodyAttachments(vec![SoftBodyAttachment {
particle: 29,
body: ball,
}]),
));