ParticleSystemSolver2.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: ParticleSystemSolver2.h
3 > Project Name: CubbyFlow
4 > Author: Chan-Ho Chris Ohk
5 > Purpose: Basic 2-D particle system solver.
6 > Created Time: 2017/05/20
7 > Copyright (c) 2018, Chan-Ho Chris Ohk
8 *************************************************************************/
9 #ifndef CUBBYFLOW_PARTICLE_SYSTEM_SOLVER2_H
10 #define CUBBYFLOW_PARTICLE_SYSTEM_SOLVER2_H
11 
17 #include <Core/Utils/Constants.h>
18 #include <Core/Vector/Vector2.h>
19 
20 namespace CubbyFlow
21 {
35  {
36  public:
37  class Builder;
38 
41 
43  ParticleSystemSolver2(double radius, double mass);
44 
46  virtual ~ParticleSystemSolver2();
47 
49  double GetDragCoefficient() const;
50 
59  void SetDragCoefficient(double newDragCoefficient);
60 
62  double GetRestitutionCoefficient() const;
63 
73  void SetRestitutionCoefficient(double newRestitutionCoefficient);
74 
76  const Vector2D& GetGravity() const;
77 
79  void SetGravity(const Vector2D& newGravity);
80 
90 
92  const Collider2Ptr& GetCollider() const;
93 
95  void SetCollider(const Collider2Ptr& newCollider);
96 
98  const ParticleEmitter2Ptr& GetEmitter() const;
99 
101  void SetEmitter(const ParticleEmitter2Ptr& newEmitter);
102 
104  const VectorField2Ptr& GetWind() const;
105 
114  void SetWind(const VectorField2Ptr& newWind);
115 
117  static Builder GetBuilder();
118 
119  protected:
121  void OnInitialize() override;
122 
124  void OnAdvanceTimeStep(double timeStepInSeconds) override;
125 
127  virtual void AccumulateForces(double timeStepInSeconds);
128 
130  virtual void OnBeginAdvanceTimeStep(double timeStepInSeconds);
131 
133  virtual void OnEndAdvanceTimeStep(double timeStepInSeconds);
134 
136  void ResolveCollision();
137 
140  void ResolveCollision(
141  ArrayAccessor1<Vector2D> newPositions,
142  ArrayAccessor1<Vector2D> newVelocities);
143 
145  void SetParticleSystemData(const ParticleSystemData2Ptr& newParticles);
146 
147  private:
148  double m_dragCoefficient = 1e-4;
149  double m_restitutionCoefficient = 0.0;
150  Vector2D m_gravity = Vector2D(0.0, GRAVITY);
151 
152  ParticleSystemData2Ptr m_particleSystemData;
153  ParticleSystemData2::VectorData m_newPositions;
154  ParticleSystemData2::VectorData m_newVelocities;
155  Collider2Ptr m_collider;
156  ParticleEmitter2Ptr m_emitter;
157  VectorField2Ptr m_wind;
158 
159  void BeginAdvanceTimeStep(double timeStepInSeconds);
160 
161  void EndAdvanceTimeStep(double timeStepInSeconds);
162 
163  void AccumulateExternalForces();
164 
165  void TimeIntegration(double timeStepInSeconds);
166 
167  void UpdateCollider(double timeStepInSeconds) const;
168 
169  void UpdateEmitter(double timeStepInSeconds) const;
170  };
171 
173  using ParticleSystemSolver2Ptr = std::shared_ptr<ParticleSystemSolver2>;
174 
178  template <typename DerivedBuilder>
180  {
181  public:
183  DerivedBuilder& WithRadius(double radius);
184 
186  DerivedBuilder& WithMass(double mass);
187 
188  protected:
189  double m_radius = 1e-3;
190  double m_mass = 1e-3;
191  };
192 
193  template <typename T>
195  {
196  m_radius = radius;
197  return static_cast<T&>(*this);
198  }
199 
200  template <typename T>
202  {
203  m_mass = mass;
204  return static_cast<T&>(*this);
205  }
206 
211  public ParticleSystemSolverBuilderBase2<ParticleSystemSolver2::Builder>
212  {
213  public:
216 
219  };
220 }
221 
222 #endif
virtual void AccumulateForces(double timeStepInSeconds)
Accumulates forces applied to the particles.
void SetCollider(const Collider2Ptr &newCollider)
Sets the collider.
void SetGravity(const Vector2D &newGravity)
Sets the gravity.
Base class for particle-based solver builder.
Definition: ParticleSystemSolver2.h:179
void ResolveCollision()
Resolves any collisions occurred by the particles.
virtual void OnEndAdvanceTimeStep(double timeStepInSeconds)
Called after a time-step is completed.
const ParticleEmitter2Ptr & GetEmitter() const
Returns the emitter.
static Builder GetBuilder()
Returns builder fox ParticleSystemSolver2.
Front-end to create ParticleSystemSolver2 objects step by step.
Definition: ParticleSystemSolver2.h:210
std::shared_ptr< ParticleSystemSolver2 > ParticleSystemSolver2Ptr
Shared pointer type for the ParticleSystemSolver2.
Definition: ParticleSystemSolver2.h:173
std::shared_ptr< Collider2 > Collider2Ptr
Shared pointer type for the Collider2.
Definition: Collider2.h:112
double GetRestitutionCoefficient() const
Gets the restitution coefficient.
std::shared_ptr< ParticleEmitter2 > ParticleEmitter2Ptr
Shared pointer for the ParticleEmitter2 type.
Definition: ParticleEmitter2.h:71
virtual ~ParticleSystemSolver2()
Destructor.
const VectorField2Ptr & GetWind() const
Returns the wind field.
void OnAdvanceTimeStep(double timeStepInSeconds) override
Called to advance a single time-step.
constexpr double GRAVITY
Gravity.
Definition: Constants.h:156
void SetRestitutionCoefficient(double newRestitutionCoefficient)
Sets the restitution coefficient.
void SetEmitter(const ParticleEmitter2Ptr &newEmitter)
Sets the emitter.
void OnInitialize() override
Initializes the simulator.
std::shared_ptr< ParticleSystemData2 > ParticleSystemData2Ptr
Shared pointer type of ParticleSystemData2.
Definition: ParticleSystemData2.h:250
double m_mass
Definition: ParticleSystemSolver2.h:190
1-D array class.
Definition: Array1.h:29
Abstract base class for physics-based animation.
Definition: PhysicsAnimation.h:22
Definition: pybind11Utils.h:24
ParticleSystemSolver2Ptr MakeShared() const
Builds shared pointer of ParticleSystemSolver2 instance.
Vector2< double > Vector2D
Double-type 2D vector.
Definition: Vector2.h:341
double m_radius
Definition: ParticleSystemSolver2.h:189
ParticleSystemSolver2 Build() const
Builds ParticleSystemSolver2.
Basic 2-D particle system solver.
Definition: ParticleSystemSolver2.h:34
void SetWind(const VectorField2Ptr &newWind)
Sets the wind.
const ParticleSystemData2Ptr & GetParticleSystemData() const
Returns the particle system data.
std::shared_ptr< VectorField2 > VectorField2Ptr
Shared pointer for the VectorField2 type.
Definition: VectorField2.h:44
void SetParticleSystemData(const ParticleSystemData2Ptr &newParticles)
Assign a new particle system data.
const Collider2Ptr & GetCollider() const
Returns the collider.
1-D array accessor class.
Definition: ArrayAccessor1.h:28
virtual void OnBeginAdvanceTimeStep(double timeStepInSeconds)
Called when a time-step is about to begin.
void SetDragCoefficient(double newDragCoefficient)
Sets the drag coefficient.
2-D vector class.
Definition: Vector2.h:26
const Vector2D & GetGravity() const
Returns the gravity.
DerivedBuilder & WithRadius(double radius)
Returns builder with particle radius.
Definition: ParticleSystemSolver2.h:194
DerivedBuilder & WithMass(double mass)
Returns builder with mass per particle.
Definition: ParticleSystemSolver2.h:201
ParticleSystemSolver2()
Constructs an empty solver.
double GetDragCoefficient() const
Returns the drag coefficient.