/
particlesystem.cpp
1185 lines (1015 loc) · 45.6 KB
/
particlesystem.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
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
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <vector>
#include <stdio.h>
//#include <irrKlang.h>
#include "utils.h"
//using namespace irrklang;
//With inspiration from Thomas Jakobsen and that Verlet website
//Models particles. By Verlet, each particle stores it's old position and it's current position.
//and extrapolates it's velocity from that. Also has acceleration if we need it.
//constraints constrains the Particle (by links but right now it's just within a box defined by vectors)
using namespace std;
//#pragma comment(lib, "irrKlang.lib")
class Sphere {
public:
Eigen::Vector3d oldPos, curPos, acc;
double radius;
double mass;
Sphere(){}
Sphere(double curx, double cury, double curz,
double radius, double mass){
this->oldPos << curx, cury, curz;
this->curPos << curx, cury, curz;
this->radius = radius;
this->mass = mass;
this->acc << 0, 0, 0;
}
void initVel(double velx, double vely, double velz){
this->oldPos << curPos(0)-velx, curPos(1)-vely, curPos(2)-velz;
}
void setAcc(double accx, double accy, double accz){
this->acc << accx, accy, accz;
}
void applyForce(Eigen::Vector3d force) {
this->acc = this->acc + force/mass;
}
void zeroAcc(){
this->acc << 0, 0, 0;
}
void Verlet(double dtimestep, Eigen::Vector3d& world_acc) {
//cout << curPos.transpose() << endl;
//cout << "BOYBOYBOYBOY" << endl;
Eigen::Vector3d temp = curPos;
curPos = curPos + (1.0-1e-5)*(curPos-oldPos) + (acc+world_acc)*dtimestep*dtimestep;
oldPos = temp;
acc = Eigen::Vector3d(0,0,0);
}
void constraints (Eigen::Vector3d& ll, Eigen::Vector3d up) {
//collisionConstraints(spheres);
boundaryConstraints(ll, up);
}
bool boundaryIntersect (Eigen::Vector3d& ll, Eigen::Vector3d& up){
double x = curPos[0];
double y = curPos[1];
double z = curPos[2];
double minx = ll[0] + radius;
double maxx = up[0] - radius;
double miny = ll[1] + radius;
double maxy = up[1] - radius;
double minz = ll[2] + radius;
double maxz = up[2] - radius;
if (x < minx || x > maxx || y < miny || y > maxy || z < minz || z > maxz)
return true;
return false;
}
bool groundIntersect (Eigen::Vector3d& ll, Eigen::Vector3d& up){
double x = curPos[0];
double y = curPos[1];
double z = curPos[2];
double minx = ll[0] + radius;
double maxx = up[0] - radius;
double miny = ll[1] + radius;
double maxy = up[1] - radius;
double minz = ll[2] + radius;
double maxz = up[2] - radius;
if (y < miny)
return true;
return false;
}
void boundaryConstraints (Eigen::Vector3d& ll, Eigen::Vector3d& up) {
double x = curPos[0];
double y = curPos[1];
double z = curPos[2];
double minx = ll[0] + radius;
double maxx = up[0] - radius;
double miny = ll[1] + radius;
double maxy = up[1] - radius;
double minz = ll[2] + radius;
double maxz = up[2] - radius;
if (x < minx) {
double x_dist = curPos[0] - oldPos[0];
curPos[0] = (minx - curPos[0]) + minx;
oldPos[0] = curPos[0] + 0.2*x_dist;
}
if (x > maxx) {
double x_dist = curPos[0] - oldPos[0];
curPos[0] = maxx - (curPos[0] - maxx);
oldPos[0] = curPos[0] + 0.2*x_dist;
}
if (y < miny) {
double y_dist = curPos[1] - oldPos[1];
curPos[1] = (miny - curPos[1]) + miny;
oldPos[1] = curPos[1] + 0.2*y_dist;
}
if (y > maxy) {
double y_dist = curPos[1] - oldPos[1];
curPos[1] = maxy - (curPos[1] - maxy);
oldPos[1] = curPos[1] + 0.2*y_dist;
}
if (z < minz) {
double z_dist = curPos[2] - oldPos[2];
curPos[2] = (minz - curPos[2]) + minz;
oldPos[2] = curPos[2] + 0.2*z_dist;
}
if (z > maxz) {
double z_dist = curPos[2] - oldPos[2];
curPos[2] = maxz - (curPos[2] - maxz);
oldPos[2] = curPos[2] + 0.2*z_dist;
}
if (x < minx + 0.5){
// Degrade other motion from friction
double y_dir = curPos[1] - oldPos[1];
double z_dir = curPos[2] - oldPos[2];
oldPos[1] = curPos[1] - 0.3*y_dir;
oldPos[2] = curPos[2] - 0.3*z_dir;
}
if (x > maxx - 0.5){
// Degrade other motion from friction
double y_dir = curPos[1] - oldPos[1];
double z_dir = curPos[2] - oldPos[2];
oldPos[1] = curPos[1] - 0.3*y_dir;
oldPos[2] = curPos[2] - 0.3*z_dir;
}
if (y < miny + 0.5){
// Degrade other motion from friction
double x_dir = curPos[0] - oldPos[0];
double z_dir = curPos[2] - oldPos[2];
oldPos[0] = curPos[0] - 0.3*x_dir;
oldPos[2] = curPos[2] - 0.3*z_dir;
}
if (y > maxy - 0.5){
// Degrade other motion from friction
double x_dir = curPos[0] - oldPos[0];
double z_dir = curPos[2] - oldPos[2];
oldPos[0] = curPos[0] - 0.3*x_dir;
oldPos[2] = curPos[2] - 0.3*z_dir;
}
if (z < minz + 0.5){
// Degrade other motion from friction
double x_dir = curPos[0] - oldPos[0];
double y_dir = curPos[1] - oldPos[1];
oldPos[0] = curPos[0] - 0.3*x_dir;
oldPos[1] = curPos[1] - 0.3*y_dir;
}
if (z > maxz - 0.5){
// Degrade other motion from friction
double x_dir = curPos[0] - oldPos[0];
double y_dir = curPos[1] - oldPos[1];
oldPos[0] = curPos[0] - 0.3*x_dir;
oldPos[1] = curPos[1] - 0.3*y_dir;
}
}
bool sphereIntersect(Sphere* other){
Eigen::Vector3d colaxis = other->curPos - this->curPos;
if (other != this && (colaxis.norm() < other->radius + this->radius))
return true;
return false;
}
//May want to change to list of Spheres
//http://studiofreya.com/blog/3d-math-and-physics/simple-sphere-sphere-collision-detection-and-collision-response/
void sphereCollisionConstraints(Sphere* other) {
Eigen::Vector3d colaxis = other->curPos - this->curPos;
if (other != this && (colaxis.norm() < other->radius + this->radius)) {
Eigen::Vector3d thiscolaxis = colaxis.normalized();
Eigen::Vector3d thisVel = curPos - oldPos;
Eigen::Vector3d thisVelx = thiscolaxis.dot(thisVel) * thiscolaxis;
Eigen::Vector3d thisVely = thisVel - thisVelx;
Eigen::Vector3d othercolaxis = -thiscolaxis;
Eigen::Vector3d otherVel = other->curPos - other->oldPos;
Eigen::Vector3d otherVelx = othercolaxis.dot(otherVel) * othercolaxis;
Eigen::Vector3d otherVely = otherVel - otherVelx;
Eigen::Vector3d otherNewVel = otherVelx * ((other->mass - this->mass)/(this->mass + other->mass))
+ thisVelx * (2 * this->mass/(this->mass + other->mass)) + otherVely;
Eigen::Vector3d thisNewVel = thisVelx * ((this->mass - other->mass)/(this->mass + other->mass))
+ otherVelx * (2 * other->mass/(this->mass + other->mass)) + thisVely;
oldPos = curPos - thisNewVel;
other->oldPos = other->curPos - otherNewVel;
}
}
};
class Explosive : public Sphere{
public:
double force;
};
// Time based explosive
class Grenade : public Explosive {
public:
double fuse;
Grenade(double curx, double cury, double curz,
double radius, double mass, double fuse,
double force){
this->oldPos << curx, cury, curz;
this->curPos << curx, cury, curz;
this->radius = radius;
this->mass = mass;
this->fuse = fuse;
this->acc << 0, 0, 0;
this->force = force;
}
};
// Time based explosive
class Flare : public Explosive {
public:
double fuse;
Eigen::Vector3d color;
Flare(double curx, double cury, double curz,
double radius, double mass, double fuse,
double force, Eigen::Vector3d& color){
this->oldPos << curx, cury, curz;
this->curPos << curx, cury, curz;
this->radius = radius;
this->mass = mass;
this->fuse = fuse;
this->color = color;
this->acc << 0, 0, 0;
this->force = force;
}
};
// Collision based explosive
class Rocket : public Explosive {
public:
bool collision;
Rocket(double curx, double cury, double curz,
double radius, double mass, double force){
this->oldPos << curx, cury, curz;
this->curPos << curx, cury, curz;
this->radius = radius;
this->mass = mass;
this->acc << 0, 0, 0;
this->collision = false;
this->force = force;
}
};
class Cylinder{
public:
Cylinder(){}
Cylinder(Sphere* node1, Sphere* node2, double r){
this->node1 = node1;
this->node2 = node2;
this->r = r;
}
void constraints(vector<Cylinder*>& body_parts, int idx){
for (int i = 0; i < body_parts.size(); i++){
if (i == idx)
continue;
double bias = fmin(this->r, body_parts[i]->r);
Eigen::Vector3d curr_part_dir = (this->node2->curPos - this->node1->curPos).normalized();
//cout << (this->node2->curPos - this->node1->curPos).norm() << endl;
Eigen::Vector3d curr_part_node_1 = this->node1->curPos + 1.05*bias*curr_part_dir;
Eigen::Vector3d curr_part_node_2 = this->node2->curPos - 1.05*bias*curr_part_dir;
double closest_dist = Seg2SegDist(curr_part_node_1, curr_part_node_2, body_parts[i]->node1->curPos, body_parts[i]->node2->curPos);
//cout << closest_dist << " " << bias << endl;
if (closest_dist > bias)
continue;
//cout << "blah" << endl;
//exit(-1);
double ext_dist = r - closest_dist;
// Hacks to get rid of nans
Eigen::Vector3d nan_bias(1e-10, 1e-10, 1e-10);
Eigen::Vector3d this_old_mid = (this->node1->oldPos + this->node2->oldPos)/2;
Eigen::Vector3d this_cur_mid = (this->node1->curPos + this->node2->curPos)/2;
Eigen::Vector3d A = (this_cur_mid - this_old_mid + nan_bias);
double A_norm = (this_cur_mid - this_old_mid).norm();
// Other body part also gets moved
Eigen::Vector3d other_old_mid = (body_parts[i]->node1->oldPos + body_parts[i]->node2->oldPos)/2;
Eigen::Vector3d other_cur_mid = (body_parts[i]->node1->curPos + body_parts[i]->node2->curPos)/2;
Eigen::Vector3d B = (other_cur_mid - other_old_mid + nan_bias);
double B_norm = (other_cur_mid - other_old_mid).norm();
// Some approximate math to get new directions
double total_norm = A_norm + B_norm;
Eigen::Vector3d D = (this_cur_mid - other_cur_mid).normalized();
Eigen::Vector3d E = (other_cur_mid - this_cur_mid).normalized();
double this_mass = (this->node1->mass + this->node2->mass);
double other_mass = (body_parts[i]->node1->mass + body_parts[i]->node2->mass);
double w1 = 1 - this_mass/(this_mass + other_mass);
double w2 = 1 - other_mass/(this_mass + other_mass);
Eigen::Vector3d this_node1_v = (this->node1->curPos - this->node1->oldPos) + w1*total_norm*D;
//cout << (this->node1->curPos - this->node1->oldPos).transpose() << endl << endl;
//cout << this_node1_v.transpose() << endl << endl;
Eigen::Vector3d this_node2_v = (this->node2->curPos - this->node2->oldPos) + w1*total_norm*D;
Eigen::Vector3d other_node1_v = (body_parts[i]->node1->curPos - body_parts[i]->node1->oldPos) + w2*total_norm*E;
Eigen::Vector3d other_node2_v = (body_parts[i]->node2->curPos - body_parts[i]->node2->oldPos) + w2*total_norm*E;
this->node1->curPos += D*this->r;
this->node2->curPos += D*this->r;
this->node1->oldPos = this->node1->curPos - 0.5*this_node1_v;
this->node2->oldPos = this->node2->curPos - 0.5*this_node2_v;
body_parts[i]->node1->curPos += E*body_parts[i]->r;
body_parts[i]->node2->curPos += E*body_parts[i]->r;
body_parts[i]->node1->oldPos = body_parts[i]->node1->curPos - 0.5*other_node1_v;
body_parts[i]->node2->oldPos = body_parts[i]->node2->curPos - 0.5*other_node2_v;
}
}
// - line has starting point (x0, y0, z0) and ending point (x1, y1, z1)
bool LineIntersect(Eigen::Vector3d& line_start, Eigen::Vector3d& line_end,
Eigen::Vector3d& intersection) {
// Solution : http://www.gamedev.net/community/forums/topic.asp?topic_id=467789
Eigen::Vector3d line_dir = (line_end - line_start).normalized();
Eigen::Vector3d A = this->node1->curPos;
Eigen::Vector3d B = this->node2->curPos;
Eigen::Vector3d nan_bias1(1e-10, -1e-10, 1e-10);
Eigen::Vector3d nan_bias2(-1e-10, 1e-10, -1e-10);
Eigen::Vector3d AB = (B - A)+nan_bias1;
Eigen::Vector3d AO = (line_start - A)+nan_bias2;
Eigen::Vector3d AOxAB = AO.cross(AB);
Eigen::Vector3d VxAB = line_dir.cross(AB);
double ab2 = AB.dot(AB);
double a = VxAB.dot(VxAB);
double b = 2 * VxAB.dot(AOxAB);
double c = AOxAB.dot(AOxAB) - (r*r * ab2);
double d = b*b - 4*a*c;
if (d < 0)
return false;
double t = (-b - sqrt(d)) / (2 * a);
if (t < 0)
return false;
intersection = line_start + line_dir*t; /// intersection point
Eigen::Vector3d projection = A + (AB.dot(intersection - A) / ab2) * AB; /// intersection projected onto cylinder axis
if ((projection - A).norm() + (B - projection).norm() > AB.norm() + 1e-5)
return false;
return true;
}
bool sphereIntersect(Sphere* s){
Sphere* n1 = this->node1;
Sphere* n2 = this->node2;
double d = PointLineSegDist(n1->curPos, n2->curPos, s->curPos);
if (d > s->radius + this->r)
return false;
return true;
}
void sphereCollisionConstraints(Sphere* s){
Sphere* n1 = this->node1;
Sphere* n2 = this->node2;
double d = PointLineSegDist(n1->curPos, n2->curPos, s->curPos);
if (d > s->radius + this->r)
return;
Eigen::Vector3d proj = ProjPointLineSeg(n1->curPos, n2->curPos, s->curPos);
Eigen::Vector3d dir = (proj - s->curPos).normalized();
double offset = (proj-s->curPos).norm();
Eigen::Vector3d s_vel = s->curPos - s->oldPos;
double s_mag = s_vel.norm();
s_vel = 0.5*(1-s->mass/(n1->mass+n2->mass+s->mass))*s_mag*dir;
Eigen::Vector3d other_vel_1 = n1->curPos - n1->oldPos;
double other_mag_1 = other_vel_1.norm();
other_vel_1 += 0.5*(1-n1->mass/(n1->mass+n2->mass+s->mass))*s_mag*dir;
Eigen::Vector3d other_vel_2 = n2->curPos - n2->oldPos;
double other_mag_2 = other_vel_2.norm();
other_vel_2 += 0.5*(1-n2->mass/(n1->mass+n2->mass+s->mass))*s_mag*dir;
s->curPos -= dir*offset;
s->oldPos = s->curPos - s_vel;
n1->curPos += dir*offset;
n2->curPos += dir*offset;
n1->oldPos = n1->curPos - other_vel_1;
n2->oldPos = n2->curPos - other_vel_2;
}
Sphere* node1;
Sphere* node2;
double r;
};
class Link {
public:
Sphere *s1, *s2;
double const_dist, max_dist;
virtual double constraints(){return 0;};
};
class HardLink : public Link {
public:
HardLink(){}
HardLink(Sphere *s1, Sphere *s2) {
this->s1 = s1;
this->s2 = s2;
this->const_dist = ((*s1).curPos-(*s2).curPos).norm();
}
double constraints() {
Eigen::Vector3d vec = ((*s2).curPos - (*s1).curPos);
double magnitude = vec.norm();
double ext_dist = magnitude - const_dist;
// lighter objects move further
double weight1 = fmax(1.0 - (*s1).mass/((*s1).mass + (*s2).mass), 1e-5);
double weight2 = fmax(1.0 - (*s2).mass/((*s1).mass + (*s2).mass), 1e-5);
if (abs(ext_dist) > 1e-5) {
Eigen::Vector3d s1s2 = vec.normalized();
Eigen::Vector3d s2s1 = -(vec.normalized());
(*s1).curPos = (*s1).curPos + weight1*ext_dist*s1s2;
(*s2).curPos = (*s2).curPos + weight2*ext_dist*s2s1;
(*s1).oldPos = (*s1).oldPos + 0.6*weight1*ext_dist*s1s2;
(*s2).oldPos = (*s2).oldPos + 0.6*weight2*ext_dist*s2s1;
}
return ext_dist;
}
};
class SoftLink : public Link {
public:
SoftLink(){}
SoftLink(Sphere *s1, Sphere *s2, double const_dist, double max_dist) {
this->s1 = s1;
this->s2 = s2;
this->const_dist = const_dist;
this->max_dist = max_dist;
}
double constraints() {
Eigen::Vector3d vec = ((*s2).curPos - (*s1).curPos);
double magnitude = vec.norm();
double ext_dist = 0;
double weight1 = fmax(1.0 - (*s1).mass/((*s1).mass + (*s2).mass), 1e-5);
double weight2 = fmax(1.0 - (*s2).mass/((*s1).mass + (*s2).mass), 1e-5);
Eigen::Vector3d s1s2 = vec.normalized();
Eigen::Vector3d s2s1 = -s1s2;
if (magnitude - max_dist > 1e-5) {
(*s1).curPos = (*s1).curPos + weight1*ext_dist*s1s2;
(*s2).curPos = (*s2).curPos + weight2*ext_dist*s2s1;
(*s1).oldPos = (*s1).oldPos + weight1*ext_dist*s1s2;
(*s2).oldPos = (*s2).oldPos + weight2*ext_dist*s2s1;
return magnitude - max_dist;
} else if (const_dist - magnitude > 1e-5) {
(*s1).curPos = (*s1).curPos + weight1*ext_dist*s2s1;
(*s2).curPos = (*s2).curPos + weight2*ext_dist*s1s2;
(*s1).oldPos = (*s1).oldPos + weight1*ext_dist*s2s1;
(*s2).oldPos = (*s2).oldPos + weight2*ext_dist*s1s2;
return const_dist - magnitude;
}
return 0;
}
};
class Angle {
/* s4
/\
L5 / \ L4
s2/____\s3
\ L3 /
L1 \ / L2
\/
s1
Create an angle constraint as shown above
The diagram above is 180 degrees.
Rotate right hand rule around vector (s2-s3).
*/
public:
Sphere *s1, *s2, *s3, *s4;
Eigen::Vector3d maxPos;
double const_angle;
virtual void constraints(){cout << "this should not be called" << endl;};
};
class SoftAngle : public Angle {
public:
SoftAngle() {}
SoftAngle(Link *l1, Link *l2, Link *l3, Link *l4, Link *l5, double angle) {
//Links in hinge may have been constructed weirdly, thus parse the spheres that make up the hinge
//into the above diagram.
if (l1->s1 == l2->s1) {
this->s1 = l1->s1;
this->s2 = l1->s2;
this->s3 = l2->s2;
} else if (l1->s1 == l2->s2) {
this->s1 = l1->s1;
this->s2 = l1->s2;
this->s3 = l2->s1;
} else if (l1->s2 == l2->s1) {
this->s1 = l1->s2;
this->s2 = l1->s1;
this->s3 = l2->s2;
} else if (l1->s2 == l2->s2) {
this->s1 = l1->s2;
this->s2 = l1->s1;
this->s3 = l2->s1;
}
if (l4->s1 == l5->s1) {
this->s4 = l4->s1;
} else if (l4->s1 == l5->s2) {
this->s4 = l4->s1;
} else if (l4->s2 == l5->s1) {
this->s4 = l4->s2;
} else if (l4->s2 == l5->s2) {
this->s4 = l4->s2;
}
this->const_angle = angle;
}
SoftAngle(Sphere *ss1, Sphere *ss2, Sphere *ss3, Sphere *ss4, double angle) {
s1 = ss1;
s2 = ss2;
s3 = ss3;
s4 = ss4;
this->const_angle = angle;
}
void constraints() {
//Variables for max angle position
//cout << "in angle constraints" << endl;
Eigen::Vector3d hingeCenter = (s2->curPos - s3->curPos)/2 + s3->curPos;
Eigen::Vector3d s2s3 = (s2->curPos - s3->curPos).normalized();
Eigen::Vector3d v1 = s4->curPos - hingeCenter;
Eigen::Vector3d v2 = s1->curPos - hingeCenter;
//cout << "initial magnitude " << v1.norm() << endl;
Eigen::Vector3d up = s2s3.cross(v2);
double orientation = up.dot(v1);
double angle = acos(v1.normalized().dot(v2.normalized())/(v1.normalized().norm() * v2.normalized().norm())) * 180/3.1415926535;
if ( orientation < 0 ) {
angle = 360-angle;
}
//cout << "angle " << angle << endl;
if ((angle > const_angle || angle < 20) && (v1.normalized() != v2.normalized())) {
//cout << "angle invalid" << endl;
//find the angle between s1-hingeCenter and s1-s4
Eigen::Vector3d s1s4 = (s4->curPos - s1->curPos);
Eigen::Vector3d s1hc = (hingeCenter - s1->curPos);
double another_angle = acos(s1s4.dot(s1hc)/(s1s4.norm() * s1hc.norm()));
double rotate_amount = -another_angle;
//cout << rotate_amount << endl;
Eigen::Matrix3d firstrotation;
firstrotation = AngleAxisd(-rotate_amount/15, s2s3);
Eigen::Vector3d newPosS4 = firstrotation * (s4->curPos - s1->curPos);
Eigen::Vector3d newPosS2 = firstrotation * (s2->curPos - s1->curPos);
Eigen::Vector3d newPosS3 = firstrotation * (s3->curPos - s1->curPos);
Eigen::Vector3d newPosS1 = s1->curPos;
Eigen::Vector3d oldPosS4 = firstrotation * (s4->oldPos - s1->curPos);
Eigen::Vector3d oldPosS2 = firstrotation * (s2->oldPos - s1->curPos);
Eigen::Vector3d oldPosS3 = firstrotation * (s3->oldPos - s1->curPos);
Eigen::Vector3d oldPosS1 = s1->oldPos;
double rotation_size = 280;
double rotation_amount;
if (angle > rotation_size) {
rotation_amount = 20 + 360-angle;
} else if (angle < 20) {
rotation_amount = 20-angle;
} else {
rotation_amount = const_angle - angle;
}
Eigen::Matrix3d rotation1;
rotation1 = AngleAxisd((rotation_amount/10) * 3.1415926535/180, s2s3);
Eigen::Matrix3d rotation2;
rotation2 = AngleAxisd((-rotation_amount/10) * 3.1415926535/180, s2s3);
newPosS4 = newPosS4 + s1->curPos;
newPosS2 = newPosS2 + s1->curPos;
newPosS3 = newPosS3 + s1->curPos;
oldPosS4 = oldPosS4 + s1->curPos;
oldPosS2 = oldPosS2 + s1->curPos;
oldPosS3 = oldPosS3 + s1->curPos;
hingeCenter = (newPosS2 - newPosS3)/2 + newPosS3;
//cout << "intermediate magnitude " << (newPosS4 - hingeCenter).norm() << endl;
newPosS4 = rotation1 * (newPosS4 - hingeCenter);
oldPosS4 = rotation1 * (oldPosS4 - hingeCenter);
newPosS1 = rotation2 * (newPosS1 - hingeCenter);
oldPosS1 = rotation2 * (oldPosS1 - hingeCenter);
newPosS4 = newPosS4 + hingeCenter;
oldPosS4 = oldPosS4 + hingeCenter;
newPosS1 = newPosS1 + hingeCenter;
oldPosS1 = oldPosS1 + hingeCenter;
Eigen::Vector3d nanbias = Eigen::Vector3d(1e-5, 1e-5, 1e-5);
s1->curPos = newPosS1;
s2->curPos = newPosS2;
s3->curPos = newPosS3;
s4->curPos = newPosS4;
Eigen::Vector3d diff1 = s1->oldPos - newPosS1;
Eigen::Vector3d diff2 = s2->oldPos - newPosS2;
Eigen::Vector3d diff3 = s3->oldPos - newPosS3;
Eigen::Vector3d diff4 = s4->oldPos - newPosS4;
//s1->oldPos = newPosS1 + 0.9*(s1->oldPos - newPosS1 + nanbias);
//s2->oldPos = newPosS2 + 0.9*(s2->oldPos - newPosS2 + nanbias);
//s3->oldPos = newPosS3 + 0.9*(s3->oldPos - newPosS3 + nanbias);
//s4->oldPos = newPosS4 + 0.9*(s4->oldPos - newPosS4 + nanbias);
//Eigen::Vector3d diff1 = oldPosS1 - newPosS1;
//Eigen::Vector3d diff2 = oldPosS2 - newPosS2;
//Eigen::Vector3d diff3 = oldPosS3 - newPosS3;
//Eigen::Vector3d diff4 = oldPosS4 - newPosS4;
//s1->oldPos = newPosS1 + Eigen::Vector3d(0.1*diff1(0), diff1(1), 0.1*diff1(2));
//s2->oldPos = newPosS2 + Eigen::Vector3d(0.1*diff2(0), diff2(1), 0.1*diff2(2));
//s3->oldPos = newPosS3 + Eigen::Vector3d(0.1*diff3(0), diff3(1), 0.1*diff3(2));
//s4->oldPos = newPosS4 + Eigen::Vector3d(0.1*diff4(0), diff4(1), 0.1*diff4(2));
//cout << "final magnitude " << (s4->curPos - hingeCenter).norm() << endl;
//cout << "" << endl;
//s1->oldPos = (s1->curPos) + 0.1 * (oldPosS1 - newPosS1);
//s4->oldPos = (s4->curPos) + 0.1 * (oldPosS1 - newPosS1);
}
// cout << "s1s2 magnitude after " << (s1->curPos - s2->curPos).norm() << endl;
// cout << "End rotation constraint" << endl;
//cout << "" << endl;
}
};
class HardAngle : public Angle {
public:
HardAngle() {}
HardAngle(Link *l1, Link *l2, Link *l3, Link *l4, Link * l5, double angle) {
//Links in hinge may have been constructed weirdly, thus parse the spheres that make up the hinge
//into the above diagram.
if (l1->s1 == l2->s1) {
this->s1 = l1->s1;
this->s2 = l1->s2;
this->s3 = l2->s2;
} else if (l1->s1 == l2->s2) {
this->s1 = l1->s1;
this->s2 = l1->s2;
this->s3 = l2->s1;
} else if (l1->s2 == l2->s1) {
this->s1 = l1->s2;
this->s2 = l1->s1;
this->s3 = l2->s2;
} else if (l1->s2 == l2->s2) {
this->s1 = l1->s2;
this->s2 = l1->s1;
this->s3 = l2->s1;
}
if (l4->s1 == l5->s1) {
this->s4 = l4->s1;
} else if (l4->s1 == l5->s2) {
this->s4 = l4->s1;
} else if (l4->s2 == l5->s1) {
this->s4 = l4->s2;
} else if (l4->s2 == l5->s2) {
this->s4 = l4->s2;
}
this->const_angle = angle;
}
void constraints() {
//Variables for max angle position
Eigen::Vector3d maxPos;
Eigen::Matrix3d rotation;
Eigen::Vector3d diff;
double cosa, sina, mcosa, msina;
Eigen::Vector3d hingeCenter = (s2->curPos + s3->curPos)/2;
Eigen::Vector3d s2s3 = (s2->curPos - s3->curPos).normalized();
Eigen::Vector3d v1 = s4->curPos - hingeCenter;
Eigen::Vector3d v2 = s1->curPos - hingeCenter;
//Need the up vector to enlarge to 360 degrees
Eigen::Vector3d up = s2s3.cross(v2);
//Calculated using the dotproduct
double orientation = up.dot(v2);
//use dotproduct cosine relation to find angle (in degrees), unfortunately limited to 0-180 degrees so need
//to use up vector (calculated above) to enlarge to 360 degrees.
double angle = acos(v1.dot(v2)/(v1.norm() * v2.norm())) * 180/3.1415926535;
if ( orientation < 0 ) {
angle = angle + 180;
}
if (!(abs(angle - const_angle) < 1e-3)) {
double rotation_amount = const_angle - angle;
cosa = cos(rotation_amount * 3.1415926535/180); sina = sin(rotation_amount * 3.1415926535/180); mcosa = 1 - cosa; msina = 1 - sina;
rotation <<
cosa + s2s3[0] * s2s3[0] * mcosa, s2s3[0] * s2s3[1] * mcosa - s2s3[2] * sina, s2s3[0] * s2s3[2] * mcosa + s2s3[1] * sina,
s2s3[1] * s2s3[0] * mcosa + s2s3[2] * sina, cosa + s2s3[1] * s2s3[1] * mcosa, s2s3[1] * s2s3[2] * mcosa - s2s3[0] * sina,
s2s3[2] * s2s3[0] * mcosa - s2s3[1] * sina, s2s3[2] * s2s3[1] * mcosa + s2s3[0] * sina, cosa + s2s3[2] * s2s3[2] * mcosa;
maxPos = rotation * s1->curPos;
diff = maxPos - s4->curPos;
s4->curPos = maxPos;
s4->oldPos = s4->oldPos + diff;
}
}
};
class restrictedRotationAngle {
/* s4
/\
L5 / \ L4
s2/____\s3
\ L3 /
L1 \ / L2
\/
s1
Above is diagram from angle constraints. this is 180 degrees. so fron should be pointing into the picture
Thus take the cross of hingCenter->s4 to s3->s2 so this is pointing to the front.
Front is defined by passing in and storing the position of the pelvis->l_pelvis crossed with spine.
This class is always restricted to 180 degrees region defined by front.
Create an angle constraint as shown above
The diagram above is 180 degrees.
Rotate right hand rule around vector (s2-s3).
*/
public:
Sphere *l_pelvis, *pelvis, *spine_top, *stomach,
*s1, *s2, *s3, *s4;
restrictedRotationAngle() {}
restrictedRotationAngle(Link *l1, Link *l2, Link *l3, Link *l4, Link *l5, Sphere *l_pelvis, Sphere *pelvis, Sphere *spine_top, Sphere *stomach) {
//Links in hinge may have been constructed weirdly, thus parse the spheres that make up the hinge
//into the above diagram.
if (l1->s1 == l2->s1) {
this->s1 = l1->s1;
this->s2 = l1->s2;
this->s3 = l2->s2;
} else if (l1->s1 == l2->s2) {
this->s1 = l1->s1;
this->s2 = l1->s2;
this->s3 = l2->s1;
} else if (l1->s2 == l2->s1) {
this->s1 = l1->s2;
this->s2 = l1->s1;
this->s3 = l2->s2;
} else if (l1->s2 == l2->s2) {
this->s1 = l1->s2;
this->s2 = l1->s1;
this->s3 = l2->s1;
}
if (l4->s1 == l5->s1) {
this->s4 = l4->s1;
} else if (l4->s1 == l5->s2) {
this->s4 = l4->s1;
} else if (l4->s2 == l5->s1) {
this->s4 = l4->s2;
} else if (l4->s2 == l5->s2) {
this->s4 = l4->s2;
}
this->l_pelvis = l_pelvis;
this->pelvis = pelvis;
this->spine_top = spine_top;
this->stomach = stomach;
}
restrictedRotationAngle(Sphere *ss1, Sphere *ss2, Sphere *ss3, Sphere *ss4, Sphere *l_pelvis, Sphere *pelvis, Sphere *spine_top, Sphere *stomach) {
s1 = ss1;
s2 = ss2;
s3 = ss3;
s4 = ss4;
this->l_pelvis = l_pelvis;
this->pelvis = pelvis;
this->spine_top = spine_top;
this->stomach = stomach;
}
void constraints() {
//Calculate the front vector
//cout << "in other constraints" << endl;
Eigen::Vector3d front = -(l_pelvis->curPos - pelvis->curPos).cross(spine_top->curPos - pelvis->curPos).normalized();
//Calculate the orientation of the knee
Eigen::Vector3d hingeCenter = (s2->curPos - s3->curPos)/2 + s3->curPos;
Eigen::Vector3d knee_orientation = (s3->curPos - hingeCenter).cross(s1->curPos - hingeCenter);
//Calculate other orientation
Eigen::Vector3d otherfront = -(stomach->curPos - pelvis->curPos).cross(l_pelvis->curPos - pelvis->curPos);
//cout << otherfront.transpose() << endl;
double otherangle = acos(otherfront.normalized().dot((hingeCenter-s1->curPos).normalized())/(otherfront.normalized().norm() * (hingeCenter-s1->curPos).normalized().norm()));
//See if valid rotation angle, if it just return
if (front.dot(knee_orientation) >= 0 || otherangle < 3.1415926535/2) {
//cout << front.transpose() << endl;
//cout << knee_orientation.transpose() << endl;
//cout << "valid" << endl;
return;
}
//calculate the angle between the knee_orientation and the front vector, to determine how much to rotate
double angle = acos(front.normalized().dot(knee_orientation.normalized())/(front.normalized().norm() * knee_orientation.normalized().norm()));
//cout << "angle" << angle << endl;
//how much we want to actually rotate
double rotation_amount = angle - 3.1415926535;
//cout << "rotation amount " << rotation_amount << endl;
Eigen::Matrix3d rotation;
rotation = AngleAxisd(rotation_amount/15, (s1->curPos - hingeCenter).normalized());
Eigen::Vector3d newPosS4 = rotation * (s4->curPos - s1->curPos) + s1->curPos;
Eigen::Vector3d newPosS3 = rotation * (s3->curPos - s1->curPos) + s1->curPos;
Eigen::Vector3d newPosS2 = rotation * (s2->curPos - s1->curPos) + s1->curPos;
Eigen::Vector3d newPosS1 = s1->curPos;
Eigen::Vector3d oldPosS4 = rotation * (s4->oldPos - s1->curPos) + s1->curPos;
Eigen::Vector3d oldPosS3 = rotation * (s3->oldPos - s1->curPos) + s1->curPos;
Eigen::Vector3d oldPosS2 = rotation * (s2->oldPos - s1->curPos) + s1->curPos;
Eigen::Vector3d oldPosS1 = rotation * (s1->oldPos - s1->curPos) + s1->curPos;
//Calculate adjusted orientation of the knee
Eigen::Vector3d adj_hingeCenter = (newPosS2 - newPosS3)/2 + newPosS3;
Eigen::Vector3d adj_knee_orientation = (newPosS3 - adj_hingeCenter).cross(newPosS1 - adj_hingeCenter);
if (front.dot(adj_knee_orientation) >= 0) {
Eigen::Vector3d diff1 = oldPosS1 - newPosS1;
Eigen::Vector3d diff2 = oldPosS2 - newPosS2;
Eigen::Vector3d diff3 = oldPosS3 - newPosS3;
Eigen::Vector3d diff4 = oldPosS4 - newPosS4;
s1->curPos = newPosS1; //s1->oldPos = newPosS1 + 0.1 * (oldPosS1 - newPosS1);
s2->curPos = newPosS2; //s2->oldPos = newPosS2 + 0.1 * (oldPosS2 - newPosS2);
s3->curPos = newPosS3; //s3->oldPos = newPosS1 + 0.1 * (oldPosS3 - newPosS3);
s4->curPos = newPosS4; //s4->oldPos = newPosS4 + 0.1 * (oldPosS4 - newPosS4);
s1->oldPos = s1->curPos + Eigen::Vector3d(0.0*diff1(0), diff1(1), 0.0*diff1(2));
s2->oldPos = s2->curPos + Eigen::Vector3d(0.0*diff2(0), diff2(1), 0.0*diff2(2));
s3->oldPos = s3->curPos + Eigen::Vector3d(0.0*diff3(0), diff3(1), 0.0*diff3(2));
s4->oldPos = s4->curPos + Eigen::Vector3d(0.0*diff4(0), diff4(1), 0.0*diff4(2));
return;
}
rotation = AngleAxisd(rotation_amount/15, (hingeCenter - s1->curPos).normalized());
newPosS4 = rotation * (s4->curPos - s1->curPos) + s1->curPos;
newPosS3 = rotation * (s3->curPos - s1->curPos) + s1->curPos;
newPosS2 = rotation * (s2->curPos - s1->curPos) + s1->curPos;
newPosS1 = rotation * (s1->curPos - s1->curPos) + s1->curPos;
oldPosS4 = rotation * (s4->oldPos - s1->curPos) + s1->curPos;
oldPosS3 = rotation * (s3->oldPos - s1->curPos) + s1->curPos;
oldPosS2 = rotation * (s2->oldPos - s1->curPos) + s1->curPos;
oldPosS1 = rotation * (s1->oldPos - s1->curPos) + s1->curPos;
Eigen::Vector3d diff1 = oldPosS1 - newPosS1;
Eigen::Vector3d diff2 = oldPosS2 - newPosS2;
Eigen::Vector3d diff3 = oldPosS3 - newPosS3;
Eigen::Vector3d diff4 = oldPosS4 - newPosS4;
s1->curPos = newPosS1; //s1->oldPos = newPosS1 + 0.1 * (oldPosS1 - newPosS1);
s2->curPos = newPosS2; //s2->oldPos = newPosS2 + 0.1 * (oldPosS2 - newPosS2);
s3->curPos = newPosS3; //s3->oldPos = newPosS1 + 0.1 * (oldPosS3 - newPosS3);
s4->curPos = newPosS4; //s4->oldPos = newPosS4 + 0.1 * (oldPosS4 - newPosS4);
s1->oldPos = s1->curPos + Eigen::Vector3d(0.0*diff1(0), diff1(1), 0.0*diff1(2));
s2->oldPos = s2->curPos + Eigen::Vector3d(0.0*diff2(0), diff2(1), 0.0*diff2(2));
s3->oldPos = s3->curPos + Eigen::Vector3d(0.0*diff3(0), diff3(1), 0.0*diff3(2));
s4->oldPos = s4->curPos + Eigen::Vector3d(0.0*diff4(0), diff4(1), 0.0*diff4(2));
return;
}
};
class ParticleSystem {
public:
vector <Sphere*> Grav_Nodes;
vector <Sphere*> SS;
vector <Link*> LL;
vector <Link*> Grav;
vector <Cylinder*> CC;
double dtimestep;
vector <Angle*> AA;
vector <restrictedRotationAngle*> RR;
vector <Grenade*> grenades;
vector <Flare*> explosions;
vector <Rocket*> rockets;
Eigen::Vector3d box_corner;
Eigen::Vector3d box_dims;
Eigen::Vector3d world_acc;
ParticleSystem(double box_corner_x, double box_corner_y, double box_corner_z,
double box_dims_x, double box_dims_y, double box_dims_z,
double timestep) {
this->box_corner << box_corner_x, box_corner_y, box_corner_z;
this->box_dims << box_dims_x, box_dims_y, box_dims_z;
this->dtimestep = timestep;
this->world_acc << 0, 0, 0;
}
void setAcc(double accx, double accy, double accz){
this->world_acc << accx, accy, accz;
}
void zeroParticleAcc();
void TimeStep(bool use_angle_constraints, bool use_other_angle_constraints);
void Verlet();
void SatisfyConstraints(bool use_angle_constraints, bool use_other_angle_constraints);
void GetBox(vector<Eigen::Vector3d>& vertices);
void FireRay(Eigen::Vector3d& start, Eigen::Vector3d& dir, double mag);
void CreateExplosion(Explosive* exploder);
void ComputeExplosions();
};
void ParticleSystem::zeroParticleAcc(){
for (int i = 0; i < this->SS.size(); i++){
(this->SS[i])->zeroAcc();
}
}
void ParticleSystem::Verlet () {
vector<Sphere*>::iterator si;
for (si = SS.begin(); si != SS.end(); ++si) {
(*si)->Verlet(dtimestep, world_acc);
}
for (int i = 0; i < grenades.size(); i++) {
grenades[i]->Verlet(dtimestep, world_acc);
}
for (int i = 0; i < rockets.size(); i++){
Eigen::Vector3d zero_acc(0, 0, 0);
rockets[i]->Verlet(dtimestep, zero_acc);
}
}
void ApplyExplosiveForce(Explosive* exploder, Sphere* explodee){
Eigen::Vector3d blast = (explodee->curPos - exploder->curPos);
Eigen::Vector3d blast_Direction = blast.normalized();
double blast_dist = blast.norm();
explodee->applyForce(exploder->force * blast_Direction/(blast_dist * blast_dist));