Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions OpenGLEngine/OpenGLEngine/BungeeChordComponent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once
#include "ECSConfig.h"

namespace Reality
{
struct BungeeChordComponent
{
BungeeChordComponent(float _springConstant = 10.0f, float _restLength = 10.0f, ECSEntity _connectedEntity = ECSEntity())
: springConstant(_springConstant), restLength(_restLength), connectedEntity(_connectedEntity)
{

}
float springConstant;
float restLength;
ECSEntity connectedEntity;
};
}
60 changes: 60 additions & 0 deletions OpenGLEngine/OpenGLEngine/BungeeChordSystem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "BungeeChordSystem.h"
#include "ForceAccumulatorComponent.h"

namespace Reality
{
BungeeChordSystem::BungeeChordSystem()
{
requireComponent<TransformComponent>();
requireComponent<BungeeChordComponent>();
}

void BungeeChordSystem::Update(float deltaTime)
{
for (auto e : getEntities())
{
auto& springTransform = e.getComponent<TransformComponent>();
auto& spring = e.getComponent<BungeeChordComponent>();

if (spring.connectedEntity.hasComponent<ForceAccumulatorComponent>()
&& spring.connectedEntity.hasComponent<TransformComponent>())
{
auto& forceAcc = spring.connectedEntity.getComponent<ForceAccumulatorComponent>();
auto& transform = spring.connectedEntity.getComponent<TransformComponent>();

Vector3 relativePosition = transform.position - springTransform.position;
float length = glm::length(relativePosition);
if (length > 0)
{
float deltaL = length - spring.restLength;

if (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);
}

}
}
}
}
14 changes: 14 additions & 0 deletions OpenGLEngine/OpenGLEngine/BungeeChordSystem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once
#include "ECSConfig.h"
#include "TransformComponent.h"
#include "BungeeChordComponent.h"

namespace Reality
{
class BungeeChordSystem : public ECSSystem
{
public:
BungeeChordSystem();
void Update(float deltaTime);
};
}
18 changes: 18 additions & 0 deletions OpenGLEngine/OpenGLEngine/BuoyancyComponent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once
#include "ECSConfig.h"

namespace Reality
{
struct BuoyancyComponent
{
BuoyancyComponent(float _maxDepth = 5.f, float _volume = 10.f)
:maxDepth(_maxDepth), volume(_volume)
{

}

float maxDepth;
float volume;
};
}

58 changes: 58 additions & 0 deletions OpenGLEngine/OpenGLEngine/BuoyancySystem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include "BuoyancySystem.h"
#include "ForceAccumulatorComponent.h"

namespace Reality
{
BuoyancySystem::BuoyancySystem()
{
requireComponent<TransformComponent>();
requireComponent<BuoyancyComponent>();
}

void BuoyancySystem::Update(float deltaTime)
{
for (auto e : getEntities())
{
int i = 0;
++i;

//Draw water surface
getWorld().data.renderUtil->DrawLine(Vector3(-500, 0, -5), Vector3(500, 0, 5));


auto& entityTransform = e.getComponent<TransformComponent>();
auto& entityBuoyancy = e.getComponent<BuoyancyComponent>();
auto& entityForceAcc = e.getComponent<ForceAccumulatorComponent>();

float depth = entityTransform.position.y;

//Draw cube
getWorld().data.renderUtil->DrawCube(
entityTransform.position/* - Vector3(0, 1, 0) * entityBuoyancy.maxDepth * 0.5f*/,
Vector3(entityBuoyancy.maxDepth * 2.f, entityBuoyancy.maxDepth * 2.f, entityBuoyancy.maxDepth * 2.f),
Vector3(0, 0, 0));

Vector3 force = Vector3(0.f, 0.f, 0.f);

//check if entity is out of water
if (depth >= waterHeight + entityBuoyancy.maxDepth)
{
force.y = 0.f;
}

//check if entity is at maximum depth
else if (depth <= waterHeight - entityBuoyancy.maxDepth)
{
force.y = liquidDensity * entityBuoyancy.volume;
}

//otherwise entity is partly submerged
else
{
force.y = (liquidDensity * entityBuoyancy.volume * (entityBuoyancy.maxDepth - depth - waterHeight)) / (2 * entityBuoyancy.maxDepth);
}

entityForceAcc.AddForce(force);
}
}
}
18 changes: 18 additions & 0 deletions OpenGLEngine/OpenGLEngine/BuoyancySystem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once
#include "ECSConfig.h"
#include "TransformComponent.h"
#include "BuoyancyComponent.h"

namespace Reality
{
class BuoyancySystem : public ECSSystem
{
public:
BuoyancySystem();
void Update(float deltaTime);

float waterHeight = 0.f;
float liquidDensity = 10.f;
};
}

14 changes: 12 additions & 2 deletions OpenGLEngine/OpenGLEngine/CableComponent.h
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
49 changes: 49 additions & 0 deletions OpenGLEngine/OpenGLEngine/CableSystem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "CableSystem.h"
#include "TransformComponent.h"
#include "ParticleContactEvent.h"

namespace Reality
{
CableSystem::CableSystem()
{
requireComponent<CableComponent>();
}

void CableSystem::Update(float deltaTime)
{
for (auto e : getEntities())
{
auto& cable = e.getComponent<CableComponent>();

if (cable.entityA.hasComponent<TransformComponent>() &&
cable.entityB.hasComponent<TransformComponent>())
{
auto& transformA = cable.entityA.getComponent<TransformComponent>();
auto& transformB = cable.entityB.getComponent<TransformComponent>();

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<ParticleContactEvent>(
cable.entityA,
cable.entityB,
cable.restitution,
normal,
penetration
);
}

getWorld().data.renderUtil->DrawLine(
transformA.position,
transformB.position,
Color::Magenta
);
}
}
}
}
13 changes: 13 additions & 0 deletions OpenGLEngine/OpenGLEngine/CableSystem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once
#include "ECSConfig.h"
#include "CableComponent.h"

namespace Reality
{
class CableSystem : public ECSSystem
{
public:
CableSystem();
void Update(float deltaTime);
};
}
16 changes: 16 additions & 0 deletions OpenGLEngine/OpenGLEngine/DragForceComponent.h
Original file line number Diff line number Diff line change
@@ -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;
};
}
29 changes: 29 additions & 0 deletions OpenGLEngine/OpenGLEngine/DragForceSystem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "DragForceSystem.h"

namespace Reality
{
DragForceSystem::DragForceSystem()
{
requireComponent<ParticleComponent>();
requireComponent<ForceAccumulatorComponent>();
requireComponent<DragForceComponent>();
}

void DragForceSystem::Update(float deltaTime)
{
for (auto e : getEntities())
{
auto& particle = e.getComponent<ParticleComponent>();
auto& forceAcc = e.getComponent<ForceAccumulatorComponent>();
auto& drag = e.getComponent<DragForceComponent>();

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);
}
}
}
}
15 changes: 15 additions & 0 deletions OpenGLEngine/OpenGLEngine/DragForceSystem.h
Original file line number Diff line number Diff line change
@@ -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);
};
}
1 change: 1 addition & 0 deletions OpenGLEngine/OpenGLEngine/ECSConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#define RANDOM_FLOAT(LO, HI) LO + static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / (HI - LO)))
#define DEBUG_LOG_LEVEL 3

namespace Reality
{
Expand Down
20 changes: 20 additions & 0 deletions OpenGLEngine/OpenGLEngine/FireworksComponent.h
Original file line number Diff line number Diff line change
@@ -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;
};
}
Loading