Trying to have composite rigidbodies 2D and imprecision issues with linear velocity. #38339
Description
Godot version:
3.2.1
OS/device including version:
Windows 10
Issue description:
I've been trying to have composite rigidbodies in 2D, AKA rigidbodies that can enter other bigger rigidbodies and be tied to their parent's accelerations.
Rules are simple: velocities are conserved when entering and leaving. Acceleration of a rigidbodies affect the rigidbodies inside it. Children rigidbodies can freely move but if they're immobile they'll always stick to the same local position (like any non physic node).
How it is achieved: Every _integrate_forces call, I remove the parent's previous velocities and add the new ones. It mostly works with linear velocities, but there's a problem with angular velocities.
The parent's angular velocity has to be translated into children following a circle.
To achieve that I roughly do:
var local_origin = state.transform.origin - parent.transform.origin
state.linear_velocity -= (local_origin - local_origin.rotated( -parent_previous_angular_velocity * state.step ) ) / state.step
# linear_velocity should be worth its own speed which here is going to be (0, 0)
state.linear_velocity += (local_origin.rotated( parent.angular_velocity * state.step ) - local_origin) / state.step
Which visually works, but I am getting tiny errors as soon as the parent starts rotating.
If the parent is at position x 600 and child at 650, it'll compute a local_origin of 50 and a linear_velocity of -0.000229, and I should get the next turn a position of the child of 649,999996.
But next turn it shows me a perfect 650 again, I have an error of -0.000004 that I can't compensate for. Similar errors appear on the y axis as well.
I've tried many things to mitigate the computations errors but they don't seem to change anything. When I compute myself the position it should have using the linear velocity I get the correct position. The angular and linear damps are 0, and I've code that is meant to compensate both when they're not 0 and they work well without errors.
Is it a bug? Is there something I am doing wrong?
Is there a better way to achieve what I've been trying to achieve? I've seen issues suggesting this functionality will be present in godot 4.0 but I've been pulling my hair for at least 15h to try and find a good way to do it. I really want my game to have little ships docked in big ships.
Thanks!