From a479d7246bc1cf8cc8de95e736563a707b84e20d Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Sat, 17 Jul 2021 16:36:27 +0200 Subject: [PATCH] Reworked coordinate and rotation conversion from Quake to Unity. It still feels weird, but entity models are now seemingly oriented correctly, and some strange edge cases with entities rotating in the wrong direction have been cleared up. --- Assets/Scripts/Data/QExtensions.cs | 8 +++++--- Assets/Scripts/Modules/RenderModule.cs | 8 ++++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/Assets/Scripts/Data/QExtensions.cs b/Assets/Scripts/Data/QExtensions.cs index 8580da2..d93e3f4 100644 --- a/Assets/Scripts/Data/QExtensions.cs +++ b/Assets/Scripts/Data/QExtensions.cs @@ -48,16 +48,18 @@ public static class QExtensions public static Vector3 ToUnity(this Vector3 vec) { - return new Vector3(vec.x, vec.z, vec.y); + return new Vector3(vec.y, vec.z, -vec.x); } public static Vector3 ToUnityPosition(this QVec3 origin) { - return new Vector3(origin.x, origin.z, origin.y); + return new Vector3(origin.y, origin.z, -origin.x); } public static Quaternion ToUnityRotation(this QVec3 angles) { - return Quaternion.Euler(angles.x, -angles.y, -angles.z); + return Quaternion.AngleAxis(-angles.y, Vector3.up) + * Quaternion.AngleAxis(angles.x, Vector3.right) + * Quaternion.AngleAxis(angles.z, Vector3.forward); } } diff --git a/Assets/Scripts/Modules/RenderModule.cs b/Assets/Scripts/Modules/RenderModule.cs index 9470c54..da350b9 100644 --- a/Assets/Scripts/Modules/RenderModule.cs +++ b/Assets/Scripts/Modules/RenderModule.cs @@ -74,8 +74,12 @@ public partial class RenderModule if (cam == null) return; - angles.y -= 90; // Don't know why this correction is necessary - + // Don't know exactly why this correction is necessary. + // Mixing Quake and Unity's coordinate systems creates a bit of a mess. With the current set of conversions + // we at least get everything to look right in every case I've tested, but it still seems a bit off somehow. + angles.y += 180; + angles.z = -angles.z; + cam.transform.position = origin.ToUnityPosition(); cam.transform.rotation = angles.ToUnityRotation(); }