SpringJoint - Object orbits mouse cursor during SpringJoint dragging
- Dominant language
- Rust
- Stars
- 5.7k
- Forks
- 387
- Avg merge
- 4d 23h
- Merged PRs (30d)
- 6
Description
**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
Contributor guide
Assessment
This issue has not been assessed yet.