-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctionsCloth.cpp
97 lines (86 loc) · 3.39 KB
/
FunctionsCloth.cpp
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
#include "ClassCloth.h"
void Cloth::updateAllAccelerations() {
for (int i = 0; i < this->height; i++) {
for (int j = 0; j < this->width; j++) {
Particle* ptrP = this->TABparticles[i][j];
if (ptrP != nullptr) {
ptrP->updateAcceleration(this->gravity);
}
}
return;
}
}
void Cloth::simulateVerlet(float dt) {
for (int i = 0; i < this->height; i++) {
for (int j = 0; j < this->width; j++) {
Particle *ptrP = this->TABparticles[i][j];
if (ptrP != nullptr) {
ptrP->updateVelocity(dt);
ptrP->updateAcceleration(this->gravity);
ptrP->updateVerlet(dt);
}
}
}
return;
}
//
void Cloth::JakobsenMethod() {
for (int i = 0; i < this->height; i++) {
for (int j = 0; j < this->width; j++) {
Particle* ptrP = this->TABparticles[i][j];
if (ptrP != nullptr) {
for (auto nearestP: ptrP->nearestParticles) {
if (nearestP != nullptr) {
sf::Vector2f delta_pos = nearestP->pos - ptrP->pos;
float current_distance = sqrt(delta_pos.x * delta_pos.x + delta_pos.y * delta_pos.y);
sf::Vector2f direction = delta_pos / current_distance;
float deltaDistance = current_distance - this->distance;
if ((deltaDistance > 0) && (deltaDistance >= this->distance*0.75)) {
ptrP->cutTwoParticles(nearestP);
continue;
}
sf::Vector2f correction = direction * (deltaDistance / 2.0f);
if (!ptrP->moving && nearestP->moving) {
nearestP->pos -= correction * 2.0f;
}
if (ptrP->moving && !nearestP->moving) {
ptrP->pos += correction * 2.0f;
}
if (ptrP->moving && nearestP->moving) {
ptrP->pos += correction;
nearestP->pos -= correction;
}
}
}
}
}
}
return;
}
void Cloth::suppParticle(sf::Vector2f mousePos) {
bool stop = false;
for (int i = 0; i < this->height; i++) {
for (int j = 0; j < this->width; j++) {
if (stop) break;
Particle* ptrP = this->TABparticles[i][j];
if (ptrP != nullptr) {
if ((mousePos.x + 5.0 > ptrP->pos.x) && (mousePos.x - 5.0 < ptrP->pos.x)) {
if ((mousePos.y + 5.0 > ptrP->pos.y) && (mousePos.y - 5.0 < ptrP->pos.y)) {
for (auto pNearest: this->TABparticles[i][j]->nearestParticles) {
if (pNearest == nullptr) continue;
for (auto &p: pNearest->nearestParticles) {
Particle** ptrp = &p;
if (p == ptrP) {
p = nullptr;
}
}
}
this->TABparticles[i][j]= nullptr;
stop = true;
break;
}
}
}
}
}
}