Movement is functional!

There is a small bug that can occur, seems like gimbal lock or something
like that. Only rarely though (close to coordinate origin?)
This commit is contained in:
karl 2020-01-07 22:07:38 +01:00
parent 0908a39aa5
commit b514cd38a7
3 changed files with 10 additions and 17 deletions

View File

@ -44,7 +44,7 @@ class KeyboardMovementSystem : public EntitySystem, public EventSubscriber<Input
} 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->moving.x = 1; movement->moving.x = -1;
} else if (event.action == GLFW_RELEASE) { } else if (event.action == GLFW_RELEASE) {
movement->moving.x = 0; movement->moving.x = 0;
} }
@ -52,7 +52,7 @@ class KeyboardMovementSystem : public EntitySystem, public EventSubscriber<Input
} 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->moving.x = -1; movement->moving.x = 1;
} else if (event.action == GLFW_RELEASE) { } else if (event.action == GLFW_RELEASE) {
movement->moving.x = 0; movement->moving.x = 0;
} }
@ -64,7 +64,7 @@ class KeyboardMovementSystem : public EntitySystem, public EventSubscriber<Input
void tick(World *pWorld, float deltaTime) override { void tick(World *pWorld, float deltaTime) override {
pWorld->each<Transform, Movement>( pWorld->each<Transform, Movement>(
[&](Entity *ent, ComponentHandle<Transform> transform, ComponentHandle<Movement> movement) { [&](Entity *ent, ComponentHandle<Transform> transform, ComponentHandle<Movement> movement) {
transform->translate(glm::mat3x3(transform->matrix) * glm::vec3(movement->moving) * movement->speed * deltaTime); transform->translate(glm::vec3(movement->moving) * movement->speed * deltaTime);
}); });
} }

View File

@ -28,7 +28,7 @@ public:
void receive(World *pWorld, const MouseMoveEvent &event) override { void receive(World *pWorld, const MouseMoveEvent &event) override {
pWorld->each<Transform, MouseLook, Camera>([&](Entity *ent, ComponentHandle<Transform> transform, ComponentHandle<MouseLook> mouse, ComponentHandle<Camera> camera) { pWorld->each<Transform, MouseLook, Camera>([&](Entity *ent, ComponentHandle<Transform> transform, ComponentHandle<MouseLook> mouse, ComponentHandle<Camera> camera) {
double xOffset = event.newX - lastX; double xOffset = lastX - event.newX;
double yOffset = lastY - event.newY; double yOffset = lastY - event.newY;
lastX = event.newX; lastX = event.newX;
@ -46,21 +46,14 @@ public:
if(pitch < -89.0f) if(pitch < -89.0f)
pitch = -89.0f; pitch = -89.0f;
glm::vec3 direction; glm::mat4x4 newTransform = glm::mat4x4(1.0);
direction.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
direction.y = sin(glm::radians(pitch));
direction.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
glm::vec3 cameraOrigin = transform->getPosition(); newTransform = glm::rotate(newTransform, glm::radians((float)pitch), transform->right());
glm::vec3 cameraFront = glm::normalize(direction); newTransform = glm::rotate(newTransform, glm::radians((float)yaw), glm::vec3(0.0, 1.0, 0.0));
glm::vec3 cameraUp = glm::vec3(0.0, 1.0, 0.0);
glm::vec3 cameraRight = glm::cross(cameraFront, cameraUp);
glm::mat4x4 lookAt = glm::lookAt(cameraOrigin, cameraOrigin + cameraFront, cameraUp); newTransform[3] = transform->matrix[3];
lookAt[3] = glm::vec4(cameraOrigin, 1.0); transform->matrix = newTransform;
transform->matrix = lookAt;
}); });
} }

View File

@ -23,7 +23,7 @@ public:
shader.use(); shader.use();
shader.setMat4("projection", camera->projection); shader.setMat4("projection", camera->projection);
shader.setMat4("view", cameraTransform->matrix); shader.setMat4("view", glm::inverse(cameraTransform->matrix));
pWorld->each<Mesh, Transform>([&](Entity *ent, ComponentHandle<Mesh> mesh, ComponentHandle<Transform> transform) { pWorld->each<Mesh, Transform>([&](Entity *ent, ComponentHandle<Mesh> mesh, ComponentHandle<Transform> transform) {
shader.setMat4("model", transform->matrix); shader.setMat4("model", transform->matrix);