Line data Source code
1 : #ifndef __PARTICLES_HPP__
2 : #define __PARTICLES_HPP__
3 :
4 : #include "core/data/FileReader.hpp"
5 : #include "core/data/IFileReader.hpp"
6 :
7 : #include "core/math/Glm.hpp"
8 :
9 : #include <memory>
10 : #include <vector>
11 : #include <string>
12 :
13 : namespace Soldank
14 : {
15 : class Particle
16 : {
17 : public:
18 : Particle() = default;
19 :
20 : Particle(bool _active,
21 : glm::vec2 _position,
22 : glm::vec2 _old_position,
23 : glm::vec2 _velocity,
24 : glm::vec2 force,
25 : float one_over_mass,
26 : float timestep,
27 : float gravity,
28 : float e_damping,
29 : float v_damping);
30 :
31 : void Euler();
32 : void Verlet();
33 :
34 0 : glm::vec2 GetVelocity() const { return velocity_; }
35 : void SetVelocity(glm::vec2 new_velocity) { velocity_ = new_velocity; }
36 0 : glm::vec2 GetForce() const { return force_; }
37 0 : void SetForce(glm::vec2 new_force) { force_ = new_force; }
38 0 : float GetOneOverMass() const { return one_over_mass_; }
39 :
40 : bool active{ false };
41 : glm::vec2 position{};
42 : glm::vec2 old_position{};
43 : glm::vec2 velocity_{};
44 :
45 : private:
46 : glm::vec2 force_{};
47 : float one_over_mass_{};
48 : float timestep_{};
49 : float gravity_{};
50 : float e_damping_{};
51 : float v_damping_{};
52 : };
53 :
54 : struct Constraint
55 : {
56 : bool active;
57 : glm::uvec2 particle_num;
58 : float rest_length;
59 : };
60 :
61 : enum class ParticleSystemType : unsigned int
62 : {
63 : Soldier = 0,
64 : Flag,
65 : Weapon,
66 : Kit,
67 : Parachute,
68 : StationaryGun
69 : };
70 :
71 : class ParticleSystem
72 : {
73 : public:
74 : ParticleSystem(const std::vector<Particle>& particles,
75 : const std::vector<Constraint>& constraints);
76 :
77 : void DoVerletTimestep();
78 : void DoVerletTimestepFor(unsigned int particle_num, unsigned int constraint_num);
79 : void DoEulerTimestep();
80 : void DoEulerTimestepFor(unsigned int particle_num);
81 : void SatisfyConstraints();
82 : void SatisfyConstraintFor(unsigned int constraint_num);
83 : static void SatisfyConstraint(const Constraint& constraint, std::vector<Particle>& particles);
84 :
85 : static std::shared_ptr<ParticleSystem> Load(ParticleSystemType particle_system_type,
86 : float scale = 4.5F,
87 : const IFileReader& file_reader = FileReader());
88 :
89 0 : bool GetActive(unsigned int particle_num) const
90 : {
91 : // TODO: indexes are from 1 like in pascal, we need to change it at loading time to be
92 : // indexed from 0
93 0 : return particles_[particle_num - 1].active;
94 : }
95 :
96 0 : const glm::vec2& GetPos(unsigned int particle_num) const
97 : {
98 0 : return particles_[particle_num - 1].position;
99 : }
100 :
101 0 : void SetPos(unsigned int particle_num, glm::vec2 new_pos)
102 : {
103 0 : particles_[particle_num - 1].position = new_pos;
104 0 : }
105 :
106 0 : const glm::vec2& GetOldPos(unsigned int particle_num) const
107 : {
108 0 : return particles_[particle_num - 1].old_position;
109 : }
110 :
111 0 : void SetOldPos(unsigned int particle_num, glm::vec2 new_old_pos)
112 : {
113 0 : particles_[particle_num - 1].old_position = new_old_pos;
114 0 : }
115 :
116 0 : glm::vec2 GetForce(unsigned int particle_num)
117 : {
118 0 : return particles_[particle_num - 1].GetForce();
119 : }
120 :
121 0 : void SetForce(unsigned int particle_num, glm::vec2 new_force)
122 : {
123 0 : particles_[particle_num - 1].SetForce(new_force);
124 0 : }
125 :
126 0 : const std::vector<Particle>& GetParticles() const { return particles_; }
127 :
128 : const std::vector<Constraint>& GetConstraints() const { return constraints_; }
129 :
130 : private:
131 : static void ScaleParticles(ParticleSystem* particle_system, float scale);
132 : static std::shared_ptr<ParticleSystem> LoadFromFile(
133 : const std::string& file_name,
134 : float scale,
135 : float timestep,
136 : float gravity,
137 : float e_damping,
138 : float v_damping,
139 : const IFileReader& file_reader = FileReader());
140 :
141 : std::vector<Particle> particles_;
142 : std::vector<Constraint> constraints_;
143 : };
144 : } // namespace Soldank
145 :
146 : #endif
|