From a083b76ec421aa5050099f1ffb35627f313b0d7d Mon Sep 17 00:00:00 2001 From: Andrew James Date: Sat, 2 Jul 2022 10:47:14 +1000 Subject: [PATCH] Avoid casting unnecessarily in worldToNormalScreen (#4852) This doesn't trigger warnings on msvc, not sure about other compilers. Also added asserts to all functions performing rotations to match Rotate() --- Source/engine/displacement.hpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Source/engine/displacement.hpp b/Source/engine/displacement.hpp index 4ebf8da00..7229084ae 100644 --- a/Source/engine/displacement.hpp +++ b/Source/engine/displacement.hpp @@ -127,6 +127,7 @@ struct DisplacementOf { */ constexpr DisplacementOf worldToScreen() const { + static_assert(std::is_signed::value, "DeltaT must be signed for transformations involving a rotation"); return { (deltaY - deltaX) * 32, (deltaY + deltaX) * -16 }; } @@ -139,6 +140,7 @@ struct DisplacementOf { */ constexpr DisplacementOf screenToWorld() const { + static_assert(std::is_signed::value, "DeltaT must be signed for transformations involving a rotation"); return { (2 * deltaY + deltaX) / -64, (2 * deltaY - deltaX) / -64 }; } @@ -153,6 +155,7 @@ struct DisplacementOf { constexpr DisplacementOf screenToLight() const { + static_assert(std::is_signed::value, "DeltaT must be signed for transformations involving a rotation"); return { (2 * deltaY + deltaX) / 8, (2 * deltaY - deltaX) / 8 }; } @@ -163,12 +166,13 @@ struct DisplacementOf { */ [[nodiscard]] Displacement worldToNormalScreen() const { + static_assert(std::is_signed::value, "DeltaT must be signed for transformations involving a rotation"); // Most transformations between world and screen space take shortcuts when scaling to simplify the math. This // routine is typically used with missiles where we want a normal vector that can be multiplied with a target // velocity (given in pixels). We could normalize the vector first but then we'd need to scale it during // rotation from world to screen space. To save performing unnecessary divisions we rotate first without // correcting the scaling. This gives a vector in elevation projection aligned with screen space. - Displacement rotated { static_cast(deltaY) - static_cast(deltaX), -(static_cast(deltaY) + static_cast(deltaX)) }; + DisplacementOf rotated { deltaY - deltaX, -(deltaY + deltaX) }; // then normalize this vector Displacement rotatedAndNormalized = rotated.normalized(); // and finally scale the y axis to bring it to isometric projection