From ef183ff94753698b47c95fa8d5f888bd2fef6a91 Mon Sep 17 00:00:00 2001 From: neumond Date: Sat, 27 Apr 2024 21:10:13 +0300 Subject: [PATCH] Fix loud white nosie after mouse steering (#5812) * Prevent NaN poisoning in right-mouse rotation (fixes loud thruster sounds and inability to turn) * Document savebump changes --- SAVEBUMP.txt | 1 + src/DynamicBody.cpp | 6 ++++++ src/ship/PlayerShipController.cpp | 4 +++- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/SAVEBUMP.txt b/SAVEBUMP.txt index 6641191416f..927d128bae3 100644 --- a/SAVEBUMP.txt +++ b/SAVEBUMP.txt @@ -13,4 +13,5 @@ Regular tasks - data/libs/NameGen.lua: Add new authors to name generator One-time tasks +- DynamicBody.cpp: the std::isnan() check in constructor should be removed - add your one-time tasks here diff --git a/src/DynamicBody.cpp b/src/DynamicBody.cpp index f82f3e390cc..f506e458aa2 100644 --- a/src/DynamicBody.cpp +++ b/src/DynamicBody.cpp @@ -67,6 +67,12 @@ DynamicBody::DynamicBody(const Json &jsonObj, Space *space) : throw SavedGameCorruptException(); } + // fix saves with nans + // SAVEBUMP: This can be removed starting with save version 91 + if (std::isnan(m_angVel.x) || std::isnan(m_angVel.y) || std::isnan(m_angVel.z)) { + m_angVel = vector3d(0.0); + } + m_aiMessage = AIError::AIERROR_NONE; m_decelerating = false; } diff --git a/src/ship/PlayerShipController.cpp b/src/ship/PlayerShipController.cpp index 23353a18235..d35fa3abc50 100644 --- a/src/ship/PlayerShipController.cpp +++ b/src/ship/PlayerShipController.cpp @@ -106,7 +106,9 @@ struct PlayerShipController::Util { auto good_speed = sqrt(2 * max_accel * want_rot) * 0.95; auto frame_speed = want_rot / timeStep; auto tot_speed = std::min(good_speed, frame_speed / 2); - rot_vel = (ship_dir.Cross(dir) * c.m_ship->GetOrient()).Normalized() * tot_speed; + // this cross product often becomes all-zeros (ship_dir coincides with requested dir) + // NormalizedSafe is crucial there to avoid NaN poisoning + rot_vel = (ship_dir.Cross(dir) * c.m_ship->GetOrient()).NormalizedSafe() * tot_speed; } return rot_vel; };