Repository navigation
Expand file tree
/
Copy pathclothParticle.cpp
More file actions
130 lines (108 loc) · 2.82 KB
/
Copy pathclothParticle.cpp
File metadata and controls
130 lines (108 loc) · 2.82 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
// Thanks to "Advanced Character Physics" written by Thomas Jakobsen
#include "clothParticle.h"
ClothParticle::ClothParticle(bool fixed){
_mass = 1;
_position = Vec3f(0.0);
_prevPosition = Vec3f(0.0);
_velocity = Vec3f(0.0);
_isFixed = fixed;
_acc = Vec3f(0.0);
// _state = new Vec3f[2];
// _forces = Vec3f(0.0);
// updateState();
}
ClothParticle::ClothParticle(float mass, const Vec3f Position, const Vec3f Velocity, bool fixed){
_mass = mass;
_position = Vec3f(Position);
_velocity = Velocity;
_isFixed = fixed;
_acc = Vec3f(0.0);
_prevPosition = Vec3f(0.0);
// _state = new Vec3f[2];
// _forces = Vec3f(0.0);
// updateState();
}
ClothParticle::ClothParticle(const Vec3f Position, const Vec3f Velocity, bool fixed)
{
_mass = 1.0;
_position = Vec3f(Position);
_velocity = Velocity;
_isFixed = fixed;
_acc = Vec3f(0.0);
_prevPosition = Vec3f(0.0);
// _state = new Vec3f[2];
// _forces = Vec3f(0.0);
// updateState();
}
ClothParticle::ClothParticle(const Vec3f Position, bool fixed)
{
_mass = 1.0;
_position = Vec3f(Position);
_velocity = Vec3f(0.0);
_isFixed = fixed;
_acc = Vec3f(0.0);
_prevPosition = Vec3f(0.0);
// _state = new Vec3f[2];
// _forces = Vec3f(0.0);
// updateState();
}
void ClothParticle::drawClothParticle() {
glPointSize(3.0f); // when something get checked, enable it
glBegin(GL_POINTS);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(_position[0], _position[1], _position[2]);
glEnd();
}
// void ClothParticle::addForce(Vec3f force)
// {
// _forces += force;
// }
// Vec3f* ClothParticle::getState()
// {
// return _state;
// }
// void ClothParticle::setState(Vec3f p, Vec3f v)
// {
// _position = p;
// // _position[0] = 0.0;
// // _position[2] = 0.0;
// _velocity = v;
// updateState();
// }
// void ClothParticle::updateState()
// {
// _state[0] = _position;
// _state[1] = _velocity;
// // cout << "velocity is: " << _velocity[0] << ", " << _velocity[1] << ", " << _velocity[2] << endl;
// }
void ClothParticle::setPosition(Vec3f p)
{
_position = p;
// updateState();
}
void ClothParticle::setVelocity(Vec3f v)
{
_velocity = v;
// updateState();
}
void ClothParticle::derivEval(float deltaT, Vec3f forces)
{
Vec3f new_position = _velocity * deltaT;
Vec3f new_velocity = forces/_mass * deltaT;
_position += new_position;
_velocity += new_velocity;
// Vec3f derivative[2] = {_velocity, (_forces/_mass)};
// cout << _velocity[0] << ", " << _velocity[1] << ", " << _velocity[2] << endl;
// _forces = Vec3f(0.0);
// cout << _forces[1] << endl;
// return derivative;
}
void ClothParticle::update(float t) {
if(!_isFixed) {
Vec3f old_pos = _position;
_position = _position + (_position - _prevPosition) * (1.0 - 0.01) + _acc * (t * t);
// _position = _position + (1.0 - 0.02) * (_position - _prevPosition) + _acc * t;
_prevPosition = old_pos;
_acc = Vec3f(0.0);
}
}