From c1dfe5c80e1b831cd0dfea41631a7139ae8de944 Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Wed, 3 May 2023 01:32:51 +0200 Subject: [PATCH] Improved coordinate handling --- src/coordinates/coordinate_transformer.cpp | 21 ++++++++++++++ src/coordinates/coordinate_transformer.h | 17 +++++++++++ src/coordinates/coordinates.h | 28 +++++++++++++------ .../isometric_coordinate_transformer.cpp | 23 --------------- .../isometric_coordinate_transformer.h | 21 -------------- src/coordinates/translated_coordinates.cpp | 4 +-- src/coordinates/translated_coordinates.h | 6 ++-- 7 files changed, 62 insertions(+), 58 deletions(-) create mode 100644 src/coordinates/coordinate_transformer.cpp create mode 100644 src/coordinates/coordinate_transformer.h delete mode 100644 src/coordinates/isometric_coordinate_transformer.cpp delete mode 100644 src/coordinates/isometric_coordinate_transformer.h diff --git a/src/coordinates/coordinate_transformer.cpp b/src/coordinates/coordinate_transformer.cpp new file mode 100644 index 0000000..8cbcaba --- /dev/null +++ b/src/coordinates/coordinate_transformer.cpp @@ -0,0 +1,21 @@ +#include "coordinate_transformer.h" + +// Initialize matrix +const Eigen::Matrix CoordinateTransformer::worldToIsometricMatrix = + (Eigen::Matrix() << 0.5f, 0.5f, 0, + -0.5f, 0.5f, -1, + 1, 1, 0).finished(); + +IsometricCoordinates CoordinateTransformer::worldToIsometric(WorldCoordinates worldCoordinates) +{ + Eigen::Vector3f worldCoordinatesVector; + worldCoordinatesVector << worldCoordinates.x, worldCoordinates.y, worldCoordinates.z; + + Eigen::Vector3f isoCoordinatesVector = worldToIsometricMatrix * worldCoordinatesVector; + + return IsometricCoordinates( + isoCoordinatesVector.x(), // x + isoCoordinatesVector.y(), // y + isoCoordinatesVector.z() // depth + ); +} diff --git a/src/coordinates/coordinate_transformer.h b/src/coordinates/coordinate_transformer.h new file mode 100644 index 0000000..d12f5e2 --- /dev/null +++ b/src/coordinates/coordinate_transformer.h @@ -0,0 +1,17 @@ +#ifndef HOLESOME_COORDINATE_TRANSFORMER_H +#define HOLESOME_COORDINATE_TRANSFORMER_H + +#include +#include "coordinates.h" + +class CoordinateTransformer +{ +private: + static const Eigen::Matrix worldToIsometricMatrix; + +public: + static IsometricCoordinates worldToIsometric(WorldCoordinates worldCoordinates); +}; + + +#endif //HOLESOME_COORDINATE_TRANSFORMER_H diff --git a/src/coordinates/coordinates.h b/src/coordinates/coordinates.h index 21583e3..04d75ee 100644 --- a/src/coordinates/coordinates.h +++ b/src/coordinates/coordinates.h @@ -5,12 +5,14 @@ #ifndef HOLESOME_COORDINATES_H #define HOLESOME_COORDINATES_H -struct WorldCoordinates { +struct WorldCoordinates +{ float x; float y; float z; // Height - WorldCoordinates operator+(WorldCoordinates other) const { + WorldCoordinates operator+(WorldCoordinates other) const + { return { x + other.x, y + other.y, @@ -18,11 +20,13 @@ struct WorldCoordinates { }; } - WorldCoordinates operator-(WorldCoordinates other) const { + WorldCoordinates operator-(WorldCoordinates other) const + { return (*this) + (other * -1); } - WorldCoordinates operator*(float factor) const { + WorldCoordinates operator*(float factor) const + { return { x * factor, y * factor, @@ -30,25 +34,31 @@ struct WorldCoordinates { }; } - WorldCoordinates operator/(float factor) const { + WorldCoordinates operator/(float factor) const + { return (*this) * (1 / factor); } - bool operator==(WorldCoordinates other) const { + bool operator==(WorldCoordinates other) const + { return x == other.x && y == other.y && z == other.z; } - bool operator!=(WorldCoordinates other) const { + bool operator!=(WorldCoordinates other) const + { return !(*this == other); } }; -struct ScreenCoordinates { +struct IsometricCoordinates +{ float x; float y; + float depth; // Bigger means further back. Can be used for accurate rendering order. }; -struct GridCoordinates { +struct GridCoordinates +{ float x; float y; }; diff --git a/src/coordinates/isometric_coordinate_transformer.cpp b/src/coordinates/isometric_coordinate_transformer.cpp deleted file mode 100644 index fdf27e1..0000000 --- a/src/coordinates/isometric_coordinate_transformer.cpp +++ /dev/null @@ -1,23 +0,0 @@ -// -// Created by max on 27.04.23. -// - -#include "isometric_coordinate_transformer.h" - -IsometricCoordinateTransformer::IsometricCoordinateTransformer() { - worldToScreenMatrix = Eigen::Matrix(); - worldToScreenMatrix << 0.5f, 0.5f, 0, - -0.5f, 0.5f, -1; -} - -ScreenCoordinates IsometricCoordinateTransformer::worldToScreen(WorldCoordinates worldCoordinates) const { - Eigen::Vector3f worldCoordinatesVector; - worldCoordinatesVector << worldCoordinates.x, worldCoordinates.y, worldCoordinates.z; - - Eigen::Vector2f screenCoordinatesVector = worldToScreenMatrix * worldCoordinatesVector; - - return { - screenCoordinatesVector.x(), - screenCoordinatesVector.y() - }; -} diff --git a/src/coordinates/isometric_coordinate_transformer.h b/src/coordinates/isometric_coordinate_transformer.h deleted file mode 100644 index 846326f..0000000 --- a/src/coordinates/isometric_coordinate_transformer.h +++ /dev/null @@ -1,21 +0,0 @@ -// -// Created by max on 27.04.23.struct -// - -#ifndef HOLESOME_ISOMETRIC_COORDINATE_TRANSFORMER_H -#define HOLESOME_ISOMETRIC_COORDINATE_TRANSFORMER_H - -#include -#include "coordinates.h" - -class IsometricCoordinateTransformer { -private: - Eigen::Matrix worldToScreenMatrix; - -public: - IsometricCoordinateTransformer(); - ScreenCoordinates worldToScreen(WorldCoordinates worldCoordinates) const; -}; - - -#endif //HOLESOME_ISOMETRIC_COORDINATE_TRANSFORMER_H diff --git a/src/coordinates/translated_coordinates.cpp b/src/coordinates/translated_coordinates.cpp index 93b6829..adb39bc 100644 --- a/src/coordinates/translated_coordinates.cpp +++ b/src/coordinates/translated_coordinates.cpp @@ -8,8 +8,8 @@ WorldCoordinates TranslatedCoordinates::getWorldCoordinates() const { return worldCoordinates; } -ScreenCoordinates TranslatedCoordinates::getScreenCoordinates() const { - return isoCoordTransformer->worldToScreen(worldCoordinates); +IsometricCoordinates TranslatedCoordinates::getScreenCoordinates() const { + return isoCoordTransformer->worldToIsometric(worldCoordinates); } GridCoordinates TranslatedCoordinates::getGridCoordinates() const { diff --git a/src/coordinates/translated_coordinates.h b/src/coordinates/translated_coordinates.h index 886721b..d4280ac 100644 --- a/src/coordinates/translated_coordinates.h +++ b/src/coordinates/translated_coordinates.h @@ -7,7 +7,7 @@ #include #include "coordinates.h" -#include "isometric_coordinate_transformer.h" +#include "coordinate_transformer.h" #define INITIAL_WORLD_TO_GRID_FACTOR 0.25f @@ -17,7 +17,7 @@ public: WorldCoordinates getWorldCoordinates() const; - ScreenCoordinates getScreenCoordinates() const; + IsometricCoordinates getScreenCoordinates() const; GridCoordinates getGridCoordinates() const; @@ -28,7 +28,7 @@ public: private: WorldCoordinates worldCoordinates; const float worldToGridFactor = INITIAL_WORLD_TO_GRID_FACTOR; - const std::shared_ptr isoCoordTransformer = std::make_shared(); + const std::shared_ptr isoCoordTransformer = std::make_shared(); };