3 #include "cl_collision.h"
7 typedef struct physentity_s
9 // this may be a entity_t, or a prvm_edict_t, or whatever
12 // can be NULL if it is a bbox object
15 // node this entity crosses
16 // for avoiding unnecessary collisions
19 // matrix for converting from model to world coordinates
20 double modeltoworldmatrix[3][4];
22 // matrix for converting from world to model coordinates
23 double worldtomodelmatrix[3][4];
25 // if this is a bmodel, this is used for culling it quickly
26 // if this is not a bmodel, this is used for actual collisions
27 double mins[3], maxs[3];
32 trace_t CL_TraceBox(const vec3_t start, const vec3_t mins, const vec3_t maxs, const vec3_t end, int hitbmodels, int *hitent, int hitsupercontentsmask, qboolean hitplayers)
36 vec3_t tracemins, tracemaxs;
37 trace_t cliptrace, trace;
38 vec3_t starttransformed, endtransformed, starttransformedmins, endtransformedmins, starttransformedmaxs, endtransformedmaxs;
39 vec3_t startmins, startmaxs, endmins, endmaxs, entmins, entmaxs;
40 vec_t *playermins, *playermaxs;
42 VectorAdd(start, mins, startmins);
43 VectorAdd(start, maxs, startmaxs);
44 VectorAdd(end, mins, endmins);
45 VectorAdd(end, maxs, endmaxs);
47 memset (&cliptrace, 0 , sizeof(trace_t));
48 cliptrace.fraction = 1;
49 cliptrace.realfraction = 1;
51 if (cl.worldmodel && cl.worldmodel->TraceBox)
52 cl.worldmodel->TraceBox(cl.worldmodel, 0, &cliptrace, startmins, startmaxs, endmins, endmaxs, hitsupercontentsmask);
57 if (hitbmodels && cl_num_brushmodel_entities)
59 tracemins[0] = min(start[0], end[0]) + mins[0];
60 tracemaxs[0] = max(start[0], end[0]) + maxs[0];
61 tracemins[1] = min(start[1], end[1]) + mins[1];
62 tracemaxs[1] = max(start[1], end[1]) + maxs[1];
63 tracemins[2] = min(start[2], end[2]) + mins[2];
64 tracemaxs[2] = max(start[2], end[2]) + maxs[2];
66 // look for embedded bmodels
67 for (n = 0;n < cl_num_brushmodel_entities;n++)
69 ent = &cl_entities[cl_brushmodel_entities[n]].render;
70 if (!BoxesOverlap(tracemins, tracemaxs, ent->mins, ent->maxs))
73 Matrix4x4_Transform(&ent->inversematrix, start, starttransformed);
74 Matrix4x4_Transform(&ent->inversematrix, end, endtransformed);
75 VectorAdd(starttransformed, mins, starttransformedmins);
76 VectorAdd(starttransformed, maxs, starttransformedmaxs);
77 VectorAdd(endtransformed, mins, endtransformedmins);
78 VectorAdd(endtransformed, maxs, endtransformedmaxs);
80 memset (&trace, 0 , sizeof(trace_t));
82 trace.realfraction = 1;
84 if (ent->model && ent->model->TraceBox)
85 ent->model->TraceBox(ent->model, 0, &trace, starttransformedmins, starttransformedmaxs, endtransformedmins, endtransformedmaxs, hitsupercontentsmask);
87 // LordHavoc: take the 'best' answers from the new trace and combine with existing data
89 cliptrace.allsolid = true;
92 cliptrace.startsolid = true;
93 if (cliptrace.realfraction == 1)
95 *hitent = cl_brushmodel_entities[n];
97 // don't set this except on the world, because it can easily confuse
98 // monsters underwater if there's a bmodel involved in the trace
99 // (inopen && inwater is how they check water visibility)
101 // cliptrace.inopen = true;
103 cliptrace.inwater = true;
104 if (trace.realfraction < cliptrace.realfraction)
106 cliptrace.fraction = trace.fraction;
107 cliptrace.realfraction = trace.realfraction;
108 cliptrace.plane = trace.plane;
110 *hitent = cl_brushmodel_entities[n];
111 Matrix4x4_Transform3x3(&ent->matrix, trace.plane.normal, cliptrace.plane.normal);
113 cliptrace.startsupercontents |= trace.startsupercontents;
118 tracemins[0] = min(start[0], end[0]) + mins[0];
119 tracemaxs[0] = max(start[0], end[0]) + maxs[0];
120 tracemins[1] = min(start[1], end[1]) + mins[1];
121 tracemaxs[1] = max(start[1], end[1]) + maxs[1];
122 tracemins[2] = min(start[2], end[2]) + mins[2];
123 tracemaxs[2] = max(start[2], end[2]) + maxs[2];
125 for (n = 1;n < cl.maxclients+1;n++)
127 if (n != cl.playerentity)
129 ent = &cl_entities[n].render;
131 playermins = cl_playerstandmins;
132 playermaxs = cl_playerstandmaxs;
133 VectorAdd(ent->origin, playermins, entmins);
134 VectorAdd(ent->origin, playermaxs, entmaxs);
135 if (!BoxesOverlap(tracemins, tracemaxs, entmins, entmaxs))
138 memset (&trace, 0 , sizeof(trace_t));
140 trace.realfraction = 1;
142 Matrix4x4_Transform(&ent->inversematrix, start, starttransformed);
143 Matrix4x4_Transform(&ent->inversematrix, end, endtransformed);
144 Collision_ClipTrace_Box(&trace, playermins, playermaxs, starttransformed, mins, maxs, endtransformed, hitsupercontentsmask, SUPERCONTENTS_SOLID);
146 // LordHavoc: take the 'best' answers from the new trace and combine with existing data
148 cliptrace.allsolid = true;
149 if (trace.startsolid)
151 cliptrace.startsolid = true;
152 if (cliptrace.realfraction == 1)
156 // don't set this except on the world, because it can easily confuse
157 // monsters underwater if there's a bmodel involved in the trace
158 // (inopen && inwater is how they check water visibility)
160 // cliptrace.inopen = true;
162 cliptrace.inwater = true;
163 if (trace.realfraction < cliptrace.realfraction)
165 cliptrace.fraction = trace.fraction;
166 cliptrace.realfraction = trace.realfraction;
167 cliptrace.plane = trace.plane;
170 Matrix4x4_Transform3x3(&ent->matrix, trace.plane.normal, cliptrace.plane.normal);
172 cliptrace.startsupercontents |= trace.startsupercontents;
176 cliptrace.fraction = bound(0, cliptrace.fraction, 1);
177 cliptrace.realfraction = bound(0, cliptrace.realfraction, 1);
178 VectorLerp(start, cliptrace.fraction, end, cliptrace.endpos);
182 float CL_SelectTraceLine(const vec3_t start, const vec3_t end, vec3_t impact, vec3_t normal, int *hitent, entity_render_t *ignoreent)
184 float maxfrac, maxrealfrac;
186 entity_render_t *ent;
187 float tracemins[3], tracemaxs[3];
189 float tempnormal[3], starttransformed[3], endtransformed[3];
191 memset (&trace, 0 , sizeof(trace_t));
193 trace.realfraction = 1;
194 VectorCopy (end, trace.endpos);
198 if (cl.worldmodel && cl.worldmodel->TraceBox)
199 cl.worldmodel->TraceBox(cl.worldmodel, 0, &trace, start, start, end, end, SUPERCONTENTS_SOLID);
202 VectorCopy(trace.plane.normal, normal);
203 //cl_traceline_startsupercontents = trace.startsupercontents;
204 maxfrac = trace.fraction;
205 maxrealfrac = trace.realfraction;
207 tracemins[0] = min(start[0], end[0]);
208 tracemaxs[0] = max(start[0], end[0]);
209 tracemins[1] = min(start[1], end[1]);
210 tracemaxs[1] = max(start[1], end[1]);
211 tracemins[2] = min(start[2], end[2]);
212 tracemaxs[2] = max(start[2], end[2]);
214 // look for embedded bmodels
215 for (n = 0;n < cl_num_entities;n++)
217 if (!cl_entities_active[n])
219 ent = &cl_entities[n].render;
220 if (!BoxesOverlap(ent->mins, ent->maxs, tracemins, tracemaxs))
222 if (!ent->model || !ent->model->TraceBox)
224 if ((ent->flags & RENDER_EXTERIORMODEL) && !chase_active.integer)
226 // if transparent and not selectable, skip entity
227 if (!(cl_entities[n].state_current.effects & EF_SELECTABLE) && (ent->alpha < 1 || (ent->effects & (EF_ADDITIVE | EF_NODEPTHTEST))))
229 if (ent == ignoreent)
231 Matrix4x4_Transform(&ent->inversematrix, start, starttransformed);
232 Matrix4x4_Transform(&ent->inversematrix, end, endtransformed);
234 if (ent->model && ent->model->TraceBox)
235 ent->model->TraceBox(ent->model, ent->frameblend[0].frame, &trace, starttransformed, starttransformed, endtransformed, endtransformed, SUPERCONTENTS_SOLID);
237 //cl_traceline_startsupercontents |= trace.startsupercontents;
238 if (maxrealfrac > trace.realfraction)
242 maxfrac = trace.fraction;
243 maxrealfrac = trace.realfraction;
246 VectorCopy(trace.plane.normal, tempnormal);
247 Matrix4x4_Transform3x3(&ent->matrix, tempnormal, normal);
251 maxfrac = bound(0, maxfrac, 1);
252 maxrealfrac = bound(0, maxrealfrac, 1);
253 //if (maxfrac < 0 || maxfrac > 1) Con_Printf("fraction out of bounds %f %s:%d\n", maxfrac, __FILE__, __LINE__);
255 VectorLerp(start, maxfrac, end, impact);
259 void CL_FindNonSolidLocation(const vec3_t in, vec3_t out, vec_t radius)
261 // FIXME: check multiple brush models
262 if (cl.worldmodel && cl.worldmodel->brush.FindNonSolidLocation)
263 cl.worldmodel->brush.FindNonSolidLocation(cl.worldmodel, in, out, radius);
266 int CL_PointQ1Contents(const vec3_t p)
268 return Mod_Q1BSP_NativeContentsFromSuperContents(NULL, CL_TraceBox(p, vec3_origin, vec3_origin, p, true, NULL, 0, false).startsupercontents);
270 // FIXME: check multiple brush models
271 if (cl.worldmodel && cl.worldmodel->brush.PointContentsQ1)
272 return cl.worldmodel->brush.PointContentsQ1(cl.worldmodel, p);
277 int CL_PointSuperContents(const vec3_t p)
279 return CL_TraceBox(p, vec3_origin, vec3_origin, p, true, NULL, 0, false).startsupercontents;
281 // FIXME: check multiple brush models
282 if (cl.worldmodel && cl.worldmodel->brush.PointContentsQ1)
283 return cl.worldmodel->brush.PointContentsQ1(cl.worldmodel, p);