dimforge / rapier

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

Cuboid Collider Gets Stuck After Contact with Polyline Collider #643

Open Mutefish0 opened 1 month ago

Mutefish0 commented 1 month ago

A cuboid collider attached to a dynamic rigidbody gets stuck after contacting a polyline collider attached to a fixed rigidbody.

As shown in the video: when moving left and right on the square polyline collider, it gets stuck at the corners, but moving on the cuboid collider works fine.

https://github.com/dimforge/rapier/assets/15227926/249ea285-1f4e-469b-b747-7f5ddc20d267

Reproduce

Modify the examples2d/character_controller2.rs file to the following code, then run it.

use rapier2d::prelude::*;
use rapier_testbed2d::Testbed;
use rapier_testbed2d::{KeyCode};

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

    /*
     * Ground
     */
    let ground_size = 10.0;
    let ground_height = 0.1;

    let rigid_body = RigidBodyBuilder::fixed().translation(vector![0.0, -ground_height]);
    let floor_handle = bodies.insert(rigid_body);
    let collider = ColliderBuilder::cuboid(ground_size, ground_height);
    colliders.insert_with_parent(collider, floor_handle, &mut bodies);

     /*
     *  Polyline Collider 
     */

    let mut points = Vec::new();
    points.push(point![0.0, 0.0]);
    points.push(point![0.0, 2.0]);
    points.push(point![2.0, 2.0]);
    points.push(point![2.0, 0.0]);
    points.push(point![0.0, 0.0]);

    let rigid_body = RigidBodyBuilder::fixed();
    let handle = bodies.insert(rigid_body);
    let collider: ColliderBuilder = ColliderBuilder::polyline(points, None);
    colliders.insert_with_parent(collider, handle, &mut bodies);
     /*
     *  Box Collider 
     */
    let rigid_body = RigidBodyBuilder::fixed().translation(vector![5.0, 1.0]);
    let handle = bodies.insert(rigid_body);
    let collider = ColliderBuilder::cuboid(1.0, 1.0);
    colliders.insert_with_parent(collider, handle, &mut bodies);

    /*
     * Character we will control manually.
     */
    let rb = RigidBodyBuilder::dynamic().lock_rotations().translation(vector![-3.0, 5.0]);
    let character_handle = bodies.insert(rb);
    let collider = ColliderBuilder::cuboid(0.5, 0.5).friction(0.0).restitution(0.0).restitution_combine_rule(CoefficientCombineRule::Multiply).friction_combine_rule(CoefficientCombineRule::Multiply);
    colliders.insert_with_parent(collider, character_handle, &mut bodies);

    /*
     * Set up the testbed.
     */
    testbed.set_world(bodies, colliders, impulse_joints, multibody_joints);
    //testbed.set_character_body(character_handle);
    testbed.look_at(point![0.0, 1.0], 100.0);

    testbed.add_callback(move |gfx, physics, _, _| {
        let Some(gfx) = gfx else { return };

        for key in gfx.keys().get_pressed() {
            match *key {
                KeyCode::ArrowRight => {
                    let rb = physics.bodies.get_mut(character_handle).unwrap();

                    let vel = rb.linvel();

                    rb.set_linvel(vector![2.0, vel.y], true);
                }
                KeyCode::ArrowLeft => {
                    let rb = physics.bodies.get_mut(character_handle).unwrap();
                    let vel = rb.linvel();
                    rb.set_linvel(vector![-2.0, vel.y], true);
                }

                KeyCode::Space => {
                    let rb = physics.bodies.get_mut(character_handle).unwrap();
                    let vel = rb.linvel();
                    rb.set_linvel(vector![vel.x, 6.0], true);
                }

                _ => {}
            }
        }

        for key in gfx.keys().get_just_released() {
            match *key {
                KeyCode::ArrowRight => {
                    let rb = physics.bodies.get_mut(character_handle).unwrap();

                    let vel = rb.linvel();

                    rb.set_linvel(vector![0.0, vel.y], true);
                }
                KeyCode::ArrowLeft => {
                    let rb = physics.bodies.get_mut(character_handle).unwrap();
                    let vel = rb.linvel();
                    rb.set_linvel(vector![0.0, vel.y], true);
                }

                _ => {}
            }
        }   
    });
}
sebcrozet commented 1 month ago

Thank you for providing a test code. This looks like what is commonly known as the "internal edge problem" (or "shadow collisions") where the cube is hitting a vertex or the backface of the polyline segment. While we have something in place for addressing this in 3D, it hasn’t been implemented in 2D yet.

Mutefish0 commented 1 month ago

Thank you for the quick response and explanation.

I am setting up colliders for a tilemap in a 2D platformer game. Using polylines is more convenient, but I am encountering this issue. Is there any workaround or recommended approach to mitigate this issue in 2D for the time being? Any guidance would be greatly appreciated!