-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.cpp
More file actions
146 lines (118 loc) · 4.32 KB
/
Copy pathcontext.cpp
File metadata and controls
146 lines (118 loc) · 4.32 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "context.h"
#include <stdio.h>
Context::Context() {}
const float K_DAMPING = 0.96;
void Context::updatePhysicalSystem(float dt) {
// Refer to Equations.pdf from the assignement for more details on the different steps
this->applyExternalForce(dt); // (5)
this->dampVelocities(); // (6)
this->updateExpectedPosition(dt); // (7)
//this->updateNeighbors(dt);
this->activeConstraints.clear();
//this->addFluidConstraints(dt);
this->addDynamicContactConstraints(); // (8)
this->addStaticContactConstraints(); // (8)
this->projectConstraints(); // (9-11)
this->updateVelocityAndPosition(dt); // (12-15)
this->applyFriction(dt); // (16)
}
void Context::applyExternalForce(float dt) {
for (Object& object: objects){
for (auto& particle: object.particles) {
// GRAVITY
if (this->isGravityOn) {
particle->appliedForces = 0.0003*Vec2{0, static_cast<float>(-particle->mass*9.81)};
}
particle->velocity = particle->velocity + (dt / particle->mass) * particle->appliedForces;
}
}
}
void Context::dampVelocities() {
for (Object& object: objects){
for (auto& particle: object.particles) {
particle->velocity = K_DAMPING*particle->velocity;
}
}
}
void Context::updateExpectedPosition(float dt) {
for (Object& object: objects){
for (auto& particle: object.particles) {
particle->expectedPos = particle->pos + dt * particle->velocity;
}
}
}
void Context::updateNeighbors() {
/*
for (Particle& particle_i : circles) {
for (Particle& particle_j : circles) {
if (length(particle_j.pos - particle_i.pos) < 50) {
particle_i.neighbors.push_back(&particle_j);
}
}
}
*/
}
void Context::addFluidConstraints() {
for (Object& object: objects) {
for (auto& particle_i : object.particles) {
auto constraint = std::make_unique<FluidConstraint>(particle_i.get());
if (constraint->isSatisfied()) {
addConstraint(std::move(constraint));
}
}
}
}
void Context::addDynamicContactConstraints() {
for (Object& object: objects) {
for (auto& particle_i : object.particles) {
for (const auto& particle_j : object.particles) {
if (&(*particle_j) == particle_i.get()) continue;
auto link = std::make_unique<LinkConstraint>(*particle_j, particle_i.get());
if (link->isSatisfied()) {
addConstraint(std::move(link));
}
}
for(const Object& otherObject: objects) {
if (&otherObject == &object) continue;
for (const auto& particle_j: otherObject.particles) {
auto constraint = std::make_unique<DynamicConstraint>(*particle_j, particle_i.get());
if (constraint->isSatisfied()) {
addConstraint(std::move(constraint));
particle_i->isActivated = true;
}
}
}
}
}
}
void Context::addStaticContactConstraints() {
for (auto& collider : colliders) {
for (Object& object: objects){
for (auto& particle: object.particles) {
auto constraint = std::make_unique<StaticConstraint>(*collider.get(), particle.get());
if (constraint->isSatisfied()) {
addConstraint(std::move(constraint));
particle->isActivated = true;
}
}
}
}
}
void Context::enforceConstraint(const Constraint& constraint, Particle& particle) {
particle.expectedPos = particle.expectedPos + constraint.getDelta();
}
void Context::projectConstraints() {
for (const auto& constraint : activeConstraints) {
enforceConstraint(*constraint, *constraint->particle);
}
}
void Context::updateVelocityAndPosition(float dt) {
for (Object& object: objects){
for (auto& particle: object.particles) {
particle->velocity = 1/dt * (particle->expectedPos - particle->pos);
particle->pos = particle->expectedPos;
}
}
}
void Context::applyFriction(float dt) {
}