Skip to content

Commit

Permalink
Update DebugBox.cpp
Browse files Browse the repository at this point in the history
Remove some includes that arent needed, and tidy up some general code
HandleKeyboard in Camera.h now allows moving in multiple directions at the same time
  • Loading branch information
ConniBug committed Mar 5, 2024
1 parent c9277d4 commit 9a30593
Show file tree
Hide file tree
Showing 14 changed files with 262 additions and 347 deletions.
16 changes: 8 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ set(SOURCE_FILES

src/window.cpp
include/Window.h
src/Shader.cpp include/Shader.h
src/logging.cpp include/logging.h
src/vector.cpp include/vector.h
src/Shader.cpp include/Shader.h
src/logging.cpp include/logging.h
src/Vector.cpp include/Vector.h
src/Entity_t.cpp include/Entity_t.h
src/Camera.cpp include/Camera.h
src/Storage.cpp include/Storage.h
src/Scene.cpp include/Scene.h
src/Camera.cpp include/Camera.h
src/Storage.cpp include/Storage.h
src/Scene.cpp include/Scene.h

src/PhysicsEngine.cpp include/PhysicsEngine.h
Scenes/main_scene.cpp Scenes/main_scene.h
src/PhysicsEngine.cpp include/PhysicsEngine.h
Scenes/main_scene.cpp Scenes/main_scene.h

Scenes/orth_scene.cpp Scenes/orth_scene.h

Expand Down
49 changes: 24 additions & 25 deletions Scenes/main_scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
//

#include "main_scene.h"
#include "logging.h"
#include "glm.hpp"
#include "gtc/matrix_transform.hpp"
#include "Camera.h"
#include "glad/glad.h"
#include "../include/PhysicsEngine.h"
#include "Camera.h"
#include "DebugRendering/DebugBox.h"
#include "Window.h"
#include "glad/glad.h"
#include "glm.hpp"
#include "gtc/matrix_transform.hpp"
#include "logging.h"

void main_scene::Init(Camera *camera) {
logging::verbose("main_scene::init()");
Expand All @@ -21,23 +22,23 @@ void main_scene::Init(Camera *camera) {
shaders.push_back(new Shader("shaders/debug_line_vertex.glsl", "shaders/debug_line.frag"));

// spawn entities at random positions
// int max_r = 100;
// int count = 100000;
// for (int i = 0; i < count; i++) {
// auto entity = new Entity_t(shaders[0],
// glm::vec3(rand() % max_r - max_r / 2, rand() % max_r - max_r / 2, rand() % max_r - max_r / 2),
// glm::vec3(0.0f),
// glm::vec3(0.5f));
// entity->physics.engine = new PhysicsEngine(entity);
// entity->physics.enabled = i % 10;
// entity_list.push_back(entity);
// }
// int max_r = 100;
// int count = 100000;
// for (int i = 0; i < count; i++) {
// auto entity = new Entity_t(shaders[0],
// glm::vec3(rand() % max_r - max_r / 2, rand() % max_r - max_r / 2, rand() % max_r - max_r / 2),
// glm::vec3(0.0f),
// glm::vec3(0.5f));
// entity->physics.engine = new PhysicsEngine(entity);
// entity->physics.enabled = i % 10;
// entity_list.push_back(entity);
// }
auto entity = new Entity_t(shaders[0],
glm::vec3(0, 0, 0),
glm::vec3(0.0f),
glm::vec3(0.5f));
entity->physics.engine = new PhysicsEngine(entity);
entity->physics.enabled = 0;
entity->physics.enabled = true;
entity_list.push_back(entity);


Expand All @@ -50,13 +51,13 @@ void main_scene::Init(Camera *camera) {

void main_scene::Update(double deltaTime) {
// create transformations
glm::mat4 view = glm::mat4(1.0f); // make sure to initialize matrix to identity matrix first
glm::mat4 projection = camera->getProjectionMatrix(storage->window.width, storage->window.height);
glm::mat4 view = glm::mat4(1.0f);// make sure to initialize matrix to identity matrix first
glm::mat4 projection = camera->getProjectionMatrix(storage->window.width, storage->window.height);

view *= camera->view_matrix();


for (auto &entity : entity_list) {
for (auto &entity: entity_list) {
// First we want to apply physics e.g. gravity etc
{
entity->physics.engine->ApplyPhysics(deltaTime);
Expand All @@ -67,15 +68,15 @@ void main_scene::Update(double deltaTime) {
{
entity->timer += deltaTime;
if (entity->timer > 2) {
// entity->position.y = (rand() % 200) / 10;
// entity->position.y = (rand() % 200) / 10;
entity->timer = 0;
}
}

// This is where im simulating a collision system clamping all blocks to have their center be above y 0
{
// if (entity->position.y < 0)
// entity->position.y = 0;
if (entity->position.y < 0)
entity->position.y = 0;
}

// entity->scale += glm::vec3(0.0f, 0.0f, 0.1f);
Expand All @@ -89,8 +90,6 @@ void main_scene::Update(double deltaTime) {
this->draw_debug();
}

void main_scene::WindowResize(int width, int height) {

void main_scene::HandleMouseMovement(float xoffset, float yoffset) {
const float pitch_max = 89;
if (camera->pitch < -pitch_max) {
Expand Down
3 changes: 1 addition & 2 deletions Scenes/main_scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ class main_scene : public Scene {
public:
void Init(Camera *camera) override;
void Update(double deltaTime) override;
void WindowResize(int width, int height) override;
void HandleMouseMovement(float xoffset, float yoffset) override;
};


#endif //INC_2D_ENGINE_MAIN_SCENE_H
#endif//INC_2D_ENGINE_MAIN_SCENE_H
22 changes: 11 additions & 11 deletions Scenes/orth_scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
//

#include "orth_scene.h"
#include "logging.h"
#include "glm.hpp"
#include "gtc/matrix_transform.hpp"
#include "Camera.h"
#include "glad/glad.h"
#include "PhysicsEngine.h"
#include "CollisionEngine.h"
#include "DebugRendering.h"
#include "DebugRendering/DebugBox.h"
#include "DebugRendering/DebugLine.h"
#include "PhysicsEngine.h"
#include "glad/glad.h"
#include "glm.hpp"
#include "gtc/matrix_transform.hpp"
#include "logging.h"

static DebugBox *box;
void orth_scene::Init(Camera *camera) {
Expand Down Expand Up @@ -44,12 +44,12 @@ void orth_scene::Init(Camera *camera) {
}

void orth_scene::Update(double deltaTime) {
// std::cout << "orth_scene::update()" << std::endl;
// std::cout << "orth_scene::update()" << std::endl;


// create transformations
glm::mat4 view = glm::mat4(1.0f); // make sure to initialize matrix to identity matrix first
glm::mat4 projection = glm::mat4(1.0f);
glm::mat4 view = glm::mat4(1.0f);// make sure to initialize matrix to identity matrix first
glm::mat4 projection = glm::mat4(1.0f);

projection = glm::ortho((storage->window.width / 2.0f), -(storage->window.width / 2.0f),
-(storage->window.height / 2.0f), (storage->window.height / 2.0f),
Expand Down Expand Up @@ -80,7 +80,7 @@ void orth_scene::Update(double deltaTime) {
entity->position.y = -(storage->window.height / 2);
}

// entity->scale = glm::vec3(camera->fov * 7, 2.1f, 2.1f);
// entity->scale = glm::vec3(camera->fov * 7, 2.1f, 2.1f);

// And now im finally drawing the frame, if drew the frame
// before the collision we would see the blocks jittering and "glitching" up and down above y 0
Expand All @@ -91,7 +91,7 @@ void orth_scene::Update(double deltaTime) {
// Render UI Elements
{

// shaders[1]->apply();
// shaders[1]->apply();
draw_debug();
}
}
Expand Down
16 changes: 9 additions & 7 deletions include/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@
#define DEFAULT_SENSITIVITY 0.1f
#define DEFAULT_ZOOM 45.0f

#include "Vector.h"
#include "glm.hpp"
#include "gtc/matrix_transform.hpp"
#include "gtc/type_ptr.hpp"
#include "logging.h"
#include "vec3.hpp"


#include <iostream>

Expand Down Expand Up @@ -79,18 +78,21 @@ class Camera {

void HandleKeyboard(CameraMovement direction, double delta_time, bool sprinting = false) {
float velocity = this->movement_speed * delta_time * (sprinting ? this->sprint_speed : 1.);
glm::vec3 move_by = glm::vec3(0.0f, 0.0f, 0.0f);
if (direction == FORWARD) {
position += front * velocity;
glm::vec3 t = front * velocity;
move_by += t;
}
if (direction == BACKWARD) {
position -= front * velocity;
move_by -= (front * velocity);
}
if (direction == LEFT) {
position -= right * velocity;
move_by -= (right * velocity);
}
if (direction == RIGHT) {
position += right * velocity;
move_by += (right * velocity);
}
this->position += move_by;
}

void HandleMouseMovement(float x_offset, float y_offset) {
Expand Down Expand Up @@ -143,7 +145,7 @@ class Camera {
// front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
this->front = glm::normalize(front);

this->right = glm::normalize(glm::cross(this->front, this->global_up));
this->right = glm::normalize(glm::cross(this->global_up, this->front));
this->up = glm::normalize(glm::cross(this->right, this->front));
}
};
Expand Down
13 changes: 7 additions & 6 deletions include/Entity_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
#define INC_2D_ENGINE_ENTITY_T_H


#include "vec3.hpp"
#include "glad/glad.h"
#include "glm.hpp"
#include "gtc/matrix_transform.hpp"
#include "logging.h"
#include "vec3.hpp"
//#include "PhysicsEngine.h"

#include "Shader.h"
#include "Vector.h"

class PhysicsEngine;
class Entity_t {
Expand All @@ -22,20 +23,20 @@ class Entity_t {

public:
double timer;
Shader* shader;
Shader *shader;

class Physics {
public:
bool enabled = false;
PhysicsEngine* engine;
PhysicsEngine *engine;
} physics;

glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 rotation = glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 scale = glm::vec3(1.0f, 1.0f, 1.0f);

Entity_t(Shader* shader);
Entity_t(Shader* shader, glm::vec3 position, glm::vec3 rotation, glm::vec3 scale);
Entity_t(Shader *shader);
Entity_t(Shader *shader, glm::vec3 position, glm::vec3 rotation, glm::vec3 scale);

~Entity_t();

Expand All @@ -47,4 +48,4 @@ class Entity_t {
};


#endif //INC_2D_ENGINE_ENTITY_T_H
#endif//INC_2D_ENGINE_ENTITY_T_H
43 changes: 43 additions & 0 deletions include/Vector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// Created by Conni Bilham on 10/07/2023.
//

#ifndef INC_2D_ENGINE_VECTOR_H
#define INC_2D_ENGINE_VECTOR_H

class Vector {

public:
double x, y, z;

Vector(double x, double y, double z);
Vector(double x, double y);
Vector(double x);
Vector();

template<typename T>
Vector operator+(T &other);

template<typename T>
Vector operator-(T &other);

template<typename T>
Vector operator*(const T &other) const;

template<typename T>
Vector operator/(T &other);


bool operator==(const Vector &other);
bool operator!=(const Vector &other);

double magnitude();
Vector normalize();
double dot(const Vector &other);
Vector cross(const Vector &other);

Vector direction(Vector &other);
};


#endif//INC_2D_ENGINE_VECTOR_H
50 changes: 0 additions & 50 deletions include/vector.h

This file was deleted.

Loading

0 comments on commit 9a30593

Please sign in to comment.