forked from cleroy8/PositionBasedDynamics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.h
More file actions
112 lines (81 loc) · 3.58 KB
/
Copy pathcontext.h
File metadata and controls
112 lines (81 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#ifndef CONTEXT_H
#define CONTEXT_H
#include "StaticConstraint.h"
#include "DynamicConstraint.h"
#include "particle.h"
#include "planecollider.h"
#include "spherecollider.h"
#include "boxcollider.h"
#include "ParticleLink.h"
#include <vector>
#include <cmath>
class Context
{
public:
Context();
/// push new Particle to the vector
void addParticle(const Particle particle);
/// get number of Particles present in the current context
const int getNbParticles() const;
/// return std::vector of all particles listed in the context.
const std::vector<std::shared_ptr<Particle>>& getParticles() const;
/**
* @brief getColliders
* @return std::vector of Colliders, whether Plane, Spherical, of Rectangular, using std::variant.
*/
const std::vector<std::variant<PlaneCollider, SphereCollider, BoxCollider>> getColliders() const;
/// get whether the particles can be destroyed.
const bool getCollisionsToggle() const ;
void setNbUpdates(int n);
/// updates all particles and their attributes for the next iteration.
void updatePhysicalSystem(const float dt);
std::vector<DynamicConstraint>& getDynamicConstraints();
/// Destroys particles after too many collisions.
void destroyParticles();
/// Reset the context (remove all particles).
void reset();
void changeCollisions();
/**
* @brief setSelectedParticle set selected particle to particle that contains pos.
* @param pos
*/
void setSelectedParticle(Vec2 pos);
void linkSelectedParticles();
void removeLinksWithDeletedParticles();
private:
std::vector<std::shared_ptr<Particle>> particles;
std::vector<std::variant<PlaneCollider,SphereCollider, BoxCollider>> colliders;
std::vector<StaticConstraint> staticConstraints;
std::vector<DynamicConstraint> dynamicConstraints;
std::vector<ParticleLink> particleLinks;
std::shared_ptr<Particle> selectedParticles[2] = {nullptr, nullptr};
bool collisions;
int nbUpdates;
/// Sets velocity of each particle according to external forces implemented herein.
void applyExternalForce(const float dt);
/// For each particle, sets position to new expected position and velocity to the difference between current position and previous position.
void updateVelocityAndPosition(const float dt);
/// Sets expected position of each particle according to its velocity.
void updateExpectedPosition(const float dt);
/**
* @brief Add contact constraints between particles and static environment obstacles when expected positions make them overlap,
* e.g. Box colliders and spherical colliders.
*/
void addStaticContactConstraints();
/// Add contact constraints between particles when expected positions make them overlap.
void addDynamicContactConstraints();
/**
* @brief Check if input part1 and part2 are in contact according to their expected positions
* and return DynamicConstraint if so.
* @param part1, part2 Particle& input
* @returns std::optional value containing DynamicContact if contact is detected.
*/
std::optional<DynamicConstraint> checkDynamicContact(std::shared_ptr<Particle> part1, std::shared_ptr<Particle> part2);
/// Calculate new positions of each particle according to all constraints.
void projectConstraints();
/// Calculate friction applied to each particle and updates its velocity accordingly.
void applyFriction(const float dt);
/// delete all contact constraints from the context, both static and dynamic.
void deleteContactConstraints();
};
#endif // CONTEXT_H