From c3faf2d8ca78bb1e7c4d68ad218108d75f3458a3 Mon Sep 17 00:00:00 2001 From: Bojarzin <47534935+Bojarzin@users.noreply.github.com> Date: Fri, 31 Jan 2020 15:39:28 -0500 Subject: [PATCH 1/3] Create shiiiiit.txt --- shiiiiit.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 shiiiiit.txt diff --git a/shiiiiit.txt b/shiiiiit.txt new file mode 100644 index 0000000..e69de29 From 76d58f97f2b1e8a9dda6354137e37aea78892ce2 Mon Sep 17 00:00:00 2001 From: Bojarzin <47534935+Bojarzin@users.noreply.github.com> Date: Fri, 31 Jan 2020 15:43:25 -0500 Subject: [PATCH 2/3] V1 My version of the assignment --- OpenGLEngine/OpenGLEngine/AeroComponent.h | 17 - .../OpenGLEngine/AeroControlComponent.h | 19 - .../OpenGLEngine/AeroControlSystem.cpp | 46 -- OpenGLEngine/OpenGLEngine/AeroControlSystem.h | 17 - .../OpenGLEngine/AeroMinMaxComponent.h | 18 - OpenGLEngine/OpenGLEngine/AeroSystem.cpp | 46 -- OpenGLEngine/OpenGLEngine/AeroSystem.h | 18 - .../OpenGLEngine/BoxColliderComponent.h | 27 - .../OpenGLEngine/BoxColliderSystem.cpp | 95 --- OpenGLEngine/OpenGLEngine/BoxColliderSystem.h | 17 - OpenGLEngine/OpenGLEngine/BungeeComponent.h | 26 + OpenGLEngine/OpenGLEngine/BungeeSystem.cpp | 152 +++++ .../{RodSystem.h => BungeeSystem.h} | 6 +- OpenGLEngine/OpenGLEngine/BuoyancyComponent.h | 25 + OpenGLEngine/OpenGLEngine/BuoyancySystem.cpp | 73 +++ .../{CameraLookSystem.h => BuoyancySystem.h} | 6 +- OpenGLEngine/OpenGLEngine/CableComponent.h | 14 - .../OpenGLEngine/CableComponentSystem.cpp | 45 -- .../OpenGLEngine/CableComponentSystem.h | 13 - .../OpenGLEngine/CameraLookComponent.h | 13 - .../OpenGLEngine/CameraLookSystem.cpp | 23 - OpenGLEngine/OpenGLEngine/ContactEvent.h | 40 -- .../OpenGLEngine/ContactGenerationSystem.cpp | 23 - .../OpenGLEngine/ContactGenerationSystem.h | 18 - .../OpenGLEngine/ContactInfoReciever.h | 66 -- .../OpenGLEngine/ContactResolutionSystem.cpp | 225 ------- .../OpenGLEngine/ContactResolutionSystem.h | 20 - .../OpenGLEngine/DragForceComponent.h | 16 + OpenGLEngine/OpenGLEngine/DragForceSystem.cpp | 29 + ...rceGeneratorSystem.h => DragForceSystem.h} | 8 +- OpenGLEngine/OpenGLEngine/ECSConfig.h | 1 + .../OpenGLEngine/FireworksComponent.h | 20 + OpenGLEngine/OpenGLEngine/FireworksSystem.cpp | 60 ++ ...ticleSpawnerSystem.h => FireworksSystem.h} | 7 +- .../OpenGLEngine/FixedSpringComponent.h | 13 +- .../FixedSpringForceGeneratorSystem.cpp | 53 -- .../OpenGLEngine/FixedSpringSystem.cpp | 55 ++ ...eGeneratorSystem.h => FixedSpringSystem.h} | 5 +- .../OpenGLEngine/FlighSimulatorComponent.h | 15 - .../OpenGLEngine/FlightSimulatorSystem.cpp | 52 -- .../OpenGLEngine/FlightSimulatorSystem.h | 17 - .../OpenGLEngine/FollowCameraComponent.h | 15 - .../OpenGLEngine/FollowCameraSystem.cpp | 21 - .../OpenGLEngine/FollowCameraSystem.h | 14 - .../OpenGLEngine/ForceAccumulatorComponent.h | 30 + .../OpenGLEngine/ForceAccumulatorSystem.cpp | 11 +- .../OpenGLEngine/ForceAccumulatorSystem.h | 2 +- .../ForceAndTorqueAccumulatorSystem.cpp | 27 - .../ForceAndTorqueAccumulatorSystem.h | 14 - .../OpenGLEngine/GravityForceComponent.h | 15 + .../GravityForceGeneratorSystem.cpp | 21 - .../OpenGLEngine/GravityForceSystem.cpp | 24 + .../OpenGLEngine/GravityForceSystem.h | 15 + .../OpenGLEngine/InfiniteSpawnComponent.h | 15 - .../OpenGLEngine/InfiniteSpawnSystem.cpp | 34 - .../OpenGLEngine/InfiniteSpawnSystem.h | 14 - .../InfiniteSpawnTargetComponent.h | 13 - .../InfiniteSpawnTargetSystem.cpp | 20 - .../OpenGLEngine/InfiniteSpawnTargetSystem.h | 14 - OpenGLEngine/OpenGLEngine/LifeTimeComponent.h | 18 - OpenGLEngine/OpenGLEngine/LifeTimeSystem.cpp | 27 - OpenGLEngine/OpenGLEngine/LifeTimeSystem.h | 14 - OpenGLEngine/OpenGLEngine/Main.cpp | 580 ++++++------------ .../OpenGLEngine/MoveInBoundsComponent.h | 16 - .../OpenGLEngine/MoveInBoundsSystem.cpp | 64 -- .../OpenGLEngine/MoveInBoundsSystem.h | 14 - OpenGLEngine/OpenGLEngine/NBodyComponent.h | 16 + OpenGLEngine/OpenGLEngine/NBodySystem.cpp | 51 ++ OpenGLEngine/OpenGLEngine/NBodySystem.h | 17 + .../OpenGLEngine/OpenGLEngine.vcxproj | 216 ++----- .../OpenGLEngine/OpenGLEngine.vcxproj.filters | 330 ++++------ .../OpenGLEngine/PairedSpringComponent.h | 19 +- .../PairedSpringForceGeneratorSystem.cpp | 60 -- .../OpenGLEngine/PairedSpringSystem.cpp | 61 ++ ...GeneratorSystem.h => PairedSpringSystem.h} | 5 +- OpenGLEngine/OpenGLEngine/ParticleComponent.h | 26 +- .../OpenGLEngine/ParticleContactComponent.h | 25 - .../OpenGLEngine/ParticleContactEvent.h | 27 + .../ParticleContactResolutionSystem.cpp | 174 +----- .../ParticleContactResolutionSystem.h | 11 +- .../OpenGLEngine/ParticleSpawnerComponent.h | 14 - .../OpenGLEngine/ParticleSpawnerSystem.cpp | 50 -- .../OpenGLEngine/ParticleSphereComponent.h | 15 + .../OpenGLEngine/ParticleSphereSystem.cpp | 63 ++ .../OpenGLEngine/ParticleSphereSystem.h | 16 + OpenGLEngine/OpenGLEngine/ParticleSystem.cpp | 20 +- OpenGLEngine/OpenGLEngine/ParticleSystem.h | 1 - .../OpenGLEngine/RenderingSystemV2.cpp | 40 -- OpenGLEngine/OpenGLEngine/RenderingSystemV2.h | 13 - .../OpenGLEngine/RigidBodyComponent.h | 76 --- OpenGLEngine/OpenGLEngine/RigidBodyData.h | 15 - OpenGLEngine/OpenGLEngine/RigidBodySystem.cpp | 77 --- OpenGLEngine/OpenGLEngine/RigidBodySystem.h | 17 - .../RigidbodyGravityForceGeneratorSystem.cpp | 21 - .../RigidbodyGravityForceGeneratorSystem.h | 14 - OpenGLEngine/OpenGLEngine/RodComponent.h | 15 - OpenGLEngine/OpenGLEngine/RodSystem.cpp | 62 -- OpenGLEngine/OpenGLEngine/RotateComponent.h | 20 +- OpenGLEngine/OpenGLEngine/RotateSystem.cpp | 25 +- OpenGLEngine/OpenGLEngine/RotateSystem.h | 15 +- .../SetAerodynamicTensorSystem.cpp | 42 -- .../OpenGLEngine/SetAerodynamicTensorSystem.h | 14 - OpenGLEngine/OpenGLEngine/SpawnTargetEvent.h | 15 - .../OpenGLEngine/SphereColliderComponent.h | 16 - .../OpenGLEngine/SphereColliderSystem.cpp | 91 --- .../OpenGLEngine/SphereColliderSystem.h | 18 - OpenGLEngine/OpenGLEngine/SphereComponent.h | 9 - .../SphereContactGeneratorSystem.cpp | 103 ---- .../SphereContactGeneratorSystem.h | 19 - .../OpenGLEngine/TransformComponentV2.h | 157 ----- .../UpdateTransformMatricesSystem.cpp | 22 - .../UpdateTransformMatricesSystem.h | 13 - shiiiiit.txt | 0 113 files changed, 1269 insertions(+), 3388 deletions(-) delete mode 100644 OpenGLEngine/OpenGLEngine/AeroComponent.h delete mode 100644 OpenGLEngine/OpenGLEngine/AeroControlComponent.h delete mode 100644 OpenGLEngine/OpenGLEngine/AeroControlSystem.cpp delete mode 100644 OpenGLEngine/OpenGLEngine/AeroControlSystem.h delete mode 100644 OpenGLEngine/OpenGLEngine/AeroMinMaxComponent.h delete mode 100644 OpenGLEngine/OpenGLEngine/AeroSystem.cpp delete mode 100644 OpenGLEngine/OpenGLEngine/AeroSystem.h delete mode 100644 OpenGLEngine/OpenGLEngine/BoxColliderComponent.h delete mode 100644 OpenGLEngine/OpenGLEngine/BoxColliderSystem.cpp delete mode 100644 OpenGLEngine/OpenGLEngine/BoxColliderSystem.h create mode 100644 OpenGLEngine/OpenGLEngine/BungeeComponent.h create mode 100644 OpenGLEngine/OpenGLEngine/BungeeSystem.cpp rename OpenGLEngine/OpenGLEngine/{RodSystem.h => BungeeSystem.h} (55%) create mode 100644 OpenGLEngine/OpenGLEngine/BuoyancyComponent.h create mode 100644 OpenGLEngine/OpenGLEngine/BuoyancySystem.cpp rename OpenGLEngine/OpenGLEngine/{CameraLookSystem.h => BuoyancySystem.h} (52%) delete mode 100644 OpenGLEngine/OpenGLEngine/CableComponent.h delete mode 100644 OpenGLEngine/OpenGLEngine/CableComponentSystem.cpp delete mode 100644 OpenGLEngine/OpenGLEngine/CableComponentSystem.h delete mode 100644 OpenGLEngine/OpenGLEngine/CameraLookComponent.h delete mode 100644 OpenGLEngine/OpenGLEngine/CameraLookSystem.cpp delete mode 100644 OpenGLEngine/OpenGLEngine/ContactEvent.h delete mode 100644 OpenGLEngine/OpenGLEngine/ContactGenerationSystem.cpp delete mode 100644 OpenGLEngine/OpenGLEngine/ContactGenerationSystem.h delete mode 100644 OpenGLEngine/OpenGLEngine/ContactInfoReciever.h delete mode 100644 OpenGLEngine/OpenGLEngine/ContactResolutionSystem.cpp delete mode 100644 OpenGLEngine/OpenGLEngine/ContactResolutionSystem.h create mode 100644 OpenGLEngine/OpenGLEngine/DragForceComponent.h create mode 100644 OpenGLEngine/OpenGLEngine/DragForceSystem.cpp rename OpenGLEngine/OpenGLEngine/{GravityForceGeneratorSystem.h => DragForceSystem.h} (50%) create mode 100644 OpenGLEngine/OpenGLEngine/FireworksComponent.h create mode 100644 OpenGLEngine/OpenGLEngine/FireworksSystem.cpp rename OpenGLEngine/OpenGLEngine/{ParticleSpawnerSystem.h => FireworksSystem.h} (54%) delete mode 100644 OpenGLEngine/OpenGLEngine/FixedSpringForceGeneratorSystem.cpp create mode 100644 OpenGLEngine/OpenGLEngine/FixedSpringSystem.cpp rename OpenGLEngine/OpenGLEngine/{FixedSpringForceGeneratorSystem.h => FixedSpringSystem.h} (64%) delete mode 100644 OpenGLEngine/OpenGLEngine/FlighSimulatorComponent.h delete mode 100644 OpenGLEngine/OpenGLEngine/FlightSimulatorSystem.cpp delete mode 100644 OpenGLEngine/OpenGLEngine/FlightSimulatorSystem.h delete mode 100644 OpenGLEngine/OpenGLEngine/FollowCameraComponent.h delete mode 100644 OpenGLEngine/OpenGLEngine/FollowCameraSystem.cpp delete mode 100644 OpenGLEngine/OpenGLEngine/FollowCameraSystem.h create mode 100644 OpenGLEngine/OpenGLEngine/ForceAccumulatorComponent.h delete mode 100644 OpenGLEngine/OpenGLEngine/ForceAndTorqueAccumulatorSystem.cpp delete mode 100644 OpenGLEngine/OpenGLEngine/ForceAndTorqueAccumulatorSystem.h create mode 100644 OpenGLEngine/OpenGLEngine/GravityForceComponent.h delete mode 100644 OpenGLEngine/OpenGLEngine/GravityForceGeneratorSystem.cpp create mode 100644 OpenGLEngine/OpenGLEngine/GravityForceSystem.cpp create mode 100644 OpenGLEngine/OpenGLEngine/GravityForceSystem.h delete mode 100644 OpenGLEngine/OpenGLEngine/InfiniteSpawnComponent.h delete mode 100644 OpenGLEngine/OpenGLEngine/InfiniteSpawnSystem.cpp delete mode 100644 OpenGLEngine/OpenGLEngine/InfiniteSpawnSystem.h delete mode 100644 OpenGLEngine/OpenGLEngine/InfiniteSpawnTargetComponent.h delete mode 100644 OpenGLEngine/OpenGLEngine/InfiniteSpawnTargetSystem.cpp delete mode 100644 OpenGLEngine/OpenGLEngine/InfiniteSpawnTargetSystem.h delete mode 100644 OpenGLEngine/OpenGLEngine/LifeTimeComponent.h delete mode 100644 OpenGLEngine/OpenGLEngine/LifeTimeSystem.cpp delete mode 100644 OpenGLEngine/OpenGLEngine/LifeTimeSystem.h delete mode 100644 OpenGLEngine/OpenGLEngine/MoveInBoundsComponent.h delete mode 100644 OpenGLEngine/OpenGLEngine/MoveInBoundsSystem.cpp delete mode 100644 OpenGLEngine/OpenGLEngine/MoveInBoundsSystem.h create mode 100644 OpenGLEngine/OpenGLEngine/NBodyComponent.h create mode 100644 OpenGLEngine/OpenGLEngine/NBodySystem.cpp create mode 100644 OpenGLEngine/OpenGLEngine/NBodySystem.h delete mode 100644 OpenGLEngine/OpenGLEngine/PairedSpringForceGeneratorSystem.cpp create mode 100644 OpenGLEngine/OpenGLEngine/PairedSpringSystem.cpp rename OpenGLEngine/OpenGLEngine/{PairedSpringForceGeneratorSystem.h => PairedSpringSystem.h} (59%) delete mode 100644 OpenGLEngine/OpenGLEngine/ParticleContactComponent.h create mode 100644 OpenGLEngine/OpenGLEngine/ParticleContactEvent.h delete mode 100644 OpenGLEngine/OpenGLEngine/ParticleSpawnerComponent.h delete mode 100644 OpenGLEngine/OpenGLEngine/ParticleSpawnerSystem.cpp create mode 100644 OpenGLEngine/OpenGLEngine/ParticleSphereComponent.h create mode 100644 OpenGLEngine/OpenGLEngine/ParticleSphereSystem.cpp create mode 100644 OpenGLEngine/OpenGLEngine/ParticleSphereSystem.h delete mode 100644 OpenGLEngine/OpenGLEngine/RenderingSystemV2.cpp delete mode 100644 OpenGLEngine/OpenGLEngine/RenderingSystemV2.h delete mode 100644 OpenGLEngine/OpenGLEngine/RigidBodyComponent.h delete mode 100644 OpenGLEngine/OpenGLEngine/RigidBodyData.h delete mode 100644 OpenGLEngine/OpenGLEngine/RigidBodySystem.cpp delete mode 100644 OpenGLEngine/OpenGLEngine/RigidBodySystem.h delete mode 100644 OpenGLEngine/OpenGLEngine/RigidbodyGravityForceGeneratorSystem.cpp delete mode 100644 OpenGLEngine/OpenGLEngine/RigidbodyGravityForceGeneratorSystem.h delete mode 100644 OpenGLEngine/OpenGLEngine/RodComponent.h delete mode 100644 OpenGLEngine/OpenGLEngine/RodSystem.cpp delete mode 100644 OpenGLEngine/OpenGLEngine/SetAerodynamicTensorSystem.cpp delete mode 100644 OpenGLEngine/OpenGLEngine/SetAerodynamicTensorSystem.h delete mode 100644 OpenGLEngine/OpenGLEngine/SpawnTargetEvent.h delete mode 100644 OpenGLEngine/OpenGLEngine/SphereColliderComponent.h delete mode 100644 OpenGLEngine/OpenGLEngine/SphereColliderSystem.cpp delete mode 100644 OpenGLEngine/OpenGLEngine/SphereColliderSystem.h delete mode 100644 OpenGLEngine/OpenGLEngine/SphereComponent.h delete mode 100644 OpenGLEngine/OpenGLEngine/SphereContactGeneratorSystem.cpp delete mode 100644 OpenGLEngine/OpenGLEngine/SphereContactGeneratorSystem.h delete mode 100644 OpenGLEngine/OpenGLEngine/TransformComponentV2.h delete mode 100644 OpenGLEngine/OpenGLEngine/UpdateTransformMatricesSystem.cpp delete mode 100644 OpenGLEngine/OpenGLEngine/UpdateTransformMatricesSystem.h delete mode 100644 shiiiiit.txt diff --git a/OpenGLEngine/OpenGLEngine/AeroComponent.h b/OpenGLEngine/OpenGLEngine/AeroComponent.h deleted file mode 100644 index aa005a9..0000000 --- a/OpenGLEngine/OpenGLEngine/AeroComponent.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once -#include "ECSConfig.h" - -namespace Reality -{ - struct AeroComponent - { - AeroComponent(ECSEntity _target = ECSEntity(), Mat3 _aerodynamicTensor = Mat3(1.0f), Vector3 _relativePoint = Vector3(0, 0, 0)) - :target(_target), aerodynamicTensor(_aerodynamicTensor), relativePoint(_relativePoint) - { - - } - Mat3 aerodynamicTensor; - ECSEntity target; - Vector3 relativePoint; - }; -} diff --git a/OpenGLEngine/OpenGLEngine/AeroControlComponent.h b/OpenGLEngine/OpenGLEngine/AeroControlComponent.h deleted file mode 100644 index a918690..0000000 --- a/OpenGLEngine/OpenGLEngine/AeroControlComponent.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once -#include "ECSConfig.h" -#include - -namespace Reality -{ - struct AeroControlComponent - { - AeroControlComponent(std::vector _positiveKeys = { GLFW_KEY_UP }, std::vector _negetiveKeys = { GLFW_KEY_DOWN }, float _rate = 1) - :positiveKeys(_positiveKeys), negetiveKeys(_negetiveKeys), rate(_rate) - { - - } - - std::vector positiveKeys; - std::vector negetiveKeys; - float rate; - }; -} diff --git a/OpenGLEngine/OpenGLEngine/AeroControlSystem.cpp b/OpenGLEngine/OpenGLEngine/AeroControlSystem.cpp deleted file mode 100644 index 98b7cab..0000000 --- a/OpenGLEngine/OpenGLEngine/AeroControlSystem.cpp +++ /dev/null @@ -1,46 +0,0 @@ -#include "AeroControlSystem.h" - -namespace Reality -{ - AeroControlSystem::AeroControlSystem() - { - requireComponent(); - requireComponent(); - } - - void AeroControlSystem::Update(float deltaTime) - { - pKey = false; - nKey = false; - for (auto e : getEntities()) - { - auto& aero = e.getComponent(); - auto& control = e.getComponent(); - - for (auto key : control.positiveKeys) - { - if (glfwGetKey(getWorld().data.renderUtil->window->glfwWindow, key) == GLFW_PRESS) - { - aero.controlSetting += control.rate * deltaTime; - pKey = true; - } - } - - for (auto key : control.negetiveKeys) - { - if (glfwGetKey(getWorld().data.renderUtil->window->glfwWindow, key) == GLFW_PRESS) - { - aero.controlSetting -= control.rate * deltaTime; - nKey = true; - } - } - - if (!pKey && !nKey) - { - aero.controlSetting = 0; - } - - aero.controlSetting = glm::clamp(aero.controlSetting, -1.0f, 1.0f); - } - } -} diff --git a/OpenGLEngine/OpenGLEngine/AeroControlSystem.h b/OpenGLEngine/OpenGLEngine/AeroControlSystem.h deleted file mode 100644 index 27b8ec1..0000000 --- a/OpenGLEngine/OpenGLEngine/AeroControlSystem.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once -#include "ECSConfig.h" -#include "AeroMinMaxComponent.h" -#include "AeroControlComponent.h" - -namespace Reality -{ - class AeroControlSystem : public ECSSystem - { - public: - AeroControlSystem(); - void Update(float deltaTime); - private: - bool pKey = false; - bool nKey = false; - }; -} diff --git a/OpenGLEngine/OpenGLEngine/AeroMinMaxComponent.h b/OpenGLEngine/OpenGLEngine/AeroMinMaxComponent.h deleted file mode 100644 index 3d6abf6..0000000 --- a/OpenGLEngine/OpenGLEngine/AeroMinMaxComponent.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once -#include "ECSConfig.h" - -namespace Reality -{ - struct AeroMinMaxComponent - { - AeroMinMaxComponent(Mat3 _minTensor = Mat3(1.0f), Mat3 _baseTensor = Mat3(1.0f), Mat3 _maxTensor = Mat3(1.0f), float _controlSetting = 0.0f) - :minTensor(_minTensor), baseTensor(_baseTensor), maxTensor(_maxTensor), controlSetting(_controlSetting) - { - - } - Mat3 minTensor; - Mat3 baseTensor; - Mat3 maxTensor; - float controlSetting; - }; -} diff --git a/OpenGLEngine/OpenGLEngine/AeroSystem.cpp b/OpenGLEngine/OpenGLEngine/AeroSystem.cpp deleted file mode 100644 index 8b68db7..0000000 --- a/OpenGLEngine/OpenGLEngine/AeroSystem.cpp +++ /dev/null @@ -1,46 +0,0 @@ -#include "AeroSystem.h" -#include "RigidBodyComponent.h" -#include "TransformComponentV2.h" - -namespace Reality -{ - AeroSystem::AeroSystem() - { - requireComponent(); - } - - void AeroSystem::UpdateForceFromTensor(ECSWorld& world, AeroComponent& aero, RigidBodyComponent& body, TransformComponentV2& transform) - { - // Calculate total velocity (windspeed and body's velocity). - Vector3 velocity = body.velocity; - velocity += windspeed; - - // Calculate the velocity in body coordinates - Vector3 bodyVel = transform.WorldToLocalDirection(velocity); - - // Calculate the force in body coordinates - Vector3 bodyForce = aero.aerodynamicTensor * bodyVel / 1.0f; - Vector3 force = transform.LocalToWorldDirection(bodyForce); - - // Apply the force - Vector3 forcePoint = transform.LocalToWorldPosition(aero.relativePoint); - body.AddForceAtPoint(force, forcePoint, transform.GetPosition()); - - //world.data.renderUtil->DrawLine(transform.GetPosition(), forcePoint + force * 1000.0f, Color::Purple); - world.data.renderUtil->DrawLine(forcePoint, forcePoint + force * 1000.0f, Color::Purple); - } - - void AeroSystem::Update(float deltaTime) - { - for (auto e : getEntities()) - { - auto& aero = e.getComponent(); - if (aero.target.hasComponent() && aero.target.hasComponent()) - { - auto& rigidbody = aero.target.getComponent(); - auto& transform = aero.target.getComponent(); - UpdateForceFromTensor(getWorld(), aero, rigidbody, transform); - } - } - } -} diff --git a/OpenGLEngine/OpenGLEngine/AeroSystem.h b/OpenGLEngine/OpenGLEngine/AeroSystem.h deleted file mode 100644 index 63ccebd..0000000 --- a/OpenGLEngine/OpenGLEngine/AeroSystem.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once -#include "ECSConfig.h" -#include "AeroComponent.h" - -namespace Reality -{ - struct RigidBodyComponent; - struct TransformComponentV2; - class AeroSystem : public ECSSystem - { - public: - AeroSystem(); - void Update(float deltaTime); - Vector3 windspeed = Vector3(0, 0, 0); - private: - void UpdateForceFromTensor(ECSWorld& world, AeroComponent& aero, RigidBodyComponent& body, TransformComponentV2& transform); - }; -} diff --git a/OpenGLEngine/OpenGLEngine/BoxColliderComponent.h b/OpenGLEngine/OpenGLEngine/BoxColliderComponent.h deleted file mode 100644 index 7ef0bf3..0000000 --- a/OpenGLEngine/OpenGLEngine/BoxColliderComponent.h +++ /dev/null @@ -1,27 +0,0 @@ -#pragma once -#include "ECSConfig.h" -#include - -namespace Reality -{ - struct BoxColliderComponent - { - BoxColliderComponent(ECSEntity _body = ECSEntity(), Vector3 _size = Vector3(0, 0, 0), Vector3 _offset = Vector3(0, 0, 0), Vector3 _eulerAngles = Vector3(0, 0, 0)) - : body(_body), size(_size), offset(_offset), rp3dId(-1) - { - SetRotation(_eulerAngles); - } - ECSEntity body; - Vector3 offset; - Vector3 size; - Quaternion orientation; - // Euler angles in degrees - inline void SetRotation(Vector3 eulerAngles) - { - glm::vec3 rotationInRads = glm::vec3(glm::radians(eulerAngles.x), - glm::radians(eulerAngles.y), glm::radians(eulerAngles.z)); - orientation = glm::quat(rotationInRads); - } - int rp3dId; - }; -} diff --git a/OpenGLEngine/OpenGLEngine/BoxColliderSystem.cpp b/OpenGLEngine/OpenGLEngine/BoxColliderSystem.cpp deleted file mode 100644 index 898ccc4..0000000 --- a/OpenGLEngine/OpenGLEngine/BoxColliderSystem.cpp +++ /dev/null @@ -1,95 +0,0 @@ -#include "BoxColliderSystem.h" -#include "RigidBodyComponent.h" -#include "RigidBodySystem.h" - -namespace Reality -{ - BoxColliderSystem::BoxColliderSystem(rp3d::CollisionWorld& _rp3dWorld) - :rp3dWorld(_rp3dWorld) - { - requireComponent(); - } - - void BoxColliderSystem::Update(float deltaTime) - { - std::vector rp3dShapesTemp; - std::vector aliveIds; - aliveIds.resize(rp3dShapes.size()); - int id = 0; - - for (auto e : getEntities()) - { - auto &boxCollider = e.getComponent(); - - if (boxCollider.body.isAlive() && boxCollider.body.hasComponent()) - { - auto &body = boxCollider.body.getComponent(); - - // Update RP3D Ids - // Calculate local rp3d transform - rp3d::Vector3 initPosition(boxCollider.offset.x, - boxCollider.offset.y, - boxCollider.offset.z); - rp3d::Quaternion initOrientation = rp3d::Quaternion(boxCollider.orientation.x, boxCollider.orientation.y, boxCollider.orientation.z, boxCollider.orientation.w); - rp3d::Transform rp3dtransform(initPosition, initOrientation); - - auto rp3dBody = getWorld().getSystemManager().getSystem().rp3dBodies[body.rp3dId]; - // If new rigidbody, create an entry - if (boxCollider.rp3dId < 0) - { - rp3d::BoxShape* shape = new rp3d::BoxShape(rp3d::Vector3(boxCollider.size.x, boxCollider.size.y, boxCollider.size.z) * 0.5f); - // Add the collision shape to the rigid body - - rp3d::ProxyShape * proxyShape = rp3dBody->addCollisionShape(shape, rp3dtransform); - proxyShape->setUserData(&boxCollider); - rp3dShapesTemp.push_back(proxyShape); - body.inertiaTensor[0][0] += (1.0f / 12.0f) * (pow(2 * boxCollider.size.y, 2) + pow(2 * boxCollider.size.z, 2)) / body.inverseMass; - body.inertiaTensor[1][1] += (1.0f / 12.0f) * (pow(2 * boxCollider.size.z, 2) + pow(2 * boxCollider.size.x, 2)) / body.inverseMass; - body.inertiaTensor[2][2] += (1.0f / 12.0f) * (pow(2 * boxCollider.size.x, 2) + pow(2 * boxCollider.size.y, 2)) / body.inverseMass; - boxCollider.rp3dId = id; - } - else if (boxCollider.body.isAlive()) - { - rp3d::ProxyShape * shape = rp3dShapes[boxCollider.rp3dId]; - shape->setLocalToBodyTransform(rp3dtransform); - aliveIds[boxCollider.rp3dId] = 1; - rp3dShapesTemp.push_back(shape); - boxCollider.rp3dId = id; - } - id++; - - if (boxCollider.body.hasComponent()) - { - auto& bodyTransform = boxCollider.body.getComponent(); - getWorld().data.renderUtil->DrawCube(bodyTransform.GetUnScaledTransformationMatrix() * Vector4(boxCollider.offset, 1.0f), boxCollider.size, bodyTransform.GetOrientation() * boxCollider.orientation); - } - } - else - { - // No need to kill it, the death of RB already killed it - aliveIds[boxCollider.rp3dId] = 1; - e.kill(); - } - - } - - for (int i = 0; i < aliveIds.size(); i++) - { - if (aliveIds[i] == 0) - { - if (rp3dShapes[i]) - { - auto shape = rp3dShapes[i]->getCollisionShapePublic(); - - if (rp3dShapes[i]->getBody() && rp3dShapes[i]->getBody()->getProxyShapesList()) - { - rp3dShapes[i]->getBody()->removeCollisionShape(rp3dShapes[i]); - } - delete shape; - } - } - } - - rp3dShapes = rp3dShapesTemp; - } -} diff --git a/OpenGLEngine/OpenGLEngine/BoxColliderSystem.h b/OpenGLEngine/OpenGLEngine/BoxColliderSystem.h deleted file mode 100644 index 189e426..0000000 --- a/OpenGLEngine/OpenGLEngine/BoxColliderSystem.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once -#include "ECSConfig.h" -#include "BoxColliderComponent.h" -#include "TransformComponentV2.h" -#include -namespace Reality -{ - class BoxColliderSystem : public ECSSystem - { - public: - BoxColliderSystem(rp3d::CollisionWorld& _rp3dWorld); - void Update(float deltaTime); - private: - rp3d::CollisionWorld& rp3dWorld; - std::vector rp3dShapes; - }; -} diff --git a/OpenGLEngine/OpenGLEngine/BungeeComponent.h b/OpenGLEngine/OpenGLEngine/BungeeComponent.h new file mode 100644 index 0000000..926683a --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/BungeeComponent.h @@ -0,0 +1,26 @@ +#pragma once +#include "ECSConfig.h" + +namespace Reality +{ + struct BungeeComponent + { + BungeeComponent(float _springConstant = 10.0f, + float _restLength = 10.0f, + ECSEntity _connectedEntityA = ECSEntity(), + ECSEntity _connectedEntityB = ECSEntity() + ) + : springConstant(_springConstant), + restLength(_restLength), + connectedEntityA(_connectedEntityA), + connectedEntityB(_connectedEntityB) + { + + } + + float springConstant; + float restLength; + ECSEntity connectedEntityA; + ECSEntity connectedEntityB; + }; +} diff --git a/OpenGLEngine/OpenGLEngine/BungeeSystem.cpp b/OpenGLEngine/OpenGLEngine/BungeeSystem.cpp new file mode 100644 index 0000000..106d629 --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/BungeeSystem.cpp @@ -0,0 +1,152 @@ +#include "BungeeSystem.h" +#include "TransformComponent.h" +#include "ForceAccumulatorComponent.h" + +namespace Reality +{ + BungeeSystem::BungeeSystem() + { + requireComponent(); + } + + void BungeeSystem::Update(float deltaTime) + { + for (auto e : getEntities()) + { + auto& bungee = e.getComponent(); + + // Paired Bungees + if (!e.hasComponent()) { + + if (bungee.connectedEntityA.hasComponent() + && bungee.connectedEntityB.hasComponent()) + { + float deltaL = 0; + + auto& transformA = bungee.connectedEntityA.getComponent(); + auto& transformB = bungee.connectedEntityB.getComponent(); + + Vector3 relativePosition = transformA.position - transformB.position; + float length = glm::length(relativePosition); + + if (length <= bungee.restLength) + { + float g = 1.0f / (1.0f + pow(abs(0), 0.5f)); + float r = 1 - g; + + Color col = Color(r, g, 0, 1); + + float deltaLength = length / 10.0f; + Vector3 direction = -glm::normalize(relativePosition); + for (int i = 0; i < 10; i++) + { + getWorld().data.renderUtil->DrawCube( + transformA.position + (float)i * deltaLength * direction, + Vector3(1.0f, 1.0f, 1.0f) * min((bungee.restLength / 20.0f), 100.0f), Vector3(0, 0, 0), col); + } + getWorld().data.renderUtil->DrawLine(transformA.position, transformB.position, + col); + return; + } + + if (length > 0) + { + deltaL = length - bungee.restLength; + + Vector3 force = -glm::normalize(relativePosition); + force *= bungee.springConstant * deltaL; + + /*if (bungee.connectedEntityA.hasComponent()) + { + bungee.connectedEntityA.getComponent().AddForce(force); + }*/ + if (bungee.connectedEntityB.hasComponent()) + { + bungee.connectedEntityB.getComponent().AddForce(-force); + } + + float g = 1.0f / (1.0f + pow(abs(deltaL), 0.5f)); + float r = 1 - g; + + Color col = Color(r, g, 0, 1); + + float deltaLength = length / 10.0f; + Vector3 direction = -glm::normalize(relativePosition); + for (int i = 0; i < 10; i++) + { + getWorld().data.renderUtil->DrawCube( + transformA.position + (float)i * deltaLength * direction, + Vector3(1.0f, 1.0f, 1.0f) * min((bungee.restLength / 20.0f), 100.0f), Vector3(0, 0, 0), col); + } + getWorld().data.renderUtil->DrawLine(transformA.position, transformB.position, + col); + } + } + } + + // If there is just a single particle attached to a bungee + else + { + auto& bungeeTransform = e.getComponent(); + if (bungee.connectedEntityA.hasComponent() + && bungee.connectedEntityA.hasComponent()) + { + float deltaL = 0; + + auto& transformA = bungee.connectedEntityA.getComponent(); + auto& forceAcc = bungee.connectedEntityA.getComponent(); + + Vector3 relativePosition = transformA.position - bungeeTransform.position; + float length = glm::length(relativePosition); + + if (length <= bungee.restLength) + { + float g = 1.0f / (1.0f + pow(abs(deltaL), 0.5f)); + float r = 1 - g; + + Color col = Color(r, g, 0, 1); + + float deltaLength = length / 10.0f; + Vector3 direction = -glm::normalize(relativePosition); + for (int i = 0; i < 10; i++) + { + getWorld().data.renderUtil->DrawCube( + transformA.position + (float)i * deltaLength * direction, + Vector3(1.0f, 1.0f, 1.0f) * min((bungee.restLength / 20.0f), 100.0f), Vector3(0, 0, 0), col); + } + getWorld().data.renderUtil->DrawLine(transformA.position, bungeeTransform.position, + col); + return; + } + + if (length > 0) + { + deltaL = length - bungee.restLength; + + Vector3 force = -glm::normalize(relativePosition); + force *= bungee.springConstant * deltaL; + forceAcc.AddForce(force); + } + float g = 1.0f / (1.0f + pow(abs(deltaL), 0.5f)); + float r = 1 - g; + + Color col = Color(r, g, 0, 1); + + float deltaLength = length / 10.0f; + Vector3 direction = -glm::normalize(relativePosition); + for (int i = 0; i < 10; i++) + { + getWorld().data.renderUtil->DrawCube( + transformA.position + (float)i * deltaLength * direction, + Vector3(1.0f, 1.0f, 1.0f) * min((bungee.restLength / 20.0f), 100.0f), Vector3(0, 0, 0), col); + } + getWorld().data.renderUtil->DrawLine(transformA.position, bungeeTransform.position, + col); + + } + } + + + } + } +} diff --git a/OpenGLEngine/OpenGLEngine/RodSystem.h b/OpenGLEngine/OpenGLEngine/BungeeSystem.h similarity index 55% rename from OpenGLEngine/OpenGLEngine/RodSystem.h rename to OpenGLEngine/OpenGLEngine/BungeeSystem.h index 12ccb83..c487a72 100644 --- a/OpenGLEngine/OpenGLEngine/RodSystem.h +++ b/OpenGLEngine/OpenGLEngine/BungeeSystem.h @@ -1,13 +1,13 @@ #pragma once #include "ECSConfig.h" -#include "RodComponent.h" +#include "BungeeComponent.h" namespace Reality { - class RodSystem : public ECSSystem + class BungeeSystem : public ECSSystem { public: - RodSystem(); + BungeeSystem(); void Update(float deltaTime); }; } diff --git a/OpenGLEngine/OpenGLEngine/BuoyancyComponent.h b/OpenGLEngine/OpenGLEngine/BuoyancyComponent.h new file mode 100644 index 0000000..1c1c729 --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/BuoyancyComponent.h @@ -0,0 +1,25 @@ +#pragma once +#include "ECSConfig.h" + +namespace Reality +{ + struct BuoyancyComponent + { + BuoyancyComponent(float _maxDepth = 10.0f, float _volume = 100.0f, float _waterHeight = 10.0f, ECSEntity _buoy = ECSEntity(), float _density = 1000.0f) + : maxDepth(_maxDepth), + volume(_volume), + waterHeight(_waterHeight), + buoyEntity(_buoy), + density(_density) + { + + } + float maxDepth, + volume, + waterHeight, + density, + waterXWidth, + waterZWidth; + ECSEntity buoyEntity; + }; +} \ No newline at end of file diff --git a/OpenGLEngine/OpenGLEngine/BuoyancySystem.cpp b/OpenGLEngine/OpenGLEngine/BuoyancySystem.cpp new file mode 100644 index 0000000..48f6a98 --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/BuoyancySystem.cpp @@ -0,0 +1,73 @@ +#include "BuoyancySystem.h" +#include "TransformComponent.h" +#include "ForceAccumulatorComponent.h" + +namespace Reality +{ + BuoyancySystem::BuoyancySystem() + { + requireComponent(); + + } + + void BuoyancySystem::Update(float deltaTime) + { + for (auto e : getEntities()) + { + auto& buoyancy = e.getComponent(); + auto& waterTransform = e.getComponent(); + + // Variable to change so the boundary check always works + buoyancy.waterXWidth = 50.0f; + buoyancy.waterZWidth = 50.0f; + + if (buoyancy.buoyEntity.hasComponent()) + { + auto& buoyTransform = buoyancy.buoyEntity.getComponent(); + + getWorld().data.renderUtil->DrawLine(Vector3(0.0f, buoyancy.waterHeight + buoyancy.maxDepth, 0.0f), buoyTransform.position, + Color(1, 0, 0, 1)); + + //getWorld().data.renderUtil->DrawSphere(Vector3(0.0f), 30.0f, Color(0, 0, 1, 1)); + getWorld().data.renderUtil->DrawCube(Vector3(0.0f, buoyancy.waterHeight + buoyancy.maxDepth - 15.0f, 0.0f), Vector3(buoyancy.waterXWidth, 30.0f, buoyancy.waterZWidth), Vector3(0, 0, 0), Color(0, 0, 1, 1)); + + getWorld().data.renderUtil->RenderText("Water Density: " + std::to_string(buoyancy.density), 50.0f, 1000.0f, 1.0f, Color(0.15, 0.2, 1, 1)); + + float depth = buoyTransform.position.y; + Vector3 force = Vector3(0, 0, 0); + + if (buoyTransform.position.x >= 0 - buoyancy.waterXWidth / 2 && // Check if the Buoy actually falls within the water's boundary + buoyTransform.position.x <= 0 + buoyancy.waterXWidth / 2 && + buoyTransform.position.z >= 0 - buoyancy.waterZWidth / 2 && + buoyTransform.position.z <= 0 + buoyancy.waterZWidth / 2) + { + // If the buoy is not in the water at all + if (depth >= buoyancy.waterHeight + buoyancy.maxDepth) + { + return; + } + + // If the buoy is fully submerged + if (depth <= buoyancy.waterHeight - buoyancy.maxDepth) + { + force.y = buoyancy.density * buoyancy.volume; + if (buoyancy.buoyEntity.hasComponent()) + { + buoyancy.buoyEntity.getComponent().AddForce(-force); + } + return; + } + + // If the buoy is just partially submerged + force.y = buoyancy.density * buoyancy.volume * + (depth - buoyancy.maxDepth - buoyancy.waterHeight) / 2 * buoyancy.maxDepth; + if (buoyancy.buoyEntity.hasComponent()) + { + buoyancy.buoyEntity.getComponent().AddForce(-force); + } + } + } + + } + } +} diff --git a/OpenGLEngine/OpenGLEngine/CameraLookSystem.h b/OpenGLEngine/OpenGLEngine/BuoyancySystem.h similarity index 52% rename from OpenGLEngine/OpenGLEngine/CameraLookSystem.h rename to OpenGLEngine/OpenGLEngine/BuoyancySystem.h index 8d17a1c..609dc4c 100644 --- a/OpenGLEngine/OpenGLEngine/CameraLookSystem.h +++ b/OpenGLEngine/OpenGLEngine/BuoyancySystem.h @@ -1,13 +1,13 @@ #pragma once #include "ECSConfig.h" -#include "CameraLookComponent.h" +#include "BuoyancyComponent.h" namespace Reality { - class CameraLookSystem : public ECSSystem + class BuoyancySystem : public ECSSystem { public: - CameraLookSystem(); + BuoyancySystem(); void Update(float deltaTime); }; } diff --git a/OpenGLEngine/OpenGLEngine/CableComponent.h b/OpenGLEngine/OpenGLEngine/CableComponent.h deleted file mode 100644 index 07d09c6..0000000 --- a/OpenGLEngine/OpenGLEngine/CableComponent.h +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once -#include "ECSConfig.h" -namespace Reality -{ - struct CableComponent - { - CableComponent(ECSEntity a = ECSEntity(), ECSEntity b = ECSEntity(), float _maxLength = 10, float _restitution = 1.0f) - : entityA(a), entityB(b), maxLength(_maxLength), restitution(_restitution){} - ECSEntity entityA; - ECSEntity entityB; - float maxLength; - float restitution; - }; -} diff --git a/OpenGLEngine/OpenGLEngine/CableComponentSystem.cpp b/OpenGLEngine/OpenGLEngine/CableComponentSystem.cpp deleted file mode 100644 index 18a69ce..0000000 --- a/OpenGLEngine/OpenGLEngine/CableComponentSystem.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#include "CableComponentSystem.h" -#include "TransformComponent.h" -#include "ParticleContactComponent.h" - -namespace Reality -{ - CableComponentSystem::CableComponentSystem() - { - requireComponent(); - } - - void CableComponentSystem::Update(float deltaTime) - { - for (auto e : getEntities()) - { - auto& cable = e.getComponent(); - float length = glm::length(cable.entityA.getComponent().position - - cable.entityB.getComponent().position); - - getWorld().data.renderUtil->DrawSphere(cable.entityA.getComponent().position, 1, Color::Magenta); - getWorld().data.renderUtil->DrawSphere(cable.entityB.getComponent().position, 1, Color::Magenta); - - getWorld().data.renderUtil->DrawLine(cable.entityA.getComponent().position, - cable.entityB.getComponent().position, Color::Blue); - - if (length < cable.maxLength) - { - continue; - } - - Vector3 normal = glm::normalize(cable.entityB.getComponent().position - - cable.entityA.getComponent().position); - - float penetration = length - cable.maxLength; - - auto contactEntity = getWorld().createEntity(); - contactEntity.addComponent( - cable.entityA, - cable.entityB, - cable.restitution, - normal, - penetration); - } - } -} diff --git a/OpenGLEngine/OpenGLEngine/CableComponentSystem.h b/OpenGLEngine/OpenGLEngine/CableComponentSystem.h deleted file mode 100644 index 78fd957..0000000 --- a/OpenGLEngine/OpenGLEngine/CableComponentSystem.h +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once -#include "ECSConfig.h" -#include "CableComponent.h" -namespace Reality -{ - class CableComponentSystem : public ECSSystem - { - public: - CableComponentSystem(); - void Update(float deltaTime); - }; -} - diff --git a/OpenGLEngine/OpenGLEngine/CameraLookComponent.h b/OpenGLEngine/OpenGLEngine/CameraLookComponent.h deleted file mode 100644 index 80c44a4..0000000 --- a/OpenGLEngine/OpenGLEngine/CameraLookComponent.h +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once -#include "ECSConfig.h" - -namespace Reality -{ - struct CameraLookComponent - { - CameraLookComponent() - { - - } - }; -} diff --git a/OpenGLEngine/OpenGLEngine/CameraLookSystem.cpp b/OpenGLEngine/OpenGLEngine/CameraLookSystem.cpp deleted file mode 100644 index 420c611..0000000 --- a/OpenGLEngine/OpenGLEngine/CameraLookSystem.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include "CameraLookSystem.h" -#include "MouseMoveEvent.h" - -namespace Reality -{ - CameraLookSystem::CameraLookSystem() - { - requireComponent(); - } - - void CameraLookSystem::Update(float deltaTime) - { - for (auto e : getEntities()) - { - Camera& camera = getWorld().data.renderUtil->camera; - auto mouseMoveEvents = getWorld().getEventManager().getEvents(); - for (auto event : mouseMoveEvents) - { - camera.ProcessMouseMovement(event.deltaX, event.deltaY); - } - } - } -} diff --git a/OpenGLEngine/OpenGLEngine/ContactEvent.h b/OpenGLEngine/OpenGLEngine/ContactEvent.h deleted file mode 100644 index c8ef717..0000000 --- a/OpenGLEngine/OpenGLEngine/ContactEvent.h +++ /dev/null @@ -1,40 +0,0 @@ -#pragma once -#include "ECSConfig.h" -#include "RigidBodyComponent.h" - -namespace Reality -{ - struct ContactEvent - { - ContactEvent(ECSEntity _bodyA = ECSEntity(), ECSEntity _bodyB = ECSEntity(), Vector3 _normal = Vector3(0, 0 , 0), float _penetrationDepth = 0, Vector3 _localPointOnShape1 = Vector3(0, 0, 0), Vector3 _localPointOnShape2 = Vector3(0, 0, 0), Vector3 _worldPoint1 = Vector3(0, 0, 0), Vector3 _worldPoint2 = Vector3(0, 0, 0), float _restitution = 1.0f) - :entityA(_bodyA), entityB(_bodyB), normal(_normal), penetrationDepth(_penetrationDepth), localPointOnShape1(_localPointOnShape1), localPointOnShape2(_localPointOnShape2), worldPoint1(_worldPoint1), worldPoint2(_worldPoint2), restitution(_restitution) - { - - } - - ECSEntity entityA; - - ECSEntity entityB; - - /// Normalized normal vector of the contact (from body1 toward body2) in world space - Vector3 normal; - - /// Penetration depth - float penetrationDepth; - - /// Contact point on proxy shape 1 in local-space of proxy shape 1 - Vector3 localPointOnShape1; - - /// Contact point on proxy shape 2 in local-space of proxy shape 2 - Vector3 localPointOnShape2; - - /// Contact point on proxy shape 1 in world-space - Vector3 worldPoint1; - - /// Contact point on proxy shape 2 in world-space - Vector3 worldPoint2; - - /// Coefficient Of restitution - float restitution; - }; -} diff --git a/OpenGLEngine/OpenGLEngine/ContactGenerationSystem.cpp b/OpenGLEngine/OpenGLEngine/ContactGenerationSystem.cpp deleted file mode 100644 index 3ecb9f3..0000000 --- a/OpenGLEngine/OpenGLEngine/ContactGenerationSystem.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include "ContactGenerationSystem.h" - -namespace Reality -{ - ContactGenerationSystem::ContactGenerationSystem(rp3d::CollisionWorld& _rp3dWorld) : rp3dWorld(_rp3dWorld) - { - - } - - void ContactGenerationSystem::Update(float deltaTime) - { - if (contactReciever == nullptr) - { - contactReciever = new ContactInfoReciever(getWorld()); - } - rp3dWorld.testCollision(contactReciever); - } - - ContactGenerationSystem::~ContactGenerationSystem() - { - delete contactReciever; - } -} diff --git a/OpenGLEngine/OpenGLEngine/ContactGenerationSystem.h b/OpenGLEngine/OpenGLEngine/ContactGenerationSystem.h deleted file mode 100644 index 14c2b24..0000000 --- a/OpenGLEngine/OpenGLEngine/ContactGenerationSystem.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once -#include "ECSConfig.h" -#include "ContactInfoReciever.h" -#include - -namespace Reality -{ - class ContactGenerationSystem : public ECSSystem - { - public: - ContactGenerationSystem(rp3d::CollisionWorld& _rp3dWorld); - void Update(float deltaTime); - ~ContactGenerationSystem(); - private: - rp3d::CollisionWorld& rp3dWorld; - ContactInfoReciever* contactReciever = nullptr; - }; -} diff --git a/OpenGLEngine/OpenGLEngine/ContactInfoReciever.h b/OpenGLEngine/OpenGLEngine/ContactInfoReciever.h deleted file mode 100644 index f472fdd..0000000 --- a/OpenGLEngine/OpenGLEngine/ContactInfoReciever.h +++ /dev/null @@ -1,66 +0,0 @@ -#pragma once -#include "ECSConfig.h" -#include "ContactEvent.h" -#include "RigidBodyData.h" -#include -#include -#include -namespace Reality -{ - class ContactInfoReciever : public rp3d::CollisionCallback - { - public: - ContactInfoReciever(ECSWorld& _world) : world(_world) {} - ECSWorld& world; - void notifyContact(const CollisionCallbackInfo& collisionCallbackInfo) - { - rp3d::ContactManifoldListElement * listElem = collisionCallbackInfo.contactManifoldElements; - // For each contact manifold - for (; listElem != nullptr; listElem = listElem->getNext()) { - rp3d::ContactManifold * manifold = listElem->getContactManifold(); - // Get the contact point - rp3d::ContactPoint * point = manifold->getContactPoints(); - // For each contact point of the manifold - for (int i = 0; i < manifold->getNbContactPoints(); i++) { - // Get the world - space contact point on body 1 - rp3d::Vector3 pos1 = point->getLocalPointOnShape1(); - // Get the world - space contact point on body 1 - rp3d::Vector3 pos2 = point->getLocalPointOnShape2(); - // Get the world - space contact normal - rp3d::Vector3 normal = point->getNormal(); - normal.normalize(); - // Get the penetration - rp3d::decimal penetration = point->getPenetrationDepth(); - // Get Body 1 - rp3d::CollisionBody* body1 = manifold->getBody1(); - assert(body1); - RigidBodyData* bodyA = (RigidBodyData*)(manifold->getBody1()->getUserData()); - // Get Body 2 - rp3d::CollisionBody* body2 = manifold->getBody2(); - assert(body2); - RigidBodyData* bodyB = (RigidBodyData*)(manifold->getBody2()->getUserData()); - // Get Collider 1 - rp3d::ProxyShape* shape1 = manifold->getShape1(); - assert(shape1); - // Get Collider 2 - rp3d::ProxyShape* shape2 = manifold->getShape2(); - assert(shape2); - // Get World Point 1 - rp3d::Vector3 worldPos1 = shape1->getLocalToWorldTransform() * pos1; - // Get World Point 2 - rp3d::Vector3 worldPos2 = shape2->getLocalToWorldTransform() * pos2; - // Create a contact event - world.getEventManager().emitEvent(bodyA->entity, bodyB->entity, - Vector3(normal.x, normal.y, normal.z), - penetration, - Vector3(pos1.x, pos1.y, pos1.z), - Vector3(pos2.x, pos2.y, pos2.z), - Vector3(worldPos1.x, worldPos1.y, worldPos1.z), - Vector3(worldPos2.x, worldPos2.y, worldPos2.z)); - // Go to the next point in the list - point = point->getNext(); - } - } - } - }; -} diff --git a/OpenGLEngine/OpenGLEngine/ContactResolutionSystem.cpp b/OpenGLEngine/OpenGLEngine/ContactResolutionSystem.cpp deleted file mode 100644 index 5999763..0000000 --- a/OpenGLEngine/OpenGLEngine/ContactResolutionSystem.cpp +++ /dev/null @@ -1,225 +0,0 @@ -#include "ContactResolutionSystem.h" -#include "TransformComponentV2.h" -#include "RigidBodyComponent.h" -#include - -namespace Reality -{ - - ContactResolutionSystem::ContactResolutionSystem(rp3d::CollisionWorld& _rp3dWorld) : rp3dWorld(_rp3dWorld) - { - } - - void ContactResolutionSystem::Update(float deltaTime) - { - auto contactEvents = getWorld().getEventManager().getEvents(); - for (auto& contact : contactEvents) - { - ResolvePenetration(contact); - ResolveVelocity(contact); - } - getWorld().data.renderUtil->RenderText("Num Contacts = " + to_string(contactEvents.size()), 1920 * 0.5f - 100, 1080 * 0.5f, 0.5f, Color::Red); - } - void ContactResolutionSystem::ResolvePenetration(ContactEvent & contact) - { - // Get Rigidbodies involved - auto& rbA = contact.entityA.getComponent(); - auto& rbB = contact.entityB.getComponent(); - auto& transformA = contact.entityA.getComponent(); - auto& transformB = contact.entityB.getComponent(); - - // Calculate contact point as avg - Vector3 contactPoint = (contact.worldPoint1 + contact.worldPoint2) * 0.5f; - - // Relative Positions - Vector3 relativePositionA = contact.worldPoint1 - transformA.GetPosition(); - Vector3 relativePositionB = contact.worldPoint2 - transformB.GetPosition(); - contact.normal *= -1; - // World Inertia Tensors - Mat3 worldInvInertiaTensorA = rbA.worldInverseInertiaTensor(transformA.GetRotationMatrix()); - Mat3 worldInvInertiaTensorB = rbB.worldInverseInertiaTensor(transformB.GetRotationMatrix()); - - float totalInertia = 0; - - Vector3 angularInertiaWorldA = glm::cross(relativePositionA, contact.normal); - angularInertiaWorldA = worldInvInertiaTensorA * angularInertiaWorldA; - angularInertiaWorldA = glm::cross(angularInertiaWorldA, relativePositionA); - - float angularInertiaA = glm::dot(angularInertiaWorldA, contact.normal); - float linearInertiaA = rbA.inverseMass; - totalInertia += angularInertiaA + linearInertiaA; - - Vector3 angularInertiaWorldB = glm::cross(relativePositionB, contact.normal); - angularInertiaWorldB = worldInvInertiaTensorB * angularInertiaWorldB; - angularInertiaWorldB = glm::cross(angularInertiaWorldB, relativePositionB); - - float angularInertiaB = glm::dot(angularInertiaWorldB, contact.normal); - float linearInertiaB = rbB.inverseMass; - totalInertia += angularInertiaB + linearInertiaB; - - // Total Moves - float inverseInertia = 1 / totalInertia; - float linearMoveA = contact.penetrationDepth * linearInertiaA * inverseInertia; - float linearMoveB = -contact.penetrationDepth * linearInertiaB * inverseInertia; - float angularMoveA = contact.penetrationDepth * angularInertiaA * inverseInertia; - float angularMoveB = -contact.penetrationDepth * angularInertiaB * inverseInertia; - - float limitA = angularLimitConstant * glm::length(relativePositionA); - if (abs(angularMoveA) > limitA) - { - float totalMoveA = linearMoveA + angularMoveA; - // Set the new angular move, with the same sign as before. - if (angularMoveA >= 0) - { - angularMoveA = limitA; - } else - { - angularMoveA = -limitA; - } - // Make the linear move take the extra slack. - linearMoveA = totalMoveA - angularMoveA; - } - - float limitB = angularLimitConstant * glm::length(relativePositionB); - if (abs(angularMoveB) > limitB) - { - float totalMoveB = linearMoveB + angularMoveB; - // Set the new angular move, with the same sign as before. - if (angularMoveB >= 0) - { - angularMoveB = limitB; - } - else - { - angularMoveB = -limitB; - } - // Make the linear move take the extra slack. - linearMoveB = totalMoveB - angularMoveB; - } - - // Update Linear Moves - transformA.SetPosition(transformA.GetPosition() + contact.normal * linearMoveA); - transformB.SetPosition(transformB.GetPosition() + contact.normal * linearMoveB); - - // Update Rotational Moves - // A - Vector3 impulsiveTorqueA = glm::cross(relativePositionA, contact.normal); - Vector3 impulsePerMoveA = worldInvInertiaTensorA * impulsiveTorqueA; - - Vector3 rotationPerMoveA = impulsePerMoveA * (1 / angularInertiaA); - Vector3 rotationA = rotationPerMoveA * angularMoveA; - - glm::quat rotationQuatA = glm::quat(0, rotationA.x, rotationA.y, rotationA.z); - transformA.SetOrientation(glm::normalize(transformA.GetOrientation() + 0.5f * rotationQuatA * transformA.GetOrientation())); - - // B - Vector3 impulsiveTorqueB = glm::cross(relativePositionB, contact.normal); - Vector3 impulsePerMoveB = worldInvInertiaTensorB * impulsiveTorqueB; - - Vector3 rotationPerMoveB = impulsePerMoveB * (1 / angularInertiaB); - Vector3 rotationB = rotationPerMoveB * angularMoveB; - - glm::quat rotationQuatB = glm::quat(0, rotationB.x, rotationB.y, rotationB.z); - transformB.SetOrientation(glm::normalize(transformB.GetOrientation() + 0.5f * rotationQuatB * transformB.GetOrientation())); - contact.normal *= -1; - - } - void ContactResolutionSystem::ResolveVelocity(ContactEvent & contact) - { - // Get Rigidbodies involved - auto& rbA = contact.entityA.getComponent(); - auto& rbB = contact.entityB.getComponent(); - auto& transformA = contact.entityA.getComponent(); - auto& transformB = contact.entityB.getComponent(); - - // Calculate contact point as avg - Vector3 contactPoint = (contact.worldPoint1 + contact.worldPoint2) * 0.5f; - - // Calculate contact point basis - Vector3 contactY; - Vector3 contactZ; - Mat3 contactLocalToWorld = Mat3(1.0f); - CalculateContactBasis(contact.normal, contactLocalToWorld, contactY, contactZ); - getWorld().data.renderUtil->DrawLine(contactPoint, contactPoint + 5.0f * contact.normal, Color::Red); - getWorld().data.renderUtil->DrawLine(contactPoint, contactPoint + 5.0f * contactY, Color::Green); - getWorld().data.renderUtil->DrawLine(contactPoint, contactPoint + 5.0f * contactZ, Color::Blue); - - // Relative Positions - Vector3 relativePositionA = contact.worldPoint1 - transformA.GetPosition(); - Vector3 relativePositionB = contact.worldPoint2 - transformB.GetPosition(); - - // World Inertia Tensors - Mat3 worldInvInertiaTensorA = rbA.worldInverseInertiaTensor(transformA.GetRotationMatrix()); - Mat3 worldInvInertiaTensorB = rbB.worldInverseInertiaTensor(transformB.GetRotationMatrix()); - - // Calculate velocity Per Unit Impulse - // Body A - Vector3 torquePerUnitImpulseA = glm::cross(relativePositionA, contact.normal); - Vector3 rotationPerUnitImpulseA = worldInvInertiaTensorA * torquePerUnitImpulseA; - Vector3 deltaVelWorldA = glm::cross(rotationPerUnitImpulseA, relativePositionA); - - float deltaVel = glm::dot(deltaVelWorldA, contact.normal); - deltaVel += rbA.inverseMass; - - // Body B - Vector3 torquePerUnitImpulseB = glm::cross(relativePositionB, contact.normal); - Vector3 rotationPerUnitImpulseB = worldInvInertiaTensorB * torquePerUnitImpulseB; - Vector3 deltaVelWorldB = glm::cross(rotationPerUnitImpulseB, relativePositionB); - - deltaVel += glm::dot(deltaVelWorldB, contact.normal); - deltaVel += rbB.inverseMass; - - // Closing Velocity - Vector3 velocityA = glm::cross(rbA.angularVelocity, relativePositionA); - velocityA += rbA.velocity; - - Vector3 velocityB = glm::cross(rbB.angularVelocity, relativePositionB); - velocityB += rbB.velocity; - - Vector3 closingVelocityWorld = velocityA - velocityB; - Vector3 closingVelocityLocal = glm::transpose(contactLocalToWorld) * closingVelocityWorld; - - if (closingVelocityLocal.x < 0) - { - return; - } - // Delta Velocity and impulse - float desiredDeltaVelocityLocal = -closingVelocityLocal.x * (1 + 0.4f); - Vector3 impulseContact = Vector3(desiredDeltaVelocityLocal / deltaVel, 0, 0); - Vector3 impulseA = contactLocalToWorld * impulseContact; - Vector3 impulseB = -impulseA; - - // Calculate Velocity Change - // A - Vector3 velocityChangeA = impulseA * rbA.inverseMass; - Vector3 rotationalTorqueA = glm::cross(relativePositionA, impulseA); - Vector3 angularVelocityChangeA = worldInvInertiaTensorA * rotationalTorqueA; - rbA.velocity += velocityChangeA; - rbA.angularVelocity += angularVelocityChangeA; - // B - Vector3 velocityChangeB = impulseB * rbB.inverseMass; - Vector3 rotationalTorqueB = glm::cross(relativePositionB, impulseB); - Vector3 angularVelocityChangeB = worldInvInertiaTensorB * rotationalTorqueB; - rbB.velocity += velocityChangeB; - rbB.angularVelocity += angularVelocityChangeB; - - // Debug Drawing - getWorld().data.renderUtil->DrawSphere(contact.worldPoint1, 0.4f, Color::Red); - getWorld().data.renderUtil->DrawSphere(contact.worldPoint2, 0.4f, Color::Blue); - getWorld().data.renderUtil->DrawLine(contact.worldPoint1, contact.worldPoint2, Color::Beige); - } - void ContactResolutionSystem::CalculateContactBasis(Vector3 contactNormal, Mat3 & transformationMat, Vector3 & y, Vector3 & z) - { - Vector3 possibleYAxis = Vector3(0, 1.0f, 0); - if (glm::length(glm::cross(possibleYAxis, contactNormal)) <= 0.01f) - { - possibleYAxis = Vector3(0, 0, 1.0f); - } - z = glm::normalize(glm::cross(contactNormal, possibleYAxis)); - y = glm::normalize(glm::cross(z, contactNormal)); - - transformationMat = Mat3(contactNormal.x, contactNormal.y, contactNormal.z, - y.x, y.y, y.z, - z.x, z.y, z.z); - } -} diff --git a/OpenGLEngine/OpenGLEngine/ContactResolutionSystem.h b/OpenGLEngine/OpenGLEngine/ContactResolutionSystem.h deleted file mode 100644 index 2b70f08..0000000 --- a/OpenGLEngine/OpenGLEngine/ContactResolutionSystem.h +++ /dev/null @@ -1,20 +0,0 @@ -#pragma once -#include "ECSConfig.h" -#include "ContactEvent.h" -#include - -namespace Reality -{ - class ContactResolutionSystem : public ECSSystem - { - public: - ContactResolutionSystem(rp3d::CollisionWorld& _rp3dWorld); - void Update(float deltaTime); - rp3d::CollisionWorld& rp3dWorld; - float angularLimitConstant = 0.2f; - private: - void ResolvePenetration(ContactEvent& contact); - void ResolveVelocity(ContactEvent& contact); - void CalculateContactBasis(Vector3 contactNormal, Mat3& transformationMat, Vector3& y, Vector3& z); - }; -} \ No newline at end of file diff --git a/OpenGLEngine/OpenGLEngine/DragForceComponent.h b/OpenGLEngine/OpenGLEngine/DragForceComponent.h new file mode 100644 index 0000000..dec367f --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/DragForceComponent.h @@ -0,0 +1,16 @@ +#pragma once +#include "ECSConfig.h" + +namespace Reality +{ + struct DragForceComponent + { + DragForceComponent(float _k1 = 0.0f, float _k2 = 0.0f) + : k1(_k1), k2(_k2) + { + + } + float k1; + float k2; + }; +} diff --git a/OpenGLEngine/OpenGLEngine/DragForceSystem.cpp b/OpenGLEngine/OpenGLEngine/DragForceSystem.cpp new file mode 100644 index 0000000..591e326 --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/DragForceSystem.cpp @@ -0,0 +1,29 @@ +#include "DragForceSystem.h" + +namespace Reality +{ + DragForceSystem::DragForceSystem() + { + requireComponent(); + requireComponent(); + requireComponent(); + } + + void DragForceSystem::Update(float deltaTime) + { + for (auto e : getEntities()) + { + auto& particle = e.getComponent(); + auto& forceAcc = e.getComponent(); + auto& drag = e.getComponent(); + + float speed = glm::length(particle.velocity); + if (speed > 0) + { + Vector3 force = -glm::normalize(particle.velocity); + force *= drag.k1 * speed + drag.k2 * pow(speed, 2); + forceAcc.AddForce(force); + } + } + } +} diff --git a/OpenGLEngine/OpenGLEngine/GravityForceGeneratorSystem.h b/OpenGLEngine/OpenGLEngine/DragForceSystem.h similarity index 50% rename from OpenGLEngine/OpenGLEngine/GravityForceGeneratorSystem.h rename to OpenGLEngine/OpenGLEngine/DragForceSystem.h index c9a720f..d3f21e8 100644 --- a/OpenGLEngine/OpenGLEngine/GravityForceGeneratorSystem.h +++ b/OpenGLEngine/OpenGLEngine/DragForceSystem.h @@ -1,15 +1,15 @@ #pragma once #include "ECSConfig.h" #include "ParticleComponent.h" +#include "ForceAccumulatorComponent.h" +#include "DragForceComponent.h" namespace Reality { - class GravityForceGeneratorSystem : public ECSSystem + class DragForceSystem : public ECSSystem { public: - Vector3 gravity = Vector3(0, -9.8f, 0); - GravityForceGeneratorSystem(); + DragForceSystem(); void Update(float deltaTime); }; } - diff --git a/OpenGLEngine/OpenGLEngine/ECSConfig.h b/OpenGLEngine/OpenGLEngine/ECSConfig.h index dee62f8..08fdbcd 100644 --- a/OpenGLEngine/OpenGLEngine/ECSConfig.h +++ b/OpenGLEngine/OpenGLEngine/ECSConfig.h @@ -4,6 +4,7 @@ #include #include #define RANDOM_FLOAT(LO, HI) LO + static_cast (rand()) / (static_cast (RAND_MAX / (HI - LO))) +#define DEBUG_LOG_LEVEL 3 namespace Reality { diff --git a/OpenGLEngine/OpenGLEngine/FireworksComponent.h b/OpenGLEngine/OpenGLEngine/FireworksComponent.h new file mode 100644 index 0000000..f988802 --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/FireworksComponent.h @@ -0,0 +1,20 @@ +#pragma once +#include "ECSConfig.h" + +namespace Reality +{ + struct FireworksComponent + { + FireworksComponent(int _numberOfParticles = 6, int _generation = 3, float _spawnTime = 3, float _velocityScale = 10.0f, Color _color = Color::Green) + :numberOfParticles(_numberOfParticles), generation(_generation), spawnTime(_spawnTime), velocityScale(_velocityScale),color(_color), timer(0.0f) + { + + } + int numberOfParticles; + int generation; + float spawnTime; + float timer; + float velocityScale; + Color color; + }; +} diff --git a/OpenGLEngine/OpenGLEngine/FireworksSystem.cpp b/OpenGLEngine/OpenGLEngine/FireworksSystem.cpp new file mode 100644 index 0000000..3d24430 --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/FireworksSystem.cpp @@ -0,0 +1,60 @@ +#include "FireworksSystem.h" +#include "ParticleComponent.h" +#include "ForceAccumulatorComponent.h" +#include "GravityForceComponent.h" + +namespace Reality +{ + FireworksSystem::FireworksSystem() + { + requireComponent(); + requireComponent(); + } + + void FireworksSystem::Update(float deltaTime) + { + for (auto e : getEntities()) + { + auto& transform = e.getComponent(); + auto& fireworks = e.getComponent(); + + fireworks.timer += deltaTime; + if (fireworks.timer > fireworks.spawnTime) + { + if (fireworks.generation > 0) + { + float deltaAngle = 2 * AI_MATH_PI / fireworks.numberOfParticles; + for (int i = 0; i < fireworks.numberOfParticles; i++) + { + auto particle = getWorld().createEntity(); + particle.addComponent(transform.position); + float angle = i * deltaAngle; + Vector3 velocity = Vector3(0, 1, 0); + velocity.x = cos(angle); + velocity.z = sin(angle); + velocity *= fireworks.velocityScale; + particle.addComponent(velocity); + particle.addComponent(); + particle.addComponent(); + float colorAlpha = (float)i / (float)fireworks.numberOfParticles; + particle.addComponent( + fireworks.numberOfParticles, + fireworks.generation - 1, + fireworks.spawnTime + RANDOM_FLOAT(-0.3f, 0.3f), + fireworks.velocityScale, + Color(colorAlpha, 0, 1 - colorAlpha) + ); + + } + } + e.kill(); + } + + + if (DEBUG_LOG_LEVEL > 0) + { + getWorld().data.renderUtil->DrawSphere(transform.position, 1.0f, fireworks.color); + } + } + } +} diff --git a/OpenGLEngine/OpenGLEngine/ParticleSpawnerSystem.h b/OpenGLEngine/OpenGLEngine/FireworksSystem.h similarity index 54% rename from OpenGLEngine/OpenGLEngine/ParticleSpawnerSystem.h rename to OpenGLEngine/OpenGLEngine/FireworksSystem.h index 72fa052..c8bdc7f 100644 --- a/OpenGLEngine/OpenGLEngine/ParticleSpawnerSystem.h +++ b/OpenGLEngine/OpenGLEngine/FireworksSystem.h @@ -1,15 +1,14 @@ #pragma once #include "ECSConfig.h" #include "TransformComponent.h" -#include "ParticleSpawnerComponent.h" +#include "FireworksComponent.h" namespace Reality { - class ParticleSpawnerSystem : public ECSSystem + class FireworksSystem : public ECSSystem { public: - ParticleSpawnerSystem(); + FireworksSystem(); void Update(float deltaTime); }; } - diff --git a/OpenGLEngine/OpenGLEngine/FixedSpringComponent.h b/OpenGLEngine/OpenGLEngine/FixedSpringComponent.h index b8b312b..dabd2d8 100644 --- a/OpenGLEngine/OpenGLEngine/FixedSpringComponent.h +++ b/OpenGLEngine/OpenGLEngine/FixedSpringComponent.h @@ -5,10 +5,17 @@ namespace Reality { struct FixedSpringComponent { - FixedSpringComponent(float _springConstant = 10, float _restLength = 10, ECSEntity e = ECSEntity()) - :springConstant(_springConstant), restLength(_restLength), entity(e){} + FixedSpringComponent(float _springConstant = 10.0f, + float _restLength = 10.0f, + ECSEntity _connectedEntity = ECSEntity()) + : springConstant(_springConstant), + restLength(_restLength), + connectedEntity(_connectedEntity) + { + + } float springConstant; float restLength; - ECSEntity entity; + ECSEntity connectedEntity; }; } diff --git a/OpenGLEngine/OpenGLEngine/FixedSpringForceGeneratorSystem.cpp b/OpenGLEngine/OpenGLEngine/FixedSpringForceGeneratorSystem.cpp deleted file mode 100644 index 01f799b..0000000 --- a/OpenGLEngine/OpenGLEngine/FixedSpringForceGeneratorSystem.cpp +++ /dev/null @@ -1,53 +0,0 @@ -#include "FixedSpringForceGeneratorSystem.h" -#include "ParticleComponent.h" - -namespace Reality -{ - FixedSpringForceGeneratorSystem::FixedSpringForceGeneratorSystem() - { - requireComponent(); - requireComponent(); - } - - - void FixedSpringForceGeneratorSystem::Update(float deltaTime) - { - for (auto e : getEntities()) - { - auto &transform = e.getComponent(); - auto &spring = e.getComponent(); - - if (spring.entity.hasComponent() && - spring.entity.hasComponent()) - { - auto &particle = spring.entity.getComponent(); - auto &entityTransform = spring.entity.getComponent(); - - Vector3 relativePosition = entityTransform.position - transform.position; - float length = glm::length(relativePosition); - float x = length - spring.restLength; - Vector3 direction = glm::normalize(relativePosition); - - particle.AddForce(-spring.springConstant * x * direction); - - /* - float g = 1.0f / (1.0f + pow(abs(x), 0.5f)); - float r = 1 - g; - - Color color = Color(r, g, 0, 1); - - float deltaLength = length / 10.0f; - for (int i = 0; i < 10; i++) - { - getWorld().data.renderUtil->DrawCube( - transform.position + (float)i * deltaLength * direction, - Vector3(0.1f, 0.1f, 0.1f) * min((spring.springConstant / 10.0f), 5.0f), Vector3(0,0,0), color); - } - - getWorld().data.renderUtil->DrawLine( - transform.position, transform.position + length * direction, color);*/ - } - } - - } -} diff --git a/OpenGLEngine/OpenGLEngine/FixedSpringSystem.cpp b/OpenGLEngine/OpenGLEngine/FixedSpringSystem.cpp new file mode 100644 index 0000000..ae5a0f6 --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/FixedSpringSystem.cpp @@ -0,0 +1,55 @@ +#include "FixedSpringSystem.h" +#include "ForceAccumulatorComponent.h" + +namespace Reality +{ + FixedSpringSystem::FixedSpringSystem() + { + requireComponent(); + requireComponent(); + } + + void FixedSpringSystem::Update(float deltaTime) + { + for (auto e : getEntities()) + { + auto& springTransform = e.getComponent(); + auto& spring = e.getComponent(); + + if (spring.connectedEntity.hasComponent() + && spring.connectedEntity.hasComponent()) + { + auto& forceAcc = spring.connectedEntity.getComponent(); + auto& transform = spring.connectedEntity.getComponent(); + + Vector3 relativePosition = transform.position - springTransform.position; + float length = glm::length(relativePosition); + if (length > 0) + { + float deltaL = length - spring.restLength; + Vector3 force = -glm::normalize(relativePosition); + force *= spring.springConstant * deltaL; + forceAcc.AddForce(force); + + float g = 1.0f / (1.0f + pow(abs(deltaL), 0.5f)); + float r = 1 - g; + + Color col = Color(r, g, 0, 1); + + float deltaLength = length / 10.0f; + Vector3 direction = -glm::normalize(relativePosition); + for (int i = 0; i < 10; i++) + { + getWorld().data.renderUtil->DrawCube( + transform.position + (float)i * deltaLength * direction, + Vector3(1.0f, 1.0f, 1.0f) * min((spring.restLength / 20.0f), 100.0f), Vector3(0, 0, 0), col); + } + + getWorld().data.renderUtil->DrawLine(springTransform.position, transform.position, + col); + } + + } + } + } +} diff --git a/OpenGLEngine/OpenGLEngine/FixedSpringForceGeneratorSystem.h b/OpenGLEngine/OpenGLEngine/FixedSpringSystem.h similarity index 64% rename from OpenGLEngine/OpenGLEngine/FixedSpringForceGeneratorSystem.h rename to OpenGLEngine/OpenGLEngine/FixedSpringSystem.h index df47176..89968b1 100644 --- a/OpenGLEngine/OpenGLEngine/FixedSpringForceGeneratorSystem.h +++ b/OpenGLEngine/OpenGLEngine/FixedSpringSystem.h @@ -5,11 +5,10 @@ namespace Reality { - class FixedSpringForceGeneratorSystem : public ECSSystem + class FixedSpringSystem : public ECSSystem { public: - FixedSpringForceGeneratorSystem(); + FixedSpringSystem(); void Update(float deltaTime); }; } - diff --git a/OpenGLEngine/OpenGLEngine/FlighSimulatorComponent.h b/OpenGLEngine/OpenGLEngine/FlighSimulatorComponent.h deleted file mode 100644 index 6d4420c..0000000 --- a/OpenGLEngine/OpenGLEngine/FlighSimulatorComponent.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once -#include "ECSConfig.h" - -namespace Reality -{ - struct FlighSimulatorComponent - { - FlighSimulatorComponent(Vector3 _propulsion = Vector3(0.0f ,0.0f, 1000.0f)) - :propulsion(_propulsion) - { - - } - Vector3 propulsion; - }; -} diff --git a/OpenGLEngine/OpenGLEngine/FlightSimulatorSystem.cpp b/OpenGLEngine/OpenGLEngine/FlightSimulatorSystem.cpp deleted file mode 100644 index a2ff9d5..0000000 --- a/OpenGLEngine/OpenGLEngine/FlightSimulatorSystem.cpp +++ /dev/null @@ -1,52 +0,0 @@ -#include "FlightSimulatorSystem.h" -#include "LifeTimeComponent.h" - -namespace Reality -{ - FlightSimulatorSystem::FlightSimulatorSystem() - { - requireComponent(); - requireComponent(); - requireComponent(); - } - - void FlightSimulatorSystem::Update(float deltaTime) - { - timer += deltaTime; - for (auto e : getEntities()) - { - auto& rigidbody = e.getComponent(); - auto& transform = e.getComponent(); - auto& flight = e.getComponent(); - - rigidbody.AddForce(transform.LocalToWorldDirection(flight.propulsion)); - //rigidbody.AddForce(Vector3(0, -10, 0)); - - // smoke - if (timer > 0.1f) - { - /*for (int i = 0; i < 3; i++) - { - auto e = getWorld().createEntity(); - e.addComponent(transform.GetPosition() + Vector3(RANDOM_FLOAT(-5.0f, 5.0f), RANDOM_FLOAT(-5.0f, 5.0f), RANDOM_FLOAT(-5.0f, 5.0f)) - transform.Forward() * 15.0f); - e.addComponent(7.0f); - }*/ - auto e = getWorld().createEntity(); - e.addComponent(transform.GetPosition() - transform.Forward() * 15.0f); - e.addComponent(7.0f); - timer = 0; - } - } - - if (getEntities().size() > 0) - { - float width = getWorld().data.renderUtil->window->width; - float height = getWorld().data.renderUtil->window->height; - - getWorld().data.renderUtil->RenderText("W , S - Pitch Controls", width / 2 - 100.0f, 60.0f, 0.4f, Color::Orange); - getWorld().data.renderUtil->RenderText("A , D - Yaw Controls", width / 2 - 100.0f, 35.0f, 0.4f, Color::Orange); - getWorld().data.renderUtil->RenderText("Q , E - Roll Controls", width / 2 - 100.0f, 10.0f, 0.4f, Color::Orange); - } - - } -} diff --git a/OpenGLEngine/OpenGLEngine/FlightSimulatorSystem.h b/OpenGLEngine/OpenGLEngine/FlightSimulatorSystem.h deleted file mode 100644 index 67dcc5c..0000000 --- a/OpenGLEngine/OpenGLEngine/FlightSimulatorSystem.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once -#include "ECSConfig.h" -#include "FlighSimulatorComponent.h" -#include "RigidBodyComponent.h" -#include "TransformComponentV2.h" - -namespace Reality -{ - class FlightSimulatorSystem : public ECSSystem - { - public: - FlightSimulatorSystem(); - void Update(float deltaTime); - private: - float timer = 0; - }; -} diff --git a/OpenGLEngine/OpenGLEngine/FollowCameraComponent.h b/OpenGLEngine/OpenGLEngine/FollowCameraComponent.h deleted file mode 100644 index d78cb5c..0000000 --- a/OpenGLEngine/OpenGLEngine/FollowCameraComponent.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once -#include "ECSConfig.h" - -namespace Reality -{ - struct FollowCameraComponent - { - FollowCameraComponent(Vector3 _follow = Vector3(0.0f, 20.0f, 0.0f)) - :follow(_follow) - { - - } - Vector3 follow; - }; -} diff --git a/OpenGLEngine/OpenGLEngine/FollowCameraSystem.cpp b/OpenGLEngine/OpenGLEngine/FollowCameraSystem.cpp deleted file mode 100644 index 2a7fb5a..0000000 --- a/OpenGLEngine/OpenGLEngine/FollowCameraSystem.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include "FollowCameraSystem.h" - -namespace Reality -{ - FollowCameraSystem::FollowCameraSystem() - { - requireComponent(); - requireComponent(); - } - - void FollowCameraSystem::Update(float deltaTime) - { - Camera& cam = getWorld().data.renderUtil->camera; - for (auto e : getEntities()) - { - auto& follow = e.getComponent(); - auto& transform = e.getComponent(); - getWorld().data.renderUtil->camera.Position = transform.GetPosition() + /*transform.LocalToWorldDirection(follow.follow)*/ - cam.Front * 100.0f; - } - } -} diff --git a/OpenGLEngine/OpenGLEngine/FollowCameraSystem.h b/OpenGLEngine/OpenGLEngine/FollowCameraSystem.h deleted file mode 100644 index 2ce6a43..0000000 --- a/OpenGLEngine/OpenGLEngine/FollowCameraSystem.h +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once -#include "ECSConfig.h" -#include "FollowCameraComponent.h" -#include "TransformComponentV2.h" - -namespace Reality -{ - class FollowCameraSystem : public ECSSystem - { - public: - FollowCameraSystem(); - void Update(float deltaTime); - }; -} diff --git a/OpenGLEngine/OpenGLEngine/ForceAccumulatorComponent.h b/OpenGLEngine/OpenGLEngine/ForceAccumulatorComponent.h new file mode 100644 index 0000000..62ca949 --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/ForceAccumulatorComponent.h @@ -0,0 +1,30 @@ +#pragma once +#include "ECSConfig.h" + +namespace Reality +{ + struct ForceAccumulatorComponent + { + ForceAccumulatorComponent(float _mass = 1.0f) + : inverseMass(1.0f / _mass), forceAccumulator(Vector3(0, 0, 0)) + { + + } + float inverseMass; + + inline void AddForce(Vector3 force) + { + forceAccumulator += force; + } + inline void ResetAccumulator() + { + forceAccumulator = Vector3(0, 0, 0); + } + inline Vector3 GetAccumulatedForce() + { + return forceAccumulator; + } + private: + Vector3 forceAccumulator; + }; +} diff --git a/OpenGLEngine/OpenGLEngine/ForceAccumulatorSystem.cpp b/OpenGLEngine/OpenGLEngine/ForceAccumulatorSystem.cpp index d1bbb3d..b4f7c21 100644 --- a/OpenGLEngine/OpenGLEngine/ForceAccumulatorSystem.cpp +++ b/OpenGLEngine/OpenGLEngine/ForceAccumulatorSystem.cpp @@ -1,21 +1,22 @@ #include "ForceAccumulatorSystem.h" - namespace Reality { ForceAccumulatorSystem::ForceAccumulatorSystem() { requireComponent(); + requireComponent(); } - void ForceAccumulatorSystem::Update(float deltaTime) { for (auto e : getEntities()) { - auto &particle = e.getComponent(); - particle.accelaration = particle.GetForce() * particle.inverseMass; - particle.ResetForceAccumulator(); + auto& particle = e.getComponent(); + auto& forceAcc = e.getComponent(); + + particle.acceleration = forceAcc.GetAccumulatedForce() * forceAcc.inverseMass; + forceAcc.ResetAccumulator(); } } } diff --git a/OpenGLEngine/OpenGLEngine/ForceAccumulatorSystem.h b/OpenGLEngine/OpenGLEngine/ForceAccumulatorSystem.h index ff375ad..ce612ed 100644 --- a/OpenGLEngine/OpenGLEngine/ForceAccumulatorSystem.h +++ b/OpenGLEngine/OpenGLEngine/ForceAccumulatorSystem.h @@ -1,6 +1,7 @@ #pragma once #include "ECSConfig.h" #include "ParticleComponent.h" +#include "ForceAccumulatorComponent.h" namespace Reality { @@ -11,4 +12,3 @@ namespace Reality void Update(float deltaTime); }; } - diff --git a/OpenGLEngine/OpenGLEngine/ForceAndTorqueAccumulatorSystem.cpp b/OpenGLEngine/OpenGLEngine/ForceAndTorqueAccumulatorSystem.cpp deleted file mode 100644 index 3b91bdb..0000000 --- a/OpenGLEngine/OpenGLEngine/ForceAndTorqueAccumulatorSystem.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include "ForceAndTorqueAccumulatorSystem.h" - -namespace Reality -{ - ForceAndTorqueAccumulatorSystem::ForceAndTorqueAccumulatorSystem() - { - requireComponent(); - requireComponent(); - } - - void ForceAndTorqueAccumulatorSystem::Update(float deltaTime) - { - for (auto e : getEntities()) - { - auto &rigidbody = e.getComponent(); - auto &transform = e.getComponent(); - - rigidbody.accelaration = rigidbody.GetForce() * rigidbody.inverseMass; - rigidbody.ResetForceAccumulator(); - - Mat3 rotMat = transform.GetRotationMatrix(); - rigidbody.angularAccelaration = rigidbody.worldInverseInertiaTensor(rotMat) - * rigidbody.GetTorque(); - rigidbody.ResetTorqueAccumulator(); - } - } -} diff --git a/OpenGLEngine/OpenGLEngine/ForceAndTorqueAccumulatorSystem.h b/OpenGLEngine/OpenGLEngine/ForceAndTorqueAccumulatorSystem.h deleted file mode 100644 index d4f0221..0000000 --- a/OpenGLEngine/OpenGLEngine/ForceAndTorqueAccumulatorSystem.h +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once -#include "ECSConfig.h" -#include "RigidBodyComponent.h" -#include "TransformComponentV2.h" - -namespace Reality -{ - class ForceAndTorqueAccumulatorSystem : public ECSSystem - { - public: - ForceAndTorqueAccumulatorSystem(); - void Update(float deltaTime); - }; -} diff --git a/OpenGLEngine/OpenGLEngine/GravityForceComponent.h b/OpenGLEngine/OpenGLEngine/GravityForceComponent.h new file mode 100644 index 0000000..09ad65b --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/GravityForceComponent.h @@ -0,0 +1,15 @@ +#pragma once +#include "ECSConfig.h" + +namespace Reality +{ + struct GravityForceComponent + { + GravityForceComponent(float _gravityScale = 1.0f) + : gravityScale(_gravityScale) + { + + } + float gravityScale; + }; +} diff --git a/OpenGLEngine/OpenGLEngine/GravityForceGeneratorSystem.cpp b/OpenGLEngine/OpenGLEngine/GravityForceGeneratorSystem.cpp deleted file mode 100644 index c1df543..0000000 --- a/OpenGLEngine/OpenGLEngine/GravityForceGeneratorSystem.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include "GravityForceGeneratorSystem.h" - - -namespace Reality -{ - GravityForceGeneratorSystem::GravityForceGeneratorSystem() - { - requireComponent(); - } - - - void GravityForceGeneratorSystem::Update(float deltaTime) - { - for (auto e : getEntities()) - { - auto &particle = e.getComponent(); - particle.AddForce(gravity * particle.gravityScale / particle.inverseMass); - } - - } -} diff --git a/OpenGLEngine/OpenGLEngine/GravityForceSystem.cpp b/OpenGLEngine/OpenGLEngine/GravityForceSystem.cpp new file mode 100644 index 0000000..07d38d6 --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/GravityForceSystem.cpp @@ -0,0 +1,24 @@ +#include "GravityForceSystem.h" + +namespace Reality +{ + GravityForceSystem::GravityForceSystem() + { + requireComponent(); + requireComponent(); + } + + void GravityForceSystem::Update(float deltaTime) + { + for (auto e : getEntities()) + { + auto& forceAcc = e.getComponent(); + auto& gravity = e.getComponent(); + + if (forceAcc.inverseMass > 0) + { + forceAcc.AddForce(worldGravity * gravity.gravityScale / forceAcc.inverseMass); + } + } + } +} diff --git a/OpenGLEngine/OpenGLEngine/GravityForceSystem.h b/OpenGLEngine/OpenGLEngine/GravityForceSystem.h new file mode 100644 index 0000000..850e359 --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/GravityForceSystem.h @@ -0,0 +1,15 @@ +#pragma once +#include "ECSConfig.h" +#include "ForceAccumulatorComponent.h" +#include "GravityForceComponent.h" + +namespace Reality +{ + class GravityForceSystem : public ECSSystem + { + public: + GravityForceSystem(); + void Update(float deltaTime); + Vector3 worldGravity = Vector3(0.0f, -9.8f, 0.0f); + }; +} diff --git a/OpenGLEngine/OpenGLEngine/InfiniteSpawnComponent.h b/OpenGLEngine/OpenGLEngine/InfiniteSpawnComponent.h deleted file mode 100644 index 8a81892..0000000 --- a/OpenGLEngine/OpenGLEngine/InfiniteSpawnComponent.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once -#include "ECSConfig.h" - -namespace Reality -{ - struct InfiniteSpawnComponent - { - InfiniteSpawnComponent(float _height = 0) - : height(_height) - { - - } - float height; - }; -} diff --git a/OpenGLEngine/OpenGLEngine/InfiniteSpawnSystem.cpp b/OpenGLEngine/OpenGLEngine/InfiniteSpawnSystem.cpp deleted file mode 100644 index ae10ce8..0000000 --- a/OpenGLEngine/OpenGLEngine/InfiniteSpawnSystem.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#include "InfiniteSpawnSystem.h" -#include "SpawnTargetEvent.h" - -namespace Reality -{ - InfiniteSpawnSystem::InfiniteSpawnSystem() - { - requireComponent(); - requireComponent(); - } - - void InfiniteSpawnSystem::Update(float deltaTime) - { - auto targetEvents = getWorld().getEventManager().getEvents(); - - if (targetEvents.size() > 0) - { - Vector3 target = targetEvents[0].targetPos; - - for (auto e : getEntities()) - { - auto& spawn = e.getComponent(); - auto& transform = e.getComponent(); - - if (abs(target.z - transform.GetPosition().z) > 2050.0f) - { - transform.SetPosition(transform.GetPosition() + (target.z > transform.GetPosition().z ? 4050.0f : -4050.0f) * Vector3(0, 0, 1)); - spawn.height = RANDOM_FLOAT(100.0f, 500.0f); - } - getWorld().data.renderUtil->DrawCube(transform.GetPosition() + spawn.height * 0.5f * Vector3(0, 1, 0), Vector3(50.0f, spawn.height, 50.0f)); - } - } - } -} diff --git a/OpenGLEngine/OpenGLEngine/InfiniteSpawnSystem.h b/OpenGLEngine/OpenGLEngine/InfiniteSpawnSystem.h deleted file mode 100644 index cb95b34..0000000 --- a/OpenGLEngine/OpenGLEngine/InfiniteSpawnSystem.h +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once -#include "ECSConfig.h" -#include "InfiniteSpawnComponent.h" -#include "TransformComponentV2.h" - -namespace Reality -{ - class InfiniteSpawnSystem : public ECSSystem - { - public: - InfiniteSpawnSystem(); - void Update(float deltaTime); - }; -} diff --git a/OpenGLEngine/OpenGLEngine/InfiniteSpawnTargetComponent.h b/OpenGLEngine/OpenGLEngine/InfiniteSpawnTargetComponent.h deleted file mode 100644 index 317a9ad..0000000 --- a/OpenGLEngine/OpenGLEngine/InfiniteSpawnTargetComponent.h +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once -#include "ECSConfig.h" - -namespace Reality -{ - struct InfiniteSpawnTargetComponent - { - InfiniteSpawnTargetComponent() - { - - } - }; -} diff --git a/OpenGLEngine/OpenGLEngine/InfiniteSpawnTargetSystem.cpp b/OpenGLEngine/OpenGLEngine/InfiniteSpawnTargetSystem.cpp deleted file mode 100644 index 5434839..0000000 --- a/OpenGLEngine/OpenGLEngine/InfiniteSpawnTargetSystem.cpp +++ /dev/null @@ -1,20 +0,0 @@ -#include "InfiniteSpawnTargetSystem.h" -#include "SpawnTargetEvent.h" - -namespace Reality -{ - InfiniteSpawnTargetSystem::InfiniteSpawnTargetSystem() - { - requireComponent(); - requireComponent(); - } - - void InfiniteSpawnTargetSystem::Update(float deltaTime) - { - for (auto e : getEntities()) - { - auto& transform = e.getComponent(); - getWorld().getEventManager().emitEvent(transform.GetPosition()); - } - } -} diff --git a/OpenGLEngine/OpenGLEngine/InfiniteSpawnTargetSystem.h b/OpenGLEngine/OpenGLEngine/InfiniteSpawnTargetSystem.h deleted file mode 100644 index 7694392..0000000 --- a/OpenGLEngine/OpenGLEngine/InfiniteSpawnTargetSystem.h +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once -#include "ECSConfig.h" -#include "InfiniteSpawnTargetComponent.h" -#include "TransformComponentV2.h" - -namespace Reality -{ - class InfiniteSpawnTargetSystem : public ECSSystem - { - public: - InfiniteSpawnTargetSystem(); - void Update(float deltaTime); - }; -} diff --git a/OpenGLEngine/OpenGLEngine/LifeTimeComponent.h b/OpenGLEngine/OpenGLEngine/LifeTimeComponent.h deleted file mode 100644 index 0173690..0000000 --- a/OpenGLEngine/OpenGLEngine/LifeTimeComponent.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once -#include "ECSConfig.h" - -namespace Reality -{ - struct LifeTimeComponent - { - LifeTimeComponent(float _maxTime = 10) - :maxTime(_maxTime), timer(0), startSize(RANDOM_FLOAT(1.0f, 3.0f)), size(0) - { - - } - float maxTime; - float timer; - float startSize; - float size; - }; -} diff --git a/OpenGLEngine/OpenGLEngine/LifeTimeSystem.cpp b/OpenGLEngine/OpenGLEngine/LifeTimeSystem.cpp deleted file mode 100644 index b188084..0000000 --- a/OpenGLEngine/OpenGLEngine/LifeTimeSystem.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include "LifeTimeSystem.h" - -namespace Reality -{ - LifeTimeSystem::LifeTimeSystem() - { - requireComponent(); - requireComponent(); - } - - void LifeTimeSystem::Update(float deltaTime) - { - for (auto e : getEntities()) - { - auto& life = e.getComponent(); - auto& transform = e.getComponent(); - - life.timer += deltaTime; - life.size = life.startSize + 10.0f * pow(life.timer, 0.5f); - getWorld().data.renderUtil->DrawSphere(transform.GetPosition(), life.size, Color::Yellow); - if (life.timer > life.maxTime) - { - e.kill(); - } - } - } -} diff --git a/OpenGLEngine/OpenGLEngine/LifeTimeSystem.h b/OpenGLEngine/OpenGLEngine/LifeTimeSystem.h deleted file mode 100644 index 17b76fb..0000000 --- a/OpenGLEngine/OpenGLEngine/LifeTimeSystem.h +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once -#include "ECSConfig.h" -#include "LifeTimeComponent.h" -#include "TransformComponentV2.h" - -namespace Reality -{ - class LifeTimeSystem : public ECSSystem - { - public: - LifeTimeSystem(); - void Update(float deltaTime); - }; -} diff --git a/OpenGLEngine/OpenGLEngine/Main.cpp b/OpenGLEngine/OpenGLEngine/Main.cpp index 91a5161..8afb064 100644 --- a/OpenGLEngine/OpenGLEngine/Main.cpp +++ b/OpenGLEngine/OpenGLEngine/Main.cpp @@ -1,59 +1,42 @@ //#define STB_IMAGE_IMPLEMENTATION -#include "UpdateTransformMatricesSystem.h" #include "RenderingSystem.h" -#include "RenderingSystemV2.h" #include "InputEventSystem.h" +#include "FPSControlSystem.h" #include "RotateSystem.h" -#include "ParticleSystem.h" -#include "ParticleSpawnerSystem.h" -#include "GravityForceGeneratorSystem.h" -#include "FixedSpringForceGeneratorSystem.h" +#include "FireworksSystem.h" +#include "GravityForceSystem.h" +#include "DragForceSystem.h" +#include "FixedSpringSystem.h" +#include "PairedSpringSystem.h" #include "ForceAccumulatorSystem.h" -#include "PairedSpringForceGeneratorSystem.h" -#include "SphereContactGeneratorSystem.h" -#include "ParticleContactResolutionSystem.h" -#include "CableComponentSystem.h" -#include "RodSystem.h" -#include "ForceAndTorqueAccumulatorSystem.h" -#include "RigidBodySystem.h" -#include "RigidbodyGravityForceGeneratorSystem.h" -#include "ContactGenerationSystem.h" -#include "ContactResolutionSystem.h" -#include "SphereColliderSystem.h" -#include "BoxColliderSystem.h" -#include "MoveInBoundsSystem.h" -#include "FPSControlSystem.h" +#include "ParticleSystem.h" #include "DynamicDirectionalLightSystem.h" #include "DynamicPointLightSystem.h" #include "DynamicSpotLightSystem.h" -#include "FlightSimulatorSystem.h" -#include "FollowCameraSystem.h" -#include "InfiniteSpawnSystem.h" -#include "InfiniteSpawnTargetSystem.h" -#include "AeroControlSystem.h" -#include "SetAerodynamicTensorSystem.h" -#include "AeroSystem.h" -#include "CameraLookSystem.h" -#include "LifeTimeSystem.h" #include #include #include -#define DEBUG_LOG_LEVEL 3 +#include "BungeeSystem.h" +#include "BuoyancySystem.h" +#include "NBodySystem.h" +#include "ParticleSphereSystem.h" +#include "ParticleContactResolutionSystem.h" using namespace Reality; void LoadShaders(ECSWorld& world); void LoadModels(ECSWorld& world); +void SetupLights(ECSWorld& world); void MakeABunchaObjects(ECSWorld& world); +void MakeFireworks(ECSWorld& world); +void Make3Particles(ECSWorld& world); void MakeABunchaSprings(ECSWorld& world); -void MakeABunchaSpheres(ECSWorld& world); -void MakeACable(ECSWorld& world); -void MakeCablesAndRods(ECSWorld& world); -void MakeFlight(ECSWorld& world); -void TestContacts(ECSWorld& world); -void TestCollision(ECSWorld& world); -void SetupLights(ECSWorld& world); +void MakeBungee(ECSWorld& world); +void MakeBuoyancy(ECSWorld& world); +void MakeGravity(ECSWorld& world); + +bool bungeeControl = false; int main() { @@ -63,7 +46,7 @@ int main() world.data.InitRendering(); //LoadAssets(world); - world.data.renderUtil->camera.Position = Vector3(0, 15.0f, 100.0f); + world.data.renderUtil->camera.Position = Vector3(0, 40.0f, 50.0f); world.data.renderUtil->SetFOV(60); // Create entities @@ -71,62 +54,36 @@ int main() auto e = world.createEntity(); e.addComponent(); - //auto wall = world.createEntity(); - //wall.addComponent(Vector3(0, -3.0f, 0.0f), Vector3(0.1f, 0.1f, 0.1f), Vector3(0, 270, 0)); - //// Add mesh - //wall.addComponent("Resources/Models/Sponza-master/sponza.obj"); - SetupLights(world); //MakeABunchaObjects(world); - //MakeABunchaSpheres(world); + //MakeFireworks(world); + //Make3Particles(world); //MakeABunchaSprings(world); - //MakeACable(world); - //akeCablesAndRods(world); - //MakeFlight(world); - //TestContacts(world); - TestCollision(world); + //MakeBungee(world); + //MakeBuoyancy(world); + MakeGravity(world); // Create Systems - world.getSystemManager().addSystem(); world.getSystemManager().addSystem(); - world.getSystemManager().addSystem(); world.getSystemManager().addSystem(); + world.getSystemManager().addSystem(); world.getSystemManager().addSystem(); - world.getSystemManager().addSystem(); - world.getSystemManager().addSystem(); - world.getSystemManager().addSystem(); - world.getSystemManager().addSystem(); - world.getSystemManager().addSystem(); - world.getSystemManager().addSystem(); - world.getSystemManager().addSystem(); - world.getSystemManager().addSystem(); - world.getSystemManager().addSystem(); + world.getSystemManager().addSystem(); + world.getSystemManager().addSystem(); + world.getSystemManager().addSystem(); + world.getSystemManager().addSystem(); + world.getSystemManager().addSystem(); world.getSystemManager().addSystem(); - world.getSystemManager().addSystem(); - world.getSystemManager().addSystem(); - world.getSystemManager().addSystem(); - world.getSystemManager().addSystem(); - world.getSystemManager().addSystem(); - world.getSystemManager().addSystem(); - world.getSystemManager().addSystem(); - world.getSystemManager().addSystem(); - world.getSystemManager().addSystem(); - world.getSystemManager().addSystem(); - world.getSystemManager().addSystem(); - world.getSystemManager().addSystem(); + world.getSystemManager().addSystem(); world.getSystemManager().addSystem(); world.getSystemManager().addSystem(); world.getSystemManager().addSystem(); - - // Rigidbody Physics - rp3d::CollisionWorld rp3dWorld; - world.getSystemManager().addSystem(rp3dWorld); - world.getSystemManager().addSystem(rp3dWorld); - world.getSystemManager().addSystem(rp3dWorld); - world.getSystemManager().addSystem(rp3dWorld); - world.getSystemManager().addSystem(rp3dWorld); - world.getSystemManager().addSystem(); - + world.getSystemManager().addSystem(); + world.getSystemManager().addSystem(); + world.getSystemManager().addSystem(); + world.getSystemManager().addSystem(); + world.getSystemManager().addSystem(); + float time = glfwGetTime(); float stepTime = glfwGetTime(); @@ -134,6 +91,7 @@ int main() float elapsedDeltaTime = 0; float logicDelta = 0; float debugDelta = 0; + bool timeToggle = true; LoadShaders(world); bool shadersLoaded = false; @@ -172,58 +130,53 @@ int main() // Game Logic Update world.getSystemManager().getSystem().Update(deltaTime); world.getSystemManager().getSystem().Update(deltaTime); - world.getSystemManager().getSystem().Update(deltaTime); - world.getSystemManager().getSystem().Update(deltaTime); - - //Flight Sim - world.getSystemManager().getSystem().Update(deltaTime); - world.getSystemManager().getSystem().Update(deltaTime); - world.getSystemManager().getSystem().Update(deltaTime); - world.getSystemManager().getSystem().Update(deltaTime); - world.getSystemManager().getSystem().Update(deltaTime); - world.getSystemManager().getSystem().Update(deltaTime); - world.getSystemManager().getSystem().Update(deltaTime); - world.getSystemManager().getSystem().Update(deltaTime); + world.getSystemManager().getSystem().Update(deltaTime); + world.getSystemManager().getSystem().Update(deltaTime); // Update Transform - world.getSystemManager().getSystem().Update(deltaTime); + // Physics - float fixedDeltaTime = glfwGetKey(world.data.renderUtil->window->glfwWindow, GLFW_KEY_SPACE) == GLFW_PRESS ? 1 / 60.0f : 0; + float fixedDeltaTime = glfwGetKey(world.data.renderUtil->window->glfwWindow, GLFW_KEY_SPACE) == GLFW_PRESS ? 1 / 60.0f : 0; + // timeToggle = glfwGetKey(world.data.renderUtil->window->glfwWindow, GLFW_KEY_SPACE) == GLFW_PRESS ? !timeToggle : timeToggle; + // float fixedDeltaTime = !timeToggle ? 1 / 60.0f : 0; //float fixedDeltaTime = 1 / 60.0f; - world.getSystemManager().getSystem().Update(fixedDeltaTime); - world.getSystemManager().getSystem().Update(fixedDeltaTime); - // Particle Force Generators - world.getSystemManager().getSystem().Update(fixedDeltaTime); - world.getSystemManager().getSystem().Update(fixedDeltaTime); - world.getSystemManager().getSystem().Update(fixedDeltaTime); + // Force Generator + world.getSystemManager().getSystem().Update(fixedDeltaTime); + world.getSystemManager().getSystem().Update(fixedDeltaTime); + world.getSystemManager().getSystem().Update(fixedDeltaTime); + world.getSystemManager().getSystem().Update(fixedDeltaTime); + world.getSystemManager().getSystem().Update(fixedDeltaTime); + world.getSystemManager().getSystem().Update(fixedDeltaTime); + world.getSystemManager().getSystem().Update(fixedDeltaTime); + + + // Force Accumulator world.getSystemManager().getSystem().Update(fixedDeltaTime); - world.getSystemManager().getSystem().Update(fixedDeltaTime); - // Rigiidbody Force Generators and collisions - world.getSystemManager().getSystem().Update(fixedDeltaTime); - world.getSystemManager().getSystem().Update(fixedDeltaTime); - world.getSystemManager().getSystem().Update(fixedDeltaTime); - world.getSystemManager().getSystem().Update(fixedDeltaTime); - world.getSystemManager().getSystem().Update(fixedDeltaTime); - // Physics Solvers - world.getSystemManager().getSystem().Update(fixedDeltaTime); - world.getSystemManager().getSystem().Update(fixedDeltaTime); - world.getSystemManager().getSystem().Update(fixedDeltaTime); world.getSystemManager().getSystem().Update(fixedDeltaTime); - world.getSystemManager().getSystem().Update(fixedDeltaTime); - world.getSystemManager().getSystem().Update(fixedDeltaTime); + // Integrator + world.getSystemManager().getSystem().Update(fixedDeltaTime); // Rendering Update + ///*** HACK: For the last DrawCall not working on some systems + world.data.renderUtil->DrawCube(Vector3(0, 0, 0), Vector3(0, 0, 0)); + ///*** HACK: For the last DrawCall not working on some systems world.getSystemManager().getSystem().Update(deltaTime); world.getSystemManager().getSystem().Update(deltaTime); world.getSystemManager().getSystem().Update(deltaTime); world.getSystemManager().getSystem().Update(deltaTime); - world.getSystemManager().getSystem().Update(deltaTime); elapsedDeltaTime = glfwGetTime() - time; logicDelta = elapsedDeltaTime - world.data.renderUtil->GetRenderDelta(); stepTime = glfwGetTime(); + // Bungee Control + + if(glfwGetKey(world.data.renderUtil->window->glfwWindow, GLFW_KEY_B)) + { + bungeeControl != bungeeControl; + } + // Debug if (DEBUG_LOG_LEVEL > 0) { @@ -277,8 +230,8 @@ void LoadModels(ECSWorld& world) { world.data.assetLoader->StartModelLoading({ //ModelData("Resources/Models/snowy-mountain-terrain/SnowyMountainMesh.obj"), - //ModelData("Resources/Models/Sponza-master/sponza.obj"), - //ModelData("Resources/Models/nanosuit/nanosuit.obj"),*/ + ModelData("Resources/Models/Sponza-master/sponza.obj"), + ModelData("Resources/Models/nanosuit/nanosuit.obj"), ModelData("Resources/Models/supermarine-spitfire/spitfire.fbx", {{"spitfire_d.png"}}) }); @@ -286,290 +239,146 @@ void LoadModels(ECSWorld& world) void MakeABunchaObjects(ECSWorld& world) { - auto e = world.createEntity(); - e.addComponent(Vector3(4, 10.0f, 48), Vector3(0.10f, 0.1f, 0.1f), Vector3(-90, 180, 0)); - // Add mesh - e.addComponent("Resources/Models/supermarine-spitfire/spitfire.fbx"); - e.addComponent(0, 40, 0); - - e = world.createEntity(); - e.addComponent(Vector3(4, 10.0f, -62), Vector3(0.1f, 0.1f, 0.1f), Vector3(-90, 0, 0)); - // Add mesh - e.addComponent("Resources/Models/supermarine-spitfire/spitfire.fbx"); - e.addComponent(0, 40, 0); -} - -void MakeABunchaSprings(ECSWorld& world) -{ - auto e = world.createEntity(); - float yOffset = 30; - e.addComponent(Vector3(-2.5f, -5 + yOffset, -3), Vector3(1.0f, 1.0f, 1.0f)); - e.addComponent(); + auto castle = world.createEntity(); + castle.addComponent(Vector3(0, -3.0f, 0.0f), Vector3(0.1f, 0.1f, 0.1f), Vector3(0, 270, 0)); // Add mesh - e.addComponent("Resources/Models/nanosuit/nanosuit.obj"); + castle.addComponent("Resources/Models/Sponza-master/sponza.obj"); - auto springEntinty = world.createEntity(); - springEntinty.addComponent(Vector3(-2.5f, 0 + yOffset, 3)); - springEntinty.addComponent(8, 2, e); - - auto e2 = world.createEntity(); - e2.addComponent(Vector3(2.5f, -5 + yOffset, -1), Vector3(1.0f, 1.0f, 1.0f)); - e2.addComponent(); - // Add mesh - e2.addComponent("Resources/Models/nanosuit/nanosuit.obj"); - - auto springEntinty2 = world.createEntity(); - springEntinty2.addComponent(Vector3(2.5f, 0 + yOffset, 1)); - springEntinty2.addComponent(5, 5, e2); - - auto pairedSpring = world.createEntity(); - pairedSpring.addComponent(100, 5.0f, e, e2); - - auto e3 = world.createEntity(); - e3.addComponent(Vector3(-7.5f, -7.5f + yOffset, 1), Vector3(1.0f, 1.0f, 1.0f)); - e3.addComponent(); - // Add mesh - e3.addComponent("Resources/Models/nanosuit/nanosuit.obj"); - - auto springEntinty3 = world.createEntity(); - springEntinty3.addComponent(Vector3(-7.5f, -10 + yOffset, -1)); - springEntinty3.addComponent(7, 7, e3); - - auto e4 = world.createEntity(); - e4.addComponent(Vector3(7.5f, -7.5f + yOffset, 3), Vector3(1.0f, 1.0f, 1.0f)); - e4.addComponent(); - // Add mesh - e4.addComponent("Resources/Models/nanosuit/nanosuit.obj"); - - auto springEntinty4 = world.createEntity(); - springEntinty4.addComponent(Vector3(7.5f, -10 + yOffset, -3)); - springEntinty4.addComponent(5, 0, e4); - - auto pairedSpring2 = world.createEntity(); - pairedSpring2.addComponent(100, 5.2f, e, e3); - - auto pairedSpring3 = world.createEntity(); - pairedSpring3.addComponent(100, 5.2f, e2, e4); + //auto flight = world.createEntity(); + //flight.addComponent(Vector3(0, 30, -50), Vector3(0.1f, 0.1f, 0.1f), Vector3(270, 0, 0)); + //// Add mesh + //flight.addComponent("Resources/Models/supermarine-spitfire/spitfire.fbx"); + //flight.addComponent(Vector3(0, 90, 0)); + //flight.addComponent(Vector3(0, 30, 0)); + //flight.addComponent(); + //flight.addComponent(); - auto pairedSpring4 = world.createEntity(); - pairedSpring4.addComponent(100, 10.0f, e3, e4); } -void MakeABunchaSpheres(ECSWorld& world) +void MakeFireworks(ECSWorld & world) { - for (int i = 0; i < 30; i++) + for (int i = 0; i < 3; i++) { - auto e = world.createEntity(); - //e.addComponent(Vector3(RANDOM_FLOAT(-1, 1), 20,0)); - - e.addComponent(Vector3(RANDOM_FLOAT(-15.0f, 15.0f), RANDOM_FLOAT(6.0f, 34.0f), RANDOM_FLOAT(-15.0f, 15.0f))); - e.addComponent(1, Vector3(RANDOM_FLOAT(-5, 5), RANDOM_FLOAT(-5, 5), RANDOM_FLOAT(-5, 5))); - e.addComponent(1); - Color col = Color(0, RANDOM_FLOAT(0.0f, 1.0f), RANDOM_FLOAT(0.0f, 1.0f)); - //e.addComponent(20.0f, col, col, col); + auto fireworks = world.createEntity(); + fireworks.addComponent(Vector3(-100 + 100 * i, 30 + RANDOM_FLOAT(-10, 10), -50)); + fireworks.addComponent(Vector3(0, 100, 0)); + fireworks.addComponent(); + fireworks.addComponent(); + fireworks.addComponent(6, 3, 3 + RANDOM_FLOAT(-1, 1)); } - - auto ref = world.createEntity(); - ref.addComponent(Vector3(0, 20, 0), Vector3(0.3f, 0.3f, 0.3f), Vector3(0, 180, 0)); - // Add mesh - ref.addComponent("Resources/Models/nanosuit/nanosuit.obj"); - ref.addComponent(0, 40, 0); + } -void MakeACable(ECSWorld& world) +void Make3Particles(ECSWorld & world) { - auto e1 = world.createEntity(); - e1.addComponent(Vector3(0, 40, 0)); - //e1.addComponent(1, Vector3(0,0,0), 0); - - auto e2 = world.createEntity(); - e2.addComponent(Vector3(0, 30, 0)); - e2.addComponent(1); - - auto e = world.createEntity(); - e.addComponent(e1, e2, 20); + auto particle1 = world.createEntity(); + particle1.addComponent(Vector3(-10, 60, -50)); + particle1.addComponent(Vector3(0, 0, 0)); + particle1.addComponent(); + particle1.addComponent(); + particle1.addComponent(0, 0); + + auto particle2 = world.createEntity(); + particle2.addComponent(Vector3(0, 60, -50)); + particle2.addComponent(Vector3(0, 0, 0)); + particle2.addComponent(); + particle2.addComponent(); + particle2.addComponent(1, 0); + + auto particle3 = world.createEntity(); + particle3.addComponent(Vector3(10, 60, -50)); + particle3.addComponent(Vector3(0, 0, 0)); + particle3.addComponent(); + particle3.addComponent(); + particle3.addComponent(1, 1); } -void MakeCablesAndRods(ECSWorld& world) +void MakeABunchaSprings(ECSWorld & world) { - auto eFixed = world.createEntity(); - eFixed.addComponent(Vector3(10, 40, 0)); - //e1.addComponent(1, Vector3(0,0,0), 0); - - auto eFixed2 = world.createEntity(); - eFixed2.addComponent(Vector3(20, 10, 0)); - - auto eFixed3 = world.createEntity(); - eFixed3.addComponent(Vector3(-20, 10, 0)); - - auto e1 = world.createEntity(); - e1.addComponent(Vector3(0, 30, 0)); - e1.addComponent(10); - - auto e2 = world.createEntity(); - e2.addComponent(Vector3(-10, 20, 0)); - e2.addComponent(10); - - auto e3 = world.createEntity(); - e3.addComponent(Vector3(0, 10, 0)); - e3.addComponent(10); - - auto e4 = world.createEntity(); - e4.addComponent(Vector3(10, 20, 0)); - e4.addComponent(10); - - auto eCable = world.createEntity(); - eCable.addComponent(eFixed, e1, 20); - - auto eCable2 = world.createEntity(); - eCable2.addComponent(1000, 20, eFixed2, e4); - - auto eCable3 = world.createEntity(); - eCable3.addComponent(1000, 20, eFixed3, e2); - - auto eRod1 = world.createEntity(); - eRod1.addComponent(e1, e2, 10 * sqrt(2)); - auto eRod2 = world.createEntity(); - eRod2.addComponent(e2, e3, 10 * sqrt(2)); - auto eRod3 = world.createEntity(); - eRod3.addComponent(e3, e4, 10 * sqrt(2)); - auto eRod4 = world.createEntity(); - eRod4.addComponent(e4, e1, 10 * sqrt(2)); - - auto eRodDiagonal1 = world.createEntity(); - eRodDiagonal1.addComponent(e1, e3, 20); - auto eRodDiagonal2 = world.createEntity(); - eRodDiagonal2.addComponent(e2, e4, 20); + auto particle1 = world.createEntity(); + particle1.addComponent(Vector3(0, 20, -50)); + particle1.addComponent(Vector3(0, 0, 0)); + particle1.addComponent(); + particle1.addComponent(); + + auto particle2= world.createEntity(); + particle2.addComponent(Vector3(-10, 0, -50)); + particle2.addComponent(Vector3(0, 0, 0)); + particle2.addComponent(); + particle2.addComponent(); + + auto spring1 = world.createEntity(); + spring1.addComponent(Vector3(10, 60, -50)); + spring1.addComponent(20.0f, 20.0f, particle1); + + auto spring2 = world.createEntity(); + spring2.addComponent(Vector3(-10, 60, -50)); + spring2.addComponent(20.0f, 15.0f, particle1); + + auto pairedSpring = world.createEntity(); + pairedSpring.addComponent(20.0f, 20.0f, particle1, particle2); + } -void MakeFlight(ECSWorld& world) +void MakeBungee(ECSWorld& world) { - auto e = world.createEntity(); - glm::vec3 rotationInRads = glm::vec3(glm::radians(-90.0f), - glm::radians(180.0f), glm::radians(0.0f)); - Quaternion orientation = glm::quat(rotationInRads); - e.addComponent(Vector3(0, 350.0f, 0), Vector3(0.10f, 0.1f, 0.1f)); - // Add mesh - e.addComponent("Resources/Models/supermarine-spitfire/spitfire.fbx", Vector3(0, -50, 20), Vector3(-90, 0, 0)); - e.addComponent(10.0f ,0.3f, 0.5f); - e.addComponent(); - e.addComponent(Vector3(0.0f, 15.0f, 40.0f)); - e.addComponent(); - e.addComponent(); - - std::vector p1 { GLFW_KEY_E }; - std::vector n1 { GLFW_KEY_Q }; - //Right Wing - auto RW = world.createEntity(); - RW.addComponent(p1, n1); - RW.addComponent(Mat3(0, 0, 0, 0, 0.000f, 0, 0, -0.0005f, 0), - Mat3(0, 0, 0, 0, 0, 0, 0, 0, 0), - Mat3(0, 0, 0, 0, -0.000f, 0, 0, 0.0005f, 0)); - RW.addComponent(e, Mat3(1.0f), Vector3(100.0f, 0, 50.0f)); - - //Left Wing - auto LW = world.createEntity(); - LW.addComponent(n1, p1); - LW.addComponent(Mat3(0, 0, 0, 0, 0.000f, 0, 0, -0.0005f, 0), - Mat3(0, 0, 0, 0, 0, 0, 0, 0, 0), - Mat3(0, 0, 0, 0, -0.000f, 0, 0, 0.0005f, 0)); - LW.addComponent(e, Mat3(1.0f), Vector3(-100.0f, 0, 50.0f)); - - //Rudder - std::vector pR = { GLFW_KEY_A }; - std::vector nR = { GLFW_KEY_D }; - auto R = world.createEntity(); - R.addComponent(pR, nR); - R.addComponent(Mat3(0, 0, 0, 0, 0, 0, 0.002f, 0, 0), - Mat3(0, 0, 0, 0, 0, 0, 0.00f, 0, 0), - Mat3(0, 0, 0, 0, 0, 0, -0.002f, 0, 0)); - R.addComponent(e, Mat3(1.0f), Vector3(0, 0, -200.0f)); - - //Back Wing - std::vector p2{ GLFW_KEY_W }; - std::vector n2{ GLFW_KEY_S }; - auto RW2 = world.createEntity(); - RW2.addComponent(p2, n2); - RW2.addComponent(Mat3(0, 0, 0, 0, 0, 0, 0, -0.0015f, 0), - Mat3(0, 0, 0, 0, 0, 0, 0, 0, 0), - Mat3(0, 0, 0, 0, 0, 0, 0, 0.0015f, 0)); - RW2.addComponent(e, Mat3(1.0f), Vector3(0.0f, 0, -200.0f)); - - for (int i = -40; i <= 40; i++) + // Create particle + auto particle1 = world.createEntity(); + particle1.addComponent(Vector3(0, 0, -50)); + particle1.addComponent(Vector3(0, 0, 0)); + particle1.addComponent(); + particle1.addComponent(); + + // Create bungee force + auto bungee = world.createEntity(); + bungee.addComponent(Vector3(0, 0, -50)); + bungee.addComponent(8.0f, 30.0f, particle1); + + // If you want two particles attached by bungee (doesn't work properly...) + if (bungeeControl) { - auto buildingR = world.createEntity(); - buildingR.addComponent(Vector3(100.0f, 0.0f, 50.0f * i)); - buildingR.addComponent(RANDOM_FLOAT(100.0f, 500.0f)); - - auto buildingL = world.createEntity(); - buildingL.addComponent(Vector3(-100.0f, 0.0f, 50.0f * i)); - buildingL.addComponent(RANDOM_FLOAT(100.0f, 500.0f)); + auto particle2 = world.createEntity(); + particle2.addComponent(Vector3(0, -15, -50)); + particle2.addComponent(Vector3(0, 0, 0)); + particle2.addComponent(); + particle2.addComponent(); + + auto pairedBungee = world.createEntity(); + pairedBungee.addComponent(20.0f, 50.0f, particle1, particle2); } } -void TestContacts(ECSWorld& world) +void MakeBuoyancy(ECSWorld& world) { - for (int i = 0; i < 30; i++) - { - auto e = world.createEntity(); - e.addComponent(Vector3(RANDOM_FLOAT(-200.0f, 200.0f), RANDOM_FLOAT(-200.0f, 200.0f), RANDOM_FLOAT(-200.0f, 200.0f)), - Vector3(1, 1, 1), - Vector3(RANDOM_FLOAT(-180.0f, 180.0f), RANDOM_FLOAT(-180.0f, 180.0f), RANDOM_FLOAT(-180.0f, 180.0f))); - e.addComponent(); - e.addComponent(Vector3(RANDOM_FLOAT(-10.0f, 10.0f), RANDOM_FLOAT(-10.0f, 10.0f), RANDOM_FLOAT(-10.0f, 10.0f)), - Vector3(200, 200, 200)); - auto col = world.createEntity(); - if ((RANDOM_FLOAT(0.0f, 1.0f) >= 0.5f)) - { - col.addComponent(e, RANDOM_FLOAT(10.0f, 50.0f)); - } - else - { - col.addComponent(e, Vector3(RANDOM_FLOAT(30.0f, 70.0f), RANDOM_FLOAT(30.0f, 70.0f), RANDOM_FLOAT(30.0f, 70.0f))); - } - } - /*for (int i = 0; i < 2; i++) - { - auto e = world.createEntity(); - e.addComponent(Vector3(50 * ( i % 2 == 0 ? -1 : 1), 0, 0)); - e.addComponent(); - e.addComponent(Vector3(10 * (i % 2 == 0 ? 1 : -1), 0, 0), Vector3(100, 100, 100)); - auto col = world.createEntity(); - col.addComponent(e, 30); - }*/ + auto particle1 = world.createEntity(); + particle1.addComponent(Vector3(0, 75, 0)); + particle1.addComponent(Vector3(0, 0, 0)); + particle1.addComponent(); + particle1.addComponent(); + particle1.addComponent(0.01f, 0.01f); + + auto buoyancy = world.createEntity(); + + buoyancy.addComponent(Vector3(0, 0, 0)); + buoyancy.addComponent(25.0f, 0.04f, 25.0f, particle1, 10.0f); } -void TestCollision(ECSWorld& world) +void MakeGravity(ECSWorld& world) { - // Floor 1 - auto floor1 = world.createEntity(); - floor1.addComponent(Vector3(0, -50, 0), Vector3(1, 1, 1), Vector3(0, 0, 0)); - floor1.addComponent(100000.0f, 0.4f, 0.3f, Vector3(0, 0, 0), Vector3(0, 0, 0), 0); - auto floorCol1 = world.createEntity(); - floorCol1.addComponent(floor1, Vector3(1000, 10, 1000)); - - // Floor 2 - /*auto floor2 = world.createEntity(); - floor2.addComponent(Vector3(80, -50, 0), Vector3(1, 1, 1), Vector3(0, 0, 30)); - floor2.addComponent(10000.0f, 0.0f, 0.0f, Vector3(0, 0, 0), Vector3(0, 0, 0), 0); - auto floorCol2 = world.createEntity(); - floorCol2.addComponent(floor2, Vector3(300, 10, 300));*/ - - //// Object 1 - //auto object1 = world.createEntity(); - //object1.addComponent(Vector3(-30, 50, 0)); - //object1.addComponent(); - //auto objectCol1 = world.createEntity(); - //objectCol1.addComponent(object1, 10); - - // Object 2 - for (int i = 0; i < 40; i++) + //world.data.renderUtil->DrawSphere(Vector3(0.0f), 300, Color(1, 1, 0, 1)); + + for (int i = 0; i < 10; i++) { - auto object2 = world.createEntity(); - object2.addComponent(Vector3(RANDOM_FLOAT(-50.0f, 50.0f), 50, RANDOM_FLOAT(-50.0f, 50.0f)), Vector3(1, 1, 1), Vector3(RANDOM_FLOAT(0, 180), RANDOM_FLOAT(0, 180), RANDOM_FLOAT(0, 180))); - object2.addComponent(10.0f, 0.1f, 0.1f, Vector3(0, 0, 0), Vector3(0, 0, 0), 5); - auto objectCol2 = world.createEntity(); - objectCol2.addComponent(object2, Vector3(10, 10, 10)); + auto sphere = world.createEntity(); + sphere.addComponent + (Vector3(RANDOM_FLOAT(-20, 20), 0.0f, RANDOM_FLOAT(-20, 20))); + sphere.addComponent + (Vector3(RANDOM_FLOAT(-5, 5), RANDOM_FLOAT(-5, 5), RANDOM_FLOAT(-5, 5))); + + float mass = RANDOM_FLOAT((0.5, 2.0f)); + sphere.addComponent(mass); + sphere.addComponent(); + sphere.addComponent(1.5*mass); } } @@ -577,64 +386,48 @@ void SetupLights(ECSWorld& world) { auto l = world.createEntity(); l.addComponent(Vector3(0, 0, 0), Vector3(0, 0, 0), Vector3(90, 0, 0)); - l.addComponent(Color(0.00, 0.0, 0), Color::White, Color::Orange); + l.addComponent(Color(0.0, 0.1, 0.1), Color(0.0, 0.1, 0.1), Color(0.0, 0.1, 0.1)); // Lanterns auto pl1 = world.createEntity(); pl1.addComponent(Vector3(22, 14, 48.5f)); pl1.addComponent(100.0f, Color(0.1, 0, 0), Color(1.0f, 0.0f, 0.0f), Color(1.0f, 0.0f, 0.0f)); - pl1.addComponent(); auto hook = world.createEntity(); hook.addComponent(Vector3(23, 15, 48.0f)); - hook.addComponent(5, 1, pl1); hook = world.createEntity(); hook.addComponent(Vector3(22, 13.5f, 50.5f)); - hook.addComponent(5, 1, pl1); hook = world.createEntity(); hook.addComponent(Vector3(21, 12.5f, 47.5f)); - hook.addComponent(5, 1, pl1); auto pl2 = world.createEntity(); pl2.addComponent(Vector3(-14.5f, 14, 49.0f)); pl2.addComponent(100.0f, Color(0, 0, 0.1f), Color(0.0f, 0.0f, 1.0f), Color(0.0f, 0.0f, 1.0f)); - pl2.addComponent(); hook = world.createEntity(); hook.addComponent(Vector3(-14.5f + 1, 14 - 1, 49.0f - 1)); - hook.addComponent(5, 1, pl2); hook = world.createEntity(); hook.addComponent(Vector3(-14.5f - 0.5f, 14 + 1, 49.0f)); - hook.addComponent(5, 1, pl2); hook = world.createEntity(); hook.addComponent(Vector3(-14.5f, 14 - 1, 49.0f + 1)); - hook.addComponent(5, 1, pl2); auto pl3 = world.createEntity(); pl3.addComponent(Vector3(22, 14, -62.0f)); pl3.addComponent(100.0f, Color(0, 0.1f, 0), Color(0.0f, 1.0f, 0.0f), Color(0.0f, 1.0f, 0.0f)); - pl3.addComponent(); hook = world.createEntity(); hook.addComponent(Vector3(22 - 1, 14 - 1, -62.0f)); - hook.addComponent(5, 1, pl3); hook = world.createEntity(); hook.addComponent(Vector3(22, 14 + 0.5f, -62.0f - 1)); - hook.addComponent(5, 1, pl3); hook = world.createEntity(); hook.addComponent(Vector3(22 + 1, 14, -62.0f + 0.5f)); - hook.addComponent(5, 1, pl3); auto pl4 = world.createEntity(); pl4.addComponent(Vector3(-14.5f, 14, -61.5f)); pl4.addComponent(100.0f, Color(0.1, 0.05, 0), Color(1.0f, 0.55f, 0.0f), Color(1.0f, 0.55f, 0.0f)); - pl4.addComponent(); hook = world.createEntity(); hook.addComponent(Vector3(-14.5f - 1, 14, -61.5f -1)); - hook.addComponent(5, 1, pl4); hook = world.createEntity(); hook.addComponent(Vector3(-14.5f - 0.25f, 14 - 0.5f, -61.5f + 1)); - hook.addComponent(5, 1, pl4); hook = world.createEntity(); hook.addComponent(Vector3(-14.5f + 0.5f, 14+ 1, -61.5f + 1)); - hook.addComponent(5, 1, pl4); // Spears std::vector cols = { Color(1,0,0), Color(0,1,0), Color(0,0,1), Color(0.7f,0.55f,0) }; @@ -645,7 +438,6 @@ void SetupLights(ECSWorld& world) pl1 = world.createEntity(); pl1.addComponent(Vector3((i % 2 == 0 ? 8 : -1), 85, 49.5f - 37 * j), Vector3(1, 1, 1), Vector3(180, 0, 0)); pl1.addComponent(10.0f, 100, Color(0, 0, 0), cols[3 - j], cols[3 - j], 5); - pl1.addComponent((i % 2 == 0 ? 1 : -1) * 100,100,100); } } } \ No newline at end of file diff --git a/OpenGLEngine/OpenGLEngine/MoveInBoundsComponent.h b/OpenGLEngine/OpenGLEngine/MoveInBoundsComponent.h deleted file mode 100644 index c8ecce3..0000000 --- a/OpenGLEngine/OpenGLEngine/MoveInBoundsComponent.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once -#include "ECSConfig.h" - -namespace Reality -{ - struct MoveInBoundsComponent - { - MoveInBoundsComponent(Vector3 _velocity = Vector3(1, 1, 1), Vector3 _bounds = Vector3(10, 10, 10)) - :velocity(_velocity), bounds(_bounds) - { - - } - Vector3 velocity; - Vector3 bounds; - }; -} diff --git a/OpenGLEngine/OpenGLEngine/MoveInBoundsSystem.cpp b/OpenGLEngine/OpenGLEngine/MoveInBoundsSystem.cpp deleted file mode 100644 index 36f0fd9..0000000 --- a/OpenGLEngine/OpenGLEngine/MoveInBoundsSystem.cpp +++ /dev/null @@ -1,64 +0,0 @@ -#include "MoveInBoundsSystem.h" -#include "RigidBodyComponent.h" -#include "SphereColliderComponent.h" -#include "BoxColliderComponent.h" -namespace Reality -{ - MoveInBoundsSystem::MoveInBoundsSystem() - { - requireComponent(); - requireComponent(); - } - - void MoveInBoundsSystem::Update(float deltaTime) - { - for (auto e : getEntities()) - { - auto& move = e.getComponent(); - auto& transform = e.getComponent(); - bool spawn = false; - if (abs(transform.GetPosition().x) > move.bounds.x && transform.GetPosition().x * move.velocity.x > 0) - { - spawn = true; - //move.velocity.x *= -1; - } - if (abs(transform.GetPosition().y) > move.bounds.y && transform.GetPosition().y * move.velocity.y > 0) - { - spawn = true; - //move.velocity.y *= -1; - } - if (abs(transform.GetPosition().z) > move.bounds.z && transform.GetPosition().z * move.velocity.z > 0) - { - spawn = true; - //move.velocity.z *= -1; - } - transform.SetPosition(transform.GetPosition() + move.velocity * deltaTime); - if (spawn) - { - /*auto en = getWorld().createEntity(); - en.addComponent(-transform.GetPosition()); - en.addComponent(); - en.addComponent(move.velocity, move.bounds); - auto col = getWorld().createEntity(); - col.addComponent(en, 30);*/ - auto en = getWorld().createEntity(); - en.addComponent(Vector3(RANDOM_FLOAT(-200.0f, 200.0f), RANDOM_FLOAT(-200.0f, 200.0f), RANDOM_FLOAT(-200.0f, 200.0f)), - Vector3(1, 1, 1), - Vector3(RANDOM_FLOAT(-180.0f, 180.0f), RANDOM_FLOAT(-180.0f, 180.0f), RANDOM_FLOAT(-180.0f, 180.0f))); - en.addComponent(); - en.addComponent(Vector3(RANDOM_FLOAT(-10.0f, 10.0f), RANDOM_FLOAT(-10.0f, 10.0f), RANDOM_FLOAT(-10.0f, 10.0f)), - Vector3(200, 200, 200)); - auto col = getWorld().createEntity(); - if ((RANDOM_FLOAT(0.0f, 1.0f) >= 0.5f)) - { - col.addComponent(en, RANDOM_FLOAT(10.0f, 50.0f)); - } - else - { - col.addComponent(en, Vector3(RANDOM_FLOAT(30.0f, 70.0f), RANDOM_FLOAT(30.0f, 70.0f), RANDOM_FLOAT(30.0f, 70.0f))); - } - e.kill(); - } - } - } -} diff --git a/OpenGLEngine/OpenGLEngine/MoveInBoundsSystem.h b/OpenGLEngine/OpenGLEngine/MoveInBoundsSystem.h deleted file mode 100644 index 9fa2839..0000000 --- a/OpenGLEngine/OpenGLEngine/MoveInBoundsSystem.h +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once -#include "ECSConfig.h" -#include "MoveInBoundsComponent.h" -#include "TransformComponentV2.h" - -namespace Reality -{ - class MoveInBoundsSystem : public ECSSystem - { - public: - MoveInBoundsSystem(); - void Update(float deltaTime); - }; -} diff --git a/OpenGLEngine/OpenGLEngine/NBodyComponent.h b/OpenGLEngine/OpenGLEngine/NBodyComponent.h new file mode 100644 index 0000000..c9b33b8 --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/NBodyComponent.h @@ -0,0 +1,16 @@ +#pragma once +#include "ECSConfig.h" + + +namespace Reality +{ + struct NBodyComponent + { + NBodyComponent(float _gravityScale = 1.0f) + : gravityScale(_gravityScale) + { + + } + float gravityScale; + }; +} diff --git a/OpenGLEngine/OpenGLEngine/NBodySystem.cpp b/OpenGLEngine/OpenGLEngine/NBodySystem.cpp new file mode 100644 index 0000000..5e20f9b --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/NBodySystem.cpp @@ -0,0 +1,51 @@ +#include "NBodySystem.h" +#include "TransformComponent.h" + +namespace Reality +{ + NBodySystem::NBodySystem() + { + requireComponent(); + } + + void NBodySystem::Update(float deltaTime) + { + for (auto e : getEntities()) + { + auto& particleTransform = e.getComponent(); + auto& particleForceAcc = e.getComponent(); + + // Calculate the force of gravity + float force = + gravityConstant * ((mass*(1 / particleForceAcc.inverseMass)) / + pow(glm::length(particleTransform.position - centerPoint), 2)); + + particleForceAcc.AddForce((glm::normalize(centerPoint - particleTransform.position) + *force)); + } + + // Gravitational force pulls all particles towards each other, as well as the "sun" + if (getEntities().size() > 1) + { + for (int i = 0; i < getEntities().size() - 1; i++) { + for (int j = i + 1; j < getEntities().size() - 1; j++) { + auto& particle1Transform = getEntities()[i].getComponent(); + auto& particle1ForceAcc = getEntities()[i].getComponent(); + auto& particle2Transform = getEntities()[j].getComponent(); + auto& particle2ForceAcc = getEntities()[j].getComponent(); + + float force = + gravityConstant * (((1 / particle1ForceAcc.inverseMass)*(1 / particle2ForceAcc.inverseMass)) / + pow(glm::length(particle1Transform.position - particle2Transform.position), 2)); + + particle1ForceAcc.AddForce((glm::normalize(particle2Transform.position - particle1Transform.position) + *force)); + particle2ForceAcc.AddForce((glm::normalize(particle1Transform.position - particle2Transform.position) + *force)); + } + } + } + // Draw "sun" + getWorld().data.renderUtil->DrawSphere(Vector3(0.0f), 3, Color(1, 1, 0, 1)); + } +} diff --git a/OpenGLEngine/OpenGLEngine/NBodySystem.h b/OpenGLEngine/OpenGLEngine/NBodySystem.h new file mode 100644 index 0000000..94e8d6e --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/NBodySystem.h @@ -0,0 +1,17 @@ +#pragma once +#include "ECSConfig.h" +#include "NBodyComponent.h" +#include "ForceAccumulatorComponent.h" + +namespace Reality +{ + class NBodySystem : public ECSSystem + { + public: + NBodySystem(); + void Update(float deltaTime); + float gravityConstant = 3.0f; + Vector3 centerPoint = Vector3(0.0f); + float mass = 250.0f; + }; +} diff --git a/OpenGLEngine/OpenGLEngine/OpenGLEngine.vcxproj b/OpenGLEngine/OpenGLEngine/OpenGLEngine.vcxproj index dbb8b45..b17ae0d 100644 --- a/OpenGLEngine/OpenGLEngine/OpenGLEngine.vcxproj +++ b/OpenGLEngine/OpenGLEngine/OpenGLEngine.vcxproj @@ -121,30 +121,17 @@ - - - - - - - - - + - - + - - - - - + @@ -160,33 +147,25 @@ - - + - + - - + - - - + - - - - @@ -196,109 +175,59 @@ - + - - - - - - - - - - - + - - - - - + + + - - + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - + - - - - + - + + - - - - - - - - - + + - + - @@ -320,54 +249,41 @@ - - - + - + - + - - + - - - + - - + - - + + + - + - - - - - - - - @@ -379,86 +295,50 @@ - + - + - - - - - - - - - - - - - - - + - + - + - - - - - + + + + + - + + + + - - - - + - - - - - - - - - - - - - - - - - - - - - - diff --git a/OpenGLEngine/OpenGLEngine/OpenGLEngine.vcxproj.filters b/OpenGLEngine/OpenGLEngine/OpenGLEngine.vcxproj.filters index 0199877..5c3801b 100644 --- a/OpenGLEngine/OpenGLEngine/OpenGLEngine.vcxproj.filters +++ b/OpenGLEngine/OpenGLEngine/OpenGLEngine.vcxproj.filters @@ -43,12 +43,36 @@ {09a9940b-c17f-43c0-becf-d8da8efa3d30} - - {1a9d715c-69e4-4d0c-b881-159070a97fa6} - {3f5a8043-c367-47f5-bbe1-005323d21b00} + + {3b57134f-d90e-46bd-8888-c0777df6a173} + + + {2a1ab3ed-c9b4-469a-8efb-a6ad96814ca1} + + + {a47e3b81-90db-42eb-a5f1-e5a072f9b973} + + + {05f83a53-3654-4ae0-99f7-3b5b0131942b} + + + {4c54bdb5-bf1a-46eb-97aa-4ad2f029416e} + + + {26df7371-0611-42cd-b6e5-2556995c23d7} + + + {d7dfa830-a004-42bd-ba44-27121774ea6a} + + + {3b55161d-d015-4fd4-9031-364339f7b1d4} + + + {3de3ce0c-06e6-4384-a478-59fbb453ba67} + @@ -81,27 +105,9 @@ ECSCore - - TestCS - - - Physics\Particles - - - Physics\Particles - - - Physics\Particles - - - Physics\Particles - Input - - TestCS - Rendering @@ -126,86 +132,53 @@ Rendering - - TestCS - - - TestCS - - - TestCS - Rendering - - ComponentsCore - - + Rendering - - TestCS\FlightSim - - - TestCS\FlightSim - - - TestCS\FlightSim - - - TestCS\FlightSim - - + TestCS - + TestCS - - TestCS\FlightSim + + Physics\Particles - - TestCS\FlightSim + + Physics\Particles - - TestCS\FlightSim + + TestCS\FireWorksDemo - - Rendering + + Physics\Particles\ForceGenerators\Gravity - - Physics\Rigidbody + + Physics\Particles\ForceGenerators\Drag - - Physics\Rigidbody + + Physics\Particles\ForceGenerators\FixedSpring - - Physics\Particles + + Physics\Particles\PairedSpring - - Physics\Rigidbody + + Physics\BungeeCord - - Physics\Rigidbody + + Physics\Buoyancy - - Physics\Rigidbody + + Physics\N-Body - - Physics\Particles + + Physics - Physics\Particles - - - Physics\Rigidbody - - - TestCS - - - Physics\Rigidbody + Physics @@ -272,42 +245,12 @@ ECSCore - - TestCS - - - TestCS - - - Physics\Particles - - - Physics\Particles - - - Physics\Particles - - - Physics\Particles - - - Physics\Particles - - - Physics\Particles - Input Input - - TestCS - - - TestCS - Rendering @@ -332,155 +275,92 @@ Rendering - - TestCS - - - TestCS - - - TestCS - - - TestCS - - - TestCS - - - TestCS - - - ComponentsCore - - - ComponentsCore - - + Rendering - - TestCS\FlightSim - - - TestCS\FlightSim - - - TestCS\FlightSim - - - TestCS\FlightSim - - - TestCS\FlightSim - - - TestCS\FlightSim - - - TestCS\FlightSim - - - TestCS\FlightSim - - + TestCS - + TestCS - + TestCS - + TestCS - - TestCS\FlightSim - - - TestCS\FlightSim - - - TestCS\FlightSim + + Physics\Particles - - TestCS\FlightSim + + Physics\Particles - - TestCS\FlightSim + + Physics\Particles - - TestCS\FlightSim + + Physics\Particles - - TestCS\FlightSim + + TestCS\FireWorksDemo - - Rendering + + TestCS\FireWorksDemo - - Physics\Rigidbody + + Physics\Particles\ForceGenerators\Gravity - - Physics\Rigidbody + + Physics\Particles\ForceGenerators\Gravity - - Physics\Rigidbody + + Physics\Particles\ForceGenerators\Drag - - Physics\Rigidbody + + Physics\Particles\ForceGenerators\Drag - Physics\Particles + Physics\Particles\ForceGenerators\FixedSpring - - Physics\Particles + + Physics\Particles\ForceGenerators\FixedSpring - - Physics\Rigidbody + + Physics\Particles\PairedSpring - - Physics\Rigidbody + + Physics\Particles\PairedSpring - - Physics\Rigidbody + + Physics\BungeeCord - - Physics\Rigidbody + + Physics\BungeeCord - - Physics\Rigidbody + + Physics\Buoyancy - - Physics\Rigidbody + + Physics\Buoyancy - - Physics\Particles + + Physics\N-Body - - Physics\Particles + + Physics\N-Body - - Physics\Particles + + Physics - - Physics\Particles - - - Physics\Rigidbody - - - Physics\Rigidbody + + Physics - - TestCS - - - TestCS + + Physics - - Physics\Rigidbody + + Physics diff --git a/OpenGLEngine/OpenGLEngine/PairedSpringComponent.h b/OpenGLEngine/OpenGLEngine/PairedSpringComponent.h index 88bf9a9..0511892 100644 --- a/OpenGLEngine/OpenGLEngine/PairedSpringComponent.h +++ b/OpenGLEngine/OpenGLEngine/PairedSpringComponent.h @@ -5,11 +5,20 @@ namespace Reality { struct PairedSpringComponent { - PairedSpringComponent(float _springConstant = 10, float _restLength = 10, ECSEntity a = ECSEntity(), ECSEntity b = ECSEntity()) - :springConstant(_springConstant), restLength(_restLength), entityA(a), entityB(b) {} + PairedSpringComponent(float _springConstant = 10.0f, + float _restLength = 10.0f, + ECSEntity _connectedEntityA = ECSEntity(), + ECSEntity _connectedEntityB = ECSEntity()) + : springConstant(_springConstant), + restLength(_restLength), + connectedEntityA(_connectedEntityA), + connectedEntityB(_connectedEntityB) + { + + } float springConstant; float restLength; - ECSEntity entityA; - ECSEntity entityB; + ECSEntity connectedEntityA; + ECSEntity connectedEntityB; }; -} \ No newline at end of file +} diff --git a/OpenGLEngine/OpenGLEngine/PairedSpringForceGeneratorSystem.cpp b/OpenGLEngine/OpenGLEngine/PairedSpringForceGeneratorSystem.cpp deleted file mode 100644 index 532d8eb..0000000 --- a/OpenGLEngine/OpenGLEngine/PairedSpringForceGeneratorSystem.cpp +++ /dev/null @@ -1,60 +0,0 @@ -#include "PairedSpringForceGeneratorSystem.h" -#include "TransformComponent.h" -#include "ParticleComponent.h" - -namespace Reality -{ - PairedSpringForceGeneratorSystem::PairedSpringForceGeneratorSystem() - { - requireComponent(); - } - - - void PairedSpringForceGeneratorSystem::Update(float deltaTime) - { - for (auto e : getEntities()) - { - auto &spring = e.getComponent(); - - if (spring.entityA.hasComponent() && - spring.entityB.hasComponent()) - { - //auto &particle = spring.entity.getComponent(); - auto &entityTransformA = spring.entityA.getComponent(); - auto &entityTransformB = spring.entityB.getComponent(); - - Vector3 relativePosition = entityTransformA.position - entityTransformB.position; - float length = glm::length(relativePosition); - float x = length - spring.restLength; - Vector3 direction = glm::normalize(relativePosition); - - if (spring.entityA.hasComponent()) - { - spring.entityA.getComponent().AddForce(-spring.springConstant * x * direction); - } - - if (spring.entityB.hasComponent()) - { - spring.entityB.getComponent().AddForce(spring.springConstant * x * direction); - } - - float g = 1.0f / (1.0f + pow(abs(x), 0.5f)); - float r = (1 - g); - Color color = Color(r, g, 0, 1); - - float deltaLength = length / 10.0f; - for (int i = 0; i < 10; i++) - { - getWorld().data.renderUtil->DrawCube( - entityTransformB.position + (float)i * deltaLength * direction, - Vector3(0.1f, 0.1f, 0.1f) * min((spring.springConstant / 10.0f), 5.0f), Vector3(0,0,0), color); - } - - getWorld().data.renderUtil->DrawLine( - entityTransformB.position, entityTransformB.position + length * direction, color); - } - } - - } -} - diff --git a/OpenGLEngine/OpenGLEngine/PairedSpringSystem.cpp b/OpenGLEngine/OpenGLEngine/PairedSpringSystem.cpp new file mode 100644 index 0000000..70e7b38 --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/PairedSpringSystem.cpp @@ -0,0 +1,61 @@ +#include "PairedSpringSystem.h" +#include "TransformComponent.h" +#include "ForceAccumulatorComponent.h" + +namespace Reality +{ + PairedSpringSystem::PairedSpringSystem() + { + requireComponent(); + } + + void PairedSpringSystem::Update(float deltaTime) + { + for (auto e : getEntities()) + { + auto& spring = e.getComponent(); + + if (spring.connectedEntityA.hasComponent() + && spring.connectedEntityB.hasComponent()) + { + auto& transformA = spring.connectedEntityA.getComponent(); + auto& transformB = spring.connectedEntityB.getComponent(); + + Vector3 relativePosition = transformA.position - transformB.position; + float length = glm::length(relativePosition); + if (length > 0) + { + float deltaL = length - spring.restLength; + Vector3 force = -glm::normalize(relativePosition); + force *= spring.springConstant * deltaL; + + if (spring.connectedEntityA.hasComponent()) + { + spring.connectedEntityA.getComponent().AddForce(force); + } + if (spring.connectedEntityB.hasComponent()) + { + spring.connectedEntityB.getComponent().AddForce(-force); + } + + float g = 1.0f / (1.0f + pow(abs(deltaL), 0.5f)); + float r = 1 - g; + + Color col = Color(r, g, 0, 1); + + float deltaLength = length / 10.0f; + Vector3 direction = glm::normalize(relativePosition); + for (int i = 0; i < 10; i++) + { + getWorld().data.renderUtil->DrawCube( + transformB.position + (float)i * deltaLength * direction, + Vector3(1.0f, 1.0f, 1.0f) * min((spring.restLength / 20.0f), 100.0f), Vector3(0, 0, 0), col); + } + getWorld().data.renderUtil->DrawLine(transformA.position, transformB.position, + col); + } + + } + } + } +} diff --git a/OpenGLEngine/OpenGLEngine/PairedSpringForceGeneratorSystem.h b/OpenGLEngine/OpenGLEngine/PairedSpringSystem.h similarity index 59% rename from OpenGLEngine/OpenGLEngine/PairedSpringForceGeneratorSystem.h rename to OpenGLEngine/OpenGLEngine/PairedSpringSystem.h index 5adbcab..5cf0204 100644 --- a/OpenGLEngine/OpenGLEngine/PairedSpringForceGeneratorSystem.h +++ b/OpenGLEngine/OpenGLEngine/PairedSpringSystem.h @@ -4,11 +4,10 @@ namespace Reality { - class PairedSpringForceGeneratorSystem : public ECSSystem + class PairedSpringSystem : public ECSSystem { public: - PairedSpringForceGeneratorSystem(); + PairedSpringSystem(); void Update(float deltaTime); }; } - diff --git a/OpenGLEngine/OpenGLEngine/ParticleComponent.h b/OpenGLEngine/OpenGLEngine/ParticleComponent.h index 3c7e057..2580c48 100644 --- a/OpenGLEngine/OpenGLEngine/ParticleComponent.h +++ b/OpenGLEngine/OpenGLEngine/ParticleComponent.h @@ -5,30 +5,12 @@ namespace Reality { struct ParticleComponent { - ParticleComponent(float mass = 1.0f, Vector3 _velocity = Vector3(0,0,0), float _gravityScale = 1) : - velocity(_velocity), gravityScale(_gravityScale) + ParticleComponent(Vector3 _velocity = Vector3(0, 0, 0)) + :velocity(_velocity), acceleration(Vector3(0, 0, 0)) { - inverseMass = 1 / mass; - accelaration = Vector3(0, 0, 0); - forceAccumulator = Vector3(0, 0, 0); + } + Vector3 acceleration; Vector3 velocity; - Vector3 accelaration; - float inverseMass; - float gravityScale; - inline void AddForce(Vector3 force) - { - forceAccumulator += force; - } - inline Vector3 GetForce() - { - return forceAccumulator; - } - inline void ResetForceAccumulator() - { - forceAccumulator = Vector3(0, 0, 0); - } - private: - Vector3 forceAccumulator; }; } diff --git a/OpenGLEngine/OpenGLEngine/ParticleContactComponent.h b/OpenGLEngine/OpenGLEngine/ParticleContactComponent.h deleted file mode 100644 index fad5d2d..0000000 --- a/OpenGLEngine/OpenGLEngine/ParticleContactComponent.h +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once -#include "ECSConfig.h" - -namespace Reality -{ - struct ParticleContactComponent - { - ParticleContactComponent(ECSEntity a = ECSEntity(), - ECSEntity b = ECSEntity(), - float _restitution = 1, - Vector3 _normal = Vector3(0,1,0), float _penetration = 0): - entityA(a), - entityB(b), - restitution(_restitution), - normal(_normal), - penetration (_penetration), - deltaMovePerMass(Vector3(0, 0, 0)){} - ECSEntity entityA; - ECSEntity entityB; - float restitution; - Vector3 normal; - float penetration; - Vector3 deltaMovePerMass; - }; -} \ No newline at end of file diff --git a/OpenGLEngine/OpenGLEngine/ParticleContactEvent.h b/OpenGLEngine/OpenGLEngine/ParticleContactEvent.h new file mode 100644 index 0000000..9fddfd9 --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/ParticleContactEvent.h @@ -0,0 +1,27 @@ +#pragma once +#include "ECSConfig.h" + +namespace Reality +{ + struct ParticleContactEvent + { + ParticleContactEvent(ECSEntity _entityA = ECSEntity(), + ECSEntity _entityB = ECSEntity(), + float _restitution = 1.0f, + Vector3 _normal = Vector3(0,1,0), + float _penetration = 0) + :entityA(_entityA), + entityB(_entityB), + restitution(_restitution), + normal(_normal), + penetration(_penetration) + { + + } + ECSEntity entityA; + ECSEntity entityB; + float restitution; + Vector3 normal; + float penetration; + }; +} diff --git a/OpenGLEngine/OpenGLEngine/ParticleContactResolutionSystem.cpp b/OpenGLEngine/OpenGLEngine/ParticleContactResolutionSystem.cpp index 42c892a..e8e4a07 100644 --- a/OpenGLEngine/OpenGLEngine/ParticleContactResolutionSystem.cpp +++ b/OpenGLEngine/OpenGLEngine/ParticleContactResolutionSystem.cpp @@ -1,23 +1,32 @@ #include "ParticleContactResolutionSystem.h" #include "ParticleComponent.h" -#include "TransformComponent.h" +#include "ForceAccumulatorComponent.h" namespace Reality { ParticleContactResolutionSystem::ParticleContactResolutionSystem() { - requireComponent(); + } - float ParticleContactResolutionSystem::CalculateSeparatingVelocity(ParticleContactComponent& contact) + void ParticleContactResolutionSystem::Update(float deltaTime) { - Vector3 velocityA = contact.entityA.hasComponent() ? contact.entityA.getComponent().velocity : Vector3(0, 0, 0); - Vector3 velocityB = contact.entityB.hasComponent() ? contact.entityB.getComponent().velocity : Vector3(0, 0, 0); - Vector3 relativeVel = velocityA - velocityB; - return glm::dot(relativeVel, contact.normal); + auto contactEvents = getWorld().getEventManager().getEvents(); + for (auto& contactEvent : contactEvents) + { + ResolveVelocity(contactEvent, deltaTime); + } } - - void ParticleContactResolutionSystem::ResolveVelocity(ParticleContactComponent& contact, float deltaTime) + float ParticleContactResolutionSystem::CalculateSeparatingVelocity(ParticleContactEvent & contact) + { + Vector3 velocityA = contact.entityA.hasComponent() ? + contact.entityA.getComponent().velocity : Vector3(0, 0, 0); + Vector3 velocityB = contact.entityB.hasComponent() ? + contact.entityB.getComponent().velocity : Vector3(0, 0, 0); + Vector3 relativeVelocity = velocityA - velocityB; + return glm::dot(relativeVelocity, contact.normal); + } + void ParticleContactResolutionSystem::ResolveVelocity(ParticleContactEvent & contact, float deltaTime) { float separatingVelocity = CalculateSeparatingVelocity(contact); @@ -26,38 +35,14 @@ namespace Reality return; } - bool isAvalid = contact.entityA.hasComponent(); - bool isBvalid = contact.entityB.hasComponent(); - float invM1 = isAvalid ? contact.entityA.getComponent().inverseMass : 0; - float invM2 = isBvalid ? contact.entityB.getComponent().inverseMass : 0; - - - float newSeparatingVelocity = -separatingVelocity * contact.restitution; - - // Check the velocity build up due to accelaration only - Vector3 accCausedVelocity = Vector3(0, 0, 0); - if (isAvalid) - { - accCausedVelocity += contact.entityA.getComponent().accelaration; - } - if (isBvalid) - { - accCausedVelocity -= contact.entityB.getComponent().accelaration; - } - float accCausedSepVelocity = glm::dot(accCausedVelocity, contact.normal) * deltaTime; + bool isValidA = contact.entityA.hasComponent(); + bool isValidB = contact.entityB.hasComponent(); - // If we have a closing velocity due to accelaration build up, - // remove it from new separating velocity - if (accCausedSepVelocity < 0) - { - newSeparatingVelocity += contact.restitution * accCausedSepVelocity; - if (newSeparatingVelocity < 0) - { - newSeparatingVelocity = 0; - } - } + float invM1 = isValidA ? contact.entityA.getComponent().inverseMass : 0; + float invM2 = isValidB ? contact.entityA.getComponent().inverseMass : 0; - float deltaVelocity = newSeparatingVelocity - separatingVelocity; + float newSeparatingVelocity = -contact.restitution * separatingVelocity; + float deltaVel = newSeparatingVelocity - separatingVelocity; float totalInverseMass = invM1 + invM2; @@ -66,121 +51,16 @@ namespace Reality return; } - float impulse = deltaVelocity / totalInverseMass; - + float impulse = deltaVel / totalInverseMass; Vector3 impulsePerIMass = contact.normal * impulse; - if (isAvalid) + if (contact.entityA.hasComponent()) { contact.entityA.getComponent().velocity += impulsePerIMass * invM1; } - if (isBvalid) + if (contact.entityB.hasComponent()) { contact.entityB.getComponent().velocity -= impulsePerIMass * invM2; } } - - void ParticleContactResolutionSystem::ResolveInterpenetration(ParticleContactComponent& contact) - { - if (contact.penetration <= 0) - { - return; - } - - bool isAvalid = contact.entityA.hasComponent(); - bool isBvalid = contact.entityB.hasComponent(); - float invM1 = isAvalid ? contact.entityA.getComponent().inverseMass : 0; - float invM2 = isBvalid ? contact.entityB.getComponent().inverseMass : 0; - - float totalInverseMass = invM1 + invM2; - - if (totalInverseMass <= 0) - { - return; - } - - Vector3 movePerMass = contact.normal * (-contact.penetration / totalInverseMass); - contact.deltaMovePerMass = movePerMass; - if (isAvalid) - { - contact.entityA.getComponent().position -= movePerMass * invM1; - } - - if (isBvalid) - { - contact.entityB.getComponent().position += movePerMass * invM2; - } - //contact.penetration = 0; - } - - void ParticleContactResolutionSystem::UpdateInterpenetration(ParticleContactComponent & bestContact, ParticleContactComponent & contact) - { - bool isAvalid = contact.entityA.hasComponent(); - bool isBvalid = contact.entityB.hasComponent(); - float invM1 = isAvalid ? contact.entityA.getComponent().inverseMass : 0; - float invM2 = isBvalid ? contact.entityB.getComponent().inverseMass : 0; - if (bestContact.entityA == contact.entityA || bestContact.entityB == contact.entityA) - { - float mult = bestContact.entityA == contact.entityA ? -1 : 1; - Vector3 deltaMove = mult * bestContact.deltaMovePerMass * invM1; - float deltaPenetration = glm::dot(deltaMove, contact.normal); - contact.penetration -= deltaPenetration; - } - if (bestContact.entityB == contact.entityB || bestContact.entityA == contact.entityB) - { - float mult = bestContact.entityA == contact.entityB ? -1 : 1; - Vector3 deltaMove = mult * bestContact.deltaMovePerMass * invM2; - float deltaPenetration = glm::dot(deltaMove, contact.normal); - contact.penetration += deltaPenetration; - } - } - - void ParticleContactResolutionSystem::Update(float deltaTime) - { - iterationsUsed = 0; - iterations = getEntities().size() * 2; - - if (getEntities().size() > 0) - { - unsigned int bestContactIndex = 0; - unsigned int lastBest = 0; - while (iterationsUsed < iterations) - { - // Find the contact with the largest closing velocity - float max = 0; - for (int i = 0; i < getEntities().size(); i++) - { - auto e = getEntities()[i]; - auto &contact = e.getComponent(); - if (iterationsUsed > 0) - { - UpdateInterpenetration(getEntities()[lastBest].getComponent(), contact); - } - float sepVel = CalculateSeparatingVelocity(contact); - if (sepVel < max) - { - max = sepVel; - bestContactIndex = i; - } - } - if (max >= 0) - { - break; - } - auto& bestContact = getEntities()[bestContactIndex].getComponent(); - ResolveVelocity(bestContact, deltaTime); - ResolveInterpenetration(bestContact); - lastBest = bestContactIndex; - iterationsUsed++; - } - - for (auto e : getEntities()) - { - e.kill(); - } - } - - - - } } diff --git a/OpenGLEngine/OpenGLEngine/ParticleContactResolutionSystem.h b/OpenGLEngine/OpenGLEngine/ParticleContactResolutionSystem.h index ff0daa1..4f45757 100644 --- a/OpenGLEngine/OpenGLEngine/ParticleContactResolutionSystem.h +++ b/OpenGLEngine/OpenGLEngine/ParticleContactResolutionSystem.h @@ -1,6 +1,6 @@ #pragma once #include "ECSConfig.h" -#include "ParticleContactComponent.h" +#include "ParticleContactEvent.h" namespace Reality { @@ -9,13 +9,8 @@ namespace Reality public: ParticleContactResolutionSystem(); void Update(float deltaTime); - unsigned int iterations = 1; private: - float CalculateSeparatingVelocity(ParticleContactComponent& contact); - void ResolveVelocity(ParticleContactComponent& contact, float deltaTime); - void ResolveInterpenetration(ParticleContactComponent& contact); - void UpdateInterpenetration(ParticleContactComponent& bestContact, ParticleContactComponent& contact); - unsigned int iterationsUsed = 0; + float CalculateSeparatingVelocity(ParticleContactEvent& contact); + void ResolveVelocity(ParticleContactEvent& contact, float deltaTime); }; } - diff --git a/OpenGLEngine/OpenGLEngine/ParticleSpawnerComponent.h b/OpenGLEngine/OpenGLEngine/ParticleSpawnerComponent.h deleted file mode 100644 index ffc769c..0000000 --- a/OpenGLEngine/OpenGLEngine/ParticleSpawnerComponent.h +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once -#include "ECSConfig.h" -namespace Reality -{ - struct ParticleSpawnerComponent - { - ParticleSpawnerComponent(unsigned int _numParticles = 10, float _duration = 1, float _particleSpeed = 10) - :numberOfParticles(_numParticles), duration(_duration), particleSpeed(_particleSpeed), timer(_duration){} - float particleSpeed; - unsigned int numberOfParticles; - float duration; - float timer; - }; -} \ No newline at end of file diff --git a/OpenGLEngine/OpenGLEngine/ParticleSpawnerSystem.cpp b/OpenGLEngine/OpenGLEngine/ParticleSpawnerSystem.cpp deleted file mode 100644 index 68689ab..0000000 --- a/OpenGLEngine/OpenGLEngine/ParticleSpawnerSystem.cpp +++ /dev/null @@ -1,50 +0,0 @@ -#include "ParticleSpawnerSystem.h" -#include "ParticleComponent.h" -#include "MeshComponent.h" - -namespace Reality -{ - ParticleSpawnerSystem::ParticleSpawnerSystem() - { - requireComponent(); - requireComponent(); - } - - void ParticleSpawnerSystem::Update(float deltaTime) - { - for (auto e : getEntities()) - { - auto &particleSpawner = e.getComponent(); - auto &transform = e.getComponent(); - - // Update Timer - particleSpawner.timer += deltaTime; - - if (particleSpawner.timer >= particleSpawner.duration) - { - float deltaAngle = 2 * 3.14f / particleSpawner.numberOfParticles; - for (int i = 0; i < particleSpawner.numberOfParticles; i++) - { - auto e = getWorld().createEntity(); - e.addComponent(transform.position, Vector3(0.1f, 0.1f, 0.1f)); - - // Calculate velocity, spread out in a circle - Vector3 velocity; - velocity.y = particleSpawner.particleSpeed; - float angle = i * deltaAngle; - velocity.x = cos(angle) * particleSpawner.particleSpeed; - velocity.z = sin(angle) * particleSpawner.particleSpeed; - - // Add particle with gravity - e.addComponent(1.0f, velocity); - - // Add mesh - e.addComponent("Resources/Models/nanosuit/nanosuit.obj"); - } - - // Reset timer - particleSpawner.timer = 0; - } - } - } -} diff --git a/OpenGLEngine/OpenGLEngine/ParticleSphereComponent.h b/OpenGLEngine/OpenGLEngine/ParticleSphereComponent.h new file mode 100644 index 0000000..ae30097 --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/ParticleSphereComponent.h @@ -0,0 +1,15 @@ +#pragma once +#include "ECSConfig.h" + +namespace Reality +{ + struct ParticleSphereComponent + { + ParticleSphereComponent(float _radius = 1.0f) + :radius(_radius) + { + + } + float radius; + }; +} diff --git a/OpenGLEngine/OpenGLEngine/ParticleSphereSystem.cpp b/OpenGLEngine/OpenGLEngine/ParticleSphereSystem.cpp new file mode 100644 index 0000000..266b959 --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/ParticleSphereSystem.cpp @@ -0,0 +1,63 @@ +#include "ParticleSphereSystem.h" +#include "ParticleContactEvent.h" + +namespace Reality +{ + ParticleSphereSystem::ParticleSphereSystem() + { + requireComponent(); + requireComponent(); + } + + void ParticleSphereSystem::Update(float deltaTime) + { + for (auto e : getEntities()) + { + if (DEBUG_LOG_LEVEL > 0) + { + getWorld().data.renderUtil->DrawSphere( + e.getComponent().position, + e.getComponent().radius, + Color::Red + ); + } + } + if (getEntities().size() > 1) + { + for (int i = 0; i < getEntities().size() - 1; i++) + { + for (int j = i + 1; j < getEntities().size(); j++) + { + if (CheckCollision(getEntities()[i], getEntities()[j]) && DEBUG_LOG_LEVEL > 0) + { + getWorld().data.renderUtil->DrawLine( + getEntities()[i].getComponent().position, + getEntities()[j].getComponent().position, + Color::Red + ); + } + } + } + } + } + bool ParticleSphereSystem::CheckCollision(ECSEntity & sphereA, ECSEntity & sphereB) + { + Vector3 posA = sphereA.getComponent().position; + Vector3 posB = sphereB.getComponent().position; + float r1 = sphereA.getComponent().radius; + float r2 = sphereB.getComponent().radius; + + if (glm::distance(posA, posB) < r1 + r2) + { + getWorld().getEventManager().emitEvent( + sphereA, + sphereB, + 1.0f, + glm::normalize(posA-posB), + r1 + r2 - glm::distance(posA, posB) + ); + return true; + } + return false; + } +} diff --git a/OpenGLEngine/OpenGLEngine/ParticleSphereSystem.h b/OpenGLEngine/OpenGLEngine/ParticleSphereSystem.h new file mode 100644 index 0000000..d3859df --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/ParticleSphereSystem.h @@ -0,0 +1,16 @@ +#pragma once +#include "ECSConfig.h" +#include "ParticleSphereComponent.h" +#include "TransformComponent.h" + +namespace Reality +{ + class ParticleSphereSystem : public ECSSystem + { + public: + ParticleSphereSystem(); + void Update(float deltaTime); + private: + bool CheckCollision(ECSEntity& sphereA, ECSEntity& sphereB); + }; +} diff --git a/OpenGLEngine/OpenGLEngine/ParticleSystem.cpp b/OpenGLEngine/OpenGLEngine/ParticleSystem.cpp index 41744a2..c9b3b97 100644 --- a/OpenGLEngine/OpenGLEngine/ParticleSystem.cpp +++ b/OpenGLEngine/OpenGLEngine/ParticleSystem.cpp @@ -1,6 +1,5 @@ #include "ParticleSystem.h" - namespace Reality { ParticleSystem::ParticleSystem() @@ -13,21 +12,16 @@ namespace Reality { for (auto e : getEntities()) { - auto &particle = e.getComponent(); - auto &transform = e.getComponent(); + auto& transform = e.getComponent(); + auto& particle = e.getComponent(); + + particle.velocity += particle.acceleration * deltaTime; + transform.position += particle.velocity * deltaTime; - // HACK for bounce - if (transform.position.y <= -10) + if (DEBUG_LOG_LEVEL > 0) { - //particle.velocity.y = -particle.velocity.y; - //e.kill(); + getWorld().data.renderUtil->DrawSphere(transform.position); } - - // Update velocity from accelarartion - particle.velocity += particle.accelaration * deltaTime; - - // Update position from velocity - transform.position += particle.velocity * deltaTime; } } } diff --git a/OpenGLEngine/OpenGLEngine/ParticleSystem.h b/OpenGLEngine/OpenGLEngine/ParticleSystem.h index 4c69212..2c926e8 100644 --- a/OpenGLEngine/OpenGLEngine/ParticleSystem.h +++ b/OpenGLEngine/OpenGLEngine/ParticleSystem.h @@ -12,4 +12,3 @@ namespace Reality void Update(float deltaTime); }; } - diff --git a/OpenGLEngine/OpenGLEngine/RenderingSystemV2.cpp b/OpenGLEngine/OpenGLEngine/RenderingSystemV2.cpp deleted file mode 100644 index aa2dbef..0000000 --- a/OpenGLEngine/OpenGLEngine/RenderingSystemV2.cpp +++ /dev/null @@ -1,40 +0,0 @@ -#include "RenderingSystemV2.h" - -namespace Reality -{ - RenderingSystemV2::RenderingSystemV2() - { - requireComponent(); - requireComponent(); - } - - void RenderingSystemV2::Update(float deltaTime) - { - for (auto e : getEntities()) - { - auto& transform = e.getComponent(); - auto &model = e.getComponent(); - - if (getWorld().data.assetLoader->ModelsLoaded()) - { - getWorld().data.assetLoader->SetLight(getWorld().data.renderUtil->camera.Position); - } - if (model.modelId < 0) - { - model.modelId = getWorld().data.assetLoader->GetModelId(model.mesh); - } - if (model.modelId >= 0) - { - //Mat4 modelMat = glm::translate(glm::mat4(1.0f), model.offset.x * transform.Right() + model.offset.y * transform.Up() + model.offset.z * transform.Forward()) * transform.transformationMatrix; - //Mat4 modelMat = glm::translate(transform.transformationMatrix, model.offset.x * transform.Right() + model.offset.y * transform.Up() + model.offset.z * transform.Forward()); - glm::vec3 rotationOffsetInRads = glm::vec3(glm::radians(model.rotation.x), glm::radians(model.rotation.y), glm::radians(model.rotation.z)); - glm::mat4 rotationOffsetMat = glm::toMat4(glm::quat(rotationOffsetInRads)); - Mat4 modelMat = transform.GetTransformationMatrix() * glm::translate(glm::mat4(1.0f), model.offset) * rotationOffsetMat; - getWorld().data.renderUtil->DrawModel(model.modelId, modelMat); - } - getWorld().data.renderUtil->DrawLine(transform.GetPosition(), transform.GetPosition() + transform.Right() * 10.0f, Color::Red); - getWorld().data.renderUtil->DrawLine(transform.GetPosition(), transform.GetPosition() + transform.Up() * 10.0f, Color::Green); - getWorld().data.renderUtil->DrawLine(transform.GetPosition(), transform.GetPosition() + transform.Forward() * 10.0f, Color::Blue); - } - } -} diff --git a/OpenGLEngine/OpenGLEngine/RenderingSystemV2.h b/OpenGLEngine/OpenGLEngine/RenderingSystemV2.h deleted file mode 100644 index b2a18e5..0000000 --- a/OpenGLEngine/OpenGLEngine/RenderingSystemV2.h +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once -#include "ECSConfig.h" -#include "TransformComponentV2.h" -#include "MeshComponent.h" -namespace Reality -{ - class RenderingSystemV2 : public ECSSystem - { - public: - RenderingSystemV2(); - void Update(float deltaTime); - }; -} diff --git a/OpenGLEngine/OpenGLEngine/RigidBodyComponent.h b/OpenGLEngine/OpenGLEngine/RigidBodyComponent.h deleted file mode 100644 index f391a16..0000000 --- a/OpenGLEngine/OpenGLEngine/RigidBodyComponent.h +++ /dev/null @@ -1,76 +0,0 @@ -#pragma once -#include "ECSConfig.h" - -namespace Reality -{ - struct RigidBodyComponent - { - RigidBodyComponent(float mass = 1.0f, float _linearDamping = 0.4f, float _angularDamping = 0.3f, Vector3 _velocity = Vector3(0, 0, 0), Vector3 _angularVelocity = Vector3(0, 0, 0), float _gravityScale = 1) - : inverseMass(1.0f / mass), velocity(_velocity), angularVelocity(_angularVelocity), linearDamping(_linearDamping), angularDamping(_angularDamping), gravityScale(_gravityScale), rp3dId(-1) - { - inertiaTensor = glm::mat3(0.0f); - inertiaTensor[0][0] = 0.001f; - inertiaTensor[1][1] = 0.001f; - inertiaTensor[2][2] = 0.001f; - accelaration = Vector3(0, 0, 0); - angularAccelaration = Vector3(0, 0, 0); - forceAccumulator = Vector3(0, 0, 0); - torqueAccumulator = Vector3(0, 0, 0); - } - float inverseMass; - Vector3 velocity; - Vector3 angularVelocity; - Vector3 accelaration; - Vector3 angularAccelaration; - Mat3 inertiaTensor; - float gravityScale; - float linearDamping; - float angularDamping; - int rp3dId; - - Mat3 worldInverseInertiaTensor(Mat3 localToWorld) - { - Mat3 worldInertiaTensor = localToWorld * inertiaTensor * glm::inverse(localToWorld); - return glm::inverse(worldInertiaTensor); - } - - - // Forces - inline void AddForce(Vector3 force) - { - forceAccumulator += force; - } - inline Vector3 GetForce() - { - return forceAccumulator; - } - inline void ResetForceAccumulator() - { - forceAccumulator = Vector3(0, 0, 0); - } - - // Torque and force - inline void AddForceAtPoint(Vector3 force, Vector3 point, Vector3 origin) - { - AddForce(force); - AddTorque(glm::cross(point - origin, force)); - } - - // Torque - inline void AddTorque(Vector3 torque) - { - torqueAccumulator += torque; - } - inline Vector3 GetTorque() - { - return torqueAccumulator; - } - inline void ResetTorqueAccumulator() - { - torqueAccumulator = Vector3(0, 0, 0); - } - private: - Vector3 forceAccumulator; - Vector3 torqueAccumulator; - }; -} diff --git a/OpenGLEngine/OpenGLEngine/RigidBodyData.h b/OpenGLEngine/OpenGLEngine/RigidBodyData.h deleted file mode 100644 index d02cd64..0000000 --- a/OpenGLEngine/OpenGLEngine/RigidBodyData.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once -#include "ECSConfig.h" - -namespace Reality -{ - struct RigidBodyData - { - RigidBodyData(ECSEntity _entity) - :entity(_entity) - { - - } - ECSEntity entity; - }; -} diff --git a/OpenGLEngine/OpenGLEngine/RigidBodySystem.cpp b/OpenGLEngine/OpenGLEngine/RigidBodySystem.cpp deleted file mode 100644 index aa20197..0000000 --- a/OpenGLEngine/OpenGLEngine/RigidBodySystem.cpp +++ /dev/null @@ -1,77 +0,0 @@ -#include "RigidBodySystem.h" -#include "RigidBodyData.h" - -namespace Reality -{ - RigidBodySystem::RigidBodySystem(rp3d::CollisionWorld& _rp3dWorld) - :rp3dWorld(_rp3dWorld) - { - requireComponent(); - requireComponent(); - } - - void RigidBodySystem::Update(float deltaTime) - { - std::vector rp3dBodiesTemp; - std::vector aliveIds; - aliveIds.resize(rp3dBodies.size()); - int id = 0; - - for (auto e : getEntities()) - { - auto &rigidbody = e.getComponent(); - auto &transform = e.getComponent(); - - // Update RP3D Ids - // Calculate rp3d transform - rp3d::Vector3 initPosition(transform.GetPosition().x, - transform.GetPosition().y, - transform.GetPosition().z); - Quaternion quat = transform.GetOrientation(); - rp3d::Quaternion initOrientation = rp3d::Quaternion( - quat.x, quat.y, quat.z, quat.w); - rp3d::Transform rp3dtransform(initPosition, initOrientation); - // If new rigidbody, create an entry - if (rigidbody.rp3dId < 0) - { - rp3d::CollisionBody * body = rp3dWorld.createCollisionBody(rp3dtransform); - RigidBodyData* data = new RigidBodyData(e); - body->setUserData(data); - rp3dBodiesTemp.push_back(body); - rigidbody.rp3dId = id; - } - else - { - rp3d::CollisionBody * body = rp3dBodies[rigidbody.rp3dId]; - body->setTransform(rp3dtransform); - aliveIds[rigidbody.rp3dId] = 1; - rp3dBodiesTemp.push_back(body); - rigidbody.rp3dId = id; - } - id++; - - // Update velocity from accelarartion - rigidbody.velocity += rigidbody.accelaration * deltaTime; - rigidbody.angularVelocity += rigidbody.angularAccelaration * deltaTime; - - // Damping - rigidbody.velocity *= pow(1.0f - rigidbody.linearDamping, deltaTime); - rigidbody.angularVelocity *= pow(1.0f - rigidbody.angularDamping, deltaTime); - - transform.SetPosition(transform.GetPosition() + rigidbody.velocity * deltaTime); - glm::quat angularVelocityQuat = glm::quat(0, rigidbody.angularVelocity.x, rigidbody.angularVelocity.y, rigidbody.angularVelocity.z); - getWorld().data.renderUtil->DrawLine(transform.GetPosition(), transform.GetPosition() + rigidbody.angularVelocity, Color::Blue); - transform.SetOrientation(glm::normalize(transform.GetOrientation() + 0.5f * angularVelocityQuat * transform.GetOrientation() * deltaTime)); - } - - for (int i = 0; i < aliveIds.size(); i++) - { - if (aliveIds[i] == 0) - { - rp3dWorld.destroyCollisionBody(rp3dBodies[i]); - } - } - - rp3dBodies = rp3dBodiesTemp; - } -} diff --git a/OpenGLEngine/OpenGLEngine/RigidBodySystem.h b/OpenGLEngine/OpenGLEngine/RigidBodySystem.h deleted file mode 100644 index 88b8e03..0000000 --- a/OpenGLEngine/OpenGLEngine/RigidBodySystem.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once -#include "ECSConfig.h" -#include "RigidBodyComponent.h" -#include "TransformComponentV2.h" -#include - -namespace Reality -{ - class RigidBodySystem : public ECSSystem - { - public: - RigidBodySystem(rp3d::CollisionWorld& _rp3dWorld); - void Update(float deltaTime); - rp3d::CollisionWorld& rp3dWorld; - std::vector rp3dBodies; - }; -} diff --git a/OpenGLEngine/OpenGLEngine/RigidbodyGravityForceGeneratorSystem.cpp b/OpenGLEngine/OpenGLEngine/RigidbodyGravityForceGeneratorSystem.cpp deleted file mode 100644 index bf6fcef..0000000 --- a/OpenGLEngine/OpenGLEngine/RigidbodyGravityForceGeneratorSystem.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include "RigidbodyGravityForceGeneratorSystem.h" - -namespace Reality -{ - RigidbodyGravityForceGeneratorSystem::RigidbodyGravityForceGeneratorSystem() - { - requireComponent(); - } - - void RigidbodyGravityForceGeneratorSystem::Update(float deltaTime) - { - for (auto e : getEntities()) - { - auto &body = e.getComponent(); - if (body.inverseMass > 0) - { - body.AddForce(gravity * body.gravityScale / body.inverseMass); - } - } - } -} diff --git a/OpenGLEngine/OpenGLEngine/RigidbodyGravityForceGeneratorSystem.h b/OpenGLEngine/OpenGLEngine/RigidbodyGravityForceGeneratorSystem.h deleted file mode 100644 index e1ea768..0000000 --- a/OpenGLEngine/OpenGLEngine/RigidbodyGravityForceGeneratorSystem.h +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once -#include "ECSConfig.h" -#include "RigidBodyComponent.h" - -namespace Reality -{ - class RigidbodyGravityForceGeneratorSystem : public ECSSystem - { - public: - Vector3 gravity = Vector3(0.0f, -9.8f, 0.0f); - RigidbodyGravityForceGeneratorSystem(); - void Update(float deltaTime); - }; -} diff --git a/OpenGLEngine/OpenGLEngine/RodComponent.h b/OpenGLEngine/OpenGLEngine/RodComponent.h deleted file mode 100644 index 33583ad..0000000 --- a/OpenGLEngine/OpenGLEngine/RodComponent.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once -#include "ECSConfig.h" - -namespace Reality -{ - struct RodComponent - { - RodComponent(ECSEntity a = ECSEntity(), ECSEntity b = ECSEntity(), float _length = 10) - : entityA(a), entityB(b), length(_length) - {} - ECSEntity entityA; - ECSEntity entityB; - float length; - }; -} diff --git a/OpenGLEngine/OpenGLEngine/RodSystem.cpp b/OpenGLEngine/OpenGLEngine/RodSystem.cpp deleted file mode 100644 index f1da27e..0000000 --- a/OpenGLEngine/OpenGLEngine/RodSystem.cpp +++ /dev/null @@ -1,62 +0,0 @@ -#include "RodSystem.h" -#include "TransformComponent.h" -#include "ParticleContactComponent.h" - -namespace Reality -{ - RodSystem::RodSystem() - { - requireComponent(); - } - - void RodSystem::Update(float deltaTime) - { - for (auto e : getEntities()) - { - auto& rod = e.getComponent(); - float currentLength = glm::length(rod.entityA.getComponent().position - - rod.entityB.getComponent().position); - - getWorld().data.renderUtil->DrawSphere(rod.entityA.getComponent().position, 1, Color::Purple); - getWorld().data.renderUtil->DrawSphere(rod.entityB.getComponent().position, 1, Color::Purple); - - if (currentLength == rod.length) - { - continue; - } - - Vector3 normal = glm::normalize(rod.entityB.getComponent().position - - rod.entityA.getComponent().position); - - - if (currentLength > rod.length) - { - auto contactEntity = getWorld().createEntity(); - contactEntity.addComponent( - rod.entityA, - rod.entityB, - 0, - normal, - currentLength - rod.length); - getWorld().data.renderUtil->DrawLine(rod.entityA.getComponent().position, - rod.entityB.getComponent().position, - Color::Yellow); - } - else - { - auto contactEntity = getWorld().createEntity(); - contactEntity.addComponent( - rod.entityA, - rod.entityB, - 0, - -normal, - rod.length - currentLength); - getWorld().data.renderUtil->DrawLine(rod.entityA.getComponent().position, - rod.entityB.getComponent().position, - Color::Yellow); - } - - - } - } -} diff --git a/OpenGLEngine/OpenGLEngine/RotateComponent.h b/OpenGLEngine/OpenGLEngine/RotateComponent.h index ecc5968..bde45b6 100644 --- a/OpenGLEngine/OpenGLEngine/RotateComponent.h +++ b/OpenGLEngine/OpenGLEngine/RotateComponent.h @@ -1,9 +1,15 @@ #pragma once -struct RotateComponent +#include "ECSConfig.h" + +namespace Reality { - float xRot; - float yRot; - float zRot; - RotateComponent(float x = 0, float y = 0, float z = 0) - : xRot(x), yRot(y), zRot(z) {} -}; + struct RotateComponent + { + RotateComponent(Vector3 _rotationVelocity = Vector3(0, 0, 0)) + : rotationVelocity(_rotationVelocity) + { + + } + Vector3 rotationVelocity; + }; +} diff --git a/OpenGLEngine/OpenGLEngine/RotateSystem.cpp b/OpenGLEngine/OpenGLEngine/RotateSystem.cpp index 6bd16a0..19146ff 100644 --- a/OpenGLEngine/OpenGLEngine/RotateSystem.cpp +++ b/OpenGLEngine/OpenGLEngine/RotateSystem.cpp @@ -1,20 +1,21 @@ #include "RotateSystem.h" -RotateSystem::RotateSystem() +namespace Reality { - requireComponent(); - requireComponent(); -} + RotateSystem::RotateSystem() + { + requireComponent(); + requireComponent(); + } -void RotateSystem::Update(float deltaTime) -{ - for (auto e : getEntities()) + void RotateSystem::Update(float deltaTime) { - auto &rotate = e.getComponent(); - auto &transform = e.getComponent(); + for (auto e : getEntities()) + { + auto& transform = e.getComponent(); + auto& rotate = e.getComponent(); - transform.eulerAngles.x += rotate.xRot * deltaTime; - transform.eulerAngles.y += rotate.yRot * deltaTime; - transform.eulerAngles.z += rotate.zRot * deltaTime; + transform.eulerAngles += rotate.rotationVelocity * deltaTime; + } } } diff --git a/OpenGLEngine/OpenGLEngine/RotateSystem.h b/OpenGLEngine/OpenGLEngine/RotateSystem.h index 1a9cc29..76a4255 100644 --- a/OpenGLEngine/OpenGLEngine/RotateSystem.h +++ b/OpenGLEngine/OpenGLEngine/RotateSystem.h @@ -3,11 +3,12 @@ #include "TransformComponent.h" #include "RotateComponent.h" -using namespace Reality; -class RotateSystem : public ECSSystem +namespace Reality { -public: - RotateSystem(); - void Update(float deltaTime); -}; - + class RotateSystem : public ECSSystem + { + public: + RotateSystem(); + void Update(float deltaTime); + }; +} diff --git a/OpenGLEngine/OpenGLEngine/SetAerodynamicTensorSystem.cpp b/OpenGLEngine/OpenGLEngine/SetAerodynamicTensorSystem.cpp deleted file mode 100644 index 6ba18d7..0000000 --- a/OpenGLEngine/OpenGLEngine/SetAerodynamicTensorSystem.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include "SetAerodynamicTensorSystem.h" - -namespace Reality -{ - SetAerodynamicTensorSystem::SetAerodynamicTensorSystem() - { - requireComponent(); - requireComponent(); - } - - void SetAerodynamicTensorSystem::Update(float deltaTime) - { - for (auto e : getEntities()) - { - auto& aero = e.getComponent(); - auto& minMax = e.getComponent(); - - /*if (minMax.controlSetting <= -1.0f) - { - aero.aerodynamicTensor = minMax.minTensor; - } - else if (minMax.controlSetting >= 1.0f) - { - aero.aerodynamicTensor = minMax.maxTensor; - }*/ - if (minMax.controlSetting <= 0) - { - aero.aerodynamicTensor = -minMax.controlSetting * minMax.minTensor + (minMax.controlSetting + 1) * minMax.baseTensor; - } - else if (minMax.controlSetting > 0) - { - aero.aerodynamicTensor = (1-minMax.controlSetting) * minMax.baseTensor + minMax.controlSetting * minMax.maxTensor; - } - /*else - { - aero.aerodynamicTensor = minMax.baseTensor; - }*/ - - - } - } -} diff --git a/OpenGLEngine/OpenGLEngine/SetAerodynamicTensorSystem.h b/OpenGLEngine/OpenGLEngine/SetAerodynamicTensorSystem.h deleted file mode 100644 index ad467f0..0000000 --- a/OpenGLEngine/OpenGLEngine/SetAerodynamicTensorSystem.h +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once -#include "ECSConfig.h" -#include "AeroComponent.h" -#include "AeroMinMaxComponent.h" - -namespace Reality -{ - class SetAerodynamicTensorSystem : public ECSSystem - { - public: - SetAerodynamicTensorSystem(); - void Update(float deltaTime); - }; -} diff --git a/OpenGLEngine/OpenGLEngine/SpawnTargetEvent.h b/OpenGLEngine/OpenGLEngine/SpawnTargetEvent.h deleted file mode 100644 index a4ce6ad..0000000 --- a/OpenGLEngine/OpenGLEngine/SpawnTargetEvent.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once -#include "ECSConfig.h" - -namespace Reality -{ - struct SpawnTargetEvent - { - SpawnTargetEvent(Vector3 _targetPos = Vector3(0,0,0)) - :targetPos(_targetPos) - { - - } - Vector3 targetPos; - }; -} diff --git a/OpenGLEngine/OpenGLEngine/SphereColliderComponent.h b/OpenGLEngine/OpenGLEngine/SphereColliderComponent.h deleted file mode 100644 index 3d5e9cd..0000000 --- a/OpenGLEngine/OpenGLEngine/SphereColliderComponent.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once -#include "ECSConfig.h" - -namespace Reality -{ - struct SphereColliderComponent - { - SphereColliderComponent(ECSEntity _body = ECSEntity(), float _radius = 1.0f, Vector3 _offset = Vector3(0, 0, 0)) - : body(_body), offset(_offset), radius(_radius), rp3dId(-1) - {} - ECSEntity body; - Vector3 offset; - float radius; - int rp3dId; - }; -} diff --git a/OpenGLEngine/OpenGLEngine/SphereColliderSystem.cpp b/OpenGLEngine/OpenGLEngine/SphereColliderSystem.cpp deleted file mode 100644 index 46c752f..0000000 --- a/OpenGLEngine/OpenGLEngine/SphereColliderSystem.cpp +++ /dev/null @@ -1,91 +0,0 @@ -#include "SphereColliderSystem.h" -#include "RigidBodyComponent.h" -#include "RigidBodySystem.h" - -namespace Reality -{ - SphereColliderSystem::SphereColliderSystem(rp3d::CollisionWorld& _rp3dWorld) - :rp3dWorld(_rp3dWorld) - { - requireComponent(); - } - - void SphereColliderSystem::Update(float deltaTime) - { - std::vector rp3dShapesTemp; - std::vector aliveIds; - aliveIds.resize(rp3dShapes.size()); - int id = 0; - - for (auto e : getEntities()) - { - auto &sphereCollider = e.getComponent(); - - if (sphereCollider.body.isAlive() && sphereCollider.body.hasComponent()) - { - auto &body = sphereCollider.body.getComponent(); - - // Update RP3D Ids - // Calculate local rp3d transform - rp3d::Vector3 initPosition(sphereCollider.offset.x, - sphereCollider.offset.y, - sphereCollider.offset.z); - rp3d::Quaternion initOrientation = rp3d::Quaternion::identity(); - rp3d::Transform rp3dtransform(initPosition, initOrientation); - - auto rp3dBody = getWorld().getSystemManager().getSystem().rp3dBodies[body.rp3dId]; - // If new rigidbody, create an entry - if (sphereCollider.rp3dId < 0) - { - rp3d::decimal radius = rp3d::decimal(sphereCollider.radius); - rp3d::SphereShape* shape = new rp3d::SphereShape(radius); - // Add the collision shape to the rigid body - - rp3d::ProxyShape * proxyShape = rp3dBody->addCollisionShape(shape, rp3dtransform); - proxyShape->setUserData(&sphereCollider); - rp3dShapesTemp.push_back(proxyShape); - sphereCollider.rp3dId = id; - } - else if (sphereCollider.body.isAlive()) - { - rp3d::ProxyShape * shape = rp3dShapes[sphereCollider.rp3dId]; - shape->setLocalToBodyTransform(rp3dtransform); - aliveIds[sphereCollider.rp3dId] = 1; - rp3dShapesTemp.push_back(shape); - sphereCollider.rp3dId = id; - } - id++; - - if (sphereCollider.body.hasComponent()) - { - getWorld().data.renderUtil->DrawSphere(sphereCollider.body.getComponent().GetUnScaledTransformationMatrix() * Vector4(sphereCollider.offset, 1.0f), sphereCollider.radius); - } - } - else - { - // No need to kill it, the death of RB already killed it - aliveIds[sphereCollider.rp3dId] = 1; - e.kill(); - } - } - - for (int i = 0; i < aliveIds.size(); i++) - { - if (aliveIds[i] == 0) - { - if (rp3dShapes[i]) - { - auto shape = rp3dShapes[i]->getCollisionShapePublic(); - - if (rp3dShapes[i]->getBody() && rp3dShapes[i]->getBody()->getProxyShapesList()) - { - rp3dShapes[i]->getBody()->removeCollisionShape(rp3dShapes[i]); - } - delete shape; - } - } - } - - rp3dShapes = rp3dShapesTemp; - } -} diff --git a/OpenGLEngine/OpenGLEngine/SphereColliderSystem.h b/OpenGLEngine/OpenGLEngine/SphereColliderSystem.h deleted file mode 100644 index 9be8223..0000000 --- a/OpenGLEngine/OpenGLEngine/SphereColliderSystem.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once -#include "ECSConfig.h" -#include "SphereColliderComponent.h" -#include "TransformComponentV2.h" -#include - -namespace Reality -{ - class SphereColliderSystem : public ECSSystem - { - public: - SphereColliderSystem(rp3d::CollisionWorld& _rp3dWorld); - void Update(float deltaTime); - private: - rp3d::CollisionWorld& rp3dWorld; - std::vector rp3dShapes; - }; -} diff --git a/OpenGLEngine/OpenGLEngine/SphereComponent.h b/OpenGLEngine/OpenGLEngine/SphereComponent.h deleted file mode 100644 index 07a05ce..0000000 --- a/OpenGLEngine/OpenGLEngine/SphereComponent.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once -namespace Reality -{ - struct SphereComponent - { - SphereComponent(float _radius = 1) : radius(_radius){} - float radius; - }; -} \ No newline at end of file diff --git a/OpenGLEngine/OpenGLEngine/SphereContactGeneratorSystem.cpp b/OpenGLEngine/OpenGLEngine/SphereContactGeneratorSystem.cpp deleted file mode 100644 index 011e244..0000000 --- a/OpenGLEngine/OpenGLEngine/SphereContactGeneratorSystem.cpp +++ /dev/null @@ -1,103 +0,0 @@ -#include "SphereContactGeneratorSystem.h" -#include "ParticleContactComponent.h" - - -namespace Reality -{ - SphereContactGeneratorSystem::SphereContactGeneratorSystem() - { - requireComponent(); - requireComponent(); - requireComponent(); - } - - - void SphereContactGeneratorSystem::Update(float deltaTime) - { - if (!dummyCreated) - { - dummy = getWorld().createEntity(); - dummyCreated = true; - } - auto entities = getEntities(); - for (int i = 0; i < entities.size(); i++) - { - auto& transform1 = entities[i].getComponent(); - auto& sphere1 = entities[i].getComponent(); - auto& particle1 = entities[i].getComponent(); - bool collided = false; - // Check collisions with other spheres - if (entities.size() > 1) - { - if (i < entities.size() - 1) - { - for (int j = i + 1; j < entities.size(); j++) - { - auto& transform2 = entities[j].getComponent(); - auto& sphere2 = entities[j].getComponent(); - auto& particle2 = entities[j].getComponent(); - - if (glm::length(transform1.position - transform2.position) - < sphere1.radius + sphere2.radius) - { - float penetration = sphere1.radius + sphere2.radius - - glm::length(transform1.position - transform2.position); - ECSEntity e = getWorld().createEntity(); - Vector3 normal = glm::normalize(transform1.position - transform2.position); - - getWorld().data.renderUtil->DrawLine(transform1.position - sphere1.radius * normal, - transform1.position - sphere1.radius * normal + penetration * normal, Color(0, 0, 1)); - - e.addComponent(entities[i], - entities[j], - 1.0f, - normal, - penetration); - collided = true; - } - } - } - } - // Check collision with Hardcoded walls - if (abs(transform1.position.x) >= 14) - { - float penetration = abs(transform1.position.x) - 14; - ECSEntity e = getWorld().createEntity(); - Vector3 normal = (transform1.position.x > 0 ? -1.0f : 1.0f) * Vector3(1, 0, 0); - e.addComponent(entities[i], - dummy, - 1.0f, - normal, - penetration); - collided = true; - } - if (abs(transform1.position.y - 20) >= 14) - { - float penetration = abs(transform1.position.y - 20) - 14; - ECSEntity e = getWorld().createEntity(); - Vector3 normal = ((transform1.position.y - 20) > 0 ? -1.0f : 1.0f) * Vector3(0, 1, 0); - e.addComponent(entities[i], - dummy, - 1.0f, - normal, - penetration); - collided = true; - } - if (abs(transform1.position.z) >= 14) - { - float penetration = abs(transform1.position.z) - 14; - ECSEntity e = getWorld().createEntity(); - Vector3 normal = (transform1.position.z > 0 ? -1.0f : 1.0f) * Vector3(0, 0, 1); - e.addComponent(entities[i], - dummy, - 1.0f, - normal, - penetration); - collided = true; - } - Color col = collided ? Color(1, 0, 0, 1) : Color(0, 1, 0, 1); - getWorld().data.renderUtil->DrawSphere(transform1.position, sphere1.radius, col); - } - //getWorld().data.renderUtil->DrawCube(Vector3(0, 20, 0), Vector3(30, 30, 30)); - } -} diff --git a/OpenGLEngine/OpenGLEngine/SphereContactGeneratorSystem.h b/OpenGLEngine/OpenGLEngine/SphereContactGeneratorSystem.h deleted file mode 100644 index 06c47a7..0000000 --- a/OpenGLEngine/OpenGLEngine/SphereContactGeneratorSystem.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once -#include "ECSConfig.h" -#include "SphereComponent.h" -#include "ParticleComponent.h" -#include "TransformComponent.h" - -namespace Reality -{ - class SphereContactGeneratorSystem : public ECSSystem - { - public: - SphereContactGeneratorSystem(); - void Update(float deltaTime); - private: - bool dummyCreated = false; - ECSEntity dummy; - }; -} - diff --git a/OpenGLEngine/OpenGLEngine/TransformComponentV2.h b/OpenGLEngine/OpenGLEngine/TransformComponentV2.h deleted file mode 100644 index 27575d5..0000000 --- a/OpenGLEngine/OpenGLEngine/TransformComponentV2.h +++ /dev/null @@ -1,157 +0,0 @@ -#pragma once -#include "ECSConfig.h" -#include - -namespace Reality -{ - struct TransformComponentV2 - { - TransformComponentV2(Vector3 _position = Vector3(0, 0, 0), Vector3 _scale = Vector3(1, 1, 1), Vector3 _eulerAngles = Vector3(0.0f, 0.0f, 0.0f)) : - position(_position), scale(_scale) - { - SetRotation(_eulerAngles); - } - private: - Vector3 position; - Vector3 scale; - Quaternion orientation; - Mat4 scaleMatrix; - Mat4 rotationMatrix; - Mat4 translationMatrix; - Mat4 unScaledTransformationMatrix; - Mat4 transformationMatrix; - bool dirty = true; - - inline void UpdateMatrices() - { - scaleMatrix = glm::scale(glm::mat4(1.0f), scale); - translationMatrix = glm::translate(glm::mat4(1.0f), position); - rotationMatrix = glm::toMat4(orientation); - unScaledTransformationMatrix = translationMatrix * rotationMatrix; - transformationMatrix = unScaledTransformationMatrix * scaleMatrix; - dirty = false; - } - - public: - inline void SetPosition(const Vector3& _position) - { - position = _position; - dirty = true; - } - - inline Vector3 GetPosition() { return position; } - - inline void SetScale(const Vector3& _scale) - { - scale = _scale; - dirty = true; - } - - inline Vector3 GetScale() { return scale; } - - inline void SetOrientation(const Quaternion& _orientation) - { - orientation = _orientation; - dirty = true; - } - - inline Quaternion GetOrientation() { return orientation; } - - // Euler angles in degrees - inline void SetRotation(Vector3 eulerAngles) - { - glm::vec3 rotationInRads = glm::vec3(glm::radians(eulerAngles.x), - glm::radians(eulerAngles.y), glm::radians(eulerAngles.z)); - orientation = glm::quat(rotationInRads); - dirty = true; - } - - inline Vector3 GetRotation() { return glm::eulerAngles(orientation); } - - inline Vector3 Up() - { - return orientation * Vector3(0.0f, 1.0f, 0.0f); - } - - inline Vector3 Right() - { - return orientation * Vector3(1.0f, 0.0f, 0.0f); - } - - inline Vector3 Forward() - { - return orientation * Vector3(0.0f, 0.0f, 1.0f); - } - - inline Mat4 GetScaleMatrix() - { - if (dirty) - { - UpdateMatrices(); - } - return scaleMatrix; - } - - inline Mat4 GetRotationMatrix() - { - if (dirty) - { - UpdateMatrices(); - } - return rotationMatrix; - } - - inline Mat4 GetTranslationMatrix() - { - if (dirty) - { - UpdateMatrices(); - } - return transformationMatrix; - } - - inline Mat4 GetUnScaledTransformationMatrix() - { - if (dirty) - { - UpdateMatrices(); - } - return unScaledTransformationMatrix; - } - - inline Mat4 GetTransformationMatrix() - { - if (dirty) - { - UpdateMatrices(); - } - return transformationMatrix; - } - - inline Vector3 WorldToLocalPosition(Vector3 _position) - { - return glm::inverse(GetTransformationMatrix()) * Vector4(_position, 1); - - } - - inline Vector3 LocalToWorldPosition(Vector3 _position) - { - return GetTransformationMatrix() * Vector4(_position, 1); - } - - //TODO : Check - inline Vector3 WorldToLocalDirection(Vector3 direction) - { - if (glm::length(orientation) > 0) - { - return glm::inverse(orientation) * direction; - } - return Vector3(0, 0, 0); - } - - inline Vector3 LocalToWorldDirection(Vector3 direction) - { - return orientation * direction; - } - }; -} diff --git a/OpenGLEngine/OpenGLEngine/UpdateTransformMatricesSystem.cpp b/OpenGLEngine/OpenGLEngine/UpdateTransformMatricesSystem.cpp deleted file mode 100644 index a17a6ce..0000000 --- a/OpenGLEngine/OpenGLEngine/UpdateTransformMatricesSystem.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#include "UpdateTransformMatricesSystem.h" - -namespace Reality -{ - UpdateTransformMatricesSystem::UpdateTransformMatricesSystem() - { - requireComponent(); - } - - void UpdateTransformMatricesSystem::Update(float deltaTime) - { - for (auto e : getEntities()) - { - auto& transform = e.getComponent(); - //transform.scaleMatrix = glm::scale(glm::mat4(1.0f), transform.scale); - //transform.translationMatrix = glm::translate(glm::mat4(1.0f), transform.position); - //transform.rotationMatrix = glm::toMat4(transform.orientation); - //transform.unScaledTransformationMatrix = transform.translationMatrix * transform.rotationMatrix; - //transform.transformationMatrix = transform.unScaledTransformationMatrix * transform.scaleMatrix; - } - } -} diff --git a/OpenGLEngine/OpenGLEngine/UpdateTransformMatricesSystem.h b/OpenGLEngine/OpenGLEngine/UpdateTransformMatricesSystem.h deleted file mode 100644 index bcda6dc..0000000 --- a/OpenGLEngine/OpenGLEngine/UpdateTransformMatricesSystem.h +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once -#include "ECSConfig.h" -#include "TransformComponentV2.h" - -namespace Reality -{ - class UpdateTransformMatricesSystem : public ECSSystem - { - public: - UpdateTransformMatricesSystem(); - void Update(float deltaTime); - }; -} diff --git a/shiiiiit.txt b/shiiiiit.txt deleted file mode 100644 index e69de29..0000000 From dee7da5ce641ec3b7202f541929acf922e4b9caf Mon Sep 17 00:00:00 2001 From: Bojarzin <47534935+Bojarzin@users.noreply.github.com> Date: Fri, 31 Jan 2020 17:03:58 -0500 Subject: [PATCH 3/3] Added density control --- OpenGLEngine/OpenGLEngine/BungeeComponent.h | 4 ++-- OpenGLEngine/OpenGLEngine/BungeeSystem.cpp | 11 ++--------- OpenGLEngine/OpenGLEngine/BuoyancySystem.cpp | 14 +++++++++++++- OpenGLEngine/OpenGLEngine/Main.cpp | 13 ++++++++----- OpenGLEngine/OpenGLEngine/NBodySystem.cpp | 7 ++++++- 5 files changed, 31 insertions(+), 18 deletions(-) diff --git a/OpenGLEngine/OpenGLEngine/BungeeComponent.h b/OpenGLEngine/OpenGLEngine/BungeeComponent.h index 926683a..38bc0d4 100644 --- a/OpenGLEngine/OpenGLEngine/BungeeComponent.h +++ b/OpenGLEngine/OpenGLEngine/BungeeComponent.h @@ -5,8 +5,8 @@ namespace Reality { struct BungeeComponent { - BungeeComponent(float _springConstant = 10.0f, - float _restLength = 10.0f, + BungeeComponent(float _springConstant = 10.0f, + float _restLength = 10.0f, ECSEntity _connectedEntityA = ECSEntity(), ECSEntity _connectedEntityB = ECSEntity() ) diff --git a/OpenGLEngine/OpenGLEngine/BungeeSystem.cpp b/OpenGLEngine/OpenGLEngine/BungeeSystem.cpp index 106d629..4176745 100644 --- a/OpenGLEngine/OpenGLEngine/BungeeSystem.cpp +++ b/OpenGLEngine/OpenGLEngine/BungeeSystem.cpp @@ -49,21 +49,14 @@ namespace Reality return; } - if (length > 0) + if (length > bungee.restLength) { deltaL = length - bungee.restLength; Vector3 force = -glm::normalize(relativePosition); force *= bungee.springConstant * deltaL; - /*if (bungee.connectedEntityA.hasComponent()) - { - bungee.connectedEntityA.getComponent().AddForce(force); - }*/ - if (bungee.connectedEntityB.hasComponent()) - { - bungee.connectedEntityB.getComponent().AddForce(-force); - } + bungee.connectedEntityB.getComponent().AddForce(-force); float g = 1.0f / (1.0f + pow(abs(deltaL), 0.5f)); float r = 1 - g; diff --git a/OpenGLEngine/OpenGLEngine/BuoyancySystem.cpp b/OpenGLEngine/OpenGLEngine/BuoyancySystem.cpp index 48f6a98..92b5b74 100644 --- a/OpenGLEngine/OpenGLEngine/BuoyancySystem.cpp +++ b/OpenGLEngine/OpenGLEngine/BuoyancySystem.cpp @@ -7,7 +7,6 @@ namespace Reality BuoyancySystem::BuoyancySystem() { requireComponent(); - } void BuoyancySystem::Update(float deltaTime) @@ -21,6 +20,19 @@ namespace Reality buoyancy.waterXWidth = 50.0f; buoyancy.waterZWidth = 50.0f; + if (glfwGetKey(getWorld().data.renderUtil->window->glfwWindow, GLFW_KEY_UP)) + { + buoyancy.density += 1.0f; + } + + if (glfwGetKey(getWorld().data.renderUtil->window->glfwWindow, GLFW_KEY_DOWN)) + { + if (buoyancy.density >= 1.0f) + { + buoyancy.density -= 1.0f; + } + } + if (buoyancy.buoyEntity.hasComponent()) { auto& buoyTransform = buoyancy.buoyEntity.getComponent(); diff --git a/OpenGLEngine/OpenGLEngine/Main.cpp b/OpenGLEngine/OpenGLEngine/Main.cpp index 8afb064..55c4c5d 100644 --- a/OpenGLEngine/OpenGLEngine/Main.cpp +++ b/OpenGLEngine/OpenGLEngine/Main.cpp @@ -37,6 +37,7 @@ void MakeBuoyancy(ECSWorld& world); void MakeGravity(ECSWorld& world); bool bungeeControl = false; +float liquidDensity = 10.0f; int main() { @@ -59,9 +60,9 @@ int main() //MakeFireworks(world); //Make3Particles(world); //MakeABunchaSprings(world); - //MakeBungee(world); + MakeBungee(world); //MakeBuoyancy(world); - MakeGravity(world); + //MakeGravity(world); // Create Systems world.getSystemManager().addSystem(); @@ -136,10 +137,10 @@ int main() // Update Transform // Physics - float fixedDeltaTime = glfwGetKey(world.data.renderUtil->window->glfwWindow, GLFW_KEY_SPACE) == GLFW_PRESS ? 1 / 60.0f : 0; + // float fixedDeltaTime = glfwGetKey(world.data.renderUtil->window->glfwWindow, GLFW_KEY_SPACE) == GLFW_PRESS ? 1 / 60.0f : 0; // timeToggle = glfwGetKey(world.data.renderUtil->window->glfwWindow, GLFW_KEY_SPACE) == GLFW_PRESS ? !timeToggle : timeToggle; // float fixedDeltaTime = !timeToggle ? 1 / 60.0f : 0; - //float fixedDeltaTime = 1 / 60.0f; + float fixedDeltaTime = 1 / 60.0f; // Force Generator world.getSystemManager().getSystem().Update(fixedDeltaTime); world.getSystemManager().getSystem().Update(fixedDeltaTime); @@ -358,9 +359,11 @@ void MakeBuoyancy(ECSWorld& world) particle1.addComponent(0.01f, 0.01f); auto buoyancy = world.createEntity(); - + buoyancy.addComponent(Vector3(0, 0, 0)); buoyancy.addComponent(25.0f, 0.04f, 25.0f, particle1, 10.0f); + + buoyancy.getComponent().density = liquidDensity; } void MakeGravity(ECSWorld& world) diff --git a/OpenGLEngine/OpenGLEngine/NBodySystem.cpp b/OpenGLEngine/OpenGLEngine/NBodySystem.cpp index 5e20f9b..43bf5a9 100644 --- a/OpenGLEngine/OpenGLEngine/NBodySystem.cpp +++ b/OpenGLEngine/OpenGLEngine/NBodySystem.cpp @@ -46,6 +46,11 @@ namespace Reality } } // Draw "sun" - getWorld().data.renderUtil->DrawSphere(Vector3(0.0f), 3, Color(1, 1, 0, 1)); + for (auto e : getEntities()) + { + if (e.hasComponent()) { + getWorld().data.renderUtil->DrawSphere(centerPoint, 3, Color(1, 1, 1, 1)); + } + } } }