From a9a325dc7b65ce39dd3587cd4fb0e9ae60ecf20d Mon Sep 17 00:00:00 2001 From: Andrew James Date: Mon, 30 May 2022 21:39:41 +1000 Subject: [PATCH] Document/simplify the UpdateMissileVelocity logic The multiplication by `1 << 21` was unnecessary, with the way the division is performed it doesn't add precision. Missile velocities are 16 bit fixed values so split out the normalisation step in order to make it clearer what the multiplication and use of `v` ends up doing. --- Source/missiles.cpp | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Source/missiles.cpp b/Source/missiles.cpp index 922d220a2..7365d07f9 100644 --- a/Source/missiles.cpp +++ b/Source/missiles.cpp @@ -107,11 +107,15 @@ void UpdateMissileVelocity(Missile &missile, Point destination, int v) if (missile.position.tile == destination) return; - double dxp = (destination.x + missile.position.tile.y - missile.position.tile.x - destination.y) * (1 << 21); - double dyp = (destination.y + destination.x - missile.position.tile.x - missile.position.tile.y) * (1 << 21); + // rotate so that +x leads to the right of screen and +y leads to the bottom (screen space but without the scaling factor) + double dxp = (destination.x + missile.position.tile.y - missile.position.tile.x - destination.y); + double dyp = (destination.y + destination.x - missile.position.tile.x - missile.position.tile.y); double dr = sqrt(dxp * dxp + dyp * dyp); - missile.position.velocity.deltaX = static_cast((dxp * (v << 16)) / dr); - missile.position.velocity.deltaY = static_cast((dyp * (v << 15)) / dr); + // velocity is stored in screen coordinates so apply the scaling factor to the y axis while normalizing. + double normalizedX = dxp / dr; + double normalizedY = dyp / dr / 2; + missile.position.velocity.deltaX = static_cast(normalizedX * (v << 16)); + missile.position.velocity.deltaY = static_cast(normalizedY * (v << 16)); } /**