soft_body_attachments
A particle can also be attached directly to a rigid-body (SoftBody.attachParticle), 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
SoftBody.detachParticle:
- Example 2D
- Example 3D
// Attach the last particle of a rope to a rigid box, at the particle's position.
let ropeDesc = RAPIER.SoftBodyDesc.rope({ x: 8.0, y: 9.0 }, { x: 12.0, y: 9.0 }, 25)
.setPinnedParticles([0])
.setSoftness(40.0, 1.0);
let rope = world.createSoftBody(ropeDesc);
let last = rope.particlePosition(24);
let weight = world.createRigidBody(RAPIER.RigidBodyDesc.dynamic().setTranslation(last.x, last.y - 0.4));
world.createCollider(RAPIER.ColliderDesc.cuboid(0.3, 0.3).setDensity(2.0), weight);
rope.attachParticle(24, weight);
// Attach the last particle of a rope to a rigid ball, at the particle's position.
let ropeDesc = RAPIER.SoftBodyDesc.rope({ x: -0.5, y: 5.0, z: 3.0 }, { x: 2.5, y: 5.0, z: 3.0 }, 30)
.setPinnedParticles([0])
.setSoftness(40.0, 1.0);
let rope = world.createSoftBody(ropeDesc);
let last = rope.particlePosition(29);
let ball = world.createRigidBody(RAPIER.RigidBodyDesc.dynamic().setTranslation(last.x, last.y - 0.3, last.z));
world.createCollider(RAPIER.ColliderDesc.ball(0.25).setDensity(2.0), ball);
rope.attachParticle(29, ball);