0

I would like to connect my sphere to a rigid body, how would I do that with rapier?

fn main() {
  let position = Point3::new(0.0, 0.0, 0.0);
  let sphere = BoundingSphere::new(position, 100.0);
  let rigid_body = RigidBodyBuilder::fixed();
  // rigid_body.set_object(sphere); <- How would I do this?
}
kemicofa ghost
  • 16,349
  • 8
  • 82
  • 131

1 Answers1

0

I suppose the type BoundingSphere is a parry BoundingSphere?

In that case you can't mix and match these with rapier.

How to add rapier collider spheres to RigidBody

I will give an explanation how to add a sphere or multiple spheres to a RigidBody below:

You will need your world objects, for example these:

// The world objects.
let mut bodies = RigidBodySet::new();
let mut colliders = ColliderSet::new();

the rigid body that you already have in the question must be inserted into the world:

// Build the rigid body.
let sphere = RigidBodyBuilder::dynamic().translation(vector![0.0, 50.0, 0.0]);
let sphere_handle = bodies.insert(sphere);

Then you have to create either one compound shape (which I think is closer to your use case) or multiple colliders. With the first option you get:

// Create a compound shape and attach it to a single collider.
let shapes = vec![
    (
        Isometry::identity(),
        SharedShape::ball(10.0),
    )
];

let collider = ColliderBuilder::compound(shapes);
colliders.insert_with_parent(collider, sphere_handle, &mut bodies);

or if you want to attach mutiple shapes / spheres:

// Create a compound shape and attach it to a single collider.
let shapes = vec![
    (
        Isometry::identity(),
        SharedShape::ball(10.0),
    ),
    (
        Isometry::translation(2.0, 10.0, 0.0),
        SharedShape::ball(10.0),
    ),
    (
        Isometry::translation(5.0, 5.0, 5.0),
        SharedShape::ball(10.0),
    )
];

let collider = ColliderBuilder::compound(shapes);
colliders.insert_with_parent(collider, sphere_handle, &mut bodies);

This would be a full example using rapiers' testbed:

use rapier3d::prelude::*;
use rapier_testbed3d::Testbed;

pub fn init_world(testbed: &mut Testbed) {
    // The world objects.
    let mut bodies = RigidBodySet::new();
    let mut colliders = ColliderSet::new();
    let impulse_joints = ImpulseJointSet::new();
    let multibody_joints = MultibodyJointSet::new();

    // (Optional) Add ground plane.
    let rigid_body = RigidBodyBuilder::fixed().translation(vector![0.0, -0.1, 0.0]);
    let handle = bodies.insert(rigid_body);
    let ground = ColliderBuilder::cuboid(50.0, 0.1, 50.0);
    colliders.insert_with_parent(ground, handle, &mut bodies);

    // Build the rigid body.
    let sphere = RigidBodyBuilder::dynamic().translation(vector![0.0, 50.0, 0.0]);
    let sphere_handle = bodies.insert(sphere);

    // Create a compound shape and attach it to a single collider.
    let shapes = vec![
        (
            Isometry::identity(),
            SharedShape::ball(10.0),
        )
    ];

    let collider = ColliderBuilder::compound(shapes);
    colliders.insert_with_parent(collider, sphere_handle, &mut bodies);

    // Testbed only.
    testbed.set_world(bodies, colliders, impulse_joints, multibody_joints);
    testbed.look_at(point![100.0, 100.0, 100.0], Point::origin());
}

which will result in this: Rapier sphere

or that when using multiple spheres: Rapier multiple spheres

frankenapps
  • 5,800
  • 6
  • 28
  • 69