From d6105d9f9a580c3b542c8adbeaee0d3656bc4ebe Mon Sep 17 00:00:00 2001 From: Collin Smith Date: Sat, 19 Oct 2019 15:04:41 -0700 Subject: [PATCH] Improved isometric camera API Documented isometric camera API Implemented pix and tile offsets instead of just offset Made pix and tile offsets private -- pix offset set via offset and tile offset set automatically toTile50 may be deprecated in the future if I cannot find a use for it (tile offset may have superseded it) --- .../com/riiablo/camera/IsometricCamera.java | 43 ++++++++++++++++--- tools/src/com/riiablo/camera/CameraTool.java | 4 +- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/core/src/com/riiablo/camera/IsometricCamera.java b/core/src/com/riiablo/camera/IsometricCamera.java index 4f569ce3..8dc2215e 100644 --- a/core/src/com/riiablo/camera/IsometricCamera.java +++ b/core/src/com/riiablo/camera/IsometricCamera.java @@ -8,13 +8,16 @@ import com.riiablo.util.EngineUtils; public class IsometricCamera extends OrthographicCamera { private final Vector2 tmp = new Vector2(); - public final Vector2 offset = new Vector2(); public final Vector2 position = new Vector2(); + private final Vector2 pixOffset = new Vector2(); + private final Vector2 tileOffset = new Vector2(); + public IsometricCamera() {} public void offset(float x, float y) { - offset.set(x, y); + pixOffset.set(x, y); + toWorld(pixOffset.x, pixOffset.y, tileOffset.setZero()); } @Override @@ -26,7 +29,7 @@ public class IsometricCamera extends OrthographicCamera { public void translate(float x, float y) { position.add(x, y); toScreen(position.x, position.y, tmp); - super.position.set(tmp, 0).add(offset.x, offset.y, 0); + super.position.set(tmp, 0).add(pixOffset.x, pixOffset.y, 0); } public void set(Vector2 vec) { @@ -36,44 +39,72 @@ public class IsometricCamera extends OrthographicCamera { public void set(float x, float y) { position.set(x, y); toScreen(position.x, position.y, tmp); - super.position.set(tmp, 0).add(offset.x, offset.y, 0); + super.position.set(tmp, 0).add(pixOffset.x, pixOffset.y, 0); } + /** + * Converts tile coords to screen coords. + */ public Vector2 toScreen(Vector2 worldCoords) { return toScreen(worldCoords.x, worldCoords.y, worldCoords); } + /** + * Converts tile coords to screen coords. + */ public Vector2 toScreen(float x, float y, Vector2 dst) { return EngineUtils.worldToScreenCoords(x, y, dst); } + /** + * Converts screen coords to tile coords. + */ public Vector2 toWorld(Vector2 screenCoords) { return toWorld(screenCoords.x, screenCoords.y, screenCoords); } + /** + * Converts screen coords to tile coords. + */ public Vector2 toWorld(float x, float y, Vector2 dst) { x /= Tile.SUBTILE_WIDTH50; y /= Tile.SUBTILE_HEIGHT50; - dst.x = ( x - y) / 2 - 0.5f; - dst.y = (-x - y) / 2 - 0.5f; + dst.x = ( x - y) / 2 - tileOffset.x; + dst.y = (-x - y) / 2 - tileOffset.y; return dst; } + /** + * Rounds tile coords to the floor integer tile coords from the specified float tile coords. + */ public Vector2 toTile(Vector2 worldCoords) { return toTile(worldCoords.x, worldCoords.y, worldCoords); } + /** + * Rounds tile coords to the floor integer tile coords from the specified float tile coords. + */ public Vector2 toTile(float x, float y, Vector2 dst) { + x += tileOffset.x; + y += tileOffset.y; dst.x = x < 0 ? MathUtils.floor(x) : MathUtils.floorPositive(x); dst.y = y < 0 ? MathUtils.floor(y) : MathUtils.floorPositive(y); return dst; } + /** + * Rounds tile coords to the closest integer tile coords from the specified float tile coords. + */ public Vector2 toTile50(Vector2 worldCoords) { return toTile50(worldCoords.x, worldCoords.y, worldCoords); } + /** + * Rounds tile coords to the closest integer tile coords from the specified float tile coords. + */ public Vector2 toTile50(float x, float y, Vector2 dst) { + x += tileOffset.x; + y += tileOffset.y; dst.x = x < 0 ? MathUtils.round(x) : MathUtils.roundPositive(x); dst.y = y < 0 ? MathUtils.round(y) : MathUtils.roundPositive(y); return dst; diff --git a/tools/src/com/riiablo/camera/CameraTool.java b/tools/src/com/riiablo/camera/CameraTool.java index 3bb05c06..9e289c14 100644 --- a/tools/src/com/riiablo/camera/CameraTool.java +++ b/tools/src/com/riiablo/camera/CameraTool.java @@ -175,7 +175,7 @@ public class CameraTool extends ApplicationAdapter { iso.unproject(vec2); iso.toWorld(vec2); wrld.set(vec2); - iso.toTile50(vec2); + iso.toTile(vec2); loc.set(vec2); iso.toScreen(vec2); @@ -189,7 +189,7 @@ public class CameraTool extends ApplicationAdapter { } shapes.end(); pos.set(iso.position); - iso.toTile50(pos); + iso.toTile(pos); shapes.begin(ShapeRenderer.ShapeType.Filled); { vec2.set(pos);