diff --git a/OpenGLEngine/OpenGLEngine/BungeeComponent.h b/OpenGLEngine/OpenGLEngine/BungeeComponent.h new file mode 100644 index 0000000..2be08a5 --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/BungeeComponent.h @@ -0,0 +1,19 @@ +#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..9da196a --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/BungeeSystem.cpp @@ -0,0 +1,50 @@ +#include "BungeeSystem.h" +#include "TransformComponent.h" +#include "ForceAccumulatorComponent.h" +#include "InputEventSystem.h" + +namespace Reality +{ + BungeeSystem::BungeeSystem() + { + requireComponent(); + } + + void BungeeSystem::Update(float deltaTime) + { + for (auto e : getEntities()) + { + auto& bungee = e.getComponent(); + + if (bungee.connectedEntityA.hasComponent() + && bungee.connectedEntityB.hasComponent()) + { + 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 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); + + if (DEBUG_LOG_LEVEL > 0) + { + getWorld().data.renderUtil->DrawLine(transformA.position, transformB.position, deltaL > 0 ? Color::Red : Color::Green); + } + } + } + } + } +} diff --git a/OpenGLEngine/OpenGLEngine/BungeeSystem.h b/OpenGLEngine/OpenGLEngine/BungeeSystem.h new file mode 100644 index 0000000..3cae8fa --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/BungeeSystem.h @@ -0,0 +1,14 @@ +#pragma once +#include "ECSConfig.h" +#include "TransformComponent.h" +#include "BungeeComponent.h" + +namespace Reality +{ + class BungeeSystem : public ECSSystem + { + public: + BungeeSystem(); + void Update(float deltaTime); + }; +} diff --git a/OpenGLEngine/OpenGLEngine/BuoyancyComponent.h b/OpenGLEngine/OpenGLEngine/BuoyancyComponent.h new file mode 100644 index 0000000..65f1016 --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/BuoyancyComponent.h @@ -0,0 +1,19 @@ +#pragma once +#include "ECSConfig.h" + +namespace Reality +{ + struct BuoyancyComponent + { + BuoyancyComponent(float _maxDepth = 10.0f, float _volume = 10.0f, float _waterHeight = 0.0f, float _liquidDensity = 1000.0f) + : maxDepth(_maxDepth), volume(_volume), waterHeight(_waterHeight), liquidDensity(_liquidDensity) + { + + } + + float maxDepth; + float volume; + float waterHeight; + float liquidDensity; + }; +} diff --git a/OpenGLEngine/OpenGLEngine/BuoyancySystem.cpp b/OpenGLEngine/OpenGLEngine/BuoyancySystem.cpp new file mode 100644 index 0000000..1e9e65a --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/BuoyancySystem.cpp @@ -0,0 +1,107 @@ +#include "BuoyancySystem.h" +#include "TransformComponent.h" +#include "ForceAccumulatorComponent.h" +#include "ParticleComponent.h" +#include "GravityForceComponent.h" +namespace Reality +{ + BuoyancySystem::BuoyancySystem() + { + requireComponent(); + requireComponent(); + } + + void BuoyancySystem::Update(float deltaTime) + { + if (glfwGetKey(getWorld().data.renderUtil->window->glfwWindow, GLFW_KEY_UP) == GLFW_PRESS) + { + for (auto e : getEntities()) + { + if (e.hasComponent()) + { + e.getComponent().liquidDensity += 5; + + if (e.getComponent().liquidDensity >= 10000) + { + e.getComponent().liquidDensity = 10000; + } + + currentBuoyancyValue = e.getComponent().liquidDensity; + } + } + } + + if (glfwGetKey(getWorld().data.renderUtil->window->glfwWindow, GLFW_KEY_DOWN) == GLFW_PRESS) + { + for (auto e : getEntities()) + { + if (e.hasComponent()) + { + e.getComponent().liquidDensity -= 5; + + if (e.getComponent().liquidDensity <= 10) + { + e.getComponent().liquidDensity = 10; + } + + currentBuoyancyValue = e.getComponent().liquidDensity; + } + } + } + + for (auto e : getEntities()) + { + auto& buoyancy = e.getComponent(); + auto& forceAcc = e.getComponent(); + + currentBuoyancyValue = buoyancy.liquidDensity; + + if (e.hasComponent()) + { + auto& transform = e.getComponent(); + + float depth = transform.position.y; + + if (DEBUG_LOG_LEVEL > 0) + { + getWorld().data.renderUtil->DrawCube(Vector3(0, (buoyancy.waterHeight - buoyancy.maxDepth) / 2, -50), Vector3(width * 2, buoyancy.maxDepth, length * 2) , Quaternion(Vector3(1, 1, 1), Vector3(1, 1, 1) ), Color::Blue); + } + + if (depth >= buoyancy.waterHeight || (transform.position.x >= width + 50 || transform.position.x <= -width - 50) || (transform.position.z >= length + 50 || transform.position.z <= -length - 50)) + { + + } + else + { + Vector3 force(0, 0, 0); + + if (depth <= buoyancy.waterHeight - buoyancy.maxDepth) + { + force.y = buoyancy.liquidDensity * buoyancy.volume; + cout << force.y << endl; + + forceAcc.AddForce(force); + if (DEBUG_LOG_LEVEL > 0) + { + getWorld().data.renderUtil->DrawLine(Vector3(transform.position.x, buoyancy.waterHeight, transform.position.z), transform.position, Color::Red); + } + } + else + { + float scale = depth + buoyancy.maxDepth <= 1 ? 1 : depth + buoyancy.maxDepth; + + force.y = buoyancy.liquidDensity / (scale - buoyancy.waterHeight) * buoyancy.volume; + cout << force.y << endl; + + forceAcc.AddForce(force); + + if (DEBUG_LOG_LEVEL > 0) + { + getWorld().data.renderUtil->DrawLine(Vector3(transform.position.x, buoyancy.waterHeight, transform.position.z), transform.position, Color::Green); + } + } + } + } + } + } +} diff --git a/OpenGLEngine/OpenGLEngine/BuoyancySystem.h b/OpenGLEngine/OpenGLEngine/BuoyancySystem.h new file mode 100644 index 0000000..e5fc0ae --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/BuoyancySystem.h @@ -0,0 +1,19 @@ +#pragma once +#include "ECSConfig.h" +#include "TransformComponent.h" +#include "BuoyancyComponent.h" + +namespace Reality +{ + class BuoyancySystem : public ECSSystem + { + public: + BuoyancySystem(); + void Update(float deltaTime); + + bool spawning = true; + float currentBuoyancyValue; + float width = 50; + float length = 50; + }; +} diff --git a/OpenGLEngine/OpenGLEngine/CableComponent.h b/OpenGLEngine/OpenGLEngine/CableComponent.h index 07d09c6..87972d5 100644 --- a/OpenGLEngine/OpenGLEngine/CableComponent.h +++ b/OpenGLEngine/OpenGLEngine/CableComponent.h @@ -1,11 +1,21 @@ #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){} + CableComponent(ECSEntity a = ECSEntity(), + ECSEntity b = ECSEntity(), + float _maxLength = 10, + float _restitution = 1) + : entityA(a), + entityB(b), + maxLength(_maxLength), + restitution(_restitution) + { + + } ECSEntity entityA; ECSEntity entityB; float maxLength; diff --git a/OpenGLEngine/OpenGLEngine/CableSystem.cpp b/OpenGLEngine/OpenGLEngine/CableSystem.cpp new file mode 100644 index 0000000..4aa3143 --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/CableSystem.cpp @@ -0,0 +1,49 @@ +#include "CableSystem.h" +#include "TransformComponent.h" +#include "ParticleContactEvent.h" + +namespace Reality +{ + CableSystem::CableSystem() + { + requireComponent(); + } + + void CableSystem::Update(float deltaTime) + { + for (auto e : getEntities()) + { + auto& cable = e.getComponent(); + + if (cable.entityA.hasComponent() && + cable.entityB.hasComponent()) + { + auto& transformA = cable.entityA.getComponent(); + auto& transformB = cable.entityB.getComponent(); + + Vector3 relativePos = transformA.position - transformB.position; + float length = glm::length(relativePos); + + if (length > cable.maxLength) + { + Vector3 normal = -glm::normalize(relativePos); + float penetration = length - cable.maxLength; + + getWorld().getEventManager().emitEvent( + cable.entityA, + cable.entityB, + cable.restitution, + normal, + penetration + ); + } + + getWorld().data.renderUtil->DrawLine( + transformA.position, + transformB.position, + Color::Magenta + ); + } + } + } +} diff --git a/OpenGLEngine/OpenGLEngine/CableSystem.h b/OpenGLEngine/OpenGLEngine/CableSystem.h new file mode 100644 index 0000000..a888363 --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/CableSystem.h @@ -0,0 +1,13 @@ +#pragma once +#include "ECSConfig.h" +#include "CableComponent.h" + +namespace Reality +{ + class CableSystem : public ECSSystem + { + public: + CableSystem(); + void Update(float deltaTime); + }; +} 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/DragForceSystem.h b/OpenGLEngine/OpenGLEngine/DragForceSystem.h new file mode 100644 index 0000000..d3f21e8 --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/DragForceSystem.h @@ -0,0 +1,15 @@ +#pragma once +#include "ECSConfig.h" +#include "ParticleComponent.h" +#include "ForceAccumulatorComponent.h" +#include "DragForceComponent.h" + +namespace Reality +{ + class DragForceSystem : public ECSSystem + { + public: + 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/FireworksSystem.h b/OpenGLEngine/OpenGLEngine/FireworksSystem.h new file mode 100644 index 0000000..c8bdc7f --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/FireworksSystem.h @@ -0,0 +1,14 @@ +#pragma once +#include "ECSConfig.h" +#include "TransformComponent.h" +#include "FireworksComponent.h" + +namespace Reality +{ + class FireworksSystem : public ECSSystem + { + public: + 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/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/FixedSpringSystem.h b/OpenGLEngine/OpenGLEngine/FixedSpringSystem.h new file mode 100644 index 0000000..89968b1 --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/FixedSpringSystem.h @@ -0,0 +1,14 @@ +#pragma once +#include "ECSConfig.h" +#include "TransformComponent.h" +#include "FixedSpringComponent.h" + +namespace Reality +{ + class FixedSpringSystem : public ECSSystem + { + public: + FixedSpringSystem(); + 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/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/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/Main.cpp b/OpenGLEngine/OpenGLEngine/Main.cpp index 91a5161..1c20986 100644 --- a/OpenGLEngine/OpenGLEngine/Main.cpp +++ b/OpenGLEngine/OpenGLEngine/Main.cpp @@ -1,651 +1,766 @@ -//#define STB_IMAGE_IMPLEMENTATION -#include "UpdateTransformMatricesSystem.h" -#include "RenderingSystem.h" -#include "RenderingSystemV2.h" -#include "InputEventSystem.h" -#include "RotateSystem.h" -#include "ParticleSystem.h" -#include "ParticleSpawnerSystem.h" -#include "GravityForceGeneratorSystem.h" -#include "FixedSpringForceGeneratorSystem.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 "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 - -using namespace Reality; - -void LoadShaders(ECSWorld& world); -void LoadModels(ECSWorld& world); -void MakeABunchaObjects(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); - -int main() -{ - ECSWorld world; - - // Init and Load - world.data.InitRendering(); - //LoadAssets(world); - - world.data.renderUtil->camera.Position = Vector3(0, 15.0f, 100.0f); - world.data.renderUtil->SetFOV(60); - // Create entities - - // Make a player controller - 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); - //MakeABunchaSprings(world); - //MakeACable(world); - //akeCablesAndRods(world); - //MakeFlight(world); - //TestContacts(world); - TestCollision(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(); - - // 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(); - - - float time = glfwGetTime(); - float stepTime = glfwGetTime(); - float deltaTime = 0; - float elapsedDeltaTime = 0; - float logicDelta = 0; - float debugDelta = 0; - - LoadShaders(world); - bool shadersLoaded = false; - bool modelsLoadStarted = false; - // game loop - // ----------- - while (!glfwWindowShouldClose(world.data.renderUtil->window->glfwWindow)) - { - float current = glfwGetTime(); - deltaTime = current - time; - deltaTime = 1 / 60.0f; - time = glfwGetTime(); - - world.update(); - - // Poll OpenGl events - glfwPollEvents(); - - world.data.renderUtil->ClearDisplay(world.data.renderUtil->window->glfwWindow); - - // Load - if (!shadersLoaded) - { - shadersLoaded = world.data.assetLoader->ShadersLoaded(); - } - if(shadersLoaded && !modelsLoadStarted) - { - LoadModels(world); - modelsLoadStarted = true; - } - // Update View - world.data.renderUtil->UpdateViewMatrix(); - // Process Input - world.getSystemManager().getSystem().Update(deltaTime); - - // 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); - - // 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 = 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); - 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); - - // Rendering Update - 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(); - - // Debug - if (DEBUG_LOG_LEVEL > 0) - { - world.data.renderUtil->RenderText("FPS : " + std::to_string((int)round(1.0f / deltaTime)), 1810.0f, 1060.0f, 0.5f, Color(0, 1, 1, 1)); - } - if (DEBUG_LOG_LEVEL > 1) - { - int logic = (int)round(logicDelta * 100.0f / deltaTime); - std::string logicString = logic < 10 ? " " + std::to_string(logic) : std::to_string(logic); - int render = (int)round(world.data.renderUtil->GetRenderDelta() * 100.0f / deltaTime); - std::string renderString = logic < 10 ? " " + std::to_string(render) : std::to_string(render); - int debug = (int)round(debugDelta * 100.0f / deltaTime); - std::string debugString = logic < 10 ? " " + std::to_string(debug) : std::to_string(debug); - - world.data.renderUtil->RenderText("Logic : " + logicString + "%" + - //+ " | Physics : " + std::to_string((int)round(physicsDelta * 100.0f / deltaTime)) + "%" + - + " | Rendering : " + renderString + "%" + - + " | Debug : " + debugString + "%" - , 1680.0f, 1040.0f, 0.25f, Color(0, 1, 1, 1)); - } - if (DEBUG_LOG_LEVEL > 2) - { - world.data.renderUtil->RenderText("Draw Calls : " + std::to_string(world.data.renderUtil->GetDrawCalls()) - + " | Verts : " + std::to_string(world.data.renderUtil->GetVerts()) - + " | Tris : " + std::to_string(world.data.renderUtil->GetTris()) - + " | Lines : " + std::to_string(world.data.renderUtil->GetLines()) - , 1610.0f, 1020.0f, 0.25f, Color(0, 1, 1, 1)); - } - - // Update debug delta - debugDelta = glfwGetTime() - stepTime; - stepTime = glfwGetTime(); - - world.data.renderUtil->SwapBuffers(world.data.renderUtil->window->glfwWindow); - - // Show FPS in console - //std::cout << "FPS : " << 1.0f / deltaTime << std::endl; - } - - // glfw: terminate, clearing all previously allocated GLFW resources. - // ------------------------------------------------------------------ - glfwTerminate(); - return 0; -} - -void LoadShaders(ECSWorld& world) -{ - world.data.assetLoader->StartShaderLoading({ {"Shaders/Lighting_Maps.vs", "Shaders/Lighting_Maps.fs"} }); -} -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/supermarine-spitfire/spitfire.fbx", - {{"spitfire_d.png"}}) - }); -} - -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(); - // Add mesh - e.addComponent("Resources/Models/nanosuit/nanosuit.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 pairedSpring4 = world.createEntity(); - pairedSpring4.addComponent(100, 10.0f, e3, e4); -} - -void MakeABunchaSpheres(ECSWorld& world) -{ - for (int i = 0; i < 30; 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 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) -{ - 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); -} - -void MakeCablesAndRods(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); -} - -void MakeFlight(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++) - { - 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)); - } -} - -void TestContacts(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); - }*/ -} - -void TestCollision(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++) - { - 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)); - } -} - -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); - - // 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) }; - for (int i = 1; i < 3; i++) - { - for (int j = 0; j < 4; j++) - { - 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); - } - } +//#define STB_IMAGE_IMPLEMENTATION +#include "RenderingSystem.h" +#include "RenderingSystemV2.h" +#include "InputEventSystem.h" +#include "FPSControlSystem.h" +#include "RotateSystem.h" +#include "RotateSystemV2.h" +#include "FireworksSystem.h" +#include "GravityForceSystem.h" +#include "DragForceSystem.h" +#include "FixedSpringSystem.h" +#include "PairedSpringSystem.h" +#include "ParticleSphereSystem.h" +#include "CableSystem.h" +#include "RodSystem.h" +#include "PlaneSystem.h" +#include "ParticleContactResolutionSystem.h" +#include "ResetPenetrationDeltaMoveSystem.h" +#include "ForceAccumulatorSystem.h" +#include "ParticleSystem.h" +#include "DynamicDirectionalLightSystem.h" +#include "DynamicPointLightSystem.h" +#include "DynamicSpotLightSystem.h" +#include +#include +#include + +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 MakeABunchaCablesAndRods(ECSWorld& world); +void MakeARopeBridge(ECSWorld& world); +void MakeABunchaObjectsV2(ECSWorld& world); + +int main() +{ + ECSWorld world; + + // Init and Load + world.data.InitRendering(); + //LoadAssets(world); + + world.data.renderUtil->camera.Position = Vector3(0, 0.0f, 50.0f); + world.data.renderUtil->SetFOV(60); + // Create entities + + // Make a player controller + auto e = world.createEntity(); + e.addComponent(); + + SetupLights(world); + //MakeABunchaObjects(world); + //MakeFireworks(world); + //Make3Particles(world); + //MakeABunchaSprings(world); + //MakeABunchaSpheres(world); + //MakeABunchaCablesAndRods(world); + MakeARopeBridge(world); + //MakeABunchaObjectsV2(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(); + + float time = glfwGetTime(); + float stepTime = glfwGetTime(); + float deltaTime = 0; + float elapsedDeltaTime = 0; + float logicDelta = 0; + float debugDelta = 0; + + LoadShaders(world); + bool shadersLoaded = false; + bool modelsLoadStarted = false; + // game loop + // ----------- + while (!glfwWindowShouldClose(world.data.renderUtil->window->glfwWindow)) + { + float current = glfwGetTime(); + deltaTime = current - time; + deltaTime = 1 / 60.0f; + time = glfwGetTime(); + + world.update(); + + // Poll OpenGl events + glfwPollEvents(); + + world.data.renderUtil->ClearDisplay(world.data.renderUtil->window->glfwWindow); + + // Load + if (!shadersLoaded) + { + shadersLoaded = world.data.assetLoader->ShadersLoaded(); + } + if(shadersLoaded && !modelsLoadStarted) + { + LoadModels(world); + modelsLoadStarted = true; + } + // Update View + world.data.renderUtil->UpdateViewMatrix(); + // Process Input + world.getSystemManager().getSystem().Update(deltaTime); + + // Game Logic Update + 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 + + // Physics + //float fixedDeltaTime = glfwGetKey(world.data.renderUtil->window->glfwWindow, GLFW_KEY_SPACE) == GLFW_PRESS ? 1 / 60.0f : 0; + float fixedDeltaTime = 1 / 60.0f; + // Force Generator + 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); + + // Contact Resolution + 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(); + + // Debug + if (DEBUG_LOG_LEVEL > 0) + { + world.data.renderUtil->RenderText("FPS : " + std::to_string((int)round(1.0f / deltaTime)), 1810.0f, 1060.0f, 0.5f, Color(0, 1, 1, 1)); + } + if (DEBUG_LOG_LEVEL > 1) + { + int logic = (int)round(logicDelta * 100.0f / deltaTime); + std::string logicString = logic < 10 ? " " + std::to_string(logic) : std::to_string(logic); + int render = (int)round(world.data.renderUtil->GetRenderDelta() * 100.0f / deltaTime); + std::string renderString = logic < 10 ? " " + std::to_string(render) : std::to_string(render); + int debug = (int)round(debugDelta * 100.0f / deltaTime); + std::string debugString = logic < 10 ? " " + std::to_string(debug) : std::to_string(debug); + + world.data.renderUtil->RenderText("Logic : " + logicString + "%" + + //+ " | Physics : " + std::to_string((int)round(physicsDelta * 100.0f / deltaTime)) + "%" + + + " | Rendering : " + renderString + "%" + + + " | Debug : " + debugString + "%" + , 1680.0f, 1040.0f, 0.25f, Color(0, 1, 1, 1)); + } + if (DEBUG_LOG_LEVEL > 2) + { + world.data.renderUtil->RenderText("Draw Calls : " + std::to_string(world.data.renderUtil->GetDrawCalls()) + + " | Verts : " + std::to_string(world.data.renderUtil->GetVerts()) + + " | Tris : " + std::to_string(world.data.renderUtil->GetTris()) + + " | Lines : " + std::to_string(world.data.renderUtil->GetLines()) + , 1610.0f, 1020.0f, 0.25f, Color(0, 1, 1, 1)); + } + + // Update debug delta + debugDelta = glfwGetTime() - stepTime; + stepTime = glfwGetTime(); + + world.data.renderUtil->SwapBuffers(world.data.renderUtil->window->glfwWindow); + + // Show FPS in console + //std::cout << "FPS : " << 1.0f / deltaTime << std::endl; + } + + // glfw: terminate, clearing all previously allocated GLFW resources. + // ------------------------------------------------------------------ + glfwTerminate(); + return 0; +} + +void LoadShaders(ECSWorld& world) +{ + world.data.assetLoader->StartShaderLoading({ {"Shaders/Lighting_Maps.vs", "Shaders/Lighting_Maps.fs"} }); +} +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/supermarine-spitfire/spitfire.fbx", + {{"spitfire_d.png"}}) + }); +} + +void MakeABunchaObjects(ECSWorld& world) +{ + 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 + castle.addComponent("Resources/Models/Sponza-master/sponza.obj"); + + //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(); + +} + +void MakeFireworks(ECSWorld & world) +{ + for (int i = 0; i < 3; i++) + { + 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)); + } + +} + +void Make3Particles(ECSWorld & world) +{ + 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 MakeABunchaSprings(ECSWorld & world) +{ + 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 MakeABunchaSpheres(ECSWorld & world) +{ + for (int i = 0; i < 40; i++) + { + auto sphere = world.createEntity(); + sphere.addComponent(Vector3(RANDOM_FLOAT(-10, 10), RANDOM_FLOAT(-10, 10), RANDOM_FLOAT(-10, 10))); + sphere.addComponent(Vector3(RANDOM_FLOAT(-40, 40), RANDOM_FLOAT(-40, 40), RANDOM_FLOAT(-40, 40))); + sphere.addComponent(1.0f); + sphere.addComponent(); + sphere.addComponent(RANDOM_FLOAT(1, 3)); + } +} + +void CreateParticleArchetype(ECSEntity e) +{ + e.addComponent(); + e.addComponent(); + e.addComponent(); + //e.addComponent(); + e.addComponent(); +} + +void MakeARopeBridge(ECSWorld & world) +{ + auto ePivot1 = world.createEntity(); + ePivot1.addComponent(Vector3(3, 10, 5)); + + auto e1 = world.createEntity(); + e1.addComponent(Vector3(0, 3, 5)); + CreateParticleArchetype(e1); + + auto ePivot2 = world.createEntity(); + ePivot2.addComponent(Vector3(3, 10, -5)); + + auto e2 = world.createEntity(); + e2.addComponent(Vector3(0, 3, -5)); + CreateParticleArchetype(e2); + + auto rod1 = world.createEntity(); + rod1.addComponent(e1, e2, 10); + + auto cable1 = world.createEntity(); + cable1.addComponent(ePivot1, e1, 18, 0.5); + + auto cable2 = world.createEntity(); + cable2.addComponent(ePivot2, e2, 18, 0.5); + + // 2 + auto ePivot3 = world.createEntity(); + ePivot3.addComponent(Vector3(3 + 10, 10, 5)); + + auto e3 = world.createEntity(); + e3.addComponent(Vector3(0 + 10, 0, 5)); + CreateParticleArchetype(e3); + + auto ePivot4 = world.createEntity(); + ePivot4.addComponent(Vector3(3 + 10, 10, -5)); + + auto e4 = world.createEntity(); + e4.addComponent(Vector3(0 + 10, 0, -5)); + CreateParticleArchetype(e4); + + auto rod2 = world.createEntity(); + rod2.addComponent(e3, e4, 10); + + auto cable3 = world.createEntity(); + cable3.addComponent(ePivot3, e3, 15, 0.5); + + auto cable4 = world.createEntity(); + cable4.addComponent(ePivot4, e4, 15, 0.5); + + auto sphere1 = world.createEntity(); + sphere1.addComponent(Vector3(5, 10, -4)); + sphere1.addComponent(Vector3(0, 0, 0)); + sphere1.addComponent(1.0f); + sphere1.addComponent(); + sphere1.addComponent(1.0f); + + auto plane1 = world.createEntity(); + plane1.addComponent(e1, e2, e3, e4); + + // 3 + auto ePivot5 = world.createEntity(); + ePivot5.addComponent(Vector3(3 - 10, 10, 5)); + + auto e5 = world.createEntity(); + e5.addComponent(Vector3(0 - 10, -3, 5)); + CreateParticleArchetype(e5); + + auto ePivot6 = world.createEntity(); + ePivot6.addComponent(Vector3(3 - 10, 10, -5)); + + auto e6 = world.createEntity(); + e6.addComponent(Vector3(0 - 10, -3, -5)); + CreateParticleArchetype(e6); + + auto rod3 = world.createEntity(); + rod3.addComponent(e5, e6, 10); + + auto cable5 = world.createEntity(); + cable5.addComponent(ePivot5, e5, 21, 0.5); + + auto cable6 = world.createEntity(); + cable6.addComponent(ePivot6, e6, 21, 0.5); + + auto plane2 = world.createEntity(); + plane2.addComponent(e5, e6, e1, e2); + + // + auto ePivot7 = world.createEntity(); + ePivot7.addComponent(Vector3(3 - 20, 10, 5)); + + auto e7 = world.createEntity(); + e7.addComponent(Vector3(0 - 20, -5, 5)); + CreateParticleArchetype(e7); + + auto ePivot8 = world.createEntity(); + ePivot8.addComponent(Vector3(3 - 20, 10, -5)); + + auto e8 = world.createEntity(); + e8.addComponent(Vector3(0 - 20, -5, -5)); + CreateParticleArchetype(e8); + + auto rod4 = world.createEntity(); + rod4.addComponent(e7, e8, 10); + + auto cable7 = world.createEntity(); + cable7.addComponent(ePivot7, e7, 23, 0.5); + + auto cable8 = world.createEntity(); + cable8.addComponent(ePivot8, e8, 23, 0.5); + + auto plane3 = world.createEntity(); + plane3.addComponent(e7, e8, e5, e6); + + // + + // + auto ePivot9 = world.createEntity(); + ePivot9.addComponent(Vector3(3 - 30, 10, 5)); + + auto e9 = world.createEntity(); + e9.addComponent(Vector3(0 - 30, -3, 5)); + CreateParticleArchetype(e9); + + auto ePivot10 = world.createEntity(); + ePivot10.addComponent(Vector3(3 - 30, 10, -5)); + + auto e10 = world.createEntity(); + e10.addComponent(Vector3(0 - 30, -3, -5)); + CreateParticleArchetype(e10); + + auto rod5 = world.createEntity(); + rod5.addComponent(e9, e10, 10); + + auto cable9 = world.createEntity(); + cable9.addComponent(ePivot9, e9, 21, 0.5); + + auto cable10 = world.createEntity(); + cable10.addComponent(ePivot10, e10, 21, 0.5); + + auto plane4 = world.createEntity(); + plane4.addComponent(e9, e10, e7, e8); + + // + + // + auto ePivot11 = world.createEntity(); + ePivot11.addComponent(Vector3(3 - 40, 10, 5)); + + auto e11 = world.createEntity(); + e11.addComponent(Vector3(0 - 40, 0, 5)); + CreateParticleArchetype(e11); + + auto ePivot12 = world.createEntity(); + ePivot12.addComponent(Vector3(3 - 40, 10, -5)); + + auto e12 = world.createEntity(); + e12.addComponent(Vector3(0 - 40, 0, -5)); + CreateParticleArchetype(e12); + + auto rod6 = world.createEntity(); + rod5.addComponent(e11, e12, 10); + + auto cable11 = world.createEntity(); + cable11.addComponent(ePivot11, e11, 18, 0.5); + + auto cable12 = world.createEntity(); + cable12.addComponent(ePivot12, e12, 18, 0.5); + + auto plane5 = world.createEntity(); + plane5.addComponent(e11, e12, e9, e10); + + // + + // + auto ePivot13 = world.createEntity(); + ePivot13.addComponent(Vector3(3 - 50, 10, 5)); + + auto e13 = world.createEntity(); + e13.addComponent(Vector3(0 - 50, 3, 5)); + CreateParticleArchetype(e13); + + auto ePivot14 = world.createEntity(); + ePivot14.addComponent(Vector3(3 - 50, 10, -5)); + + auto e14 = world.createEntity(); + e14.addComponent(Vector3(0 - 50, 3, -5)); + CreateParticleArchetype(e14); + + auto rod7 = world.createEntity(); + rod5.addComponent(e13, e14, 10); + + auto cable13 = world.createEntity(); + cable13.addComponent(ePivot13, e13, 15, 0.5); + + auto cable14 = world.createEntity(); + cable14.addComponent(ePivot14, e14, 15, 0.5); + + auto plane6 = world.createEntity(); + plane6.addComponent(e13, e14, e11, e12); + + // + + // rods + auto rod8 = world.createEntity(); + rod8.addComponent(e1, e3, 10); + auto rod9 = world.createEntity(); + rod9.addComponent(e2, e4, 10); + auto rod10 = world.createEntity(); + rod10.addComponent(e5, e1, 10); + auto rod11 = world.createEntity(); + rod11.addComponent(e6, e2, 10); + + auto rod12 = world.createEntity(); + rod12.addComponent(e7, e5, 10); + auto rod13 = world.createEntity(); + rod13.addComponent(e8, e6, 10); + auto rod14 = world.createEntity(); + rod14.addComponent(e9, e7, 10); + auto rod15 = world.createEntity(); + rod15.addComponent(e10, e8, 10); + + auto rod16 = world.createEntity(); + rod16.addComponent(e11, e9, 10); + auto rod17 = world.createEntity(); + rod17.addComponent(e12, e10, 10); + auto rod18 = world.createEntity(); + rod18.addComponent(e13, e11, 10); + auto rod19 = world.createEntity(); + rod19.addComponent(e14, e12, 10); + + // diagonal rods + auto rod20 = world.createEntity(); + rod20.addComponent(e1, e4, 10 * pow(2.0f, 0.5f)); + auto rod21 = world.createEntity(); + rod21.addComponent(e2, e3, 10 * pow(2.0f, 0.5f)); + auto rod22 = world.createEntity(); + rod22.addComponent(e6, e1, 10 * pow(2.0f, 0.5f)); + auto rod23 = world.createEntity(); + rod23.addComponent(e5, e2, 10 * pow(2.0f, 0.5f)); + + auto rod24 = world.createEntity(); + rod24.addComponent(e5, e8, 10 * pow(2.0f, 0.5f)); + auto rod25 = world.createEntity(); + rod25.addComponent(e6, e7, 10 * pow(2.0f, 0.5f)); + auto rod26 = world.createEntity(); + rod26.addComponent(e7, e10, 10 * pow(2.0f, 0.5f)); + auto rod27 = world.createEntity(); + rod27.addComponent(e8, e9, 10 * pow(2.0f, 0.5f)); + + auto rod28 = world.createEntity(); + rod28.addComponent(e9, e12, 10 * pow(2.0f, 0.5f)); + auto rod29 = world.createEntity(); + rod29.addComponent(e10, e11, 10 * pow(2.0f, 0.5f)); + auto rod30 = world.createEntity(); + rod30.addComponent(e11, e14, 10 * pow(2.0f, 0.5f)); + auto rod31 = world.createEntity(); + rod31.addComponent(e12, e13, 10 * pow(2.0f, 0.5f)); +} + +void MakeABunchaObjectsV2(ECSWorld & world) +{ + //auto flightV1 = world.createEntity(); + //flightV1.addComponent(Vector3(0, 30, -50), Vector3(0.1f, 0.1f, 0.1f), Vector3(270, 0, 0)); + //// Add mesh + //flightV1.addComponent("Resources/Models/supermarine-spitfire/spitfire.fbx"); + //flightV1.addComponent(Vector3(0, 90, 0)); + + auto flightV2 = world.createEntity(); + flightV2.addComponent(Vector3(0, 30, -50), Vector3(0.1f, 0.1f, 0.1f), Vector3(270, 0, 0)); + // Add mesh + flightV2.addComponent("Resources/Models/supermarine-spitfire/spitfire.fbx"); + flightV2.addComponent(Vector3(0, 45, 0)); + +} + +void MakeABunchaCablesAndRods(ECSWorld & world) +{ + auto ePivot = world.createEntity(); + ePivot.addComponent(Vector3(3, 10, 0)); + + auto e1 = world.createEntity(); + e1.addComponent(Vector3(0, 10, 0)); + e1.addComponent(); + e1.addComponent(); + e1.addComponent(); + e1.addComponent(); + e1.addComponent(); + + auto e2 = world.createEntity(); + e2.addComponent(Vector3(5, 5, 0)); + e2.addComponent(); + e2.addComponent(); + e2.addComponent(); + e2.addComponent(); + e2.addComponent(); + + auto e3 = world.createEntity(); + e3.addComponent(Vector3(0, 0, 0)); + e3.addComponent(); + e3.addComponent(); + e3.addComponent(); + e3.addComponent(); + e3.addComponent(); + + auto e4 = world.createEntity(); + e4.addComponent(Vector3(-5, 5, 0)); + e4.addComponent(); + e4.addComponent(); + e4.addComponent(); + e4.addComponent(); + e4.addComponent(); + + auto cable1 = world.createEntity(); + //cable.addComponent(ePivot, e1, 5, 1); + cable1.addComponent(50, 2, ePivot, e1); + + auto cable2 = world.createEntity(); + //cable.addComponent(ePivot, e1, 5, 1); + cable2.addComponent(50, 25, ePivot, e2); + + auto cable3 = world.createEntity(); + cable3.addComponent(ePivot, e3, 15, 1); + //cable3.addComponent(50, 20, ePivot, e3); + + auto rod1 = world.createEntity(); + rod1.addComponent(e1, e2, 5 * pow(2, 0.5f)); + + auto rod2 = world.createEntity(); + rod2.addComponent(e2, e3, 5 * pow(2, 0.5f)); + + auto rod3 = world.createEntity(); + rod3.addComponent(e3, e4, 5 * pow(2, 0.5f)); + + auto rod4 = world.createEntity(); + rod4.addComponent(e4, e1, 5 * pow(2, 0.5f)); + + auto rod5 = world.createEntity(); + rod5.addComponent(e1, e3, 10); + + auto rod6 = world.createEntity(); + rod6.addComponent(e2, e4, 10); + + //for (int i = 0; i < 20; i++) + //{ + // auto e1 = world.createEntity(); + // e1.addComponent(Vector3(RANDOM_FLOAT(-5, 5), RANDOM_FLOAT(-5, 5), RANDOM_FLOAT(-5, 5))); + // e1.addComponent(); + // e1.addComponent(); + // e1.addComponent(); + // e1.addComponent(RANDOM_FLOAT(0.5, 1.5)); + // e1.addComponent(); + + // auto e2 = world.createEntity(); + // e2.addComponent(Vector3(RANDOM_FLOAT(-5, 5), RANDOM_FLOAT(-5, 5), RANDOM_FLOAT(-5, 5))); + // e2.addComponent(); + // e2.addComponent(); + // e2.addComponent(); + // e2.addComponent(RANDOM_FLOAT(0.5, 1.5)); + // e2.addComponent(); + + // auto rod = world.createEntity(); + // rod.addComponent(e1, e2, RANDOM_FLOAT(6, 10)); + //} +} + +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.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)); + auto hook = world.createEntity(); + hook.addComponent(Vector3(23, 15, 48.0f)); + hook = world.createEntity(); + hook.addComponent(Vector3(22, 13.5f, 50.5f)); + hook = world.createEntity(); + hook.addComponent(Vector3(21, 12.5f, 47.5f)); + + 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)); + hook = world.createEntity(); + hook.addComponent(Vector3(-14.5f + 1, 14 - 1, 49.0f - 1)); + hook = world.createEntity(); + hook.addComponent(Vector3(-14.5f - 0.5f, 14 + 1, 49.0f)); + hook = world.createEntity(); + hook.addComponent(Vector3(-14.5f, 14 - 1, 49.0f + 1)); + + 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)); + hook = world.createEntity(); + hook.addComponent(Vector3(22 - 1, 14 - 1, -62.0f)); + hook = world.createEntity(); + hook.addComponent(Vector3(22, 14 + 0.5f, -62.0f - 1)); + hook = world.createEntity(); + hook.addComponent(Vector3(22 + 1, 14, -62.0f + 0.5f)); + + 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)); + hook = world.createEntity(); + hook.addComponent(Vector3(-14.5f - 1, 14, -61.5f -1)); + hook = world.createEntity(); + hook.addComponent(Vector3(-14.5f - 0.25f, 14 - 0.5f, -61.5f + 1)); + hook = world.createEntity(); + hook.addComponent(Vector3(-14.5f + 0.5f, 14+ 1, -61.5f + 1)); + + // Spears + std::vector cols = { Color(1,0,0), Color(0,1,0), Color(0,0,1), Color(0.7f,0.55f,0) }; + for (int i = 1; i < 3; i++) + { + for (int j = 0; j < 4; j++) + { + 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); + } + } } \ No newline at end of file diff --git a/OpenGLEngine/OpenGLEngine/NBodyComponent.h b/OpenGLEngine/OpenGLEngine/NBodyComponent.h new file mode 100644 index 0000000..600f829 --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/NBodyComponent.h @@ -0,0 +1,16 @@ +#pragma once +#include "ECSConfig.h" + +namespace Reality +{ + struct NBodyComponent + { + NBodyComponent(float _mass = 1.0f) + : mass(_mass) + { + + } + + float mass; + }; +} diff --git a/OpenGLEngine/OpenGLEngine/NBodySystem.cpp b/OpenGLEngine/OpenGLEngine/NBodySystem.cpp new file mode 100644 index 0000000..4622bd9 --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/NBodySystem.cpp @@ -0,0 +1,93 @@ +#include "NBodySystem.h" +#include "TransformComponent.h" +#include "ForceAccumulatorComponent.h" + +namespace Reality +{ + NBodySystem::NBodySystem() + { + requireComponent(); + } + + void NBodySystem::Update(float deltaTime) + { + for (auto e1 : getEntities()) + { + if (e1.hasComponent()) + { + auto& transform1 = e1.getComponent(); + auto& nBody1 = e1.getComponent(); + + for (auto e2 : getEntities()) + { + if (e1 != e2) + { + if (e2.hasComponent()) + { + auto& transform2 = e2.getComponent(); + auto& nBody2 = e2.getComponent(); + + Vector3 relativePosition = transform1.position - transform2.position; + + float length = glm::length(relativePosition); + + float forceN; + + forceN = nGravity * ((nBody1.mass * nBody2.mass) / length); + + + if (e2.hasComponent()) + { + e2.getComponent().AddForce(relativePosition * forceN); + } + + if (DEBUG_LOG_LEVEL > 0) + { + getWorld().data.renderUtil->DrawSphere(transform1.position, nBody1.mass > 100 ? 5 : nBody1.mass / 5, Color()); + } + } + } + } + } + } + //if (getEntities().size() > 1) + //{ + // for (int i = 0; i < getEntities().size() - 1; i++) + // { + // if (getEntities()[i].hasComponent()) + // { + // auto& transform1 = getEntities()[i].getComponent(); + // auto& nBody1 = getEntities()[i].getComponent(); + // + // for (int j = i + 1; j < getEntities().size(); j++) + // { + // if (getEntities()[j].hasComponent()) + // { + // auto& transform2 = getEntities()[j].getComponent(); + // auto& nBody2 = getEntities()[j].getComponent(); + // + // Vector3 relativePosition = transform1.position - transform2.position; + // + // float length = glm::length(relativePosition); + // + // float forceN; + // + // forceN = nGravity * ((nBody1.mass * nBody2.mass) / length); + // + // if (getEntities()[j].hasComponent()) + // { + // getEntities()[j].getComponent().AddForce(relativePosition * forceN); + // } + // + // if (DEBUG_LOG_LEVEL > 0) + // { + // getWorld().data.renderUtil->DrawSphere(transform1.position, nBody1.mass > 100 ? 5 : nBody1.mass / 20, Color::Red); + // } + // } + // + // } + // } + // } + //} + } +} diff --git a/OpenGLEngine/OpenGLEngine/NBodySystem.h b/OpenGLEngine/OpenGLEngine/NBodySystem.h new file mode 100644 index 0000000..e7c74ee --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/NBodySystem.h @@ -0,0 +1,14 @@ +#pragma once +#include "ECSConfig.h" +#include "NBodyComponent.h" + +namespace Reality +{ + class NBodySystem : public ECSSystem + { + public: + NBodySystem(); + void Update(float deltaTime); + float nGravity = 1.0f; + }; +} diff --git a/OpenGLEngine/OpenGLEngine/OpenGLEngine.vcxproj b/OpenGLEngine/OpenGLEngine/OpenGLEngine.vcxproj index dbb8b45..b20c8c8 100644 --- a/OpenGLEngine/OpenGLEngine/OpenGLEngine.vcxproj +++ b/OpenGLEngine/OpenGLEngine/OpenGLEngine.vcxproj @@ -1,480 +1,414 @@ - - - - - Debug - Win32 - - - Release - Win32 - - - Debug - x64 - - - Release - x64 - - - - 15.0 - {BF971B9A-9ACC-4B50-8E62-4CC32E780AA5} - OpenGLEngine - 10.0.17763.0 - - - - Application - true - v141 - MultiByte - - - Application - false - v141 - true - MultiByte - - - Application - true - v141 - MultiByte - - - Application - false - v141 - true - MultiByte - - - - - - - - - - - - - - - - - - - - - $(SolutionDir)\Include;$(IncludePath) - $(SolutionDir)Lib;$(LibraryPath) - - - - Level3 - Disabled - true - true - - - glfw3.lib;opengl32.lib;assimp-vc141-mtd.lib;freetype.lib;reactphysics3d.lib;%(AdditionalDependencies) - - - - - Level3 - Disabled - true - true - - - - - Level3 - MaxSpeed - true - true - true - true - - - true - true - - - - - Level3 - MaxSpeed - true - true - true - true - - - true - true - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 15.0 + {BF971B9A-9ACC-4B50-8E62-4CC32E780AA5} + OpenGLEngine + 10.0.17763.0 + + + + Application + true + v141 + MultiByte + + + Application + false + v141 + true + MultiByte + + + Application + true + v141 + MultiByte + + + Application + false + v141 + true + MultiByte + + + + + + + + + + + + + + + + + + + + + $(SolutionDir)\Include;$(IncludePath) + $(SolutionDir)Lib;$(LibraryPath) + + + + Level3 + Disabled + true + true + + + glfw3.lib;opengl32.lib;assimp-vc141-mtd.lib;freetype.lib;reactphysics3d.lib;%(AdditionalDependencies) + + + + + Level3 + Disabled + true + true + + + + + Level3 + MaxSpeed + true + true + true + true + + + true + true + + + + + Level3 + MaxSpeed + true + true + true + true + + + true + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/OpenGLEngine/OpenGLEngine/OpenGLEngine.vcxproj.filters b/OpenGLEngine/OpenGLEngine/OpenGLEngine.vcxproj.filters index 0199877..1eccea6 100644 --- a/OpenGLEngine/OpenGLEngine/OpenGLEngine.vcxproj.filters +++ b/OpenGLEngine/OpenGLEngine/OpenGLEngine.vcxproj.filters @@ -1,518 +1,422 @@ - - - - - {4FC737F1-C7A5-4376-A066-2A32D752A2FF} - cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx - - - {93995380-89BD-4b04-88EB-625FBE52EBFB} - h;hh;hpp;hxx;hm;inl;inc;ipp;xsd - - - {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} - rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms - - - {f5f91895-769b-4e5d-ba26-2e5ae9b27d4b} - - - {4908f04b-fde2-4c3c-aee0-85a0d4fe4985} - - - {3e028a8c-baa4-4fc3-bd18-fe7b14239d28} - - - {db276a7f-edce-43c9-ae12-65e1b140881e} - - - {db2edee8-1859-4e65-b307-c403f18f9748} - - - {f8a9d179-76b0-4e0e-a76c-a1a25c3113a1} - - - {e52077c3-76ae-4886-a342-fbe29e147594} - - - {f1bb8739-5560-4299-ba72-675bd37e40e5} - - - {95539126-9b92-4bc9-a37c-8c78d512b869} - - - {09a9940b-c17f-43c0-becf-d8da8efa3d30} - - - {1a9d715c-69e4-4d0c-b881-159070a97fa6} - - - {3f5a8043-c367-47f5-bbe1-005323d21b00} - - - - - Source Files - - - Source Files - - - Mix - - - Mix - - - Mix - - - Mix - - - Mix - - - Rendering - - - Rendering - - - ECSCore - - - TestCS - - - Physics\Particles - - - Physics\Particles - - - Physics\Particles - - - Physics\Particles - - - Input - - - TestCS - - - Rendering - - - Rendering - - - Rendering - - - Rendering - - - Rendering - - - Rendering - - - Rendering - - - Rendering - - - TestCS - - - TestCS - - - TestCS - - - Rendering - - - ComponentsCore - - - Rendering - - - TestCS\FlightSim - - - TestCS\FlightSim - - - TestCS\FlightSim - - - TestCS\FlightSim - - - TestCS - - - TestCS - - - TestCS\FlightSim - - - TestCS\FlightSim - - - TestCS\FlightSim - - - Rendering - - - Physics\Rigidbody - - - Physics\Rigidbody - - - Physics\Particles - - - Physics\Rigidbody - - - Physics\Rigidbody - - - Physics\Rigidbody - - - Physics\Particles - - - Physics\Particles - - - Physics\Rigidbody - - - TestCS - - - Physics\Rigidbody - - - - - Source Files - - - Header Files - - - ECSCore - - - Rendering - - - ComponentsCore - - - ECSCore - - - Mix - - - Mix - - - Mix - - - Mix - - - Mix - - - Mix - - - Mix - - - Rendering - - - Rendering - - - Rendering - - - Rendering - - - Rendering - - - Rendering - - - Rendering - - - ECSCore - - - TestCS - - - TestCS - - - Physics\Particles - - - Physics\Particles - - - Physics\Particles - - - Physics\Particles - - - Physics\Particles - - - Physics\Particles - - - Input - - - Input - - - TestCS - - - TestCS - - - Rendering - - - Rendering - - - Rendering - - - Rendering - - - Rendering - - - Rendering - - - Rendering - - - 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 - - - TestCS\FlightSim - - - TestCS\FlightSim - - - TestCS\FlightSim - - - TestCS\FlightSim - - - Rendering - - - Physics\Rigidbody - - - Physics\Rigidbody - - - Physics\Rigidbody - - - Physics\Rigidbody - - - Physics\Particles - - - Physics\Particles - - - Physics\Rigidbody - - - Physics\Rigidbody - - - Physics\Rigidbody - - - Physics\Rigidbody - - - Physics\Rigidbody - - - Physics\Rigidbody - - - Physics\Particles - - - Physics\Particles - - - Physics\Particles - - - Physics\Particles - - - Physics\Rigidbody - - - Physics\Rigidbody - - - TestCS - - - TestCS - - - Physics\Rigidbody - - - - - Shaders - - - Shaders - - - Shaders - - - Shaders - - - Shaders - - - Shaders - - - Shaders - - - Shaders - - - Shaders - - - Shaders - - + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + {f5f91895-769b-4e5d-ba26-2e5ae9b27d4b} + + + {4908f04b-fde2-4c3c-aee0-85a0d4fe4985} + + + {3e028a8c-baa4-4fc3-bd18-fe7b14239d28} + + + {db276a7f-edce-43c9-ae12-65e1b140881e} + + + {db2edee8-1859-4e65-b307-c403f18f9748} + + + {f8a9d179-76b0-4e0e-a76c-a1a25c3113a1} + + + {e52077c3-76ae-4886-a342-fbe29e147594} + + + {f1bb8739-5560-4299-ba72-675bd37e40e5} + + + {95539126-9b92-4bc9-a37c-8c78d512b869} + + + {09a9940b-c17f-43c0-becf-d8da8efa3d30} + + + {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} + + + {8f440b87-7f9c-4d7f-993c-ec5fa08dde39} + + + {8d7d692e-fedf-4e75-9661-f2eb1dd346c1} + + + + + Source Files + + + Source Files + + + Mix + + + Mix + + + Mix + + + Mix + + + Mix + + + Rendering + + + Rendering + + + ECSCore + + + Input + + + Rendering + + + Rendering + + + Rendering + + + Rendering + + + Rendering + + + Rendering + + + Rendering + + + Rendering + + + Rendering + + + Rendering + + + TestCS + + + TestCS + + + Physics\Particles + + + Physics\Particles + + + TestCS\FireWorksDemo + + + Physics\Particles\ForceGenerators\Gravity + + + Physics\Particles\ForceGenerators\Drag + + + Physics\Particles\ForceGenerators\FixedSpring + + + Physics\Particles\PairedSpring + + + Physics\Particles + + + TestCS\ParticleSphereDemo + + + TestCS\CablesAndRods + + + TestCS\CablesAndRods + + + Physics\Particles + + + Rendering + + + TestCS + + + TestCS + + + + + Source Files + + + Header Files + + + ECSCore + + + Rendering + + + ComponentsCore + + + ECSCore + + + Mix + + + Mix + + + Mix + + + Mix + + + Mix + + + Mix + + + Mix + + + Rendering + + + Rendering + + + Rendering + + + Rendering + + + Rendering + + + Rendering + + + Rendering + + + ECSCore + + + Input + + + Input + + + Rendering + + + Rendering + + + Rendering + + + Rendering + + + Rendering + + + Rendering + + + Rendering + + + Rendering + + + Rendering + + + TestCS + + + TestCS + + + TestCS + + + TestCS + + + Physics\Particles + + + Physics\Particles + + + Physics\Particles + + + Physics\Particles + + + TestCS\FireWorksDemo + + + TestCS\FireWorksDemo + + + Physics\Particles\ForceGenerators\Gravity + + + Physics\Particles\ForceGenerators\Gravity + + + Physics\Particles\ForceGenerators\Drag + + + Physics\Particles\ForceGenerators\Drag + + + Physics\Particles\ForceGenerators\FixedSpring + + + Physics\Particles\ForceGenerators\FixedSpring + + + Physics\Particles\PairedSpring + + + Physics\Particles\PairedSpring + + + Physics\Particles + + + Physics\Particles + + + TestCS\ParticleSphereDemo + + + TestCS\ParticleSphereDemo + + + TestCS\CablesAndRods + + + TestCS\CablesAndRods + + + TestCS\CablesAndRods + + + TestCS\CablesAndRods + + + Physics\Particles + + + Physics\Particles + + + ComponentsCore + + + Rendering + + + TestCS + + + TestCS + + + TestCS + + + TestCS + + + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + + Shaders + + \ No newline at end of file diff --git a/OpenGLEngine/OpenGLEngine/OpenGLEngine.vcxproj.user b/OpenGLEngine/OpenGLEngine/OpenGLEngine.vcxproj.user new file mode 100644 index 0000000..6e2aec7 --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/OpenGLEngine.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file 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/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/PairedSpringSystem.h b/OpenGLEngine/OpenGLEngine/PairedSpringSystem.h new file mode 100644 index 0000000..5cf0204 --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/PairedSpringSystem.h @@ -0,0 +1,13 @@ +#pragma once +#include "ECSConfig.h" +#include "PairedSpringComponent.h" + +namespace Reality +{ + class PairedSpringSystem : public ECSSystem + { + public: + 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/ParticleContactEvent.h b/OpenGLEngine/OpenGLEngine/ParticleContactEvent.h new file mode 100644 index 0000000..0d7074e --- /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.0f, 0), + float _penetration = 0.0f) + : 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..0b53901 100644 --- a/OpenGLEngine/OpenGLEngine/ParticleContactResolutionSystem.cpp +++ b/OpenGLEngine/OpenGLEngine/ParticleContactResolutionSystem.cpp @@ -1,186 +1,185 @@ #include "ParticleContactResolutionSystem.h" #include "ParticleComponent.h" +#include "ForceAccumulatorSystem.h" #include "TransformComponent.h" +#include "PenetrationDeltaMoveComponent.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(); + if (contactEvents.size() > 0) + { + for (int i = 0; i < velocityIterations; i++) + { + // Sort from highest incoming OR most negetive separting velocity to least + std::sort(contactEvents.begin(), contactEvents.end(), + [this](auto a, auto b) + { + return CalculateSeparationVelocity(a) < CalculateSeparationVelocity(b); + }); + ResolveVelocity(contactEvents[0], deltaTime); + } + for (int i = 0; i < positionIterations; i++) + { + // Sort from highest penetration to the least + std::sort(contactEvents.begin(), contactEvents.end(), + [this](auto a, auto b) + { + return CalculateActualPenetration(a) > CalculateActualPenetration(b); + }); + ResolveInterPenetration(contactEvents[0]); + } + } + //for (auto& contact : contactEvents) + //{ + // ResolveVelocity(contact, deltaTime); + // ResolveInterPenetration(contact); + //} } - void ParticleContactResolutionSystem::ResolveVelocity(ParticleContactComponent& contact, float deltaTime) + float ParticleContactResolutionSystem::CalculateSeparationVelocity(ParticleContactEvent & contact) { - float separatingVelocity = CalculateSeparatingVelocity(contact); + Vector3 velocityA = contact.entityA.hasComponent() ? + contact.entityA.getComponent().velocity : Vector3(0, 0, 0); - if (separatingVelocity > 0) - { - return; - } + Vector3 velocityB = contact.entityB.hasComponent() ? + contact.entityB.getComponent().velocity : Vector3(0, 0, 0); - 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; + Vector3 separationVelocity = velocityA - velocityB; + return glm::dot(separationVelocity, contact.normal); + } - float newSeparatingVelocity = -separatingVelocity * contact.restitution; + float ParticleContactResolutionSystem::CalculateActualPenetration(ParticleContactEvent & contact) + { + float actualPenetration = contact.penetration; - // Check the velocity build up due to accelaration only - Vector3 accCausedVelocity = Vector3(0, 0, 0); - if (isAvalid) - { - accCausedVelocity += contact.entityA.getComponent().accelaration; - } - if (isBvalid) + if (contact.entityA.hasComponent()) { - accCausedVelocity -= contact.entityB.getComponent().accelaration; + Vector3 deltaMove = contact.entityA.getComponent().deltaMove; + actualPenetration -= glm::dot(deltaMove, contact.normal); } - float accCausedSepVelocity = glm::dot(accCausedVelocity, contact.normal) * deltaTime; - // If we have a closing velocity due to accelaration build up, - // remove it from new separating velocity - if (accCausedSepVelocity < 0) + if (contact.entityB.hasComponent()) { - newSeparatingVelocity += contact.restitution * accCausedSepVelocity; - if (newSeparatingVelocity < 0) - { - newSeparatingVelocity = 0; - } + Vector3 deltaMove = contact.entityB.getComponent().deltaMove; + actualPenetration += glm::dot(deltaMove, contact.normal); } - float deltaVelocity = newSeparatingVelocity - separatingVelocity; + return actualPenetration; + } - float totalInverseMass = invM1 + invM2; + void ParticleContactResolutionSystem::ResolveVelocity(ParticleContactEvent & contact, float deltaTime) + { + float initialVelocity = CalculateSeparationVelocity(contact); - if (totalInverseMass <= 0) + if (initialVelocity > 0) { return; } - float impulse = deltaVelocity / totalInverseMass; - - Vector3 impulsePerIMass = contact.normal * impulse; + float finalVelocity = -initialVelocity * contact.restitution; - if (isAvalid) + Vector3 relativeAccelaration = Vector3(0, 0, 0); + if (contact.entityA.hasComponent()) { - contact.entityA.getComponent().velocity += impulsePerIMass * invM1; + relativeAccelaration += contact.entityA.getComponent().acceleration; } - if (isBvalid) + if (contact.entityB.hasComponent()) { - contact.entityB.getComponent().velocity -= impulsePerIMass * invM2; + relativeAccelaration -= contact.entityB.getComponent().acceleration; } - } - void ParticleContactResolutionSystem::ResolveInterpenetration(ParticleContactComponent& contact) - { - if (contact.penetration <= 0) + float accCausedSepVelocity = glm::dot(relativeAccelaration, contact.normal) * deltaTime; + + if (accCausedSepVelocity < 0) { - return; + finalVelocity += contact.restitution * accCausedSepVelocity; + + if (finalVelocity < 0) + { + finalVelocity = 0; + } } - 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 deltaVelocity = finalVelocity - initialVelocity; + + float invMA = contact.entityA.hasComponent() ? + contact.entityA.getComponent().inverseMass : 0; + + float invMB = contact.entityB.hasComponent() ? + contact.entityB.getComponent().inverseMass : 0; - float totalInverseMass = invM1 + invM2; + float totalInverseMass = invMA + invMB; if (totalInverseMass <= 0) { return; } - Vector3 movePerMass = contact.normal * (-contact.penetration / totalInverseMass); - contact.deltaMovePerMass = movePerMass; - if (isAvalid) + float impulse = deltaVelocity / totalInverseMass; + Vector3 impulsePerIMass = impulse * contact.normal; + + if (contact.entityA.hasComponent()) { - contact.entityA.getComponent().position -= movePerMass * invM1; + contact.entityA.getComponent().velocity += impulsePerIMass * invMA; } - if (isBvalid) + if (contact.entityB.hasComponent()) { - contact.entityB.getComponent().position += movePerMass * invM2; + contact.entityB.getComponent().velocity -= impulsePerIMass * invMB; } - //contact.penetration = 0; } - - void ParticleContactResolutionSystem::UpdateInterpenetration(ParticleContactComponent & bestContact, ParticleContactComponent & contact) + void ParticleContactResolutionSystem::ResolveInterPenetration(ParticleContactEvent & 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 actualPenetration = CalculateActualPenetration(contact); + + if (actualPenetration < 0) { - float mult = bestContact.entityA == contact.entityA ? -1 : 1; - Vector3 deltaMove = mult * bestContact.deltaMovePerMass * invM1; - float deltaPenetration = glm::dot(deltaMove, contact.normal); - contact.penetration -= deltaPenetration; + return; } - if (bestContact.entityB == contact.entityB || bestContact.entityA == contact.entityB) + + float invMassA = contact.entityA.hasComponent() ? + contact.entityA.getComponent().inverseMass : 0; + + float invMassB = contact.entityB.hasComponent() ? + contact.entityB.getComponent().inverseMass : 0; + + float totalInverseMass = invMassA + invMassB; + + if (totalInverseMass <= 0) { - float mult = bestContact.entityA == contact.entityB ? -1 : 1; - Vector3 deltaMove = mult * bestContact.deltaMovePerMass * invM2; - float deltaPenetration = glm::dot(deltaMove, contact.normal); - contact.penetration += deltaPenetration; + return; } - } - void ParticleContactResolutionSystem::Update(float deltaTime) - { - iterationsUsed = 0; - iterations = getEntities().size() * 2; + Vector3 movePerUnitIMass = contact.normal * (actualPenetration / totalInverseMass); - if (getEntities().size() > 0) + if (contact.entityA.hasComponent()) { - unsigned int bestContactIndex = 0; - unsigned int lastBest = 0; - while (iterationsUsed < iterations) + Vector3 deltaMove = movePerUnitIMass * invMassA; + contact.entityA.getComponent().position += deltaMove; + if (contact.entityA.hasComponent()) { - // 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++; + contact.entityA.getComponent().deltaMove += deltaMove; } + } - for (auto e : getEntities()) + if (contact.entityB.hasComponent()) + { + Vector3 deltaMove = movePerUnitIMass * invMassB; + contact.entityB.getComponent().position -= movePerUnitIMass * invMassB; + if (contact.entityB.hasComponent()) { - e.kill(); + contact.entityB.getComponent().deltaMove -= deltaMove; } } - - } } diff --git a/OpenGLEngine/OpenGLEngine/ParticleContactResolutionSystem.h b/OpenGLEngine/OpenGLEngine/ParticleContactResolutionSystem.h index ff0daa1..a203f9b 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,12 @@ 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 CalculateSeparationVelocity(ParticleContactEvent& contact); + float CalculateActualPenetration(ParticleContactEvent& contact); + void ResolveVelocity(ParticleContactEvent& contact, float deltaTime); + void ResolveInterPenetration(ParticleContactEvent& contact); + int velocityIterations = 4; + int positionIterations = 8; }; } - diff --git a/OpenGLEngine/OpenGLEngine/ParticleSphereComponent.h b/OpenGLEngine/OpenGLEngine/ParticleSphereComponent.h new file mode 100644 index 0000000..556ba9a --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/ParticleSphereComponent.h @@ -0,0 +1,16 @@ +#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..7ea6bf6 --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/ParticleSphereSystem.cpp @@ -0,0 +1,145 @@ +#include "ParticleSphereSystem.h" +#include "ParticleContactEvent.h" + +namespace Reality +{ + ParticleSphereSystem::ParticleSphereSystem() + { + requireComponent(); + requireComponent(); + } + + void ParticleSphereSystem::Update(float deltaTime) + { + //if (!createBox) + //{ + // boundingBox = getWorld().createEntity(); + // createBox = true; + //} + + //// Draw Bounding Box + //if (getEntities().size() > 0) + //{ + // getWorld().data.renderUtil->DrawCube( + // Vector3(0, 0, 0), + // Vector3(20, 20, 20), + // Vector3(0, 0, 0), + // Color::Purple + // ); + //} + + for (int i = 0; i < getEntities().size(); i++) + { + auto e = getEntities()[i]; + auto& transform = e.getComponent(); + auto& sphere = e.getComponent(); + + // Draw Debug Sphere + if (DEBUG_LOG_LEVEL > 0) + { + getWorld().data.renderUtil->DrawSphere( + transform.position, + sphere.radius, + Color::Orange + ); + } + + //// Collision Check with X + //if (abs(transform.position.x) + sphere.radius >= 10) + //{ + // Vector3 normal = Vector3(transform.position.x > 0 ? -1 : 1, 0, 0); + // float penetration = abs(transform.position.x) + sphere.radius - 10; + // + // getWorld().getEventManager().emitEvent( + // e, + // boundingBox, + // 0.8f, + // normal, + // penetration); + // + // getWorld().data.renderUtil->DrawLine( + // transform.position - normal * sphere.radius, + // transform.position - normal * (sphere.radius - penetration), + // Color::Red); + //} + // + //// Collision Check with Y + //if (abs(transform.position.y) + sphere.radius >= 10) + //{ + // Vector3 normal = Vector3(0, transform.position.y > 0 ? -1 : 1, 0); + // float penetration = abs(transform.position.y) + sphere.radius - 10; + // + // getWorld().getEventManager().emitEvent( + // e, + // boundingBox, + // 0.8f, + // normal, + // penetration); + // + // getWorld().data.renderUtil->DrawLine( + // transform.position - normal * sphere.radius, + // transform.position - normal * (sphere.radius - penetration), + // Color::Red); + //} + // + //// Collision Check with Z + //if (abs(transform.position.z) + sphere.radius >= 10) + //{ + // Vector3 normal = Vector3(0, 0, transform.position.z > 0 ? -1 : 1); + // float penetration = abs(transform.position.z) + sphere.radius - 10; + // + // getWorld().getEventManager().emitEvent( + // e, + // boundingBox, + // 0.8f, + // normal, + // penetration); + // + // getWorld().data.renderUtil->DrawLine( + // transform.position - normal * sphere.radius, + // transform.position - normal * (sphere.radius - penetration), + // Color::Red); + //} + + // Collision with other spheres + if (i < getEntities().size() - 1) + { + for (int j = i + 1; j < getEntities().size(); j++) + { + CheckCollision(e, getEntities()[j]); + } + } + } + } + void ParticleSphereSystem::CheckCollision(ECSEntity sphereEntityA, ECSEntity sphereEntityB) + { + auto& transformA = sphereEntityA.getComponent(); + auto& sphereA = sphereEntityA.getComponent(); + + auto& transformB = sphereEntityB.getComponent(); + auto& sphereB = sphereEntityB.getComponent(); + + Vector3 relativePos = transformA.position - transformB.position; + float distance = glm::length(relativePos); + + if (distance < sphereA.radius + sphereB.radius) + { + Vector3 normal = glm::normalize(relativePos); + float penetration = sphereA.radius + sphereB.radius - distance; + + getWorld().getEventManager().emitEvent( + sphereEntityA, + sphereEntityB, + 0.8f, + normal, + penetration + ); + + getWorld().data.renderUtil->DrawLine( + transformA.position - normal * sphereA.radius, + transformB.position + normal * sphereB.radius, + Color::Red + ); + } + } +} diff --git a/OpenGLEngine/OpenGLEngine/ParticleSphereSystem.h b/OpenGLEngine/OpenGLEngine/ParticleSphereSystem.h new file mode 100644 index 0000000..0bda2db --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/ParticleSphereSystem.h @@ -0,0 +1,18 @@ +#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 createBox = false; + ECSEntity boundingBox; + void CheckCollision(ECSEntity sphereEntityA, ECSEntity sphereEntityB); + }; +} 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/PenetrationDeltaMoveComponent.h b/OpenGLEngine/OpenGLEngine/PenetrationDeltaMoveComponent.h new file mode 100644 index 0000000..fb50100 --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/PenetrationDeltaMoveComponent.h @@ -0,0 +1,14 @@ +#pragma once +#include "ECSConfig.h" + +namespace Reality +{ + struct PenetrationDeltaMoveComponent + { + PenetrationDeltaMoveComponent() : deltaMove(Vector3(0, 0, 0)) + { + + } + Vector3 deltaMove; + }; +} diff --git a/OpenGLEngine/OpenGLEngine/PlaneComponent.h b/OpenGLEngine/OpenGLEngine/PlaneComponent.h new file mode 100644 index 0000000..c343c6c --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/PlaneComponent.h @@ -0,0 +1,29 @@ +#pragma once +#include "ECSConfig.h" + +namespace Reality +{ + struct PlaneComponent + { + PlaneComponent(ECSEntity _p1 = ECSEntity(), ECSEntity _p2 = ECSEntity(), ECSEntity _p3 = ECSEntity(), ECSEntity _p4 = ECSEntity()) : p1(_p1), p2(_p2), p3(_p3), p4(_p4) + { + + } + + ECSEntity p1; + ECSEntity p2; + ECSEntity p3; + ECSEntity p4; + + Vector3 normal1; + Vector3 normal2; + + Vector3 middle1; + Vector3 middle2; + + float d1; + float d2; + + vector spheres; + }; +} diff --git a/OpenGLEngine/OpenGLEngine/PlaneSystem.cpp b/OpenGLEngine/OpenGLEngine/PlaneSystem.cpp new file mode 100644 index 0000000..4384764 --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/PlaneSystem.cpp @@ -0,0 +1,210 @@ +#include "PlaneSystem.h" +#include "TransformComponent.h" +#include "ParticleSphereSystem.h" +#include "ParticleContactEvent.h" + +namespace Reality +{ + PlaneSystem::PlaneSystem() + { + requireComponent(); + } + + void PlaneSystem::Update(float deltaTime) + { + for (auto e : getEntities()) + { + auto& plane = e.getComponent(); + + Vector3 planep1 = plane.p1.getComponent().position; + Vector3 planep2 = plane.p2.getComponent().position; + Vector3 planep3 = plane.p3.getComponent().position; + Vector3 planep4 = plane.p4.getComponent().position; + + plane.normal1 = glm::normalize(glm::cross((planep3 - planep1), (planep2 - planep1))); + + plane.normal2 = glm::normalize(glm::cross((planep3 - planep2), (planep4 - planep2))); + + + plane.middle1 = (planep1 + planep2 + planep3) / 3.0f; + + plane.middle2 = (planep2 + planep3 + planep4) / 3.0f; + + + plane.d1 = -((plane.normal1.x * plane.middle1.x) + (plane.normal1.y * plane.middle1.y) + (plane.normal1.z * plane.middle1.z)); + + plane.d2 = -((plane.normal2.x * plane.middle2.x) + (plane.normal2.y * plane.middle2.y) + (plane.normal2.z * plane.middle2.z)); + + float l = (plane.normal1.x * plane.middle1.x) + (plane.normal1.y * plane.middle1.y) + (plane.normal1.z * plane.middle1.z) + plane.d1; + + plane.spheres = getWorld().getSystemManager().getSystem().getEntities(); + + for (int i = 0; i < plane.spheres.size(); i++) + { + checkCollision(e, plane.spheres[i]); + } + } + } + + void PlaneSystem::checkCollision(ECSEntity _plane, ECSEntity _sphere) + { + auto& plane = _plane.getComponent(); + + Vector3 planep1 = plane.p1.getComponent().position; + Vector3 planep2 = plane.p2.getComponent().position; + Vector3 planep3 = plane.p3.getComponent().position; + Vector3 planep4 = plane.p4.getComponent().position; + + auto& sphere = _sphere.getComponent(); + auto& sphereTransform = _sphere.getComponent(); + + // Distance = (A*x0+B*y0+C*z0+D) / Sqrt(A*A+B*B+C*C) + + float distance1 = ((plane.normal1.x * sphereTransform.position.x) + (plane.normal1.y * sphereTransform.position.y) + (plane.normal1.z * sphereTransform.position.z) + plane.d1) + / sqrt(plane.normal1.x * plane.normal1.x + plane.normal1.y * plane.normal1.y + plane.normal1.z * plane.normal1.z); + + float distance2 = ((plane.normal2.x * sphereTransform.position.x) + (plane.normal2.y * sphereTransform.position.y) + (plane.normal2.z * sphereTransform.position.z) + plane.d2) + / sqrt(plane.normal2.x * plane.normal2.x + plane.normal2.y * plane.normal2.y + plane.normal2.z * plane.normal2.z); + + Vector3 projPosition1 = sphereTransform.position - (sphereTransform.position * plane.normal1 / abs(plane.normal1) * abs(plane.normal1)) * plane.normal1; + + Vector3 projPosition2 = sphereTransform.position - (sphereTransform.position * plane.normal2 / abs(plane.normal2) * abs(plane.normal2)) * plane.normal2; + + if (abs(distance1) <= sphere.radius && checkPointInTriangle(projPosition1, planep1, planep2, planep3)) + { + cout << "Collision" << endl; + + float penetration = sphere.radius - abs(distance1); + + getWorld().getEventManager().emitEvent( + _sphere, + plane.p1, + 1.0f, + plane.normal1, + penetration + ); + + getWorld().getEventManager().emitEvent( + _sphere, + plane.p2, + 1.0f, + plane.normal1, + penetration + ); + + getWorld().getEventManager().emitEvent( + _sphere, + plane.p3, + 1.0f, + plane.normal1, + penetration + ); + + getWorld().data.renderUtil->DrawTriangle(planep1, planep2, planep3, Color::Red); + getWorld().data.renderUtil->DrawTriangle(planep2, planep3, planep4, Color::Green); + } + else if (abs(distance2) <= sphere.radius && checkPointInTriangle(projPosition2, planep2, planep3, planep4)) + { + cout << "Collision" << endl; + + float penetration = sphere.radius - abs(distance2); + + getWorld().getEventManager().emitEvent( + _sphere, + plane.p2, + 1.0f, + plane.normal2, + penetration + ); + + getWorld().getEventManager().emitEvent( + _sphere, + plane.p3, + 1.0f, + plane.normal2, + penetration + ); + + getWorld().getEventManager().emitEvent( + _sphere, + plane.p4, + 1.0f, + plane.normal2, + penetration + ); + + getWorld().data.renderUtil->DrawTriangle(planep2, planep3, planep4, Color::Red); + getWorld().data.renderUtil->DrawTriangle(planep1, planep2, planep3, Color::Green); + } + else + { + getWorld().data.renderUtil->DrawTriangle(planep1, planep2, planep3, Color::Green); + getWorld().data.renderUtil->DrawTriangle(planep2, planep3, planep4, Color::Green); + } + + if (sphereTransform.position.y <= -20) + { + sphereTransform.position = plane.middle1; + + sphereTransform.position.x += RANDOM_FLOAT(-3.0f, 3.0f); + sphereTransform.position.z += RANDOM_FLOAT(-3.0f, 3.0f); + sphereTransform.position.y += 20.0f; + } + + //Vector3 relativeLocation = plane.normal1 - sphereTransform.position; + //float d = glm::length(relativeLocation); + // + //float distance1 = (plane.normal1.x * sphereTransform.position.x) + (plane.normal1.y * sphereTransform.position.y) + (plane.normal1.z * sphereTransform.position.z) - plane.d1; + //float distance2 = (plane.normal2.x * sphereTransform.position.x) + (plane.normal2.y * sphereTransform.position.y) + (plane.normal2.z * sphereTransform.position.z) - plane.d2; + // + //float j = glm::length((sphere.radius - abs(distance1)) * plane.normal1); + + //float temp = sphere.radius - abs(distance); + + //if(p == sphere.radius - abs(glm::distance(sphereTransform.position, plane.middle1) * plane.normal1)) + + } + + bool PlaneSystem::checkSameSide(Vector3 p1, Vector3 p2, Vector3 a, Vector3 b) + { + Vector3 cp1 = glm::cross(b - a, p1 - a); + Vector3 cp2 = glm::cross(b - a, p2 - a); + + float d = glm::dot(cp1, cp2); + + if (glm::dot(cp1, cp2) >= 0) + { + return true; + } + else + { + return false; + } + } + + bool PlaneSystem::checkPointInTriangle(Vector3 p, Vector3 a, Vector3 b, Vector3 c) + { + if (checkSameSide(p, a, b, c) && checkSameSide(p, b, a, c) && checkSameSide(p, c, a, b)) + { + return true; + } + else + { + return false; + } + } + + /* + + function SameSide(p1,p2, a,b) + cp1 = CrossProduct(b-a, p1-a) + cp2 = CrossProduct(b-a, p2-a) + if DotProduct(cp1, cp2) >= 0 then return true + else return false + + function PointInTriangle(p, a,b,c) + if SameSide(p,a, b,c) and SameSide(p,b, a,c) + and SameSide(p,c, a,b) then return true + else return false + */ +} diff --git a/OpenGLEngine/OpenGLEngine/PlaneSystem.h b/OpenGLEngine/OpenGLEngine/PlaneSystem.h new file mode 100644 index 0000000..1c6a015 --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/PlaneSystem.h @@ -0,0 +1,19 @@ +#pragma once +#include "ECSConfig.h" +#include "PlaneComponent.h" +#include "ParticleSphereComponent.h" + +namespace Reality +{ + class PlaneSystem : public ECSSystem + { + public: + PlaneSystem(); + void Update(float deltaTime); + + private: + void checkCollision(ECSEntity plane, ECSEntity sphere); + bool checkSameSide(Vector3 p1, Vector3 p2, Vector3 a, Vector3 b); + bool checkPointInTriangle(Vector3 p1, Vector3 a, Vector3 b, Vector3 c); + }; +} diff --git a/OpenGLEngine/OpenGLEngine/RenderingSystemV2.cpp b/OpenGLEngine/OpenGLEngine/RenderingSystemV2.cpp index aa2dbef..1ed4cac 100644 --- a/OpenGLEngine/OpenGLEngine/RenderingSystemV2.cpp +++ b/OpenGLEngine/OpenGLEngine/RenderingSystemV2.cpp @@ -1,40 +1,41 @@ -#include "RenderingSystemV2.h" - -namespace Reality -{ - RenderingSystemV2::RenderingSystemV2() - { - requireComponent(); - requireComponent(); - } - - void RenderingSystemV2::Update(float deltaTime) - { +#include "RenderingSystemV2.h" +#include "Shader.h" +#include "Camera.h" + +namespace Reality +{ + RenderingSystemV2::RenderingSystemV2() + { + requireComponent(); + requireComponent(); + } + + void RenderingSystemV2::Update(float deltaTime) + { for (auto e : getEntities()) { - auto& transform = e.getComponent(); - auto &model = e.getComponent(); + auto &transform = e.getComponent(); + auto &mesh = e.getComponent(); if (getWorld().data.assetLoader->ModelsLoaded()) { getWorld().data.assetLoader->SetLight(getWorld().data.renderUtil->camera.Position); } - if (model.modelId < 0) + + if (mesh.modelId < 0) { - model.modelId = getWorld().data.assetLoader->GetModelId(model.mesh); + mesh.modelId = getWorld().data.assetLoader->GetModelId(mesh.mesh); } - if (model.modelId >= 0) + + if (mesh.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->DrawModel(mesh.modelId, transform.GetTransformationMatrix()); } - 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); - } - } -} + + // Draw + //getWorld().data.renderUtil->DrawCube(transform.position, Vector3(10,10,10), transform.eulerAngles); + //getWorld().data.renderUtil->DrawCube(transform.position + Vector3(0, transform.scale.y , 0) * 7.5f, transform.scale * 15.0f, transform.eulerAngles); + //getWorld().data.renderUtil->DrawLine(transform.position - Vector3(1, 1, 1), transform.position + Vector3(1, 1, 1)); + } + } +} diff --git a/OpenGLEngine/OpenGLEngine/RenderingSystemV2.h b/OpenGLEngine/OpenGLEngine/RenderingSystemV2.h index b2a18e5..ff72234 100644 --- a/OpenGLEngine/OpenGLEngine/RenderingSystemV2.h +++ b/OpenGLEngine/OpenGLEngine/RenderingSystemV2.h @@ -2,6 +2,10 @@ #include "ECSConfig.h" #include "TransformComponentV2.h" #include "MeshComponent.h" + +class Shader; +class Camera; + namespace Reality { class RenderingSystemV2 : public ECSSystem @@ -11,3 +15,4 @@ namespace Reality void Update(float deltaTime); }; } + diff --git a/OpenGLEngine/OpenGLEngine/ResetPenetrationDeltaMoveSystem.cpp b/OpenGLEngine/OpenGLEngine/ResetPenetrationDeltaMoveSystem.cpp new file mode 100644 index 0000000..8b1d203 --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/ResetPenetrationDeltaMoveSystem.cpp @@ -0,0 +1,18 @@ +#include "ResetPenetrationDeltaMoveSystem.h" + +namespace Reality +{ + ResetPenetrationDeltaMoveSystem::ResetPenetrationDeltaMoveSystem() + { + requireComponent(); + } + + void ResetPenetrationDeltaMoveSystem::Update(float deltaTime) + { + for (auto e : getEntities()) + { + auto& penetration = e.getComponent(); + penetration.deltaMove = Vector3(0, 0, 0); + } + } +} diff --git a/OpenGLEngine/OpenGLEngine/ResetPenetrationDeltaMoveSystem.h b/OpenGLEngine/OpenGLEngine/ResetPenetrationDeltaMoveSystem.h new file mode 100644 index 0000000..958dfed --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/ResetPenetrationDeltaMoveSystem.h @@ -0,0 +1,13 @@ +#pragma once +#include "ECSConfig.h" +#include "PenetrationDeltaMoveComponent.h" + +namespace Reality +{ + class ResetPenetrationDeltaMoveSystem : public ECSSystem + { + public: + ResetPenetrationDeltaMoveSystem(); + void Update(float deltaTime); + }; +} diff --git a/OpenGLEngine/OpenGLEngine/RodComponent.h b/OpenGLEngine/OpenGLEngine/RodComponent.h index 33583ad..5c5b3ef 100644 --- a/OpenGLEngine/OpenGLEngine/RodComponent.h +++ b/OpenGLEngine/OpenGLEngine/RodComponent.h @@ -5,11 +5,17 @@ namespace Reality { struct RodComponent { - RodComponent(ECSEntity a = ECSEntity(), ECSEntity b = ECSEntity(), float _length = 10) - : entityA(a), entityB(b), length(_length) - {} + RodComponent(ECSEntity a = ECSEntity(), + ECSEntity b = ECSEntity(), + float _rodLength = 10) + : entityA(a), + entityB(b), + rodLength(_rodLength) + { + + } ECSEntity entityA; ECSEntity entityB; - float length; + float rodLength; }; } diff --git a/OpenGLEngine/OpenGLEngine/RodSystem.cpp b/OpenGLEngine/OpenGLEngine/RodSystem.cpp index f1da27e..c3f6bb7 100644 --- a/OpenGLEngine/OpenGLEngine/RodSystem.cpp +++ b/OpenGLEngine/OpenGLEngine/RodSystem.cpp @@ -1,6 +1,6 @@ #include "RodSystem.h" #include "TransformComponent.h" -#include "ParticleContactComponent.h" +#include "ParticleContactEvent.h" namespace Reality { @@ -14,49 +14,50 @@ namespace Reality 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) + if (rod.entityA.hasComponent() && + rod.entityB.hasComponent()) { - continue; + auto& transformA = rod.entityA.getComponent(); + auto& transformB = rod.entityB.getComponent(); + + Vector3 relativePos = transformA.position - transformB.position; + float length = glm::length(relativePos); + + if (length > rod.rodLength) + { + Vector3 normal = -glm::normalize(relativePos); + float penetration = length - rod.rodLength; + + getWorld().getEventManager().emitEvent( + rod.entityA, + rod.entityB, + 0, + normal, + penetration + ); + } + + if (length < rod.rodLength) + { + Vector3 normal = glm::normalize(relativePos); + float penetration = rod.rodLength - length; + + getWorld().getEventManager().emitEvent( + rod.entityA, + rod.entityB, + 0, + normal, + penetration + ); + } + + getWorld().data.renderUtil->DrawLine( + transformA.position, + transformB.position, + Color::Beige + ); } - - 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/RotateComponentV2.h b/OpenGLEngine/OpenGLEngine/RotateComponentV2.h new file mode 100644 index 0000000..886d43e --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/RotateComponentV2.h @@ -0,0 +1,15 @@ +#pragma once +#include "ECSConfig.h" + +namespace Reality +{ + struct RotateComponentV2 + { + RotateComponentV2(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/RotateSystemV2.cpp b/OpenGLEngine/OpenGLEngine/RotateSystemV2.cpp new file mode 100644 index 0000000..286d03a --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/RotateSystemV2.cpp @@ -0,0 +1,26 @@ +#include "RotateSystemV2.h" +#include + +namespace Reality +{ + RotateSystemV2::RotateSystemV2() + { + requireComponent(); + requireComponent(); + } + + void RotateSystemV2::Update(float deltaTime) + { + for (auto e : getEntities()) + { + auto& transform = e.getComponent(); + auto& rotate = e.getComponent(); + + Vector3 axis = glm::normalize(rotate.rotationVelocity); + float angle = glm::radians(glm::length(rotate.rotationVelocity)) * deltaTime; + Quaternion deltaQuat = glm::angleAxis(angle, axis); + Quaternion quat = glm::normalize(deltaQuat * transform.GetEulerAngles()); + transform.SetOrientation(deltaQuat * transform.GetOrientation()); + } + } +} diff --git a/OpenGLEngine/OpenGLEngine/RotateSystemV2.h b/OpenGLEngine/OpenGLEngine/RotateSystemV2.h new file mode 100644 index 0000000..2cbf7a2 --- /dev/null +++ b/OpenGLEngine/OpenGLEngine/RotateSystemV2.h @@ -0,0 +1,13 @@ +#pragma once +#include "ECSConfig.h" +#include "TransformComponentV2.h" +#include "RotateComponentV2.h" +namespace Reality +{ + class RotateSystemV2 : public ECSSystem + { + public: + RotateSystemV2(); + void Update(float deltaTime); + }; +} diff --git a/OpenGLEngine/OpenGLEngine/TransformComponentV2.h b/OpenGLEngine/OpenGLEngine/TransformComponentV2.h index 27575d5..170dae0 100644 --- a/OpenGLEngine/OpenGLEngine/TransformComponentV2.h +++ b/OpenGLEngine/OpenGLEngine/TransformComponentV2.h @@ -6,11 +6,12 @@ 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)) : + TransformComponentV2(Vector3 _position = Vector3(0, 0, 0), Vector3 _scale = Vector3(1, 1, 1), Vector3 _eulerAngles = Vector3(0, 0, 0)) : position(_position), scale(_scale) { - SetRotation(_eulerAngles); + SetEulerAngles(_eulerAngles); } + private: Vector3 position; Vector3 scale; @@ -20,67 +21,57 @@ namespace Reality Mat4 translationMatrix; Mat4 unScaledTransformationMatrix; Mat4 transformationMatrix; - bool dirty = true; + bool dirty; inline void UpdateMatrices() { - scaleMatrix = glm::scale(glm::mat4(1.0f), scale); - translationMatrix = glm::translate(glm::mat4(1.0f), position); + scaleMatrix = glm::scale(Mat4(1.0f), scale); + translationMatrix = glm::translate(Mat4(1.0f), position); rotationMatrix = glm::toMat4(orientation); unScaledTransformationMatrix = translationMatrix * rotationMatrix; transformationMatrix = unScaledTransformationMatrix * scaleMatrix; - dirty = false; } public: - inline void SetPosition(const Vector3& _position) + + inline void Setposition(Vector3 _position) { position = _position; dirty = true; } - inline Vector3 GetPosition() { return position; } - - inline void SetScale(const Vector3& _scale) + inline void SetScale(Vector3 _scale) { scale = _scale; dirty = true; } - inline Vector3 GetScale() { return scale; } - - inline void SetOrientation(const Quaternion& _orientation) + inline Vector3 GetScale() { - orientation = _orientation; - dirty = true; + return scale; } - inline Quaternion GetOrientation() { return orientation; } - - // Euler angles in degrees - inline void SetRotation(Vector3 eulerAngles) + inline void SetOrientation(Quaternion _orientation) { - glm::vec3 rotationInRads = glm::vec3(glm::radians(eulerAngles.x), - glm::radians(eulerAngles.y), glm::radians(eulerAngles.z)); - orientation = glm::quat(rotationInRads); + orientation = _orientation; dirty = true; } - inline Vector3 GetRotation() { return glm::eulerAngles(orientation); } - - inline Vector3 Up() + inline Quaternion GetOrientation() { - return orientation * Vector3(0.0f, 1.0f, 0.0f); + return orientation; } - inline Vector3 Right() + inline void SetEulerAngles(Vector3 _eulerAngles) { - return orientation * Vector3(1.0f, 0.0f, 0.0f); + Vector3 eulerRads = Vector3(glm::radians(_eulerAngles.x), glm::radians(_eulerAngles.y), glm::radians(_eulerAngles.z)); + orientation = glm::quat(eulerRads); + dirty = true; } - inline Vector3 Forward() + inline Vector3 GetEulerAngles() { - return orientation * Vector3(0.0f, 0.0f, 1.0f); + return glm::eulerAngles(orientation); } inline Mat4 GetScaleMatrix() @@ -92,24 +83,24 @@ namespace Reality return scaleMatrix; } - inline Mat4 GetRotationMatrix() + inline Mat4 GetTranslationMatrix() { if (dirty) { UpdateMatrices(); } - return rotationMatrix; + return translationMatrix; } - - inline Mat4 GetTranslationMatrix() + + inline Mat4 GetRotationMatrix() { if (dirty) { UpdateMatrices(); } - return transformationMatrix; + return rotationMatrix; } - + inline Mat4 GetUnScaledTransformationMatrix() { if (dirty) @@ -118,7 +109,7 @@ namespace Reality } return unScaledTransformationMatrix; } - + inline Mat4 GetTransformationMatrix() { if (dirty) @@ -128,30 +119,48 @@ namespace Reality return transformationMatrix; } - inline Vector3 WorldToLocalPosition(Vector3 _position) + inline Vector3 Right() { - return glm::inverse(GetTransformationMatrix()) * Vector4(_position, 1); + return orientation * Vector3(1, 0, 0); + } + inline Vector3 Up() + { + return orientation * Vector3(0, 1, 0); } - inline Vector3 LocalToWorldPosition(Vector3 _position) + inline Vector3 Forward() { - return GetTransformationMatrix() * Vector4(_position, 1); + return orientation * Vector3(0, 0, 1); } - //TODO : Check - inline Vector3 WorldToLocalDirection(Vector3 direction) + inline Vector3 LocalToWorldPosition(Vector3 localPosition) { - if (glm::length(orientation) > 0) + return transformationMatrix * Vector4(localPosition, 1.0f); + } + + inline Vector3 WorldToLocalPosition(Vector3 worldPosition) + { + if (abs(glm::determinant(transformationMatrix)) > 0) { - return glm::inverse(orientation) * direction; + return glm::inverse(transformationMatrix) * Vector4(worldPosition, 1.0f); } return Vector3(0, 0, 0); } - inline Vector3 LocalToWorldDirection(Vector3 direction) + inline Vector3 LocalToWorldDirection(Vector3 localDirection) + { + return orientation * localDirection; + } + + inline Vector3 WorldToLocalDirection(Vector3 worldDirection) { - return orientation * direction; + if (glm::length(orientation) > 0) + { + return glm::inverse(orientation) * worldDirection; + } + return Vector3(0, 0, 0); } + }; -} +} \ No newline at end of file