From 5332f2038073a5aa28c302118932c4f2b87f2456 Mon Sep 17 00:00:00 2001 From: Im-Rises Date: Sun, 18 Dec 2022 00:02:13 -0500 Subject: [PATCH 01/12] wip --- .../ContactGenerator/ParticleCollide.h | 2 +- .../ParticleCollider/ParticleCollider.cpp | 15 -------- .../ParticleCollider/ParticleCollider.h | 23 ------------- .../ParticleCollider/ParticleCollider.cpp | 30 ++++++++++++++++ .../ParticleCollider/ParticleCollider.h | 34 +++++++++++++++++++ .../Collider/ParticleCollider/placeholder | 0 .../RigidbodyPrimitiveCollider.h | 2 +- .../Scene/Prefabs/ParticlePrefab.cpp | 4 +-- test/CMakeLists.txt | 1 - test/TestParticule/TestParticule.cpp | 23 ------------- test/TestParticule/TestParticule.h | 16 --------- 11 files changed, 68 insertions(+), 82 deletions(-) delete mode 100644 PhysicalEngine/ParticleContact/ParticleCollider/ParticleCollider.cpp delete mode 100644 PhysicalEngine/ParticleContact/ParticleCollider/ParticleCollider.h create mode 100644 PhysicalEngine/Scene/Components/Collider/ParticleCollider/ParticleCollider.cpp create mode 100644 PhysicalEngine/Scene/Components/Collider/ParticleCollider/ParticleCollider.h delete mode 100644 PhysicalEngine/Scene/Components/Collider/ParticleCollider/placeholder delete mode 100644 test/TestParticule/TestParticule.cpp delete mode 100644 test/TestParticule/TestParticule.h diff --git a/PhysicalEngine/ParticleContact/ContactGenerator/ParticleCollide.h b/PhysicalEngine/ParticleContact/ContactGenerator/ParticleCollide.h index 1bcc3b9..8b12968 100644 --- a/PhysicalEngine/ParticleContact/ContactGenerator/ParticleCollide.h +++ b/PhysicalEngine/ParticleContact/ContactGenerator/ParticleCollide.h @@ -3,7 +3,7 @@ #include "ParticleContactGenerator.h" -#include "../ParticleCollider/ParticleCollider.h" +#include "../../Scene/Components/Collider/ParticleCollider/ParticleCollider.h" #include diff --git a/PhysicalEngine/ParticleContact/ParticleCollider/ParticleCollider.cpp b/PhysicalEngine/ParticleContact/ParticleCollider/ParticleCollider.cpp deleted file mode 100644 index 629cca8..0000000 --- a/PhysicalEngine/ParticleContact/ParticleCollider/ParticleCollider.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include "ParticleCollider.h" - - -ParticleCollider::ParticleCollider(Particle* particle, float radius) { - m_radius = radius; - m_particle = particle; -} - -float ParticleCollider::getRadius() const { - return m_radius; -} - -Particle* ParticleCollider::getParticle() { - return m_particle; -} diff --git a/PhysicalEngine/ParticleContact/ParticleCollider/ParticleCollider.h b/PhysicalEngine/ParticleContact/ParticleCollider/ParticleCollider.h deleted file mode 100644 index b19db7f..0000000 --- a/PhysicalEngine/ParticleContact/ParticleCollider/ParticleCollider.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef PARTICLE_COLLIDER_H -#define PARTICLE_COLLIDER_H - -#include "../../Scene/Components/PhysicalComponent/Particle/Particle.h" - -class ParticleCollider { -private: - float m_radius; - Particle* m_particle; - -public: - ParticleCollider(Particle* particle, float radius); - -#pragma region Getter setter - - float getRadius() const; - - Particle* getParticle(); - -#pragma endregion -}; - -#endif // !PARTICLE_COLLIDER_H diff --git a/PhysicalEngine/Scene/Components/Collider/ParticleCollider/ParticleCollider.cpp b/PhysicalEngine/Scene/Components/Collider/ParticleCollider/ParticleCollider.cpp new file mode 100644 index 0000000..9bda23f --- /dev/null +++ b/PhysicalEngine/Scene/Components/Collider/ParticleCollider/ParticleCollider.cpp @@ -0,0 +1,30 @@ +#include "ParticleCollider.h" +#include "../../../GameObject.h" +#include "../../PhysicalComponent/Particle/Particle.h" +#include + +ParticleCollider::ParticleCollider(GameObject* gameObject, float radius) : Component(gameObject) { + m_radius = radius; + // m_particle = nullptr; + // gameObject->getComponentByClass(m_particle); +} + +float ParticleCollider::getRadius() const { + return m_radius; +} + +// Particle* ParticleCollider::getParticle() { +// return m_particle; +// } + +std::string ParticleCollider::getName() const { + return COMPONENT_TYPE; +} + +void ParticleCollider::update(float time) { +} + +void ParticleCollider::drawGui() { + ImGui::Text("Particle Collider"); + ImGui::DragFloat("Radius", &m_radius, 0.1f, 0.0f, 100.0f); +} diff --git a/PhysicalEngine/Scene/Components/Collider/ParticleCollider/ParticleCollider.h b/PhysicalEngine/Scene/Components/Collider/ParticleCollider/ParticleCollider.h new file mode 100644 index 0000000..d75b5a6 --- /dev/null +++ b/PhysicalEngine/Scene/Components/Collider/ParticleCollider/ParticleCollider.h @@ -0,0 +1,34 @@ +#ifndef PARTICLE_COLLIDER_H +#define PARTICLE_COLLIDER_H + +#include "../../Component.h" + +class Particle; + +#define PARTICLE_COLLIDER_COMPONENT "Particle_Collider" + +class ParticleCollider : public Component { +private: + static constexpr const char* COMPONENT_TYPE = PARTICLE_COLLIDER_COMPONENT; + float m_radius; + // Particle* m_particle; + +public: + ParticleCollider(GameObject* gameObject, float radius = 1); + +#pragma region Getter setter + + float getRadius() const; + + Particle* getParticle(); + + std::string getName() const override; + + void update(float time) override; + + void drawGui() override; + +#pragma endregion +}; + +#endif // !PARTICLE_COLLIDER_H diff --git a/PhysicalEngine/Scene/Components/Collider/ParticleCollider/placeholder b/PhysicalEngine/Scene/Components/Collider/ParticleCollider/placeholder deleted file mode 100644 index e69de29..0000000 diff --git a/PhysicalEngine/Scene/Components/Collider/RigidbodyCollider/RigidbodyPrimitiveCollider.h b/PhysicalEngine/Scene/Components/Collider/RigidbodyCollider/RigidbodyPrimitiveCollider.h index 08e9067..c0611ea 100644 --- a/PhysicalEngine/Scene/Components/Collider/RigidbodyCollider/RigidbodyPrimitiveCollider.h +++ b/PhysicalEngine/Scene/Components/Collider/RigidbodyCollider/RigidbodyPrimitiveCollider.h @@ -5,7 +5,7 @@ #include "../../Component.h" #include "../../../GameObject.h" -#define RIGIDBODY_PRIMITIVE_COLLIDER "RigidbodyPrimitiveColliderComponent" +#define RIGIDBODY_PRIMITIVE_COLLIDER "Rigidbody_Primitive_Collider" enum RigidbodyPrimitiveColliderType { RIGIDBODY_PRIMITIVE_COLLIDER_TYPE_NONE = 0, diff --git a/PhysicalEngine/Scene/Prefabs/ParticlePrefab.cpp b/PhysicalEngine/Scene/Prefabs/ParticlePrefab.cpp index 28a074e..c77da6a 100644 --- a/PhysicalEngine/Scene/Prefabs/ParticlePrefab.cpp +++ b/PhysicalEngine/Scene/Prefabs/ParticlePrefab.cpp @@ -1,5 +1,5 @@ #include "ParticlePrefab.h" -#include "../../ParticleContact/ParticleCollider/ParticleCollider.h" +#include "../Components/Collider/ParticleCollider/ParticleCollider.h" #include "../Components/Mesh/Sphere/Sphere.h" #include "../Components/PhysicalComponent/Particle/Particle.h" #include "../PhysicalEngine/Force/AnchoredSpring.h" @@ -11,7 +11,7 @@ ParticlePrefab::ParticlePrefab(Scene* scene) : GameObject(scene, new Sphere(1, 2 auto* particle = new Particle(this); particle->addForceToList(new AnchoredSpring({ 0, 0, 0 }, 0.5f, 0.5f)); addComponent(particle); - ParticleCollider particleCollider(particle, 1); + ParticleCollider particleCollider(GameObject, 1); scene->addParticleCollider(particleCollider); } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 14a5eec..2b01293 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -10,4 +10,3 @@ foreach (test ${SRCS_TEST}) add_executable(${testName} ${test} ${HEADERS} ${SRCS}) add_test(${testName} ${testName}) endforeach () - diff --git a/test/TestParticule/TestParticule.cpp b/test/TestParticule/TestParticule.cpp deleted file mode 100644 index 0869d1c..0000000 --- a/test/TestParticule/TestParticule.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include "TestParticule.h" - -TestParticule::TestParticule() { - m_testParticule = Particule(1, 1, 1, 0); -} - -int TestParticule::TestCalculePosition() { - m_testParticule.setSpeed(2, 0, 0); - Vector3d speedTest = m_testParticule.getSpeed(); - if (!(speedTest.getx() == 2 && speedTest.gety() == 0 && speedTest.getz() == 0)) { - printf("setSpeed dosn't work expected (2,0,0), but obtained "); - return 1; - } - m_testParticule.calculatePosition(2); - Vector3d positionTest = m_testParticule.getPosition(); - if (!(positionTest.getx() == 5 && positionTest.gety() == 1 && positionTest.getz() == 1)) { - printf("calculatePosition dosn't work expected (5,1,1), but obtained "); - return 1; - } - - printf("testParticule Ok"); - return 0; -} diff --git a/test/TestParticule/TestParticule.h b/test/TestParticule/TestParticule.h deleted file mode 100644 index 71a0e91..0000000 --- a/test/TestParticule/TestParticule.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef TESTPARTICULE_H -#define TESTPARTICULE_H - -#include "../PhysicalEngine/Scene/Components/PhysicalComponent/Particle/Particle.h" - -class TestParticule { - -public: - // Particule m_testParticule; - - TestParticule(); - - int TestCalculePosition(); -}; - -#endif // !TESTPARTICULE_H From e1406782c0c04f7e225448d191ea65c67045c754 Mon Sep 17 00:00:00 2001 From: Im-Rises Date: Sun, 18 Dec 2022 00:11:44 -0500 Subject: [PATCH 02/12] wip --- .../ContactGenerator/ParticleCollide.cpp | 41 +++++++++++-------- .../ContactGenerator/ParticleCollide.h | 9 ++-- .../ParticleCollider/ParticleCollider.h | 2 +- .../Scene/Prefabs/ParticlePrefab.cpp | 3 +- PhysicalEngine/Scene/Scene.cpp | 3 +- PhysicalEngine/Scene/Scene.h | 2 +- imgui.ini | 4 +- 7 files changed, 34 insertions(+), 30 deletions(-) diff --git a/PhysicalEngine/ParticleContact/ContactGenerator/ParticleCollide.cpp b/PhysicalEngine/ParticleContact/ContactGenerator/ParticleCollide.cpp index b6aa726..072f8b6 100644 --- a/PhysicalEngine/ParticleContact/ContactGenerator/ParticleCollide.cpp +++ b/PhysicalEngine/ParticleContact/ContactGenerator/ParticleCollide.cpp @@ -1,36 +1,45 @@ #include "ParticleCollide.h" +#include "../../Scene/GameObject.h" + ParticleCollide::ParticleCollide(float elast) { - elasticity=elast; + elasticity = elast; } -void ParticleCollide::addCollider(ParticleCollider particleCollider) { - m_colliders.push_back(particleCollider); +void ParticleCollide::addCollider(ParticleCollider* particleCollider) { + m_colliders.push_back(particleCollider); } -int ParticleCollide::addContact(ParticleContact *particleContact, unsigned int limit, unsigned int current) { - for (int i = 0; i < m_colliders.size(); i++) { - for (int j = i + 1; j < m_colliders.size(); j++) { - if (current < limit) { - Particle *particle0 = m_colliders[i].getParticle(); - Particle *particle1 = m_colliders[j].getParticle(); +int ParticleCollide::addContact(ParticleContact* particleContact, unsigned int limit, unsigned int current) { + for (int i = 0; i < m_colliders.size(); i++) + { + for (int j = i + 1; j < m_colliders.size(); j++) + { + if (current < limit) + { + Particle* particle0 = nullptr; + m_colliders[i]->getGameObject()->getComponentByClass(particle0); + Particle* particle1 = nullptr; + m_colliders[j]->getGameObject()->getComponentByClass(particle1); + // Particle* particle0 = m_colliders[i]->getParticle(); + // Particle* particle1 = m_colliders[j]->getParticle(); float distance = particle0->getPosition().distance(particle1->getPosition()); - float sumRadius = m_colliders[i].getRadius() + m_colliders[j].getRadius(); - if (distance < sumRadius) { - particleContact[current].SetParticles(particle0, particle1); + float sumRadius = m_colliders[i]->getRadius() + m_colliders[j]->getRadius(); + if (distance < sumRadius) + { + particleContact[current].SetParticles(particle0, particle1); particleContact[current].setPenetration(sumRadius - distance); particleContact[current].setElasticity(elasticity); Vector3d normalParticle = particle0->getPosition() - particle1->getPosition(); particleContact[current].setContactNormal(normalParticle); current += 1; } - } else { + } + else + { return current; } - } } return current; } - - diff --git a/PhysicalEngine/ParticleContact/ContactGenerator/ParticleCollide.h b/PhysicalEngine/ParticleContact/ContactGenerator/ParticleCollide.h index 8b12968..8d7a132 100644 --- a/PhysicalEngine/ParticleContact/ContactGenerator/ParticleCollide.h +++ b/PhysicalEngine/ParticleContact/ContactGenerator/ParticleCollide.h @@ -10,19 +10,16 @@ class ParticleCollide : public ParticleContactGenerator { private: - - std::vector m_colliders; + std::vector m_colliders; float elasticity; public: explicit ParticleCollide(float elast); - void addCollider(ParticleCollider particleCollider); - - int addContact(ParticleContact *particleContact, unsigned int limit, unsigned int current) override; - + void addCollider(ParticleCollider* particleCollider); + int addContact(ParticleContact* particleContact, unsigned int limit, unsigned int current) override; }; #endif // !PARTICLECOLLIDE_H diff --git a/PhysicalEngine/Scene/Components/Collider/ParticleCollider/ParticleCollider.h b/PhysicalEngine/Scene/Components/Collider/ParticleCollider/ParticleCollider.h index d75b5a6..d947443 100644 --- a/PhysicalEngine/Scene/Components/Collider/ParticleCollider/ParticleCollider.h +++ b/PhysicalEngine/Scene/Components/Collider/ParticleCollider/ParticleCollider.h @@ -20,7 +20,7 @@ class ParticleCollider : public Component { float getRadius() const; - Particle* getParticle(); + // Particle* getParticle(); std::string getName() const override; diff --git a/PhysicalEngine/Scene/Prefabs/ParticlePrefab.cpp b/PhysicalEngine/Scene/Prefabs/ParticlePrefab.cpp index c77da6a..08c2fca 100644 --- a/PhysicalEngine/Scene/Prefabs/ParticlePrefab.cpp +++ b/PhysicalEngine/Scene/Prefabs/ParticlePrefab.cpp @@ -11,8 +11,7 @@ ParticlePrefab::ParticlePrefab(Scene* scene) : GameObject(scene, new Sphere(1, 2 auto* particle = new Particle(this); particle->addForceToList(new AnchoredSpring({ 0, 0, 0 }, 0.5f, 0.5f)); addComponent(particle); - ParticleCollider particleCollider(GameObject, 1); - scene->addParticleCollider(particleCollider); + scene->addParticleCollider(new ParticleCollider(this, 1)); } ParticlePrefab::~ParticlePrefab() { diff --git a/PhysicalEngine/Scene/Scene.cpp b/PhysicalEngine/Scene/Scene.cpp index cd5612d..0f8243e 100644 --- a/PhysicalEngine/Scene/Scene.cpp +++ b/PhysicalEngine/Scene/Scene.cpp @@ -146,11 +146,10 @@ unsigned int Scene::getFrameBufferId() const { return fbo; } -void Scene::addParticleCollider(ParticleCollider particleCollider) { +void Scene::addParticleCollider(ParticleCollider* particleCollider) { particleCollide.addCollider(particleCollider); } - std::vector& Scene::getGameObjects() { return gameObjects; } diff --git a/PhysicalEngine/Scene/Scene.h b/PhysicalEngine/Scene/Scene.h index 1c0642f..db14edc 100644 --- a/PhysicalEngine/Scene/Scene.h +++ b/PhysicalEngine/Scene/Scene.h @@ -69,7 +69,7 @@ class Scene { public: ParticleContactGeneratorRegistry getParticleContactGeneratorRegistry(); - void addParticleCollider(ParticleCollider particleCollider); + void addParticleCollider(ParticleCollider* particleCollider); unsigned int getFrameBufferId() const; diff --git a/imgui.ini b/imgui.ini index 6ca1657..d7f09b7 100644 --- a/imgui.ini +++ b/imgui.ini @@ -49,8 +49,8 @@ Size=872,706 Collapsed=0 [Window][Scene View] -Pos=217,16 -Size=751,477 +Pos=216,22 +Size=752,475 Collapsed=0 [Window][Inspector] From c92b9f3adc871b1085ea5af492294e77de8f589c Mon Sep 17 00:00:00 2001 From: Im-Rises Date: Sun, 18 Dec 2022 00:25:10 -0500 Subject: [PATCH 03/12] wip --- .../ContactGenerator/ParticleCollide.cpp | 9 ++++++ .../ContactGenerator/ParticleCollide.h | 2 ++ .../Scene/Prefabs/ParticlePrefab.cpp | 2 +- PhysicalEngine/Scene/Scene.cpp | 29 +++++++++++++++---- PhysicalEngine/Scene/Scene.h | 6 +++- imgui.ini | 4 +-- 6 files changed, 42 insertions(+), 10 deletions(-) diff --git a/PhysicalEngine/ParticleContact/ContactGenerator/ParticleCollide.cpp b/PhysicalEngine/ParticleContact/ContactGenerator/ParticleCollide.cpp index 072f8b6..10e74e7 100644 --- a/PhysicalEngine/ParticleContact/ContactGenerator/ParticleCollide.cpp +++ b/PhysicalEngine/ParticleContact/ContactGenerator/ParticleCollide.cpp @@ -21,8 +21,14 @@ int ParticleCollide::addContact(ParticleContact* particleContact, unsigned int l m_colliders[i]->getGameObject()->getComponentByClass(particle0); Particle* particle1 = nullptr; m_colliders[j]->getGameObject()->getComponentByClass(particle1); + + // Check if the particles have colliders if not, skip these particles // Particle* particle0 = m_colliders[i]->getParticle(); // Particle* particle1 = m_colliders[j]->getParticle(); + if (particle0 == nullptr || particle1 == nullptr) + continue; + + // Check if the particles are colliding float distance = particle0->getPosition().distance(particle1->getPosition()); float sumRadius = m_colliders[i]->getRadius() + m_colliders[j]->getRadius(); if (distance < sumRadius) @@ -43,3 +49,6 @@ int ParticleCollide::addContact(ParticleContact* particleContact, unsigned int l } return current; } +void ParticleCollide::cleanColliders() { + m_colliders.clear(); +} diff --git a/PhysicalEngine/ParticleContact/ContactGenerator/ParticleCollide.h b/PhysicalEngine/ParticleContact/ContactGenerator/ParticleCollide.h index 8d7a132..13f0be0 100644 --- a/PhysicalEngine/ParticleContact/ContactGenerator/ParticleCollide.h +++ b/PhysicalEngine/ParticleContact/ContactGenerator/ParticleCollide.h @@ -20,6 +20,8 @@ class ParticleCollide : public ParticleContactGenerator { void addCollider(ParticleCollider* particleCollider); int addContact(ParticleContact* particleContact, unsigned int limit, unsigned int current) override; + + void cleanColliders(); }; #endif // !PARTICLECOLLIDE_H diff --git a/PhysicalEngine/Scene/Prefabs/ParticlePrefab.cpp b/PhysicalEngine/Scene/Prefabs/ParticlePrefab.cpp index 08c2fca..c8f3b2f 100644 --- a/PhysicalEngine/Scene/Prefabs/ParticlePrefab.cpp +++ b/PhysicalEngine/Scene/Prefabs/ParticlePrefab.cpp @@ -11,7 +11,7 @@ ParticlePrefab::ParticlePrefab(Scene* scene) : GameObject(scene, new Sphere(1, 2 auto* particle = new Particle(this); particle->addForceToList(new AnchoredSpring({ 0, 0, 0 }, 0.5f, 0.5f)); addComponent(particle); - scene->addParticleCollider(new ParticleCollider(this, 1)); + // scene->addParticleCollider(new ParticleCollider(this, 1)); } ParticlePrefab::~ParticlePrefab() { diff --git a/PhysicalEngine/Scene/Scene.cpp b/PhysicalEngine/Scene/Scene.cpp index 0f8243e..7248594 100644 --- a/PhysicalEngine/Scene/Scene.cpp +++ b/PhysicalEngine/Scene/Scene.cpp @@ -80,12 +80,14 @@ void Scene::update(float deltaTime) { physicHandler.update(gameObject, deltaTime); } - // Detect collision + // Detect particles collision + collectParticleColliders(); ParticleContact* particleContacts = particleContactGeneratorRegistry.generateAllContacts(); // Resolve collisions ParticleContact test = particleContacts[0]; particleContactResolver.resolveContact(particleContacts, particleContactGeneratorRegistry.getSize(), deltaTime); + cleanParticleColliders(); // Clean octree octree.CleanOctree(octree.root); @@ -102,8 +104,6 @@ void Scene::update(float deltaTime) { } // Test collisions octree.TestAllCollisions(octree.root); - - // } } void Scene::draw(int display_w, int display_h) { @@ -146,9 +146,9 @@ unsigned int Scene::getFrameBufferId() const { return fbo; } -void Scene::addParticleCollider(ParticleCollider* particleCollider) { - particleCollide.addCollider(particleCollider); -} +// void Scene::addParticleCollider(ParticleCollider* particleCollider) { +// particleCollide.addCollider(particleCollider); +// } std::vector& Scene::getGameObjects() { return gameObjects; @@ -177,6 +177,7 @@ GameObject* Scene::getPtrGameObjectByIndex(int index) const { Camera* Scene::getCameraPtr() { return &camera; } + void Scene::deleteGameObject(GameObject* gameObject) { for (auto it = gameObjects.begin(); it != gameObjects.end(); ++it) { @@ -199,3 +200,19 @@ GameObject* Scene::createGameObject(std::string name) { } } } + +void Scene::cleanParticleColliders() { + particleCollide.cleanColliders(); +} + +void Scene::collectParticleColliders() { + for (GameObject* gameObject : gameObjects) + { + ParticleCollider* particleCollider = nullptr; + gameObject->getComponentByClass(particleCollider); + if (particleCollider != nullptr) + { + particleCollide.addCollider(particleCollider); + } + } +} diff --git a/PhysicalEngine/Scene/Scene.h b/PhysicalEngine/Scene/Scene.h index db14edc..044ea09 100644 --- a/PhysicalEngine/Scene/Scene.h +++ b/PhysicalEngine/Scene/Scene.h @@ -69,7 +69,11 @@ class Scene { public: ParticleContactGeneratorRegistry getParticleContactGeneratorRegistry(); - void addParticleCollider(ParticleCollider* particleCollider); +// void addParticleCollider(ParticleCollider* particleCollider); + + void collectParticleColliders(); + + void cleanParticleColliders(); unsigned int getFrameBufferId() const; diff --git a/imgui.ini b/imgui.ini index d7f09b7..ddd3f03 100644 --- a/imgui.ini +++ b/imgui.ini @@ -9,7 +9,7 @@ Size=201,52 Collapsed=0 [Window][Hierarchy] -Pos=7,108 +Pos=7,107 Size=201,296 Collapsed=0 @@ -54,7 +54,7 @@ Size=752,475 Collapsed=0 [Window][Inspector] -Pos=972,21 +Pos=973,20 Size=301,693 Collapsed=0 From 208e6a4f99e30ddce46f7caeec8d4940d9f0ca81 Mon Sep 17 00:00:00 2001 From: Im-Rises Date: Sun, 18 Dec 2022 00:29:45 -0500 Subject: [PATCH 04/12] Partile issues --- PhysicalEngine/Scene/Components/Component.cpp | 12 ++++++++---- PhysicalEngine/Scene/Components/Component.h | 2 +- imgui.ini | 2 +- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/PhysicalEngine/Scene/Components/Component.cpp b/PhysicalEngine/Scene/Components/Component.cpp index 319a664..9aa2cfa 100644 --- a/PhysicalEngine/Scene/Components/Component.cpp +++ b/PhysicalEngine/Scene/Components/Component.cpp @@ -1,5 +1,6 @@ #include "Component.h" -//#include "Collider/Collider.h" +// #include "Collider/Collider.h" +#include "Collider/ParticleCollider/ParticleCollider.h" #include "Collider/RigidbodyCollider/RigidbodyCuboidRectangleCollider/RigidbodyCuboidRectangleCollider.h" #include "Collider/RigidbodyCollider/RigidbodyPlaneCollider/RigidbodyPlaneCollider.h" #include "Collider/RigidbodyCollider/RigidbodySphereCollider/RigidbodySphereCollider.h" @@ -8,7 +9,7 @@ #include -const char* Component::componentsNamesList[] = { RIGIDBODY_COMPONENT, PARTICLE_COMPONENT, RIGIDBODY_PLANE_COLLIDER, RIGIDBODY_SPHERE_COLLIDER, RIGIDBODY_CUBOID_RECTANGLE_COLLIDER }; +const char* Component::componentsNamesList[] = { RIGIDBODY_COMPONENT, PARTICLE_COMPONENT, RIGIDBODY_PLANE_COLLIDER, RIGIDBODY_SPHERE_COLLIDER, RIGIDBODY_CUBOID_RECTANGLE_COLLIDER, PARTICLE_COLLIDER_COMPONENT }; // const char* Component::componentsNamesList[] = { RIGIDBODY_COMPONENT, PARTICLE_COMPONENT, COLLIDER_COMPONENT }; Component::Component(GameObject* gameObject) { @@ -49,6 +50,9 @@ Component* Component::createComponent(const std::string& name, GameObject* gameO case 4: { return new RigidbodyCuboidRectangleCollider(gameObject, 1.0f, 1.0f, 1.0f); } + case 5: { + return new ParticleCollider(gameObject, 1.0f); + } default: { std::cerr << "Component::createComponent: Unknown component name" << std::endl; return nullptr; @@ -57,8 +61,8 @@ Component* Component::createComponent(const std::string& name, GameObject* gameO } index++; } - std::cerr << "Component::createComponent: Unknown component name" << std::endl; - return nullptr; + // std::cerr << "Component::createComponent: Unknown component name" << std::endl; + // return nullptr; } GameObject* Component::getGameObject() { return m_gameObject; diff --git a/PhysicalEngine/Scene/Components/Component.h b/PhysicalEngine/Scene/Components/Component.h index 89d8aa2..714a2ae 100644 --- a/PhysicalEngine/Scene/Components/Component.h +++ b/PhysicalEngine/Scene/Components/Component.h @@ -16,7 +16,7 @@ class GameObject; class Component { public: - static const char* componentsNamesList[5]; + static const char* componentsNamesList[6]; private: static constexpr const char* COMPONENT_TYPE = "Component"; diff --git a/imgui.ini b/imgui.ini index ddd3f03..5714eb9 100644 --- a/imgui.ini +++ b/imgui.ini @@ -54,7 +54,7 @@ Size=752,475 Collapsed=0 [Window][Inspector] -Pos=973,20 +Pos=973,19 Size=301,693 Collapsed=0 From 0c56b6c5d93934b25364c7a187f08e25ba5a1ac4 Mon Sep 17 00:00:00 2001 From: Im-Rises Date: Sun, 18 Dec 2022 00:41:28 -0500 Subject: [PATCH 05/12] wip --- PhysicalEngine/Game.cpp | 25 ++++++++++++++++--- .../PhysicalComponent/Rigidbody/Rigidbody.cpp | 3 +++ .../PhysicalComponent/Rigidbody/Rigidbody.h | 2 ++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/PhysicalEngine/Game.cpp b/PhysicalEngine/Game.cpp index 0fccff1..9ebf3cb 100644 --- a/PhysicalEngine/Game.cpp +++ b/PhysicalEngine/Game.cpp @@ -2,6 +2,7 @@ #include "Force/Spring.h" #include "Scene/Components/Mesh/Cylinder/Cylinder.h" +#include "Scene/Components/Mesh/Sphere/Sphere.h" #include "Scene/Components/PhysicalComponent/Particle/Particle.h" #include "Scene/GameObject.h" #include "Scene/Prefabs/ParticlePrefab.h" @@ -51,25 +52,43 @@ void Game::start(Scene* s) { scene->addGameObject(plan); plan->addComponent(new RigidbodyPlaneCollider(plan, 10, 10)); + auto* sphere = new RigidbodyPrefab(scene, new Sphere(1)); + sphere->addComponent(new RigidbodySphereCollider(sphere, 1)); + sphere->transform.setPosition({ -4, 0, 0 }); + scene->addGameObject(sphere); + auto* rigidbodyPrefab = new RigidbodyPrefab(scene); scene->addGameObject(rigidbodyPrefab); - rigidbodyPrefab->transform.positionX = -3; + rigidbodyPrefab->transform.positionX = -1.5; rigidbodyPrefab->transform.positionY = 3; - rigidbodyPrefab->transform.positionZ = -3; + rigidbodyPrefab->transform.positionZ = 0; Rigidbody* rigidbody; rigidbodyPrefab->getComponentByClass(rigidbody); + rigidbody->setAngularSpeed({ 0, 1, 0 }); RigidbodyCuboidRectangleCollider* rigidbodyCuboidRectangleCollider = new RigidbodyCuboidRectangleCollider(rigidbodyPrefab, 1, 0.5f, 0.5f); rigidbodyPrefab->addComponent(rigidbodyCuboidRectangleCollider); auto* rigidbodyPrefab2 = new RigidbodyPrefab(scene); - rigidbodyPrefab2->transform.positionX = 3; + rigidbodyPrefab2->transform.positionX = 1.5; rigidbodyPrefab2->transform.positionY = 3; rigidbodyPrefab2->transform.positionZ = -3; scene->addGameObject(rigidbodyPrefab2); Rigidbody* rigidbody2; rigidbodyPrefab2->getComponentByClass(rigidbody2); + rigidbody2->setAngularSpeed({ 0, 0, -1 }); RigidbodyCuboidRectangleCollider* rigidbodyCuboidRectangleCollider2 = new RigidbodyCuboidRectangleCollider(rigidbodyPrefab2, 1, 0.5f, 0.5f); rigidbodyPrefab2->addComponent(rigidbodyCuboidRectangleCollider2); + + auto* rigidbodyPrefab3 = new RigidbodyPrefab(scene); + rigidbodyPrefab3->transform.positionX = 3; + rigidbodyPrefab3->transform.positionY = 3; + rigidbodyPrefab3->transform.positionZ = 0; + scene->addGameObject(rigidbodyPrefab3); + Rigidbody* rigidbody3; + rigidbodyPrefab3->getComponentByClass(rigidbody3); + rigidbody3->setAngularSpeed({ 2, 0, 0 }); + RigidbodyCuboidRectangleCollider* rigidbodyCuboidRectangleCollider3 = new RigidbodyCuboidRectangleCollider(rigidbodyPrefab3, 1, 0.5f, 0.5f); + rigidbodyPrefab3->addComponent(rigidbodyCuboidRectangleCollider3); } void Game::goLeft() { diff --git a/PhysicalEngine/Scene/Components/PhysicalComponent/Rigidbody/Rigidbody.cpp b/PhysicalEngine/Scene/Components/PhysicalComponent/Rigidbody/Rigidbody.cpp index 73c08f6..1e99c48 100644 --- a/PhysicalEngine/Scene/Components/PhysicalComponent/Rigidbody/Rigidbody.cpp +++ b/PhysicalEngine/Scene/Components/PhysicalComponent/Rigidbody/Rigidbody.cpp @@ -274,3 +274,6 @@ void Rigidbody::stop() { isKinematic = true; } +void Rigidbody::setAngularSpeed(const Vector3d& angularSpeed) { + m_angularSpeed = angularSpeed; +} diff --git a/PhysicalEngine/Scene/Components/PhysicalComponent/Rigidbody/Rigidbody.h b/PhysicalEngine/Scene/Components/PhysicalComponent/Rigidbody/Rigidbody.h index 4dda6ee..cbf715b 100644 --- a/PhysicalEngine/Scene/Components/PhysicalComponent/Rigidbody/Rigidbody.h +++ b/PhysicalEngine/Scene/Components/PhysicalComponent/Rigidbody/Rigidbody.h @@ -70,6 +70,8 @@ class Rigidbody : public PhysicalComponent { Vector3d getAngularSpeed() const; + void setAngularSpeed(const Vector3d& angularSpeed); + void deleteForceAtPoint(ForceGenerator* forceGenerator); // template From 68ff0832904bdc7239bc2f3ce64309024dac68ce Mon Sep 17 00:00:00 2001 From: Im-Rises Date: Sun, 18 Dec 2022 00:43:30 -0500 Subject: [PATCH 06/12] wip --- PhysicalEngine/Game.cpp | 150 ++++++++++++++++---------------- PhysicalEngine/Game.h | 16 ++-- PhysicalEngine/InputManager.cpp | 32 +++---- 3 files changed, 99 insertions(+), 99 deletions(-) diff --git a/PhysicalEngine/Game.cpp b/PhysicalEngine/Game.cpp index 9ebf3cb..0354c3e 100644 --- a/PhysicalEngine/Game.cpp +++ b/PhysicalEngine/Game.cpp @@ -91,78 +91,78 @@ void Game::start(Scene* s) { rigidbodyPrefab3->addComponent(rigidbodyCuboidRectangleCollider3); } -void Game::goLeft() { - // m_p->setSpeed(-m_speed, 0, 0); - - for (auto& gameObject : scene->getGameObjects()) - { - auto* particle = dynamic_cast(gameObject->getComponentByName(PARTICLE_COMPONENT)); - if (particle != nullptr) - { - AnchoredSpring* anchoredSpring; - particle->getForceByClass(anchoredSpring); - if (anchoredSpring != nullptr) - { - anchoredSpring->translate({ -m_speed, 0, 0 }); - } - } - } -} - -void Game::goRight() { - // m_p->setSpeed(m_speed, 0, 0); - - for (auto& gameObject : scene->getGameObjects()) - { - auto* particle = dynamic_cast(gameObject->getComponentByName(PARTICLE_COMPONENT)); - if (particle != nullptr) - { - AnchoredSpring* anchoredSpring; - particle->getForceByClass(anchoredSpring); - if (anchoredSpring != nullptr) - { - anchoredSpring->translate({ m_speed, 0, 0 }); - } - } - } -} - -void Game::goUp() { - // m_p->setSpeed(0, m_speed, 0); - - for (auto& gameObject : scene->getGameObjects()) - { - auto* particle = dynamic_cast(gameObject->getComponentByName(PARTICLE_COMPONENT)); - if (particle != nullptr) - { - AnchoredSpring* anchoredSpring; - particle->getForceByClass(anchoredSpring); - if (anchoredSpring != nullptr) - { - anchoredSpring->translate({ 0, m_speed, 0 }); - } - } - } -} - -void Game::goDown() { - // m_p->setSpeed(0, -m_speed, 0); - - for (auto& gameObject : scene->getGameObjects()) - { - auto* particle = dynamic_cast(gameObject->getComponentByName(PARTICLE_COMPONENT)); - if (particle != nullptr) - { - AnchoredSpring* anchoredSpring; - particle->getForceByClass(anchoredSpring); - if (anchoredSpring != nullptr) - { - anchoredSpring->translate({ 0, -m_speed, 0 }); - } - } - } -} - -float* Game::getPtrSpeed() { - return &m_speed; -} +//void Game::goLeft() { +// // m_p->setSpeed(-m_speed, 0, 0); +// +// for (auto& gameObject : scene->getGameObjects()) +// { +// auto* particle = dynamic_cast(gameObject->getComponentByName(PARTICLE_COMPONENT)); +// if (particle != nullptr) +// { +// AnchoredSpring* anchoredSpring; +// particle->getForceByClass(anchoredSpring); +// if (anchoredSpring != nullptr) +// { +// anchoredSpring->translate({ -m_speed, 0, 0 }); +// } +// } +// } +//} +// +//void Game::goRight() { +// // m_p->setSpeed(m_speed, 0, 0); +// +// for (auto& gameObject : scene->getGameObjects()) +// { +// auto* particle = dynamic_cast(gameObject->getComponentByName(PARTICLE_COMPONENT)); +// if (particle != nullptr) +// { +// AnchoredSpring* anchoredSpring; +// particle->getForceByClass(anchoredSpring); +// if (anchoredSpring != nullptr) +// { +// anchoredSpring->translate({ m_speed, 0, 0 }); +// } +// } +// } +//} +// +//void Game::goUp() { +// // m_p->setSpeed(0, m_speed, 0); +// +// for (auto& gameObject : scene->getGameObjects()) +// { +// auto* particle = dynamic_cast(gameObject->getComponentByName(PARTICLE_COMPONENT)); +// if (particle != nullptr) +// { +// AnchoredSpring* anchoredSpring; +// particle->getForceByClass(anchoredSpring); +// if (anchoredSpring != nullptr) +// { +// anchoredSpring->translate({ 0, m_speed, 0 }); +// } +// } +// } +//} +// +//void Game::goDown() { +// // m_p->setSpeed(0, -m_speed, 0); +// +// for (auto& gameObject : scene->getGameObjects()) +// { +// auto* particle = dynamic_cast(gameObject->getComponentByName(PARTICLE_COMPONENT)); +// if (particle != nullptr) +// { +// AnchoredSpring* anchoredSpring; +// particle->getForceByClass(anchoredSpring); +// if (anchoredSpring != nullptr) +// { +// anchoredSpring->translate({ 0, -m_speed, 0 }); +// } +// } +// } +//} + +//float* Game::getPtrSpeed() { +// return &m_speed; +//} diff --git a/PhysicalEngine/Game.h b/PhysicalEngine/Game.h index db94fc1..003a216 100644 --- a/PhysicalEngine/Game.h +++ b/PhysicalEngine/Game.h @@ -20,16 +20,16 @@ class Game { void start(Scene* scene); - void goLeft(); - - void goRight(); - - void goUp(); - - void goDown(); +// void goLeft(); +// +// void goRight(); +// +// void goUp(); +// +// void goDown(); public: - float* getPtrSpeed(); +// float* getPtrSpeed(); }; #endif // GAME_H diff --git a/PhysicalEngine/InputManager.cpp b/PhysicalEngine/InputManager.cpp index 2af091d..96996d4 100644 --- a/PhysicalEngine/InputManager.cpp +++ b/PhysicalEngine/InputManager.cpp @@ -46,22 +46,22 @@ void InputManager::keyPressed(GLFWwindow* window, int key, PhysicalEngineLaunche glfwSetWindowShouldClose(window, GLFW_TRUE); break; } - case GLFW_KEY_RIGHT: { - engine->game.goRight(); - break; - } - case GLFW_KEY_LEFT: { - engine->game.goLeft(); - break; - } - case GLFW_KEY_UP: { - engine->game.goUp(); - break; - } - case GLFW_KEY_DOWN: { - engine->game.goDown(); - break; - } + // case GLFW_KEY_RIGHT: { + // engine->game.goRight(); + // break; + // } + // case GLFW_KEY_LEFT: { + // engine->game.goLeft(); + // break; + // } + // case GLFW_KEY_UP: { + // engine->game.goUp(); + // break; + // } + // case GLFW_KEY_DOWN: { + // engine->game.goDown(); + // break; + // } // case GLFW_KEY_F: { // engine->focusCameraOnGameObject(); // break; From 99f62d36c7f77b15d573969cf32ffde7d089c572 Mon Sep 17 00:00:00 2001 From: Im-Rises Date: Sun, 18 Dec 2022 01:32:11 -0500 Subject: [PATCH 07/12] wip --- PhysicalEngine/InputManager.cpp | 125 +++++++++++++++------- PhysicalEngine/InputManager.h | 8 +- PhysicalEngine/PhysicalEngineLauncher.cpp | 2 + PhysicalEngine/Scene/Camera.cpp | 48 ++++++--- PhysicalEngine/Scene/Camera.h | 16 ++- PhysicalEngine/Scene/Scene.cpp | 3 + imgui.ini | 2 +- 7 files changed, 144 insertions(+), 60 deletions(-) diff --git a/PhysicalEngine/InputManager.cpp b/PhysicalEngine/InputManager.cpp index 96996d4..84af729 100644 --- a/PhysicalEngine/InputManager.cpp +++ b/PhysicalEngine/InputManager.cpp @@ -13,7 +13,7 @@ bool InputManager::mouseLeftButtonPressed = false; float InputManager::mouseLastPosX = 0.0; float InputManager::mouseLastPosY = 0.0; -float InputManager::movementSpeed = 0.2f; +GLFWwindow* InputManager::m_window = nullptr; void InputManager::key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) { auto* engine = (PhysicalEngineLauncher*)glfwGetWindowUserPointer(window); @@ -24,28 +24,45 @@ void InputManager::key_callback(GLFWwindow* window, int key, int scancode, int a keyPressed(window, key, engine); break; } - case GLFW_RELEASE: { - keyReleased(window, key, engine); - break; - } - case GLFW_REPEAT: { - keyRepeated(window, key, engine); - break; - } - default: { - std::cout << "Unknown keyboard action" << std::endl; - break; - } + // case GLFW_RELEASE: { + // keyReleased(window, key, engine); + // break; + // } + // case GLFW_REPEAT: { + // keyRepeated(window, key, engine); + // break; + // } + // default: { + // std::cout << "Unknown keyboard action" << std::endl; + // break; + // } } } void InputManager::keyPressed(GLFWwindow* window, int key, PhysicalEngineLauncher* engine) { + Camera* camera = engine->scene->getCameraPtr(); switch (key) { case GLFW_KEY_ESCAPE: { glfwSetWindowShouldClose(window, GLFW_TRUE); break; } + // case GLFW_KEY_UP: { + // camera->moveForward(); + // break; + // } + // case GLFW_KEY_DOWN: { + // camera->moveBackward(); + // break; + // } + // case GLFW_KEY_LEFT: { + // camera->moveLeft(); + // break; + // } + // case GLFW_KEY_RIGHT: { + // camera->moveRight(); + // break; + // } // case GLFW_KEY_RIGHT: { // engine->game.goRight(); // break; @@ -76,34 +93,47 @@ void InputManager::keyPressed(GLFWwindow* window, int key, PhysicalEngineLaunche } } -void InputManager::keyReleased(GLFWwindow* window, int key, PhysicalEngineLauncher* engine) { -} - -void InputManager::keyRepeated(GLFWwindow* window, int key, PhysicalEngineLauncher* engine) { - Camera* camera = engine->scene->getCameraPtr(); - switch (key) - { - case GLFW_KEY_UP: { - camera->moveForward(movementSpeed); - break; - } - case GLFW_KEY_DOWN: { - camera->moveBackward(movementSpeed); - break; - } - case GLFW_KEY_LEFT: { - camera->moveLeft(movementSpeed); - break; - } - case GLFW_KEY_RIGHT: { - camera->moveRight(movementSpeed); - break; - } - default: { - break; - } - } -} +// void InputManager::keyReleased(GLFWwindow* window, int key, PhysicalEngineLauncher* engine) { +// } +// +// void InputManager::keyRepeated(GLFWwindow* window, int key, PhysicalEngineLauncher* engine) { +// // // measure number of function calls per second +// // static int counter = 0; +// // static double lastTime = glfwGetTime(); +// // double currentTime = glfwGetTime(); +// // counter++; +// // if (currentTime - lastTime >= 1.0) +// // { // If last prinf() was more than 1 sec ago +// // // printf and reset timer +// // printf("%d", counter); +// // counter = 0; +// // lastTime += 1.0; +// // } +// +// Camera* camera = engine->scene->getCameraPtr(); +// switch (key) +// { +// case GLFW_KEY_UP: { +// camera->moveForward(); +// break; +// } +// case GLFW_KEY_DOWN: { +// camera->moveBackward(); +// break; +// } +// case GLFW_KEY_LEFT: { +// camera->moveLeft(); +// break; +// } +// case GLFW_KEY_RIGHT: { +// camera->moveRight(); +// break; +// } +// default: { +// break; +// } +// } +// } // void InputManager::scroll_callback(GLFWwindow* window, double xOffset, double yOffset) { // auto* engine = (PhysicalEngineLauncher*)glfwGetWindowUserPointer(window); @@ -127,3 +157,16 @@ void InputManager::mouse_button_callback(GLFWwindow* window, int button, int act mouseRightButtonPressed = (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS); mouseLeftButtonPressed = (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS); } + +bool InputManager::isForwardKeyPressed() { + return glfwGetKey(m_window, GLFW_KEY_UP) == GLFW_PRESS; +} +bool InputManager::isBackwardKeyPressed() { + return glfwGetKey(m_window, GLFW_KEY_DOWN) == GLFW_PRESS; +} +bool InputManager::isLeftKeyPressed() { + return glfwGetKey(m_window, GLFW_KEY_LEFT) == GLFW_PRESS; +} +bool InputManager::isRightKeyPressed() { + return glfwGetKey(m_window, GLFW_KEY_RIGHT) == GLFW_PRESS; +} diff --git a/PhysicalEngine/InputManager.h b/PhysicalEngine/InputManager.h index 2c58fc8..4912075 100644 --- a/PhysicalEngine/InputManager.h +++ b/PhysicalEngine/InputManager.h @@ -13,9 +13,9 @@ class InputManager { static float mouseLastPosX; static float mouseLastPosY; - static float movementSpeed; public: + static GLFWwindow* m_window; static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods); private: @@ -31,6 +31,12 @@ class InputManager { static void cursor_position_callback(GLFWwindow* window, double xPos, double yPos); static void mouse_button_callback(GLFWwindow* window, int button, int action, int mods); + +public: + static bool isForwardKeyPressed(); + static bool isBackwardKeyPressed(); + static bool isLeftKeyPressed(); + static bool isRightKeyPressed(); }; #endif // INPUT_MANAGER_H diff --git a/PhysicalEngine/PhysicalEngineLauncher.cpp b/PhysicalEngine/PhysicalEngineLauncher.cpp index bb60c76..9beaf6a 100644 --- a/PhysicalEngine/PhysicalEngineLauncher.cpp +++ b/PhysicalEngine/PhysicalEngineLauncher.cpp @@ -130,6 +130,8 @@ PhysicalEngineLauncher::PhysicalEngineLauncher() { glBindFramebuffer(GL_FRAMEBUFFER, 0); glEnable(GL_DEPTH_TEST); // Enable depth testing + + InputManager::m_window = window; } PhysicalEngineLauncher::~PhysicalEngineLauncher() { diff --git a/PhysicalEngine/Scene/Camera.cpp b/PhysicalEngine/Scene/Camera.cpp index d89d460..670885e 100644 --- a/PhysicalEngine/Scene/Camera.cpp +++ b/PhysicalEngine/Scene/Camera.cpp @@ -1,27 +1,26 @@ #include "Camera.h" +#include "../InputManager.h" + Camera::Camera() { } Camera::~Camera() = default; -void Camera::moveForward(float speed) { - cameraPos += cameraMoveSpeed * cameraFront; +void Camera::moveForward() { + cameraPosMovementBuffer += cameraMoveSpeed * cameraFront; } -void Camera::moveBackward(float speed) { - - cameraPos -= cameraMoveSpeed * cameraFront; +void Camera::moveBackward() { + cameraPosMovementBuffer -= cameraMoveSpeed * cameraFront; } -void Camera::moveLeft(float speed) { - - cameraPos -= glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraMoveSpeed; +void Camera::moveLeft() { + cameraPosMovementBuffer -= glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraMoveSpeed; } -void Camera::moveRight(float speed) { - - cameraPos += glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraMoveSpeed; +void Camera::moveRight() { + cameraPosMovementBuffer += glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraMoveSpeed; } void Camera::processMouseMovement(float xOffset, float yOffset) { @@ -31,7 +30,8 @@ void Camera::processMouseMovement(float xOffset, float yOffset) { yaw += xOffset; pitch += yOffset; - if (constrainPitch) { + if (constrainPitch) + { if (pitch > 89.0f) pitch = 89.0f; if (pitch < -89.0f) @@ -52,3 +52,27 @@ float Camera::getFov() const { glm::mat4 Camera::getViewMatrix() const { return glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp); } + +void Camera::resetCameraPosMovementBuffer() { + cameraPosMovementBuffer = glm::vec3(0.0f, 0.0f, 0.0f); +} + +void Camera::update(float deltaTime) { + if (InputManager::isForwardKeyPressed()) + { + moveForward(); + } + if (InputManager::isBackwardKeyPressed()) + { + moveBackward(); + } + if (InputManager::isLeftKeyPressed()) + { + moveLeft(); + } + if (InputManager::isRightKeyPressed()) + { + moveRight(); + } + cameraPos += cameraPosMovementBuffer * deltaTime; +} diff --git a/PhysicalEngine/Scene/Camera.h b/PhysicalEngine/Scene/Camera.h index 9449b3b..4da07d2 100644 --- a/PhysicalEngine/Scene/Camera.h +++ b/PhysicalEngine/Scene/Camera.h @@ -15,7 +15,7 @@ class Camera { glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f); glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f); - float cameraMoveSpeed = 0.4f; + float cameraMoveSpeed = 4.0f; float cameraSensitivity = 0.1f; float yaw = -90.0f; @@ -23,24 +23,30 @@ class Camera { bool constrainPitch = true; + glm::vec3 cameraPosMovementBuffer = glm::vec3(0.0f, 0.0f, 0.0f); + public: Camera(); ~Camera(); - void moveForward(float speed); + void moveForward(); - void moveBackward(float speed); + void moveBackward(); - void moveLeft(float speed); + void moveLeft(); - void moveRight(float speed); + void moveRight(); void processMouseMovement(float xOffset, float yOffset); glm::mat4 getViewMatrix() const; float getFov() const; + + void resetCameraPosMovementBuffer(); + + void update(float deltaTime); }; diff --git a/PhysicalEngine/Scene/Scene.cpp b/PhysicalEngine/Scene/Scene.cpp index cd5612d..ab52f5f 100644 --- a/PhysicalEngine/Scene/Scene.cpp +++ b/PhysicalEngine/Scene/Scene.cpp @@ -61,6 +61,9 @@ void Scene::destroy() { } void Scene::update(float deltaTime) { + camera.update(deltaTime); + camera.resetCameraPosMovementBuffer(); + // physicalUpdateTimer += deltaTime; // Update the game objects (particles, ...) diff --git a/imgui.ini b/imgui.ini index 1b74cd2..1c88b9d 100644 --- a/imgui.ini +++ b/imgui.ini @@ -49,7 +49,7 @@ Size=872,706 Collapsed=0 [Window][Scene View] -Pos=219,17 +Pos=219,16 Size=751,477 Collapsed=0 From ef26a875309d5ccf32f72f02e5cb8a1d029b1c6f Mon Sep 17 00:00:00 2001 From: Im-Rises Date: Sun, 18 Dec 2022 01:42:35 -0500 Subject: [PATCH 08/12] Corrected movements issues --- PhysicalEngine/Scene/Camera.cpp | 21 +++++++++++++-------- PhysicalEngine/Scene/Camera.h | 5 +++-- PhysicalEngine/Scene/Scene.cpp | 1 - 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/PhysicalEngine/Scene/Camera.cpp b/PhysicalEngine/Scene/Camera.cpp index 670885e..cdc5a9e 100644 --- a/PhysicalEngine/Scene/Camera.cpp +++ b/PhysicalEngine/Scene/Camera.cpp @@ -38,11 +38,10 @@ void Camera::processMouseMovement(float xOffset, float yOffset) { pitch = -89.0f; } - glm::vec3 front; - front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch)); - front.y = sin(glm::radians(pitch)); - front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch)); - cameraFront = glm::normalize(front); + + frontBuffer.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch)); + frontBuffer.y = sin(glm::radians(pitch)); + frontBuffer.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch)); } float Camera::getFov() const { @@ -53,9 +52,6 @@ glm::mat4 Camera::getViewMatrix() const { return glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp); } -void Camera::resetCameraPosMovementBuffer() { - cameraPosMovementBuffer = glm::vec3(0.0f, 0.0f, 0.0f); -} void Camera::update(float deltaTime) { if (InputManager::isForwardKeyPressed()) @@ -74,5 +70,14 @@ void Camera::update(float deltaTime) { { moveRight(); } + + // Update camera position and rotation cameraPos += cameraPosMovementBuffer * deltaTime; + if (frontBuffer != glm::vec3(0.0f, 0.0f, 0.0f)) + { + cameraFront = glm::normalize(frontBuffer); + } + // Reset camera buffers + cameraPosMovementBuffer = glm::vec3(0.0f, 0.0f, 0.0f); + frontBuffer = glm::vec3(0.0f, 0.0f, 0.0f); } diff --git a/PhysicalEngine/Scene/Camera.h b/PhysicalEngine/Scene/Camera.h index 4da07d2..e47db87 100644 --- a/PhysicalEngine/Scene/Camera.h +++ b/PhysicalEngine/Scene/Camera.h @@ -23,7 +23,10 @@ class Camera { bool constrainPitch = true; + // Buffers for camera movement glm::vec3 cameraPosMovementBuffer = glm::vec3(0.0f, 0.0f, 0.0f); + glm::vec3 frontBuffer; + public: Camera(); @@ -44,8 +47,6 @@ class Camera { float getFov() const; - void resetCameraPosMovementBuffer(); - void update(float deltaTime); }; diff --git a/PhysicalEngine/Scene/Scene.cpp b/PhysicalEngine/Scene/Scene.cpp index ab52f5f..31f6385 100644 --- a/PhysicalEngine/Scene/Scene.cpp +++ b/PhysicalEngine/Scene/Scene.cpp @@ -62,7 +62,6 @@ void Scene::destroy() { void Scene::update(float deltaTime) { camera.update(deltaTime); - camera.resetCameraPosMovementBuffer(); // physicalUpdateTimer += deltaTime; From 0e36e6b35dae44b4e3f1580821d99950a7d28c3c Mon Sep 17 00:00:00 2001 From: Im-Rises Date: Sun, 18 Dec 2022 01:54:40 -0500 Subject: [PATCH 09/12] updated inputs --- PhysicalEngine/InputManager.cpp | 15 +++++++++++---- PhysicalEngine/InputManager.h | 4 +++- PhysicalEngine/PhysicalEngineLauncher.cpp | 3 ++- PhysicalEngine/Scene/Camera.cpp | 15 +++++++++++++++ PhysicalEngine/Scene/Camera.h | 6 ++++++ 5 files changed, 37 insertions(+), 6 deletions(-) diff --git a/PhysicalEngine/InputManager.cpp b/PhysicalEngine/InputManager.cpp index 84af729..c760a4c 100644 --- a/PhysicalEngine/InputManager.cpp +++ b/PhysicalEngine/InputManager.cpp @@ -135,10 +135,11 @@ void InputManager::keyPressed(GLFWwindow* window, int key, PhysicalEngineLaunche // } // } -// void InputManager::scroll_callback(GLFWwindow* window, double xOffset, double yOffset) { -// auto* engine = (PhysicalEngineLauncher*)glfwGetWindowUserPointer(window); -// engine->scene->translateCamera(Vector3d(0, 0, movementSpeed * (float)yOffset)); -// } +void InputManager::scroll_callback(GLFWwindow* window, double xOffset, double yOffset) { + auto* engine = (PhysicalEngineLauncher*)glfwGetWindowUserPointer(window); + Camera* camera = engine->scene->getCameraPtr(); + camera->setScrollOffset(yOffset); +} void InputManager::cursor_position_callback(GLFWwindow* window, double xPos, double yPos) { if (mouseRightButtonPressed) @@ -170,3 +171,9 @@ bool InputManager::isLeftKeyPressed() { bool InputManager::isRightKeyPressed() { return glfwGetKey(m_window, GLFW_KEY_RIGHT) == GLFW_PRESS; } +bool InputManager::isUpKeyPressed() { + return glfwGetKey(m_window, GLFW_KEY_PAGE_UP) == GLFW_PRESS; +} +bool InputManager::isDownKeyPressed() { + return glfwGetKey(m_window, GLFW_KEY_PAGE_DOWN) == GLFW_PRESS; +} diff --git a/PhysicalEngine/InputManager.h b/PhysicalEngine/InputManager.h index 4912075..d70c47c 100644 --- a/PhysicalEngine/InputManager.h +++ b/PhysicalEngine/InputManager.h @@ -26,7 +26,7 @@ class InputManager { static void keyRepeated(GLFWwindow* window, int key, PhysicalEngineLauncher* engine); public: - // static void scroll_callback(GLFWwindow* window, double xOffset, double yOffset); + static void scroll_callback(GLFWwindow* window, double xOffset, double yOffset); static void cursor_position_callback(GLFWwindow* window, double xPos, double yPos); @@ -37,6 +37,8 @@ class InputManager { static bool isBackwardKeyPressed(); static bool isLeftKeyPressed(); static bool isRightKeyPressed(); + static bool isUpKeyPressed(); + static bool isDownKeyPressed(); }; #endif // INPUT_MANAGER_H diff --git a/PhysicalEngine/PhysicalEngineLauncher.cpp b/PhysicalEngine/PhysicalEngineLauncher.cpp index 9beaf6a..a028764 100644 --- a/PhysicalEngine/PhysicalEngineLauncher.cpp +++ b/PhysicalEngine/PhysicalEngineLauncher.cpp @@ -86,7 +86,7 @@ PhysicalEngineLauncher::PhysicalEngineLauncher() { // Initialize GLFW callbacks glfwSetWindowUserPointer(window, this); glfwSetKeyCallback(window, InputManager::key_callback); - // glfwSetScrollCallback(window, InputManager::scroll_callback); + glfwSetScrollCallback(window, InputManager::scroll_callback); glfwSetCursorPosCallback(window, InputManager::cursor_position_callback); glfwSetMouseButtonCallback(window, InputManager::mouse_button_callback); @@ -131,6 +131,7 @@ PhysicalEngineLauncher::PhysicalEngineLauncher() { glEnable(GL_DEPTH_TEST); // Enable depth testing + // Setup window for inputs InputManager::m_window = window; } diff --git a/PhysicalEngine/Scene/Camera.cpp b/PhysicalEngine/Scene/Camera.cpp index cdc5a9e..780333a 100644 --- a/PhysicalEngine/Scene/Camera.cpp +++ b/PhysicalEngine/Scene/Camera.cpp @@ -70,6 +70,16 @@ void Camera::update(float deltaTime) { { moveRight(); } + if (InputManager::isUpKeyPressed()) + { + cameraPosMovementBuffer += cameraMoveSpeed * cameraUp; + } + if (InputManager::isDownKeyPressed()) + { + cameraPosMovementBuffer -= cameraMoveSpeed * cameraUp; + } + + cameraPosMovementBuffer += scrollOffset * cameraMoveSpeed * cameraUp; // Update camera position and rotation cameraPos += cameraPosMovementBuffer * deltaTime; @@ -80,4 +90,9 @@ void Camera::update(float deltaTime) { // Reset camera buffers cameraPosMovementBuffer = glm::vec3(0.0f, 0.0f, 0.0f); frontBuffer = glm::vec3(0.0f, 0.0f, 0.0f); + scrollOffset = 0.0f; +} + +void Camera::setScrollOffset(float offset) { + scrollOffset = offset * scrollSensitivity; } diff --git a/PhysicalEngine/Scene/Camera.h b/PhysicalEngine/Scene/Camera.h index e47db87..d135ccb 100644 --- a/PhysicalEngine/Scene/Camera.h +++ b/PhysicalEngine/Scene/Camera.h @@ -27,6 +27,10 @@ class Camera { glm::vec3 cameraPosMovementBuffer = glm::vec3(0.0f, 0.0f, 0.0f); glm::vec3 frontBuffer; + // Scroll values + float scrollOffset = 0.0f; + float scrollSensitivity = 4.0f; + public: Camera(); @@ -48,6 +52,8 @@ class Camera { float getFov() const; void update(float deltaTime); + + void setScrollOffset(float offset); }; From 0cd02b44f8064d0706dca4827586f4c728b2c76a Mon Sep 17 00:00:00 2001 From: Im-Rises Date: Sun, 18 Dec 2022 02:00:18 -0500 Subject: [PATCH 10/12] nothing --- .../ParticlesContactGeneratorRegistry.cpp | 9 +++++---- imgui.ini | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/PhysicalEngine/ParticleContact/ParticlesContactGeneratorRegistry.cpp b/PhysicalEngine/ParticleContact/ParticlesContactGeneratorRegistry.cpp index 9c4dcab..36239e4 100644 --- a/PhysicalEngine/ParticleContact/ParticlesContactGeneratorRegistry.cpp +++ b/PhysicalEngine/ParticleContact/ParticlesContactGeneratorRegistry.cpp @@ -5,11 +5,11 @@ ParticleContactGeneratorRegistry::ParticleContactGeneratorRegistry(unsigned int m_maxSize = maxSize; } -void ParticleContactGeneratorRegistry::addParticleGenerator(ParticleContactGenerator *particleContactGenerator) { +void ParticleContactGeneratorRegistry::addParticleGenerator(ParticleContactGenerator* particleContactGenerator) { m_particlesContactGenerators.push_back(particleContactGenerator); } -std::vector ParticleContactGeneratorRegistry::GetParticlesContactGenerators() const { +std::vector ParticleContactGeneratorRegistry::GetParticlesContactGenerators() const { return m_particlesContactGenerators; } @@ -25,9 +25,10 @@ ParticleContactGeneratorRegistry::~ParticleContactGeneratorRegistry() { // } } -ParticleContact *ParticleContactGeneratorRegistry::generateAllContacts() { +ParticleContact* ParticleContactGeneratorRegistry::generateAllContacts() { unsigned int current = 0; - for (int i = 0; i < m_particlesContactGenerators.size() && current < m_maxSize; i++) { + for (int i = 0; i < m_particlesContactGenerators.size() && current < m_maxSize; i++) + { current = m_particlesContactGenerators[i]->addContact(m_allContact, m_maxSize, current); } m_size = current; diff --git a/imgui.ini b/imgui.ini index 5714eb9..35e3747 100644 --- a/imgui.ini +++ b/imgui.ini @@ -54,7 +54,7 @@ Size=752,475 Collapsed=0 [Window][Inspector] -Pos=973,19 +Pos=971,19 Size=301,693 Collapsed=0 From df69c2a503d63ae607a28152ef186c8b8e1e7824 Mon Sep 17 00:00:00 2001 From: Im-Rises Date: Sun, 18 Dec 2022 02:09:13 -0500 Subject: [PATCH 11/12] Updated imgui.ini --- imgui.ini | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/imgui.ini b/imgui.ini index 3ded0ea..fa5e658 100644 --- a/imgui.ini +++ b/imgui.ini @@ -123,3 +123,13 @@ Pos=606,248 Size=367,216 Collapsed=0 +[Window][Inspector] +Pos=972,23 +Size=298,689 +Collapsed=0 + +[Window][Scene View] +Pos=216,23 +Size=748,473 +Collapsed=0 + From 7668bded6d6be953f13c2d83689fed8f381c659c Mon Sep 17 00:00:00 2001 From: Im-Rises Date: Sun, 18 Dec 2022 09:12:00 -0500 Subject: [PATCH 12/12] Updated workflows --- .github/workflows/cpp-cmake-publish.yml | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/.github/workflows/cpp-cmake-publish.yml b/.github/workflows/cpp-cmake-publish.yml index 5a7933b..eeb86c0 100644 --- a/.github/workflows/cpp-cmake-publish.yml +++ b/.github/workflows/cpp-cmake-publish.yml @@ -22,13 +22,13 @@ jobs: run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} - name: Copy exe to root run: | - cp ./build/ParticleEngine/Release/ParticleEngine.exe . + cp ./build/PhysicalEngine/Release/PhysicalEngine.exe . - name: Upload artifact release uses: actions/upload-artifact@v2 with: name: artifact-windows path: | - ./ParticleEngine.exe + ./PhysicalEngine.exe ./imgui.ini build-ubuntu: name: Build ubuntu @@ -38,19 +38,18 @@ jobs: - name: Install libs run: | sudo apt-get install libglfw3-dev - sudo apt install xorg-dev - name: Configure CMake run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} - name: Build run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} - name: Copy exe to root - run: cp ./build/ParticleEngine/ParticleEngine ./ParticleEngineLauncher + run: cp ./build/PhysicalEngine/PhysicalEngine ./PhysicalEngineLauncher - name: Upload artifact release uses: actions/upload-artifact@v2 with: name: artifact-ubuntu path: | - ./ParticleEngineLauncher + ./PhysicalEngineLauncher ./imgui.ini build-macos: name: Build macos @@ -64,13 +63,13 @@ jobs: - name: Build run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} - name: Copy exe to root - run: cp ./build/ParticleEngine/ParticleEngine ./ParticleEngineLauncher + run: cp ./build/PhysicalEngine/PhysicalEngine ./PhysicalEngineLauncher - name: Upload artifact release uses: actions/upload-artifact@v2 with: name: artifact-macos path: | - ./ParticleEngineLauncher + ./PhysicalEngineLauncher ./imgui.ini release-project: name: Release project @@ -102,12 +101,12 @@ jobs: with: upload_url: ${{ steps.create-new-release.outputs.upload_url }} asset_path: release-windows.zip - asset_name: ParticleEngine-windows-v${{ github.run_number }}.zip + asset_name: PhysicalEngine-windows-v${{ github.run_number }}.zip asset_content_type: application/zip - name: Delete windows release files run: | rm release-windows.zip - rm ParticleEngine.exe + rm PhysicalEngine.exe - name: Download ubuntu artifact content uses: actions/download-artifact@v2 with: @@ -125,12 +124,12 @@ jobs: with: upload_url: ${{ steps.create-new-release.outputs.upload_url }} asset_path: release-linux.tar.gz - asset_name: ParticleEngine-linux-v${{ github.run_number }}.tar.gz + asset_name: PhysicalEngine-linux-v${{ github.run_number }}.tar.gz asset_content_type: application/zip - name: Delete ubuntu release files run: | rm release-linux.tar.gz - rm ParticleEngineLauncher + rm PhysicalEngineLauncher - name: Download macos artifact content uses: actions/download-artifact@v2 with: @@ -148,5 +147,5 @@ jobs: with: upload_url: ${{ steps.create-new-release.outputs.upload_url }} asset_path: release-macos.tar.gz - asset_name: ParticleEngine-macos-v${{ github.run_number }}.tar.gz + asset_name: PhysicalEngine-macos-v${{ github.run_number }}.tar.gz asset_content_type: application/zip