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
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;
};
}
60 changes: 60 additions & 0 deletions OpenGLEngine/OpenGLEngine/FireworksSystem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "FireworksSystem.h"
#include "ParticleComponent.h"
#include "ForceAccumulatorComponent.h"
#include "GravityForceComponent.h"

namespace Reality
{
FireworksSystem::FireworksSystem()
{
requireComponent<TransformComponent>();
requireComponent<FireworksComponent>();
}

void FireworksSystem::Update(float deltaTime)
{
for (auto e : getEntities())
{
auto& transform = e.getComponent<TransformComponent>();
auto& fireworks = e.getComponent<FireworksComponent>();

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<TransformComponent>(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<ParticleComponent>(velocity);
particle.addComponent<ForceAccumulatorComponent>();
particle.addComponent<GravityForceComponent>();
float colorAlpha = (float)i / (float)fireworks.numberOfParticles;
particle.addComponent<FireworksComponent>(
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);
}
}
}
}
14 changes: 14 additions & 0 deletions OpenGLEngine/OpenGLEngine/FireworksSystem.h
Original file line number Diff line number Diff line change
@@ -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);
};
}
13 changes: 10 additions & 3 deletions OpenGLEngine/OpenGLEngine/FixedSpringComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}
55 changes: 55 additions & 0 deletions OpenGLEngine/OpenGLEngine/FixedSpringSystem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include "FixedSpringSystem.h"
#include "ForceAccumulatorComponent.h"

namespace Reality
{
FixedSpringSystem::FixedSpringSystem()
{
requireComponent<TransformComponent>();
requireComponent<FixedSpringComponent>();
}

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

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;
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/FixedSpringSystem.h
Original file line number Diff line number Diff line change
@@ -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);
};
}
30 changes: 30 additions & 0 deletions OpenGLEngine/OpenGLEngine/ForceAccumulatorComponent.h
Original file line number Diff line number Diff line change
@@ -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;
};
}
Loading