dimforge / rapier

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

Erased angular velocity since v0.18 #587

Closed Nicolas-Ferre closed 3 months ago

Nicolas-Ferre commented 5 months ago

Issue Since version 0.18, when the angvel of a RigidBody is set to a value different than 0, it comes back to 0 after execution of PhysicsPipeline::step. This problem is not present in version 0.17.

Bug reproduction This code can be used to reproduce the issue:

use rapier2d::na::Vector2;
use rapier2d::prelude::*;
use std::f32::consts::FRAC_PI_4;

#[derive(Default)]
pub(crate) struct Physics {
    bodies: RigidBodySet,
    colliders: ColliderSet,
    island_manager: IslandManager,
    impulse_joints: ImpulseJointSet,
    multibody_joints: MultibodyJointSet,
    integration_parameters: IntegrationParameters,
    physics_pipeline: PhysicsPipeline,
    broad_phase: BroadPhase,
    narrow_phase: NarrowPhase,
    ccd_solver: CCDSolver,
}

fn main() {
    let mut physics = Physics::default();
    let body_handle = physics.bodies.insert(
        RigidBodyBuilder::new(RigidBodyType::Dynamic)
            .can_sleep(false)
            .angvel(FRAC_PI_4)
            .build(),
    );
    physics.integration_parameters.dt = 2.;
    physics.physics_pipeline.step(
        &Vector2::zeros(),
        &physics.integration_parameters,
        &mut physics.island_manager,
        &mut physics.broad_phase,
        &mut physics.narrow_phase,
        &mut physics.bodies,
        &mut physics.colliders,
        &mut physics.impulse_joints,
        &mut physics.multibody_joints,
        &mut physics.ccd_solver,
        None,
        &(),
        &(),
    );
    assert_eq!(physics.bodies[body_handle].angvel(), FRAC_PI_4);
}

The code panics in the following conditions:

Additional notes It seems that the angular velocity is erased when the following line is run: https://github.com/dimforge/rapier/blob/da92e5c2837b27433286cf0dd9d887fd44dda254/src/dynamics/solver/velocity_solver.rs#L307

sebcrozet commented 4 months ago

The reason why the angular velocity is being erased is because your rigid-body doesn’t have any mass/angular inertia, which is a requirement for the new solver (and is more correct physically speaking).

Nicolas-Ferre commented 3 months ago

Thank you very much for the explanation, I added an angular inertia and it indeed works well now.