From e5b73930ce4f17ca96e38309ad719c7a6274427b Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Sun, 30 Apr 2023 15:33:46 +0200 Subject: [PATCH] Center tile heightmap around (0,0). --- scenes/TileWorld.cs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/scenes/TileWorld.cs b/scenes/TileWorld.cs index 2b9afab..3a23441 100644 --- a/scenes/TileWorld.cs +++ b/scenes/TileWorld.cs @@ -40,9 +40,9 @@ public class TileWorld : Spatial Heightmap.Create((int)Size.x, (int)Size.y, false, Image.Format.Rf); Heightmap.Lock(); - foreach (int coord_x in Enumerable.Range(0, (int)Size.x)) + foreach (int coord_x in Enumerable.Range(-(int) Size.x / 2, (int)Size.x)) { - foreach (int coord_y in Enumerable.Range(0, (int)Size.y)) + foreach (int coord_y in Enumerable.Range(-(int) Size.y / 2, (int)Size.y)) { SetHeightAtOffset(new Vector2(coord_x, coord_y), 5f); } @@ -75,8 +75,8 @@ public class TileWorld : Spatial public bool IsOffsetCoordValid(Vector2 offset_coord) { - return ((int)Math.Clamp(offset_coord.x, 0, Size.x) == (int)offset_coord.x - && (int)Math.Clamp(offset_coord.y, 0, Size.y) == (int)offset_coord.y); + return ((int)Math.Clamp(offset_coord.x, -Size.x / 2, Size.x / 2 - 1) == (int)offset_coord.x + && (int)Math.Clamp(offset_coord.y, -Size.y / 2, Size.y / 2 - 1) == (int)offset_coord.y); } @@ -98,7 +98,9 @@ public class TileWorld : Spatial return; } - Heightmap.SetPixel((int) offset_coord.x, (int) offset_coord.y, new Color(height / HeightScale, 0f, 0f)); + Vector2 texture_coord = offset_coord + Size / 2; + + Heightmap.SetPixel((int) texture_coord.x, (int) texture_coord.y, new Color(height / HeightScale, 0f, 0f)); } public float GetHeightAtOffset(Vector2 offset_coord) @@ -108,7 +110,9 @@ public class TileWorld : Spatial return 0; } - return Heightmap.GetPixel((int)offset_coord.x, (int)offset_coord.y).r * HeightScale - HeightScale * 0.5f ; + Vector2 texture_coord = offset_coord + Size / 2; + + return Heightmap.GetPixel((int)texture_coord.x, (int)texture_coord.y).r * HeightScale - HeightScale * 0.5f ; }