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 diff --git a/PhysicalEngine/Game.cpp b/PhysicalEngine/Game.cpp index 0fccff1..0354c3e 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,99 +52,117 @@ 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); -} -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 }); - } - } - } + 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::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..c760a4c 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,44 +24,61 @@ 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_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_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; + // } + // 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; @@ -76,40 +93,54 @@ void InputManager::keyPressed(GLFWwindow* window, int key, PhysicalEngineLaunche } } -void InputManager::keyReleased(GLFWwindow* window, int key, PhysicalEngineLauncher* engine) { -} +// 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::keyRepeated(GLFWwindow* window, int key, PhysicalEngineLauncher* engine) { +void InputManager::scroll_callback(GLFWwindow* window, double xOffset, double yOffset) { + auto* engine = (PhysicalEngineLauncher*)glfwGetWindowUserPointer(window); 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; - } - } + camera->setScrollOffset(yOffset); } -// 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::cursor_position_callback(GLFWwindow* window, double xPos, double yPos) { if (mouseRightButtonPressed) { @@ -127,3 +158,22 @@ 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; +} +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 2c58fc8..d70c47c 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: @@ -26,11 +26,19 @@ 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); 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(); + static bool isUpKeyPressed(); + static bool isDownKeyPressed(); }; #endif // INPUT_MANAGER_H diff --git a/PhysicalEngine/ParticleContact/ContactGenerator/ParticleCollide.cpp b/PhysicalEngine/ParticleContact/ContactGenerator/ParticleCollide.cpp index b6aa726..10e74e7 100644 --- a/PhysicalEngine/ParticleContact/ContactGenerator/ParticleCollide.cpp +++ b/PhysicalEngine/ParticleContact/ContactGenerator/ParticleCollide.cpp @@ -1,36 +1,54 @@ #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); + + // 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) { - 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; } - - +void ParticleCollide::cleanColliders() { + m_colliders.clear(); +} diff --git a/PhysicalEngine/ParticleContact/ContactGenerator/ParticleCollide.h b/PhysicalEngine/ParticleContact/ContactGenerator/ParticleCollide.h index 1bcc3b9..13f0be0 100644 --- a/PhysicalEngine/ParticleContact/ContactGenerator/ParticleCollide.h +++ b/PhysicalEngine/ParticleContact/ContactGenerator/ParticleCollide.h @@ -3,26 +3,25 @@ #include "ParticleContactGenerator.h" -#include "../ParticleCollider/ParticleCollider.h" +#include "../../Scene/Components/Collider/ParticleCollider/ParticleCollider.h" #include 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; + void cleanColliders(); }; #endif // !PARTICLECOLLIDE_H 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/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/PhysicalEngine/PhysicalEngineLauncher.cpp b/PhysicalEngine/PhysicalEngineLauncher.cpp index bb60c76..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); @@ -130,6 +130,9 @@ PhysicalEngineLauncher::PhysicalEngineLauncher() { glBindFramebuffer(GL_FRAMEBUFFER, 0); glEnable(GL_DEPTH_TEST); // Enable depth testing + + // Setup window for inputs + InputManager::m_window = window; } PhysicalEngineLauncher::~PhysicalEngineLauncher() { diff --git a/PhysicalEngine/Scene/Camera.cpp b/PhysicalEngine/Scene/Camera.cpp index d89d460..780333a 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,18 +30,18 @@ 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) 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 { @@ -52,3 +51,48 @@ float Camera::getFov() const { glm::mat4 Camera::getViewMatrix() const { return glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp); } + + +void Camera::update(float deltaTime) { + if (InputManager::isForwardKeyPressed()) + { + moveForward(); + } + if (InputManager::isBackwardKeyPressed()) + { + moveBackward(); + } + if (InputManager::isLeftKeyPressed()) + { + moveLeft(); + } + if (InputManager::isRightKeyPressed()) + { + 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; + 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); + scrollOffset = 0.0f; +} + +void Camera::setScrollOffset(float offset) { + scrollOffset = offset * scrollSensitivity; +} diff --git a/PhysicalEngine/Scene/Camera.h b/PhysicalEngine/Scene/Camera.h index 9449b3b..d135ccb 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,37 @@ class Camera { bool constrainPitch = true; + // Buffers for camera movement + 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(); ~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 update(float deltaTime); + + void setScrollOffset(float offset); }; 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..d947443 --- /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/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/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 diff --git a/PhysicalEngine/Scene/Prefabs/ParticlePrefab.cpp b/PhysicalEngine/Scene/Prefabs/ParticlePrefab.cpp index 28a074e..c8f3b2f 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,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(particle, 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..6bbbdd4 100644 --- a/PhysicalEngine/Scene/Scene.cpp +++ b/PhysicalEngine/Scene/Scene.cpp @@ -61,6 +61,8 @@ void Scene::destroy() { } void Scene::update(float deltaTime) { + camera.update(deltaTime); + // physicalUpdateTimer += deltaTime; // Update the game objects (particles, ...) @@ -80,12 +82,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 +106,6 @@ void Scene::update(float deltaTime) { } // Test collisions octree.TestAllCollisions(octree.root); - - // } } void Scene::draw(int display_w, int display_h) { @@ -146,10 +148,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; @@ -178,6 +179,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) { @@ -200,3 +202,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 1c0642f..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 1b74cd2..fa5e658 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 @@ -48,16 +48,6 @@ Pos=397,8 Size=872,706 Collapsed=0 -[Window][Scene View] -Pos=219,17 -Size=751,477 -Collapsed=0 - -[Window][Inspector] -Pos=974,21 -Size=301,693 -Collapsed=0 - [Window][Project files] Pos=5,506 Size=204,209 @@ -133,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 + 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