Skip to content

Commit

Permalink
[#67]Gravity Force Generator is implemented.
Browse files Browse the repository at this point in the history
  • Loading branch information
ilia-glushchenko committed Aug 1, 2017
1 parent 51fedc9 commit 4e607a2
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
31 changes: 29 additions & 2 deletions Pegasus/include/ParticleForceGenerator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include "Pegasus/include/Particle.hpp"

#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/norm.hpp>
#include <glm/glm.hpp>

#include <map>
Expand Down Expand Up @@ -40,16 +42,41 @@ class ParticleForceRegistry
std::map<Particle*, std::set<ParticleForceGenerator*>> mRegistrations;
};

class ParticleGravity : public ParticleForceGenerator
class ParticleStaticField : public ParticleForceGenerator
{
public:
explicit ParticleGravity(glm::dvec3 const& g);
explicit ParticleStaticField(glm::dvec3 const& g);
void UpdateForce(Particle& p) override;

private:
glm::dvec3 const m_gravity;
};

class ParticleGravity : public ParticleForceGenerator
{
public:
explicit ParticleGravity(Particle const& particle, double G = 6.674e-11)
: m_particle(particle)
, m_G(G)
{
}

void UpdateForce(Particle& p) override
{
glm::dvec3 gravityDirection = m_particle.GetPosition() - p.GetPosition();

double const gravityMagnitude =
m_G * (m_particle.GetMass() * p.GetMass())
/ glm::length2(gravityDirection);

p.AddForce(glm::normalize(gravityDirection) * gravityMagnitude);
}

private:
Particle const& m_particle;
double const m_G;
};

class ParticleDrag : public ParticleForceGenerator
{
public:
Expand Down
4 changes: 2 additions & 2 deletions Pegasus/sources/ParticleForceGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ void pegasus::ParticleForceRegistry::UpdateForces()
}
}

pegasus::ParticleGravity::ParticleGravity(glm::dvec3 const& g)
pegasus::ParticleStaticField::ParticleStaticField(glm::dvec3 const& g)
: m_gravity(g)
{
}

void pegasus::ParticleGravity::UpdateForce(Particle& p)
void pegasus::ParticleStaticField::UpdateForce(Particle& p)
{
if (!p.HasFiniteMass())
{
Expand Down
27 changes: 19 additions & 8 deletions demo/Falling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ void FallingDemo::SceneReset()
);
m_particles.back().SetVelocity(randDouble(), randDouble() - 5, randDouble());
m_particles.back().SetDamping(1.0f);
m_particles.back().SetMass(1e6);
}

//Create rigid bodies
Expand Down Expand Up @@ -238,15 +239,26 @@ void FallingDemo::SceneReset()
}
}

//Create forces
m_forces.push_back(std::make_unique<pegasus::ParticleGravity>(glm::dvec3{ 0, -9.8, 0 }));

//Register forces
for (auto& particle : m_particles)
{
m_forceRegistry.Add(particle, *m_forces.front());
m_forces.push_back(std::make_unique<pegasus::ParticleGravity>(particle));

for(auto& p : m_particles)
{
if (&p != &particle)
m_forceRegistry.Add(p, *m_forces.back());
}
}

//Create forces
//m_forces.push_back(std::make_unique<pegasus::ParticleStaticField>(glm::dvec3{ 0, -9.8, 0 }));

//Register forces
//for (auto& particle : m_particles)
//{
// m_forceRegistry.Add(particle, *m_forces.front());
//}

//Create contact generators
for (auto& body : m_rigidBodies)
{
Expand All @@ -255,7 +267,7 @@ void FallingDemo::SceneReset()
}

//Create plane particle and rigid body
m_particles.emplace_front();
/*m_particles.emplace_front();
m_particles.front().SetPosition({1, -position * 2, 0});
m_particles.front().SetInverseMass(0);
m_rigidBodies.emplace_front(
Expand All @@ -269,8 +281,7 @@ void FallingDemo::SceneReset()
AddCube({ position * 2, position, 0 }, boxSide);
AddCube({ -position * 2, position, 0 }, boxSide);
AddCube({ 0, position, -position * 2 }, boxSide);

AddBoundingVolumes();
AddBoundingVolumes();*/

activeObject = m_rigidBodies.end();
std::advance(activeObject, -1);
Expand Down

0 comments on commit 4e607a2

Please sign in to comment.