rapier icon indicating copy to clipboard operation
rapier copied to clipboard

SpringJoint - Object orbits mouse cursor during SpringJoint dragging

Open namse opened this issue 1 year ago • 1 comments

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:

  1. The dragged object should follow the mouse cursor's movement relatively directly, potentially with some slight delay or spring using RigidBodyBuilder::dynamic() and ColliderBuilder::cuboid(...)).
  2. Create another RigidBody (Body B) to represent the mouse position (e.g., using RigidBodyBuilder::kinematic_position_based() or fixed()).
  3. 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 SpringJoint between 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, setting rest_length to 0.0, stiffness to 200, damping to critical damping ratio.
  4. 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(...)).
  5. On drag end (e.g., mouse button up):
    • Remove the SpringJoint and the anchor Body B.
  6. 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

namse avatar Apr 30 '25 02:04 namse

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.

luxalpa avatar Jun 28 '25 13:06 luxalpa