-
Notifications
You must be signed in to change notification settings - Fork 0
/
world.cpp
366 lines (300 loc) · 7.88 KB
/
world.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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#include "stdafx.h"
#include "world.h"
#include <stdio.h>
#define atx(X) ox + (X) * ex
#define aty(Y) oy + (Y) * ey
float gravityScale = 5.0f;
Vector2 gravity( 0, 10.0f * gravityScale );
float fixed_dt = 1.0/60.; // avance del tiempo constante
World _world;
char status_bar[255];
World::World()
{
m_bodyCount = 0;
m_constraintCount = 0;
strcpy(status_bar,"");
time = 0;
}
World::~World()
{
Clear();
}
void World::Clear()
{
for(int i = 0; i < m_bodyCount; ++i)
{
delete bodies[i];
bodies[i] = NULL;
}
m_bodyCount = 0;
for(int i = 0; i < m_constraintCount ; ++i)
{
delete constraints[i];
constraints[i] = NULL;
}
m_constraintCount = 0;
}
void World::Update(float elapsed_time)
{
// Generate new collision info
m_contactCount = 0;
for(int i = 0; i < m_bodyCount-1; ++i)
{
Body *A = bodies[i];
for(int j = i + 1; j < m_bodyCount; ++j)
{
Body *B = bodies[j];
// si ambos cuerpos son estaticos, no pueden colisionar
if(A->inverseMass == 0 && B->inverseMass== 0)
continue;
// si Ambos cuerpos forman un joint tampoco pueden colisionar, aunque si lo pueden hacer en la vida real
// el solver no es estable con este tipo de configuracion
//if(is_joint(A,B))
//continue;
ContactSolver C;
if(C.BodyCollide(elapsed_time , A,B))
{
contacts[m_contactCount++] = C;
}
}
}
// Integrar fuerzas primer parte
for(int i = 0; i < m_bodyCount; ++i)
bodies[i]->IntegrateForces( elapsed_time/2. );
// Initialize collision: Precalculo la info de contactos
for(int i=0;i<m_contactCount;++i)
contacts[i].PreCalc();
// Solve collisions
for(int j = 0; j < 5; ++j)
{
for(int i = 0; i < m_contactCount; ++i)
contacts[i].ApplyImpulse(j);
}
// Integrar velocidades
for(int i = 0; i < m_bodyCount; ++i)
bodies[i]->IntegrateVelocity( elapsed_time );
// Integrar fuerzas segunda parte
for(int i = 0; i < m_bodyCount; ++i)
bodies[i]->IntegrateForces( elapsed_time/2. );
// Correct positions
for(int i = 0; i < m_contactCount; ++i)
{
contacts[i].PositionalCorrection( );
}
// Limpiar todas las fuerzas, pues ya fueron integradas
for(int i = 0; i < m_bodyCount; ++i)
{
bodies[i]->force = Vector2(0,0);
bodies[i]->torque = 0;
}
// Solve constrains
//for(int j = 0; j < 3; ++j)
for(int i = 0; i < m_constraintCount; ++i)
constraints[i]->Solve();
time += elapsed_time;
}
void World::Render( HDC hdc,int ox,int oy,float ex,float ey)
{
HFONT hfont = CreateFont(12,0,0,0,0,0,0,0,0,0,0,0,0,"Tahoma");
HFONT hfontOld = (HFONT)SelectObject(hdc,hfont);
for(int i = 0; i < m_bodyCount; ++i)
{
bodies[i]->Render(hdc,ox,oy,ex,ey);
if(false)
{
// info de la posicion
Vector2 pos = bodies[i]->position;
SetTextAlign(hdc,TA_CENTER);
char saux[255];
sprintf(saux,"%.2f",pos.y);
TextOutA(hdc, atx(pos.x) ,aty(pos.y),saux,strlen(saux));
}
}
HPEN hpen = CreatePen(PS_SOLID,1,RGB(64,0,0));
HPEN hpenOld = (HPEN)SelectObject(hdc,hpen);
HBRUSH hbrush = CreateSolidBrush(RGB(0,0,0));
HBRUSH hbrushOld = (HBRUSH)SelectObject(hdc,hbrush);
for(int i = 0; i < m_contactCount; ++i)
{
for(int j = 0; j < contacts[i].contact_count; ++j)
{
Vector2 c = contacts[i].contacts[j];
Vector2 n = contacts[i].normal;
Ellipse(hdc,atx(c.x)-5,aty(c.y)-5,atx(c.x)+5,aty(c.y)+5);
MoveToEx(hdc , atx(c.x) ,aty(c.y),NULL);
c = c + n;
LineTo(hdc , atx(c.x) ,aty(c.y));
}
}
HPEN hpen2 = CreatePen(PS_SOLID,2,RGB(192,192,255));
SelectObject(hdc,hpen2);
for(int i = 0; i < m_constraintCount; ++i)
{
switch(constraints[i]->visible)
{
case 1:
{
Vector2 c = constraints[i]->A->position + constraints[i]->A->shape->matWorld*constraints[i]->ptA;
MoveToEx(hdc , atx(c.x) ,aty(c.y),NULL);
c = constraints[i]->B->position + constraints[i]->B->shape->matWorld*constraints[i]->ptB;
LineTo(hdc , atx(c.x) ,aty(c.y));
}
break;
case 2:
{
Vector2 p0 = constraints[i]->A->position + constraints[i]->A->shape->matWorld*constraints[i]->ptA;
Vector2 p1 = constraints[i]->B->position + constraints[i]->B->shape->matWorld*constraints[i]->ptB;
Vector2 dir_v = p1-p0;
dir_v.Normalize();
Vector2 dir_w = dir_v.normal();
float rA = ((GeoCircle *)constraints[i]->A->shape)->radius;
float rB = ((GeoCircle *)constraints[i]->B->shape)->radius;
Vector2 Q[4];
Q[0] = p0 + dir_w*rA;
Q[1] = p1 + dir_w*rB;
Q[2] = p1 - dir_w*rB;
Q[3] = p0 - dir_w*rA;
POINT pt[5];
for(int t=0;t<4;++t)
{
pt[t].x = atx(Q[t].x);
pt[t].y = aty(Q[t].y);
}
pt[4] = pt[0];
Polygon(hdc,pt,5);
}
break;
}
}
/*
TextOutA(hdc,10,10,status_bar,strlen(status_bar));
char buffer[255];
float dW = bodies[0]->orient - bodies[1]->orient;
sprintf(buffer,"%10.1f",dW);
TextOutA(hdc,10,20,buffer,strlen(buffer));
*/
SelectObject(hdc,hpenOld);
DeleteObject(hpen);
DeleteObject(hpen2);
SelectObject(hdc,hfontOld);
DeleteObject(hfont);
SelectObject(hdc,hbrushOld);
DeleteObject(hbrush);
}
Body *World::AddCircle( float x, float y , float r)
{
Body *b = bodies[m_bodyCount++] = new Body();
b->CreateCircle(r,Vector2(x,y));
return b;
}
Body *World::AddBox( float x, float y , float w, float h)
{
Body *b = bodies[m_bodyCount++] = new Body();
b->CreateBox(w,h,Vector2(x,y));
return b;
}
Body *World::AddTri( float ax, float ay , float bx, float by ,float cx, float cy )
{
Body *b = bodies[m_bodyCount++] = new Body();
b->CreateTri(ax,ay,bx,by,cx,cy);
return b;
}
Body *World::AddPoly( Vector2 *pt,int cant_pt)
{
Body *b = bodies[m_bodyCount++] = new Body();
b->CreatePoly(pt,cant_pt);
return b;
}
Constraint *World::AddConstraint(Body *A,Body *B,float dist)
{
Constraint *c = constraints[m_constraintCount++] = new Constraint;
c->A = A;
c->B = B;
c->dist = dist;
// x defecto el pto se toma en el centro de gravedad
c->ptA = c->ptB = Vector2(0,0);
c->joint = false;
c->angle_constraint = false;
c->exact_distance = false;
c->visible = 1;
return c;
}
Constraint *World::AddDistanceConstraint(Body *A,Vector2 ptA , Body *B,Vector2 ptB)
{
Constraint *c = constraints[m_constraintCount++] = new Constraint;
c->A = A;
c->B = B;
c->ptA = ptA;
c->ptB = ptB;
Vector2 rA = A->position + A->shape->matWorld*ptA;
Vector2 rB = B->position + B->shape->matWorld*ptB;
c->dist = (rA - rB).Length();
c->joint = false;
c->angle_constraint = false;
c->exact_distance = true;
c->visible = 1;
return c;
}
Constraint *World::AddJointConstraint(Body *A,Body *B)
{
Constraint *c = constraints[m_constraintCount++] = new Constraint;
c->A = A;
c->B = B;
// x defecto el pivote se toma en el centro de gravedad
c->ptA = c->ptB = Vector2(0,0);
// y la distancia exacta que tienen
c->dist = (A->position - B->position).Length();
c->joint = true;
c->exact_distance = true;
c->angle_constraint = false;
c->visible = 2;
return c;
}
Constraint *World::AddAngleConstraint(Body *A,Body *B,Body *C)
{
Constraint *c = constraints[m_constraintCount++] = new Constraint;
c->A = A;
c->B = B;
c->C = C;
c->joint = false;
c->exact_distance = false;
c->angle_constraint = true;
// determino el angulo (cos angulo)
Vector2 L = A->position-B->position;
Vector2 R = C->position-B->position;
L.Normalize();
R.Normalize();
c->angle = acos(dot(L,R));
return c;
}
// devuelve si el pto esta cercano al centro de gravedad de un cuerpo
int World::PointNearBodyCM(Vector2 pt, float dE)
{
int rta = -1;
float min_dist = dE;
for(int i = 0;i<m_bodyCount;++i)
if(bodies[i]->inverseMass!=0) // solo se aplica a objetos dinamicos
{
float dist = (pt-bodies[i]->position).Length();
if(dist<min_dist)
{
rta = i;
min_dist = dist;
}
}
return rta;
}
// devuelve true si el cuerpo A y el B forman un joint
bool World::is_joint(Body *A,Body *B)
{
bool rta = false;
int i=0;
while(i<m_constraintCount && !rta)
{
if((constraints[i]->A==A && constraints[i]->B==B) || (constraints[i]->A==B && constraints[i]->B==A))
rta = true;
else
++i;
}
return rta;
}