Commit 0476fd60 authored by Georg Schaefer's avatar Georg Schaefer
Browse files

add some basic particle stuff

parent 7c9bf674
#ifndef __GDW_FORCE_PLANE_HPP__
#define __GDW_FORCE_PLANE_HPP__
#include <glm/glm.hpp>
#include <particle/modules/particle_module.hpp>
namespace gdw {
class entity;
class particle_emitter;
}
namespace gdw {
class force_plane : public particle_module {
private:
glm::vec3 position_;
glm::vec3 normal_;
float force_;
float reach_;
bool relative_;
public:
force_plane(entity& owner, const glm::vec3& position, const glm::vec3& normal, float force, float reach, bool relative) noexcept;
~force_plane() = default;
void update(float delta_time, particle_emitter& emitter);
static unsigned int priority() noexcept {
return 1;
}
};
}
#endif
#ifndef __GDW_PARTICLE_CIRCLE_HPP__
#define __GDW_PARTICLE_CIRCLE_HPP__
#include <random>
#include <glm/glm.hpp>
#include <particle/modules/particle_shape.hpp>
namespace gdw {
class entity;
}
namespace gdw {
class particle_circle : public particle_shape {
private:
float radius_;
mutable std::uniform_real_distribution<float> real_distribution_;
public:
particle_circle(entity& owner, const glm::vec3& position, const glm::quat& rotation, float radius) noexcept;
~particle_circle() = default;
glm::vec3 point() const noexcept;
glm::vec3 bounding_point() const noexcept;
private:
static std::default_random_engine& generator() noexcept {
static std::default_random_engine dre;
return dre;
}
};
}
#endif
......@@ -5,7 +5,9 @@
#include <particle/particle_component.hpp>
#include <particle/particle_system.hpp>
#include <particle/modules/force_field.hpp>
#include <particle/modules/force_plane.hpp>
#include <particle/modules/initial_movement.hpp>
#include <particle/modules/particle_circle.hpp>
#include <particle/modules/particle_emitter.hpp>
#include <particle/modules/particle_plane.hpp>
#include <particle/modules/particle_sphere.hpp>
......@@ -104,11 +106,11 @@ namespace gdw {
auto &player_move = player.emplace_back<movement_component>();
player_move.set_damping(5.f);
auto& pc = player.emplace_back<particle_component>();
pc.add_emitter(make_unique<particle_plane>(player, glm::vec3(0.f), glm::angleAxis(0.f, glm::vec3(0.f)), glm::vec2(5.f, 0.f), glm::vec2(0.f, 5.f)),
true, true, "texture/particle/dummy.dds", glm::vec2(0.5f), blend_mode::alpha, 2.f, 0.05f, 10);
pc.add_emitter(make_unique<particle_sphere>(player, glm::vec3(0.f), 10.f),
true, true, "texture/particle/dummy.dds", glm::vec2(0.5f), blend_mode::alpha, 2.f, 0.5f, 10);
pc.add_module<initial_movement>(glm::vec3(0.f, 2.f, 0.f), glm::vec3(0.f, 0.5f, 0.2f), true);
pc.add_emitter(make_unique<particle_circle>(player, glm::vec3(0.f), glm::angleAxis(glm::radians(90.f), glm::vec3(0.f, 0.f, 1.f)), 2.f),
true, true, "texture/particle/dummy.dds", glm::vec2(0.5f), blend_mode::alpha, 2.f, 0.05f, 30);
/*pc.add_emitter(make_unique<particle_sphere>(player, glm::vec3(0.f), 10.f),
true, true, "texture/particle/dummy.dds", glm::vec2(0.5f), blend_mode::alpha, 2.f, 0.5f, 10);*/
pc.add_module<force_plane>(glm::vec3(0.f), glm::vec3(1.f, 0.f, 0.f), 0.5f, 5.f, true);
player_ = player.id();
......
#include <iostream>
#include <glm/gtx/string_cast.hpp>
#include <ecs/entity.hpp>
#include <particle/modules/force_plane.hpp>
#include <particle/modules/particle_emitter.hpp>
namespace gdw {
force_plane::force_plane(entity& owner, const glm::vec3& position, const glm::vec3& normal, float force, float reach, bool relative) noexcept
: particle_module(owner), position_(position), normal_(normal), force_(force), reach_(reach), relative_(relative) {}
void force_plane::update(float delta_time, particle_emitter& emitter) {
auto model = owner_.transform();
auto position = glm::vec3(model * glm::vec4(position_, 1.f));
auto rotation = glm::toMat4(owner_.rotation());
auto normal = glm::vec3(rotation * glm::vec4(normal_, 0.f));
auto& particles = emitter.particles();
auto p_dot_n = glm::dot(position, normal);
auto q_dot_n = glm::dot(position + normal * reach_, normal);
for (auto& particle : particles) {
auto x = particle.position;
auto x_dot_n = glm::dot(x, normal);
if (x_dot_n >= p_dot_n) {
if (x_dot_n <= q_dot_n) {
particle.acceleration = normal * (force_ / particle.mass);
}
}
}
}
}
#include <glm/gtx/string_cast.hpp>
#include <ecs/entity.hpp>
#include <particle/modules/particle_circle.hpp>
#include <util/logger.hpp>
namespace gdw {
particle_circle::particle_circle(entity& owner, const glm::vec3& position, const glm::quat& rotation, float radius) noexcept
: particle_shape(owner, position, rotation, glm::vec3(1.f)), radius_(radius), real_distribution_(0.f, 1.f) {}
glm::vec3 particle_circle::point() const noexcept {
const static auto pi_two = glm::pi<float>() * 2.f;
auto lambda = real_distribution_(generator());
auto sigma = real_distribution_(generator());
auto point = glm::vec4(sigma * radius_ * glm::cos(lambda), 0.f, sigma * radius_ * glm::sin(lambda), 1.f);
return glm::vec3(transform() * point);
}
glm::vec3 particle_circle::bounding_point() const noexcept {
const static auto pi_two = glm::pi<float>() * 2.f;
auto lambda = real_distribution_(generator());
auto point = glm::vec4(radius_ * glm::cos(lambda), 0.f, radius_ * glm::sin(lambda), 1.f);
return glm::vec3(transform() * point);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment