-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollisionSystem.cpp
More file actions
768 lines (710 loc) · 23.6 KB
/
CollisionSystem.cpp
File metadata and controls
768 lines (710 loc) · 23.6 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
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
/*
* CollisionSystem.cpp
*
* Created on: 15/10/2013
* Author: stewart
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <cfloat>
#include "CollisionSystem.h"
#include "SculptObject.h"
int spheresPerModel = 24;
float gconstant = 0.000000067;
CollisionSystem::CollisionSystem(SculptObject** models) {
num_NonSunObjects = 18;
int i;
worldObjects = (Object*) malloc(sizeof(Object) * (num_NonSunObjects + 1));
Model* sphere = loadModel("sculpt_sphere.obj");
// Make collision models
collisionModels = new CollisionModel*[8];
collisionModels[0] = simpleSphereModel(sphere, 1.22698890045f);
for (i = 1; i < 8; i++) {
float scale = 1;
if (i >= 2 && i <= 4) {
scale = 0.25; // asteroid
} else if (i >= 5 && i <= 7) {
scale = 0.5; // planet
}
collisionModels[i] = multiSphereModel(models[i - 1], spheresPerModel,
scale);
}
// default values
for (i = 0; i <= num_NonSunObjects; i++) {
worldObjects[i].position.x = 0;
worldObjects[i].position.y = 0;
worldObjects[i].position.z = 0;
worldObjects[i].rotation.x = 0;
worldObjects[i].rotation.y = 0;
worldObjects[i].rotation.z = 0;
worldObjects[i].direction.x = 0;
worldObjects[i].direction.y = 0;
worldObjects[i].direction.z = 0;
worldObjects[i].speed = 0;
worldObjects[i].weight = 0;
worldObjects[i].displayModel = NULL;
worldObjects[i].collisionModel = NULL;
worldObjects[i].radius = 0;
}
// the sun
worldObjects[0].weight = 100000;
worldObjects[0].collisionModel = collisionModels[0]; // basic sphere not used anywhere else
// planets
worldObjects[1].position.x = -3.5;
worldObjects[1].position.y = -3.5;
worldObjects[1].displayModel = models[5];
worldObjects[1].collisionModel = collisionModels[6];
worldObjects[1].direction.x = -5;
worldObjects[1].direction.y = 5;
worldObjects[1].direction.z = 0;
normalize(&worldObjects[1].direction);
worldObjects[1].speed = orbitalVelocity(0, 1);
worldObjects[1].weight = 50;
worldObjects[2].position.x = 5;
worldObjects[2].position.y = -5;
worldObjects[2].displayModel = models[4];
worldObjects[2].collisionModel = collisionModels[5];
worldObjects[2].direction.x = -5;
worldObjects[2].direction.y = -5;
worldObjects[2].direction.z = 0;
normalize(&worldObjects[2].direction);
worldObjects[2].speed = orbitalVelocity(0, 2);
worldObjects[2].weight = 100;
worldObjects[3].position.x = 6.5;
worldObjects[3].position.y = 6.5;
worldObjects[3].displayModel = models[6];
worldObjects[3].collisionModel = collisionModels[7];
worldObjects[3].direction.x = 5;
worldObjects[3].direction.y = -5;
worldObjects[3].direction.z = 0;
normalize(&worldObjects[3].direction);
worldObjects[3].speed = orbitalVelocity(0, 3);
worldObjects[3].weight = 200;
// static meteors (collision system)
worldObjects[4].position.x = -8;
worldObjects[4].position.y = 8;
worldObjects[4].position.z = 8;
worldObjects[4].displayModel = models[1];
worldObjects[4].collisionModel = collisionModels[2];
worldObjects[4].direction.x = 1;
worldObjects[4].direction.y = -1;
normalize(&worldObjects[4].direction);
worldObjects[4].speed = 0.06;
worldObjects[4].weight = 2;
worldObjects[5].position.x = 8;
worldObjects[5].position.y = 0;
worldObjects[5].position.z = 8;
worldObjects[5].displayModel = models[2];
worldObjects[5].collisionModel = collisionModels[3];
worldObjects[5].direction.x = -1;
worldObjects[5].speed = 0.035;
worldObjects[5].weight = 2;
// meteor belt
int numInBelt = num_NonSunObjects - 5;
for (i = 6; i <= num_NonSunObjects; i++) {
worldObjects[i].position.x = cos(
(360.0 / numInBelt) * PI / 180 * (i - 6)) * 12;
worldObjects[i].position.y = sin(
(360.0 / numInBelt) * PI / 180 * (i - 6)) * 12;
worldObjects[i].position.z = 0;
worldObjects[i].weight = 0.01;
worldObjects[i].direction.x = 1;
if (worldObjects[i].position.y == 0) {
worldObjects[i].direction.x = 0;
worldObjects[i].direction.y = 1;
} else {
worldObjects[i].direction.y = -worldObjects[i].position.x
/ worldObjects[i].position.y;
}
worldObjects[i].direction.z = 0;
normalize(&(worldObjects[i].direction));
worldObjects[i].speed = orbitalVelocity(0, i);
int astModel = rand() % 3;
worldObjects[i].displayModel = models[astModel + 1];
worldObjects[i].collisionModel = collisionModels[astModel + 2];
}
// bounding spheres
for (i = 0; i <= num_NonSunObjects; i++) {
if (i == 0) {
worldObjects[i].radius = 2;
} else {
float radius = 0;
if (worldObjects[i].collisionModel->fullPolyModel != NULL) {
for (int j = 0;
j
< worldObjects[i].collisionModel->fullPolyModel->m_nNumPoint;
j++) {
if (magnitude(
&(worldObjects[i].collisionModel->fullPolyModel->m_pVertexArray[j]))
> radius) {
radius =
magnitude(
&(worldObjects[i].collisionModel->fullPolyModel->m_pVertexArray[j]));
}
}
} else {
for (int j = 0;
j
< worldObjects[i].collisionModel->fullPolyModelSculpt->m_nNumPoint;
j++) {
if (magnitude(
&(worldObjects[i].collisionModel->fullPolyModelSculpt->m_pVertexArray[j]))
> radius) {
radius =
magnitude(
&(worldObjects[i].collisionModel->fullPolyModelSculpt->m_pVertexArray[j]));
}
}
}
worldObjects[i].radius = radius;
}
}
}
float CollisionSystem::orbitalVelocity(int orbited, int orbiting) {
float dist = distanceCalcP(worldObjects[orbited].position,
worldObjects[orbiting].position);
return sqrt(
pow(worldObjects[orbited].weight, 2) * gconstant
/ (worldObjects[orbited].weight
+ worldObjects[orbiting].weight) / dist);
}
CollisionSystem::CollisionModel* CollisionSystem::simpleSphereModel(
CollisionSystem::Model* base, float scale) {
// determine full radius
float radius = 0;
for (int j = 0; j < base->m_nNumPoint; j++) {
if (magnitude(&(base->m_pVertexArray[j])) > radius) {
radius = magnitude(&(base->m_pVertexArray[j]));
}
}
CollisionModel* ret = new CollisionModel;
ret->fullPolyModel = base;
ret->fullPolyModelSculpt = NULL;
ret->sphereCount = 1;
ret->spheres = new Sphere[1];
ret->spheres[0].radius = radius * scale;
ret->spheres[0].relativePosition.x = 0;
ret->spheres[0].relativePosition.y = 0;
ret->spheres[0].relativePosition.z = 0;
return ret;
}
CollisionSystem::CollisionModel* CollisionSystem::multiSphereModel(
SculptObject* base, int sphereCount, float scale) {
// create the multi sphere model
CollisionModel* ret = new CollisionModel;
ret->fullPolyModel = NULL;
ret->fullPolyModelSculpt = base;
ret->spheres = NULL;
ret->scale = scale;
updateMultiModel(ret, sphereCount);
return ret;
}
void CollisionSystem::updateMultiModel(CollisionModel* cm, int sphereCount) {
SculptObject* base = cm->fullPolyModelSculpt;
// pick cluster indexes
int* clusterIndexes = new int[base->m_nNumPoint];
int c, i;
// pick random centers
G308_Point* centers = pickRandomPoints(base->m_pVertexArray,
base->m_nNumPoint, sphereCount);
int* pointsInCluster = new int[sphereCount];
float prevVariance = FLT_MAX;
int pass = 0;
while (true) {
pass++;
for (c = 0; c < sphereCount; c++) {
pointsInCluster[c] = 0;
}
float newVariance = 0;
for (i = 0; i < base->m_nNumPoint; i++) {
// pick cluster for this point
int picked = -1;
float dist = FLT_MAX;
for (int cluster = 0; cluster < sphereCount; cluster++) {
float thisDist = distanceCalcP(base->m_pVertexArray[i],
centers[cluster]);
if (thisDist < dist) {
picked = cluster;
dist = thisDist;
}
}
clusterIndexes[i] = picked;
pointsInCluster[picked]++;
newVariance += pow(dist, 2);
}
// is variance the same?
if (prevVariance == newVariance) {
// done
break;
}
// recalculate centroids
for (c = 0; c < sphereCount; c++) {
centers[c].x = 0;
centers[c].y = 0;
centers[c].z = 0;
}
for (i = 0; i < base->m_nNumPoint; i++) {
centers[clusterIndexes[i]].x += base->m_pVertexArray[i].x;
centers[clusterIndexes[i]].y += base->m_pVertexArray[i].y;
centers[clusterIndexes[i]].z += base->m_pVertexArray[i].z;
}
// now average
for (c = 0; c < sphereCount; c++) {
centers[c].x /= pointsInCluster[c];
centers[c].y /= pointsInCluster[c];
centers[c].z /= pointsInCluster[c];
}
// and set new variance
prevVariance = newVariance;
}
// now we make spheres
// check each cluster center against the points in it
float* maxDists = new float[sphereCount];
for (c = 0; c < sphereCount; c++) {
maxDists[c] = 0;
}
for (i = 0; i < base->m_nNumPoint; i++) {
float dist = distanceCalcP(base->m_pVertexArray[i],
centers[clusterIndexes[i]]);
if (dist > maxDists[clusterIndexes[i]]) {
maxDists[clusterIndexes[i]] = dist;
}
}
// create the actual spheres
cm->sphereCount = sphereCount;
if (cm->spheres != NULL) {
delete[] cm->spheres;
}
cm->spheres = new Sphere[sphereCount];
for (c = 0; c < sphereCount; c++) {
cm->spheres[c].radius = maxDists[c] * cm->scale;
cm->spheres[c].relativePosition.x = centers[c].x * cm->scale;
cm->spheres[c].relativePosition.y = centers[c].y * cm->scale;
cm->spheres[c].relativePosition.z = centers[c].z * cm->scale;
}
delete[] maxDists;
delete[] centers;
delete[] clusterIndexes;
}
G308_Point* CollisionSystem::pickRandomPoints(G308_Point* vertices, int count,
int howMany) {
// pick unique points
int* choices = new int[howMany];
int i;
for (i = 0; i < howMany; i++) {
choices[i] = -1;
}
int current = 0;
while (current < howMany) {
int randomIndex = rand() % count;
bool use = true;
for (int j = 0; j < current; j++) {
if (choices[j] == randomIndex) {
use = false;
break;
}
}
if (use) {
choices[current++] = randomIndex;
}
}
G308_Point* picks = new G308_Point[howMany];
for (i = 0; i < howMany; i++) {
picks[i] = vertices[choices[i]];
}
return picks;
}
CollisionSystem::Model* CollisionSystem::loadModel(const char* filename) {
Model *model = (Model*) malloc(sizeof(Model));
FILE* fp;
char mode, vmode;
char str[200];
int v1, v2, v3, n1, n2, n3, t1, t2, t3;
int numVert, numNorm, numUV, numFace;
float x, y, z;
float u, v;
numVert = numNorm = numUV = numFace = 0;
fp = fopen(filename, "r");
if (fp == NULL) {
printf("Could not find file: %s\n", filename);
exit(EXIT_FAILURE);
} else
printf("Reading %s file\n", filename);
// Check number of vertex, normal, uvCoord, and Face
while (fgets(str, 200, fp) != NULL) {
sscanf(str, "%c%c", &mode, &vmode);
switch (mode) {
case 'v': /* vertex, uv, normal */
if (vmode == 't') // uv coordinate
numUV++;
else if (vmode == 'n') // normal
numNorm++;
else if (vmode == ' ') // vertex
numVert++;
break;
case 'f': /* faces */
numFace++;
break;
}
}
model->m_nNumPoint = numVert;
model->m_nNumUV = numUV;
model->m_nNumPolygon = numFace;
model->m_nNumNormal = numNorm;
printf("Number of Point %d, UV %d, Normal %d, Face %d\n", numVert, numUV,
numNorm, numFace);
//-------------------------------------------------------------
// Allocate memory for array
//-------------------------------------------------------------
model->m_pVertexArray = new G308_Point[model->m_nNumPoint];
model->m_pNormalArray = new G308_Normal[model->m_nNumNormal];
model->m_pUVArray = new G308_UVcoord[model->m_nNumUV];
model->m_pTriangles = new G308_Triangle[model->m_nNumPolygon];
//-----------------------------------------------------------
// Read obj file
//-----------------------------------------------------------
numVert = numNorm = numUV = numFace = 0;
fseek(fp, 0L, SEEK_SET);
while (fgets(str, 200, fp) != NULL) {
sscanf(str, "%c%c", &mode, &vmode);
switch (mode) {
case 'v': /* vertex, uv, normal */
if (vmode == 't') // uv coordinate
{
sscanf(str, "vt %f %f", &u, &v);
model->m_pUVArray[numUV].u = u;
model->m_pUVArray[numUV].v = v;
numUV++;
} else if (vmode == 'n') // normal
{
sscanf(str, "vn %f %f %f", &x, &y, &z);
model->m_pNormalArray[numNorm].x = x;
model->m_pNormalArray[numNorm].y = y;
model->m_pNormalArray[numNorm].z = z;
numNorm++;
} else if (vmode == ' ') // vertex
{
sscanf(str, "v %f %f %f", &x, &y, &z);
model->m_pVertexArray[numVert].x = x;
model->m_pVertexArray[numVert].y = y;
model->m_pVertexArray[numVert].z = z;
numVert++;
}
break;
case 'f': /* faces : stored value is index - 1 since our index starts from 0, but obj starts from 1 */
if (numNorm > 0 && numUV > 0) {
sscanf(str, "f %d/%d/%d %d/%d/%d %d/%d/%d", &v1, &t1, &n1, &v2,
&t2, &n2, &v3, &t3, &n3);
} else if (numNorm > 0 && numUV == 0) {
sscanf(str, "f %d//%d %d//%d %d//%d", &v1, &n1, &v2, &n2, &v3,
&n3);
} else if (numUV > 0 && numNorm == 0) {
sscanf(str, "f %d/%d %d/%d %d/%d", &v1, &t1, &v2, &t2, &v3,
&t3);
} else if (numUV == 0 && numNorm == 0) {
sscanf(str, "f %d %d %d", &v1, &v2, &v3);
}
// Vertex indicies for triangle:
if (numVert != 0) {
model->m_pTriangles[numFace].v1 = v1 - 1;
model->m_pTriangles[numFace].v2 = v2 - 1;
model->m_pTriangles[numFace].v3 = v3 - 1;
}
// Normal indicies for triangle
if (numNorm != 0) {
model->m_pTriangles[numFace].n1 = n1 - 1;
model->m_pTriangles[numFace].n2 = n2 - 1;
model->m_pTriangles[numFace].n3 = n3 - 1;
}
// UV indicies for triangle
if (numUV != 0) {
model->m_pTriangles[numFace].t1 = t1 - 1;
model->m_pTriangles[numFace].t2 = t2 - 1;
model->m_pTriangles[numFace].t3 = t3 - 1;
}
numFace++;
break;
default:
break;
}
}
fclose(fp);
printf("Reading OBJ file is DONE.\n");
return model;
}
void CollisionSystem::step() {
// physics
processPhysics();
// collisions
processCollisions();
// rendering
render();
}
void CollisionSystem::updateAll() {
for (int i = 0; i <= num_NonSunObjects; i++) {
if (worldObjects[i].collisionModel->fullPolyModelSculpt != NULL) {
if (worldObjects[i].collisionModel->fullPolyModelSculpt->geometry_changed) {
worldObjects[i].collisionModel->fullPolyModelSculpt->geometry_changed =
false;
updateMultiModel(worldObjects[i].collisionModel,
spheresPerModel);
}
}
}
}
float CollisionSystem::floatRand(int min, int max, int precision) {
int modulo = (max - min + 1) * precision;
int randChoice = rand() % modulo;
return ((float) randChoice) / ((float) precision) + min;
}
void CollisionSystem::render() {
for (int i = 0; i <= num_NonSunObjects; i++) {
if (worldObjects[i].displayModel != NULL) {
// show this one
glPushMatrix();
glColor3f(0.8f, 0.8f, 0.8f);
glTranslatef(worldObjects[i].position.x, worldObjects[i].position.y,
worldObjects[i].position.z);
glRotatef(worldObjects[i].rotation.z, 0, 0, 1);
glRotatef(worldObjects[i].rotation.y, 0, 1, 0);
glRotatef(worldObjects[i].rotation.x, 1, 0, 0);
float scale = worldObjects[i].collisionModel->scale;
glScalef(scale, scale, scale);
worldObjects[i].displayModel->RenderGeometry(0);
glPopMatrix();
}
}
//glDisable(GL_COLOR_MATERIAL);
}
void CollisionSystem::processCollisions() {
// opening round: outer bounding spheres
for (int i = 0; i <= num_NonSunObjects; i++) {
for (int j = i + 1; j <= num_NonSunObjects; j++) {
float distance = distanceCalc(worldObjects[i].position,
worldObjects[j].position);
float sumRadii = worldObjects[i].radius + worldObjects[j].radius;
// spherical collision: if (radius sum) > distance we have a winner
if (sumRadii >= distance) {
//printf("objects %d and %d could be colliding, testing\n", i, j);
// check for collision
bool collisionFound = false;
int collisS1, collisS2;
G308_Point colPos1, colPos2;
CollisionModel* m1, *m2;
m1 = worldObjects[i].collisionModel;
m2 = worldObjects[j].collisionModel;
// precalc relevant cosine and sine values
float rot1vals[6];
float rot2vals[6];
rot1vals[0] = sin(worldObjects[i].rotation.x * PI / 180);
rot1vals[1] = cos(worldObjects[i].rotation.x * PI / 180);
rot1vals[2] = sin(worldObjects[i].rotation.y * PI / 180);
rot1vals[3] = cos(worldObjects[i].rotation.y * PI / 180);
rot1vals[4] = sin(worldObjects[i].rotation.z * PI / 180);
rot1vals[5] = cos(worldObjects[i].rotation.z * PI / 180);
rot2vals[0] = sin(worldObjects[j].rotation.x * PI / 180);
rot2vals[1] = cos(worldObjects[j].rotation.x * PI / 180);
rot2vals[2] = sin(worldObjects[j].rotation.y * PI / 180);
rot2vals[3] = cos(worldObjects[j].rotation.y * PI / 180);
rot2vals[4] = sin(worldObjects[j].rotation.z * PI / 180);
rot2vals[5] = cos(worldObjects[j].rotation.z * PI / 180);
for (int s1 = 0; s1 < m1->sphereCount; s1++) {
for (int s2 = 0; s2 < m2->sphereCount; s2++) {
G308_Point relPos1, relPos2;
relPos1 = m1->spheres[s1].relativePosition;
relPos2 = m2->spheres[s2].relativePosition;
// rotation
G308_Point rotRelPos1, rotRelPos2;
rotRelPos1.x = rot1vals[5] * rot1vals[3] * relPos1.x
+ (rot1vals[5] * rot1vals[2] * rot1vals[0]
- rot1vals[4] * rot1vals[1]) * relPos1.y
+ (rot1vals[5] * rot1vals[2] * rot1vals[1]
+ rot1vals[4] * rot1vals[0])
* relPos1.z;
rotRelPos1.y = rot1vals[4] * rot1vals[3] * relPos1.x
+ (rot1vals[4] * rot1vals[2] * rot1vals[0]
+ rot1vals[5] * rot1vals[1]) * relPos1.y
+ (rot1vals[4] * rot1vals[2] * rot1vals[1]
- rot1vals[5] * rot1vals[0])
* relPos1.z;
rotRelPos1.z = -rot1vals[2] * relPos1.x
+ rot1vals[3] * rot1vals[0] * relPos1.y
+ rot1vals[3] * rot1vals[1] * relPos1.z;
rotRelPos2.x = rot2vals[5] * rot2vals[3] * relPos2.x
+ (rot2vals[5] * rot2vals[2] * rot2vals[0]
- rot2vals[4] * rot2vals[1]) * relPos2.y
+ (rot2vals[5] * rot2vals[2] * rot2vals[1]
+ rot2vals[4] * rot2vals[0])
* relPos2.z;
rotRelPos2.y = rot2vals[4] * rot2vals[3] * relPos2.x
+ (rot2vals[4] * rot2vals[2] * rot2vals[0]
+ rot2vals[5] * rot2vals[1]) * relPos2.y
+ (rot2vals[4] * rot2vals[2] * rot2vals[1]
- rot2vals[5] * rot2vals[0])
* relPos2.z;
rotRelPos2.z = -rot2vals[2] * relPos2.x
+ rot2vals[3] * rot2vals[0] * relPos2.y
+ rot2vals[3] * rot2vals[1] * relPos2.z;
float distance =
sqrt(
pow(
(worldObjects[i].position.x
+ rotRelPos1.x)
- (worldObjects[j].position.x
+ rotRelPos2.x),
2)
+ pow(
(worldObjects[i].position.y
+ rotRelPos1.y)
- (worldObjects[j].position.y
+ rotRelPos2.y),
2)
+ pow(
(worldObjects[i].position.z
+ rotRelPos1.z)
- (worldObjects[j].position.z
+ rotRelPos2.z),
2));
if (distance
<= m1->spheres[s1].radius
+ m2->spheres[s2].radius) {
//printf(
// "found collision objects %d %d inner spheres %d %d \n",
// i, j, s1, s2);
colPos1 = rotRelPos1;
colPos2 = rotRelPos2;
collisS1 = s1;
collisS2 = s2;
collisionFound = true;
break;
}
}
if (collisionFound) {
break;
}
}
if (collisionFound) {
reactCollision(i, j, colPos1, colPos2);
}
}
}
}
}
bool CollisionSystem::detectCollision(int obj1, int obj2) {
return false;
}
void CollisionSystem::reactCollision(int obj1, int obj2, G308_Point relPos1,
G308_Point relPos2) {
if (obj1 == 0) {
// this is the sun
// "destroy" the other object by throwing it out of the clipping plane
worldObjects[obj2].position.x = 99999 + obj2 * 1000;
worldObjects[obj2].speed = 0;
} else {
// approximate a collision between obj1 and obj2
// give them a very strong push away from each other in the vector of the collision
G308_Vector diffvector;
diffvector.x = (relPos2.x + worldObjects[obj2].position.x)
- (relPos1.x + worldObjects[obj1].position.x);
diffvector.y = (relPos2.y + worldObjects[obj2].position.y)
- (relPos1.y + worldObjects[obj1].position.y);
diffvector.z = (relPos2.z + worldObjects[obj2].position.z)
- (relPos1.z + worldObjects[obj1].position.z);
normalize(&diffvector);
// Diffvector now points from obj1 to obj2
// So push obj2 further in this direction
// and pull obj1 in the other direction
G308_Vector newMovement;
float repelFactor = 0.25
* (worldObjects[obj1].weight + worldObjects[obj2].weight) / 2;
newMovement.x = diffvector.x * repelFactor / worldObjects[obj2].weight
+ worldObjects[obj2].direction.x * worldObjects[obj2].speed;
newMovement.y = diffvector.y * repelFactor / worldObjects[obj2].weight
+ worldObjects[obj2].direction.y * worldObjects[obj2].speed;
newMovement.z = diffvector.z * repelFactor / worldObjects[obj2].weight
+ worldObjects[obj2].direction.z * worldObjects[obj2].speed;
worldObjects[obj2].speed = magnitude(&newMovement);
normalize(&newMovement);
worldObjects[obj2].direction = newMovement;
// and the pull
newMovement.x = -diffvector.x * repelFactor / worldObjects[obj1].weight
+ worldObjects[obj1].direction.x * worldObjects[obj1].speed;
newMovement.y = -diffvector.y * repelFactor / worldObjects[obj1].weight
+ worldObjects[obj1].direction.y * worldObjects[obj1].speed;
newMovement.z = -diffvector.z * repelFactor / worldObjects[obj1].weight
+ worldObjects[obj1].direction.z * worldObjects[obj1].speed;
worldObjects[obj1].speed = magnitude(&newMovement);
normalize(&newMovement);
worldObjects[obj1].direction = newMovement;
}
}
void CollisionSystem::processPhysics() {
// update positions
for (int i = 0; i <= num_NonSunObjects; i++) {
// This assumes direction vector is already normalized, might not be safe in practice
worldObjects[i].position.x += worldObjects[i].direction.x
* worldObjects[i].speed;
worldObjects[i].position.y += worldObjects[i].direction.y
* worldObjects[i].speed;
worldObjects[i].position.z += worldObjects[i].direction.z
* worldObjects[i].speed;
}
// basic gravity
for (int i = 0; i <= num_NonSunObjects; i++) {
for (int j = 0; j <= num_NonSunObjects; j++) {
if (i == j) {
continue;
}
// pull j towards i
float gravity = gconstant * worldObjects[i].weight
/ pow(
distanceCalc(worldObjects[i].position,
worldObjects[j].position), 2);
G308_Vector diffvector;
diffvector.x = worldObjects[i].position.x
- worldObjects[j].position.x;
diffvector.y = worldObjects[i].position.y
- worldObjects[j].position.y;
diffvector.z = worldObjects[i].position.z
- worldObjects[j].position.z;
normalize(&diffvector);
// calculate new
G308_Vector newMovement;
newMovement.x = diffvector.x * gravity
+ worldObjects[j].direction.x * worldObjects[j].speed;
newMovement.y = diffvector.y * gravity
+ worldObjects[j].direction.y * worldObjects[j].speed;
newMovement.z = diffvector.z * gravity
+ worldObjects[j].direction.z * worldObjects[j].speed;
worldObjects[j].speed = magnitude(&newMovement);
normalize(&newMovement);
worldObjects[j].direction = newMovement;
}
}
}
float CollisionSystem::dotProduct(G308_Vector* from, G308_Vector* to) {
return from->x * to->x + from->y * to->y + from->z * to->z;
}
float CollisionSystem::distanceCalcP(G308_Point from, G308_Point to) {
return (float) sqrt(
pow(to.x - from.x, 2) + pow(to.y - from.y, 2)
+ pow(to.z - from.z, 2));
}
float CollisionSystem::distanceCalc(G308_Vector from, G308_Vector to) {
return (float) sqrt(
pow(to.x - from.x, 2) + pow(to.y - from.y, 2)
+ pow(to.z - from.z, 2));
}
float CollisionSystem::magnitude(G308_Vector* vector) {
return (float) sqrt(
pow(vector->x, 2) + pow(vector->y, 2) + pow(vector->z, 2));
}
void CollisionSystem::normalize(G308_Vector* vector) {
float len = (float) sqrt(
pow(vector->x, 2) + pow(vector->y, 2) + pow(vector->z, 2));
vector->x /= len;
vector->y /= len;
vector->z /= len;
}