Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Api improvements and fixes #304

Merged
merged 23 commits into from
Mar 20, 2022
Merged

Api improvements and fixes #304

merged 23 commits into from
Mar 20, 2022

Conversation

sebcrozet
Copy link
Member

@sebcrozet sebcrozet commented Mar 20, 2022

This is a combination of several pieces of work I needed to land before releasing the next non-alpha version. The diff is quite large because it involves several semi-automatic small modifications after some API changes (like removing the need to call .build() on all the builders).

In particular this:

  • Improves the stability of joint motors.
  • Improves the API of joints thanks to more getters/setters/builders.
  • Fix the parallel version of Rapier (fix Panic when using "parallel" feature #289).
  • Fix panics related to rigid-bodies with locked rotations/translations.

In addition, this introduces a few semantic modifications that result from recurrent user feedbacks. In particular:

Simplified event handling

The contact/intersection event system has been simplified. The difference between contact events and intersection events (for sensor colliders) was often an unnecessary source of confusion for newcomers. Even our ContactEvent and IntersectionEvent structs where mostly identical. There is now a single event type CollisionEvent that can be triggered by sensor and non-sensor colliders.

In addition, removing a collider did not trigger any ContactEvent::Stopped before. This made it very difficult for users to implement event handler based on, e.g., counters that is incremented/decremented when an event happens. This PR allows Rapier to generate events for colliders that have been removed before the current simulation step (fix #299).

Don’t clear external forces

Currently, forces applied to a rigid-body are automatically cleared at the end of each timestep. This would cause various unexpected behavior (fix #278), and would make writing a custom substepping strategy (manually calling PhysicsPipeline::step multiple times during one frame) quite difficult because the forces wouldn’t persist between substeps.

Therefore forces are no longer cleared automatically and a RigidBody::reset_forces method have been added to clear manually all the forces applied to a rigid-body.

This change also deepens the contrast between applying forces and applying impulses. Impulses are instantaneous velocity changes whereas forces are now continuous forces that persist in time. This also makes sleeping easier to manage: as long as you don’t modify forces there is no reason to manually wake-up the rigid-body. So calling .add_force(force, true) only when forces changes allows waking the rigid-body only when it is needed.

Don’t reset the velocity of kinematic bodies

Currently, the velocity of kinematic bodies are automatically reset at the end of each timestep. This means that the velocity of a velocity-based kinematic body has to be set at each frame, even if that velocity remains constant. Like forces auto-clearing, this makes custom sub-stepping strategies very difficult to implement.

With this PR, kinematic bodies velocities won’t be cleared automatically any more.

This also means that the velocity of kinematic bodies (including the velocity we automatically compute for position-based kinematic bodies) can now be read after the call to PhysicsPipeline::step, which can be useful for writing some user logic based on that velocity (fix dimforge/bevy_rapier#127).

Changelog

Fixed

  • Fix the simulation when the parallel feature is enabled.
  • Fix bug where damping would not be applied properly to some bodies.
  • Fix panics caused by various situations (contact or joints) involving rigid-bodies with locked translations/rotations.

Modified

  • Rename JointHandle to ImpulseJointHandle.
  • Rename RigidBodyMassPropsFlags to LockedAxes.
  • Rename RigidsBody::apply_force, ::apply_torque, ::apply_force_at_point to ::add_force,
    ::add_torque, and ::add_force_at_point to better reflect the fact that they are not cleared at the end
    of the timestep.
  • Rename RigidBodyType::Static to RigidBodyType::Fixed to avoid confusion with the static keyword.
  • All method referring to static rigid-bodies now use fixed instead of static.
  • Rename RigidBodyBuilder::new_static, new_kinematic_velocity_based, new_kinematic_velocity_based to
    RigidBodyBuilder::fixed, kinematic_velocity_based, kinematic_velocity_based.
  • The ContactEvent and IntersectionEvent have been replaced by a single enum CollisionEvent in order
    to simplify the user’s event handling.
  • The ActiveEvents::CONTACT_EVENTS and ActiveEvents::INTERSECTION_EVENTS flags have been replaced by a single
    flag ActiveEvents::COLLISION_EVENTS.
  • Joint motors no longer have a VelocityBased model. The new choices are AccelerationBased and ForceBased
    which are more stable.
  • Calling the .build() function from builders (RigidBodyBuilder, ColliderBuilder, etc.) is no longer necessary
    whan adding them to sets. It is automatically called thanks to Into<_> implementations.

Semantic modifications

These are changes in the behavior of the physics engine that are not necessarily
reflected by an API change:

  • RigidBody::set_linvel and RigidBody::set_angvel no longer modify the velocity of static bodies.
  • RigidBody::set_body_type will reset the velocity of a rigid-body to zero if it is static.
  • Don’t automatically clear forces at the end of a timestep.
  • Don’t reset the velocity of kinematic bodies to zero at the end of the timestep.
  • Events CollisionEvent::Stopped are now generated after a collider is removed.

@sebcrozet sebcrozet merged commit 0ac35e1 into master Mar 20, 2022
@sebcrozet sebcrozet deleted the api-improvements branch March 20, 2022 20:49
@sebcrozet sebcrozet mentioned this pull request Apr 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant