Repository navigation
Expand file tree
/
Copy pathphysics.c
More file actions
2387 lines (2121 loc) · 75.6 KB
/
Copy pathphysics.c
File metadata and controls
2387 lines (2121 loc) · 75.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
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
/*
PHYSICS.C
symbols in this file:
0013FF60 0080:
_pin_fraction (0000)
0013FFE0 00b0:
_physics_get_features_in_sphere (0000)
00140090 0070:
_code_00140090 (0000)
00140100 0110:
_render_debug_physics (0000)
00140210 0100:
_physics_instance_new (0000)
00140310 00a0:
_physics_test_point (0000)
001403B0 0120:
_physics_test_vector (0000)
001404D0 0150:
_code_001404d0 (0000)
00140620 0100:
_code_00140620 (0000)
00140720 0b80:
_physics_compute_new (0000)
001412A0 0470:
_code_001412a0 (0000)
00141710 0490:
_code_00141710 (0000)
00141BA0 0180:
_code_00141ba0 (0000)
00141D20 01b0:
_code_00141d20 (0000)
00141ED0 07c0:
_physics_update_new (0000)
00142690 1430:
_code_00142690 (0000)
00143AC0 0260:
_physics_update (0000)
0028BC20 008d:
??_C@_0IN@ODNFDOFI@mass_point?9?$DOground_material_type@ (0000)
0028BCB0 0021:
??_C@_0CB@OHGIJBLL@c?3?2halo?2SOURCE?2physics?2physics?4c@ (0000)
0028BCD4 0004:
__real@45ea6000 (0000)
0028BCD8 0057:
??_C@_0FH@DDOACPI@unit_definition?9?$DOunit?4blip_type?$DO@ (0000)
0028BD30 0015:
??_C@_0BF@GMDJIBPD@model_instance_valid?$AA@ (0000)
0028BD48 0010:
??_C@_0BA@LNKGLGKJ@rotated_forward?$AA@ (0000)
0028BD58 000b:
??_C@_0L@GBBDLPMF@rotated_up?$AA@ (0000)
0028BD64 000f:
??_C@_0P@OGJNHPDG@up?$CB?$DNrotated_up?$AA@ (0000)
0028BD74 0019:
??_C@_0BJ@GEGCJHGH@forward?$CB?$DNrotated_forward?$AA@ (0000)
0028BD90 0004:
__real@364c6ce0 (0000)
0028BD94 0004:
__real@34a5b36e (0000)
0028BD98 0004:
__real@3b33abb1 (0000)
0028BDA0 0008:
__real@3f80000000000000 (0000)
0028BDA8 0012:
??_C@_0BC@MDPNAJDL@?$CGangular_velocity?$AA@ (0000)
0028BDBC 0016:
??_C@_0BG@NPNNHCFI@?$CGangular_acceleration?$AA@ (0000)
0028BDD4 0011:
??_C@_0BB@IHCLBKLO@?$CGlinear_velocity?$AA@ (0000)
0028BDE8 0015:
??_C@_0BF@LNOJMOGI@?$CGlinear_acceleration?$AA@ (0000)
0028BE00 001d:
??_C@_0BN@FOPAIIBD@instance?9?$DOphysics?9?$DOmass?$DO0?40f?$AA@ (0000)
0028BE20 0018:
??_C@_0BI@HOJEHDKE@?$CGobject?9?$DOobject?4forward?$AA@ (0000)
0028BE38 0013:
??_C@_0BD@JHPMDDBM@?$CGobject?9?$DOobject?4up?$AA@ (0000)
0028BE4C 001c:
??_C@_0BM@EKBNDJOG@?$CGtranslational_acceleration?$AA@ (0000)
0028BE68 000d:
??_C@_0N@EMPCNLII@magic_torque?$AA@ (0000)
0028BE78 000c:
??_C@_0M@LNGNIFCJ@magic_force?$AA@ (0000)
0030CEB4 002c:
_global_gravity (0000)
_global_water_density (0004)
_global_air_density (0008)
_global_physics_collision_depth (000c)
_depths_of_hell (0010)
0045E01A 0001:
_debug_physics_disable_penetration_freeze (0000)
*/
/* ---------- headers */
#include "cseries.h"
#include "cseries/errors.h" /* port: error */
#include "physics.h"
#include "collision_features.h"
#include "collision_models.h"
#include "collisions.h"
#include "effects/material_effect_definitions.h"
#include "game/game.h"
#include "game/game_globals.h"
#include "math/real_math.h"
#include "objects/damage.h"
#include "objects/object_definitions.h"
#include "objects/object_types.h"
#include "objects/objects.h"
#include "collision_model_definitions.h"
#include "physics_definitions.h"
#include "render/render_debug.h"
#include "scenario/scenario.h"
#include "structures/structure_bsp_definitions.h"
#include "units/bipeds.h"
#include "units/unit_definitions.h"
#include "units/vehicles.h"
#include "vehicle_riding.h" /* port: port/linux/game/vehicle_riding.c */
/* ---------- constants */
enum
{
_collision_surface_two_sided_bit = 0,
_collision_surface_invisible_bit,
_collision_surface_climbable_bit,
_collision_surface_breakable_bit,
};
enum
{
_friction_type_point = 0,
_friction_type_forward,
_friction_type_left,
_friction_type_up,
NUMBER_OF_FRICTION_TYPES
};
enum
{
_point_at_rest_bit = 0,
_point_on_ground_bit,
_point_on_volatile_surface_bit,
_point_in_water_bit,
_point_antigraving_bit,
NUMBER_OF_MASS_POINT_FLAGS
};
enum
{
_powered_mass_point_ground_friction_bit = 0,
_powered_mass_point_water_friction_bit,
_powered_mass_point_air_friction_bit,
_powered_mass_point_water_lift_bit,
_powered_mass_point_air_lift_bit,
_powered_mass_point_thrust_bit,
_powered_mass_point_antigrav_bit,
NUMBER_OF_POWERED_MASS_POINT_FLAGS
};
/* ---------- macros */
/* January's physics.c expands point_from_line3d in place for points and
* vectors alike; this scalar expansion preserves that inline schedule without
* selecting the external point_from_line3d COMDAT for this object (the same
* emitted-symbol invariant as COLLISION_POINT_FROM_LINE3D in collisions.c). */
#define PHYSICS_POINT_FROM_LINE3D(point, vector, distance, result) \
do \
{ \
real *line_result = (result)->n; \
real line_distance = (distance); \
real const *line_vector = (vector)->n; \
real const *line_point = (point)->n; \
line_result[0] = line_vector[0] * line_distance + line_point[0]; \
line_result[1] = line_vector[1] * line_distance + line_point[1]; \
line_result[2] = line_vector[2] * line_distance + line_point[2]; \
} while (0)
/* ---------- structures */
struct powered_mass_point_definition
{
char name[32];
unsigned long flags;
real antigrav_strength;
real antigrav_offset;
real antigrav_height;
real antigrav_damp_fraction;
real antigrav_normal_k1;
real antigrav_normal_k0;
real unused[17];
};
#include "friction_datum.h"
#include "powered_mass_point_datum.h"
#include "mass_point_datum.h"
#include "units/vehicle_datum.h"
/* TU-local copy: no shared header declares the game globals falling-damage
* block yet; identical complete copies live in objects/damage.c and
* units/bipeds.c. */
struct game_globals_falling_damage
{
long unused0[2];
real falling_distance_lower_bound;
real falling_distance_upper_bound;
struct tag_reference falling_damage;
long terminal_velocity_unused[2];
real maximum_distance;
struct tag_reference maximum_distance_damage;
struct tag_reference vehicle_hit_environment_damage_effect;
struct tag_reference vehicle_killed_unit_damage_effect;
struct tag_reference vehicle_collision_damage;
struct tag_reference flaming_death_damage;
long unused7c[4];
real runtime_maximum_falling_velocity;
real runtime_minimum_damage_velocity;
real runtime_maximum_damage_velocity;
};
typedef char game_globals_falling_damage_size_assert[
sizeof(struct game_globals_falling_damage) == 0x98 ? 1 : -1];
typedef char powered_mass_point_definition_size_assert[
sizeof(struct powered_mass_point_definition) == 0x80 ? 1 : -1];
typedef char powered_mass_point_datum_size_assert[
sizeof(struct powered_mass_point_datum) == 0x60 ? 1 : -1];
typedef char mass_point_datum_size_assert[
sizeof(struct mass_point_datum) == 0x130 ? 1 : -1];
/* ---------- prototypes */
static short physics_mass_point_count(
struct physics_definition const *physics);
static short physics_powered_mass_point_count(
struct physics_definition const *physics);
static short physics_powered_mass_point_index(
struct physics_definition const *physics,
struct mass_point_definition const *mass_point_definition);
static short get_material_type(
long object_index,
short material_index);
static void compute_ground_plane(
long object_index,
struct mass_point_datum *mass_point,
struct mass_point_definition const *mass_point_definition);
static void friction_evaluate(
short friction_type,
real parallel_scale,
real perpendicular_scale,
struct friction_datum *friction,
real_vector3d const *forward,
real_vector3d const *up);
static boolean physics_compute_biped_collision(
struct collision_model_instance *instance,
long biped_index);
static boolean physics_compute_vehicle_collision(
struct physics_instance const *instance0,
struct physics_instance const *instance1);
static void physics_compute_unit_collisions(
long vehicle_index);
void physics_update_new(
struct physics_instance const *instance,
struct powered_mass_point_datum const *powered_mass_points,
struct mass_point_datum const *mass_points,
real_vector3d const *total_force,
real_vector3d const *total_torque);
static void physics_update_old(
long object_index,
struct powered_mass_point_datum *powered_mass_points,
struct mass_point_datum *mass_points,
real_vector3d const *magic_force,
real_vector3d const *magic_torque);
static void rotate_vectors3d_by_angular_velocity(
real_vector3d const *forward,
real_vector3d const *up,
real_vector3d const *angular_velocity,
real_vector3d *rotated_forward,
real_vector3d *rotated_up);
/* ---------- globals */
real global_gravity = 0.0035651792f;
real global_water_density = 1.0f;
real global_air_density = 0.0011f;
real global_physics_collision_depth = 0.2f;
real_plane3d depths_of_hell =
{
{ 0.0f, 0.0f, 1.0f },
-256.0f
};
boolean debug_physics_disable_penetration_freeze = FALSE;
/* port: whether a map's malformed physics was reported (once) */
static boolean warned_about_physics_mass_points;
static boolean warned_about_physics_inertial_matrix;
/* ---------- public code */
real pin_fraction(
real value,
real begin,
real end)
{
if (begin < end)
{
if (value <= begin)
return 0.0f;
else if (value >= end)
return 1.0f;
else
return (value - begin) / (end - begin);
}
else
{
if (value <= end)
return 1.0f;
else if (value >= begin)
return 0.0f;
else
return (begin - value) / (begin - end);
}
}
/* port: how many of a physics' mass points (from the map) are used: at
most MAXIMUM_MASS_POINTS_PER_PHYSICS (the rest are not, reported once) */
static short physics_mass_point_count(
struct physics_definition const *physics)
{
long count = physics->mass_points.count;
if (count > MAXIMUM_MASS_POINTS_PER_PHYSICS)
{
if (!warned_about_physics_mass_points)
{
error(_error_silent, "a physics has %ld mass points (maximum %d)",
count,
MAXIMUM_MASS_POINTS_PER_PHYSICS);
warned_about_physics_mass_points = TRUE;
}
count = MAXIMUM_MASS_POINTS_PER_PHYSICS;
}
return (short)MAX(count, 0);
}
/* port: (as physics_mass_point_count, with
MAXIMUM_POWERED_MASS_POINTS_PER_PHYSICS) */
static short physics_powered_mass_point_count(
struct physics_definition const *physics)
{
long count = physics->powered_mass_points.count;
if (count > MAXIMUM_POWERED_MASS_POINTS_PER_PHYSICS)
{
if (!warned_about_physics_mass_points)
{
error(_error_silent, "a physics has %ld powered mass points (maximum %d)",
count,
MAXIMUM_POWERED_MASS_POINTS_PER_PHYSICS);
warned_about_physics_mass_points = TRUE;
}
count = MAXIMUM_POWERED_MASS_POINTS_PER_PHYSICS;
}
return (short)MAX(count, 0);
}
/* port: a mass point's powered mass point (from the map), NONE if it
names none of the physics' used ones (physics_powered_mass_point_count) */
static short physics_powered_mass_point_index(
struct physics_definition const *physics,
struct mass_point_definition const *mass_point_definition)
{
short powered_mass_point_index = mass_point_definition->powered_mass_point_index;
if (powered_mass_point_index != NONE &&
(powered_mass_point_index < 0 ||
powered_mass_point_index >= physics_powered_mass_point_count(physics)))
{
if (!warned_about_physics_mass_points)
{
error(_error_silent, "a mass point's powered mass point #%d is not one of its physics' %ld",
powered_mass_point_index,
physics->powered_mass_points.count);
warned_about_physics_mass_points = TRUE;
}
powered_mass_point_index = NONE;
}
return powered_mass_point_index;
}
boolean physics_get_features_in_sphere(
struct physics_instance const *instance,
real_point3d const *center,
real radius,
real height,
real width,
struct collision_feature_list *features)
{
short mass_point_index;
real scaled_radius;
/* port: (physics_mass_point_count) */
short mass_point_count = physics_mass_point_count(instance->physics);
for (mass_point_index = 0;
mass_point_index < mass_point_count;
mass_point_index++)
{
struct mass_point_definition const *mass_point = TAG_BLOCK_GET_ELEMENT(
&instance->physics->mass_points,
mass_point_index,
struct mass_point_definition);
real_point3d point;
matrix4x3_transform_point(&instance->world_matrix, &mass_point->position, &point);
scaled_radius = mass_point->radius * instance->world_matrix.scale;
collision_features_from_point(
&point,
height,
scaled_radius + width,
instance->object_index,
NONE,
0,
NONE,
NONE,
features);
}
return features->count[_collision_feature_sphere] ||
features->count[_collision_feature_cylinder] ||
features->count[_collision_feature_prism];
}
static short get_material_type(
long object_index,
short material_index)
{
short material_type = NONE;
if (material_index != NONE)
{
if (object_index != NONE)
{
struct object_datum *object = object_get(object_index);
struct object_definition *definition = object_definition_get(object->definition_index);
/* port: (as in collision_model_get_material_type) */
if (definition->object.collision_model.index != NONE)
{
material_type = (short)collision_model_get_material_type(
collision_model_definition_get(definition->object.collision_model.index),
material_index);
}
}
else
{
struct structure_bsp *structure_bsp = global_structure_bsp_get();
/* port: a material (from the map) that is no material of the
bsp, or whose type is no material type, is none */
if (material_index >= 0 && material_index < structure_bsp->collision_materials.count)
{
material_type = TAG_BLOCK_GET_ELEMENT(
&structure_bsp->collision_materials,
material_index,
struct structure_collision_material)->runtime_physics_material_type;
}
if (material_type < 0 || material_type >= NUMBER_OF_MATERIAL_TYPES)
{
material_type = NONE;
}
}
}
return material_type;
}
void render_debug_physics(
struct physics_instance *instance)
{
struct object_datum *object = object_get(instance->object_index);
struct object_definition *definition = object_definition_get(object->definition_index);
real_point3d center_of_mass;
short mass_point_index;
/* port: (physics_mass_point_count) */
short mass_point_count = physics_mass_point_count(instance->physics);
matrix4x3_transform_point(
&instance->world_matrix,
&instance->physics->center_of_mass,
¢er_of_mass);
render_debug_vectors(
TRUE,
¢er_of_mass,
&instance->world_matrix.forward,
&instance->world_matrix.up,
definition->object.bounding_radius);
for (mass_point_index = 0;
mass_point_index < mass_point_count;
mass_point_index++)
{
struct mass_point_definition const *mass_point = TAG_BLOCK_GET_ELEMENT(
&instance->physics->mass_points,
mass_point_index,
struct mass_point_definition);
real_point3d position;
real_vector3d forward;
real_vector3d up;
matrix4x3_transform_point(
&instance->world_matrix,
&mass_point->position,
&position);
matrix4x3_transform_normal(
&instance->world_matrix,
&mass_point->forward,
&forward);
matrix4x3_transform_normal(
&instance->world_matrix,
&mass_point->up,
&up);
render_debug_sphere(
TRUE,
&position,
mass_point->radius,
global_real_argb_white);
render_debug_vectors(
TRUE,
&position,
&forward,
&up,
mass_point->radius * 0.5f);
}
}
boolean physics_instance_new(
struct physics_instance *instance,
long object_index)
{
struct object_datum *object = object_get(object_index);
struct object_definition *definition = object_definition_get(object->definition_index);
real_point3d center_of_mass;
if (definition->object.physics.index != NONE)
{
instance->object_index = object_index;
instance->physics = physics_definition_get(definition->object.physics.index);
instance->world_matrix.scale = 1.0f;
object_get_origin(object_index, &instance->world_matrix.position);
object_get_orientation(object_index, &instance->world_matrix.forward, &instance->world_matrix.up);
cross_product3d(&instance->world_matrix.up, &instance->world_matrix.forward, &instance->world_matrix.left);
set_real_point3d(¢er_of_mass,
-instance->physics->center_of_mass.x,
-instance->physics->center_of_mass.y,
-instance->physics->center_of_mass.z);
matrix4x3_transform_point(&instance->world_matrix, ¢er_of_mass, ¢er_of_mass);
instance->world_matrix.position = center_of_mass;
return TRUE;
}
return FALSE;
}
boolean physics_test_point(
struct physics_instance const *instance,
real_point3d const *point)
{
real_point3d local_point;
struct physics_definition const *physics;
short mass_point_index;
short mass_point_count;
matrix4x3_inverse_transform_point(&instance->world_matrix, point, &local_point);
physics = instance->physics;
/* port: (physics_mass_point_count) */
mass_point_count = physics_mass_point_count(physics);
for (mass_point_index = 0;
mass_point_index < mass_point_count;
mass_point_index++)
{
struct mass_point_definition const *mass_point = TAG_BLOCK_GET_ELEMENT(
&physics->mass_points,
mass_point_index,
struct mass_point_definition);
if (point_in_sphere(&local_point, &mass_point->position, mass_point->radius))
return TRUE;
physics = instance->physics;
}
return FALSE;
}
boolean physics_test_vector(
struct physics_instance const *instance,
real_point3d const *point,
real_vector3d const *vector,
struct physics_test_vector_result *result)
{
boolean hit = FALSE;
real_point3d local_point;
real_vector3d local_vector;
real_vector3d normal;
short mass_point_index;
short mass_point_count;
result->t = REAL_MAX;
matrix4x3_inverse_transform_point(&instance->world_matrix, point, &local_point);
matrix4x3_inverse_transform_vector(&instance->world_matrix, vector, &local_vector);
/* port: (physics_mass_point_count) */
mass_point_count = physics_mass_point_count(instance->physics);
for (mass_point_index = 0;
mass_point_index < mass_point_count;
mass_point_index++)
{
struct mass_point_definition const *mass_point = TAG_BLOCK_GET_ELEMENT(
&instance->physics->mass_points,
mass_point_index,
struct mass_point_definition);
real t;
if (sphere_test_vector3d(
&mass_point->position,
mass_point->radius,
&local_point,
&local_vector,
&t,
&normal) &&
result->t > t)
{
real_point3d intersection;
result->t = t;
// Preserve the January inline schedule without emitting a point_from_line3d COMDAT.
intersection.x = local_vector.i*t + local_point.x;
intersection.y = local_vector.j*t + local_point.y;
intersection.z = local_vector.k*t + local_point.z;
plane3d_from_point_and_normal(
&result->plane,
&intersection,
&normal);
hit = TRUE;
}
}
if (hit)
matrix4x3_transform_plane(&instance->world_matrix, &result->plane, &result->plane);
return hit;
}
static void compute_ground_plane(
long object_index,
struct mass_point_datum *mass_point,
struct mass_point_definition const *mass_point_definition)
{
struct collision_feature_list features;
struct collision_plane collision;
mass_point->ground_plane = depths_of_hell;
mass_point->ground_material_type = NONE;
mass_point->ground_depth = mass_point_definition->radius -
plane3d_distance_to_point(&mass_point->ground_plane, &mass_point->position);
if (collision_get_features_in_sphere(
_collision_test_for_bipeds_dead_flags,
&mass_point->position,
mass_point_definition->radius,
0.0f,
mass_point_definition->radius,
object_index,
&features) &&
collision_features_test_point(&features, &mass_point->position, &collision))
{
mass_point->ground_plane = collision.plane;
mass_point->ground_depth = collision.t;
mass_point->ground_material_type = get_material_type(
collision.object_index,
collision.material_index);
SET_FLAG(
mass_point->flags,
_point_on_volatile_surface_bit,
TEST_FLAG(collision.flags, _collision_surface_breakable_bit) ||
(collision.object_index != NONE &&
!TEST_FLAG(_object_mask_scenery, object_get_type(collision.object_index))));
if (collision.object_index != NONE)
object_deplete_shield(collision.object_index);
}
match_assert(
"c:\\halo\\SOURCE\\physics\\physics.c",
338,
mass_point->ground_material_type==NONE || (mass_point->ground_material_type>=0 && mass_point->ground_material_type<NUMBER_OF_MATERIAL_TYPES));
return;
}
static void friction_evaluate(
short friction_type,
real parallel_scale,
real perpendicular_scale,
struct friction_datum *friction,
real_vector3d const *forward,
real_vector3d const *up)
{
real_vector3d left;
if (friction_type == _friction_type_point)
{
friction->parallel = friction->friction;
set_real_vector3d(&friction->perpendicular, 0.0f, 0.0f, 0.0f);
return;
}
switch (friction_type)
{
case _friction_type_forward:
component_vectors_from_normal3d(
&friction->friction,
forward,
&friction->parallel,
&friction->perpendicular);
break;
case _friction_type_left:
cross_product3d(up, forward, &left);
component_vectors_from_normal3d(
&friction->friction,
&left,
&friction->parallel,
&friction->perpendicular);
break;
case _friction_type_up:
component_vectors_from_normal3d(
&friction->friction,
up,
&friction->parallel,
&friction->perpendicular);
break;
default:
match_vassert("c:\\halo\\SOURCE\\physics\\physics.c", 383, FALSE, NULL);
break;
}
scale_vector3d(&friction->parallel, parallel_scale, &friction->parallel);
scale_vector3d(&friction->perpendicular, perpendicular_scale, &friction->perpendicular);
add_vectors3d(&friction->parallel, &friction->perpendicular, &friction->friction);
return;
}
void physics_compute_new(
struct physics_instance const *instance,
struct powered_mass_point_datum const *powered_mass_points,
struct mass_point_datum *mass_points,
real_vector3d *total_force,
real_vector3d *total_torque)
{
struct object_datum *object = object_get(instance->object_index);
struct physics_definition const *physics = instance->physics;
real gravity = physics->gravity_scale*global_gravity;
short mass_point_index;
/* port: no more mass points than the caller's arrays hold, nor a
powered one the physics does not have (physics_mass_point_count,
physics_powered_mass_point_index) */
short mass_point_count = physics_mass_point_count(physics);
set_real_vector3d(total_force, 0.0f, 0.0f, -physics->mass*gravity);
set_real_vector3d(total_torque, 0.0f, 0.0f, 0.0f);
memset(mass_points, 0, sizeof(struct mass_point_datum)*mass_point_count);
for (mass_point_index = 0;
mass_point_index < mass_point_count;
mass_point_index++)
{
struct mass_point_definition const *mass_point_definition = TAG_BLOCK_GET_ELEMENT(
&physics->mass_points,
mass_point_index,
struct mass_point_definition);
struct mass_point_datum *mass_point = mass_points + mass_point_index;
struct powered_mass_point_definition const *powered_mass_point_definition = NULL;
struct powered_mass_point_datum const *powered_mass_point = NULL;
real_vector3d powered_velocity;
short powered_mass_point_index = physics_powered_mass_point_index(physics, mass_point_definition);
if (powered_mass_point_index != NONE && powered_mass_points)
{
powered_mass_point_definition = TAG_BLOCK_GET_ELEMENT(
&physics->powered_mass_points,
powered_mass_point_index,
struct powered_mass_point_definition);
powered_mass_point = powered_mass_points + powered_mass_point_index;
}
mass_point->flags = 0;
matrix4x3_transform_point(
&instance->world_matrix,
&mass_point_definition->position,
&mass_point->position);
if (powered_mass_point)
{
real_matrix4x3 powered_world_matrix;
matrix4x3_multiply(&instance->world_matrix, &powered_mass_point->rotation_matrix, &powered_world_matrix);
matrix4x3_transform_normal(&powered_world_matrix, &mass_point_definition->forward, &mass_point->forward);
matrix4x3_transform_normal(&powered_world_matrix, &mass_point_definition->up, &mass_point->up);
}
else
{
matrix4x3_transform_normal(&instance->world_matrix, &mass_point_definition->forward, &mass_point->forward);
matrix4x3_transform_normal(&instance->world_matrix, &mass_point_definition->up, &mass_point->up);
}
scenario_location_from_point(&mass_point->location, &mass_point->position);
vector_from_points3d(&object->object.position, &mass_point->position, &mass_point->radius);
cross_product3d(&object->object.angular_velocity, &mass_point->radius, &mass_point->velocity);
add_vectors3d(&object->object.translational_velocity, &mass_point->velocity, &mass_point->velocity);
compute_ground_plane(instance->object_index, mass_point, mass_point_definition);
mass_point->water_depth = scenario_location_water_depth(&mass_point->location, &mass_point->position);
if (mass_point->ground_depth > 0.0f)
{
struct material_definition const *material = scenario_material_definition_get(
mass_point->ground_material_type);
real ground_friction = physics->ground_friction;
real ground_normal_k1 = physics->ground_normal_k1;
real ground_normal_k0 = physics->ground_normal_k0;
real ground_depth = physics->ground_depth;
real ground_damp_fraction = physics->ground_damp_fraction;
real normal_velocity;
real ground_scale;
if (material->physics_ground_friction_scale > 0.0f && physics->mass <= 7500.0f)
ground_friction *= material->physics_ground_friction_scale;
if (material->physics_ground_friction_normal_k1_scale > 0.0f)
ground_normal_k1 *= material->physics_ground_friction_normal_k1_scale;
if (material->physics_ground_friction_normal_k0_scale > 0.0f)
ground_normal_k0 *= material->physics_ground_friction_normal_k0_scale;
if (material->physics_ground_depth_scale > 0.0f)
ground_depth *= material->physics_ground_depth_scale;
if (material->physics_ground_damp_fraction_scale > 0.0f)
ground_damp_fraction *= material->physics_ground_damp_fraction_scale;
normal_velocity = dot_product3d(&mass_point->velocity, &mass_point->ground_plane.n);
mass_point->normal_force_magnitude = physics->mass*
(global_gravity/ground_depth*mass_point->ground_depth - normal_velocity*ground_damp_fraction);
scale_vector3d(&mass_point->ground_plane.n, mass_point->normal_force_magnitude, &mass_point->normal_force);
scale_vector3d(&mass_point->ground_plane.n, -normal_velocity, &mass_point->velocity_relative_to_ground);
add_vectors3d(&mass_point->velocity_relative_to_ground, &mass_point->velocity, &mass_point->velocity_relative_to_ground);
ground_scale = -mass_point_definition->mass*ground_friction;
scale_vector3d(&mass_point->velocity_relative_to_ground, ground_scale, &mass_point->ground_friction.friction);
if (powered_mass_point_definition &&
TEST_FLAG(powered_mass_point_definition->flags, _powered_mass_point_ground_friction_bit) &&
powered_mass_point->ground_friction_velocity != 0.0f)
{
real fraction = pin_fraction(mass_point->ground_plane.n.k, ground_normal_k0, ground_normal_k1);
real alignment = PIN(dot_product3d(&mass_point->up, &mass_point->ground_plane.n), 0.0f, 1.0f);
real weight = alignment*alignment*fraction*fraction*ground_scale;
real_vector3d projected_velocity;
scale_vector3d(&mass_point->forward, -powered_mass_point->ground_friction_velocity, &powered_velocity);
scale_vector3d(
&mass_point->ground_plane.n,
-dot_product3d(&powered_velocity, &mass_point->ground_plane.n),
&projected_velocity);
add_vectors3d(&projected_velocity, &powered_velocity, &projected_velocity);
add_vectors3d(&mass_point->velocity_relative_to_ground, &projected_velocity, &mass_point->velocity_relative_to_ground);
scale_vector3d(&projected_velocity, weight, &projected_velocity);
add_vectors3d(&mass_point->ground_friction.friction, &projected_velocity, &mass_point->ground_friction.friction);
}
friction_evaluate(
mass_point_definition->friction_type,
mass_point_definition->friction_parallel_scale,
mass_point_definition->friction_perpendicular_scale,
&mass_point->ground_friction,
&mass_point->forward,
&mass_point->up);
}
if (mass_point->water_depth > 0.0f)
{
real depth_fraction = mass_point->water_depth < physics->water_depth ?
mass_point->water_depth/physics->water_depth : 1.0f;
real water_scale = -mass_point_definition->mass*physics->water_friction;
if (mass_point_definition->density > 0.0f && physics->water_depth > 0.0f)
{
mass_point->water_pressure_magnitude = mass_point_definition->mass/mass_point_definition->density*
physics->water_density*depth_fraction*gravity;
set_real_vector3d(&mass_point->water_pressure, 0.0f, 0.0f, mass_point->water_pressure_magnitude);
}
if (powered_mass_point_definition &&
TEST_FLAG(powered_mass_point_definition->flags, _powered_mass_point_water_friction_bit) &&
powered_mass_point->water_friction_velocity != 0.0f)
{
scale_vector3d(&mass_point->forward, -powered_mass_point->water_friction_velocity, &powered_velocity);
add_vectors3d(&powered_velocity, &mass_point->velocity, &powered_velocity);
scale_vector3d(&powered_velocity, water_scale, &mass_point->water_friction.friction);
}
else
{
scale_vector3d(&mass_point->velocity, water_scale, &mass_point->water_friction.friction);
}
friction_evaluate(
mass_point_definition->friction_type,
mass_point_definition->friction_parallel_scale,
mass_point_definition->friction_perpendicular_scale,
&mass_point->water_friction,
&mass_point->forward,
&mass_point->up);
if (powered_mass_point_definition &&
TEST_FLAG(powered_mass_point_definition->flags, _powered_mass_point_water_lift_bit) &&
powered_mass_point->water_lift_ratio != 0.0f)
{
real lift = fabs(dot_product3d(&mass_point->forward, &mass_point->velocity))*
powered_mass_point->water_lift_ratio*physics->mass*depth_fraction;
real_vector3d lift_force;
scale_vector3d(&mass_point->up, lift, &lift_force);
add_vectors3d(&mass_point->powered_force, &lift_force, &mass_point->powered_force);
}
}
{
real air_scale = -mass_point_definition->mass*physics->air_friction;
if (powered_mass_point_definition &&
TEST_FLAG(powered_mass_point_definition->flags, _powered_mass_point_air_friction_bit) &&
powered_mass_point->air_friction_velocity != 0.0f)
{
scale_vector3d(&mass_point->forward, -powered_mass_point->air_friction_velocity, &powered_velocity);
add_vectors3d(&powered_velocity, &mass_point->velocity, &powered_velocity);
scale_vector3d(&powered_velocity, air_scale, &mass_point->air_friction.friction);
}
else
{
scale_vector3d(&mass_point->velocity, air_scale, &mass_point->air_friction.friction);
}
friction_evaluate(
mass_point_definition->friction_type,
mass_point_definition->friction_parallel_scale,
mass_point_definition->friction_perpendicular_scale,
&mass_point->air_friction,
&mass_point->forward,
&mass_point->up);
if (powered_mass_point_definition &&
TEST_FLAG(powered_mass_point_definition->flags, _powered_mass_point_air_lift_bit) &&
powered_mass_point->air_lift_ratio != 0.0f)
{
real lift = fabs(dot_product3d(&mass_point->forward, &mass_point->velocity))*
physics->mass*powered_mass_point->air_lift_ratio;
real_vector3d lift_force;
scale_vector3d(&mass_point->up, lift, &lift_force);
add_vectors3d(&mass_point->powered_force, &lift_force, &mass_point->powered_force);
}
}
SET_FLAG(mass_point->flags, _point_at_rest_bit, magnitude_squared3d(&mass_point->velocity) < 0.0011111111f);
SET_FLAG(mass_point->flags, _point_on_ground_bit, mass_point->ground_depth > 0.0f);
SET_FLAG(mass_point->flags, _point_in_water_bit, mass_point->water_depth > 0.0f);
if (powered_mass_point_definition)
{
if (TEST_FLAG(powered_mass_point_definition->flags, _powered_mass_point_thrust_bit))