Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix QuatFromXZDirection for negative dx inputs causing ACUs to spawn the wrong direction #6630

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

lL1l1
Copy link
Contributor

@lL1l1 lL1l1 commented Jan 19, 2025

Issue

Initially reported on Discord that ACUs would sometimes not spawn facing the correct direction after the release with #6438.

This is because the math used in the QuatFromXZDirection(dx, dz) function did not properly account for signs, and was giving the wrong output when dx was negative. ACUs on the right side of the map have a negative dx when rotating towards the middle of the map.

Description of the proposed changes

  • Accounts for the sign of -dx
  • Explain the mathematics more thoroughly
  • Account for the division by zero case as it would print Unit ual0001 is attempting to move to an invalid coord to the log about the unit moving to an invalid coord when SetOrientation({0, 1.#INF, 0, 1.#INF}, true) happened.

Testing done on the proposed changes

  • Fill all slots on Crossfire Canal and verify that all ACUs spawn with the correct orientation. Using Crossfire Canal accounts for all quadrants of the unit circle. Make sure to pause the game right after the spawn sequence if using AI, as the AI will move the ACUs.
The follow script tests the differences between the old and new results of RotateTowards, but I didn't bother making the function results exactly the same (there are some switches in sign) because the end result for ACU orientation at the start was fine.
local utilities = import("/lua/utilities.lua")
local oldRotateTowards = function(self, tpos)
    local pos = self.pos
    local rad = math.atan2(tpos[1] - pos[1], tpos[3] - pos[3])
    local function SetRotation(angle)
        local function QuatFromRotation(rotation, x, y, z)
            local angleRot, qw, qx, qy, qz, angle
            angle = 0.00872664625 * rotation
            angleRot = math.sin(angle)
            qw = math.cos(angle)
            qx = x * angleRot
            qy = y * angleRot
            qz = z * angleRot
            return qx, qy, qz, qw
        end
        return QuatFromRotation(angle, 0, 1, 0)
    end
    return SetRotation(rad * (180 / math.pi))
end
LOG(oldRotateTowards({pos = {0, 1, 0}}, {0, 0, 0}))
LOG(repr(utilities.QuatFromXZDirection(0, 0)))

LOG(oldRotateTowards({pos = {0, 1, 0}}, {1, 0, 0}))
LOG(repr(utilities.QuatFromXZDirection(1, 0)))
LOG(oldRotateTowards({pos = {0, 1, 0}}, {0, 0, 1}))
LOG(repr(utilities.QuatFromXZDirection(0, 1)))
LOG(oldRotateTowards({pos = {0, 1, 0}}, {-1, 0, 0}))
LOG(repr(utilities.QuatFromXZDirection(-1, 0)))
LOG(oldRotateTowards({pos = {0, 1, 0}}, {0, 0, -1}))
LOG(repr(utilities.QuatFromXZDirection(0, -1)))

LOG(oldRotateTowards({pos = {0, 1, 0}}, {1, 0, 1}))
LOG(repr(utilities.QuatFromXZDirection(1, 1)))
LOG(oldRotateTowards({pos = {0, 1, 0}}, {1, 0, -1}))
LOG(repr(utilities.QuatFromXZDirection(1, -1)))
LOG(oldRotateTowards({pos = {0, 1, 0}}, {-1, 0, -1}))
LOG(repr(utilities.QuatFromXZDirection(-1, -1)))
LOG(oldRotateTowards({pos = {0, 1, 0}}, {-1, 0, 1}))
LOG(repr(utilities.QuatFromXZDirection(-1, 1)))

Checklist

  • Changes are annotated, including comments where useful
  • Changes are documented in the changelog for the next game version

@lL1l1 lL1l1 added type: bug area: sim Area that is affected by the Simulation of the Game labels Jan 19, 2025
@lL1l1 lL1l1 added this to the Development Iteration I of 2025 milestone Jan 19, 2025
@lL1l1 lL1l1 marked this pull request as ready for review January 19, 2025 03:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: sim Area that is affected by the Simulation of the Game type: bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant