dimforge / rapier

2D and 3D physics engines focused on performance.
https://rapier.rs
Apache License 2.0
4.04k stars 250 forks source link

Motorized ImpulseJoint not working when one side is a convex hull smaller than a certain size #401

Open werner291 opened 2 years ago

werner291 commented 2 years ago

Hi, it's me again.

In the code below, I have a dynamic rigid body with a convex hull collider attached to a fixed rigid body.

Note how the smaller cube does not spin when running this code.

If you switch out the collider with the bit that's commented out (so, using the 0.03 value for the vertices), the cube starts spinning!

use bevy::prelude::*;
use bevy_rapier3d::prelude::*;
use bevy_asset_loader::prelude::*;
use bevy_prototype_debug_lines::DebugLinesPlugin;
use bevy_rapier3d::prelude::ComputedColliderShape::{ConvexDecomposition, TriMesh};
use bevy_rapier3d::rapier::dynamics::{JointAxis, RigidBodyType};

fn main() {

    let rapier_configuration = RapierConfiguration {
        gravity: Vec3::new(0.0, -0.1, 0.0),
        ..Default::default()
    };

    App::new()
        .insert_resource(Msaa::default())
        .insert_resource(rapier_configuration)
        .add_plugins(DefaultPlugins)
        .add_plugin(RapierPhysicsPlugin::<NoUserData>::default())
        .add_plugin(RapierDebugRenderPlugin::default())
        .add_startup_system(setup_scene)
        .add_startup_system(setup_camera)
        .run();

}

fn setup_camera(mut commands: Commands) {
    commands.spawn_bundle(Camera3dBundle {
        transform: Transform::from_xyz(0.0, 1.0, 0.5).looking_at(Vec3::new(0.0, 0.0, -0.5), Vec3::Y),
        ..Default::default()
    });
}

fn setup_scene(mut commands: Commands) {

    let main_body = {

        commands
            .spawn()
            .insert_bundle(TransformBundle {
                local: Transform::from_xyz(0.0, 0.0, 0.0),
                ..Default::default()
            })
            .insert(Collider::cuboid(1.0, 0.1, 1.0))
            .insert(RigidBody::Fixed).id()
    };

    {

        let collider = Collider::convex_hull(&[
            Vec3::new(-0.02, -0.02, -0.02),
            Vec3::new(-0.02, -0.02, 0.02),
            Vec3::new(-0.02, 0.02, -0.02),
            Vec3::new(-0.02, 0.02, 0.02),
            Vec3::new(0.02, -0.02, -0.02),
            Vec3::new(0.02, -0.02, 0.02),
            Vec3::new(0.02, 0.02, -0.02),
            Vec3::new(0.02, 0.02, 0.02),
        ]).unwrap();

        // let collider = Collider::convex_hull(&[
        //     Vec3::new(-0.03, -0.03, -0.03),
        //     Vec3::new(-0.03, -0.03, 0.03),
        //     Vec3::new(-0.03, 0.03, -0.03),
        //     Vec3::new(-0.03, 0.03, 0.03),
        //     Vec3::new(0.03, -0.03, -0.03),
        //     Vec3::new(0.03, -0.03, 0.03),
        //     Vec3::new(0.03, 0.03, -0.03),
        //     Vec3::new(0.03, 0.03, 0.03),
        // ]).unwrap();

        let revj = RevoluteJointBuilder::new(Vec3::Y)
            .local_anchor1(Vec3::new(-0.5, 0.3, -0.5))
            .motor_velocity(-1.0, 1.0)
            .build();

        let mut joint = ImpulseJoint::new(main_body, revj);

        commands
            .spawn()
            .insert_bundle(TransformBundle {
                local: Transform::from_xyz(-0.5, 0.3, -0.5),
                ..Default::default()
            })
            .insert(collider)
            .insert(RigidBody::Dynamic)
            .insert(joint)
            .insert(Sleeping::disabled());
    }

}

And here's my dependencies:

[dependencies]
bevy = { version = "0.8.1" }
bevy_obj = { version = "0.8.0" }
bevy_asset_loader = "0.12.1"
bevy_rapier3d = "0.16.2"
bevy_prototype_debug_lines = { version = "0.8.1", features = ["3d"] }