diff options
author | Camille Mohr-Daurat <pouleyKetchoup@gmail.com> | 2021-10-22 12:58:18 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-22 12:58:18 -0700 |
commit | d47481f190cfbebb1494f7ded2f250f1cd5e4bcd (patch) | |
tree | d5efc8c3481527c59a0745952698b3e2a48b9ad9 | |
parent | 3d92de9e5da5f51f6569769ee1b2305325593065 (diff) | |
parent | 2275e264766d7866b52c573c8a10333f9916794f (diff) |
Merge pull request #54124 from Uxeron/CenterOfMassRotationFix
Fix physics body rotating incorrectly around it's center of mass
-rw-r--r-- | servers/physics_2d/godot_body_2d.cpp | 12 |
1 files changed, 4 insertions, 8 deletions
diff --git a/servers/physics_2d/godot_body_2d.cpp b/servers/physics_2d/godot_body_2d.cpp index c67a63a5dc..56f191c203 100644 --- a/servers/physics_2d/godot_body_2d.cpp +++ b/servers/physics_2d/godot_body_2d.cpp @@ -558,17 +558,13 @@ void GodotBody2D::integrate_velocities(real_t p_step) { real_t total_angular_velocity = angular_velocity + biased_angular_velocity; Vector2 total_linear_velocity = linear_velocity + biased_linear_velocity; - real_t angle = get_transform().get_rotation() + total_angular_velocity * p_step; + real_t angle_delta = total_angular_velocity * p_step; + real_t angle = get_transform().get_rotation() + angle_delta; Vector2 pos = get_transform().get_origin() + total_linear_velocity * p_step; - real_t center_of_mass_distance = center_of_mass.length(); - if (center_of_mass_distance > CMP_EPSILON) { + if (center_of_mass.length_squared() > CMP_EPSILON2) { // Calculate displacement due to center of mass offset. - real_t prev_angle = get_transform().get_rotation(); - real_t angle_base = Math::atan2(center_of_mass.y, center_of_mass.x); - Vector2 point1(Math::cos(angle_base + prev_angle), Math::sin(angle_base + prev_angle)); - Vector2 point2(Math::cos(angle_base + angle), Math::sin(angle_base + angle)); - pos += center_of_mass_distance * (point1 - point2); + pos += center_of_mass - center_of_mass.rotated(angle_delta); } _set_transform(Transform2D(angle, pos), continuous_cd_mode == PhysicsServer2D::CCD_MODE_DISABLED); |