Improved coordinate handling

This commit is contained in:
Maximilian Giller 2023-05-03 01:32:51 +02:00
parent 7cb2fd2e24
commit c1dfe5c80e
7 changed files with 62 additions and 58 deletions

View file

@ -0,0 +1,21 @@
#include "coordinate_transformer.h"
// Initialize matrix
const Eigen::Matrix<float, 3, 3> CoordinateTransformer::worldToIsometricMatrix =
(Eigen::Matrix<float, 3, 3>() << 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
);
}

View file

@ -0,0 +1,17 @@
#ifndef HOLESOME_COORDINATE_TRANSFORMER_H
#define HOLESOME_COORDINATE_TRANSFORMER_H
#include <Eigen/Dense>
#include "coordinates.h"
class CoordinateTransformer
{
private:
static const Eigen::Matrix<float, 3, 3> worldToIsometricMatrix;
public:
static IsometricCoordinates worldToIsometric(WorldCoordinates worldCoordinates);
};
#endif //HOLESOME_COORDINATE_TRANSFORMER_H

View file

@ -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;
};

View file

@ -1,23 +0,0 @@
//
// Created by max on 27.04.23.
//
#include "isometric_coordinate_transformer.h"
IsometricCoordinateTransformer::IsometricCoordinateTransformer() {
worldToScreenMatrix = Eigen::Matrix<float, 2, 3>();
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()
};
}

View file

@ -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 <Eigen/Dense>
#include "coordinates.h"
class IsometricCoordinateTransformer {
private:
Eigen::Matrix<float, 2, 3> worldToScreenMatrix;
public:
IsometricCoordinateTransformer();
ScreenCoordinates worldToScreen(WorldCoordinates worldCoordinates) const;
};
#endif //HOLESOME_ISOMETRIC_COORDINATE_TRANSFORMER_H

View file

@ -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 {

View file

@ -7,7 +7,7 @@
#include <memory>
#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<IsometricCoordinateTransformer> isoCoordTransformer = std::make_shared<IsometricCoordinateTransformer>();
const std::shared_ptr<CoordinateTransformer> isoCoordTransformer = std::make_shared<CoordinateTransformer>();
};