/
PhysicsElastic.cpp
84 lines (69 loc) · 1.95 KB
/
PhysicsElastic.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
#include "PhysicsElastic.h"
namespace Physics
{
std::list<Elastic*> Elastic::List;
Elastic::Elastic(Vertex* P1, Vertex* P2,
float Length, float Spring) :
Constraint(P1, P2, Length),
mySpring(Spring)
{
Elastic::List.push_back(this);
}
Elastic::Elastic(Vertex &P1, Vertex &P2,
float Length, float Spring) :
Constraint(P1, P2, Length),
mySpring(Spring)
{
Elastic::List.push_back(this);
}
Elastic::~Elastic()
{
Elastic::List.remove(this);
}
void Elastic::ResolveAll()
{
for(std::list<Elastic*>::iterator ite = Elastic::List.begin();
ite != Elastic::List.end(); ite++)
/*if ((*ite)->GetVector().GetLength()>10.f*(*ite)->myLength)
{
Elastic* E=(*ite);
ite--;
delete <#expression#>
}
else*/
(*ite)->Resolve();
}
void Elastic::DeleteAll()
{
while (Elastic::List.size()>0)
delete (Elastic::List.front());
}
void Elastic::Resolve()
{
// Vecteur P1P2
Vec2 Vect = P2->GetPosition() - P1->GetPosition();
// Précalcul de la distance P1P2 - Peut être optimisée par une approximation
float acLength = Vect.GetLength();
// Loi de Hooke
float factor = mySpring*(acLength - myLength);
// Rapport entre les masses : Si !=0.5, l'un des points sera moins enclin à bouger
float MassFactor = P1->GetMass()/(P1->GetMass()+P2->GetMass());
// Normalisation du vecteur (pas besoin de GetNormalized(), on a déjà acLength)
Vect = Vect/acLength;
// Si l'un des points est fixe, toute la force est appliquée à l'autre
P2->ApplyForce(-Vect*factor*(P1->IsFixe()?MassFactor*1:1)),
P1->ApplyForce(Vect*factor*(P2->IsFixe()?(1-MassFactor):1));
}
void Elastic::glDraw()
{
glPushMatrix();
glLoadIdentity();
glBegin(GL_LINES);
// Plus la contraite est forte, plus le lien est rouge
glColor3f(std::abs(myLength - GetVector().GetLength())*mySpring*0.5f/myLength, 0.f, 0.f);
glVertex2f(P1->GetPosition().x, P1->GetPosition().y);
glVertex2f(P2->GetPosition().x, P2->GetPosition().y);
glEnd();
glPopMatrix();
}
}