Add glm for vector math, used for position and movement

This commit is contained in:
karl 2020-01-07 13:10:23 +01:00
parent c4daf46cb2
commit 7675ddc3a4
8 changed files with 65 additions and 55 deletions

View File

@ -5,9 +5,10 @@ set(CMAKE_CXX_STANDARD 17)
find_package(OpenGL REQUIRED) find_package(OpenGL REQUIRED)
find_package(glfw3 REQUIRED) find_package(glfw3 REQUIRED)
find_package(glm REQUIRED)
add_executable(ecsgame Util/glad.c Util/OBJ_Loader.h Rendering/Shader.cpp Rendering/Shader.h main.cpp ECS/Components/Position.h ECS/Components/Movement.h ECS/Events/InputEvent.h ECS/Systems/GravitySystem.h ECS/Systems/PositionDebugSystem.h ECS/Systems/KeyboardMovementSystem.h) add_executable(ecsgame Util/glad.c Util/OBJ_Loader.h Rendering/Shader.cpp Rendering/Shader.h main.cpp ECS/Components/Transform.h ECS/Components/Movement.h ECS/Events/InputEvent.h ECS/Systems/GravitySystem.h ECS/Systems/PositionDebugSystem.h ECS/Systems/KeyboardMovementSystem.h ECS/Components/Camera.h)
include_directories(${OPENGL_INCLUDE_DIRS}) include_directories(${OPENGL_INCLUDE_DIRS})
target_link_libraries(ecsgame ${OPENGL_LIBRARY} glfw ${CMAKE_DL_LIBS}) target_link_libraries(ecsgame ${OPENGL_LIBRARY} glfw glm ${CMAKE_DL_LIBS})

View File

@ -6,19 +6,13 @@
#define ECSGAME_MOVEMENT_H #define ECSGAME_MOVEMENT_H
struct Movement { struct Movement {
Movement(float speedX, float speedY, float speedZ) : speedX(speedX), speedY(speedY), speedZ(speedZ) {} Movement(glm::vec3 speed) : speed(speed) {}
float speedX; glm::vec3 speed;
float speedY;
float speedZ;
int movingX = 0; glm::ivec3 moving;
int movingY = 0;
int movingZ = 0;
float velocityX = 0.0f; glm::vec3 velocity;
float velocityY = 0.0f;
float velocityZ = 0.0f;
}; };
#endif //ECSGAME_MOVEMENT_H #endif //ECSGAME_MOVEMENT_H

View File

@ -1,18 +0,0 @@
//
// Created by karl on 04.01.20.
//
#ifndef ECSGAME_POSITION_H
#define ECSGAME_POSITION_H
struct Position {
Position(float x, float y, float z) : x(x), y(y), z(z) {}
Position() : x(0.f), y(0.f), z(0.f) {}
float x;
float y;
float z;
};
#endif //ECSGAME_POSITION_H

View File

@ -0,0 +1,34 @@
//
// Created by karl on 04.01.20.
//
#ifndef ECSGAME_TRANSFORM_H
#define ECSGAME_TRANSFORM_H
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
struct Transform {
Transform() = default;
glm::mat4 matrix = glm::mat4(1.0f); // Initialize as identity
void translate(glm::vec3 offset) {
matrix = glm::translate(matrix, offset);
}
void uniform_scale(float factor) {
scale(glm::vec3(factor, factor, factor));
}
void scale(glm::vec3 factors) {
matrix = glm::scale(matrix, factors);
}
glm::vec3 getPosition() {
return matrix * glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
}
};
#endif //ECSGAME_TRANSFORM_H

View File

@ -6,7 +6,7 @@
#define ECSGAME_GRAVITYSYSTEM_H #define ECSGAME_GRAVITYSYSTEM_H
#include "../ECS.h" #include "../ECS.h"
#include "../Components/Position.h" #include "../Components/Transform.h"
using namespace ECS; using namespace ECS;
@ -17,8 +17,8 @@ public:
} }
void tick(World *pWorld, float deltaTime) override { void tick(World *pWorld, float deltaTime) override {
pWorld->each<Position>([&](Entity *ent, ComponentHandle<Position> position) { pWorld->each<Transform>([&](Entity *ent, ComponentHandle<Transform> position) {
position->y += gravityAmount * deltaTime; position->translate(glm::vec3(0.0f, gravityAmount * deltaTime, 0.0f));
}); });
} }

View File

@ -11,7 +11,7 @@
#include <iostream> #include <iostream>
#include "../ECS.h" #include "../ECS.h"
#include "../Components/Position.h" #include "../Components/Transform.h"
#include "../Events/InputEvent.h" #include "../Events/InputEvent.h"
#include "../Components/Movement.h" #include "../Components/Movement.h"
@ -28,33 +28,33 @@ class KeyboardMovementSystem : public EntitySystem, public EventSubscriber<Input
if (event.key == GLFW_KEY_W) { if (event.key == GLFW_KEY_W) {
myWorld->each<Movement>([&](Entity *ent, ComponentHandle<Movement> movement) { myWorld->each<Movement>([&](Entity *ent, ComponentHandle<Movement> movement) {
if (event.action == GLFW_PRESS) { if (event.action == GLFW_PRESS) {
movement->movingZ = -1; movement->moving.z = -1;
} else if (event.action == GLFW_RELEASE) { } else if (event.action == GLFW_RELEASE) {
movement->movingZ = 0; movement->moving.z = 0;
} }
}); });
} else if (event.key == GLFW_KEY_S) { } else if (event.key == GLFW_KEY_S) {
myWorld->each<Movement>([&](Entity *ent, ComponentHandle<Movement> movement) { myWorld->each<Movement>([&](Entity *ent, ComponentHandle<Movement> movement) {
if (event.action == GLFW_PRESS) { if (event.action == GLFW_PRESS) {
movement->movingZ = 1; movement->moving.z = 1;
} else if (event.action == GLFW_RELEASE) { } else if (event.action == GLFW_RELEASE) {
movement->movingZ = 0; movement->moving.z = 0;
} }
}); });
} else if (event.key == GLFW_KEY_A) { } else if (event.key == GLFW_KEY_A) {
myWorld->each<Movement>([&](Entity *ent, ComponentHandle<Movement> movement) { myWorld->each<Movement>([&](Entity *ent, ComponentHandle<Movement> movement) {
if (event.action == GLFW_PRESS) { if (event.action == GLFW_PRESS) {
movement->movingX = 1; movement->moving.x = 1;
} else if (event.action == GLFW_RELEASE) { } else if (event.action == GLFW_RELEASE) {
movement->movingX = 0; movement->moving.x = 0;
} }
}); });
} else if (event.key == GLFW_KEY_D) { } else if (event.key == GLFW_KEY_D) {
myWorld->each<Movement>([&](Entity *ent, ComponentHandle<Movement> movement) { myWorld->each<Movement>([&](Entity *ent, ComponentHandle<Movement> movement) {
if (event.action == GLFW_PRESS) { if (event.action == GLFW_PRESS) {
movement->movingX = -1; movement->moving.x = -1;
} else if (event.action == GLFW_RELEASE) { } else if (event.action == GLFW_RELEASE) {
movement->movingX = 0; movement->moving.x = 0;
} }
}); });
} }
@ -62,11 +62,9 @@ class KeyboardMovementSystem : public EntitySystem, public EventSubscriber<Input
} }
void tick(World *pWorld, float deltaTime) override { void tick(World *pWorld, float deltaTime) override {
pWorld->each<Position, Movement>( pWorld->each<Transform, Movement>(
[&](Entity *ent, ComponentHandle<Position> position, ComponentHandle<Movement> movement) { [&](Entity *ent, ComponentHandle<Transform> transform, ComponentHandle<Movement> movement) {
position->x += movement->movingX * movement->speedX * deltaTime; transform->translate(glm::vec3(movement->moving) * movement->speed * deltaTime);
position->y += movement->movingY * movement->speedY * deltaTime;
position->z += movement->movingZ * movement->speedZ * deltaTime;
}); });
} }

View File

@ -8,18 +8,18 @@
#include <iostream> #include <iostream>
#include "../ECS.h" #include "../ECS.h"
#include "../Components/Position.h" #include "../Components/Transform.h"
using namespace ECS; using namespace ECS;
class PositionDebugOutputSystem : public EntitySystem { class PositionDebugOutputSystem : public EntitySystem {
public: public:
void tick(World *pWorld, float deltaTime) override { void tick(World *pWorld, float deltaTime) override {
pWorld->each<Position>([&](Entity *ent, ComponentHandle<Position> position) { pWorld->each<Transform>([&](Entity *ent, ComponentHandle<Transform> transform) {
std::cout << ent->getEntityId() << ": " std::cout << ent->getEntityId() << ": "
<< position->x << ", " << transform->getPosition().x << ", "
<< position->y << ", " << transform->getPosition().y << ", "
<< position->z << transform->getPosition().z
<< std::endl; << std::endl;
}); });
} }

View File

@ -24,15 +24,16 @@ static void key_callback(GLFWwindow *window, int key, int scancode, int action,
} }
int main() { int main() {
// TODO: Could be automated by getting all classes within 'ECS/Systems'
world->registerSystem(new GravitySystem(-9.8f)); world->registerSystem(new GravitySystem(-9.8f));
world->registerSystem(new PositionDebugOutputSystem()); world->registerSystem(new PositionDebugOutputSystem());
world->registerSystem(new KeyboardMovementSystem()); world->registerSystem(new KeyboardMovementSystem());
Entity *ent = world->create(); Entity *ent = world->create();
ent->assign<Position>(0.f, 0.f, 0.f); ent->assign<Transform>();
ent->assign<Movement>(1.f, 1.f, 1.f); ent->assign<Movement>(glm::vec3(1.f, 1.f, 1.f));
ComponentHandle<Position> pos = ent->get<Position>(); ComponentHandle<Transform> pos = ent->get<Transform>();
GLFWwindow *window; GLFWwindow *window;