Repository navigation
Expand file tree
/
Copy pathscene-components.js
More file actions
2071 lines (1916 loc) · 93.1 KB
/
Copy pathscene-components.js
File metadata and controls
2071 lines (1916 loc) · 93.1 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
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
/****************************************************************************
* Code adopted from
* "UCLA's Graphics Example Code (Javascript and C++ translations available),
* by Garett Ridge for CS174a"
*
* The Scene_Component subclasses defined here describe different independent
* animation processes that you want to fire off each frame, by defining a
* display event and how to react to key and mouse input events. Create your
* own subclasses, and fill them in with all your shape drawing calls and any
* extra key / mouse controls.
****************************************************************************/
// DISCLAIMER: The collision method shown below is not used by anyone; it's
// just very quick to code. Making every collision body a stretched sphere is
// kind of a hack, and looping through a list of discrete sphere points to see
// if the volumes intersect is *really* a hack (there are perfectly good
// analytic expressions that can test if two ellipsoids intersect without
// discretizing them into points). On the other hand, for non-convex shapes
// you're usually going to have to loop through a list of discrete
// tetrahedrons defining the shape anyway.
Declare_Any_Class("Body",
{
'construct'(name, s, m, loc, scale = vec3(1, 1, 1), lv = vec3(),
la = vec3(), av = 0, aa = 0, spin_axis = vec3(1, 0, 0))
{
this.define_data_members({
name : name,
shape : s,
material : m,
scale : scale,
location_matrix : loc,
linear_velocity : lv,
linear_acceleration : la,
angular_velocity : av,
angular_acceleration : aa,
spin_axis : spin_axis,
prev_location_matrix : null,
prev_linear_velocity : null,
prev_angular_velocity : null,
collided_last_frame : false,
collision_history : [{"name":null, "collided":null}, {"name":null, "collided":null}]
});
},
'advance'(t) // Do one timestep.
{
this.prev_location_matrix = this.location_matrix;
if (Array.isArray(t) && t.length == 4) {
this.location_matrix = t;
} else {
var delta_velocity = function(aVelocity, aAcceleration) {
var i;
var delta;
for (i = 0; i < 3; i++) {
delta = aVelocity[i] + aAcceleration[i];
if ((aVelocity[i] > 0 && delta < 0) || // + -> - velocity
(aVelocity[i] < 0 && delta > 0) || // - -> + velocity
(aVelocity[i] === 0 && i != 2)) { // 0 z-velocity
aVelocity[i] = 0;
if (i !== 2)
aAcceleration[i] = 0;
}
else
aVelocity[i] = delta;
}
return aVelocity;
}
var delta;
this.linear_velocity = delta_velocity(
this.linear_velocity,
this.linear_acceleration
);
this.prev_linear_velocity = this.linear_velocity;
// Move proportionally to real time.
delta = translation(scale_vec(t, this.linear_velocity));
// Apply linear velocity - pre-multiply to keep translations together
this.location_matrix = mult(delta, this.location_matrix);
// Move proportionally to real time.
delta = rotation(t * this.angular_velocity, this.spin_axis);
// Apply angular velocity - post-multiply to keep rotations together
this.location_matrix = mult(this.location_matrix, delta);
this.prev_angular_velocity = this.angular_velocity;
}
},
'check_if_colliding'(b, a_inv, shape)
{
if (this == b) // Nothing collides with itself
return false;
// Convert sphere b to a coordinate frame where a is a unit sphere
var T = mult(a_inv, mult(b.location_matrix, scale(b.scale)));
for (let p of shape.positions) { // For each vertex in that b,
// Apply a_inv*b coordinate frame shift
var Tp = mult_vec(T, p.concat(1)).slice(0, 3);
// Check if in that coordinate frame it penetrates the unit sphere
// at the origin.
if (dot(Tp, Tp) < 1.2)
return true;
}
return false;
},
'check_if_collided_with_plane'(b, a_inv, shape) // b is stationary
{
var intersect = function(point1, point2, plane) {
var x1 = point1[0];
var y1 = point1[1];
var z1 = point1[2];
var x2 = point2[0];
var y2 = point2[1];
var z2 = point2[2];
var a = plane[0];
var b = plane[1];
var c = plane[2];
var d = plane[3];
var t = (d - (x1 * a) - (y1 * b) - (z1 * c)) / ((x2 * a) + (y2 * b) + (z2 * c) - (x1 * a) - (y1 * b) - (z1 * c));
var px = x1 + (t * (x2 - x1));
var py = y1 + (t * (y2 - y1));
var pz = z1 + (t * (z2 - z1));
return [px, py, pz];
}
var dist = function(point1, point2) {
var x1 = point1[0];
var y1 = point1[1];
var z1 = point1[2];
var x2 = point2[0];
var y2 = point2[1];
var z2 = point2[2];
var zDiff = z2 - z1;
var yDiff = y2 - y1;
var xDiff = x2 - x1;
var radicant = zDiff * zDiff + yDiff * yDiff + xDiff * xDiff;
var result = Math.pow(radicant, 0.5);
return result;
}
//console.log("Inside checking collision");
var i;
var p1 = mult_vec(this.prev_location_matrix, vec4(0, 0, 0, 1)).slice(0, 3);
//if (b.name == "fence1") {
//console.log("p1: ");
//console.log(p1);
//}
var p2 = mult_vec(this.location_matrix, vec4(0, 0, 0, 1)).slice(0, 3);
//if (b.name == "fence1") {
//console.log("p2: ");
//console.log(p2);
//}
var pVec = subtract(p1, p2);
//console.log("pVec: ");
//console.log(pVec);
var bCenter = mult_vec(b.location_matrix, vec4(0, 0, 0, 1)).slice(0, 3);
//if (b.name == "fence1") {
//console.log("bCenter: ");
//console.log(bCenter);
//}
var d = -((pVec[0] * -bCenter[0]) + (pVec[1] * - bCenter[1]) + (pVec[2] * -bCenter[2])); // Last component of plane
var intersection = intersect(p1, p2, pVec.concat(d));
//if (b.name == "fence1") {
//console.log("Intersection: ");
//console.log(intersection);
//}
var new_loc = translation(intersection);
var new_a_inv = inverse(mult(new_loc, scale(this.scale)));
var insideObj = false;
var b_inv = inverse(mult( b.location_matrix, scale(b.scale)));
//var T = mult(new_a_inv, mult(b.location_matrix, scale(b.scale)));
var T = mult(b_inv, mult(new_loc, scale(this.scale)));
for (let p of shape.positions) { // For each vertex in that b,
// Apply a_inv*b coordinate frame shift
var Tp = mult_vec(T, p.concat(1)).slice(0, 3);
// Check if in that coordinate frame it penetrates the unit sphere
// at the origin.
if (dot(Tp, Tp) < 1.2) {
insideObj = true;
break;
}
}
// Check that intersection point is between the two points
//if (b.name == "fence1")
//console.log("Distance 1: ");
//if (b.name == "fence1")
//console.log(dist(p1, intersection));
//if (b.name == "fence1")
//console.log("Distance 2: ");
//if (b.name == "fence1")
//console.log(dist(intersection, p2));
//if (b.name == "fence1")
//console.log("Real total distance: ");
//if (b.name == "fence1")
//console.log(dist(p1, p2));
//if (this.collided_last_frame && dist(p1, bCenter) < dist(p2, bCenter))
//return false;
var betweenPoints = dist(p1, intersection) + dist(intersection, p2) === dist(p1, p2);
//if (b.name == "fence1")
//console.log("Between points: " + betweenPoints);
//if (betweenPoints && (b.name == "fence1")) {
//console.log("p1: " + p1);
//console.log("Intersection: " + intersection);
//console.log("p2: " + p2);
//}
if (betweenPoints && insideObj) {
//console.log(this.collision_history);
if (this.collision_history[1].name == b.name && this.collision_history[1].collided == true) {
//console.log("Prevented double collision");
return false;
}
//console.log("reverted");
this.location_matrix = this.prev_location_matrix;
this.linear_velocity = this.prev_linear_velocity;
this.angular_velocity = this.prev_angular_velocity;
return true;
}
else {
return false;
}
},
'collided'(b, a_inv, shape)
{
var res;
res = this.check_if_colliding(b, a_inv, shape) || this.check_if_collided_with_plane(b, a_inv, shape);
this.collided_last_frame = res;
if (res) {
this.collision_history.push({"name": b.name, "collided":res});
this.collision_history.shift();
}
return res;
}
});
Declare_Any_Class("Baseball_Scene",
{
'construct'(context, canvas = context.canvas)
{
/*
* Draws the baseball field.
* @param {Object} oScene - The baseball scene.
* @returns {Object[]} The transformation matrix to the surface of the
* field.
*/
var draw_field = function(oScene) {
var tile_dimensions = {
"length" : 2 * 150, // x
"width" : 2 * 150, // y
"height" : 2 * 1 // z
};
var tilesX = 10;
var tilesY = 10;
var ground = vec3(tile_dimensions.length / 2, tile_dimensions.length / 2, 4);
var ground_center = identity();
var surface = mult(ground_center, translation(0, 0, 4));
var partial_sphere = mult(surface, translation(1.5, 300, -4));
var mound = vec3(30, 30, 10);
var infield = mult(surface, translation(0, 50, 1.0001));
var diamond = vec3(500, 500, 1);
var chalk;
var chalk_scale = vec3(500, 1, 1);
var offset;
var tile;
var i;
var j;
var gate_start = mult(surface, translation(0, 45, 1.3));
var transform;
var gate_transform;
var gate_scale = vec3(20, 5, 20);
var pole;
var pole_scale = vec3(5, 5, 300);
var outfield_gate;
var outfield_gate_scale = vec3(100, 5, 100);
var base;
var bat_box;
// Draw grass
/*for (i = 0; i < tile_dimensions.length * tilesX; i += tile_dimensions.length) {
for (offset = 0; offset < tile_dimensions.width * tilesY; offset += tile_dimensions.width) {
tile = mult(ground_center, translation(i, offset, 0));
oScene.bodies.auto.push(new Body("grass", oScene.shapes.box, oScene.grass, tile, ground));
if (i / tile_dimensions.length < 3 && offset / tile_dimensions.width < 6)
oScene.bodies.ball_collision_items.push(new Body("grass", oScene.shapes.box, oScene.grass, tile, ground));
if (offset !== 0) {
tile = mult(ground_center, translation(i, -offset, 0));
oScene.bodies.auto.push(new Body("grass", oScene.shapes.box, oScene.grass, tile, ground));
}
}
if (i !== 0) {
for (offset = 0; offset < tile_dimensions.width * tilesY; offset += tile_dimensions.width) {
tile = mult(ground_center, translation(-i, offset, 0));
oScene.bodies.auto.push(new Body("grass", oScene.shapes.box, oScene.grass, tile, ground));
if (i / tile_dimensions.length < 3 && offset / tile_dimensions.width < 6)
oScene.bodies.ball_collision_items.push(new Body("grass", oScene.shapes.box, oScene.grass, tile, ground));
if (offset !== 0) {
tile = mult(ground_center, translation(-i, -offset, 0));
oScene.bodies.auto.push(new Body("grass", oScene.shapes.box, oScene.grass, tile, ground));
}
}
}
}*/
base = mult(translation(0, 515, 4.5), rotation(45, [0, 0, 1]));
oScene.bodies.auto.push(new Body("base", oScene.shapes.box, oScene.base, base, vec3(6, 6, 1))); // Second base
oScene.bodies.auto.push(new Body("base", oScene.shapes.base, oScene.base, translation(0, 120, 4.5), vec3(8, 8, 1))); // Home plate
oScene.bodies.auto.push(new Body("grass", oScene.shapes.box, oScene.large_grass, ground_center, vec3(2800, 2800, 4)));
//oScene.bodies.ball_collision_items.push(new Body("grass", oScene.shapes.box, oScene.large_grass, ground_center, vec3(1500, 1500, 4)));
// Draw batter's boxes
oScene.bodies.auto.push(new Body("base", oScene.shapes.dbox, oScene.chalk, translation(-10, 120, 4.1), vec3(6, 15, 1)));
oScene.bodies.auto.push(new Body("base", oScene.shapes.dbox, oScene.chalk, translation(10, 120, 4.1), vec3(6, 15, 1)));
// Draw pitcher's mound
oScene.bodies.auto.push(new Body("mound", oScene.shapes.ball, oScene.infield_dirt, partial_sphere, mound));
// Draw baseball diamond
infield = mult(infield, rotation(45, [0,0,1]));
oScene.bodies.auto.push(new Body("infield", oScene.shapes.diamond, oScene.infield_dirt, infield, diamond));
//oScene.bodies.ball_collision_items.push(new Body("infield", oScene.shapes.diamond, oScene.infield_dirt, infield, diamond));
// Draw baseline, bases, foul poles, and outfield fences
chalk = mult(surface, translation(-17, 170, .1));
chalk = mult(chalk, rotation(-45, [0, 0, 1]));
pole = chalk;
base = chalk;
chalk = mult(chalk, translation(-500, 0, 0));
oScene.bodies.auto.push(new Body("foul_line", oScene.shapes.box, oScene.chalk, chalk, chalk_scale));
base = mult(base, translation(-240, 10, .5));
pole = mult(pole, translation(-1000, 0, 0));
oScene.bodies.auto.push(new Body("base", oScene.shapes.box, oScene.base, base, vec3(6, 6, 1))); // Third base
oScene.bodies.auto.push(new Body("foul_pole", oScene.shapes.cylinder, oScene.yellow_paint, pole, pole_scale));
outfield_gate = mult(pole, translation(0, 125, -70));
outfield_gate = mult(outfield_gate, rotation(90, [0, 0, 1]));
outfield_gate = mult(outfield_gate, rotation(45, [0, 1, 0]));
oScene.bodies.auto.push(new Body("fence", oScene.shapes.fence, oScene.chain, outfield_gate, outfield_gate_scale));
oScene.bodies.ball_collision_items.push(new Body("fence", oScene.shapes.fence, oScene.chain, outfield_gate, outfield_gate_scale));
outfield_gate = mult(pole, translation(0, 387, -70));
outfield_gate = mult(outfield_gate, rotation(90, [0, 0, 1]));
outfield_gate = mult(outfield_gate, rotation(45, [0, 1, 0]));
oScene.bodies.auto.push(new Body("fence", oScene.shapes.fence, oScene.chain, outfield_gate, outfield_gate_scale));
oScene.bodies.ball_collision_items.push(new Body("fence", oScene.shapes.fence, oScene.chain, outfield_gate, outfield_gate_scale));
outfield_gate = mult(pole, translation(0, 650, -70));
outfield_gate = mult(outfield_gate, rotation(90, [0, 0, 1]));
outfield_gate = mult(outfield_gate, rotation(45, [0, 1, 0]));
oScene.bodies.auto.push(new Body("fence", oScene.shapes.fence, oScene.chain, outfield_gate, outfield_gate_scale));
oScene.bodies.ball_collision_items.push(new Body("fence", oScene.shapes.fence, oScene.chain, outfield_gate, outfield_gate_scale));
outfield_gate = mult(pole, translation(60, 864.5, -70));
outfield_gate = mult(outfield_gate, rotation(50, [0, 0, 1]));
outfield_gate = mult(outfield_gate, rotation(45, [0, 1, 0]));
oScene.bodies.auto.push(new Body("fence", oScene.shapes.fence, oScene.chain, outfield_gate, outfield_gate_scale));
oScene.bodies.ball_collision_items.push(new Body("fence", oScene.shapes.fence, oScene.chain, outfield_gate, outfield_gate_scale));
chalk = mult(surface, translation(17, 170, .1));
chalk = mult(chalk, rotation(45, [0, 0, 1]));
pole = chalk;
base = chalk;
chalk = mult(chalk, translation(500, 0, 0));
oScene.bodies.auto.push(new Body("foul_line", oScene.shapes.box, oScene.chalk, chalk, chalk_scale));
base = mult(base, translation(240, 10, .5));
pole = mult(pole, translation(1000, 0, 0));
oScene.bodies.auto.push(new Body("base", oScene.shapes.box, oScene.base, base, vec3(6, 6, 1))); // First base
oScene.bodies.auto.push(new Body("foul_pole", oScene.shapes.cylinder, oScene.yellow_paint, pole, pole_scale));
outfield_gate = mult(pole, translation(0, 125, -70));
outfield_gate = mult(outfield_gate, rotation(-90, [0, 0, 1]));
outfield_gate = mult(outfield_gate, rotation(45, [0, 1, 0]));
oScene.bodies.auto.push(new Body("fence", oScene.shapes.fence, oScene.chain, outfield_gate, outfield_gate_scale));
oScene.bodies.ball_collision_items.push(new Body("fence", oScene.shapes.fence, oScene.chain, outfield_gate, outfield_gate_scale));
outfield_gate = mult(pole, translation(0, 387, -70));
outfield_gate = mult(outfield_gate, rotation(-90, [0, 0, 1]));
outfield_gate = mult(outfield_gate, rotation(45, [0, 1, 0]));
oScene.bodies.auto.push(new Body("fence", oScene.shapes.fence, oScene.chain, outfield_gate, outfield_gate_scale));
oScene.bodies.ball_collision_items.push(new Body("fence", oScene.shapes.fence, oScene.chain, outfield_gate, outfield_gate_scale));
outfield_gate = mult(pole, translation(0, 650, -70));
outfield_gate = mult(outfield_gate, rotation(-90, [0, 0, 1]));
outfield_gate = mult(outfield_gate, rotation(45, [0, 1, 0]));
oScene.bodies.auto.push(new Body("fence", oScene.shapes.fence, oScene.chain, outfield_gate, outfield_gate_scale));
oScene.bodies.ball_collision_items.push(new Body("fence", oScene.shapes.fence, oScene.chain, outfield_gate, outfield_gate_scale));
outfield_gate = mult(pole, translation(-60, 864.5, -70));
outfield_gate = mult(outfield_gate, rotation(-50, [0, 0, 1]));
outfield_gate = mult(outfield_gate, rotation(45, [0, 1, 0]));
oScene.bodies.auto.push(new Body("fence", oScene.shapes.fence, oScene.chain, outfield_gate, outfield_gate_scale));
oScene.bodies.ball_collision_items.push(new Body("fence", oScene.shapes.fence, oScene.chain, outfield_gate, outfield_gate_scale));
// Draw backstop
gate_transform = mult(gate_start, translation(0, 0, 25));
gate_transform = mult(gate_transform, rotation(45, [0, 1, 0]));
oScene.bodies.auto.push(new Body("fence", oScene.shapes.fence, oScene.chain, gate_transform, gate_scale));
oScene.bodies.ball_collision_items.push(new Body("fence1", oScene.shapes.fence, oScene.chain, gate_transform, gate_scale));
gate_transform = mult(gate_start, translation(63 * Math.cos(radians(45)), 26 * Math.sin(radians(45)), 25));
gate_transform = mult(gate_transform, rotation(45, [0, 0, 1]));
gate_transform = mult(gate_transform, rotation(45, [0, 1, 0]));
oScene.bodies.auto.push(new Body("fence", oScene.shapes.fence, oScene.chain, gate_transform, gate_scale));
oScene.bodies.ball_collision_items.push(new Body("fence", oScene.shapes.fence, oScene.chain, gate_transform, gate_scale));
gate_transform = mult(gate_start, translation(-63 * Math.cos(radians(45)), 26 * Math.sin(radians(45)), 25));
gate_transform = mult(gate_transform, rotation(-45, [0, 0, 1]));
gate_transform = mult(gate_transform, rotation(45, [0, 1, 0]));
oScene.bodies.auto.push(new Body("fence", oScene.shapes.fence, oScene.chain, gate_transform, gate_scale));
oScene.bodies.ball_collision_items.push(new Body("fence", oScene.shapes.fence, oScene.chain, gate_transform, gate_scale));
return surface;
};
/*
* Draws a baseball player as a batter.
* @param {Object} oScene - The baseball scene.
* @param {Object[]} mSurface - The transformation matrix to the
* surface of the ground.
*/
var draw_batter = function(oScene, mSurface) {
var body_top;
var head_center;
var head_dimensions = {
"length" : 2 * 4,
"width" : 2 * 4,
"height" : 2 * 4
};
var helmet_center;
var helmet_pos;
var helmet_size = vec3(
head_dimensions.length / 2,
head_dimensions.width / 2,
head_dimensions.height / 2
);
/*
* Draws the head of the player.
* @param {Object} oScene - The baseball scene.
* @param {Object[]} mBody - The transformation matrix to
* the top of the body.
* @param {Object} oHeadDimensions - The dimensions of the head.
* @returns {Object[]} The transformation matrix for the center of
* the head.
*/
var draw_head = function(oScene, mBody, oHeadDimensions) {
var offset = -.35; // Extra offset to place the head nicely on the body
var center = mult(mBody, translation(
0,
0,
offset + oHeadDimensions.height / 2)
);
var eyes = mult(center, rotation(175, [0, 0, 1]));
eyes = mult(eyes, rotation(-20, [0, 1, 0]));
eyes = mult(eyes, rotation(-50, [1, 0, 0]));
face = vec3(
-oHeadDimensions.length / 2,
oHeadDimensions.width / 2,
oHeadDimensions.height / 2
);
oScene.bodies.auto.push(new Body(
"head",
oScene.shapes.sphere,
oScene.face,
eyes,
face
));
return center;
};
/*
* Draws the body of the player.
* @param {Object} oScene - The baseball scene.
* @param {Object[]} mSurface - The transformation matrix to the
* surface of the ground.
* @returns {Object[]} The transformation matrix for the top of
* the body.
*/
var draw_body = function(oScene, mSurface) {
var body;
var zScale = 8;
var bodyScale = 6;
var body_scale_transform = vec3(bodyScale, bodyScale, zScale);
var bodyTop = mult(mSurface, translation(-10, 125, 8 + (zScale * .2)));
var leftHinge;
var rightHinge;
var bat;
var batScaled = vec3(7, 7, 7);
var rot1;
var rot2;
var swing;
var rotAngle;
// Draw main body
body = mult(mSurface, translation(-10, 125, 8));
body = mult(body, rotation(90, [0, 0, 1]));
oScene.bodies.auto.push(new Body(
"body",
oScene.shapes.tube,
oScene.cloth,
body,
body_scale_transform
));
return bodyTop;
};
/*
* Draws the shoes of the player.
* @param {Object} oShape - The Shape to draw.
* @param {Object} oMaterial - The material to use to color the
* drawn shape.
* @param {Object[]} mSurface - The transformation matrix to the
* surface of the ground.
*/
var draw_shoes = function(oScene, mSurface) {
var shoe_dimensions = {
"length" : 2 * 2, // x
"width" : 2 * 4, // y
"height" : 2 // z
};
var sizeShoe = vec3(
shoe_dimensions.length / 2,
shoe_dimensions.width / 2,
shoe_dimensions.height
);
var pos = mult(mSurface, translation(-10, 120, 1));
var flipShoe = rotation(180, [1, 0, 0]);
flipShoe = mult(flipShoe, rotation(90, [0, 0, 1]));
// First shoe
pos = mult(pos, flipShoe);
oScene.bodies.auto.push(new Body(
"shoe",
oScene.shapes.half_sphere,
oScene.rubber,
pos,
sizeShoe
));
// Second shoe
pos = mult(mSurface, translation(-10, 130, 1));
pos = mult(pos, flipShoe);
oScene.bodies.auto.push(new Body(
"shoe",
oScene.shapes.half_sphere,
oScene.rubber,
pos,
sizeShoe
));
};
draw_shoes(oScene, mSurface);
body_top = draw_body(oScene, mSurface);
head_center = draw_head(oScene, body_top, head_dimensions);
// Helmet
helmet_pos = mult(head_center, translation(
-.15 * head_dimensions.length / 2,
.1 * head_dimensions.width / 2,
.9 * head_dimensions.height / 2)
);
helmet_pos = mult(helmet_pos, rotation(165, [0, 0, 1]));
oScene.bodies.auto.push(new Body(
"helmet",
oScene.shapes.helmet,
oScene.helmet_paint,
helmet_pos,
helmet_size
));
};
/*
* Draws a baseball player as a fielder.
* @param {Object} oScene - The baseball scene.
* @param {Object[]} mSurface - The transformation matrix to the
* surface of the ground.
*/
var draw_fielder = function(oScene, mSurface) {
var body_top;
var head_center;
var head_dimensions = {
"length" : 2 * 4,
"width" : 2 * 4,
"height" : 2 * 4
};
var cap_pos;
var cap_size;
/*
* Draws the head of the player.
* @param {Object} oScene - The baseball scene
* @param {Object[]} mBody - The transformation matrix to
* the top of the body.
* @param {Object} oHeadDimensions - The dimensions of the head.
* @returns {Object[]} The transformation matrix for the center of
* the head.
*/
var draw_head = function(oScene, mBody, oHeadDimensions) {
var offset = -.35; // Extra offset to place the head nicely on the body
var center = mult(mBody, translation(
0,
0,
offset + oHeadDimensions.height / 2)
);
var eyes = mult(center, rotation(-40, [1, 0, 0]));
eyes = mult(eyes, rotation(-30, [0, 1, 0]));
face = vec3(
-oHeadDimensions.length / 2,
oHeadDimensions.width / 2,
oHeadDimensions.height / 2
);
oScene.bodies.auto.push(new Body(
"head",
oScene.shapes.sphere,
oScene.face,
eyes,
face
));
return center;
};
/*
* Draws the body of the player.
* @param {Object} oScene - The baseball scene.
* @param {Object[]} mSurface - The transformation matrix to the
* surface of the ground.
* @returns {Object[]} The transformation matrix for the top of
* the body.
*/
var draw_body = function(oScene, mSurface) {
var body;
var zScale = 8;
var bodyScale = 6;
var body_scale_transform = vec3(bodyScale, bodyScale, zScale);
var bodyTop = mult(mSurface, translation(1.5, 295, 12 + (zScale * .2)));
var leftHinge;
var rightHinge;
var rot1;
var rot2;
var glove;
var glove_size = vec3(1.5, 1.5, 1.5);
var throwMotion;
var gloveMotion;
var rotAngle;
/*
* Draws an arm of the player.
* @param {Object} oScene - The baseball scene.
* @param {Object[]} mPos - The transformation matrix to the
* position to draw the arm.
*/
var draw_arm = function(oScene, mPos) {
var arm = mPos;
var armScale = 8;
armScale = vec3(1, 1, armScale);
oScene.bodies.manual.push(new Body(
"arm",
oScene.shapes.tube,
oScene.cloth,
arm,
armScale
));
};
// Draw main body
body = mult(mSurface, translation(1.5, 295, 12));
body = mult(body, rotation(90, [0, 0, 1]));
oScene.bodies.auto.push(new Body(
"body",
oScene.shapes.tube,
oScene.cloth,
body,
body_scale_transform
));
return bodyTop;
};
/*
* Draws the shoes of the player.
* @param {Object} oScene - The baseball scene.
* @param {Object[]} mSurface - The transformation matrix to the
* surface of the ground.
*/
var draw_shoes = function(oScene, mSurface) {
var shoe_dimensions = {
"length" : 2 * 2, // x
"width" : 2 * 4, // y
"height" : 2 // z
};
var sizeShoe = vec3(
shoe_dimensions.length / 2,
-shoe_dimensions.width / 2,
shoe_dimensions.height
);
var pos = mult(mSurface, translation(1.5, 290, 5.5));
var flipShoe = rotation(180, [1, 0, 0]);
flipShoe = mult(flipShoe, rotation(90, [0, 0, 1]));
// First shoe
pos = mult(pos, flipShoe);
oScene.bodies.auto.push(new Body(
"shoe",
oScene.shapes.half_sphere,
oScene.rubber,
pos,
sizeShoe
));
// Second shoe
pos = mult(mSurface, translation(1.5, 300, 6));
pos = mult(pos, flipShoe);
oScene.bodies.auto.push(new Body(
"shoe",
oScene.shapes.half_sphere,
oScene.rubber,
pos,
sizeShoe
));
};
draw_shoes(oScene, mSurface);
body_top = draw_body(oScene, mSurface);
head_center = draw_head(oScene, body_top, head_dimensions);
// Cap
cap_pos = mult(head_center, translation(
-.01 * head_dimensions.length / 2,
-.09 * head_dimensions.width / 2,
.7 * head_dimensions.height / 2)
);
cap_pos = mult(cap_pos, rotation(90, [1, 0, 0]));
cap_pos = mult(cap_pos, rotation(-90, [0, 1, 0]));
cap_size = vec3(
head_dimensions.length / 2,
head_dimensions.width / 2,
head_dimensions.height / 2
);
oScene.bodies.auto.push(new Body(
"cap",
oScene.shapes.cap,
oScene.wool,
cap_pos,
cap_size
));
};
var surface;
var shapes = {
"box" : new Cube(),
"dbox" : new Donut_Box(),
"ball" : new Shape_From_File("resources/baseball.obj"),
"base" : new Base(),
"sphere" : new Subdivision_Sphere(5),
"diamond" : new Quarter_Circle_Diamond(),
"bat" : new Shape_From_File("resources/bat.obj"),
"half_sphere" : new Half_Sphere(15, 15),
"helmet" : new Shape_From_File("resources/helmet.obj"),
"tube" : new Body_Tube(15, 15),
"cap" : new Shape_From_File("resources/cap.obj"),
"mitt" : new Shape_From_File("resources/glove.obj"),
"square" : new Square(),
"sqhole" : new Square_Hole(),
"cylinder" : new Rounded_Capped_Cylinder(20, 20),
"fence" : new Fence(20, 20)
};
this.submit_shapes(context, shapes);
this.define_data_members({
// Materials
cork_stitch :
context.shaders_in_use["Phong_Model"].material(
Color(0, 0, 0, 1), 1, 1, 0, 40,
context.textures_in_use["stitching.jpg"]
),
base :
context.shaders_in_use["Phong_Model"].material(
Color(1, 1, 1, 1), .5, 1, .7, 40
),
infield_dirt :
context.shaders_in_use["Phong_Model"].material(
Color(230/255, 204/255, 179/255, 1), .5, 1, .7, 40
),
grass :
context.shaders_in_use["Phong_Model"].material(
Color(0, 0, 0, 1), 1, 1, 0, 40,
context.textures_in_use["grass.jpg"]
),
large_grass :
context.shaders_in_use["Phong_Model"].material(
Color(0, 0, 0, 1), 1, 1, 0, 40,
context.textures_in_use["large_grass.png"]
),
wood :
context.shaders_in_use["Phong_Model"].material(
Color(0, 0, 0, 1), 1, 1, 0, 40
),
helmet_paint :
context.shaders_in_use["Phong_Model"].material(
Color(0, 0, 0, 1), 1, 1, 0, 40,
context.textures_in_use["helmet_paint.jpg"]
),
face :
context.shaders_in_use["Phong_Model"].material(
Color(0, 0, 0, 1), 1, 1, 0, 40,
context.textures_in_use["face.jpg"]
),
rubber :
context.shaders_in_use["Phong_Model"].material(
Color(0, 0, 0, 1), 1, 1, 0, 40,
context.textures_in_use["shoe_color.jpg"]
),
cloth :
context.shaders_in_use["Phong_Model"].material(
Color(204/255, 204/255, 204/255, 1), .5, 1, .7, 40
),
skin :
context.shaders_in_use["Phong_Model"].material(
Color(255/255, 213/255, 105/255, 1), .9, 1, .7, 40
),
wool :
context.shaders_in_use["Phong_Model"].material(
Color(0, 0, 1, 1), .5, 1, .7, 40
),
leather :
context.shaders_in_use["Phong_Model"].material(
Color(0, 0, 0, 1), 1, 1, 0, 40,
context.textures_in_use["glove_leather.jpg"]
),
chalk :
context.shaders_in_use["Phong_Model"].material(
Color(1, 1, 1, 1), 1, 1, .7, 40
),
chain :
context.shaders_in_use["Phong_Model"].material(
Color(0, 0, 0, 1), 1, 1, 0, 40,
context.textures_in_use["chain.jpg"]
),
yellow_paint :
context.shaders_in_use["Phong_Model"].material(
Color(1, 1, 0, 1), .5, 1, .7, 40
),
// Miscellaneous
surface : null,
ball_free : false,
ball_transform : translation(0, 0, 10),//identity(),
ball_camera : false,
bodies : {
"auto" : [],
//"manual" : [],
"baseball" : null,
"ball_collision_items" : [],
"bat" : null
},
collider : new Subdivision_Sphere(1),
prev_anim_time : 0.0,
ball_start_time : 0,
events : [
{
"contact_x_velocity" : { "value" : -.15, "isFactor" : false },
"contact_y_velocity" : { "value" : -.8, "isFactor" : true }, // Multiply
"contact_z_velocity" : { "value" : .01, "isFactor" : true }, // Add
"release_y_velocity" : { "value" : -.06, "isFactor" : false },
"gravity" : { "value" : 0.00001 * -9.8, "isFactor" : false },
"friction_x_acceleration" : { "value" : .0005, "isFactor": false },
"friction_y_acceleration" : { "value" : .0005, "isFactor": false },
"normal_z_acceleration" : { "value" : -.5, "isFactor": true } // Multiply
},
{
"contact_x_velocity" : { "value" : .03, "isFactor" : false},
"contact_y_velocity" : { "value" : -.8, "isFactor" : true}, // Multiply
"contact_z_velocity" : { "value" : .01, "isFactor" : true}, // Add
"release_y_velocity" : { "value" : -.09, "isFactor" : false},
"gravity" : { "value" : 0.00001 * -9.8, "isFactor" : false },
"friction_x_acceleration" : { "value" : .0005, "isFactor": false },
"friction_y_acceleration" : { "value" : .0005, "isFactor": false },
"normal_z_acceleration" : { "value" : -.5, "isFactor": true } // Multiply
},
{
"contact_x_velocity" : { "value" : 0, "isFactor" : false},
"contact_y_velocity" : { "value" : -.9, "isFactor" : true}, // Multiply
"contact_z_velocity" : { "value" : .03, "isFactor" : true}, // Add
"release_y_velocity" : { "value" : -.09, "isFactor" : false},
"gravity" : { "value" : 0.000009 * -9.8, "isFactor" : false },
"friction_x_acceleration" : { "value" : 0, "isFactor": false },
"friction_y_acceleration" : { "value" : .0005, "isFactor": false },
"normal_z_acceleration" : { "value" : -.4, "isFactor": true } // Multiply
},
{
"contact_x_velocity" : { "value" : 0, "isFactor" : false},
"contact_y_velocity" : { "value" : -.9, "isFactor" : true}, // Multiply
"contact_z_velocity" : { "value" : .04, "isFactor" : true}, // Add
"release_y_velocity" : { "value" : -.09, "isFactor" : false},
"gravity" : { "value" : 0.000009 * -9.8, "isFactor" : false },
"friction_x_acceleration" : { "value" : 0, "isFactor": false },
"friction_y_acceleration" : { "value" : .0005, "isFactor": false },
"normal_z_acceleration" : { "value" : -.4, "isFactor": true } // Multiply
},
{
"contact_x_velocity" : { "value" : .03, "isFactor" : false},
"contact_y_velocity" : { "value" : -.9, "isFactor" : true}, // Multiply
"contact_z_velocity" : { "value" : .03, "isFactor" : true}, // Add
"release_y_velocity" : { "value" : -.09, "isFactor" : false},
"gravity" : { "value" : 0.000009 * -9.8, "isFactor" : false },
"friction_x_acceleration" : { "value" : .0005, "isFactor": false },
"friction_y_acceleration" : { "value" : .0005, "isFactor": false },
"normal_z_acceleration" : { "value" : -.4, "isFactor": true } // Multiply
},
{
"contact_x_velocity" : { "value" : -.03, "isFactor" : false},
"contact_y_velocity" : { "value" : -.9, "isFactor" : true}, // Multiply
"contact_z_velocity" : { "value" : .03, "isFactor" : true}, // Add
"release_y_velocity" : { "value" : -.09, "isFactor" : false},
"gravity" : { "value" : 0.000009 * -9.8, "isFactor" : false },
"friction_x_acceleration" : { "value" : .0005, "isFactor": false },
"friction_y_acceleration" : { "value" : .0005, "isFactor": false },
"normal_z_acceleration" : { "value" : -.4, "isFactor": true } // Multiply
},
{
"contact_x_velocity" : { "value" : 0, "isFactor" : false},
"contact_y_velocity" : { "value" : .8, "isFactor" : true}, // Multiply
"contact_z_velocity" : { "value" : .02, "isFactor" : true}, // Add
"release_y_velocity" : { "value" : -.11, "isFactor" : false},
"gravity" : { "value" : 0.00001 * -9.8, "isFactor" : false },
"friction_x_acceleration" : { "value" : .0005, "isFactor": false },
"friction_y_acceleration" : { "value" : .0005, "isFactor": false },
"normal_z_acceleration" : { "value" : -.5, "isFactor": true } // Multiply
},
{
"contact_x_velocity" : { "value" : .008, "isFactor" : false},
"contact_y_velocity" : { "value" : .1, "isFactor" : true}, // Multiply
"contact_z_velocity" : { "value" : .05, "isFactor" : true}, // Add
"release_y_velocity" : { "value" : -.11, "isFactor" : false},
"gravity" : { "value" : 0.00001 * -9.8, "isFactor" : false },
"friction_x_acceleration" : { "value" : .0005, "isFactor": false },
"friction_y_acceleration" : { "value" : .0005, "isFactor": false },
"normal_z_acceleration" : { "value" : -.3, "isFactor": true } // Multiply
},
{
"contact_x_velocity" : { "value" : .02, "isFactor" : false},
"contact_y_velocity" : { "value" : -.9, "isFactor" : true}, // Multiply
"contact_z_velocity" : { "value" : .04, "isFactor" : true}, // Add
"release_y_velocity" : { "value" : -.09, "isFactor" : false},
"gravity" : { "value" : 0.000009 * -9.8, "isFactor" : false },
"friction_x_acceleration" : { "value" : 0, "isFactor": false },
"friction_y_acceleration" : { "value" : .0005, "isFactor": false },
"normal_z_acceleration" : { "value" : -.4, "isFactor": true } // Multiply
},
{
"contact_x_velocity" : { "value" : -.04, "isFactor" : false},
"contact_y_velocity" : { "value" : -1.9, "isFactor" : true}, // Multiply
"contact_z_velocity" : { "value" : .001, "isFactor" : true}, // Add
"release_y_velocity" : { "value" : -.09, "isFactor" : false},
"gravity" : { "value" : 0.000009 * -9.8, "isFactor" : false },
"friction_x_acceleration" : { "value" : .1, "isFactor": false },
"friction_y_acceleration" : { "value" : .0005, "isFactor": false },
"normal_z_acceleration" : { "value" : -.4, "isFactor": true } // Multiply
},
]
});
context.globals.graphics_state.set(
lookAt([1.58, 287, 15], [0, 260, 15], [0, 0, 1]),
perspective(45, context.width/context.height, .1, 1000),
0
);
this.define_data_members({
graphics_state : context.globals.graphics_state,
thrust : vec3(),
origin : vec3(0, 0, 0),
looking : false
});
// *** Mouse controls: ***
this.mouse = { "from_center": vec2() }; // Measure mouse steering, for rotating the fly-around camera:
var mouse_position = function(e) {
return vec2(e.clientX - context.width/2, e.clientY - context.height/2);
};
canvas.addEventListener("mouseup", (function(self) {
return function(e) {
e = e || window.event;
self.mouse.anchor = undefined;
}
}) (this), false);
canvas.addEventListener("mousedown", (function(self) {
return function(e) {
e = e || window.event;
self.mouse.anchor = mouse_position(e);
}
}) (this), false);
canvas.addEventListener("mousemove", (function(self) {
return function(e) {