From 27f9f35efb0406765859ec2aee96356b6dd09977 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sat, 24 Jul 2021 23:38:42 +0100 Subject: [PATCH] Fix incorrect world height to screen depth conversion. --- OpenRA.Game/Graphics/WorldRenderer.cs | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/OpenRA.Game/Graphics/WorldRenderer.cs b/OpenRA.Game/Graphics/WorldRenderer.cs index a0162f3199..9cd3d1eac2 100644 --- a/OpenRA.Game/Graphics/WorldRenderer.cs +++ b/OpenRA.Game/Graphics/WorldRenderer.cs @@ -21,7 +21,7 @@ namespace OpenRA.Graphics public sealed class WorldRenderer : IDisposable { public static readonly Func RenderableZPositionComparisonKey = - r => ZPosition(r.Pos, r.ZOffset); + r => r.Pos.Y + r.Pos.Z + r.ZOffset; public readonly Size TileSize; public readonly int TileScale; @@ -359,7 +359,13 @@ namespace OpenRA.Graphics public float3 Screen3DPosition(WPos pos) { - var z = ZPosition(pos, 0) * (float)TileSize.Height / TileScale; + // The projection from world coordinates to screen coordinates has + // a non-obvious relationship between the y and z coordinates: + // * A flat surface with constant y (e.g. a vertical wall) in world coordinates + // transforms into a flat surface with constant z (depth) in screen coordinates. + // * Increasing the world y coordinate increases screen y and z coordinates equally. + // * Increases the world z coordinate decreases screen y but doesn't change screen z. + var z = pos.Y * (float)TileSize.Height / TileScale; return new float3((float)TileSize.Width * pos.X / TileScale, (float)TileSize.Height * (pos.Y - pos.Z) / TileScale, z); } @@ -400,16 +406,6 @@ namespace OpenRA.Graphics return new int2((int)Math.Round(xyz.X), (int)Math.Round(xyz.Y)); } - public float ScreenZPosition(WPos pos, int offset) - { - return ZPosition(pos, offset) * (float)TileSize.Height / TileScale; - } - - static int ZPosition(WPos pos, int offset) - { - return pos.Y + pos.Z + offset; - } - /// /// Returns a position in the world that is projected to the given screen position. /// There are many possible world positions, and the returned value chooses the value with no elevation.