From 5aeb6d91e98acac434b3fcefe76f25d067fb7d0b Mon Sep 17 00:00:00 2001 From: lL1l1 <82986251+lL1l1@users.noreply.github.com> Date: Sat, 18 Jan 2025 18:49:08 -0800 Subject: [PATCH 1/3] Fix `QuatFromXZDirection` for negative dx inputs --- lua/utilities.lua | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/lua/utilities.lua b/lua/utilities.lua index 0b33e22d8e..539fffe778 100644 --- a/lua/utilities.lua +++ b/lua/utilities.lua @@ -487,13 +487,32 @@ end ---@param dz number ---@return Quaternion function QuatFromXZDirection(dx, dz) - -- ang = atan2(dx, dz) -- `dz` is adjacent - -- {0, sin(ang/2), 0, cos(ang/2)} + -- division by zero case + if dx == 0 and dz == 0 then + return UnsafeQuaternion(0, 0, 0, 1) + end + + -- q = (0, sin(ang/2), 0, cos(ang/2)) -- definition of our y-axis rotation quaternion we want to get for angle `ang` + + -- sin(ang/2) = +/-sqrt((1-cos(ang))/2) -- half angle formula for sin. +/- is sign of sin(ang/2) + -- = +/-sqrt( 0.5 - cos(ang)/2 ) + -- = +/-sqrt( 0.5 - a/2h ) -- cos(ang) = adjacent/hypotenuse + -- similar is repeated for cos(ang/2) + local hypot = MathSqrt(dx*dx + dz*dz) - -- use the half-angle formulas local halfCosA = dz / (2 * hypot) local sinHalfA = MathSqrt(0.5 - halfCosA) local cosHalfA = MathSqrt(0.5 + halfCosA) + + -- Resolve the +/- part of our result: + -- sign of sin(ang/2) is + for ang in (0, 2pi) + -- sign of cos(ang/2) is + for ang in (0, pi) and - for ang in (pi, 2pi) + -- ang in (pi, 2pi) is when dx is negative, so negate cosHalfA in that case + + if dx < 0 then + return UnsafeQuaternion(0, sinHalfA, 0, -cosHalfA) + end + return UnsafeQuaternion(0, sinHalfA, 0, cosHalfA) end From df6cf7d7a5e1c8cc05e8b6c233930cb16fe686ef Mon Sep 17 00:00:00 2001 From: lL1l1 <82986251+lL1l1@users.noreply.github.com> Date: Sat, 18 Jan 2025 18:49:41 -0800 Subject: [PATCH 2/3] Use GetPosXYZ in Unit RotateTowards --- lua/sim/Unit.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/sim/Unit.lua b/lua/sim/Unit.lua index 94503d4e40..19d0e04f0a 100644 --- a/lua/sim/Unit.lua +++ b/lua/sim/Unit.lua @@ -2262,8 +2262,8 @@ Unit = ClassUnit(moho.unit_methods, IntelComponent, VeterancyComponent, DebugUni ---@param self Unit ---@param tpos Vector RotateTowards = function(self, tpos) - local pos = self:GetPosition() - local dx, dz = tpos[1] - pos[1], tpos[3] - pos[3] + local pX, _, pZ = self:GetPositionXYZ() + local dx, dz = tpos[1] - pX, tpos[3] - pZ self:SetOrientation(utilities.QuatFromXZDirection(dx, dz), true) end, From e475b21beb0ae093a23315eb65eda7f61156363c Mon Sep 17 00:00:00 2001 From: lL1l1 <82986251+lL1l1@users.noreply.github.com> Date: Sat, 18 Jan 2025 19:06:47 -0800 Subject: [PATCH 3/3] Create fix.6630.md --- changelog/snippets/fix.6630.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/snippets/fix.6630.md diff --git a/changelog/snippets/fix.6630.md b/changelog/snippets/fix.6630.md new file mode 100644 index 0000000000..00384cf6c6 --- /dev/null +++ b/changelog/snippets/fix.6630.md @@ -0,0 +1 @@ +- (#6630) Fix ACUs spawned on the right side of the map not facing towards the middle of the map.