-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphysics.cpp
More file actions
886 lines (811 loc) · 34.7 KB
/
physics.cpp
File metadata and controls
886 lines (811 loc) · 34.7 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
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
/*
USC/Viterbi/Computer Science
"Jello Cube" Assignment 1
*/
#include "jello.h"
#include "physics.h"
#include <utility>
#include <vector>
#include <iostream>
using namespace std;
void print_point(point p) {
cout<<"Values: "<<"("<<p.x<<", "<<p.y<<", "<<p.z<<")"<<endl;
}
void print_spring_ind_pair(spring s) {
cout<<"Values of spring index pair: "<<"("<<s.indexpair.first.x<<", "<<s.indexpair.first.y<<", "<<s.indexpair.first.z<<"), ";
cout<<"("<<s.indexpair.second.x<<", "<<s.indexpair.second.y<<", "<<s.indexpair.second.z<<"). ";
cout<<"Rest length is: "<<s.rest_length<<endl;
}
// modified based on PROCESS_NEIGHBOR used to record a mass point's neighbors' undeformed position.
// points inside the cude are also considered to fully model the mass-spring system.
int i,j,k,ip,jp,kp;
point pN;
#define GET_NEIGHBOUR_PT(di,dj,dk) \
ip=i+(di);\
jp=j+(dj);\
kp=k+(dk);\
if( ! ( (ip>7) || (ip<0) ||(jp>7) || (jp<0) ||(kp>7) || (kp<0) )) \
{\
pN.x = 1.0 * ip / 7;\
pN.y = 1.0 * jp / 7;\
pN.z = 1.0 * kp / 7;\
}else{\
pN.x = -404; /*neighbor not found*/\
}\
// computes 1D index in force filed of a 3D index (fi,fj,fk) given resolution of force field.
// Returns integer result in ind
#define GET_FORCEFIELD_IND(fi, fj, fk, resolution, ind) \
ind = fi*resolution*resolution + fj*resolution + fk;
// overload == operator for spring index pair equality comparison used by std::find.
// Purpose of this is to avoid duplicate inserting of a spring (i.e. spring (a,b)
// and (b,a) are a same spring)
bool operator==(const spring &lhs, const spring rhs) {
if ( (lhs.indexpair.first.x == rhs.indexpair.second.x
&&lhs.indexpair.first.y == rhs.indexpair.second.y
&&lhs.indexpair.first.z == rhs.indexpair.second.z)
&&(lhs.indexpair.second.x == rhs.indexpair.first.x
&&lhs.indexpair.second.y == rhs.indexpair.first.y
&&lhs.indexpair.second.z == rhs.indexpair.first.z) ) {
return true;
}
return false;
}
// check if a spring is new and unique by finding it in the vector of springs
bool isNewSpring (vector<spring> *sp_vector, spring s) {
return !(find(sp_vector->begin(), sp_vector->end(), s) != sp_vector->end());
}
// create structural springs, called ONCE in myinit()
void createStructuralSprings(vector<spring> & structuralSprings) {
spring currSpring;
currSpring.rest_length = 1.0/7.0;
point springIndA, springIndB;
for (i=0; i<=7; i++)
for (j=0; j<=7; j++)
for (k=0; k<=7; k++)
{
GET_NEIGHBOUR_PT(1,0,0);
if(pN.x!=-404) {
pMAKE(i,j,k,springIndA);
pMAKE(i+1,j,k, springIndB);
currSpring.indexpair = make_pair(springIndA, springIndB);
if(isNewSpring(&structuralSprings,currSpring)) structuralSprings.push_back(currSpring);
}
GET_NEIGHBOUR_PT(0,1,0);
if(pN.x!=-404) {
pMAKE(i,j,k,springIndA);
pMAKE(i,j+1,k, springIndB);
currSpring.indexpair = make_pair(springIndA, springIndB);
if(isNewSpring(&structuralSprings,currSpring)) structuralSprings.push_back(currSpring);
}
GET_NEIGHBOUR_PT(0,0,1);
if(pN.x!=-404) {
pMAKE(i,j,k,springIndA);
pMAKE(i,j,k+1, springIndB);
currSpring.indexpair = make_pair(springIndA, springIndB);
if(isNewSpring(&structuralSprings,currSpring)) structuralSprings.push_back(currSpring);
}
GET_NEIGHBOUR_PT(-1,0,0);
if(pN.x!=-404) {
pMAKE(i,j,k,springIndA);
pMAKE(i-1,j,k, springIndB);
currSpring.indexpair = make_pair(springIndA, springIndB);
if(isNewSpring(&structuralSprings,currSpring)) structuralSprings.push_back(currSpring);
}
GET_NEIGHBOUR_PT(0,-1,0);
if(pN.x!=-404) {
pMAKE(i,j,k,springIndA);
pMAKE(i,j-1,k, springIndB);
currSpring.indexpair = make_pair(springIndA, springIndB);
if(isNewSpring(&structuralSprings,currSpring)) structuralSprings.push_back(currSpring);
}
GET_NEIGHBOUR_PT(0,0,-1);
if(pN.x!=-404) {
pMAKE(i,j,k,springIndA);
pMAKE(i,j,k-1, springIndB);
currSpring.indexpair = make_pair(springIndA, springIndB);
if(isNewSpring(&structuralSprings,currSpring)) structuralSprings.push_back(currSpring);
}
}
}
// create shear springs, called ONCE in myinit()
void createShearSprings(vector<spring> & shearSprings) {
spring currSpring;
point springIndA, springIndB;
// there are only two different rest length
double short_rest_length, long_rest_length;
short_rest_length = 1.0/7.0*sqrt(2);
long_rest_length = 1.0/7.0*sqrt(3);
for (i=0; i<=7; i++)
for (j=0; j<=7; j++)
for (k=0; k<=7; k++)
{
// make curr (i,j,k) as first component (point) of a spring
pMAKE(i,j,k,springIndA);
// for below 16 neighbors, they have rest length of 1/7*sqrt(2)
GET_NEIGHBOUR_PT(1,1,0);
if(pN.x!=-404) {
pMAKE(i+1,j+1,k, springIndB);
currSpring.indexpair = make_pair(springIndA, springIndB);
if(isNewSpring(&shearSprings,currSpring)) {
currSpring.rest_length = short_rest_length;
shearSprings.push_back(currSpring);
}
}
GET_NEIGHBOUR_PT(-1,1,0);
if(pN.x!=-404) {
pMAKE(i-1,j+1,k, springIndB);
currSpring.indexpair = make_pair(springIndA, springIndB);
if(isNewSpring(&shearSprings,currSpring)) {
currSpring.rest_length = short_rest_length;
shearSprings.push_back(currSpring);
}
}
GET_NEIGHBOUR_PT(-1,-1,0);
if(pN.x!=-404) {
pMAKE(i-1,j-1,k, springIndB);
currSpring.indexpair = make_pair(springIndA, springIndB);
if(isNewSpring(&shearSprings,currSpring)) {
currSpring.rest_length = short_rest_length;
shearSprings.push_back(currSpring);
}
}
GET_NEIGHBOUR_PT(1,-1,0);
if(pN.x!=-404) {
pMAKE(i+1,j-1,k, springIndB);
currSpring.indexpair = make_pair(springIndA, springIndB);
if(isNewSpring(&shearSprings,currSpring)) {
currSpring.rest_length = short_rest_length;
shearSprings.push_back(currSpring);
}
}
/***** divider *******/
GET_NEIGHBOUR_PT(0,1,1);
if(pN.x!=-404) {
pMAKE(i,j+1,k+1, springIndB);
currSpring.indexpair = make_pair(springIndA, springIndB);
if(isNewSpring(&shearSprings,currSpring)) {
currSpring.rest_length = short_rest_length;
shearSprings.push_back(currSpring);
}
}
GET_NEIGHBOUR_PT(0,-1,1);
if(pN.x!=-404) {
pMAKE(i,j-1,k+1, springIndB);
currSpring.indexpair = make_pair(springIndA, springIndB);
if(isNewSpring(&shearSprings,currSpring)) {
currSpring.rest_length = short_rest_length;
shearSprings.push_back(currSpring);
}
}
GET_NEIGHBOUR_PT(0,-1,-1);
if(pN.x!=-404) {
pMAKE(i,j-1,k-1, springIndB);
currSpring.indexpair = make_pair(springIndA, springIndB);
if(isNewSpring(&shearSprings,currSpring)) {
currSpring.rest_length = short_rest_length;
shearSprings.push_back(currSpring);
}
}
GET_NEIGHBOUR_PT(0,1,-1);
if(pN.x!=-404) {
pMAKE(i,j+1,k-1, springIndB);
currSpring.indexpair = make_pair(springIndA, springIndB);
if(isNewSpring(&shearSprings,currSpring)) {
currSpring.rest_length = short_rest_length;
shearSprings.push_back(currSpring);
}
}
/***** divider *******/
GET_NEIGHBOUR_PT(1,0,1);
if(pN.x!=-404) {
pMAKE(i+1,j,k+1, springIndB);
currSpring.indexpair = make_pair(springIndA, springIndB);
if(isNewSpring(&shearSprings,currSpring)) {
currSpring.rest_length = short_rest_length;
shearSprings.push_back(currSpring);
}
}
GET_NEIGHBOUR_PT(-1,0,1);
if(pN.x!=-404) {
pMAKE(i-1,j,k+1, springIndB);
currSpring.indexpair = make_pair(springIndA, springIndB);
if(isNewSpring(&shearSprings,currSpring)) {
currSpring.rest_length = short_rest_length;
shearSprings.push_back(currSpring);
}
}
GET_NEIGHBOUR_PT(-1,0,-1);
if(pN.x!=-404) {
pMAKE(i-1,j,k-1, springIndB);
currSpring.indexpair = make_pair(springIndA, springIndB);
if(isNewSpring(&shearSprings,currSpring)) {
currSpring.rest_length = short_rest_length;
shearSprings.push_back(currSpring);
}
}
GET_NEIGHBOUR_PT(1,0,-1);
if(pN.x!=-404) {
pMAKE(i+1,j,k-1, springIndB);
currSpring.indexpair = make_pair(springIndA, springIndB);
if(isNewSpring(&shearSprings,currSpring)) {
currSpring.rest_length = short_rest_length;
shearSprings.push_back(currSpring);
}
}
// for below 8 neighbors, they have length of 1/7*sqrt(3)
/***** divider *******/
GET_NEIGHBOUR_PT(1,1,1);
if(pN.x!=-404) {
pMAKE(i+1,j+1,k+1, springIndB);
currSpring.indexpair = make_pair(springIndA, springIndB);
if(isNewSpring(&shearSprings,currSpring)) {
currSpring.rest_length = long_rest_length;
shearSprings.push_back(currSpring);
}
}
GET_NEIGHBOUR_PT(-1,1,1);
if(pN.x!=-404) {
pMAKE(i-1,j+1,k+1, springIndB);
currSpring.indexpair = make_pair(springIndA, springIndB);
if(isNewSpring(&shearSprings,currSpring)) {
currSpring.rest_length = long_rest_length;
shearSprings.push_back(currSpring);
}
}
GET_NEIGHBOUR_PT(-1,-1,1);
if(pN.x!=-404) {
pMAKE(i-1,j-1,k+1, springIndB);
currSpring.indexpair = make_pair(springIndA, springIndB);
if(isNewSpring(&shearSprings,currSpring)) {
currSpring.rest_length = long_rest_length;
shearSprings.push_back(currSpring);
}
}
GET_NEIGHBOUR_PT(1,-1,1);
if(pN.x!=-404) {
pMAKE(i+1,j-1,k+1, springIndB);
currSpring.indexpair = make_pair(springIndA, springIndB);
if(isNewSpring(&shearSprings,currSpring)) {
currSpring.rest_length = long_rest_length;
shearSprings.push_back(currSpring);
}
}
/***** divider *******/
GET_NEIGHBOUR_PT(1,1,-1);
if(pN.x!=-404) {
pMAKE(i+1,j+1,k-1, springIndB);
currSpring.indexpair = make_pair(springIndA, springIndB);
if(isNewSpring(&shearSprings,currSpring)) {
currSpring.rest_length = long_rest_length;
shearSprings.push_back(currSpring);
}
}
GET_NEIGHBOUR_PT(-1,1,-1);
if(pN.x!=-404) {
pMAKE(i-1,j+1,k-1, springIndB);
currSpring.indexpair = make_pair(springIndA, springIndB);
if(isNewSpring(&shearSprings,currSpring)) {
currSpring.rest_length = long_rest_length;
shearSprings.push_back(currSpring);
}
}
GET_NEIGHBOUR_PT(-1,-1,-1);
if(pN.x!=-404) {
pMAKE(i-1,j-1,k-1, springIndB);
currSpring.indexpair = make_pair(springIndA, springIndB);
if(isNewSpring(&shearSprings,currSpring)) {
currSpring.rest_length = long_rest_length;
shearSprings.push_back(currSpring);
}
}
GET_NEIGHBOUR_PT(1,-1,-1);
if(pN.x!=-404) {
pMAKE(i+1,j-1,k-1, springIndB);
currSpring.indexpair = make_pair(springIndA, springIndB);
if(isNewSpring(&shearSprings,currSpring)) {
currSpring.rest_length = long_rest_length;
shearSprings.push_back(currSpring);
}
}
/***** end *******/
}
}
// create bend springs, called ONCE in myinit()
void createBendSprings(vector<spring> & bendSprings) {
spring currSpring;
currSpring.rest_length = 1.0/7.0 * 2;
point springIndA, springIndB;
for (i=0; i<=7; i++)
for (j=0; j<=7; j++)
for (k=0; k<=7; k++)
{
GET_NEIGHBOUR_PT(2,0,0);
if(pN.x!=-404) {
pMAKE(i,j,k,springIndA);
pMAKE(i+2,j,k, springIndB);
currSpring.indexpair = make_pair(springIndA, springIndB);
if(isNewSpring(&bendSprings,currSpring)) bendSprings.push_back(currSpring);
}
GET_NEIGHBOUR_PT(0,2,0);
if(pN.x!=-404) {
pMAKE(i,j,k,springIndA);
pMAKE(i,j+2,k, springIndB);
currSpring.indexpair = make_pair(springIndA, springIndB);
if(isNewSpring(&bendSprings,currSpring)) bendSprings.push_back(currSpring);
}
GET_NEIGHBOUR_PT(0,0,2);
if(pN.x!=-404) {
pMAKE(i,j,k,springIndA);
pMAKE(i,j,k+2, springIndB);
currSpring.indexpair = make_pair(springIndA, springIndB);
if(isNewSpring(&bendSprings,currSpring)) bendSprings.push_back(currSpring);
}
GET_NEIGHBOUR_PT(-2,0,0);
if(pN.x!=-404) {
pMAKE(i,j,k,springIndA);
pMAKE(i-2,j,k, springIndB);
currSpring.indexpair = make_pair(springIndA, springIndB);
if(isNewSpring(&bendSprings,currSpring)) bendSprings.push_back(currSpring);
}
GET_NEIGHBOUR_PT(0,-2,0);
if(pN.x!=-404) {
pMAKE(i,j,k,springIndA);
pMAKE(i,j-2,k, springIndB);
currSpring.indexpair = make_pair(springIndA, springIndB);
if(isNewSpring(&bendSprings,currSpring)) bendSprings.push_back(currSpring);
}
GET_NEIGHBOUR_PT(0,0,-2);
if(pN.x!=-404) {
pMAKE(i,j,k,springIndA);
pMAKE(i,j,k-2, springIndB);
currSpring.indexpair = make_pair(springIndA, springIndB);
if(isNewSpring(&bendSprings,currSpring)) bendSprings.push_back(currSpring);
}
}
}
// computes Hook's force exerted on the first component of a spring
point computeHookElastic(spring s, double kHook, struct world * jello) {
point normL, resultForce;
double length, scaler, test_length;
int ptAi, ptAj, ptAk,
ptBi, ptBj, ptBk;
// although indexpairs are pair<point, point>, where each point have
// double x,y,z, they are initialized to be integer for all non-collision
// springs. Hence using (int) here does not having any rounding.
ptAi = (int) s.indexpair.first.x;
ptAj = (int) s.indexpair.first.y;
ptAk = (int) s.indexpair.first.z;
ptBi = (int) s.indexpair.second.x;
ptBj = (int) s.indexpair.second.y;
ptBk = (int) s.indexpair.second.z;
// calculate L based on the actual position values of the two mass
// points connected by spring s and normalize L
pDIFFERENCE(jello->p[ptAi][ptAj][ptAk], jello->p[ptBi][ptBj][ptBk], normL);
pNORMALIZE(normL);
scaler = - kHook * (length - s.rest_length);
pMULTIPLY(normL, scaler, resultForce);
// Fhook = -kHook * (|L|-s.rest_length) * normalized L
return resultForce;
}
// computes Hook's force for a collision spring
point computeHookCollision(spring s, double kHook) {
point normL, resultForce;
double length, scaler;
// calculate L based on the actual position values of the two mass
// points connected by spring s and normalize L
pDIFFERENCE(s.indexpair.first, s.indexpair.second, normL);
pNORMALIZE(normL);
scaler = - kHook * (length-s.rest_length);
pMULTIPLY(normL, scaler, resultForce);
// Fhook = -kHook * (|L|-s.rest_length) * normalized L
return resultForce;
}
// computes damping force exerted on the first component of a spring
point computeDampElastic(spring s, double kDamping, point vA, point vB, struct world * jello) {
point normL, vAminusvB, resultForce;
double length, scaler, dotProd;
// calculate L
pDIFFERENCE(jello->p[(int) s.indexpair.first.x][(int) s.indexpair.first.y][(int) s.indexpair.first.z],
jello->p[(int) s.indexpair.second.x][(int) s.indexpair.second.y][(int) s.indexpair.second.z],
normL);
// calculate vA - vB
pDIFFERENCE(vA, vB, vAminusvB);
// calculate (vA-vB) dot L
dotProd = vAminusvB.x * normL.x + vAminusvB.y * normL.y + vAminusvB.z * normL.z;
// normalize L
pNORMALIZE(normL);
scaler = - kDamping * dotProd / length;
// calculate resultForce
pMULTIPLY(normL, scaler, resultForce);
// Fdamping = -kDamping * ((vA-vB) dot L) / |L| * normalized L
return resultForce;
}
// computes damping force for a collision spring
point computeDampCollision(spring s, double kDamping, point vA) {
point vB, normL, vAminusvB, resultForce;
double length, scaler, dotProd;
// vB is collision pt's velocity, which is 0
pMAKE(0,0,0, vB);
// calculate L
pDIFFERENCE(s.indexpair.first, s.indexpair.second, normL);
// calculate vA - vB
pDIFFERENCE(vA, vB, vAminusvB);
// calculate (vA-vB) dot L
dotProd = vAminusvB.x * normL.x + vAminusvB.y * normL.y + vAminusvB.z * normL.z;
// normalize L
pNORMALIZE(normL);
scaler = - kDamping * dotProd / length;
// calculate resultForce
pMULTIPLY(normL, scaler, resultForce);
// Fdamping = -kDamping * ((vA-vB) dot L) / |L| * normalized L
return resultForce;
}
// calculate forces on mass points given a vector of strings
// updates force array 'F'
void iterateSprings(vector<spring> * vec_of_springs, struct point F[8][8][8], struct world * jello) {
point FhookOnA, FdampingOnA, FfinalOnA, FhookOnB, FdampingOnB, FfinalOnB;
int pt_A_ind_x, pt_A_ind_y, pt_A_ind_z,
pt_B_ind_x, pt_B_ind_y, pt_B_ind_z;
// for each spring in a vector of springs
for (int i=0; i<vec_of_springs->size(); i++) {
// get indecies of two mass point of current spring
pt_A_ind_x = (int) vec_of_springs->at(i).indexpair.first.x;
pt_A_ind_y = (int) vec_of_springs->at(i).indexpair.first.y;
pt_A_ind_z = (int) vec_of_springs->at(i).indexpair.first.z;
pt_B_ind_x = (int) vec_of_springs->at(i).indexpair.second.x;
pt_B_ind_y = (int) vec_of_springs->at(i).indexpair.second.y;
pt_B_ind_z = (int) vec_of_springs->at(i).indexpair.second.z;
// compute Hook and damping forces exerted on the first point of the spring
FhookOnA = computeHookElastic(vec_of_springs->at(i), jello->kElastic, jello);
FdampingOnA = computeDampElastic(vec_of_springs->at(i), jello->dElastic,
jello->v[pt_A_ind_x][pt_A_ind_y][pt_A_ind_z],
jello->v[pt_B_ind_x][pt_B_ind_y][pt_B_ind_z], jello);
// sum up Hook and damping forces exerted on the first point of the spring
pSUM(FhookOnA, FdampingOnA, FfinalOnA);
// negate Hook and damping forces, they are now forces exerted on the
// second point of the spring by Newton's 3rd law
pNEG(FhookOnA, FhookOnB);
pNEG(FdampingOnA, FdampingOnB);
pSUM(FhookOnB, FdampingOnB, FfinalOnB);
// update the two points' force in F
F[pt_A_ind_x][pt_A_ind_y][pt_A_ind_z].x += FfinalOnA.x;
F[pt_A_ind_x][pt_A_ind_y][pt_A_ind_z].y += FfinalOnA.y;
F[pt_A_ind_x][pt_A_ind_y][pt_A_ind_z].z += FfinalOnA.z;
F[pt_B_ind_x][pt_B_ind_y][pt_B_ind_z].x += FfinalOnB.x;
F[pt_B_ind_x][pt_B_ind_y][pt_B_ind_z].y += FfinalOnB.y;
F[pt_B_ind_x][pt_B_ind_y][pt_B_ind_z].z += FfinalOnB.z;
}
}
// calculate penalty force when collision happens
// updates force array 'F'
void collisionResponse (spring collisionSpring, struct point F[8][8][8], point ptOfContact, struct world * jello) {
point collisionFinalForce;
// calculate collision Hook and damping force and sum them up
pSUM(computeHookCollision(collisionSpring, jello->kCollision),
computeDampCollision(collisionSpring, jello->dCollision, jello->v[i][j][k]),
collisionFinalForce);
// update F
F[i][j][k].x += collisionFinalForce.x;
F[i][j][k].y += collisionFinalForce.y;
F[i][j][k].z += collisionFinalForce.z;
}
// detects collision and handles them by calling collisionResponse()
// Note: the indexpair for a collision spring actually stores positions of
// a collision point and point of collision on a wall
void detectWallCollision (struct point F[8][8][8], struct world * jello) {
spring collisionSpring;
collisionSpring.rest_length = 0;
point ptOfContact, collisionHookForce, collisionDampForce, collisionFinalForce;
for (i=0; i<=7; i++)
for (j=0; j<=7; j++)
for (k=0; k<=7; k++)
{
// collision with +x wall
if (jello->p[i][j][k].x > 2) {
pMAKE(2, jello->p[i][j][k].y, jello->p[i][j][k].z, ptOfContact);
collisionSpring.indexpair = make_pair(jello->p[i][j][k], ptOfContact);
collisionResponse(collisionSpring, F, ptOfContact, jello);
}
// collision with -x wall
if (jello->p[i][j][k].x < -2) {
pMAKE(-2, jello->p[i][j][k].y, jello->p[i][j][k].z, ptOfContact);
collisionSpring.indexpair = make_pair(jello->p[i][j][k], ptOfContact);
collisionResponse(collisionSpring, F, ptOfContact, jello);
}
// collision with +y wall
if (jello->p[i][j][k].y > 2) {
pMAKE(jello->p[i][j][k].x, 2, jello->p[i][j][k].z, ptOfContact);
collisionSpring.indexpair = make_pair(jello->p[i][j][k], ptOfContact);
collisionResponse(collisionSpring, F, ptOfContact, jello);
}
// collision with -y wall
if (jello->p[i][j][k].y < -2) {
pMAKE(jello->p[i][j][k].x, -2, jello->p[i][j][k].z, ptOfContact);
collisionSpring.indexpair = make_pair(jello->p[i][j][k], ptOfContact);
collisionResponse(collisionSpring, F, ptOfContact, jello);
}
// collision with +z wall
if (jello->p[i][j][k].z > 2) {
pMAKE(jello->p[i][j][k].x, jello->p[i][j][k].y, 2, ptOfContact);
collisionSpring.indexpair = make_pair(jello->p[i][j][k], ptOfContact);
collisionResponse(collisionSpring, F, ptOfContact, jello);
}
// collision with -x wall
if (jello->p[i][j][k].z < -2) {
pMAKE(jello->p[i][j][k].x, jello->p[i][j][k].y, -2, ptOfContact);
collisionSpring.indexpair = make_pair(jello->p[i][j][k], ptOfContact);
collisionResponse(collisionSpring, F, ptOfContact, jello);
}
}
}
void detectPlaneCollision(struct point F[8][8][8], struct world *jello) {
spring collisionSpring;
collisionSpring.rest_length = 0;
// plane normal with correct sign has been calculated in jello.cpp's myinit()
point n = inclinedPlaneNormal.first;
point ptOfContact;
double currValueOfEq;
// calculate ptOfContact is to find orthogonal projection
// of a point p on jello cube onto plane defined by a unit normal [a,b,c]
// hence: orthog_proj(p) = p - ((p-v) dot n)*n
// reference: https://math.stackexchange.com/questions/444968/project-a-point-in-3d-on-a-given-plane
// v is just a point on plane used for calculating orthogonal projection of a point on cube onto
// collision plane
point v, PminusV;
double PminusVdotN;
pMAKE(0,0,-jello->d/jello->c, v);
for (i=0; i<=7; i++)
for (j=0; j<=7; j++)
for (k=0; k<=7; k++)
{
currValueOfEq = jello->a * jello->p[i][j][k].x +
jello->b * jello->p[i][j][k].y +
jello->c * jello->p[i][j][k].z + jello->d;
// if negSide flag is true, cube is on the negative side of the plane
// so negate value of equation to make <0 comparision hold
if (inclinedPlaneNormal.second == true) currValueOfEq=-currValueOfEq;
if(currValueOfEq < 0)
{
// below is the implementation of the reference link
pDIFFERENCE(jello->p[i][j][k], v, PminusV);
PminusVdotN = PminusV.x * n.x + PminusV.y * n.y + PminusV.z * n.z;
pMULTIPLY(n, PminusVdotN, ptOfContact);
pDIFFERENCE(jello->p[i][j][k], ptOfContact, ptOfContact);
// make the collision spring and response
collisionSpring.indexpair = make_pair(jello->p[i][j][k], ptOfContact);
collisionResponse(collisionSpring, F, ptOfContact, jello);
}
}
}
int checkCubePtIndexAndGetFFArrInd (double pos_value, double integralGridLength) {
// check if a point's component is out of negative world boundry.
// If yes, return correspond force field array index as 0.
if (pos_value < -2) return 0;
// check if a point's component is out of positive world boundry.
// If yes, return correspond force field array index as furtherest possible positive boundry
if (pos_value > 2) return (int) 4 / integralGridLength; // i.e. res - 1
// now the point's component is within the world boundary.
// Proof: Consider p.x. Known p.x >= (-2 + i * 4 / (res - 1), consider equality.
// -> (p.x+2) / (4/res-1) = i -> (p.x+2)/integralGridLength.
// Take floor() to only consider the "minimum" point on all x,y,z direction in a cube
return (int) floor((pos_value + 2) / integralGridLength);
}
void interpolateForceFiled(struct point F[8][8][8], struct world * jello) {
double res = jello->resolution;
// length of a integral grid, which is h
double integraGridLength = 4 / (res - 1);
// indecies
int fi,fj,fk, // for force field array (1D array)
alpha, beta, gamma; // for the point of interpolation (3D array)
// for storing value get from GET_FORCEFIELD_IND macro
int ind;
// eight corners of the integral grid;
point F000, F001, F010, F011, F100, F101, F110, F111;
for (i=0; i<=7; i++)
for (j=0; j<=7; j++)
for (k=0; k<=7; k++)
{
// compute nearest force field array index for curr point
fi = checkCubePtIndexAndGetFFArrInd(jello->p[i][j][k].x, integraGridLength);
fj = checkCubePtIndexAndGetFFArrInd(jello->p[i][j][k].y, integraGridLength);
fk = checkCubePtIndexAndGetFFArrInd(jello->p[i][j][k].z, integraGridLength);
// compute alpha, beta, gamma coefficients for interpolating curr point's
// force F(alpha, beta, gamma)
alpha = ( jello->p[i][j][k].x - (-2 + fi * integraGridLength) ) / integraGridLength;
beta = ( jello->p[i][j][k].y - (-2 + fj * integraGridLength) ) / integraGridLength;
gamma = ( jello->p[i][j][k].z - (-2 + fk * integraGridLength) ) / integraGridLength;
// get eight surrounding force vectors around curr point
GET_FORCEFIELD_IND(fi ,fj ,fk ,res, ind);
F000 = jello->forceField[ind];
GET_FORCEFIELD_IND(fi ,fj ,fk+1,res, ind);
F001 = jello->forceField[ind];
GET_FORCEFIELD_IND(fi ,fj+1,fk ,res, ind);
F010 = jello->forceField[ind];
GET_FORCEFIELD_IND(fi ,fj+1,fk+1,res, ind);
F011 = jello->forceField[ind];
GET_FORCEFIELD_IND(fi+1,fj ,fk ,res, ind);
F100 = jello->forceField[ind];
GET_FORCEFIELD_IND(fi+1,fj ,fk+1,res, ind);
F101 = jello->forceField[ind];
GET_FORCEFIELD_IND(fi+1,fj+1,fk ,res, ind);
F110 = jello->forceField[ind];
GET_FORCEFIELD_IND(fi+1,fj+1,fk+1,res, ind);
F111 = jello->forceField[ind];
// multiply corresponding coefficients to each of the eight surrounding force vectors
pMULTIPLY(F000, (1 - alpha) * (1 - beta) * (1 - gamma), F000);
pMULTIPLY(F001, (1 - alpha) * (1 - beta) * gamma , F001);
pMULTIPLY(F010, (1 - alpha) * beta * (1 - gamma), F010);
pMULTIPLY(F011, (1 - alpha) * beta * gamma , F011);
pMULTIPLY(F100, alpha * (1 - beta) * (1 - gamma), F100);
pMULTIPLY(F101, alpha * (1 - beta) * gamma , F101);
pMULTIPLY(F110, alpha * beta * (1 - gamma), F110);
pMULTIPLY(F111, alpha * beta * gamma , F111);
// do the trilinear interpolation to update force on curr point
F[i][j][k].x += (F000.x + F001.x + F010.x + F011.x + F100.x + F101.x + F110.x + F111.x);
F[i][j][k].y += (F000.y + F001.y + F010.y + F011.y + F100.y + F101.y + F110.y + F111.y);
F[i][j][k].z += (F000.z + F001.z + F010.z + F011.z + F100.z + F101.z + F110.z + F111.z);
}
}
// apply equal forces on all points of the jello given user drag
// direction and magnitude of the user drag has been determined in input.cpp
// here we only need to normalize the defined direction and multiply calculated
// magnitude to the normalize direction.
void handleUserDrag(struct point F[8][8][8], struct world * jello) {
double length;
point currDragForce = dragForceDirection;
pNORMALIZE(currDragForce);
// magnify drag force for worlds with <0.001 timestep
dragForceMagnitude = dragForceMagnitude * (1.0/jello->dt)/1000;
pMULTIPLY(currDragForce, dragForceMagnitude, currDragForce);
for (i=0; i<=7; i++)
for (j=0; j<=7; j++)
for (k=0; k<=7; k++)
{
F[i][j][k].x += currDragForce.x;
F[i][j][k].y += currDragForce.y;
F[i][j][k].z += currDragForce.z;
}
// clear magnitude for future time steps
dragForceMagnitude = 0;
}
/* Computes acceleration to every control point of the jello cube,
which is in state given by 'jello'.
Returns result in array 'a'. */
void computeAcceleration(struct world * jello, struct point a[8][8][8], vector<spring> * structuralSprings,
vector<spring> * shearSprings,
vector<spring> * bendSprings)
{
point F[8][8][8];
// initialize F to 0 to avoid garbage value (since forces cumulates)
for (i=0; i<=7; i++)
for (j=0; j<=7; j++)
for (k=0; k<=7; k++)
{
F[i][j][k].x = 0.0;
F[i][j][k].y = 0.0;
F[i][j][k].z = 0.0;
}
// iterate all springs and update elastic and damping forces on mass points
iterateSprings(structuralSprings,F,jello);
iterateSprings(shearSprings,F,jello);
iterateSprings(bendSprings,F,jello);
// detect collisions and respond collisions with penalty method
if (jello->incPlanePresent==1) detectPlaneCollision(F, jello);
detectWallCollision(F, jello);
// if a force field is present, update forces on mass point with trilinear interpolation
if (jello->resolution != 0) interpolateForceFiled(F, jello);
// if interactive mode is turned on by pressing 'i',
// and if user draged the left mouse, apply equal forces
// on all points of the jello
if(dragCase!=-1 && dragForceMagnitude>0) handleUserDrag(F, jello);
// at this stage, force matrix is finalized. Compute acceleration for each mass point.
for (i=0; i<=7; i++)
for (j=0; j<=7; j++)
for (k=0; k<=7; k++)
{
a[i][j][k].x = 1.0 / jello->mass * F[i][j][k].x;
a[i][j][k].y = 1.0 / jello->mass * F[i][j][k].y;
a[i][j][k].z = 1.0 / jello->mass * F[i][j][k].z;
}
}
/* performs one step of Euler Integration */
/* as a result, updates the jello structure */
void Euler(struct world * jello, vector<spring> * structuralSprings,
vector<spring> * shearSprings,
vector<spring> * bendSprings)
{
int i,j,k;
point a[8][8][8];
computeAcceleration(jello, a, structuralSprings, shearSprings, bendSprings);
for (i=0; i<=7; i++)
for (j=0; j<=7; j++)
for (k=0; k<=7; k++)
{
jello->p[i][j][k].x += jello->dt * jello->v[i][j][k].x;
jello->p[i][j][k].y += jello->dt * jello->v[i][j][k].y;
jello->p[i][j][k].z += jello->dt * jello->v[i][j][k].z;
jello->v[i][j][k].x += jello->dt * a[i][j][k].x;
jello->v[i][j][k].y += jello->dt * a[i][j][k].y;
jello->v[i][j][k].z += jello->dt * a[i][j][k].z;
}
}
/* performs one step of RK4 Integration */
/* as a result, updates the jello structure */
void RK4(struct world * jello, vector<spring> * structuralSprings,
vector<spring> * shearSprings,
vector<spring> * bendSprings)
{
point F1p[8][8][8], F1v[8][8][8],
F2p[8][8][8], F2v[8][8][8],
F3p[8][8][8], F3v[8][8][8],
F4p[8][8][8], F4v[8][8][8];
point a[8][8][8];
struct world buffer;
int i,j,k;
buffer = *jello; // make a copy of jello
computeAcceleration(&buffer, a, structuralSprings, shearSprings, bendSprings);
for (i=0; i<=7; i++)
for (j=0; j<=7; j++)
for (k=0; k<=7; k++)
{
pMULTIPLY(jello->v[i][j][k],jello->dt,F1p[i][j][k]);
pMULTIPLY(a[i][j][k],jello->dt,F1v[i][j][k]);
pMULTIPLY(F1p[i][j][k],0.5,buffer.p[i][j][k]);
pMULTIPLY(F1v[i][j][k],0.5,buffer.v[i][j][k]);
pSUM(jello->p[i][j][k],buffer.p[i][j][k],buffer.p[i][j][k]);
pSUM(jello->v[i][j][k],buffer.v[i][j][k],buffer.v[i][j][k]);
}
computeAcceleration(&buffer, a, structuralSprings, shearSprings, bendSprings);
for (i=0; i<=7; i++)
for (j=0; j<=7; j++)
for (k=0; k<=7; k++)
{
// F2p = dt * buffer.v;
pMULTIPLY(buffer.v[i][j][k],jello->dt,F2p[i][j][k]);
// F2v = dt * a(buffer.p,buffer.v);
pMULTIPLY(a[i][j][k],jello->dt,F2v[i][j][k]);
pMULTIPLY(F2p[i][j][k],0.5,buffer.p[i][j][k]);
pMULTIPLY(F2v[i][j][k],0.5,buffer.v[i][j][k]);
pSUM(jello->p[i][j][k],buffer.p[i][j][k],buffer.p[i][j][k]);
pSUM(jello->v[i][j][k],buffer.v[i][j][k],buffer.v[i][j][k]);
}
computeAcceleration(&buffer, a, structuralSprings, shearSprings, bendSprings);
for (i=0; i<=7; i++)
for (j=0; j<=7; j++)
for (k=0; k<=7; k++)
{
// F3p = dt * buffer.v;
pMULTIPLY(buffer.v[i][j][k],jello->dt,F3p[i][j][k]);
// F3v = dt * a(buffer.p,buffer.v);
pMULTIPLY(a[i][j][k],jello->dt,F3v[i][j][k]);
pMULTIPLY(F3p[i][j][k],1.0,buffer.p[i][j][k]);
pMULTIPLY(F3v[i][j][k],1.0,buffer.v[i][j][k]);
pSUM(jello->p[i][j][k],buffer.p[i][j][k],buffer.p[i][j][k]);
pSUM(jello->v[i][j][k],buffer.v[i][j][k],buffer.v[i][j][k]);
}
computeAcceleration(&buffer, a, structuralSprings, shearSprings, bendSprings);
for (i=0; i<=7; i++)
for (j=0; j<=7; j++)
for (k=0; k<=7; k++)
{
// F3p = dt * buffer.v;
pMULTIPLY(buffer.v[i][j][k],jello->dt,F4p[i][j][k]);
// F3v = dt * a(buffer.p,buffer.v);
pMULTIPLY(a[i][j][k],jello->dt,F4v[i][j][k]);
pMULTIPLY(F2p[i][j][k],2,buffer.p[i][j][k]);
pMULTIPLY(F3p[i][j][k],2,buffer.v[i][j][k]);
pSUM(buffer.p[i][j][k],buffer.v[i][j][k],buffer.p[i][j][k]);
pSUM(buffer.p[i][j][k],F1p[i][j][k],buffer.p[i][j][k]);
pSUM(buffer.p[i][j][k],F4p[i][j][k],buffer.p[i][j][k]);
pMULTIPLY(buffer.p[i][j][k],1.0 / 6,buffer.p[i][j][k]);
pSUM(buffer.p[i][j][k],jello->p[i][j][k],jello->p[i][j][k]);
pMULTIPLY(F2v[i][j][k],2,buffer.p[i][j][k]);
pMULTIPLY(F3v[i][j][k],2,buffer.v[i][j][k]);
pSUM(buffer.p[i][j][k],buffer.v[i][j][k],buffer.p[i][j][k]);
pSUM(buffer.p[i][j][k],F1v[i][j][k],buffer.p[i][j][k]);
pSUM(buffer.p[i][j][k],F4v[i][j][k],buffer.p[i][j][k]);
pMULTIPLY(buffer.p[i][j][k],1.0 / 6,buffer.p[i][j][k]);
pSUM(buffer.p[i][j][k],jello->v[i][j][k],jello->v[i][j][k]);
}
return;
}