-
Notifications
You must be signed in to change notification settings - Fork 0
/
Constraints.h
226 lines (204 loc) · 7.21 KB
/
Constraints.h
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#ifndef CONSTRAINTS_H
#define CONSTRAINTS_H
#include "Object.h"
#include "MemoryAllocator.h"
class Force {
public:
virtual bool Linking(Object *object) = 0;
virtual void Apply(double T) = 0;
virtual void Redraw() {}
};
class Spring : public Force {
public :
double strength, L;
RGB3f color;
double D;
double width;
double damping;
Object *objectA, *objectB;
Vector2D r0, r1;
Spring(Object *objectA, Vector2D r0, Object *objectB, Vector2D r1, double strength, double L = -1.0) :
objectA(objectA), r0(r0), objectB(objectB), r1(r1), strength(strength), L(L) {
color = RGB3f::RandomBrightColor();
if (L < 0) this->L = (objectA->transformToWorld(r0) - objectB->transformToWorld(r1)).GetLength();
damping = 150;
}
void Apply(double T) {
Vector2D p = objectA->transformToWorld(r0);
Vector2D q = objectB->transformToWorld(r1);
Vector2D n = p - q;
double l = n.GetLength();
n.Normalize();
Vector2D v = objectA->GetPointVelocity(p) - objectB->GetPointVelocity(q);
double v0 = n * v;
Vector2D impulse = (-damping * v0 * T + T * strength * (L - l)) * n.GetDirection();
objectA->ApplyImpulse(p, impulse);
objectB->ApplyImpulse(q, -impulse);
}
void Redraw() {
Vector2D p = objectA->GetTransformToWorld()(r0);
Vector2D q = objectB->GetTransformToWorld()(r1);
width = max(1.0, L / 100.0);
int n = 16;
Vector2D t = (p - q).GetDirection().GetRotate() * L / 12;
vector<Vector2D> points;
points.push_back(p);
for (int i = 0; i < n; i++) {
t = -t;
points.push_back(p + (q - p) * (i + 0.5)/ n + t);
}
points.push_back(q);
graphics.DrawLines(points, color, width);
graphics.DrawPoint(p.x, p.y, color, width * 4);
graphics.DrawPoint(q.x, q.y, color, width * 4);
}
bool Linking(Object *object) {
return objectA == object || objectB == object;
}
};
class Constraint {
public:
int type;
Constraint() {type = 0;}
virtual void ProcessVelocity() = 0;
virtual void ProcessPosition() = 0;
virtual Constraint *Copy() = 0;
virtual bool Linking(Object *object) = 0;
virtual void Redraw() {}
virtual ~Constraint() {};
};
class Contact : public Constraint{
public:
#define TYPE_CONTACT 1
Shape *shapeA, *shapeB;
Object *objectA, *objectB;
Vector2D p, n;
double restitution, friction;
double depth;
Contact(Shape *a, Shape *b, Vector2D p, Vector2D n, double depth) : shapeA(a), objectA(a->object), shapeB(b), objectB(b->object), p(p), n(n), depth(depth) {
restitution = sqrt(a->restitution * b->restitution);
friction = sqrt(a->friction * b->friction);
type = 1;
}
void PreprocessVelocity() {
Vector2D v10 = objectB->GetPointVelocity(p) - objectA->GetPointVelocity(p);
Vector2D r0 = p - objectA->position, r1 = p - objectB->position;
double v0 = -n * v10;
Vector2D tao = n.GetRotate();
double cRestitution;
if (v0 > 10) cRestitution = restitution;
else cRestitution = 0.0;
double J = ((1 + cRestitution) * v0) /
(objectA->invMass + objectB->invMass + sqr(r0 % n) * objectA->invInertia + sqr(r1 % n) * objectB->invInertia);
if (J < 0) return;
Vector2D impulse = J * n;
objectA->ApplyImpulse(p, -impulse);
objectB->ApplyImpulse(p, impulse);
if (settings.frictionSwitch) {
v10 = objectB->GetPointVelocity(p) - objectA->GetPointVelocity(p);
double j = -(v10 * tao) / (objectA->invMass + objectB->invMass + sqr(r0 % tao) * objectA->invInertia + sqr(r1 % tao) * objectB->invInertia);
j = max(min(j, friction * J), -friction * J);
Vector2D fImpulse = j * tao;
objectA->ApplyImpulse(p, -fImpulse);
objectB->ApplyImpulse(p, fImpulse);
}
}
void ProcessVelocity() {
Vector2D v10 = objectB->GetPointVelocity(p) - objectA->GetPointVelocity(p);
Vector2D r0 = p - objectA->position, r1 = p - objectB->position;
double v0 = -n * v10;
Vector2D tao = n.GetRotate();
double cRestitution;
if (v0 > 10) cRestitution = restitution;
else cRestitution = 0.0;
double J = ((1 + cRestitution) * v0) /
(objectA->invMass + objectB->invMass + sqr(r0 % n) * objectA->invInertia + sqr(r1 % n) * objectB->invInertia);
if (J < 0) return;
Vector2D impulse = J * n;
objectA->ApplyImpulse(p, -impulse);
objectB->ApplyImpulse(p, impulse);
if (settings.frictionSwitch) {
v10 = objectB->GetPointVelocity(p) - objectA->GetPointVelocity(p);
double j = -(v10 * tao) / (objectA->invMass + objectB->invMass + sqr(r0 % tao) * objectA->invInertia + sqr(r1 % tao) * objectB->invInertia);
j = max(min(j, friction * J), -friction * J);
Vector2D fImpulse = j * tao;
objectA->ApplyImpulse(p, -fImpulse);
objectB->ApplyImpulse(p, fImpulse);
}
}
void ProcessPosition() {
Vector2D r0 = p - objectA->position, r1 = p - objectB->position;
double correctiveJ = (0.5 * max(0.0, depth - 0.1) / timeInterval) /
(objectA->invMass + objectB->invMass + sqr(r0 % n) * objectA->invInertia + sqr(r1 % n) * objectB->invInertia);
if (correctiveJ > 0) {
objectA->ApplyCorrectiveImpulse(p, -correctiveJ * n, true);
objectB->ApplyCorrectiveImpulse(p, correctiveJ * n, true);
}
}
Constraint *Copy() {
return new Contact(*this);
}
bool Linking(Object *object) {
if (object == objectA || object == objectB) return true;
return false;
}
void *operator new(size_t _);
void operator delete(void *p, size_t _);
};
class DistanceConstraint : public Constraint {
private:
double L;
Object *objectA, *objectB;
Vector2D r0, r1;
public:
RGB3f color;
DistanceConstraint(Object *objectA, Vector2D r0, Object *objectB, Vector2D r1) :
L((objectA->transformToWorld(r0.GetPosition()) - objectB->transformToWorld(r1.GetPosition())).GetLength()),
objectA(objectA), r0(r0), objectB(objectB), r1(r1)
{
color = RGB3f::RandomBrightColor();
}
void ProcessVelocity() {
Vector2D p = objectA->transformToWorld(r0.GetPosition());
Vector2D q = objectB->transformToWorld(r1.GetPosition());
Vector2D n = q - p;
double l = n.GetLength();
if (abs(l - L) < 0.1) return;
n.Normalize();
double J;
double v = n * (objectB->GetPointVelocity(q) - objectA->GetPointVelocity(p));
J = v / (objectA->invMass + sqr(objectA->transformToWorld(r0) % n) * objectA->invInertia
+ objectB->invMass + sqr(objectB->transformToWorld(r1) % n) * objectB->invInertia);
objectA->ApplyImpulse(p, n * J);
objectB->ApplyImpulse(q, n * -J);
}
void ProcessPosition() {
Vector2D p = objectA->position + objectA->transformToWorld(r0);
Vector2D q = objectB->position + objectB->transformToWorld(r1);
Vector2D n = q - p;
double l = n.GetLength();
if (abs(l - L) < 0.1) return;
n.Normalize();
double J;
J = (0.6 / timeInterval * (l - L)) / (objectA->invMass + sqr(objectA->transformToWorld(r0) % n) * objectA->invInertia + objectB->invMass + sqr(objectB->transformToWorld(r1) % n) * objectB->invInertia);
objectA->ApplyCorrectiveImpulse(p, n * J);
objectB->ApplyCorrectiveImpulse(q, n * -J);
}
Constraint *Copy() {
return new DistanceConstraint(*this);
}
void Redraw() {
Vector2D p = objectA->position + objectA->transformToWorld(r0);
Vector2D q = objectB->position + objectB->transformToWorld(r1);
double width = max(1.0, L / 100.0);
graphics.DrawLine(p.x, p.y, q.x, q.y, color, width);
graphics.DrawPoint(p.x, p.y, color, width * 4);
graphics.DrawPoint(q.x, q.y, color, width * 4);
}
bool Linking(Object *object) {
if (object == objectA) return true;
if (object == objectB) return true;
return false;
}
};
#endif