Path: blob/master/thirdparty/jolt_physics/patches/0004-backport-upstream-commit-e0a6a9a16.patch
45998 views
diff --git a/thirdparty/jolt_physics/Jolt/Math/Quat.h b/thirdparty/jolt_physics/Jolt/Math/Quat.h1index 6e7e3bef6c..1971e2ddf1 1006442--- a/thirdparty/jolt_physics/Jolt/Math/Quat.h3+++ b/thirdparty/jolt_physics/Jolt/Math/Quat.h4@@ -111,6 +111,9 @@ public:5/// Get axis and angle that represents this quaternion, outAngle will always be in the range \f$[0, \pi]\f$6JPH_INLINE void GetAxisAngle(Vec3 &outAxis, float &outAngle) const;78+ /// Calculate angular velocity given that this quaternion represents the rotation that is reached after inDeltaTime when starting from identity rotation9+ JPH_INLINE Vec3 GetAngularVelocity(float inDeltaTime) const;10+11/// Create quaternion that rotates a vector from the direction of inFrom to the direction of inTo along the shortest path12/// @see https://www.euclideanspace.com/maths/algebra/vectors/angleBetween/index.htm13JPH_INLINE static Quat sFromTo(Vec3Arg inFrom, Vec3Arg inTo);14diff --git a/thirdparty/jolt_physics/Jolt/Math/Quat.inl b/thirdparty/jolt_physics/Jolt/Math/Quat.inl15index 57e1947b30..7c160a5f23 10064416--- a/thirdparty/jolt_physics/Jolt/Math/Quat.inl17+++ b/thirdparty/jolt_physics/Jolt/Math/Quat.inl18@@ -151,6 +151,26 @@ void Quat::GetAxisAngle(Vec3 &outAxis, float &outAngle) const19}20}2122+Vec3 Quat::GetAngularVelocity(float inDeltaTime) const23+{24+ JPH_ASSERT(IsNormalized());25+26+ // w = cos(angle / 2), ensure it is positive so that we get an angle in the range [0, PI]27+ Quat w_pos = EnsureWPositive();28+29+ // The imaginary part of the quaternion is axis * sin(angle / 2),30+ // if the length is small use the approximation sin(x) = x to calculate angular velocity31+ Vec3 xyz = w_pos.GetXYZ();32+ float xyz_len_sq = xyz.LengthSq();33+ if (xyz_len_sq < 4.0e-4f) // Max error introduced is sin(0.02) - 0.02 = 7e-5 (when w is near 1 the angle becomes more inaccurate in the code below, so don't make this number too small)34+ return (2.0f / inDeltaTime) * xyz;35+36+ // Otherwise calculate the angle from w = cos(angle / 2) and determine the axis by normalizing the imaginary part37+ // Note that it is also possible to calculate the angle through angle = 2 * atan2(|xyz|, w). This is more accurate but also 2x as expensive.38+ float angle = 2.0f * ACos(w_pos.GetW());39+ return (xyz / (sqrt(xyz_len_sq) * inDeltaTime)) * angle;40+}41+42Quat Quat::sFromTo(Vec3Arg inFrom, Vec3Arg inTo)43{44/*45diff --git a/thirdparty/jolt_physics/Jolt/Physics/Body/MotionProperties.inl b/thirdparty/jolt_physics/Jolt/Physics/Body/MotionProperties.inl46index 474ab15c18..e6caae61f5 10064447--- a/thirdparty/jolt_physics/Jolt/Physics/Body/MotionProperties.inl48+++ b/thirdparty/jolt_physics/Jolt/Physics/Body/MotionProperties.inl49@@ -17,10 +17,7 @@ void MotionProperties::MoveKinematic(Vec3Arg inDeltaPosition, QuatArg inDeltaRot50mLinearVelocity = LockTranslation(inDeltaPosition / inDeltaTime);5152// Calculate required angular velocity53- Vec3 axis;54- float angle;55- inDeltaRotation.GetAxisAngle(axis, angle);56- mAngularVelocity = LockAngular(axis * (angle / inDeltaTime));57+ mAngularVelocity = LockAngular(inDeltaRotation.GetAngularVelocity(inDeltaTime));58}5960void MotionProperties::ClampLinearVelocity()616263