SpringJoint - Object orbits mouse cursor during SpringJoint dragging
1. Problem Description:
When implementing a drag-and-drop feature for a RigidBody using a SpringJoint in Rapier 2D, the dragged object does not follow the mouse cursor directly as expected. Instead, it frequently enters an orbital motion (circular or elliptical path) around the cursor's target position. This orbiting behavior is particularly noticeable when the mouse is moved.
2. Expected Behavior:
- The dragged object should follow the mouse cursor's movement relatively directly, potentially with some slight delay or spring using
RigidBodyBuilder::dynamic()andColliderBuilder::cuboid(...)). - Create another
RigidBody(Body B) to represent the mouse position (e.g., usingRigidBodyBuilder::kinematic_position_based()orfixed()). - On drag start (e.g., mouse button down):
- lock rotations (
lock_rotations(true, ..)) and disable gravity (set_gravity_scale(0.0, ..)) for Body A. - Create a
SpringJointbetween Body A (at its local position of mouse point) and the anchor Body B (at its local origin(0,0)). - Use
SpringJointBuilder::new(rest_length, stiffness, damping)to configure the joint, settingrest_lengthto0.0,stiffnessto200,dampingto critical damping ratio.
- lock rotations (
- During drag (e.g., mouse move):
- Update the position of the anchor Body B to the current mouse world coordinates every frame or physics step (using
anchor_body.set_translation(...)).
- Update the position of the anchor Body B to the current mouse world coordinates every frame or physics step (using
- On drag end (e.g., mouse button up):
- Remove the
SpringJointand the anchor Body B.
- Remove the
- Observation: Notice Body A orbiting around Body B's position during step 4 when the mouse is moved.
https://github.com/user-attachments/assets/5b0a5f60-c436-4160-a5e5-977e03411970
I'm having the same problem (although in a very different context). It behaves as if it doesn't try to align all directions at once but instead aligns first into one axis before aligning into the other one, causing it to gain a lot of angular momentum.