Debian Codegraph @

physics/newstyle_fi.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 "findintersection.h"
20 #include "game.h"
21 #include "polymodel.h"
22 #include "pserror.h"
23 #include "vecmat.h"
24
25 #ifndef NED_PHYSICS
26 #include "multi.h"
27 #endif
28
29 extern matrix View_matrix;
30 extern vector View_position;
31
32 static vector Original_pos;
33 static matrix Original_orient;
34
35 static vector fvi_move_fvec;
36 static vector fvi_move_uvec;
37 static bool fvi_do_orient;
38
39 static bool Fvi_f_normal;
40
41 #define MAX_INSTANCE_DEPTH 30
42
43 struct instance_context {
44 matrix m;
45 vector p;
46 vector p0;
47 vector p1;
48 vector fvec;
49 vector uvec;
50 };
51 static struct instance_context instance_stack[MAX_INSTANCE_DEPTH];
52
53 static int instance_depth = 0;
54
55 static inline void ns_compute_movement_AABB();
56 static inline bool ns_movement_manual_AABB(vector *min_xyz, vector *max_xyz);
57 static void CollideSubmodelFacesUnsorted(poly_model *pm, bsp_info *sm);
58
59 /// instance at specified point with specified orientation.
60 /// if matrix==NULL, don't modify matrix. This will be like doing an offset.
61 static void newstyle_StartInstanceMatrix(vector *pos, matrix *orient);
62 /// instance at specified point with specified orientation.
63 /// if angles==NULL, don't modify matrix. This will be like doing an offset.
64 static void newstyle_StartInstanceAngles(vector *pos, angvec *angles);
65
66 /// pops the old context
67 static void newstyle_DoneInstance();
68 static void CollideSubmodel(poly_model *pm, bsp_info *sm, uint32_t f_render_sub);
69 static void CollidePolygonModel(vector *pos, matrix *orient, int model_num, float *normalized_time,
70 uint32_t f_render_sub);
71
72 static void BuildModelAngleMatrix(matrix *mat, angle ang, vector *axis) {
73 float x, y, z;
74 float s, c, t;
75
76 x = axis->x;
77 y = axis->y;
78 z = axis->z;
79
80 s = (float)FixSin(ang);
81 c = (float)FixCos(ang);
82 t = 1.0f - c;
83
84 mat->rvec.x = t * x * x + c;
85 mat->rvec.y = t * x * y + s * z;
86 mat->rvec.z = t * x * z - s * y;
87
88 mat->uvec.x = t * x * y - s * z;
89 mat->uvec.y = t * y * y + c;
90 mat->uvec.z = t * y * z + s * x;
91
92 mat->fvec.x = t * x * z + s * y;
93 mat->fvec.y = t * y * z - s * x;
94 mat->fvec.z = t * z * z + c;
95 }
96
97 float fvi_hit_param;
98 bool fvi_check_param;
99
100 static vector ns_min_xyz;
101 static vector ns_max_xyz;
102
103 inline void ns_compute_movement_AABB() {
104 vector delta_movement = *fvi_query_ptr->p1 - *fvi_query_ptr->p0;
105 vector offset_vec;
106
107 offset_vec.x = fvi_query_ptr->rad;
108 offset_vec.y = fvi_query_ptr->rad;
109 offset_vec.z = fvi_query_ptr->rad;
110
111 ns_min_xyz = ns_max_xyz = *fvi_query_ptr->p0;
112
113 ns_min_xyz -= offset_vec;
114 ns_max_xyz += offset_vec;
115
116 if (delta_movement.x > 0.0f)
117 ns_max_xyz.x += delta_movement.x;
118 else
119 ns_min_xyz.x += delta_movement.x;
120
121 if (delta_movement.y > 0.0f)
122 ns_max_xyz.y += delta_movement.y;
123 else
124 ns_min_xyz.y += delta_movement.y;
125
126 if (delta_movement.z > 0.0f)
127 ns_max_xyz.z += delta_movement.z;
128 else
129 ns_min_xyz.z += delta_movement.z;
130 }
131
132 inline bool ns_movement_manual_AABB(vector *min_xyz, vector *max_xyz) {
133 bool overlap = true;
134
135 if (max_xyz->y < ns_min_xyz.y || ns_max_xyz.y < min_xyz->y || max_xyz->x < ns_min_xyz.x ||
136 ns_max_xyz.x < min_xyz->x || max_xyz->z < ns_min_xyz.z || ns_max_xyz.z < min_xyz->z)
137 overlap = false;
138
139 return overlap;
140 }
141
142 // collide with a submodel
143 // Parameters: nv - the number of verts in the poly
144 // pointlist - a pointer to a list of pointers to points
145 // bm - the bitmap handle if texturing. ignored if flat shading
146
147 static void CollideSubmodelFacesUnsorted(poly_model *pm, bsp_info *sm) {
148 int i;
149 int j;
150 vector colp;
151 vector newp;
152 float col_dist; // distance to hit point
153 vector *vertex_list[32];
154 vector wall_norm;
155 int face_hit_type;
156
157 // (For this reference frame)
158 ns_compute_movement_AABB();
159
160 if (ns_movement_manual_AABB(&sm->min, &sm->max)) {
161
162 for (i = 0; i < sm->num_faces; i++) {
163 if (ns_movement_manual_AABB(&sm->face_min[i], &sm->face_max[i])) {
164 polyface *fp = &sm->faces[i];
165
166 if ((GameTextures[pm->textures[fp->texnum]].flags & (TF_FLY_THRU | TF_PASS_THRU)) == 0) {
167 ASSERT(fp->nverts <= 32);
168
169 for (j = 0; j < fp->nverts; j++) {
170 vertex_list[j] = &sm->verts[fp->vertnums[j]];
171 }
172
173 face_hit_type = check_line_to_face(&newp, &colp, &col_dist, &wall_norm, fvi_query_ptr->p0, fvi_query_ptr->p1,
174 &fp->normal, vertex_list, fp->nverts, fvi_query_ptr->rad);
175 if ((fvi_query_ptr->flags & FQ_OBJ_BACKFACE) && (!face_hit_type)) {
176 vector face_normal = fp->normal;
177 int count;
178
179 face_normal *= -1.0f;
180 for (count = 0; count < fp->nverts; count++) {
181 vertex_list[fp->nverts - count - 1] = &sm->verts[fp->vertnums[count]];
182 }
183
184 face_hit_type =
185 check_line_to_face(&newp, &colp, &col_dist, &wall_norm, fvi_query_ptr->p0, fvi_query_ptr->p1,
186 &face_normal, vertex_list, fp->nverts, fvi_query_ptr->rad);
187 }
188
189 if (face_hit_type) {
190 if (col_dist < fvi_collision_dist) {
191 vector x;
192
193 fvi_check_param = true;
194 x = *fvi_query_ptr->p1 - *fvi_query_ptr->p0;
195 vm_NormalizeVector(&x);
196
197 fvi_hit_param = (newp - *fvi_query_ptr->p0) * x;
198
199 if (!(fvi_hit_param > -10000000.0 && fvi_hit_param < 10000000.0)) {
200 mprintf(0, "FVI Warning: fvi_hit_param seems yucky!\n");
201 }
202
203 fvi_collision_dist = col_dist;
204 fvi_hit_data_ptr->num_hits = 1;
205
206 fvi_hit_data_ptr->hit_object[0] = fvi_curobj;
207 fvi_hit_data_ptr->hit_subobject[0] = sm - pm->submodel;
208 fvi_hit_data_ptr->hit_type[0] = HIT_SPHERE_2_POLY_OBJECT;
209 fvi_hit_data_ptr->hit_wallnorm[0] = wall_norm;
210 fvi_hit_data_ptr->hit_face_pnt[0] = colp;
211 fvi_hit_data_ptr->hit_face[0] = i;
212
213 Fvi_f_normal = true;
214
215 if (fvi_do_orient) {
216 fvi_hit_data_ptr->hit_subobj_fvec = fvi_move_fvec;
217 fvi_hit_data_ptr->hit_subobj_uvec = fvi_move_uvec;
218 fvi_hit_data_ptr->hit_subobj_pos = newp;
219 }
220 }
221 }
222 }
223 }
224 }
225 }
226 }
227
228 // instance at specified point with specified orientation
229 // if matrix==NULL, don't modify matrix. This will be like doing an offset
230 void newstyle_StartInstanceMatrix(vector *pos, matrix *orient) {
231 vector tempv, temp0, temp1;
232 matrix tempm, tempm2;
233
234 ASSERT(instance_depth < MAX_INSTANCE_DEPTH);
235
236 instance_stack[instance_depth].m = View_matrix;
237 instance_stack[instance_depth].p = View_position;
238 instance_stack[instance_depth].p0 = *fvi_query_ptr->p0;
239 instance_stack[instance_depth].p1 = *fvi_query_ptr->p1;
240 if (fvi_do_orient) {
241 instance_stack[instance_depth].fvec = fvi_move_fvec;
242 instance_stack[instance_depth].uvec = fvi_move_uvec;
243 }
244 instance_depth++;
245
246 // step 1: subtract object position from view position
247
248 tempv = View_position - *pos;
249 temp0 = *fvi_query_ptr->p0 - *pos;
250 temp1 = *fvi_query_ptr->p1 - *pos;
251
252 if (orient) {
253 // step 2: rotate view vector through object matrix
254
255 View_position = tempv * *orient;
256 *fvi_query_ptr->p0 = temp0 * *orient;
257 *fvi_query_ptr->p1 = temp1 * *orient;
258
259 if (fvi_do_orient) {
260 fvi_move_fvec = fvi_move_fvec * *orient;
261 fvi_move_uvec = fvi_move_uvec * *orient;
262 }
263
264 // step 3: rotate object matrix through view_matrix (vm = ob * vm)
265
266 tempm2 = ~*orient;
267
268 tempm = tempm2 * View_matrix;
269
270 View_matrix = tempm;
271 }
272 }
273
274 // instance at specified point with specified orientation
275 // if angles==NULL, don't modify matrix. This will be like doing an offset
276 static void newstyle_StartInstanceAngles(vector *pos, angvec *angles) {
277 matrix tm;
278
279 if (angles == nullptr) {
280 newstyle_StartInstanceMatrix(pos, nullptr);
281 return;
282 }
283
284 vm_AnglesToMatrix(&tm, angles->p, angles->h, angles->b);
285
286 newstyle_StartInstanceMatrix(pos, &tm);
287 }
288
289 // pops the old context
290 static void newstyle_DoneInstance() {
291 instance_depth--;
292
293 ASSERT(instance_depth >= 0);
294
295 View_position = instance_stack[instance_depth].p;
296 View_matrix = instance_stack[instance_depth].m;
297
298 *fvi_query_ptr->p0 = instance_stack[instance_depth].p0;
299 *fvi_query_ptr->p1 = instance_stack[instance_depth].p1;
300 if (fvi_do_orient) {
301 fvi_move_fvec = instance_stack[instance_depth].fvec;
302 fvi_move_uvec = instance_stack[instance_depth].uvec;
303 }
304 }
305
306 void CollideSubmodel(poly_model *pm, bsp_info *sm, uint32_t f_render_sub) {
307 // Don't collide with door housings (That is the 'room' portion of the door)
308 if ((sm->flags & SOF_SHELL) || (sm->flags & SOF_FRONTFACE))
309 return;
310
311 StartPolyModelPosInstance(&sm->mod_pos);
312 vector temp_vec = sm->mod_pos + sm->offset;
313 newstyle_StartInstanceAngles(&temp_vec, &sm->angs);
314
315 // Check my bit to see if I get collided with. :)
316 if (f_render_sub & (0x00000001 << (sm - pm->submodel)))
317 CollideSubmodelFacesUnsorted(pm, sm);
318
319 for (int i = 0; i < sm->num_children; i++) {
320 CollideSubmodel(pm, &pm->submodel[sm->children[i]], f_render_sub);
321 }
322
323 newstyle_DoneInstance();
324 DonePolyModelPosInstance();
325 }
326
327 void CollidePolygonModel(vector *pos, matrix *orient, int model_num, float *normalized_time, uint32_t f_render_sub) {
328 poly_model *po;
329
330 ASSERT(Poly_models[model_num].used);
331 ASSERT(Poly_models[model_num].new_style);
332
333 fvi_check_param = false;
334
335 po = &Poly_models[model_num];
336
337 newstyle_StartInstanceMatrix(pos, orient);
338
339 SetModelAnglesAndPos(po, normalized_time);
340
341 for (int i = 0; i < po->n_models; i++) {
342 bsp_info *sm = &po->submodel[i];
343 if (sm->parent == -1)
344 CollideSubmodel(po, sm, f_render_sub);
345 }
346
347 newstyle_DoneInstance();
348 }
349
350 #define MULTI_ADD_SPHERE_MIN 1.4f
351 #define MULTI_ADD_SPHERE_MAX 2.5f
352
353 bool PolyCollideObject(object *obj) {
354 #ifndef NED_PHYSICS
355 float normalized_time[MAX_SUBOBJECTS];
356 #endif
357 vector temp_pos = Original_pos = View_position;
358 matrix temp_orient = Original_orient = View_matrix;
359 bool f_use_big_sphere = false;
360 float addition;
361
362 ASSERT(obj >= Objects && obj <= &Objects[Highest_object_index]);
363
364 #ifndef NED_PHYSICS
365 if ((Game_mode & GM_MULTI) && !(Netgame.flags & NF_USE_ACC_WEAP) && Objects[fvi_moveobj].type == OBJ_WEAPON &&
366 obj->type == OBJ_PLAYER)
367 f_use_big_sphere = true;
368 #endif
369
370 fvi_do_orient = (Objects[fvi_moveobj].type == OBJ_WEAPON);
371
372 #ifndef NED_PHYSICS
373 if (f_use_big_sphere) {
374 addition = fvi_query_ptr->rad;
375
376 if (addition < MULTI_ADD_SPHERE_MIN) {
377 addition = MULTI_ADD_SPHERE_MIN;
378 if (Objects[fvi_moveobj].mtype.phys_info.flags & PF_NEVER_USE_BIG_SPHERE)
379 addition /= 2;
380 } else if (addition > MULTI_ADD_SPHERE_MAX)
381 addition = MULTI_ADD_SPHERE_MAX;
382
383 fvi_query_ptr->rad += addition;
384 }
385 #endif
386
387 if (fvi_do_orient) {
388 fvi_move_fvec = Objects[fvi_moveobj].orient.fvec;
389 fvi_move_uvec = Objects[fvi_moveobj].orient.uvec;
390 }
391
392 Fvi_f_normal = false;
393
394 View_position = obj->pos;
395 View_matrix = obj->orient;
396
397 ASSERT(obj->flags & OF_POLYGON_OBJECT);
398
399 #ifndef NED_PHYSICS
400 if (obj->type == OBJ_PLAYER || obj->type == OBJ_ROBOT || obj->type == OBJ_DEBRIS || obj->type == OBJ_DOOR ||
401 obj->type == OBJ_BUILDING || obj->type == OBJ_CLUTTER || obj->type == OBJ_BUILDING) {
402 SetNormalizedTimeObj(obj, normalized_time);
403 CollidePolygonModel(&obj->pos, &obj->orient, obj->rtype.pobj_info.model_num, normalized_time,
404 obj->rtype.pobj_info.subobj_flags);
405 } else {
406 CollidePolygonModel(&obj->pos, &obj->orient, obj->rtype.pobj_info.model_num, nullptr,
407 obj->rtype.pobj_info.subobj_flags);
408 }
409 #else
410 CollidePolygonModel(&obj->pos, &obj->orient, obj->rtype.pobj_info.model_num, NULL, obj->rtype.pobj_info.subobj_flags);
411 #endif
412
413 View_position = temp_pos;
414 View_matrix = temp_orient;
415
416 // Converts everything into world coordinates from submodel space
417 if (fvi_check_param) {
418 vector pnt = fvi_hit_data_ptr->hit_face_pnt[0];
419 int mn = fvi_hit_data_ptr->hit_subobject[0];
420 matrix m;
421 poly_model *pm = &Poly_models[obj->rtype.pobj_info.model_num];
422
423 while (mn != -1) {
424 vector tpnt;
425
426 vm_AnglesToMatrix(&m, pm->submodel[mn].angs.p, pm->submodel[mn].angs.h, pm->submodel[mn].angs.b);
427 vm_TransposeMatrix(&m);
428
429 tpnt = pnt * m;
430 fvi_hit_data_ptr->hit_wallnorm[0] = fvi_hit_data_ptr->hit_wallnorm[0] * m;
431
432 pnt = tpnt + pm->submodel[mn].offset + pm->submodel[mn].mod_pos;
433
434 mn = pm->submodel[mn].parent;
435 }
436
437 fvi_hit_data_ptr->hit_face_pnt[0] = pnt;
438
439 m = obj->orient;
440 vm_TransposeMatrix(&m);
441
442 fvi_hit_data_ptr->hit_wallnorm[0] = fvi_hit_data_ptr->hit_wallnorm[0] * m;
443
444 // now instance for the entire object
445 fvi_hit_data_ptr->hit_face_pnt[0] = fvi_hit_data_ptr->hit_face_pnt[0] * m;
446 fvi_hit_data_ptr->hit_face_pnt[0] += obj->pos;
447
448 // Now get the hit point
449 vector x = *fvi_query_ptr->p1 - *fvi_query_ptr->p0;
450 vm_NormalizeVector(&x);
451
452 fvi_hit_data_ptr->hit_pnt = *fvi_query_ptr->p0 + x * fvi_hit_param;
453 }
454
455 if (f_use_big_sphere) {
456 fvi_query_ptr->rad -= addition;
457 }
458
459 return Fvi_f_normal;
460 }