Debian Codegraph @

physics/physics.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <cmath>
20 #include <cstdlib>
21 #include <limits>
22
23 #include "attach.h"
24 #include "collide.h"
25 #include "D3ForceFeedback.h"
26 #include "demofile.h"
27 #include "findintersection.h"
28 #include "fireball.h"
29 #include "game.h"
30 #include "multi.h"
31 #include "object.h"
32 #include "physics.h"
33 #include "player.h"
34 #include "polymodel.h"
35 #include "terrain.h"
36 #include "vecmat.h"
37 #include "viseffect.h"
38 #include "weapon.h"
39
40 // Global variables for physics system
41 uint8_t Default_player_terrain_leveling = 0;
42 uint8_t Default_player_room_leveling = 0;
43
44 // Maximum iterations thru the simulation loop NOTE: It is assumed that the player loops >= non-player loops
45 #define MAX_PLAYER_SIM_LOOPS 9
46 #define MAX_NON_PLAYER_SIM_LOOPS 5
47
48 #define CEILING_POWERUP_DELTA 30.0f
49
50 #define WIGGLE_FALLOFF_TIME 2.0
51
52 int Physics_normal_counter;
53 int Physics_normal_looping_counter;
54 int Physics_walking_counter;
55 int Physics_walking_looping_counter;
56 int Physics_vis_counter;
57
58 #ifdef _DEBUG
59 // This will allow us to debug physics in a better way.
60 struct sim_loop_info {
61 vector start_pos;
62 physics_info phys_info; // At beginnning of loop
63 fvi_info hit_info; // Hit information returned by FVI
64 };
65
66 sim_loop_info sim_loop_record[MAX_PLAYER_SIM_LOOPS];
67 #endif
68
69 #ifdef _DEBUG
70 int Physics_player_verbose = 0;
71 #endif
72
73 #define PHYSICS_GROUND_TOLERANCE 0.0001f
74 #define DAMP_ANG 1.0f // chrishack -- this is probably too tight and should be an int not a float
75 #define MAX_OBJECT_VEL 100000.0f
76 int Physics_cheat_flag = 0;
77 extern char BounceCheat;
78
79 int PhysicsLinkList[MAX_OBJECTS];
80 int Physics_NumLinked = 0;
81
82 // Current strength of gravity
83 float Gravity_strength = -32.2f; // Meters/second^2
84
85 void DoPhysLinkedFrame(object *obj) {
86 object *parent = ObjGet(obj->mtype.obj_link_info.parent_handle);
87 matrix new_orient;
88 vector new_pos;
89
90 if (parent && parent->type != OBJ_GHOST && parent->type != OBJ_DUMMY) {
91 poly_model *pm;
92 vector pnt;
93 vector fvec;
94 vector uvec;
95 matrix m;
96 int mn; // submodel number
97 float normalized_time[MAX_SUBOBJECTS];
98
99 pm = &Poly_models[parent->rtype.pobj_info.model_num];
100 ASSERT(pm->used);
101
102 SetNormalizedTimeObj(parent, normalized_time);
103
104 SetModelAnglesAndPos(pm, normalized_time);
105
106 pnt = obj->mtype.obj_link_info.pos;
107 fvec = obj->mtype.obj_link_info.fvec;
108 uvec = obj->mtype.obj_link_info.uvec;
109
110 mn = obj->mtype.obj_link_info.sobj_index;
111
112 if (mn < 0 || mn >= pm->n_models) {
113 mprintf(0, "Caught physics link bug!\n");
114 SetObjectDeadFlag(obj);
115 return;
116 }
117
118 // Instance up the tree for this gun
119 while (mn != -1) {
120 vector tpnt;
121
122 vm_AnglesToMatrix(&m, pm->submodel[mn].angs.p, pm->submodel[mn].angs.h, pm->submodel[mn].angs.b);
123 vm_TransposeMatrix(&m);
124
125 tpnt = pnt * m;
126 fvec = fvec * m;
127 uvec = uvec * m;
128
129 pnt = tpnt + pm->submodel[mn].offset + pm->submodel[mn].mod_pos;
130
131 mn = pm->submodel[mn].parent;
132 }
133
134 // now instance for the entire object
135 m = parent->orient;
136 vm_TransposeMatrix(&m);
137
138 new_pos = pnt * m;
139 fvec = fvec * m;
140 uvec = uvec * m;
141
142 vm_VectorToMatrix(&new_orient, &fvec, &uvec, NULL);
143
144 new_pos += parent->pos;
145
146 ObjSetPos(obj, &new_pos, parent->roomnum, &new_orient, false);
147 } else {
148 SetObjectDeadFlag(obj);
149 }
150 }
151
152 bool PhysCalcGround(vector *ground_point, vector *ground_normal, object *obj, int ground_num) {
153 poly_model *pm;
154 vector pnt;
155 vector normal;
156 matrix m;
157 int mn; // submodel number
158 float normalized_time[MAX_SUBOBJECTS];
159 bool f_good_gp = true;
160
161 if (obj->render_type != RT_POLYOBJ && obj->type != OBJ_PLAYER) {
162 // mprintf(0,"Object type %d is not a polyobj!\n",obj->type);
163
164 if (ground_point)
165 *ground_point = obj->pos;
166 if (ground_normal)
167 *ground_normal = obj->orient.fvec;
168
169 return false;
170 }
171
172 pm = &Poly_models[obj->rtype.pobj_info.model_num];
173
174 if (pm->n_ground == 0) {
175 mprintf(0, "WARNING: Object with no weapons is firing.\n", ground_num);
176
177 if (ground_point)
178 *ground_point = obj->pos;
179 if (ground_normal)
180 *ground_normal = obj->orient.fvec;
181
182 return false;
183 }
184
185 SetNormalizedTimeObj(obj, normalized_time);
186
187 SetModelAnglesAndPos(pm, normalized_time);
188
189 if (ground_num < 0 || ground_num >= pm->n_ground) {
190 mprintf(0, "WARNING: Bashing ground num %d to 0.\n", ground_num);
191 ground_num = 0;
192 f_good_gp = false;
193 }
194
195 pnt = pm->ground_slots[ground_num].pnt;
196 normal = pm->ground_slots[ground_num].norm;
197 mn = pm->ground_slots[ground_num].parent;
198
199 // Instance up the tree for this ground
200 while (mn != -1) {
201 vector tpnt;
202
203 vm_AnglesToMatrix(&m, pm->submodel[mn].angs.p, pm->submodel[mn].angs.h, pm->submodel[mn].angs.b);
204 vm_TransposeMatrix(&m);
205
206 tpnt = pnt * m;
207 normal = normal * m;
208
209 pnt = tpnt + pm->submodel[mn].offset + pm->submodel[mn].mod_pos;
210
211 mn = pm->submodel[mn].parent;
212 }
213
214 m = obj->orient;
215 vm_TransposeMatrix(&m);
216
217 if (ground_point)
218 *ground_point = pnt * m;
219 if (ground_normal)
220 *ground_normal = normal * m;
221
222 if (ground_point)
223 *ground_point += obj->pos;
224
225 return f_good_gp;
226 }
227
228 // Applies rotational thrust over at delta time
229 void PhysicsApplyConstRotForce(object &objp, const vector &rotforce, vector &rotvel, float deltaTime) {
230 const double drag = objp.mtype.phys_info.rotdrag;
231 const double mass = objp.mtype.phys_info.mass;
232
233 if (mass < std::numeric_limits<double>::epsilon() || drag < std::numeric_limits<double>::epsilon()) {
234 // Invalid mass/drag
235 rotvel += rotforce * deltaTime;
236 return;
237 }
238
239 // Standard angular motion with a linear air drag (drag is proportional to angular velocity)
240 const double oneOverDrag = 1.0 / drag;
241 const double rotForceOverDrag[3] = {double(rotforce.x) * oneOverDrag, double(rotforce.y) * oneOverDrag,
242 double(rotforce.z) * oneOverDrag};
243 const double dragOverMass = drag / mass;
244 const double expDoMDt = exp(-dragOverMass * double(deltaTime));
245
246 double newRotVel[3];
247 newRotVel[0] = (double(rotvel.x) - rotForceOverDrag[0]) * expDoMDt + rotForceOverDrag[0];
248 newRotVel[1] = (double(rotvel.y) - rotForceOverDrag[1]) * expDoMDt + rotForceOverDrag[1];
249 newRotVel[2] = (double(rotvel.z) - rotForceOverDrag[2]) * expDoMDt + rotForceOverDrag[2];
250
251 // set the new rotational velocity
252 rotvel.x = static_cast<float>(newRotVel[0]);
253 rotvel.y = static_cast<float>(newRotVel[1]);
254 rotvel.z = static_cast<float>(newRotVel[2]);
255 }
256
257 // Applies a linear force over time. --Like gravity or thrust
258 void PhysicsApplyConstantForce(const object &objp, vector &newPos, vector &newVel, vector &movementVec,
259 const vector &force, float deltaTime) {
260 const vector &pos = objp.pos;
261 const vector &vel = objp.mtype.phys_info.velocity;
262 const double drag = static_cast<double>(objp.mtype.phys_info.drag);
263 const double mass = static_cast<double>(objp.mtype.phys_info.mass);
264
265 if (mass < std::numeric_limits<double>::epsilon() || drag < std::numeric_limits<double>::epsilon()) {
266 // No Mass/Drag
267 movementVec = (vel * deltaTime) + (force * (0.5f * deltaTime * deltaTime));
268 newPos = pos + movementVec;
269 newVel = vel + force * deltaTime;
270 return;
271 }
272
273 // Standard motion with a linear air drag (drag is proportional to velocity)
274 const double dt = static_cast<double>(deltaTime);
275 const double oneOverDrag = 1.0 / drag;
276 const double massOverDrag = mass / drag;
277 const double forceOverDrag[3] = {force.x * oneOverDrag, force.y * oneOverDrag, force.z * oneOverDrag};
278 const double objVel[3] = {static_cast<double>(vel.x), static_cast<double>(vel.y), static_cast<double>(vel.z)};
279 const double expDoMDt = exp((-1.0 / massOverDrag) * dt);
280
281 newPos.x = static_cast<float>(static_cast<double>(pos.x) + forceOverDrag[0] * dt +
282 massOverDrag * (objVel[0] - forceOverDrag[0]) * (1.0 - expDoMDt));
283 newPos.y = static_cast<float>(static_cast<double>(pos.y) + forceOverDrag[1] * dt +
284 massOverDrag * (objVel[1] - forceOverDrag[1]) * (1.0 - expDoMDt));
285 newPos.z = static_cast<float>(static_cast<double>(pos.z) + forceOverDrag[2] * dt +
286 massOverDrag * (objVel[2] - forceOverDrag[2]) * (1.0 - expDoMDt));
287 movementVec = newPos - pos;
288 newVel.x = static_cast<float>((objVel[0] - forceOverDrag[0]) * expDoMDt + forceOverDrag[0]);
289 newVel.y = static_cast<float>((objVel[1] - forceOverDrag[1]) * expDoMDt + forceOverDrag[1]);
290 newVel.z = static_cast<float>((objVel[2] - forceOverDrag[2]) * expDoMDt + forceOverDrag[2]);
291 }
292
293 // Banks an object as it turns (we counteract this and then reapply it when we
294 // actually do the turn -- cool)
295 void set_object_turnroll(object *obj, vector *rotvel, angle *turnroll) {
296 float desired_bank;
297 angle desired_bank_angle;
298
299 desired_bank = -(rotvel->y) * obj->mtype.phys_info.turnroll_ratio;
300
301 // If desired bank > 32000, we will rotate the ship in weird ways. Should limit turn-roll to 32000 if this
302 // ever occurs. BTW This is where the limiter should be placed.
303 if (fabs(desired_bank) > 32000.0) {
304 if (desired_bank < 0.0)
305 desired_bank = -32000.0f;
306 else
307 desired_bank = 32000.0f;
308 }
309
310 if (desired_bank > -1.0) // accounts for -.9999 this is rounded to 0 in a float to int conversion
311 desired_bank_angle = desired_bank;
312 else
313 desired_bank_angle = 65535 + desired_bank;
314
315 if (*turnroll != desired_bank_angle) {
316 int delta_ang;
317 int max_roll;
318
319 max_roll = obj->mtype.phys_info.max_turnroll_rate * Frametime;
320
321 if (*turnroll > 32000)
322 delta_ang = desired_bank - (*turnroll - 65535);
323 else
324 delta_ang = desired_bank - *turnroll;
325
326 if (labs(delta_ang) > max_roll)
327 if (delta_ang > 0)
328 *turnroll += max_roll;
329 else
330 *turnroll -= max_roll;
331 else
332 *turnroll = desired_bank_angle;
333 } else
334 *turnroll = desired_bank_angle;
335 }
336
337 // Maximum number of objects that we already hit.
338 #define MAX_IGNORE_OBJS 100
339
340 // When leveling non-player objects - how many angles per sec. do they correct with.
341 #define MAX_LEVEL_ANGLES_PER_SEC 30000.0f
342
343 // Base scaler for pitch scaling of autoleveling
344 #define MAX_AUTOLEVEL_TILT_ANGLE 11000
345
346 // Multiplier for newbie autoleveling scaling of tilt angle
347 #define NEWBIE_AUTOLEVEL_TILT_ANGLE_SCALAR 1.25f
348
349 // How long a newbie sits there before the pitch leveling kicks in
350 #define NEWBIE_PITCH_LEVEL_TIME 1.5f
351
352 #define BANK_AUTOLEVEL_SPEED_SCALAR 2.0
353
354 // Rotational simulator the updates orient, rotvel, and turnroll
355 // (using these original values + frametime + rotthrust)
356 bool PhysicsDoSimRot(object *obj, float frame_time, matrix *orient, vector *rotthrust, vector *rotvel,
357 angle *turnroll) {
358 angvec tangles;
359 matrix rotmat;
360 physics_info *pi;
361 bool f_leveling = false;
362 float max_tilt_angle = 0;
363 bool f_newbie_leveling = false;
364
365 if (frame_time <= 0.0)
366 return false; // chrishack
367
368 pi = &obj->mtype.phys_info;
369
370 if (obj == Player_object && obj->control_type != CT_AI) {
371 max_tilt_angle = MAX_AUTOLEVEL_TILT_ANGLE;
372
373 if (OBJECT_OUTSIDE(obj)) {
374 if (Default_player_terrain_leveling) {
375 f_leveling = true;
376 if (Default_player_terrain_leveling > 1) {
377 max_tilt_angle *= NEWBIE_AUTOLEVEL_TILT_ANGLE_SCALAR;
378 f_newbie_leveling = true;
379 }
380 }
381 } else {
382 if (Default_player_room_leveling) {
383 f_leveling = true;
384 if (Default_player_room_leveling > 1) {
385 max_tilt_angle *= NEWBIE_AUTOLEVEL_TILT_ANGLE_SCALAR;
386 f_newbie_leveling = true;
387 }
388 }
389 }
390 } else {
391 if (pi->flags & PF_LEVELING)
392 f_leveling = true;
393 }
394
395 // Fixed rate rotaters
396 if (obj->mtype.phys_info.flags & PF_FIXED_ROT_VELOCITY) {
397 tangles.p = (int16_t)(rotvel->x * frame_time);
398 tangles.h = (int16_t)(rotvel->y * frame_time);
399 tangles.b = (int16_t)(rotvel->z * frame_time);
400
401 vm_AnglesToMatrix(&rotmat, tangles.p, tangles.h, tangles.b);
402 *orient = *orient * rotmat; // ObjSetOrient below
403
404 vm_Orthogonalize(orient); // Rest done after call
405
406 return true;
407 }
408
409 // now rotate object
410 // unrotate object for bank caused by turn
411 if (*turnroll != 0) {
412 tangles.p = tangles.h = 0;
413 tangles.b = -(*turnroll);
414 vm_AnglesToMatrix(&rotmat, tangles.p, tangles.h, tangles.b);
415 // Apply rotation matrix to the orientation matrix
416 *orient = *orient * rotmat; // ObjSetOrient is below
417 }
418
419 // Auto-leveling
420 if ((f_leveling) && (obj->type != OBJ_PLAYER)) {
421 if (fabs(rotthrust->z) < 100.0f) {
422 if ((fabs(rotvel->z) < 1500.0f) /*&& (pi->turnroll <= 10 || pi->turnroll >= 65535 - 10)*/) {
423 angvec ang;
424 vector fvec;
425 int bound;
426
427 // f_leveling = true;
428 // pi->turnroll = 0;
429
430 // extract angles from a matrix
431 vm_ExtractAnglesFromMatrix(&ang, orient);
432
433 if ((pi->flags & PF_TURNROLL) && *turnroll) {
434 bound = *turnroll;
435 if (bound > 32768) {
436 bound = 65535 - bound;
437 }
438 } else {
439 bound = 10;
440 }
441
442 if (ang.b > bound && ang.b < 65535 - bound) // About 1 degree
443 {
444 float scale;
445
446 // Scale on pitch
447 if (ang.p < 32768) {
448 scale = abs(16834 - (int)ang.p) / 16384.0f;
449 } else {
450 scale = abs(49152 - (int)ang.p) / 16384.0f;
451 }
452
453 // mprintf(0, "scale %f, ", scale);
454
455 if (ang.b < 32768) {
456 float al_rate = scale * MAX_LEVEL_ANGLES_PER_SEC * (1.0f - (abs(16834 - (int)ang.b) / 16384.0f));
457
458 if (al_rate < scale * 5000.0f) {
459 al_rate = scale * 5000.0f;
460 }
461
462 al_rate *= frame_time;
463
464 if (al_rate > ang.b)
465 al_rate = ang.b;
466
467 // mprintf(0, "L al_rate %f\n", al_rate);
468
469 ang.b -= al_rate;
470
471 } else {
472 float al_rate = scale * MAX_LEVEL_ANGLES_PER_SEC * (1.0f - (abs(16384 - ((int)ang.b - 32768)) / 16384.0f));
473
474 if (al_rate < scale * 5000.0f) {
475 al_rate = scale * 5000.0f;
476 }
477
478 al_rate *= frame_time;
479
480 if (al_rate > 65535 - ang.b)
481 al_rate = 65535 - ang.b;
482
483 // mprintf(0, "G al_rate %f\n", al_rate);
484
485 ang.b += al_rate;
486 }
487
488 fvec = orient->fvec;
489
490 vm_AnglesToMatrix(orient, ang.p, ang.h, ang.b);
491 }
492 }
493 }
494 } else if ((f_leveling) && (obj->type == OBJ_PLAYER)) {
495 bool f_pitch_leveled = false;
496 bool f_player_fired_recently = false;
497 int i;
498
499 for (i = 0; i < 21; i++) {
500 if (obj->dynamic_wb[i].last_fire_time + NEWBIE_PITCH_LEVEL_TIME > Gametime) {
501 f_player_fired_recently = true;
502 f_newbie_leveling = false;
503 break;
504 }
505 }
506
507 if (!f_player_fired_recently && f_newbie_leveling &&
508 (Players[obj->id].last_thrust_time + NEWBIE_PITCH_LEVEL_TIME < Gametime)) {
509 if (1) {
510 angvec ang;
511 int bound;
512
513 // f_leveling = true;
514 // pi->turnroll = 0;
515
516 // extract angles from a matrix
517 vm_ExtractAnglesFromMatrix(&ang, orient);
518
519 bound = 750;
520
521 // About 1 degree -- front or back
522 if ((ang.p > bound) && (ang.p < 65535 - bound) && // Forward
523 abs(ang.p - 32768) > bound) // Backward
524 {
525 float scale;
526 f_pitch_leveled = true;
527
528 if (obj->orient.uvec.y < 0.0f) {
529 ang.p += 16384;
530 }
531
532 scale = 1.05f - fabs(obj->orient.uvec.y);
533 // scale *= scale;
534
535 if (scale > 1.0)
536 scale = 1.0;
537
538 if (ang.p < 16834) {
539 rotthrust->x -= scale * obj->mtype.phys_info.full_rotthrust;
540 } else if (ang.p < 32768) {
541 rotthrust->x += scale * obj->mtype.phys_info.full_rotthrust;
542 } else if (ang.p < 49152) {
543 rotthrust->x -= scale * obj->mtype.phys_info.full_rotthrust;
544 } else {
545 rotthrust->x += scale * obj->mtype.phys_info.full_rotthrust;
546 }
547 }
548 }
549 }
550
551 if (!f_pitch_leveled && fabs(rotthrust->z) < 100.0f) {
552 angvec ang;
553 int bound;
554
555 // extract angles from a matrix
556 vm_ExtractAnglesFromMatrix(&ang, orient);
557 if ((ang.p < 32768 && (abs((int)ang.p - (int)16834) > max_tilt_angle)) ||
558 (ang.p >= 32768 && (abs((int)ang.p - (int)49152) > max_tilt_angle))) {
559
560 if ((pi->flags & PF_TURNROLL) && *turnroll) {
561 bound = *turnroll;
562 if (bound > 32768) {
563 bound = 65535 - bound;
564 }
565 } else {
566 bound = 10;
567 }
568
569 if (ang.b > bound && ang.b < 65535 - bound) // About 1 degree
570 {
571 float scale;
572
573 // Scale on pitch
574 if (ang.p < 32768) {
575 scale = (abs(16834 - (int)ang.p) - max_tilt_angle) / (16384.0f - max_tilt_angle);
576 } else {
577 scale = (abs(49152 - (int)ang.p) - max_tilt_angle) / (16384.0f - max_tilt_angle);
578 }
579
580 if (ang.b < 32768) {
581 float temp_scale;
582
583 if (ang.b > 28672) {
584 temp_scale = (1.04f - ((28672 - 16834) / 16384.0f));
585 } else {
586 temp_scale = (1.04f - (abs(16834 - (int)ang.b) / 16384.0f));
587 }
588
589 temp_scale *= temp_scale;
590 scale *= temp_scale;
591 if (f_player_fired_recently)
592 scale *= .25f;
593
594 rotthrust->z -= scale * BANK_AUTOLEVEL_SPEED_SCALAR * obj->mtype.phys_info.full_rotthrust;
595 } else {
596 float temp_scale;
597
598 if (ang.b < 36864) {
599 temp_scale = (1.04f - ((49152 - 36864) / 16384.0f));
600 } else {
601 temp_scale = (1.04f - (abs(49152 - (int)ang.b) / 16384.0f));
602 }
603
604 temp_scale *= temp_scale;
605 scale *= temp_scale;
606 if (f_player_fired_recently)
607 scale *= .25f;
608
609 rotthrust->z += scale * BANK_AUTOLEVEL_SPEED_SCALAR * obj->mtype.phys_info.full_rotthrust;
610 }
611 }
612 }
613 }
614 }
615
616 if (pi->rotdrag > 0.0f) {
617 if (!(pi->flags & PF_USES_THRUST)) {
618 *rotthrust = Zero_vector;
619 }
620
621 PhysicsApplyConstRotForce(*obj, *rotthrust, *rotvel, frame_time);
622 }
623
624 // Apply rotation to the "un-rollbanked" object
625 tangles.p = (int16_t)(rotvel->x * frame_time); // Casting to int16_t is required for aarch64 to avoid FCVTZU
626 // instruction which strips the negative sign
627 tangles.h = (int16_t)(rotvel->y * frame_time);
628 tangles.b = (int16_t)(rotvel->z * frame_time);
629
630 vm_AnglesToMatrix(&rotmat, tangles.p, tangles.h, tangles.b);
631 *orient = *orient * rotmat; // ObjSetOrient is below
632
633 // Determine the new turnroll from this amount of turning
634 if (pi->flags & PF_TURNROLL) {
635 set_object_turnroll(obj, rotvel, turnroll);
636 }
637
638 // re-rotate object for bank caused by turn
639 if (*turnroll != 0) {
640 tangles.p = tangles.h = 0;
641 tangles.b = *turnroll;
642 vm_AnglesToMatrix(&rotmat, tangles.p, tangles.h, tangles.b);
643 *orient = *orient * rotmat; // ObjSetOrient is below
644 }
645 // Make sure the new orientation is valid
646 vm_Orthogonalize(orient);
647
648 return true;
649 }
650
651 void PhysicsDoSimLinear(const object &obj, const vector &pos, const vector &force, vector &velocity,
652 vector &movementVec, vector &movementPos, float simTime, int count) {
653 // Determine velocity
654 if (obj.mtype.phys_info.flags & PF_FIXED_VELOCITY) {
655 movementVec = velocity * simTime;
656 movementPos = pos + movementVec;
657 return;
658 }
659
660 vector forceToUse = force;
661 if (!ROOMNUM_OUTSIDE(obj.roomnum) && !(obj.mtype.phys_info.flags & PF_LOCK_MASK) &&
662 Rooms[obj.roomnum].wind != Zero_vector && count == 0 && obj.mtype.phys_info.drag > 0.0f &&
663 !(obj.type == OBJ_POWERUP && Level_powerups_ignore_wind)) {
664 // Factor in outside wind
665 const vector &wind = Rooms[obj.roomnum].wind;
666 vector deltaForce = wind * obj.mtype.phys_info.drag * 16.0f;
667 forceToUse += deltaForce;
668 }
669
670 PhysicsApplyConstantForce(obj, movementPos, velocity, movementVec, forceToUse, simTime);
671
672 #ifdef _DEBUG
673 if (Physics_player_verbose && obj.type == OBJ_PLAYER) {
674 mprintf(0, "Player Velocity = %f(%f)\n", vm_GetMagnitude(&velocity), vm_GetMagnitude(&movementVec) / simTime);
675 }
676 #endif
677 }
678
679 // -----------------------------------------------------------------------------------------------------------
680 // Simulate a physics object for this frame
681 void do_physics_sim(object *obj) {
682 // Since we might not call FVI, set this here
683 Fvi_num_recorded_faces = 0;
684
685 int n_ignore_objs = 0; // The number of ignored objects
686 int ignore_obj_list[MAX_IGNORE_OBJS + 1]; // List of ignored objects
687
688 int fate; // Collision type for response code
689 vector movement_vec; // Movement in this frame
690 vector movement_pos; // End point of the movement
691
692 int count = 0; // Simulation loop counter
693 int sim_loop_limit = (obj->type == OBJ_PLAYER) ? MAX_PLAYER_SIM_LOOPS : MAX_NON_PLAYER_SIM_LOOPS;
694
695 int objnum = OBJNUM(obj); // Simulated object's index
696 fvi_info hit_info; // Hit information
697 fvi_query fq; // Movement query
698
699 float sim_time_remaining = Frametime; // Amount of simulation time remaining (current iteration)
700 float old_sim_time_remaining = Frametime; // Amount of simulation time remaining (previous iteration)
701
702 vector init_pos = obj->pos; // Initial position
703 int init_room = obj->roomnum; // Initial room
704
705 vector start_pos; // Values at the start of current simulation loop
706 vector start_vel;
707 matrix start_orient;
708 vector start_rotvel;
709 angle start_turnroll;
710 int start_room;
711
712 float moved_time; // How long objected moved before hit something
713 physics_info *pi = &obj->mtype.phys_info; // Simulated object's physics information
714
715 int bounced = 0; // Did the object bounce?
716
717 vector total_force; // Constant force acting on an object
718
719 bool f_continue_sim; // Should we run another simulation loop
720 bool f_start_fvi_record = true; // Records the rooms that are passed thru
721
722 bool f_turn_gravity_on = false;
723
724 // Assert conditions
725 ASSERT(obj->type != OBJ_NONE);
726 ASSERT(obj->movement_type == MT_PHYSICS);
727 ASSERT(!(obj->mtype.phys_info.flags & PF_USES_THRUST) || obj->mtype.phys_info.drag != 0.0);
728
729 ASSERT(std::isfinite(obj->mtype.phys_info.velocity.x)); // Caller wants to go to infinity! -- Not FVI's fault.
730 ASSERT(std::isfinite(obj->mtype.phys_info.velocity.y)); // Caller wants to go to infinity! -- Not FVI's fault.
731 ASSERT(std::isfinite(obj->mtype.phys_info.velocity.z)); // Caller wants to go to infinity! -- Not FVI's fault.
732
733 #ifdef _DEBUG
734 if (!Game_do_flying_sim) {
735 return;
736 }
737 #endif
738
739 // Make powerups above the ceiling fall with gravity
740 if (obj->type == OBJ_POWERUP && ROOMNUM_OUTSIDE(obj->roomnum) &&
741 obj->pos.y > (CEILING_HEIGHT - obj->size - CEILING_POWERUP_DELTA) && !(obj->mtype.phys_info.flags & PF_GRAVITY)) {
742 obj->mtype.phys_info.flags |= PF_GRAVITY;
743 obj->flags |= OF_TEMP_GRAVITY;
744 } else if (obj->flags & OF_TEMP_GRAVITY) {
745 obj->mtype.phys_info.flags &= ~PF_GRAVITY;
746 }
747
748 // Instant bail cases
749 if ((obj->flags & (OF_DEAD | OF_ATTACHED)) || (Frametime <= 0.0) || (obj->type == OBJ_DUMMY)) {
750 return;
751 }
752
753 if (obj->type == OBJ_PLAYER &&
754 (obj->mtype.phys_info.thrust != Zero_vector || obj->mtype.phys_info.rotthrust != Zero_vector)) {
755 Players[obj->id].last_thrust_time = Gametime;
756 }
757
758 // mprintf(0, "%d Over terrain = %d\n", obj - Objects, obj->flags & OF_OVER_TERRAIN);
759
760 // If the object wiggles
761 if ((obj->mtype.phys_info.flags & PF_WIGGLE) && (Demo_flags != DF_PLAYBACK)) {
762 float swiggle;
763 vector w_pos;
764
765 if (vm_GetMagnitude(&obj->mtype.phys_info.thrust) < .1) {
766 obj->mtype.phys_info.last_still_time -= Frametime / WIGGLE_FALLOFF_TIME;
767 } else {
768 obj->mtype.phys_info.last_still_time += Frametime / WIGGLE_FALLOFF_TIME;
769 }
770
771 if (obj->mtype.phys_info.last_still_time < 0.0)
772 obj->mtype.phys_info.last_still_time = 0.0;
773 else if (obj->mtype.phys_info.last_still_time > 1.0)
774 obj->mtype.phys_info.last_still_time = 1.0;
775
776 // Ship always keeps 10% of its wiggle
777 // if(obj->mtype.phys_info.last_still_time < 1.0)
778 {
779 float scaler = 1.0f - obj->mtype.phys_info.last_still_time;
780 if (scaler < .1f)
781 scaler = .1f;
782
783 swiggle = obj->mtype.phys_info.wiggle_amplitude * (scaler) *
784 (FixSin((angle)((int)(Gametime * obj->mtype.phys_info.wiggles_per_sec * 65535) % 65535)) -
785 FixSin((angle)((int)((Gametime - Frametime) * obj->mtype.phys_info.wiggles_per_sec * 65535) % 65535)));
786 w_pos = obj->pos + obj->orient.uvec * (swiggle);
787
788 fq.p0 = &obj->pos;
789 fq.startroom = obj->roomnum;
790 fq.p1 = &w_pos;
791 fq.rad = obj->size;
792 fq.thisobjnum = objnum;
793 fq.ignore_obj_list = NULL;
794 fq.flags = FQ_CHECK_OBJS | FQ_RECORD | FQ_IGNORE_POWERUPS;
795 fq.flags |= FQ_NEW_RECORD_LIST;
796 fq.flags |= FQ_CHECK_CEILING;
797
798 // Need for weapons to go thru transparent areas of a texture
799 if (obj->type == OBJ_WEAPON)
800 fq.flags |= FQ_TRANSPOINT;
801
802 if (obj->flags & OF_NO_OBJECT_COLLISIONS)
803 fq.flags &= ~FQ_CHECK_OBJS;
804
805 fate = fvi_FindIntersection(&fq, &hit_info);
806
807 // Only wiggle on a non-hit case
808 if (fate == HIT_NONE) {
809
810 ASSERT(hit_info.hit_room != -1);
811
812 f_start_fvi_record = false;
813
814 // update object's position and segment number
815 ObjSetPos(obj, &hit_info.hit_pnt, hit_info.hit_room, NULL, false);
816 }
817 }
818 }
819
820 // If the object is effected by gravity (normal, lighter than air, and anti-gravity)
821 if (obj->mtype.phys_info.flags & PF_GRAVITY) {
822 total_force.x = total_force.z = 0.0;
823 total_force.y = Gravity_strength * obj->mtype.phys_info.mass;
824 } else if (obj->mtype.phys_info.flags & PF_REVERSE_GRAVITY) {
825 total_force.x = total_force.z = 0.0;
826 total_force.y = -Gravity_strength * obj->mtype.phys_info.mass;
827 } else {
828 total_force.x = total_force.y = total_force.z = 0.0;
829 }
830
831 obj->flags &= (~OF_STOPPED_THIS_FRAME);
832
833 // Do rotation velocity/accel stuff
834 bool f_rotated = false;
835
836 if (!(fabs(pi->velocity.x) > .000001 || fabs(pi->velocity.y) > .000001 || fabs(pi->velocity.z) > .000001 ||
837 fabs(pi->thrust.x) > .000001 || fabs(pi->thrust.y) > .000001 || fabs(pi->thrust.z) > .000001 ||
838 fabs(pi->rotvel.x) > .000001 || fabs(pi->rotvel.y) > .000001 || fabs(pi->rotvel.z) > .000001 ||
839 fabs(pi->rotthrust.x) > .000001 || fabs(pi->rotthrust.y) > .000001 || fabs(pi->rotthrust.z) > .000001 ||
840 (obj->mtype.phys_info.flags & PF_GRAVITY) ||
841 ((!ROOMNUM_OUTSIDE(obj->roomnum)) && Rooms[obj->roomnum].wind != Zero_vector) ||
842 (obj->mtype.phys_info.flags & PF_WIGGLE) ||
843 ((obj->ai_info != NULL) && ((obj->ai_info->flags & AIF_REPORT_NEW_ORIENT) != 0)))) {
844 if ((obj->flags & OF_MOVED_THIS_FRAME)) {
845 obj->flags &= (~OF_MOVED_THIS_FRAME);
846 obj->flags |= OF_STOPPED_THIS_FRAME;
847 }
848
849 return;
850 }
851
852 if (obj->ai_info)
853 obj->ai_info->flags &= ~AIF_REPORT_NEW_ORIENT;
854
855 obj->flags |= OF_MOVED_THIS_FRAME;
856
857 // This assumes that the thrust does not change within a frame. If it does, account for it
858 // there... like missiles bouncing off a wall and changing heading. --chrishack
859 if (obj->mtype.phys_info.drag != 0.0) {
860 if (obj->mtype.phys_info.flags & PF_USES_THRUST) {
861 total_force += obj->mtype.phys_info.thrust;
862 }
863 }
864
865 // mprintf(0, "PHYSICS: Current obj: %d, %9.2fx %9.2fy %9.2fz\n", OBJNUM(obj), XYZ(&obj->pos));
866 // mprintf(2,1,0,"x %9.2f\ny %9.2f\nz %9.2f", XYZ(&obj->mtype.phys_info.velocity));
867 // mprintf(0, "PHYSICS: Current velocity (%f, %f, %f)\n", XYZ(&obj->mtype.phys_info.velocity));
868
869 Physics_normal_counter++;
870
871 // Simulate movement until we are done (i.e. Frametime has passed or object is done moving)
872 do {
873 // Values at start of sim loop
874 start_pos = obj->pos;
875 start_vel = obj->mtype.phys_info.velocity;
876 start_orient = obj->orient;
877 start_rotvel = obj->mtype.phys_info.rotvel;
878 start_turnroll = obj->mtype.phys_info.turnroll;
879 start_room = obj->roomnum;
880
881 matrix end_orient = start_orient;
882 vector end_rotvel = start_rotvel;
883 angle end_turnroll = start_turnroll;
884 vector end_vel = start_vel;
885
886 #ifdef _DEBUG
887 // This records the sim.
888 sim_loop_record[count].phys_info = obj->mtype.phys_info;
889 #endif
890
891 // Initailly assume that this is the last sim cycle
892 f_continue_sim = false;
893
894 // Determines forces
895 if (count > 0) {
896 // If the object is effected by gravity (normal, lighter than air, and anti-gravity)
897 if (obj->mtype.phys_info.flags & PF_GRAVITY) {
898 total_force.x = total_force.z = 0.0;
899 total_force.y = Gravity_strength * obj->mtype.phys_info.mass;
900 } else if (obj->mtype.phys_info.flags & PF_REVERSE_GRAVITY) {
901 total_force.x = total_force.z = 0.0;
902 total_force.y = -Gravity_strength * obj->mtype.phys_info.mass;
903 } else if (!(obj->mtype.phys_info.flags & PF_BOUNCE)) {
904 // Player ship
905 } else {
906 total_force.x = total_force.y = total_force.z = 0.0;
907 }
908 }
909
910 // Compute rotational information
911 // Chrishack -- replace OBJ_CLUTTER with a BBOX_HIT_TYPE
912 bool f_set_orient;
913
914 if ((f_rotated == false && count == 0) || obj->type == OBJ_CLUTTER) {
915 f_rotated = true;
916 f_set_orient = true;
917 PhysicsDoSimRot(obj, sim_time_remaining, &end_orient, &obj->mtype.phys_info.rotthrust, &end_rotvel,
918 &end_turnroll);
919
920 ObjSetOrient(obj, &end_orient);
921 } else {
922 f_set_orient = false;
923 }
924
925 // Updates the thrust vector to the orientation for homing weapons
926 if ((obj->type == OBJ_WEAPON) && (pi->flags & PF_USES_THRUST) && (pi->flags & PF_HOMING)) {
927 pi->thrust = end_orient.fvec * pi->full_thrust;
928 movement_vec = obj->mtype.phys_info.velocity * Frametime;
929 movement_pos = obj->pos + movement_vec;
930 }
931
932 PhysicsDoSimLinear(*obj, obj->pos, total_force, end_vel, movement_vec, movement_pos, sim_time_remaining, count);
933
934 // Only do velocity until we've reached our destination position
935 // This is useful for multiplayer
936 if (obj->mtype.phys_info.flags & PF_DESTINATION_POS) {
937 vector sub1 = obj->pos - obj->mtype.phys_info.dest_pos;
938 vector sub2 = movement_pos - obj->mtype.phys_info.dest_pos;
939
940 if (vm_DotProduct(&sub1, &sub2) <= 0) {
941 obj->mtype.phys_info.flags &= ~PF_DESTINATION_POS;
942 vm_MakeZero(&obj->mtype.phys_info.velocity);
943 vm_MakeZero(&movement_vec);
944 movement_pos = obj->pos;
945 goto end_of_sim;
946 }
947
948 if ((vm_VectorDistanceQuick(&obj->mtype.phys_info.dest_pos, &obj->pos)) < 1) {
949 vm_MakeZero(&obj->mtype.phys_info.velocity);
950 obj->mtype.phys_info.flags &= ~PF_DESTINATION_POS;
951 goto end_of_sim;
952 }
953 }
954
955 // We are done, as there is no collision response with PF_NO_COLLIDE
956 if (obj->mtype.phys_info.flags & PF_NO_COLLIDE) {
957 obj->mtype.phys_info.rotvel = end_rotvel;
958 obj->mtype.phys_info.turnroll = end_turnroll;
959 obj->mtype.phys_info.velocity = end_vel;
960
961 // ObjSetOrient(obj,&end_orient);
962 ObjSetPos(obj, &movement_pos, obj->roomnum, NULL, false);
963
964 ObjSetAABB(obj);
965 obj->last_pos = init_pos;
966
967 goto end_of_sim;
968 }
969
970 Physics_normal_looping_counter++;
971
972 // Cap ignore list -- (Objects we already hit this frame)
973 ignore_obj_list[n_ignore_objs] = -1;
974
975 vector temp_vel = obj->mtype.phys_info.velocity;
976 obj->mtype.phys_info.velocity = (movement_pos - start_pos) / sim_time_remaining;
977
978 fq.p0 = &obj->pos;
979 fq.startroom = obj->roomnum;
980 fq.p1 = &movement_pos;
981 fq.rad = obj->size;
982 fq.thisobjnum = objnum;
983 fq.ignore_obj_list = ignore_obj_list;
984 fq.flags = FQ_CHECK_OBJS | FQ_RECORD;
985 if (f_start_fvi_record)
986 fq.flags |= FQ_NEW_RECORD_LIST;
987
988 fq.o_orient = &start_orient;
989 fq.o_rotvel = &start_rotvel;
990 fq.o_rotthrust = &obj->mtype.phys_info.rotthrust;
991 fq.o_turnroll = &start_turnroll;
992 fq.o_velocity = &start_vel;
993 fq.o_thrust = &total_force;
994
995 f_start_fvi_record = false;
996
997 // Need for weapons to go thru transparent areas of a texture
998 if (obj->type == OBJ_WEAPON)
999 fq.flags |= FQ_TRANSPOINT;
1000
1001 // Added a ceiling for players and powerups
1002 if (obj->type == OBJ_POWERUP || (obj->type == OBJ_PLAYER && obj->control_type != CT_AI) ||
1003 obj->type == OBJ_OBSERVER || (obj == Players[Player_object->id].guided_obj) ||
1004 (obj->flags & OF_FORCE_CEILING_CHECK))
1005 fq.flags |= FQ_CHECK_CEILING;
1006
1007 if ((obj->type == OBJ_WEAPON) && (obj->ctype.laser_info.hit_status != WPC_NOT_USED))
1008 fq.flags |= FQ_IGNORE_WALLS;
1009
1010 if (obj->flags & OF_NO_OBJECT_COLLISIONS)
1011 fq.flags &= ~FQ_CHECK_OBJS;
1012
1013 if (IS_GENERIC(obj->type) && (Object_info[obj->id].flags & OIF_IGNORE_FORCEFIELDS_AND_GLASS))
1014 fq.flags |= FQ_IGNORE_RENDER_THROUGH_PORTALS;
1015
1016 // Initially assume that we fully move
1017 hit_info.hit_turnroll = end_turnroll;
1018 hit_info.hit_orient = end_orient;
1019 hit_info.hit_rotvel = end_rotvel;
1020 hit_info.hit_velocity = end_vel;
1021 hit_info.hit_time = sim_time_remaining;
1022
1023 if (obj->type == OBJ_PLAYER) {
1024 fq.flags |= FQ_MULTI_POINT;
1025 }
1026
1027 fate = fvi_FindIntersection(&fq, &hit_info);
1028
1029 if (fate != HIT_NONE) {
1030 if (obj->type == OBJ_PLAYER && hit_info.num_hits > 1) {
1031 vector n = Zero_vector;
1032 int i;
1033
1034 for (i = 0; i < hit_info.num_hits; i++) {
1035 n += hit_info.hit_wallnorm[i];
1036 }
1037
1038 hit_info.hit_wallnorm[0] = n;
1039 }
1040 vm_NormalizeVector(&hit_info.hit_wallnorm[0]);
1041 }
1042
1043 obj->mtype.phys_info.velocity = temp_vel;
1044
1045 // Accounts for precomputed hit rays
1046 if ((obj->type == OBJ_WEAPON) && (obj->ctype.laser_info.hit_status == WPC_HIT_WALL)) {
1047 vector to_hit_pnt, to_fvi_pos;
1048
1049 if (fate == HIT_NONE) {
1050 to_hit_pnt = obj->ctype.laser_info.hit_pnt - obj->pos;
1051 to_fvi_pos = obj->ctype.laser_info.hit_pnt - hit_info.hit_pnt;
1052 } else {
1053 to_hit_pnt = obj->ctype.laser_info.hit_wall_pnt - obj->pos;
1054 to_fvi_pos = obj->ctype.laser_info.hit_wall_pnt - hit_info.hit_face_pnt[0];
1055 }
1056
1057 if ((to_hit_pnt * to_fvi_pos) <= 0.0f) {
1058 fate = hit_info.hit_type[0] = HIT_WALL;
1059
1060 hit_info.hit_pnt = obj->ctype.laser_info.hit_pnt;
1061 hit_info.hit_face_pnt[0] = obj->ctype.laser_info.hit_wall_pnt;
1062 hit_info.hit_room = obj->ctype.laser_info.hit_room;
1063 hit_info.hit_face_room[0] = obj->ctype.laser_info.hit_pnt_room;
1064 hit_info.hit_wallnorm[0] = obj->ctype.laser_info.hit_wall_normal;
1065 hit_info.hit_face[0] = obj->ctype.laser_info.hit_face;
1066
1067 obj->ctype.laser_info.hit_status = WPC_NOT_USED;
1068 }
1069 }
1070
1071 // chrishack -- move all FVI ASSERT to here!
1072 ASSERT(fate != HIT_WALL || (fate == HIT_WALL && hit_info.hit_face[0] > -1));
1073 ASSERT(hit_info.hit_pnt.x > -100000000.0);
1074 ASSERT(hit_info.hit_pnt.y > -100000000.0);
1075 ASSERT(hit_info.hit_pnt.z > -100000000.0);
1076 ASSERT(hit_info.hit_pnt.x < 100000000.0);
1077 ASSERT(hit_info.hit_pnt.y < 100000000.0);
1078 ASSERT(hit_info.hit_pnt.z < 100000000.0);
1079 ASSERT(!(fate != HIT_OUT_OF_TERRAIN_BOUNDS && hit_info.hit_room == -1));
1080
1081 #ifdef _DEBUG
1082 sim_loop_record[count].start_pos = obj->pos;
1083 sim_loop_record[count].hit_info = hit_info;
1084
1085 if (fate != HIT_NONE) {
1086 if (Physics_player_verbose) {
1087 objnum = ObjCreate(OBJ_DEBUG_LINE, 1, hit_info.hit_room, &hit_info.hit_face_pnt[0], NULL);
1088 if (objnum >= 0) { // DAJ -1FIX
1089 Objects[objnum].rtype.line_info.end_pos = hit_info.hit_face_pnt[0] + hit_info.hit_wallnorm[0];
1090 Objects[objnum].size = vm_VectorDistance(&Player_object->pos, &hit_info.hit_pnt);
1091 ObjSetAABB(&Objects[objnum]);
1092
1093 Objects[objnum].flags |= OF_USES_LIFELEFT;
1094 Objects[objnum].lifeleft = 10.0f;
1095 Objects[objnum].lifetime = 10.0f;
1096 }
1097 if (obj == Player_object) {
1098 mprintf(0, "Fate = %d for player - ", fate);
1099 if (fate == HIT_OBJECT || fate == HIT_SPHERE_2_POLY_OBJECT) {
1100 if (Objects[hit_info.hit_object[0]].flags & OF_BIG_OBJECT) {
1101 mprintf(0, "Big object\n");
1102 } else {
1103 mprintf(0, "Small object\n");
1104 }
1105 } else {
1106 mprintf(0, "non-object\n");
1107 }
1108 }
1109 }
1110 }
1111 #endif
1112
1113 if (f_set_orient) {
1114 obj->mtype.phys_info.turnroll = hit_info.hit_turnroll;
1115 obj->mtype.phys_info.rotvel = hit_info.hit_rotvel;
1116
1117 f_set_orient = false;
1118 }
1119
1120 obj->mtype.phys_info.velocity = hit_info.hit_velocity;
1121
1122 // Instant bails for collision response
1123 if (obj->type == OBJ_PLAYER &&
1124 ((fate == HIT_OUT_OF_TERRAIN_BOUNDS) ||
1125 (ROOMNUM_OUTSIDE(hit_info.hit_room) && GetTerrainCellFromPos(&hit_info.hit_pnt) == -1))) {
1126 ObjSetPos(obj, &init_pos, init_room, NULL, false);
1127 obj->last_pos = init_pos;
1128
1129 return;
1130 } else if (fate == HIT_OUT_OF_TERRAIN_BOUNDS) {
1131 SetObjectDeadFlag(obj);
1132 obj->last_pos = init_pos;
1133
1134 return;
1135 }
1136
1137 // This is so the collide code knows that last collision position if there are multiple collisions
1138 obj->last_pos = obj->pos;
1139
1140 // update object's position and segment number
1141 ObjSetPos(obj, &hit_info.hit_pnt, hit_info.hit_room, NULL, false);
1142
1143 // Calulate amount of sim time left
1144 if (fate == HIT_NONE) {
1145 moved_time = old_sim_time_remaining = sim_time_remaining;
1146 sim_time_remaining = 0.0;
1147 f_continue_sim = false;
1148 } else {
1149 vm_NormalizeVector(&hit_info.hit_wallnorm[0]);
1150
1151 vector moved_vec_n;
1152 float attempted_dist, actual_dist;
1153
1154 // Save results of this simulation
1155 old_sim_time_remaining = sim_time_remaining;
1156 moved_vec_n = obj->pos - start_pos; // chrishack -- use this copy
1157 if (moved_vec_n != Zero_vector) {
1158 actual_dist = vm_NormalizeVector(&moved_vec_n);
1159 } else {
1160 actual_dist = 0.0f;
1161 }
1162
1163 // chrishack -- potentially bad -- outside to inside stuff
1164 // We where sitting in a wall -- invalid starting point
1165 // moved backwards
1166 if (fate == HIT_WALL && moved_vec_n * movement_vec < -0.000001 && actual_dist != 0.0) {
1167 ObjSetPos(obj, &start_pos, start_room, NULL, false);
1168
1169 moved_time = 0.0;
1170 } else {
1171 // Compute more results of this simulation
1172 attempted_dist = vm_GetMagnitude(&movement_vec);
1173 sim_time_remaining = sim_time_remaining * ((attempted_dist - actual_dist) / attempted_dist);
1174 moved_time = old_sim_time_remaining - sim_time_remaining;
1175
1176 // chrishack -- we have no time left (plus some) This can happen if FVI corrects a near tangental
1177 // movement by adding the radius - thus, we move a little too far.
1178 if (sim_time_remaining < 0.0) {
1179 sim_time_remaining = 0.0;
1180 moved_time = old_sim_time_remaining;
1181 }
1182
1183 // chrishack -- negative simulation time pasted for this sim frame
1184 if (sim_time_remaining > old_sim_time_remaining) {
1185 sim_time_remaining = old_sim_time_remaining;
1186 moved_time = 0.0;
1187 f_continue_sim = false;
1188 }
1189 }
1190
1191 // Get the collision speed as a linear interp.
1192 if (fate == HIT_OBJECT || fate == HIT_SPHERE_2_POLY_OBJECT || fate == HIT_TERRAIN || fate == HIT_WALL) {
1193 if (moved_time > 0.0001f) {
1194 obj->mtype.phys_info.velocity = (hit_info.hit_pnt - start_pos) / moved_time;
1195 } else {
1196 // mprintf(0, "Moved time is ZERO\n"); -- Add this back -- chrishack
1197 ASSERT(old_sim_time_remaining > 0.0f);
1198 obj->mtype.phys_info.velocity = start_vel;
1199 }
1200 } else {
1201 if (old_sim_time_remaining > 0.0) {
1202 obj->mtype.phys_info.velocity = obj->mtype.phys_info.velocity * (moved_time / old_sim_time_remaining) +
1203 start_vel * (sim_time_remaining / old_sim_time_remaining);
1204 }
1205 }
1206 }
1207
1208 bounced = 0;
1209
1210 // Collision response code
1211 switch (fate) {
1212 case HIT_NONE:
1213 if (obj->type == OBJ_WEAPON && (obj->mtype.phys_info.flags & (PF_GRAVITY | PF_WIND))) {
1214 if (obj->mtype.phys_info.velocity != Zero_vector)
1215 vm_VectorToMatrix(&obj->orient, &obj->mtype.phys_info.velocity, &obj->orient.uvec, NULL);
1216 }
1217 f_continue_sim = false;
1218 break;
1219 case HIT_WALL: // Finally unified, still needs some work, but it certainly better like this :)
1220 case HIT_TERRAIN: {
1221 bool f_volatile_lava; // either volatile or lava
1222 bool f_forcefield;
1223 float ff_bounce_factor = 2.0f;
1224
1225 // Check if volatile
1226 int tmap;
1227 if (!ROOMNUM_OUTSIDE(hit_info.hit_face_room[0])) // Make sure we've hit a wall, and not terrain
1228 {
1229 tmap = Rooms[hit_info.hit_face_room[0]].faces[hit_info.hit_face[0]].tmap;
1230 } else {
1231 tmap = Terrain_tex_seg[Terrain_seg[CELLNUM(hit_info.hit_face_room[0])].texseg_index].tex_index;
1232 }
1233 f_volatile_lava = (GameTextures[tmap].flags & (TF_VOLATILE + TF_LAVA)) != 0;
1234 f_forcefield = (GameTextures[tmap].flags & TF_FORCEFIELD) != 0;
1235
1236 if (f_forcefield) {
1237 int i;
1238
1239 for (i = 0; i < MAX_FORCE_FIELD_BOUNCE_TEXTURES; i++) {
1240 if (tmap == force_field_bounce_texture[i]) {
1241 ff_bounce_factor = force_field_bounce_multiplier[i];
1242 break;
1243 }
1244 }
1245 }
1246
1247 {
1248 vector moved_v;
1249 float hit_speed, wall_part;
1250 float hit_dot = 1.0;
1251
1252 float luke_test;
1253
1254 if (obj->type == OBJ_PLAYER) {
1255 luke_test = vm_GetMagnitude(&obj->mtype.phys_info.velocity);
1256 }
1257
1258 // Find hit speed
1259 moved_v = obj->pos - start_pos; // chrishack -- We already computed this!!!!!!!
1260 wall_part = moved_v * hit_info.hit_wallnorm[0];
1261
1262 if (obj->mtype.phys_info.hit_die_dot > 0.0) {
1263 vector m_normal = start_pos - obj->pos;
1264 vm_NormalizeVector(&m_normal);
1265 hit_dot = m_normal * hit_info.hit_wallnorm[0];
1266 }
1267
1268 hit_speed = -(wall_part / moved_time);
1269
1270 // Process wall hit
1271 bool hit_wall = collide_object_with_wall(obj, hit_speed, hit_info.hit_face_room[0], hit_info.hit_face[0],
1272 &hit_info.hit_face_pnt[0], &hit_info.hit_wallnorm[0], hit_dot);
1273
1274 if (!(obj->flags & OF_DEAD)) {
1275 // Check for not really hitting the wall (because it was glass & broke)
1276 if (!hit_wall) {
1277 f_continue_sim = true;
1278 break;
1279 }
1280
1281 if (obj->type == OBJ_WEAPON && ((obj->mtype.phys_info.flags & PF_BOUNCE) || f_forcefield))
1282 obj->mtype.phys_info.flags &= (~PF_NO_COLLIDE_PARENT);
1283
1284 // Slide object along wall
1285
1286 // We're constrained by wall, so subtract wall part from the velocity vector
1287 wall_part = hit_info.hit_wallnorm[0] * obj->mtype.phys_info.velocity;
1288
1289 if (!f_forcefield && !f_volatile_lava && (obj->mtype.phys_info.num_bounces <= 0) &&
1290 (obj->mtype.phys_info.flags & PF_STICK)) {
1291 obj->movement_type = MT_NONE;
1292
1293 if (!ROOMNUM_OUTSIDE(hit_info.hit_face_room[0])) {
1294 if (GameTextures[Rooms[hit_info.hit_face_room[0]].faces[hit_info.hit_face[0]].tmap].flags &
1295 (TF_VOLATILE | TF_LAVA)) {
1296 SetObjectDeadFlag(obj);
1297 }
1298 } else {
1299 if (GameTextures[Terrain_tex_seg[Terrain_seg[CELLNUM(hit_info.hit_room)].texseg_index].tex_index].flags &
1300 (TF_VOLATILE | TF_LAVA)) {
1301 SetObjectDeadFlag(obj);
1302 }
1303 }
1304
1305 if (!ROOMNUM_OUTSIDE(hit_info.hit_face_room[0]) &&
1306 (Rooms[hit_info.hit_face_room[0]].faces[hit_info.hit_face[0]].portal_num >= 0)) {
1307 obj->flags |= OF_STUCK_ON_PORTAL;
1308 obj->mtype.phys_info.stuck_room = hit_info.hit_face_room[0];
1309 obj->mtype.phys_info.stuck_portal =
1310 Rooms[hit_info.hit_face_room[0]].faces[hit_info.hit_face[0]].portal_num;
1311 }
1312
1313 vm_MakeZero(&obj->mtype.phys_info.velocity);
1314 obj->last_pos = init_pos;
1315 goto end_of_sim;
1316 } else if (f_volatile_lava || f_forcefield || (obj->mtype.phys_info.flags & PF_BOUNCE)) {
1317 wall_part *= 2.0; // Subtract out wall part twice to achieve bounce
1318 bounced = 1; // this object bounced
1319
1320 if (f_forcefield) {
1321 if (obj->type == OBJ_PLAYER)
1322 wall_part *= ff_bounce_factor; // player bounce twice as much -- chrishack try without this too.
1323 }
1324
1325 // New bounceness code
1326 if (!f_forcefield && (obj->mtype.phys_info.flags & PF_BOUNCE) &&
1327 (obj->mtype.phys_info.num_bounces != PHYSICS_UNLIMITED_BOUNCE)) {
1328 if (obj->mtype.phys_info.num_bounces == 0) {
1329 if (obj->type != OBJ_PLAYER && (obj->flags & OF_DYING)) {
1330 ASSERT((obj->control_type == CT_DYING) || (obj->control_type == CT_DYING_AND_AI));
1331 DestroyObject(obj, 50.0, obj->ctype.dying_info.death_flags);
1332 } else {
1333 if (obj->type != OBJ_PLAYER)
1334 SetObjectDeadFlag(obj);
1335 else
1336 mprintf(0, "Got a ship that was set for bounce!!! BAD!!!\n");
1337 }
1338 }
1339 obj->mtype.phys_info.num_bounces--;
1340 }
1341
1342 {
1343 float v_mag = vm_GetMagnitude(&obj->mtype.phys_info.velocity);
1344
1345 if (obj->type == OBJ_CLUTTER && hit_info.hit_wallnorm[0].y > .4 &&
1346 (obj->mtype.phys_info.velocity * hit_info.hit_wallnorm[0] > -2.0f)) {
1347
1348 if (obj->mtype.phys_info.flags & PF_GRAVITY) {
1349 f_turn_gravity_on = true;
1350 obj->mtype.phys_info.flags &= (~(PF_GRAVITY | PF_BOUNCE));
1351 obj->mtype.phys_info.drag *= 300.0f;
1352 }
1353
1354 if (v_mag < 1.8f) {
1355 obj->mtype.phys_info.velocity = Zero_vector;
1356 obj->mtype.phys_info.rotvel = Zero_vector;
1357 goto skip_sim;
1358 } else {
1359 goto slide_sim;
1360 }
1361 }
1362
1363 if (!f_forcefield && !(f_volatile_lava && obj->type == OBJ_PLAYER))
1364 bump_obj_against_fixed(obj, &hit_info.hit_face_pnt[0], &hit_info.hit_wallnorm[0]);
1365 else
1366 obj->mtype.phys_info.velocity += hit_info.hit_wallnorm[0] * (wall_part * -1.0f);
1367 }
1368
1369 } else {
1370 slide_sim:
1371
1372 float wall_force;
1373
1374 wall_force = total_force * hit_info.hit_wallnorm[0];
1375
1376 if (obj->type != OBJ_CLUTTER)
1377 total_force +=
1378 hit_info.hit_wallnorm[0] * (wall_force * -1.001f); // 1.001 so that we are not quite tangential
1379 else
1380 total_force +=
1381 hit_info.hit_wallnorm[0] * (wall_force * -1.0001f); // 1.001 so that we are not quite tangential
1382
1383 // Update velocity from wall hit.
1384 if (obj->type != OBJ_CLUTTER)
1385 obj->mtype.phys_info.velocity +=
1386 hit_info.hit_wallnorm[0] * (wall_part * -1.001f); // 1.001 so that we are not quite tangential
1387 else
1388 obj->mtype.phys_info.velocity +=
1389 hit_info.hit_wallnorm[0] * (wall_part * -1.0001f); // 1.001 so that we are not quite tangential
1390
1391 if ((obj->type == OBJ_ROBOT || obj->type == OBJ_PLAYER || (obj->type == OBJ_BUILDING && obj->ai_info)) &&
1392 sim_time_remaining == old_sim_time_remaining) {
1393 obj->mtype.phys_info.velocity += hit_info.hit_wallnorm[0];
1394 }
1395
1396 if (obj->type == OBJ_PLAYER) {
1397 float real_vel;
1398
1399 real_vel = vm_NormalizeVector(&obj->mtype.phys_info.velocity);
1400 obj->mtype.phys_info.velocity *= ((real_vel + luke_test) / 2);
1401 // obj->mtype.phys_info.velocity *= (luke_test);
1402 } else if (obj->type == OBJ_CLUTTER && !(obj->mtype.phys_info.flags & PF_LOCK_MASK)) {
1403 // Do rolling rotvel hack
1404 obj->mtype.phys_info.rotvel = Zero_vector;
1405
1406 if (obj->mtype.phys_info.velocity != Zero_vector) {
1407 vector axis;
1408 float speed = vm_GetMagnitude(&obj->mtype.phys_info.velocity);
1409
1410 vm_CrossProduct(&axis, &obj->mtype.phys_info.velocity, &hit_info.hit_wallnorm[0]);
1411 vm_NormalizeVector(&axis);
1412
1413 axis = axis * obj->orient;
1414
1415 float scale = speed * 65535.0f / (2.0f * PI * obj->size);
1416
1417 obj->mtype.phys_info.rotvel = axis * -scale;
1418 }
1419 }
1420 }
1421
1422 skip_sim:
1423
1424 // mprintf(0, "PHYSICS: Wall normal (%f, %f, %f)\n", XYZ(&hit_info.hit_wallnorm[0]));
1425 // mprintf(0, "PHYSICS: Bounded velocity (%f, %f, %f)\n", XYZ(&obj->mtype.phys_info.velocity));
1426
1427 // This only happens if the new velocity is set from hitting a forcefield.
1428 if (!(fabs(obj->mtype.phys_info.velocity.x) < MAX_OBJECT_VEL &&
1429 fabs(obj->mtype.phys_info.velocity.y) < MAX_OBJECT_VEL &&
1430 fabs(obj->mtype.phys_info.velocity.z) < MAX_OBJECT_VEL)) {
1431 float mag = vm_NormalizeVector(&obj->mtype.phys_info.velocity);
1432
1433 mprintf(0, "PHYSICS: Bashing vel for Obj %d of type %d with %f velocity", objnum, obj->type, mag);
1434 obj->mtype.phys_info.velocity *= MAX_OBJECT_VEL * 0.1f;
1435 }
1436
1437 // Weapons should face their new heading. This is so missiles are pointing in the correct direct.
1438 if (obj->type == OBJ_WEAPON && (bounced || (obj->mtype.phys_info.flags & (PF_GRAVITY | PF_WIND))))
1439 if (obj->mtype.phys_info.velocity != Zero_vector)
1440 vm_VectorToMatrix(&obj->orient, &obj->mtype.phys_info.velocity, &obj->orient.uvec, NULL);
1441 }
1442 }
1443 f_continue_sim = true;
1444
1445 } break;
1446
1447 case HIT_CEILING: {
1448 vector moved_v;
1449 float wall_part;
1450
1451 float luke_test;
1452
1453 if (obj->type == OBJ_PLAYER) {
1454 luke_test = vm_GetMagnitude(&obj->mtype.phys_info.velocity);
1455 }
1456
1457 // Find hit speed
1458 moved_v = obj->pos - start_pos; // chrishack -- We already computed this!!!!!!!
1459 wall_part = moved_v * hit_info.hit_wallnorm[0];
1460
1461 // We're constrained by wall, so subtract wall part from the velocity vector
1462 wall_part = hit_info.hit_wallnorm[0] * obj->mtype.phys_info.velocity;
1463
1464 if (obj->mtype.phys_info.flags & PF_BOUNCE) {
1465 wall_part *= 2.0; // Subtract out wall part twice to achieve bounce
1466
1467 obj->mtype.phys_info.velocity += hit_info.hit_wallnorm[0] * (wall_part * -1.0f);
1468
1469 if (obj->mtype.phys_info.coeff_restitution != 1.0f)
1470 obj->mtype.phys_info.velocity -=
1471 (obj->mtype.phys_info.velocity * (1.0f - obj->mtype.phys_info.coeff_restitution));
1472 } else {
1473 float wall_force;
1474
1475 wall_force = total_force * hit_info.hit_wallnorm[0];
1476 total_force += hit_info.hit_wallnorm[0] * (wall_force * -1.001); // 1.001 so that we are not quite tangential
1477
1478 // Update velocity from wall hit.
1479 obj->mtype.phys_info.velocity +=
1480 hit_info.hit_wallnorm[0] * (wall_part * -1.001); // 1.001 so that we are not quite tangential
1481 if (obj->type == OBJ_PLAYER) {
1482 float real_vel;
1483
1484 real_vel = vm_NormalizeVector(&obj->mtype.phys_info.velocity);
1485 obj->mtype.phys_info.velocity *= ((real_vel + luke_test) / 2.0f);
1486 // obj->mtype.phys_info.velocity *= (luke_test);
1487 }
1488 }
1489
1490 // This only happens if the new velocity is set from hitting a forcefield.
1491 ASSERT(fabs(obj->mtype.phys_info.velocity.x) < MAX_OBJECT_VEL &&
1492 fabs(obj->mtype.phys_info.velocity.y) < MAX_OBJECT_VEL &&
1493 fabs(obj->mtype.phys_info.velocity.z) < MAX_OBJECT_VEL);
1494
1495 // Weapons should face their new heading. This is so missiles are pointing in the correct direct.
1496 if (obj->type == OBJ_WEAPON && (bounced || (obj->mtype.phys_info.flags & (PF_GRAVITY | PF_WIND))))
1497 vm_VectorToMatrix(&obj->orient, &obj->mtype.phys_info.velocity, &obj->orient.uvec, NULL);
1498
1499 f_continue_sim = true;
1500 } break;
1501
1502 case HIT_OBJECT:
1503 case HIT_SPHERE_2_POLY_OBJECT: {
1504 vector old_force; // -- chrishack
1505
1506 // Mark the hit object so that on the following sim frames, in this game frame, ignore this object.
1507 ASSERT(hit_info.hit_object[0] != -1); // chrishack move this ASSERT fvi stuff above
1508
1509 // chrishack -- we should have this point from FVI!!!!!!!!!!!
1510 // Calculcate the hit point between the two objects.
1511 old_force = obj->mtype.phys_info.thrust;
1512
1513 obj->mtype.phys_info.thrust = total_force;
1514
1515 collide_two_objects(obj, &Objects[hit_info.hit_object[0]], &hit_info.hit_face_pnt[0], &hit_info.hit_wallnorm[0],
1516 &hit_info);
1517 if (obj->movement_type == MT_OBJ_LINKED)
1518 goto end_of_sim;
1519
1520 total_force = obj->mtype.phys_info.thrust;
1521
1522 obj->mtype.phys_info.thrust = old_force;
1523
1524 if ((obj->type != OBJ_POWERUP && Objects[hit_info.hit_object[0]].type != OBJ_POWERUP) &&
1525 (obj->type == OBJ_CLUTTER || obj->type == OBJ_ROBOT || obj->type == OBJ_PLAYER ||
1526 (obj->type == OBJ_BUILDING && obj->ai_info)) &&
1527 sim_time_remaining == old_sim_time_remaining &&
1528 (Objects[hit_info.hit_object[0]].movement_type == MT_NONE ||
1529 (Objects[hit_info.hit_object[0]].mtype.phys_info.velocity == Zero_vector &&
1530 (Objects[hit_info.hit_object[0]].mtype.phys_info.flags & PF_LOCK_MASK)))) {
1531 obj->mtype.phys_info.velocity += hit_info.hit_wallnorm[0];
1532 }
1533
1534 // Let object continue its movement if collide_two_objects does not mark it as dead
1535 if (!(obj->flags & OF_DEAD)) {
1536 if ((obj->mtype.phys_info.flags & PF_PERSISTENT) ||
1537 (Objects[hit_info.hit_object[0]].mtype.phys_info.flags & PF_PERSISTENT)) {
1538 if (n_ignore_objs < MAX_IGNORE_OBJS) {
1539 ignore_obj_list[n_ignore_objs++] = hit_info.hit_object[0];
1540 count--;
1541 if (count < -1)
1542 count = -1;
1543 }
1544
1545 if (obj->type == OBJ_WEAPON && (obj->mtype.phys_info.flags & PF_PERSISTENT))
1546 obj->ctype.laser_info.last_hit_handle = Objects[hit_info.hit_object[0]].handle;
1547
1548 if (Objects[hit_info.hit_object[0]].type == OBJ_WEAPON &&
1549 (Objects[hit_info.hit_object[0]].mtype.phys_info.flags & PF_PERSISTENT))
1550 Objects[hit_info.hit_object[0]].ctype.laser_info.last_hit_handle = obj->handle;
1551 } else if (Objects[hit_info.hit_object[0]].type == OBJ_POWERUP || obj->type == OBJ_POWERUP ||
1552 Objects[hit_info.hit_object[0]].type == OBJ_MARKER || obj->type == OBJ_MARKER) {
1553 if (n_ignore_objs < MAX_IGNORE_OBJS)
1554 ignore_obj_list[n_ignore_objs++] = hit_info.hit_object[0];
1555
1556 count--;
1557 if (count < -1)
1558 count = -1;
1559 }
1560
1561 f_continue_sim = true;
1562
1563 } else {
1564 f_continue_sim = false;
1565 }
1566 } break;
1567
1568 case HIT_BAD_P0:
1569 mprintf(0, "PHYSICS ERROR: Bad p0 in physics!!!\n");
1570 Int3(); // Unexpected collision type: start point not in specified segment.
1571 break;
1572
1573 default:
1574 mprintf(0, "PHYSICS ERROR: Unknown and unhandled hit type returned from FVI\n");
1575 Int3();
1576 break;
1577 }
1578
1579 // Increment the simulation loop counter
1580 count++;
1581
1582 } while ((f_continue_sim) && (!(obj->flags & OF_DEAD)) && (sim_time_remaining > 0.0f) && (count < sim_loop_limit));
1583
1584 // Special stuff should we do if we hit the simulation loop limit
1585 // If retry count is getting large, then we might be trying to do something bad.
1586 // NOTE: These numbers limit the max collisions an object can have in a single frame
1587 if (count >= sim_loop_limit) {
1588 if (obj->type == OBJ_PLAYER) {
1589 mprintf(0, "PHYSICS NOTE: Too many collisions for player!\n");
1590 obj->mtype.phys_info.velocity = Zero_vector;
1591 }
1592 }
1593
1594 end_of_sim:
1595
1596 if (f_turn_gravity_on) {
1597 obj->mtype.phys_info.flags |= (PF_GRAVITY | PF_BOUNCE);
1598 obj->mtype.phys_info.drag /= 300.0f;
1599 }
1600
1601 AttachUpdateSubObjects(obj);
1602
1603 ASSERT(std::isfinite(obj->mtype.phys_info.velocity.x)); // Caller wants to go to infinity! -- Not FVI's fault.
1604 ASSERT(std::isfinite(obj->mtype.phys_info.velocity.y)); // Caller wants to go to infinity! -- Not FVI's fault.
1605 ASSERT(std::isfinite(obj->mtype.phys_info.velocity.z)); // Caller wants to go to infinity! -- Not FVI's fault.
1606
1607 obj->last_pos = init_pos;
1608 return;
1609 }
1610
1611 int PhysCastWalkRay(object *obj, vector *p0, vector *p1, vector *hitpnt, int *start_roomnum = NULL,
1612 int *end_roomnum = NULL, vector *hit_normal = NULL) {
1613 int fate;
1614
1615 fvi_info hit_info;
1616 fvi_query fq;
1617
1618 fq.p0 = p0;
1619 fq.p1 = p1;
1620
1621 if (start_roomnum)
1622 fq.startroom = *start_roomnum;
1623 else
1624 fq.startroom = obj->roomnum;
1625
1626 fq.rad = 0.0f;
1627 fq.flags = FQ_CHECK_OBJS | FQ_IGNORE_POWERUPS | FQ_IGNORE_WEAPONS | FQ_IGNORE_MOVING_OBJECTS |
1628 FQ_IGNORE_NON_LIGHTMAP_OBJECTS;
1629 if (!end_roomnum)
1630 fq.flags |= FQ_NO_RELINK;
1631
1632 fq.thisobjnum = OBJNUM(obj);
1633 fq.ignore_obj_list = NULL;
1634
1635 fate = fvi_FindIntersection(&fq, &hit_info);
1636 if (end_roomnum)
1637 *end_roomnum = hit_info.hit_room;
1638 if (hit_normal)
1639 *hit_normal = hit_info.hit_wallnorm[0];
1640
1641 if (fate == HIT_WALL) {
1642 if (GameTextures[Rooms[hit_info.hit_face_room[0]].faces[hit_info.hit_face[0]].tmap].flags &
1643 (TF_VOLATILE | TF_FORCEFIELD | TF_LAVA)) {
1644 return HIT_NONE;
1645 }
1646 }
1647
1648 *hitpnt = hit_info.hit_pnt;
1649 return fate;
1650 }
1651
1652 void PhysCalPntOnCPntPlane(object *obj, vector *s_pnt, vector *d_pnt, float *dist) {
1653 vector tpnt = *s_pnt - obj->pos;
1654 *dist = obj->orient.uvec * tpnt;
1655
1656 *d_pnt = *s_pnt + ((-(*dist)) * obj->orient.uvec);
1657 }
1658
1659 bool PhysComputeWalkerPosOrient(object *obj, vector *pos, matrix *orient) {
1660 vector pnt[3];
1661 vector tpnt = obj->pos;
1662 matrix tmatrix = obj->orient;
1663 poly_model *pm = &Poly_models[obj->rtype.pobj_info.model_num];
1664 int num_gps = (pm->n_ground == 5) ? 3 : 1;
1665 int i;
1666
1667 bool f_ok = true;
1668
1669 obj->pos = *pos;
1670 obj->orient = *orient;
1671
1672 for (i = 0; i < num_gps; i++) {
1673 PhysCalcGround(&pnt[i], NULL, obj, i);
1674 }
1675
1676 obj->pos = tpnt;
1677 obj->orient = tmatrix;
1678
1679 if (num_gps == 3) {
1680 vector pp[3];
1681 float pdist[3];
1682 int proom[3];
1683 vector hp[3];
1684
1685 for (i = 0; i < 3; i++)
1686 PhysCalPntOnCPntPlane(obj, &pnt[i], &pp[i], &pdist[i]);
1687
1688 // Moves the points to valid locations in the mine and determines the points' room numbers
1689 for (i = 0; i < 3; i++) {
1690 vector x;
1691 vector norm;
1692
1693 int fate = PhysCastWalkRay(obj, &obj->pos, &pp[i], &x, NULL, &proom[i], &norm);
1694 if (fate != HIT_NONE) {
1695 if (norm * obj->orient.uvec < 0.4717f) {
1696 return false;
1697 }
1698
1699 pp[i] = x;
1700 }
1701 }
1702
1703 int num_ave = 0;
1704 vector ave_diff = Zero_vector;
1705
1706 for (i = 0; i < 3; i++) {
1707 int fate;
1708 vector foot_pnt = pp[i] + obj->orient.uvec * -(obj->size * 1.5f);
1709 vector norm;
1710
1711 fate = PhysCastWalkRay(obj, &pp[i], &foot_pnt, &hp[i], &proom[i], NULL, &norm);
1712
1713 if (fate != HIT_NONE) {
1714 if (norm * obj->orient.uvec < 0.4717f) {
1715 return false;
1716 }
1717
1718 ave_diff += hp[i] - pnt[i];
1719 num_ave++;
1720 } else {
1721 f_ok = false;
1722 }
1723 }
1724
1725 if (num_ave > 0) {
1726 ave_diff /= num_ave;
1727 } else
1728 ave_diff = Zero_vector;
1729
1730 vector uvec;
1731 vm_GetPerp(&uvec, &hp[0], &hp[1], &hp[2]);
1732 vm_NormalizeVector(&uvec);
1733
1734 // if(uvec.y < 0.0)
1735 // uvec *= -1.0f;
1736
1737 float dot = orient->fvec * uvec;
1738 vector fvec = orient->fvec;
1739 fvec -= (uvec * dot);
1740 vm_NormalizeVector(&fvec);
1741
1742 vm_VectorToMatrix(orient, &fvec, &uvec, NULL);
1743 *pos += ave_diff;
1744 } else { // Alignd to Y-axis
1745 int fate;
1746 vector hp;
1747 vector foot_pnt = obj->pos;
1748 foot_pnt.y -= obj->size * 3.0f;
1749
1750 fate = PhysCastWalkRay(obj, &obj->pos, &foot_pnt, &hp);
1751
1752 if (fate == HIT_NONE) {
1753 mprintf(0, "Walking object %d should be falling\n", OBJNUM(obj));
1754 // SetObjectDeadFlag(obj);
1755 f_ok = false;
1756 } else {
1757 float diff = hp.y - pnt[0].y;
1758
1759 pos->y += diff;
1760
1761 angvec a;
1762 vm_ExtractAnglesFromMatrix(&a, orient);
1763 vm_AnglesToMatrix(orient, 0.0f, a.h, 0.0f);
1764 }
1765 }
1766
1767 return f_ok;
1768 }
1769
1770 bool IsNodeValid(int cell, float min_vert_dot) {
1771 if (TerrainNormals[MAX_TERRAIN_LOD - 1][cell].normal1.y >= min_vert_dot &&
1772 TerrainNormals[MAX_TERRAIN_LOD - 1][cell].normal2.y >= min_vert_dot)
1773 return true;
1774 else
1775 return false;
1776 }
1777
1778 bool PhysValidateGroundPath(object *obj, vector *s, int sroom, vector *e, int eroom, float rad, float min_vert_dot) {
1779 if (ROOMNUM_OUTSIDE(sroom)) {
1780 int x1, x2, y1, y2, x, y, delta_y, delta_x, change_x, change_y, length, cur_node, error_term, i;
1781
1782 int new_x, new_y;
1783 int counter;
1784 int delta_ter_check = rad / TERRAIN_SIZE + 1;
1785
1786 // Determine the start end end nodes
1787 x1 = CELLNUM(sroom) % TERRAIN_WIDTH;
1788 y1 = CELLNUM(sroom) / TERRAIN_WIDTH;
1789
1790 x2 = CELLNUM(eroom) % TERRAIN_WIDTH;
1791 y2 = CELLNUM(eroom) / TERRAIN_WIDTH;
1792
1793 x = x1;
1794 y = y1;
1795
1796 // How many nodes did I move?
1797 delta_x = x2 - x1;
1798 delta_y = y2 - y1;
1799
1800 // Check the current node
1801 if (!IsNodeValid(CELLNUM(sroom), min_vert_dot)) {
1802 return false;
1803 }
1804
1805 if (delta_x == 0 && delta_y == 0)
1806 return true;
1807
1808 // check the end node
1809 cur_node = y1 * TERRAIN_DEPTH + x2;
1810 if (!IsNodeValid(cur_node, min_vert_dot)) {
1811 return false;
1812 }
1813
1814 // Do a Breshenham line algorithm
1815 if (delta_x < 0) {
1816 change_x = -1;
1817 delta_x = -delta_x;
1818 } else {
1819 change_x = 1;
1820 }
1821
1822 if (delta_y < 0) {
1823 change_y = -1;
1824 delta_y = -delta_y;
1825 } else {
1826 change_y = 1;
1827 }
1828
1829 error_term = 0;
1830 i = 1;
1831
1832 if (delta_x < delta_y) {
1833 length = delta_y + 1;
1834
1835 while (i < length) {
1836 y += change_y;
1837 error_term += delta_x;
1838
1839 if (error_term >= delta_y) {
1840 x += change_x;
1841 error_term -= delta_y;
1842 }
1843
1844 if (y >= TERRAIN_DEPTH || y < 0 || x < 0 || x >= TERRAIN_WIDTH) {
1845 return false;
1846 }
1847
1848 for (counter = -delta_ter_check; counter <= delta_ter_check; counter++) {
1849 new_x = x + counter;
1850
1851 if (new_x < 0) {
1852 counter = -x;
1853 new_x = 0;
1854 }
1855
1856 if (new_x >= TERRAIN_WIDTH)
1857 break;
1858
1859 // Check the current node for collisions -- chrishack -- This can be made iterative
1860 cur_node = y * TERRAIN_DEPTH + new_x;
1861 if (!IsNodeValid(cur_node, min_vert_dot)) {
1862 return false;
1863 }
1864 }
1865
1866 i++;
1867 }
1868
1869 } else {
1870 length = delta_x + 1;
1871
1872 while (i < length) {
1873 x += change_x;
1874 error_term += delta_y;
1875
1876 if (error_term >= delta_x) {
1877 y += change_y;
1878 error_term -= delta_x;
1879 }
1880
1881 if (y >= TERRAIN_DEPTH || y < 0 || x < 0 || x >= TERRAIN_WIDTH) {
1882 return false;
1883 }
1884
1885 for (counter = -delta_ter_check; counter <= delta_ter_check; counter++) {
1886 new_y = y + counter;
1887
1888 if (new_y < 0) {
1889 counter = -y;
1890 new_y = 0;
1891 }
1892 if (new_y >= TERRAIN_DEPTH)
1893 break;
1894
1895 // Check the current node for collisions -- chrishack -- This can be made iterative
1896 cur_node = new_y * TERRAIN_DEPTH + x;
1897 if (!IsNodeValid(cur_node, min_vert_dot)) {
1898 return false;
1899 }
1900 }
1901
1902 i++;
1903 }
1904 }
1905
1906 return true;
1907 } else {
1908 return true;
1909 }
1910 }
1911
1912 // -----------------------------------------------------------------------------------------------------------
1913 // Simulate a physics object for this frame
1914 void do_walking_sim(object *obj) {
1915 // Since we might not call FVI, set this here
1916 Fvi_num_recorded_faces = 0;
1917
1918 int n_ignore_objs = 0; // The number of ignored objects
1919 int ignore_obj_list[MAX_IGNORE_OBJS + 1]; // List of ignored objects
1920
1921 int fate; // Collision type for response code
1922 vector movement_vec; // Movement in this frame
1923 vector movement_pos; // End point of the movement
1924
1925 int count = 0; // Simulation loop counter
1926 int sim_loop_limit = (obj->type == OBJ_PLAYER) ? MAX_PLAYER_SIM_LOOPS : MAX_NON_PLAYER_SIM_LOOPS;
1927
1928 int objnum = OBJNUM(obj); // Simulated object's index
1929 fvi_info hit_info; // Hit information
1930 fvi_query fq; // Movement query
1931
1932 float sim_time_remaining = Frametime; // Amount of simulation time remaining (current iteration)
1933 float old_sim_time_remaining = Frametime; // Amount of simulation time remaining (previous iteration)
1934
1935 vector init_pos = obj->pos; // Initial position
1936 int init_room = obj->roomnum; // Initial room
1937
1938 vector start_pos = obj->pos; // Values at the start of current simulation loop
1939 vector start_vel = obj->mtype.phys_info.velocity;
1940 int start_room = obj->roomnum;
1941
1942 float moved_time; // How long objected moved before hit something
1943 physics_info *pi = &obj->mtype.phys_info; // Simulated object's physics information
1944
1945 int bounced = 0; // Did the object bounce?
1946
1947 vector total_force = Zero_vector; // Constant force acting on an object
1948
1949 bool f_continue_sim; // Should we run another simulation loop
1950 bool f_start_fvi_record = true; // Records the rooms that are passed thru
1951
1952 poly_model *pm = &Poly_models[obj->rtype.pobj_info.model_num];
1953
1954 // Assert conditions
1955 ASSERT(obj->type != OBJ_NONE);
1956 ASSERT(obj->movement_type == MT_WALKING);
1957 ASSERT(!(obj->mtype.phys_info.flags & PF_USES_THRUST) || obj->mtype.phys_info.drag != 0.0);
1958 ASSERT(std::isfinite(obj->mtype.phys_info.velocity.x)); // Caller wants to go to infinity! -- Not FVI's fault.
1959 ASSERT(std::isfinite(obj->mtype.phys_info.velocity.y)); // Caller wants to go to infinity! -- Not FVI's fault.
1960 ASSERT(std::isfinite(obj->mtype.phys_info.velocity.z)); // Caller wants to go to infinity! -- Not FVI's fault.
1961
1962 #ifdef _DEBUG
1963 if (!Game_do_walking_sim) {
1964 return;
1965 }
1966 #endif
1967
1968 // Instant bail cases
1969 if ((obj->flags & (OF_DEAD | OF_ATTACHED)) || (Frametime <= 0.0) || (obj->type == OBJ_DUMMY)) {
1970 return;
1971 }
1972
1973 obj->flags &= (~OF_STOPPED_THIS_FRAME);
1974
1975 // Do rotation velocity/accel stuff
1976 bool f_rotated = false;
1977
1978 if (!(fabs(pi->velocity.x) > .000001 || fabs(pi->velocity.y) > .000001 || fabs(pi->velocity.z) > .000001 ||
1979 fabs(pi->thrust.x) > .000001 || fabs(pi->thrust.y) > .000001 || fabs(pi->thrust.z) > .000001 ||
1980 fabs(pi->rotvel.x) > .000001 || fabs(pi->rotvel.y) > .000001 || fabs(pi->rotvel.z) > .000001 ||
1981 fabs(pi->rotthrust.x) > .000001 || fabs(pi->rotthrust.y) > .000001 || fabs(pi->rotthrust.z) > .000001 ||
1982 (obj->mtype.phys_info.flags & PF_GRAVITY) ||
1983 ((!ROOMNUM_OUTSIDE(obj->roomnum)) && Rooms[obj->roomnum].wind != Zero_vector) ||
1984 (obj->mtype.phys_info.flags & PF_WIGGLE) ||
1985 ((obj->ai_info != NULL) && ((obj->ai_info->flags & AIF_REPORT_NEW_ORIENT) != 0)))) {
1986 if ((obj->flags & OF_MOVED_THIS_FRAME)) {
1987 obj->flags &= (~OF_MOVED_THIS_FRAME);
1988 obj->flags |= OF_STOPPED_THIS_FRAME;
1989 }
1990
1991 return;
1992 }
1993
1994 if (obj->ai_info) {
1995 if (obj->ai_info->flags & AIF_REPORT_NEW_ORIENT) {
1996 if (pm->n_ground == 5) {
1997 vector pos = obj->pos;
1998 matrix orient = obj->orient;
1999
2000 if (PhysComputeWalkerPosOrient(obj, &pos, &orient) &&
2001 ((!obj->ai_info) || (orient.uvec * obj->orient.uvec > .7101 && orient.fvec * obj->orient.fvec > .7101 &&
2002 orient.rvec * obj->orient.rvec > .7101)))
2003 ObjSetPos(obj, &pos, obj->roomnum, &orient, false);
2004 else
2005 ObjSetOrient(obj, &obj->ai_info->saved_orient);
2006 }
2007 obj->ai_info->flags &= ~AIF_REPORT_NEW_ORIENT;
2008 }
2009 }
2010 obj->flags |= OF_MOVED_THIS_FRAME;
2011
2012 // This assumes that the thrust does not change within a frame. If it does, account for it
2013 // there... like missiles bouncing off a wall and changing heading. --chrishack
2014 if (obj->mtype.phys_info.drag != 0.0) {
2015 if (obj->mtype.phys_info.flags & PF_USES_THRUST) {
2016 total_force = obj->mtype.phys_info.thrust;
2017 }
2018 }
2019
2020 // mprintf(0, "PHYSICS: Current obj: %d, %9.2fx %9.2fy %9.2fz\n", OBJNUM(obj), XYZ(&obj->pos));
2021 // mprintf(2,1,0,"x %9.2f\ny %9.2f\nz %9.2f", XYZ(&obj->mtype.phys_info.velocity));
2022 // mprintf(0, "PHYSICS: Current velocity (%f, %f, %f)\n", XYZ(&obj->mtype.phys_info.velocity));
2023
2024 Physics_normal_counter++;
2025
2026 // Simulate movement until we are done (i.e. Frametime has passed or object is done moving)
2027 do {
2028 obj->mtype.phys_info.rotvel = Zero_vector;
2029
2030 #ifdef _DEBUG
2031 // This records the sim.
2032 sim_loop_record[count].phys_info = obj->mtype.phys_info;
2033 #endif
2034
2035 // Remove transient portions of the velocity
2036 vector t = (obj->mtype.phys_info.velocity * obj->orient.uvec) * obj->orient.uvec;
2037 obj->mtype.phys_info.velocity -= t;
2038
2039 // Initailly assume that this is the last sim cycle
2040 f_continue_sim = false;
2041
2042 // Determines forces
2043 if (count > 0) {
2044 total_force.x = total_force.y = total_force.z = 0.0;
2045 }
2046
2047 {
2048 movement_vec = pi->velocity * Frametime;
2049 movement_pos = obj->pos + movement_vec;
2050 vector footstep = movement_pos;
2051 int new_room;
2052
2053 if (ROOMNUM_OUTSIDE(obj->roomnum)) {
2054 new_room = GetTerrainRoomFromPos(&footstep);
2055 } else {
2056 new_room = obj->roomnum;
2057 }
2058
2059 bool f_step;
2060
2061 if (PhysValidateGroundPath(obj, &obj->pos, obj->roomnum, &footstep, new_room, obj->size, 0.6f)) {
2062 f_step = true;
2063
2064 // Determine endpos
2065 matrix norient;
2066 norient = obj->orient;
2067 if (PhysComputeWalkerPosOrient(obj, &footstep, &norient)) {
2068 if (norient.uvec * obj->orient.uvec > .7101 && norient.fvec * obj->orient.fvec > .7101 &&
2069 norient.rvec * obj->orient.rvec > .7101) {
2070 ObjSetOrient(obj, &norient);
2071
2072 movement_vec = footstep - obj->pos;
2073 movement_pos = footstep;
2074
2075 fq.p0 = &obj->pos;
2076 fq.startroom = obj->roomnum;
2077 fq.p1 = &footstep;
2078 fq.rad = obj->size;
2079 fq.thisobjnum = objnum;
2080 fq.ignore_obj_list = NULL;
2081 fq.flags = FQ_CHECK_OBJS | FQ_IGNORE_WEAPONS | FQ_IGNORE_POWERUPS | FQ_IGNORE_TERRAIN;
2082
2083 if (obj->mtype.phys_info.flags & PF_POINT_COLLIDE_WALLS) {
2084 fq.rad = 0.0f;
2085 } else if (!ROOMNUM_OUTSIDE(obj->roomnum)) {
2086 poly_model *pm = &Poly_models[obj->rtype.pobj_info.model_num];
2087 int num_gps = (pm->n_ground == 5) ? 3 : 1;
2088
2089 if (num_gps == 3)
2090 fq.rad *= 0.2f;
2091 else
2092 fq.rad *= 0.45f;
2093 }
2094
2095 if (obj->flags & OF_NO_OBJECT_COLLISIONS)
2096 fq.flags &= ~FQ_CHECK_OBJS;
2097
2098 fate = fvi_FindIntersection(&fq, &hit_info);
2099
2100 norient = obj->orient;
2101 if (PhysComputeWalkerPosOrient(obj, &hit_info.hit_pnt, &norient)) {
2102 ObjSetPos(obj, &hit_info.hit_pnt, hit_info.hit_room, &norient, false);
2103 } else {
2104 f_step = false;
2105 }
2106 } else {
2107 f_step = false;
2108 movement_vec = Zero_vector;
2109 }
2110 } else {
2111 f_step = false;
2112 movement_vec = Zero_vector;
2113 }
2114 } else {
2115 f_step = false;
2116 }
2117
2118 if (!f_step) {
2119 pi->velocity = Zero_vector;
2120 break;
2121 }
2122
2123 // If we are stationary, we are done :)
2124 if (fabs(movement_vec.x) < 0.0000001 && fabs(movement_vec.y) < 0.0000001 && fabs(movement_vec.z) < 0.0000001)
2125 break;
2126 }
2127
2128 Physics_walking_looping_counter++;
2129
2130 // Instant bails for collision response
2131 if (obj->type == OBJ_PLAYER &&
2132 ((fate == HIT_OUT_OF_TERRAIN_BOUNDS) ||
2133 (ROOMNUM_OUTSIDE(hit_info.hit_room) && GetTerrainCellFromPos(&hit_info.hit_pnt) == -1))) {
2134 ObjSetPos(obj, &init_pos, init_room, NULL, false);
2135 obj->last_pos = init_pos;
2136
2137 goto end_of_sim;
2138 } else if (fate == HIT_OUT_OF_TERRAIN_BOUNDS) {
2139 mprintf(0, "PHYSICS NOTE: Walker exited terrain\n");
2140 SetObjectDeadFlag(obj);
2141 obj->last_pos = init_pos;
2142
2143 goto end_of_sim;
2144 }
2145
2146 // Calulate amount of sim time left
2147 if (fate == HIT_NONE) {
2148 moved_time = old_sim_time_remaining = sim_time_remaining;
2149 sim_time_remaining = 0.0;
2150 f_continue_sim = false;
2151 } else {
2152 vector moved_vec_n;
2153 float attempted_dist, actual_dist;
2154
2155 // Save results of this simulation
2156 old_sim_time_remaining = sim_time_remaining;
2157 moved_vec_n = obj->pos - start_pos; // chrishack -- use this copy
2158 if (moved_vec_n != Zero_vector) {
2159 actual_dist = vm_NormalizeVector(&moved_vec_n);
2160 } else {
2161 actual_dist = 0.0f;
2162 }
2163
2164 // chrishack -- potentially bad -- outside to inside stuff
2165 // We where sitting in a wall -- invalid starting point
2166 // moved backwards
2167 if (fate == HIT_WALL && moved_vec_n * movement_vec < -0.000001 && actual_dist != 0.0) {
2168
2169 mprintf(0, "Obj %d Walked backwards!\n", OBJNUM(obj));
2170 /*
2171 mprintf(0, "PHYSICS NOTE: (%f, %f, %f) to (%f, %f, %f)\n",
2172 XYZ(&start_pos),
2173 XYZ(&obj->pos))); // don't change position or sim_time_remaining
2174 */
2175
2176 ObjSetPos(obj, &start_pos, start_room, NULL, false);
2177
2178 moved_time = 0.0;
2179 } else {
2180 // Compute more results of this simulation
2181 attempted_dist = vm_GetMagnitude(&movement_vec);
2182 sim_time_remaining = sim_time_remaining * ((attempted_dist - actual_dist) / attempted_dist);
2183 moved_time = old_sim_time_remaining - sim_time_remaining;
2184
2185 // chrishack -- we have no time left (plus some) This can happen if FVI corrects a near tangental
2186 // movement by adding the radius - thus, we move a little too far.
2187 if (sim_time_remaining < 0.0) {
2188 sim_time_remaining = 0.0;
2189 moved_time = old_sim_time_remaining;
2190 }
2191
2192 // chrishack -- negative simulation time pasted for this sim frame
2193 if (sim_time_remaining > old_sim_time_remaining) {
2194 mprintf(0,
2195 "PHYSICS WARNING: Bogus sim_time_remaining = %15.13f, old = %15.13f\nAttempted d = %15.13f, actual "
2196 "= %15.13f\n",
2197 sim_time_remaining, old_sim_time_remaining, attempted_dist, actual_dist);
2198 // Int3();
2199 sim_time_remaining = old_sim_time_remaining;
2200 moved_time = 0.0;
2201 f_continue_sim = false;
2202 }
2203 }
2204 }
2205
2206 // Get the collision speed as a linear interp.
2207 if (old_sim_time_remaining > 0.0) {
2208 obj->mtype.phys_info.velocity = obj->mtype.phys_info.velocity * (moved_time / old_sim_time_remaining) +
2209 start_vel * (sim_time_remaining / old_sim_time_remaining);
2210 }
2211
2212 // Collision response code
2213 switch (fate) {
2214 case HIT_NONE:
2215 f_continue_sim = false;
2216 break;
2217 case HIT_WALL: // Finally unified, still needs some work, but it certainly better like this :)
2218 case HIT_TERRAIN: {
2219 // if(obj->type != OBJ_CLUTTER)
2220 {
2221 vector moved_v;
2222 float hit_speed, wall_part;
2223 float hit_dot = 1.0;
2224
2225 float luke_test;
2226
2227 if (obj->type == OBJ_PLAYER || OBJ_CLUTTER) {
2228 luke_test = vm_GetMagnitude(&obj->mtype.phys_info.velocity);
2229 }
2230
2231 // Find hit speed
2232 moved_v = obj->pos - start_pos; // chrishack -- We already computed this!!!!!!!
2233 wall_part = moved_v * hit_info.hit_wallnorm[0];
2234
2235 if (obj->mtype.phys_info.hit_die_dot > 0.0) {
2236 vector m_normal = start_pos - obj->pos;
2237 vm_NormalizeVector(&m_normal);
2238 hit_dot = m_normal * hit_info.hit_wallnorm[0];
2239 }
2240
2241 hit_speed = -(wall_part / moved_time);
2242
2243 // We hit a wall (weapons cannot scrap)
2244 if ((wall_part != 0.0 && moved_time > 0.0 && (hit_speed > 0.0)) || obj->type == OBJ_WEAPON)
2245 collide_object_with_wall(obj, hit_speed, hit_info.hit_face_room[0], hit_info.hit_face[0],
2246 &hit_info.hit_face_pnt[0], &hit_info.hit_wallnorm[0], hit_dot);
2247 else // We hit a wall and are moving parallel to it
2248 collide_object_with_wall(obj, hit_speed, hit_info.hit_face_room[0], hit_info.hit_face[0],
2249 &hit_info.hit_face_pnt[0], &hit_info.hit_wallnorm[0], hit_dot);
2250
2251 if (!(obj->flags & OF_DEAD)) {
2252 bool f_forcefield = false; // bounce off a forcefield
2253
2254 if (obj->type == OBJ_WEAPON && (obj->mtype.phys_info.flags & PF_BOUNCE))
2255 obj->mtype.phys_info.flags &= (~PF_NO_COLLIDE_PARENT);
2256
2257 // Slide object along wall
2258
2259 // We're constrained by wall, so subtract wall part from the velocity vector
2260 wall_part = hit_info.hit_wallnorm[0] * obj->mtype.phys_info.velocity;
2261
2262 if ((obj->mtype.phys_info.num_bounces <= 0) && (obj->mtype.phys_info.flags & PF_STICK)) {
2263 obj->movement_type = MT_NONE;
2264 vm_MakeZero(&obj->mtype.phys_info.velocity);
2265 obj->last_pos = init_pos;
2266 goto end_of_sim;
2267 } else if (f_forcefield || (obj->mtype.phys_info.flags & PF_BOUNCE)) {
2268 wall_part *= 2.0; // Subtract out wall part twice to achieve bounce
2269
2270 if (f_forcefield) {
2271 if (obj->type == OBJ_PLAYER)
2272 wall_part *= 2; // player bounce twice as much -- chrishack try without this too.
2273 }
2274
2275 bounced = 1; // this object bounced
2276 // mprintf(0, "(%f, %f, %f) before bounce\n", XYZ(&obj->mtype.phys_info.velocity));
2277 obj->mtype.phys_info.velocity += hit_info.hit_wallnorm[0] * (wall_part * -1.0f);
2278
2279 if (obj->mtype.phys_info.coeff_restitution != 1.0f)
2280 obj->mtype.phys_info.velocity -=
2281 (obj->mtype.phys_info.velocity * (1.0f - obj->mtype.phys_info.coeff_restitution));
2282
2283 // mprintf(0, "(%f, %f, %f) after bounce\n", XYZ(&obj->mtype.phys_info.velocity));
2284 // mprintf(0, "p (%f, %f, %f) after bounce\n", XYZ(&obj->pos));
2285 } else {
2286 float wall_force;
2287
2288 wall_force = total_force * hit_info.hit_wallnorm[0];
2289 total_force +=
2290 hit_info.hit_wallnorm[0] * (wall_force * -1.001f); // 1.001 so that we are not quite tangential
2291
2292 // Update velocity from wall hit.
2293 obj->mtype.phys_info.velocity +=
2294 hit_info.hit_wallnorm[0] * (wall_part * -1.001f); // 1.001 so that we are not quite tangential
2295
2296 if ((obj->type == OBJ_ROBOT || obj->type == OBJ_PLAYER || OBJ_CLUTTER ||
2297 (obj->type == OBJ_BUILDING && obj->ai_info)) &&
2298 sim_time_remaining == old_sim_time_remaining) {
2299 obj->mtype.phys_info.velocity += hit_info.hit_wallnorm[0];
2300 }
2301
2302 if (obj->type == OBJ_PLAYER || OBJ_CLUTTER) {
2303 float real_vel;
2304
2305 real_vel = vm_NormalizeVector(&obj->mtype.phys_info.velocity);
2306 obj->mtype.phys_info.velocity *= ((real_vel + luke_test) / 2);
2307 }
2308 }
2309
2310 // mprintf(0, "PHYSICS: Wall normal (%f, %f, %f)\n", XYZ(&hit_info.hit_wallnorm[0]));
2311 // mprintf(0, "PHYSICS: Bounded velocity (%f, %f, %f)\n", XYZ(&obj->mtype.phys_info.velocity));
2312
2313 // This only happens if the new velocity is set from hitting a forcefield.
2314 if (!(fabs(obj->mtype.phys_info.velocity.x) < MAX_OBJECT_VEL &&
2315 fabs(obj->mtype.phys_info.velocity.y) < MAX_OBJECT_VEL &&
2316 fabs(obj->mtype.phys_info.velocity.z) < MAX_OBJECT_VEL)) {
2317 float mag = vm_NormalizeVector(&obj->mtype.phys_info.velocity);
2318
2319 mprintf(0, "PHYSICS: Bashing vel for Obj %d of type %d with %f velocity", objnum, obj->type, mag);
2320 obj->mtype.phys_info.velocity *= MAX_OBJECT_VEL * 0.1f;
2321 }
2322
2323 // Weapons should face their new heading. This is so missiles are pointing in the correct direct.
2324 if (obj->type == OBJ_WEAPON && (bounced || (obj->mtype.phys_info.flags & (PF_GRAVITY | PF_WIND))))
2325 if (obj->mtype.phys_info.velocity != Zero_vector)
2326 vm_VectorToMatrix(&obj->orient, &obj->mtype.phys_info.velocity, &obj->orient.uvec, NULL);
2327 }
2328 }
2329 f_continue_sim = true;
2330
2331 } break;
2332
2333 case HIT_OBJECT:
2334 case HIT_SPHERE_2_POLY_OBJECT: {
2335 vector old_force; // -- chrishack
2336
2337 // Mark the hit object so that on the following sim frames, in this game frame, ignore this object.
2338 ASSERT(hit_info.hit_object[0] != -1); // chrishack move this ASSERT fvi stuff above
2339
2340 // chrishack -- we should have this point from FVI!!!!!!!!!!!
2341 // Calculcate the hit point between the two objects.
2342 old_force = obj->mtype.phys_info.thrust;
2343
2344 obj->mtype.phys_info.thrust = total_force;
2345
2346 // hit_info.hit_wallnorm[0] *= -1.0f;
2347
2348 collide_two_objects(obj, &Objects[hit_info.hit_object[0]], &hit_info.hit_face_pnt[0], &hit_info.hit_wallnorm[0],
2349 &hit_info);
2350 if (obj->movement_type == MT_OBJ_LINKED)
2351 goto end_of_sim;
2352
2353 total_force = obj->mtype.phys_info.thrust;
2354
2355 obj->mtype.phys_info.thrust = old_force;
2356
2357 if ((obj->type == OBJ_ROBOT || obj->type == OBJ_PLAYER || OBJ_CLUTTER ||
2358 (obj->type == OBJ_BUILDING && obj->ai_info)) &&
2359 sim_time_remaining == old_sim_time_remaining && Objects[hit_info.hit_object[0]].movement_type == MT_NONE) {
2360 obj->mtype.phys_info.velocity += hit_info.hit_wallnorm[0];
2361 }
2362
2363 // mprintf(0, "OBJ %d t %d hit %d\n", OBJNUM(obj), obj->type, hit_info.hit_object[0]);
2364
2365 // Let object continue its movement if collide_two_objects does not mark it as dead
2366 if (!(obj->flags & OF_DEAD)) {
2367 if ((obj->mtype.phys_info.flags & PF_PERSISTENT) ||
2368 (Objects[hit_info.hit_object[0]].mtype.phys_info.flags & PF_PERSISTENT)) {
2369 if (n_ignore_objs < MAX_IGNORE_OBJS)
2370 ignore_obj_list[n_ignore_objs++] = hit_info.hit_object[0];
2371 } else if (Objects[hit_info.hit_object[0]].type == OBJ_POWERUP || obj->type == OBJ_POWERUP ||
2372 Objects[hit_info.hit_object[0]].type == OBJ_MARKER || obj->type == OBJ_MARKER) {
2373 if (n_ignore_objs < MAX_IGNORE_OBJS)
2374 ignore_obj_list[n_ignore_objs++] = hit_info.hit_object[0];
2375
2376 count--;
2377 if (count < -1)
2378 count = -1;
2379 }
2380
2381 f_continue_sim = true;
2382 } else {
2383 f_continue_sim = false;
2384 }
2385 } break;
2386
2387 case HIT_BAD_P0:
2388 mprintf(0, "PHYSICS ERROR: Bad p0 in physics!!!\n");
2389 Int3(); // Unexpected collision type: start point not in specified segment.
2390 break;
2391
2392 default:
2393 mprintf(0, "PHYSICS ERROR: Unknown and unhandled hit type returned from FVI\n");
2394 Int3();
2395 break;
2396 }
2397
2398 // Increment the simulation loop counter
2399 count++;
2400
2401 } while ((f_continue_sim) && (!(obj->flags & OF_DEAD)) && (sim_time_remaining > 0.0f) && (count < sim_loop_limit));
2402
2403 // Special stuff should we do if we hit the simulation loop limit
2404 // If retry count is getting large, then we might be trying to do something bad.
2405 // NOTE: These numbers limit the max collisions an object can have in a single frame
2406 if (count >= sim_loop_limit) {
2407 if (obj->type == OBJ_PLAYER) {
2408 mprintf(0, "PHYSICS NOTE: Too many collisions for player!\n");
2409 }
2410 }
2411
2412 end_of_sim:
2413 AttachUpdateSubObjects(obj);
2414
2415 ASSERT(std::isfinite(obj->mtype.phys_info.velocity.x)); // Caller wants to go to infinity! -- Not FVI's fault.
2416 ASSERT(std::isfinite(obj->mtype.phys_info.velocity.y)); // Caller wants to go to infinity! -- Not FVI's fault.
2417 ASSERT(std::isfinite(obj->mtype.phys_info.velocity.z)); // Caller wants to go to infinity! -- Not FVI's fault.
2418
2419 obj->last_pos = init_pos;
2420 return;
2421 }
2422
2423 // -----------------------------------------------------------------------------------------------------------
2424 // Simulate a physics object for this frame
2425 void do_vis_physics_sim(vis_effect *vis) {
2426 if (Frametime <= 0.0f)
2427 return;
2428
2429 vector total_force;
2430 vector old_pos = vis->pos;
2431
2432 #ifdef _DEBUG
2433 if (!Game_do_vis_sim) {
2434 return;
2435 }
2436 #endif
2437
2438 if (vis->flags & VF_DEAD) {
2439 return;
2440 }
2441
2442 if (fabsf(vis->velocity.x) < 0.000001f && fabsf(vis->velocity.y) < 0.000001f && fabsf(vis->velocity.z) < 0.000001f &&
2443 !(vis->phys_flags & PF_GRAVITY)) {
2444 return;
2445 }
2446
2447 // Determine velocity
2448 if (vis->phys_flags & PF_FIXED_VELOCITY) {
2449 vis->pos += vis->velocity * Frametime;
2450 } else {
2451 if (vis->mass > 0.0f && vis->drag > 0.0f) {
2452 // If the object is effected by gravity (normal, lighter than air, and anti-gravity)
2453 if (vis->phys_flags & PF_GRAVITY) {
2454 total_force.x = total_force.z = 0.0f;
2455 total_force.y = Gravity_strength * vis->mass;
2456 } else if (vis->phys_flags & PF_REVERSE_GRAVITY) {
2457 total_force.x = total_force.z = 0.0f;
2458 total_force.y = -Gravity_strength * vis->mass;
2459 } else {
2460 total_force.x = total_force.y = total_force.z = 0.0f;
2461 }
2462
2463 // Note: The math here is done in 64bit floats because we will run into precision problems with 32bit
2464 const double forceOverDrag[3] = {total_force.x / vis->drag, total_force.y / vis->drag, total_force.z / vis->drag};
2465 const double frameTime = Frametime;
2466 const double massOverDrag = vis->mass / vis->drag;
2467 const double dragOverMass = vis->drag / vis->mass;
2468 const double expDoMFt = exp(-dragOverMass * frameTime);
2469
2470 double visNewPos[3];
2471 visNewPos[0] = double(vis->pos.x) + forceOverDrag[0] * frameTime +
2472 massOverDrag * (double(vis->velocity.x) - forceOverDrag[0]) * (1.0 - expDoMFt);
2473 visNewPos[1] = double(vis->pos.y) + forceOverDrag[1] * frameTime +
2474 massOverDrag * (double(vis->velocity.y) - forceOverDrag[1]) * (1.0 - expDoMFt);
2475 visNewPos[2] = double(vis->pos.z) + forceOverDrag[2] * frameTime +
2476 massOverDrag * (double(vis->velocity.z) - forceOverDrag[2]) * (1.0 - expDoMFt);
2477
2478 double visNewVel[3];
2479 visNewVel[0] = (double(vis->velocity.x) - forceOverDrag[0]) * expDoMFt + forceOverDrag[0];
2480 visNewVel[1] = (double(vis->velocity.y) - forceOverDrag[1]) * expDoMFt + forceOverDrag[1];
2481 visNewVel[2] = (double(vis->velocity.z) - forceOverDrag[2]) * expDoMFt + forceOverDrag[2];
2482
2483 vis->pos.x = float(visNewPos[0]);
2484 vis->pos.y = float(visNewPos[1]);
2485 vis->pos.z = float(visNewPos[2]);
2486 vis->velocity.x = float(visNewVel[0]);
2487 vis->velocity.y = float(visNewVel[1]);
2488 vis->velocity.z = float(visNewVel[2]);
2489 } else {
2490 vector delta_velocity = {0.0f, 0.0f, 0.0f};
2491
2492 if (vis->phys_flags & PF_GRAVITY) {
2493 delta_velocity.y += Gravity_strength * Frametime;
2494 } else if (vis->phys_flags & PF_REVERSE_GRAVITY) {
2495 delta_velocity.y -= Gravity_strength * Frametime;
2496 }
2497
2498 vis->pos += (vis->velocity * Frametime) + (0.5f * delta_velocity * Frametime);
2499 vis->velocity += delta_velocity;
2500 }
2501 }
2502
2503 if (vis->phys_flags & PF_NO_COLLIDE) {
2504 return;
2505 }
2506
2507 Physics_vis_counter++;
2508
2509 // Do FVI call to check for death & update room
2510 fvi_query fq;
2511 fvi_info hit_info;
2512 int fate;
2513
2514 if (ROOMNUM_OUTSIDE(vis->roomnum)) {
2515 ASSERT(CELLNUM(vis->roomnum) <= TERRAIN_WIDTH * TERRAIN_DEPTH);
2516 ASSERT(CELLNUM(vis->roomnum) >= 0);
2517 } else {
2518 ASSERT(vis->roomnum >= 0 && vis->roomnum <= Highest_room_index && Rooms[vis->roomnum].used);
2519 }
2520
2521 fq.p0 = &old_pos;
2522 fq.startroom = vis->roomnum;
2523 fq.p1 = &vis->pos;
2524 fq.rad = 0.0f;
2525 fq.thisobjnum = -1;
2526 fq.ignore_obj_list = NULL;
2527 fq.flags = FQ_CHECK_OBJS | FQ_ONLY_DOOR_OBJ | FQ_IGNORE_WALLS;
2528
2529 fate = fvi_FindIntersection(&fq, &hit_info);
2530
2531 if (fate == HIT_NONE && hit_info.hit_room != -1) {
2532 if (hit_info.hit_room != vis->roomnum) {
2533 VisEffectRelink(vis - VisEffects, hit_info.hit_room);
2534 }
2535
2536 if (ROOMNUM_OUTSIDE(hit_info.hit_room)) {
2537 ASSERT(CELLNUM(hit_info.hit_room) <= TERRAIN_WIDTH * TERRAIN_DEPTH);
2538 ASSERT(CELLNUM(hit_info.hit_room) >= 0);
2539 } else {
2540 ASSERT(hit_info.hit_room >= 0 && hit_info.hit_room <= Highest_room_index && Rooms[hit_info.hit_room].used);
2541 }
2542 } else {
2543 VisEffectDelete(vis - VisEffects);
2544 }
2545 }
2546
2547 void phys_apply_force(object *obj, vector *force_vec, int16_t weapon_index) {
2548 if (obj->mtype.phys_info.mass == 0.0)
2549 return;
2550
2551 if (obj->movement_type != MT_PHYSICS && obj->movement_type != MT_WALKING)
2552 return;
2553
2554 if (obj->mtype.phys_info.flags & PF_LOCK_MASK) // Not done!
2555 return;
2556
2557 if ((Game_mode & GM_MULTI) &&
2558 ((obj->type == OBJ_PLAYER && obj->id != Player_num) ||
2559 ((obj->type != OBJ_PLAYER && obj->type != OBJ_POWERUP) && Netgame.local_role != LR_SERVER)))
2560 return;
2561
2562 // Do Force Feedback for the hit
2563 if (obj->id == Player_num) {
2564 // we need to convert collision_normal from world space to local space
2565 vector local_norm;
2566 float scale = 0, magnitude = 0;
2567
2568 // compute how much force to do
2569 magnitude = vm_GetMagnitude(force_vec);
2570 if (magnitude) {
2571 scale = magnitude / 2500.0f;
2572 if (scale < 0.0f)
2573 scale = 0.0f;
2574 if (scale > 1.0f)
2575 scale = 1.0f;
2576
2577 vm_MatrixMulVector(&local_norm, force_vec, &obj->orient);
2578 local_norm *= -1.0f;
2579
2580 ForceEffectsPlay(FORCE_TEST_FORCE, &scale, &local_norm);
2581 }
2582
2583 if (weapon_index != -1) {
2584 mprintf(0, "Force from weapon\n");
2585 }
2586
2587 // mprintf(0,"Force: Magnitude = %f Scale = %1.3f\n",magnitude,scale);
2588 }
2589 //------------------------------
2590
2591 // Add in acceleration due to force
2592 obj->mtype.phys_info.velocity += (*force_vec / obj->mtype.phys_info.mass);
2593 }
2594
2595 void phys_apply_rot(object *obj, vector *force_vec) {}