remove old transform and velocity components and replace with new decomposed components

focus on 2D for now
This commit is contained in:
2022-01-04 22:33:59 +01:00
parent c36fa30cbc
commit 12b0a90ad0
71 changed files with 1114 additions and 528 deletions

View File

@ -0,0 +1,12 @@
#pragma once
#include <glm/vec2.hpp>
namespace MM::Components {
struct Position2D {
glm::vec2 pos {0.f, 0.f};
};
} // MM::Components

View File

@ -0,0 +1,11 @@
#pragma once
namespace MM::Components {
// used to lift 2D into 3D space. like a z-index in css/svg
struct Position2D_ZOffset {
float z_offset = 500.f; // default camera allows values to be between 0 and 1000
};
} // MM::Components

View File

@ -0,0 +1,12 @@
#pragma once
#include <glm/vec3.hpp>
namespace MM::Components {
struct Position3D {
glm::vec3 pos {0.f, 0.f, 0.f};
};
} // MM::Components

View File

@ -0,0 +1,10 @@
#pragma once
namespace MM::Components {
struct Rotation2D {
float rot {0.f};
};
} // MM::Components

View File

@ -0,0 +1,12 @@
#pragma once
#include <glm/vec2.hpp>
namespace MM::Components {
struct Scale2D {
glm::vec2 scale {1.f, 1.f};
};
} // MM::Components

View File

@ -3,6 +3,7 @@
#include <glm/vec2.hpp>
#include <glm/vec3.hpp>
#include <glm/vec4.hpp>
#include <glm/mat4x4.hpp>
#include <nlohmann/json.hpp>
@ -12,5 +13,26 @@ namespace glm {
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(vec3, x, y, z)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(vec4, x, y, z, w)
//NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(mat4x4, [0], y, z, w)
inline void to_json(nlohmann::json& nlohmann_json_j, const mat4x4& nlohmann_json_t) {
// TODO: make 2d array?
nlohmann_json_j = nlohmann::json::array_t{};
nlohmann_json_j[0] = nlohmann_json_t[0];
nlohmann_json_j[1] = nlohmann_json_t[1];
nlohmann_json_j[2] = nlohmann_json_t[2];
nlohmann_json_j[3] = nlohmann_json_t[3];
}
inline void from_json(const nlohmann::json& nlohmann_json_j, mat4x4& nlohmann_json_t) {
if (!nlohmann_json_j.is_array()) {
//throw nlohmann::json::type_error::create(0, "", nlohmann_json_j);
assert(false && "expected array");
return; // TODO: dont fail silently
}
nlohmann_json_j.at(0).get_to(nlohmann_json_t[0]);
nlohmann_json_j.at(1).get_to(nlohmann_json_t[1]);
nlohmann_json_j.at(2).get_to(nlohmann_json_t[2]);
nlohmann_json_j.at(3).get_to(nlohmann_json_t[3]);
}
}

View File

@ -2,11 +2,11 @@
#include <nlohmann/json.hpp>
#include <mm/components/velocity2d.hpp>
#include <mm/components/position2d.hpp>
#include "./json_glm.hpp"
namespace MM::Components {
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Velocity2D, velocity, rotation)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Position2D, pos)
} // MM::Components

View File

@ -2,11 +2,11 @@
#include <nlohmann/json.hpp>
#include <mm/components/transform2d.hpp>
#include <mm/components/position2d_zoffset.hpp>
#include "./json_glm.hpp"
namespace MM::Components {
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Transform2D, position, scale, rotation)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Position2D_ZOffset, z_offset)
} // MM::Components

View File

@ -2,11 +2,11 @@
#include <nlohmann/json.hpp>
#include <mm/components/velocity3d.hpp>
#include <mm/components/position3d.hpp>
#include "./json_glm.hpp"
namespace MM::Components {
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Velocity3D, velocity, rotation)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Position3D, pos)
} // MM::Components

View File

@ -2,11 +2,11 @@
#include <nlohmann/json.hpp>
#include <mm/components/transform3d.hpp>
#include <mm/components/rotation2d.hpp>
#include "./json_glm.hpp"
namespace MM::Components {
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Transform3D, position, scale, rotation)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Rotation2D, rot)
} // MM::Components

View File

@ -0,0 +1,12 @@
#pragma once
#include <nlohmann/json.hpp>
#include <mm/components/scale2d.hpp>
#include "./json_glm.hpp"
namespace MM::Components {
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Scale2D, scale)
} // MM::Components

View File

@ -0,0 +1,12 @@
#pragma once
#include <nlohmann/json.hpp>
#include <mm/components/transform4x4.hpp>
#include "./json_glm.hpp"
namespace MM::Components {
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Transform4x4, trans)
} // MM::Components

View File

@ -0,0 +1,12 @@
#pragma once
#include <nlohmann/json.hpp>
#include <mm/components/velocity2d_position.hpp>
#include "./json_glm.hpp"
namespace MM::Components {
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Velocity2DPosition, pos_vel)
} // MM::Components

View File

@ -0,0 +1,12 @@
#pragma once
#include <nlohmann/json.hpp>
#include <mm/components/velocity2d_position_intent.hpp>
#include "./json_glm.hpp"
namespace MM::Components {
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Velocity2DPositionIntent, intent)
} // MM::Components

View File

@ -0,0 +1,12 @@
#pragma once
#include <nlohmann/json.hpp>
#include <mm/components/velocity2d_rotation.hpp>
#include "./json_glm.hpp"
namespace MM::Components {
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Velocity2DRotation, rot_vel)
} // MM::Components

View File

@ -1,55 +0,0 @@
//
// Created by FlaXxy on 29.07.2018.
//
#pragma once
#include <glm/vec2.hpp>
#include <glm/mat3x3.hpp>
#include <glm/mat4x4.hpp>
#include <glm/gtc/matrix_transform.hpp>
namespace MM::Components {
struct Transform2D {
glm::vec2 position {0.0f,0.0f};
glm::vec2 scale {1.0f,1.0f};
float rotation {0.0f};
glm::mat3x3 getTransform3(void) const {
//return transformationMatrix(scale, rotation, position);
glm::mat3x3 res(1);
res[2][0] = position.x;
res[2][1] = position.y;
float const s = sinf(rotation);
float const c = cosf(rotation);
res[0][0] = scale.x * c;
res[0][1] = scale.x * s;
res[1][0] = scale.y * -s;
res[1][1] = scale.y * c;
return res;
}
glm::mat4x4 getTransform4(float z = 500.f) const {
//return transformationMatrix(scale, rotation, position);
glm::mat4x4 res{1};
//res[2][0] = position.x;
//res[2][1] = position.y;
res = glm::translate(res, glm::vec3(position, z));
//float const s = sinf(rotation);
//float const c = cosf(rotation);
//res[0][0] = scale.x * c;
//res[0][1] = scale.x * s;
//res[1][0] = scale.y * -s;
//res[1][1] = scale.y * c;
res = glm::rotate(res, rotation, glm::vec3(0.f, 0.f, 1.f));
res = glm::scale(res, glm::vec3(scale, 1.f));
return res;
}
};
} // MM::Components

View File

@ -1,37 +0,0 @@
#pragma once
//#include <glm/mat3x3.hpp>
#include <glm/mat4x4.hpp>
#include <glm/gtc/matrix_transform.hpp>
//#include <entt/core/type_traits.hpp>
namespace MM::Components {
struct Transform3D {
glm::vec3 position {0.0f,0.0f,0.0f};
glm::vec3 scale {1.0f,1.0f,1.0f};
float rotation {0.0f};
glm::mat4x4 getTransform4(void) const {
//return transformationMatrix(scale, rotation, position);
glm::mat4x4 res{1};
//res[2][0] = position.x;
//res[2][1] = position.y;
res = glm::translate(res, position);
//float const s = sinf(rotation);
//float const c = cosf(rotation);
//res[0][0] = scale.x * c;
//res[0][1] = scale.x * s;
//res[1][0] = scale.y * -s;
//res[1][1] = scale.y * c;
res = glm::rotate(res, rotation, glm::vec3(0.f, 0.f, 1.f));
res = glm::scale(res, scale);
return res;
}
};
} // MM::Components

View File

@ -0,0 +1,15 @@
#pragma once
#include <glm/mat4x4.hpp>
namespace MM::Components {
// tag/flag to track dirty positional data, to reduce computation.
struct DirtyTransformTag {};
struct Transform4x4 {
glm::mat4x4 trans {1.f};
};
} // MM::Components

View File

@ -1,16 +0,0 @@
//
// Created by FlaXxy on 29.07.2018.
//
#pragma once
#include <glm/vec2.hpp>
namespace MM::Components {
struct Velocity2D {
glm::vec2 velocity;
float rotation {0.f};
};
} // MM::Components

View File

@ -0,0 +1,12 @@
#pragma once
#include <glm/vec2.hpp>
namespace MM::Components {
struct Velocity2DPosition {
glm::vec2 pos_vel {0.f, 0.f};
};
} // MM::Components

View File

@ -0,0 +1,12 @@
#pragma once
#include <glm/vec2.hpp>
namespace MM::Components {
struct Velocity2DPositionIntent {
glm::vec2 intent {0.f, 0.f};
};
} // MM::Components

View File

@ -0,0 +1,10 @@
#pragma once
namespace MM::Components {
struct Velocity2DRotation {
float rot_vel {0.f};
};
} // MM::Components

View File

@ -1,13 +0,0 @@
#pragma once
#include <glm/vec3.hpp>
namespace MM::Components {
struct Velocity3D {
glm::vec3 velocity {0.f, 0.f, 0.f};
glm::vec3 rotation {0.f, 0.f, 0.f};
};
} // MM::Components

View File

@ -1,33 +1,37 @@
#include "mm/components/position2d.hpp"
#include <gtest/gtest.h>
#include <mm/components/serialize/json_name.hpp>
#include <mm/components/serialize/json_color.hpp>
#include <mm/components/serialize/json_transform2d.hpp>
#include <mm/components/serialize/json_transform3d.hpp>
#include <mm/components/serialize/json_velocity2d.hpp>
#include <mm/components/serialize/json_position2d.hpp>
#include <mm/components/serialize/json_position2d_zoffset.hpp>
#include <mm/components/serialize/json_position3d.hpp>
#include <mm/components/serialize/json_rotation2d.hpp>
#include <mm/components/serialize/json_scale2d.hpp>
#include <mm/components/serialize/json_transform4x4.hpp>
#include <mm/components/serialize/json_velocity2d_position.hpp>
#include <mm/components/serialize/json_velocity2d_position_intent.hpp>
#include <mm/components/serialize/json_velocity2d_rotation.hpp>
#include <mm/components/serialize/json_view_dir2d.hpp>
#include <mm/components/serialize/json_view_dir3d.hpp>
/*#define PARSE_TEST_MACRO(type, json_string, comp_val) \
TEST(common_components_json_serialization, type) { \
MM::Components::type comp; \
{ \
auto j = nlohmann::json::parse(json_string); \
EXPECT_NO_THROW({ comp = j; }); \
ASSERT_EQ(comp, (comp_val)); \
#define TEST_JSON_SERL_EXPAND(x) x
#define TEST_JSON_SERL_IN_OUT(TYPE, JSON_STR, TEST_CORPUS) \
TEST(common_components_json_serialization, in_out_##TYPE) { \
MM::Components::TYPE comp; \
{ /* in */ \
auto j = nlohmann::json::parse(JSON_STR); \
comp = j; \
TEST_CORPUS \
} \
{ \
{ /* out */ \
nlohmann::json j; \
[>EXPECT_NO_THROW({ j = comp; });<] \
ASSERT_EQ(json_string, j.dump()); \
j = comp; \
ASSERT_EQ(JSON_STR, j.dump()); \
} \
}
PARSE_TEST_MACRO(
Name,
"{\"str\":\"test_name\"}",
MM::Components::Name{"test_name"}
); */
TEST(common_components_json_serialization, in_out_name) {
MM::Components::Name comp;
@ -72,137 +76,113 @@ TEST(common_components_json_serialization, in_out_name_fail) {
// ##############################################################
TEST(common_components_json_serialization, in_out_color) {
MM::Components::Color comp;
const char* json_test_file = R"({"color":{"w":1337.0,"x":0.0,"y":1.0,"z":3.0}})";
{ // in
auto j = nlohmann::json::parse(json_test_file);
EXPECT_NO_THROW({
comp = j;
});
glm::vec4 comp_val{0.f, 1.f, 3.f, 1337.f};
TEST_JSON_SERL_IN_OUT(
Color,
R"({"color":{"w":1337.0,"x":0.0,"y":1.0,"z":3.0}})",
TEST_JSON_SERL_EXPAND({
glm::vec4 comp_val(0.f, 1.f, 3.f, 1337.f);
ASSERT_EQ(comp.color.x, comp_val.x);
ASSERT_EQ(comp.color.y, comp_val.y);
ASSERT_EQ(comp.color.z, comp_val.z);
ASSERT_EQ(comp.color.w, comp_val.w);
}
{ // out
nlohmann::json j;
EXPECT_NO_THROW({
j = comp;
});
ASSERT_EQ(json_test_file, j.dump());
}
}
})
)
// ##############################################################
TEST(common_components_json_serialization, in_out_transform2d) {
MM::Components::Transform2D comp;
const char* json_test_file = R"({"position":{"x":42.0,"y":6.0},"rotation":99.0,"scale":{"x":1337.0,"y":68.0}})";
{ // in
auto j = nlohmann::json::parse(json_test_file);
EXPECT_NO_THROW({
comp = j;
});
ASSERT_EQ(comp.position.x, 42.f);
ASSERT_EQ(comp.position.y, 6.f);
ASSERT_EQ(comp.scale.x, 1337.f);
ASSERT_EQ(comp.scale.y, 68.f);
ASSERT_EQ(comp.rotation, 99.f);
}
{ // out
nlohmann::json j;
EXPECT_NO_THROW({
j = comp;
});
ASSERT_EQ(json_test_file, j.dump());
}
}
TEST_JSON_SERL_IN_OUT(
Position2D,
R"({"pos":{"x":42.0,"y":6.0}})",
TEST_JSON_SERL_EXPAND(
ASSERT_EQ(comp.pos.x, 42.f);
ASSERT_EQ(comp.pos.y, 6.f);
)
)
// ##############################################################
TEST(common_components_json_serialization, in_out_transform3d) {
MM::Components::Transform3D comp;
const char* json_test_file = R"({"position":{"x":42.0,"y":6.0,"z":66.0},"rotation":99.0,"scale":{"x":1337.0,"y":68.0,"z":60.0}})";
{ // in
auto j = nlohmann::json::parse(json_test_file);
EXPECT_NO_THROW({
comp = j;
});
ASSERT_EQ(comp.position.x, 42.f);
ASSERT_EQ(comp.position.y, 6.f);
ASSERT_EQ(comp.position.z, 66.f);
ASSERT_EQ(comp.scale.x, 1337.f);
ASSERT_EQ(comp.scale.y, 68.f);
ASSERT_EQ(comp.scale.z, 60.f);
// TODO: prob needs 3 rotations...
ASSERT_EQ(comp.rotation, 99.f);
}
{ // out
nlohmann::json j;
EXPECT_NO_THROW({
j = comp;
});
ASSERT_EQ(json_test_file, j.dump());
}
}
TEST_JSON_SERL_IN_OUT(
Position2D_ZOffset,
R"({"z_offset":3.0})",
TEST_JSON_SERL_EXPAND(
ASSERT_EQ(comp.z_offset, 3.f);
)
)
// ##############################################################
TEST(common_components_json_serialization, in_out_velocity2d) {
MM::Components::Velocity2D comp;
TEST_JSON_SERL_IN_OUT(
Position3D,
R"({"pos":{"x":42.0,"y":6.0,"z":44.0}})",
TEST_JSON_SERL_EXPAND(
ASSERT_EQ(comp.pos.x, 42.f);
ASSERT_EQ(comp.pos.y, 6.f);
ASSERT_EQ(comp.pos.z, 44.f);
)
)
const char* json_test_file = R"({"rotation":99.0,"velocity":{"x":42.0,"y":6.0}})";
// ##############################################################
{ // in
auto j = nlohmann::json::parse(json_test_file);
TEST_JSON_SERL_IN_OUT(
Rotation2D,
R"({"rot":42.0})",
TEST_JSON_SERL_EXPAND(
ASSERT_EQ(comp.rot, 42.f);
)
)
EXPECT_NO_THROW({
comp = j;
});
// ##############################################################
ASSERT_EQ(comp.velocity.x, 42.f);
ASSERT_EQ(comp.velocity.y, 6.f);
TEST_JSON_SERL_IN_OUT(
Scale2D,
R"({"scale":{"x":42.0,"y":6.0}})",
TEST_JSON_SERL_EXPAND(
ASSERT_EQ(comp.scale.x, 42.f);
ASSERT_EQ(comp.scale.y, 6.f);
)
)
ASSERT_EQ(comp.rotation, 99.f);
}
// ##############################################################
{ // out
nlohmann::json j;
TEST_JSON_SERL_IN_OUT(
Transform4x4,
R"({"trans":[{"w":0.0,"x":1.0,"y":0.0,"z":0.0},{"w":0.0,"x":0.0,"y":1.0,"z":0.0},{"w":0.0,"x":0.0,"y":0.0,"z":1.0},{"w":1.0,"x":0.0,"y":0.0,"z":0.0}]})",
TEST_JSON_SERL_EXPAND(
ASSERT_EQ(comp.trans, glm::mat4x4{1.f});
)
)
EXPECT_NO_THROW({
j = comp;
});
// ##############################################################
ASSERT_EQ(json_test_file, j.dump());
}
}
TEST_JSON_SERL_IN_OUT(
Velocity2DPosition,
R"({"pos_vel":{"x":42.0,"y":6.0}})",
TEST_JSON_SERL_EXPAND(
ASSERT_EQ(comp.pos_vel.x, 42.f);
ASSERT_EQ(comp.pos_vel.y, 6.f);
)
)
// ##############################################################
TEST_JSON_SERL_IN_OUT(
Velocity2DPositionIntent,
R"({"intent":{"x":42.0,"y":6.0}})",
TEST_JSON_SERL_EXPAND(
ASSERT_EQ(comp.intent.x, 42.f);
ASSERT_EQ(comp.intent.y, 6.f);
)
)
// ##############################################################
TEST_JSON_SERL_IN_OUT(
Velocity2DRotation,
R"({"rot_vel":42.0})",
TEST_JSON_SERL_EXPAND(
ASSERT_EQ(comp.rot_vel, 42.f);
)
)
// ##############################################################