3 #include "cl_collision.h"
7 typedef struct physentity_s
9 // this may be a entity_t, or a 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 Mod_CheckLoaded(cl.worldmodel);
52 if (cl.worldmodel && cl.worldmodel->TraceBox)
53 cl.worldmodel->TraceBox(cl.worldmodel, 0, &cliptrace, startmins, startmaxs, endmins, endmaxs, hitsupercontentsmask);
58 if (hitbmodels && cl_num_brushmodel_entities)
60 tracemins[0] = min(start[0], end[0]) + mins[0];
61 tracemaxs[0] = max(start[0], end[0]) + maxs[0];
62 tracemins[1] = min(start[1], end[1]) + mins[1];
63 tracemaxs[1] = max(start[1], end[1]) + maxs[1];
64 tracemins[2] = min(start[2], end[2]) + mins[2];
65 tracemaxs[2] = max(start[2], end[2]) + maxs[2];
67 // look for embedded bmodels
68 for (n = 0;n < cl_num_brushmodel_entities;n++)
70 ent = &cl_entities[cl_brushmodel_entities[n]].render;
71 if (!BoxesOverlap(tracemins, tracemaxs, ent->mins, ent->maxs))
74 Matrix4x4_Transform(&ent->inversematrix, start, starttransformed);
75 Matrix4x4_Transform(&ent->inversematrix, end, endtransformed);
76 VectorAdd(starttransformed, mins, starttransformedmins);
77 VectorAdd(starttransformed, maxs, starttransformedmaxs);
78 VectorAdd(endtransformed, mins, endtransformedmins);
79 VectorAdd(endtransformed, maxs, endtransformedmaxs);
81 memset (&trace, 0 , sizeof(trace_t));
83 trace.realfraction = 1;
85 if (ent->model && ent->model->TraceBox)
86 ent->model->TraceBox(ent->model, 0, &trace, starttransformedmins, starttransformedmaxs, endtransformedmins, endtransformedmaxs, hitsupercontentsmask);
88 // LordHavoc: take the 'best' answers from the new trace and combine with existing data
90 cliptrace.allsolid = true;
93 cliptrace.startsolid = true;
94 if (cliptrace.realfraction == 1)
96 *hitent = cl_brushmodel_entities[n];
98 // don't set this except on the world, because it can easily confuse
99 // monsters underwater if there's a bmodel involved in the trace
100 // (inopen && inwater is how they check water visibility)
102 // cliptrace.inopen = true;
104 cliptrace.inwater = true;
105 if (trace.realfraction < cliptrace.realfraction)
107 cliptrace.fraction = trace.fraction;
108 cliptrace.realfraction = trace.realfraction;
109 cliptrace.plane = trace.plane;
111 *hitent = cl_brushmodel_entities[n];
112 Matrix4x4_Transform3x3(&ent->matrix, trace.plane.normal, cliptrace.plane.normal);
114 cliptrace.startsupercontents |= trace.startsupercontents;
119 tracemins[0] = min(start[0], end[0]) + mins[0];
120 tracemaxs[0] = max(start[0], end[0]) + maxs[0];
121 tracemins[1] = min(start[1], end[1]) + mins[1];
122 tracemaxs[1] = max(start[1], end[1]) + maxs[1];
123 tracemins[2] = min(start[2], end[2]) + mins[2];
124 tracemaxs[2] = max(start[2], end[2]) + maxs[2];
126 for (n = 1;n < cl.maxclients+1;n++)
128 if (n != cl.playerentity)
130 ent = &cl_entities[n].render;
132 playermins = cl_playerstandmins;
133 playermaxs = cl_playerstandmaxs;
134 VectorAdd(ent->origin, playermins, entmins);
135 VectorAdd(ent->origin, playermaxs, entmaxs);
136 if (!BoxesOverlap(tracemins, tracemaxs, entmins, entmaxs))
139 memset (&trace, 0 , sizeof(trace_t));
141 trace.realfraction = 1;
143 Matrix4x4_Transform(&ent->inversematrix, start, starttransformed);
144 Matrix4x4_Transform(&ent->inversematrix, end, endtransformed);
145 Collision_ClipTrace_Box(&trace, playermins, playermaxs, starttransformed, mins, maxs, endtransformed, hitsupercontentsmask, SUPERCONTENTS_SOLID);
147 // LordHavoc: take the 'best' answers from the new trace and combine with existing data
149 cliptrace.allsolid = true;
150 if (trace.startsolid)
152 cliptrace.startsolid = true;
153 if (cliptrace.realfraction == 1)
157 // don't set this except on the world, because it can easily confuse
158 // monsters underwater if there's a bmodel involved in the trace
159 // (inopen && inwater is how they check water visibility)
161 // cliptrace.inopen = true;
163 cliptrace.inwater = true;
164 if (trace.realfraction < cliptrace.realfraction)
166 cliptrace.fraction = trace.fraction;
167 cliptrace.realfraction = trace.realfraction;
168 cliptrace.plane = trace.plane;
171 Matrix4x4_Transform3x3(&ent->matrix, trace.plane.normal, cliptrace.plane.normal);
173 cliptrace.startsupercontents |= trace.startsupercontents;
177 cliptrace.fraction = bound(0, cliptrace.fraction, 1);
178 cliptrace.realfraction = bound(0, cliptrace.realfraction, 1);
179 VectorLerp(start, cliptrace.fraction, end, cliptrace.endpos);
183 float CL_SelectTraceLine(const vec3_t start, const vec3_t end, vec3_t impact, vec3_t normal, int *hitent, entity_render_t *ignoreent)
185 float maxfrac, maxrealfrac;
187 entity_render_t *ent;
188 float tracemins[3], tracemaxs[3];
190 float tempnormal[3], starttransformed[3], endtransformed[3];
192 memset (&trace, 0 , sizeof(trace_t));
194 trace.realfraction = 1;
195 VectorCopy (end, trace.endpos);
199 Mod_CheckLoaded(cl.worldmodel);
200 if (cl.worldmodel && cl.worldmodel->TraceBox)
201 cl.worldmodel->TraceBox(cl.worldmodel, 0, &trace, start, start, end, end, SUPERCONTENTS_SOLID);
204 VectorCopy(trace.plane.normal, normal);
205 //cl_traceline_startsupercontents = trace.startsupercontents;
206 maxfrac = trace.fraction;
207 maxrealfrac = trace.realfraction;
209 tracemins[0] = min(start[0], end[0]);
210 tracemaxs[0] = max(start[0], end[0]);
211 tracemins[1] = min(start[1], end[1]);
212 tracemaxs[1] = max(start[1], end[1]);
213 tracemins[2] = min(start[2], end[2]);
214 tracemaxs[2] = max(start[2], end[2]);
216 // look for embedded bmodels
217 for (n = 0;n < cl_num_entities;n++)
219 if (!cl_entities_active[n])
221 ent = &cl_entities[n].render;
222 if (!BoxesOverlap(ent->mins, ent->maxs, tracemins, tracemaxs))
224 if (!ent->model || !ent->model->TraceBox)
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);