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 int cl_traceline_startsupercontents;
34 float CL_TraceLine(const vec3_t start, const vec3_t end, vec3_t impact, vec3_t normal, int hitbmodels, entity_render_t **hitent, int hitsupercontentsmask)
36 float maxfrac, maxrealfrac;
39 float tracemins[3], tracemaxs[3];
41 float tempnormal[3], starttransformed[3], endtransformed[3];
43 memset (&trace, 0 , sizeof(trace_t));
45 trace.realfraction = 1;
46 VectorCopy (end, trace.endpos);
49 *hitent = &cl_entities[0].render;
50 Mod_CheckLoaded(cl.worldmodel);
51 if (cl.worldmodel && cl.worldmodel->TraceBox)
52 cl.worldmodel->TraceBox(cl.worldmodel, 0, &trace, start, start, end, end, hitsupercontentsmask);
55 VectorCopy(trace.plane.normal, normal);
56 cl_traceline_startsupercontents = trace.startsupercontents;
57 maxfrac = trace.fraction;
58 maxrealfrac = trace.realfraction;
60 if (hitbmodels && cl_num_brushmodel_entities)
62 tracemins[0] = min(start[0], end[0]);
63 tracemaxs[0] = max(start[0], end[0]);
64 tracemins[1] = min(start[1], end[1]);
65 tracemaxs[1] = max(start[1], end[1]);
66 tracemins[2] = min(start[2], end[2]);
67 tracemaxs[2] = max(start[2], end[2]);
69 // look for embedded bmodels
70 for (n = 0;n < cl_num_brushmodel_entities;n++)
72 ent = cl_brushmodel_entities[n];
73 if (ent->mins[0] > tracemaxs[0] || ent->maxs[0] < tracemins[0]
74 || ent->mins[1] > tracemaxs[1] || ent->maxs[1] < tracemins[1]
75 || ent->mins[2] > tracemaxs[2] || ent->maxs[2] < tracemins[2])
78 Matrix4x4_Transform(&ent->inversematrix, start, starttransformed);
79 Matrix4x4_Transform(&ent->inversematrix, end, endtransformed);
81 if (ent->model && ent->model->TraceBox)
82 ent->model->TraceBox(ent->model, 0, &trace, starttransformed, starttransformed, endtransformed, endtransformed, hitsupercontentsmask);
84 cl_traceline_startsupercontents |= trace.startsupercontents;
85 if (maxrealfrac > trace.realfraction)
89 maxfrac = trace.fraction;
90 maxrealfrac = trace.realfraction;
93 VectorCopy(trace.plane.normal, tempnormal);
94 Matrix4x4_Transform3x3(&ent->matrix, tempnormal, normal);
99 if (maxfrac < 0 || maxfrac > 1) Con_Printf("fraction out of bounds %f %s:%d\n", maxfrac, __FILE__, __LINE__);
101 VectorLerp(start, maxfrac, end, impact);
105 float CL_SelectTraceLine(const vec3_t start, const vec3_t end, vec3_t impact, vec3_t normal, int *hitent)
107 float maxfrac, maxrealfrac;
109 entity_render_t *ent;
110 float tracemins[3], tracemaxs[3];
112 float tempnormal[3], starttransformed[3], endtransformed[3];
114 memset (&trace, 0 , sizeof(trace_t));
116 trace.realfraction = 1;
117 VectorCopy (end, trace.endpos);
121 Mod_CheckLoaded(cl.worldmodel);
122 if (cl.worldmodel && cl.worldmodel->TraceBox)
123 cl.worldmodel->TraceBox(cl.worldmodel, 0, &trace, start, start, end, end, SUPERCONTENTS_SOLID);
126 VectorCopy(trace.plane.normal, normal);
127 cl_traceline_startsupercontents = trace.startsupercontents;
128 maxfrac = trace.fraction;
129 maxrealfrac = trace.realfraction;
131 tracemins[0] = min(start[0], end[0]);
132 tracemaxs[0] = max(start[0], end[0]);
133 tracemins[1] = min(start[1], end[1]);
134 tracemaxs[1] = max(start[1], end[1]);
135 tracemins[2] = min(start[2], end[2]);
136 tracemaxs[2] = max(start[2], end[2]);
138 // look for embedded bmodels
139 for (n = 0;n < MAX_EDICTS;n++)
141 if (!cl_entities[n].state_current.active)
143 ent = &cl_entities[n].render;
144 if (!ent->model || !ent->model->TraceBox)
146 // if transparent and not selectable, skip entity
147 if (!(cl_entities[n].state_current.effects & EF_SELECTABLE) && (ent->alpha < 1 || (ent->effects & (EF_ADDITIVE | EF_NODEPTHTEST))))
149 if (ent->mins[0] > tracemaxs[0] || ent->maxs[0] < tracemins[0]
150 || ent->mins[1] > tracemaxs[1] || ent->maxs[1] < tracemins[1]
151 || ent->mins[2] > tracemaxs[2] || ent->maxs[2] < tracemins[2])
154 Matrix4x4_Transform(&ent->inversematrix, start, starttransformed);
155 Matrix4x4_Transform(&ent->inversematrix, end, endtransformed);
157 if (ent->model && ent->model->TraceBox)
158 ent->model->TraceBox(ent->model, ent->frameblend[0].frame, &trace, starttransformed, starttransformed, endtransformed, endtransformed, SUPERCONTENTS_SOLID);
160 cl_traceline_startsupercontents |= trace.startsupercontents;
161 if (maxrealfrac > trace.realfraction)
165 maxfrac = trace.fraction;
166 maxrealfrac = trace.realfraction;
169 VectorCopy(trace.plane.normal, tempnormal);
170 Matrix4x4_Transform3x3(&ent->matrix, tempnormal, normal);
174 if (maxfrac < 0 || maxfrac > 1) Con_Printf("fraction out of bounds %f %s:%d\n", maxfrac, __FILE__, __LINE__);
176 VectorLerp(start, maxfrac, end, impact);
180 void CL_FindNonSolidLocation(const vec3_t in, vec3_t out, vec_t radius)
182 // FIXME: check multiple brush models
183 if (cl.worldmodel && cl.worldmodel->brush.FindNonSolidLocation)
184 cl.worldmodel->brush.FindNonSolidLocation(cl.worldmodel, in, out, radius);
187 int CL_PointQ1Contents(const vec3_t p)
189 CL_TraceLine(p, p, NULL, NULL, true, NULL, 0);
190 return Mod_Q1BSP_NativeContentsFromSuperContents(NULL, cl_traceline_startsupercontents);
192 // FIXME: check multiple brush models
193 if (cl.worldmodel && cl.worldmodel->brush.PointContentsQ1)
194 return cl.worldmodel->brush.PointContentsQ1(cl.worldmodel, p);
199 int CL_PointSuperContents(const vec3_t p)
201 CL_TraceLine(p, p, NULL, NULL, true, NULL, 0);
202 return cl_traceline_startsupercontents;
204 // FIXME: check multiple brush models
205 if (cl.worldmodel && cl.worldmodel->brush.PointContentsQ1)
206 return cl.worldmodel->brush.PointContentsQ1(cl.worldmodel, p);