5 #define COLLISION_SNAPSCALE (32.0f)
6 #define COLLISION_SNAP (1.0f / COLLISION_SNAPSCALE)
7 #define COLLISION_SNAP2 (2.0f / COLLISION_SNAPSCALE)
8 #define COLLISION_PLANE_DIST_EPSILON (2.0f / COLLISION_SNAPSCALE)
10 cvar_t collision_impactnudge = {0, "collision_impactnudge", "0.03125", "how much to back off from the impact"};
11 cvar_t collision_startnudge = {0, "collision_startnudge", "0", "how much to bias collision trace start"};
12 cvar_t collision_endnudge = {0, "collision_endnudge", "0", "how much to bias collision trace end"};
13 cvar_t collision_enternudge = {0, "collision_enternudge", "0", "how much to bias collision entry fraction"};
14 cvar_t collision_leavenudge = {0, "collision_leavenudge", "0", "how much to bias collision exit fraction"};
16 void Collision_Init (void)
18 Cvar_RegisterVariable(&collision_impactnudge);
19 Cvar_RegisterVariable(&collision_startnudge);
20 Cvar_RegisterVariable(&collision_endnudge);
21 Cvar_RegisterVariable(&collision_enternudge);
22 Cvar_RegisterVariable(&collision_leavenudge);
38 void Collision_PrintBrushAsQHull(colbrushf_t *brush, const char *name)
41 Con_Printf("3 %s\n%i\n", name, brush->numpoints);
42 for (i = 0;i < brush->numpoints;i++)
43 Con_Printf("%f %f %f\n", brush->points[i].v[0], brush->points[i].v[1], brush->points[i].v[2]);
45 Con_Printf("4\n%i\n", brush->numplanes);
46 for (i = 0;i < brush->numplanes;i++)
47 Con_Printf("%f %f %f %f\n", brush->planes[i].normal[0], brush->planes[i].normal[1], brush->planes[i].normal[2], brush->planes[i].dist);
50 void Collision_ValidateBrush(colbrushf_t *brush)
52 int j, k, pointsoffplanes, pointonplanes, pointswithinsufficientplanes, printbrush;
55 if (!brush->numpoints)
57 Con_Print("Collision_ValidateBrush: brush with no points!\n");
61 // it's ok for a brush to have one point and no planes...
62 if (brush->numplanes == 0 && brush->numpoints != 1)
64 Con_Print("Collision_ValidateBrush: brush with no planes and more than one point!\n");
71 pointswithinsufficientplanes = 0;
72 for (k = 0;k < brush->numplanes;k++)
73 if (DotProduct(brush->planes[k].normal, brush->planes[k].normal) < 0.0001f)
74 Con_Printf("Collision_ValidateBrush: plane #%i (%f %f %f %f) is degenerate\n", k, brush->planes[k].normal[0], brush->planes[k].normal[1], brush->planes[k].normal[2], brush->planes[k].dist);
75 for (j = 0;j < brush->numpoints;j++)
78 for (k = 0;k < brush->numplanes;k++)
80 d = DotProduct(brush->points[j].v, brush->planes[k].normal) - brush->planes[k].dist;
81 if (d > COLLISION_PLANE_DIST_EPSILON)
83 Con_Printf("Collision_ValidateBrush: point #%i (%f %f %f) infront of plane #%i (%f %f %f %f)\n", j, brush->points[j].v[0], brush->points[j].v[1], brush->points[j].v[2], k, brush->planes[k].normal[0], brush->planes[k].normal[1], brush->planes[k].normal[2], brush->planes[k].dist);
86 if (fabs(d) > COLLISION_PLANE_DIST_EPSILON)
91 if (pointonplanes < 3)
92 pointswithinsufficientplanes++;
94 if (pointswithinsufficientplanes)
96 Con_Print("Collision_ValidateBrush: some points have insufficient planes, every point must be on at least 3 planes to form a corner.\n");
99 if (pointsoffplanes == 0) // all points are on all planes
101 Con_Print("Collision_ValidateBrush: all points lie on all planes (degenerate, no brush volume!)\n");
106 Collision_PrintBrushAsQHull(brush, "unnamed");
109 float nearestplanedist_float(const float *normal, const colpointf_t *points, int numpoints)
111 float dist, bestdist;
112 bestdist = DotProduct(points->v, normal);
116 dist = DotProduct(points->v, normal);
117 bestdist = min(bestdist, dist);
123 float furthestplanedist_float(const float *normal, const colpointf_t *points, int numpoints)
125 float dist, bestdist;
126 bestdist = DotProduct(points->v, normal);
130 dist = DotProduct(points->v, normal);
131 bestdist = max(bestdist, dist);
138 colbrushf_t *Collision_NewBrushFromPlanes(mempool_t *mempool, int numoriginalplanes, const colplanef_t *originalplanes, int supercontents)
140 // TODO: planesbuf could be replaced by a remapping table
142 int numpointsbuf = 0, maxpointsbuf = 256, numplanesbuf = 0, maxplanesbuf = 256, numelementsbuf = 0, maxelementsbuf = 256;
145 colpointf_t pointsbuf[256];
146 colplanef_t planesbuf[256];
147 int elementsbuf[1024];
148 int polypointbuf[256];
153 // enable these if debugging to avoid seeing garbage in unused data
154 memset(pointsbuf, 0, sizeof(pointsbuf));
155 memset(planesbuf, 0, sizeof(planesbuf));
156 memset(elementsbuf, 0, sizeof(elementsbuf));
157 memset(polypointbuf, 0, sizeof(polypointbuf));
158 memset(p, 0, sizeof(p));
160 // figure out how large a bounding box we need to properly compute this brush
162 for (j = 0;j < numoriginalplanes;j++)
163 maxdist = max(maxdist, fabs(originalplanes[j].dist));
164 // now make it large enough to enclose the entire brush, and round it off to a reasonable multiple of 1024
165 maxdist = floor(maxdist * (4.0 / 1024.0) + 2) * 1024.0;
166 // construct a collision brush (points, planes, and renderable mesh) from
167 // a set of planes, this also optimizes out any unnecessary planes (ones
168 // whose polygon is clipped away by the other planes)
169 for (j = 0;j < numoriginalplanes;j++)
171 // add the plane uniquely (no duplicates)
172 for (k = 0;k < numplanesbuf;k++)
173 if (VectorCompare(planesbuf[k].normal, originalplanes[j].normal) && planesbuf[k].dist == originalplanes[j].dist)
175 // if the plane is a duplicate, skip it
176 if (k < numplanesbuf)
178 // check if there are too many and skip the brush
179 if (numplanesbuf >= maxplanesbuf)
181 Con_Print("Collision_NewBrushFromPlanes: failed to build collision brush: too many planes for buffer\n");
185 // create a large polygon from the plane
187 PolygonD_QuadForPlane(p[w], originalplanes[j].normal[0], originalplanes[j].normal[1], originalplanes[j].normal[2], originalplanes[j].dist, maxdist);
189 // clip it by all other planes
190 for (k = 0;k < numoriginalplanes && pnumpoints && pnumpoints <= pmaxpoints;k++)
192 // skip the plane this polygon
193 // (nothing happens if it is processed, this is just an optimization)
196 // we want to keep the inside of the brush plane so we flip
198 PolygonD_Divide(pnumpoints, p[w], -originalplanes[k].normal[0], -originalplanes[k].normal[1], -originalplanes[k].normal[2], -originalplanes[k].dist, COLLISION_PLANE_DIST_EPSILON, pmaxpoints, p[!w], &pnumpoints, 0, NULL, NULL, NULL);
202 // if nothing is left, skip it
203 // LordHavoc: do not skip planes, because they may be bevel planes
204 // added by the map compiler to allow sliding along edges
205 //if (pnumpoints < 3)
207 // //Con_Printf("Collision_NewBrushFromPlanes: warning: polygon for plane %f %f %f %f clipped away\n", originalplanes[j].normal[0], originalplanes[j].normal[1], originalplanes[j].normal[2], originalplanes[j].dist);
211 for (k = 0;k < pnumpoints;k++)
215 for (l = 0;l < numoriginalplanes;l++)
216 if (fabs(DotProduct(&p[w][k*3], originalplanes[l].normal) - originalplanes[l].dist) < COLLISION_PLANE_DIST_EPSILON)
223 Con_Printf("Collision_NewBrushFromPlanes: warning: polygon point does not lie on at least 3 planes\n");
227 // check if there are too many polygon vertices for buffer
228 if (pnumpoints > pmaxpoints)
230 Con_Print("Collision_NewBrushFromPlanes: failed to build collision brush: too many points for buffer\n");
234 // check if there are too many triangle elements for buffer
235 if (numelementsbuf + (pnumpoints - 2) * 3 > maxelementsbuf)
237 Con_Print("Collision_NewBrushFromPlanes: failed to build collision brush: too many triangle elements for buffer\n");
241 for (k = 0;k < pnumpoints;k++)
243 // check if there is already a matching point (no duplicates)
244 for (m = 0;m < numpointsbuf;m++)
245 if (VectorDistance2(&p[w][k*3], pointsbuf[m].v) < COLLISION_SNAP2)
248 // if there is no match, add a new one
249 if (m == numpointsbuf)
251 // check if there are too many and skip the brush
252 if (numpointsbuf >= maxpointsbuf)
254 Con_Print("Collision_NewBrushFromPlanes: failed to build collision brush: too many points for buffer\n");
258 VectorCopy(&p[w][k*3], pointsbuf[numpointsbuf].v);
262 // store the index into a buffer
266 // add the triangles for the polygon
267 // (this particular code makes a triangle fan)
268 for (k = 0;k < pnumpoints - 2;k++)
270 elementsbuf[numelementsbuf++] = polypointbuf[0];
271 elementsbuf[numelementsbuf++] = polypointbuf[k + 1];
272 elementsbuf[numelementsbuf++] = polypointbuf[k + 2];
276 VectorCopy(originalplanes[j].normal, planesbuf[numplanesbuf].normal);
277 planesbuf[numplanesbuf].dist = originalplanes[j].dist;
278 planesbuf[numplanesbuf].q3surfaceflags = originalplanes[j].q3surfaceflags;
279 planesbuf[numplanesbuf].texture = originalplanes[j].texture;
283 // validate plane distances
284 for (j = 0;j < numplanesbuf;j++)
286 float d = furthestplanedist_float(planesbuf[j].normal, pointsbuf, numpointsbuf);
287 if (fabs(planesbuf[j].dist - d) > COLLISION_PLANE_DIST_EPSILON)
288 Con_Printf("plane %f %f %f %f mismatches dist %f\n", planesbuf[j].normal[0], planesbuf[j].normal[1], planesbuf[j].normal[2], planesbuf[j].dist, d);
291 // if nothing is left, there's nothing to allocate
292 if (numelementsbuf < 12 || numplanesbuf < 4 || numpointsbuf < 4)
294 Con_Printf("Collision_NewBrushFromPlanes: failed to build collision brush: %i triangles, %i planes (input was %i planes), %i vertices\n", numelementsbuf / 3, numplanesbuf, numoriginalplanes, numpointsbuf);
298 // allocate the brush and copy to it
299 brush = (colbrushf_t *)Mem_Alloc(mempool, sizeof(colbrushf_t) + sizeof(colpointf_t) * numpointsbuf + sizeof(colplanef_t) * numplanesbuf + sizeof(int) * numelementsbuf);
300 brush->supercontents = supercontents;
301 brush->numplanes = numplanesbuf;
302 brush->numpoints = numpointsbuf;
303 brush->numtriangles = numelementsbuf / 3;
304 brush->planes = (colplanef_t *)(brush + 1);
305 brush->points = (colpointf_t *)(brush->planes + brush->numplanes);
306 brush->elements = (int *)(brush->points + brush->numpoints);
307 for (j = 0;j < brush->numpoints;j++)
309 brush->points[j].v[0] = pointsbuf[j].v[0];
310 brush->points[j].v[1] = pointsbuf[j].v[1];
311 brush->points[j].v[2] = pointsbuf[j].v[2];
313 for (j = 0;j < brush->numplanes;j++)
315 brush->planes[j].normal[0] = planesbuf[j].normal[0];
316 brush->planes[j].normal[1] = planesbuf[j].normal[1];
317 brush->planes[j].normal[2] = planesbuf[j].normal[2];
318 brush->planes[j].dist = planesbuf[j].dist;
319 brush->planes[j].q3surfaceflags = planesbuf[j].q3surfaceflags;
320 brush->planes[j].texture = planesbuf[j].texture;
322 for (j = 0;j < brush->numtriangles * 3;j++)
323 brush->elements[j] = elementsbuf[j];
324 VectorCopy(brush->points[0].v, brush->mins);
325 VectorCopy(brush->points[0].v, brush->maxs);
326 for (j = 1;j < brush->numpoints;j++)
328 brush->mins[0] = min(brush->mins[0], brush->points[j].v[0]);
329 brush->mins[1] = min(brush->mins[1], brush->points[j].v[1]);
330 brush->mins[2] = min(brush->mins[2], brush->points[j].v[2]);
331 brush->maxs[0] = max(brush->maxs[0], brush->points[j].v[0]);
332 brush->maxs[1] = max(brush->maxs[1], brush->points[j].v[1]);
333 brush->maxs[2] = max(brush->maxs[2], brush->points[j].v[2]);
341 Collision_ValidateBrush(brush);
347 void Collision_CalcPlanesForPolygonBrushFloat(colbrushf_t *brush)
350 float edge0[3], edge1[3], edge2[3], normal[3], dist, bestdist;
353 // FIXME: these probably don't actually need to be normalized if the collision code does not care
354 if (brush->numpoints == 3)
356 // optimized triangle case
357 TriangleNormal(brush->points[0].v, brush->points[1].v, brush->points[2].v, brush->planes[0].normal);
358 if (DotProduct(brush->planes[0].normal, brush->planes[0].normal) < 0.0001f)
360 // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
361 brush->numplanes = 0;
366 brush->numplanes = 5;
367 VectorNormalize(brush->planes[0].normal);
368 brush->planes[0].dist = DotProduct(brush->points->v, brush->planes[0].normal);
369 VectorNegate(brush->planes[0].normal, brush->planes[1].normal);
370 brush->planes[1].dist = -brush->planes[0].dist;
371 VectorSubtract(brush->points[2].v, brush->points[0].v, edge0);
372 VectorSubtract(brush->points[0].v, brush->points[1].v, edge1);
373 VectorSubtract(brush->points[1].v, brush->points[2].v, edge2);
376 float projectionnormal[3], projectionedge0[3], projectionedge1[3], projectionedge2[3];
378 float dist, bestdist;
379 bestdist = fabs(brush->planes[0].normal[0]);
381 for (i = 1;i < 3;i++)
383 dist = fabs(brush->planes[0].normal[i]);
390 VectorClear(projectionnormal);
391 if (brush->planes[0].normal[best] < 0)
392 projectionnormal[best] = -1;
394 projectionnormal[best] = 1;
395 VectorCopy(edge0, projectionedge0);
396 VectorCopy(edge1, projectionedge1);
397 VectorCopy(edge2, projectionedge2);
398 projectionedge0[best] = 0;
399 projectionedge1[best] = 0;
400 projectionedge2[best] = 0;
401 CrossProduct(projectionedge0, projectionnormal, brush->planes[2].normal);
402 CrossProduct(projectionedge1, projectionnormal, brush->planes[3].normal);
403 CrossProduct(projectionedge2, projectionnormal, brush->planes[4].normal);
406 CrossProduct(edge0, brush->planes->normal, brush->planes[2].normal);
407 CrossProduct(edge1, brush->planes->normal, brush->planes[3].normal);
408 CrossProduct(edge2, brush->planes->normal, brush->planes[4].normal);
410 VectorNormalize(brush->planes[2].normal);
411 VectorNormalize(brush->planes[3].normal);
412 VectorNormalize(brush->planes[4].normal);
413 brush->planes[2].dist = DotProduct(brush->points[2].v, brush->planes[2].normal);
414 brush->planes[3].dist = DotProduct(brush->points[0].v, brush->planes[3].normal);
415 brush->planes[4].dist = DotProduct(brush->points[1].v, brush->planes[4].normal);
417 if (developer.integer >= 100)
423 VectorSubtract(brush->points[0].v, brush->points[1].v, edge0);
424 VectorSubtract(brush->points[2].v, brush->points[1].v, edge1);
425 CrossProduct(edge0, edge1, normal);
426 VectorNormalize(normal);
427 VectorSubtract(normal, brush->planes[0].normal, temp);
428 if (VectorLength(temp) > 0.01f)
429 Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: TriangleNormal gave wrong answer (%f %f %f != correct answer %f %f %f)\n", brush->planes->normal[0], brush->planes->normal[1], brush->planes->normal[2], normal[0], normal[1], normal[2]);
430 if (fabs(DotProduct(brush->planes[1].normal, brush->planes[0].normal) - -1.0f) > 0.01f || fabs(brush->planes[1].dist - -brush->planes[0].dist) > 0.01f)
431 Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: plane 1 (%f %f %f %f) is not opposite plane 0 (%f %f %f %f)\n", brush->planes[1].normal[0], brush->planes[1].normal[1], brush->planes[1].normal[2], brush->planes[1].dist, brush->planes[0].normal[0], brush->planes[0].normal[1], brush->planes[0].normal[2], brush->planes[0].dist);
433 if (fabs(DotProduct(brush->planes[2].normal, brush->planes[0].normal)) > 0.01f)
434 Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: plane 2 (%f %f %f %f) is not perpendicular to plane 0 (%f %f %f %f)\n", brush->planes[2].normal[0], brush->planes[2].normal[1], brush->planes[2].normal[2], brush->planes[2].dist, brush->planes[0].normal[0], brush->planes[0].normal[1], brush->planes[0].normal[2], brush->planes[2].dist);
435 if (fabs(DotProduct(brush->planes[3].normal, brush->planes[0].normal)) > 0.01f)
436 Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: plane 3 (%f %f %f %f) is not perpendicular to plane 0 (%f %f %f %f)\n", brush->planes[3].normal[0], brush->planes[3].normal[1], brush->planes[3].normal[2], brush->planes[3].dist, brush->planes[0].normal[0], brush->planes[0].normal[1], brush->planes[0].normal[2], brush->planes[3].dist);
437 if (fabs(DotProduct(brush->planes[4].normal, brush->planes[0].normal)) > 0.01f)
438 Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: plane 4 (%f %f %f %f) is not perpendicular to plane 0 (%f %f %f %f)\n", brush->planes[4].normal[0], brush->planes[4].normal[1], brush->planes[4].normal[2], brush->planes[4].dist, brush->planes[0].normal[0], brush->planes[0].normal[1], brush->planes[0].normal[2], brush->planes[4].dist);
439 if (fabs(DotProduct(brush->planes[2].normal, edge0)) > 0.01f)
440 Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: plane 2 (%f %f %f %f) is not perpendicular to edge 0 (%f %f %f to %f %f %f)\n", brush->planes[2].normal[0], brush->planes[2].normal[1], brush->planes[2].normal[2], brush->planes[2].dist, brush->points[2].v[0], brush->points[2].v[1], brush->points[2].v[2], brush->points[0].v[0], brush->points[0].v[1], brush->points[0].v[2]);
441 if (fabs(DotProduct(brush->planes[3].normal, edge1)) > 0.01f)
442 Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: plane 3 (%f %f %f %f) is not perpendicular to edge 1 (%f %f %f to %f %f %f)\n", brush->planes[3].normal[0], brush->planes[3].normal[1], brush->planes[3].normal[2], brush->planes[3].dist, brush->points[0].v[0], brush->points[0].v[1], brush->points[0].v[2], brush->points[1].v[0], brush->points[1].v[1], brush->points[1].v[2]);
443 if (fabs(DotProduct(brush->planes[4].normal, edge2)) > 0.01f)
444 Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: plane 4 (%f %f %f %f) is not perpendicular to edge 2 (%f %f %f to %f %f %f)\n", brush->planes[4].normal[0], brush->planes[4].normal[1], brush->planes[4].normal[2], brush->planes[4].dist, brush->points[1].v[0], brush->points[1].v[1], brush->points[1].v[2], brush->points[2].v[0], brush->points[2].v[1], brush->points[2].v[2]);
447 if (fabs(DotProduct(brush->points[0].v, brush->planes[0].normal) - brush->planes[0].dist) > 0.01f || fabs(DotProduct(brush->points[1].v, brush->planes[0].normal) - brush->planes[0].dist) > 0.01f || fabs(DotProduct(brush->points[2].v, brush->planes[0].normal) - brush->planes[0].dist) > 0.01f)
448 Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: edges (%f %f %f to %f %f %f to %f %f %f) off front plane 0 (%f %f %f %f)\n", brush->points[0].v[0], brush->points[0].v[1], brush->points[0].v[2], brush->points[1].v[0], brush->points[1].v[1], brush->points[1].v[2], brush->points[2].v[0], brush->points[2].v[1], brush->points[2].v[2], brush->planes[0].normal[0], brush->planes[0].normal[1], brush->planes[0].normal[2], brush->planes[0].dist);
449 if (fabs(DotProduct(brush->points[0].v, brush->planes[1].normal) - brush->planes[1].dist) > 0.01f || fabs(DotProduct(brush->points[1].v, brush->planes[1].normal) - brush->planes[1].dist) > 0.01f || fabs(DotProduct(brush->points[2].v, brush->planes[1].normal) - brush->planes[1].dist) > 0.01f)
450 Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: edges (%f %f %f to %f %f %f to %f %f %f) off back plane 1 (%f %f %f %f)\n", brush->points[0].v[0], brush->points[0].v[1], brush->points[0].v[2], brush->points[1].v[0], brush->points[1].v[1], brush->points[1].v[2], brush->points[2].v[0], brush->points[2].v[1], brush->points[2].v[2], brush->planes[1].normal[0], brush->planes[1].normal[1], brush->planes[1].normal[2], brush->planes[1].dist);
451 if (fabs(DotProduct(brush->points[2].v, brush->planes[2].normal) - brush->planes[2].dist) > 0.01f || fabs(DotProduct(brush->points[0].v, brush->planes[2].normal) - brush->planes[2].dist) > 0.01f)
452 Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: edge 0 (%f %f %f to %f %f %f) off front plane 2 (%f %f %f %f)\n", brush->points[2].v[0], brush->points[2].v[1], brush->points[2].v[2], brush->points[0].v[0], brush->points[0].v[1], brush->points[0].v[2], brush->planes[2].normal[0], brush->planes[2].normal[1], brush->planes[2].normal[2], brush->planes[2].dist);
453 if (fabs(DotProduct(brush->points[0].v, brush->planes[3].normal) - brush->planes[3].dist) > 0.01f || fabs(DotProduct(brush->points[1].v, brush->planes[3].normal) - brush->planes[3].dist) > 0.01f)
454 Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: edge 0 (%f %f %f to %f %f %f) off front plane 2 (%f %f %f %f)\n", brush->points[0].v[0], brush->points[0].v[1], brush->points[0].v[2], brush->points[1].v[0], brush->points[1].v[1], brush->points[1].v[2], brush->planes[3].normal[0], brush->planes[3].normal[1], brush->planes[3].normal[2], brush->planes[3].dist);
455 if (fabs(DotProduct(brush->points[1].v, brush->planes[4].normal) - brush->planes[4].dist) > 0.01f || fabs(DotProduct(brush->points[2].v, brush->planes[4].normal) - brush->planes[4].dist) > 0.01f)
456 Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: edge 0 (%f %f %f to %f %f %f) off front plane 2 (%f %f %f %f)\n", brush->points[1].v[0], brush->points[1].v[1], brush->points[1].v[2], brush->points[2].v[0], brush->points[2].v[1], brush->points[2].v[2], brush->planes[4].normal[0], brush->planes[4].normal[1], brush->planes[4].normal[2], brush->planes[4].dist);
462 // choose best surface normal for polygon's plane
464 for (i = 0, p = brush->points + 1;i < brush->numpoints - 2;i++, p++)
466 VectorSubtract(p[-1].v, p[0].v, edge0);
467 VectorSubtract(p[1].v, p[0].v, edge1);
468 CrossProduct(edge0, edge1, normal);
469 //TriangleNormal(p[-1].v, p[0].v, p[1].v, normal);
470 dist = DotProduct(normal, normal);
471 if (i == 0 || bestdist < dist)
474 VectorCopy(normal, brush->planes->normal);
477 if (bestdist < 0.0001f)
479 // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
480 brush->numplanes = 0;
485 brush->numplanes = brush->numpoints + 2;
486 VectorNormalize(brush->planes->normal);
487 brush->planes->dist = DotProduct(brush->points->v, brush->planes->normal);
489 // negate plane to create other side
490 VectorNegate(brush->planes[0].normal, brush->planes[1].normal);
491 brush->planes[1].dist = -brush->planes[0].dist;
492 for (i = 0, p = brush->points + (brush->numpoints - 1), p2 = brush->points;i < brush->numpoints;i++, p = p2, p2++)
494 VectorSubtract(p->v, p2->v, edge0);
495 CrossProduct(edge0, brush->planes->normal, brush->planes[i + 2].normal);
496 VectorNormalize(brush->planes[i + 2].normal);
497 brush->planes[i + 2].dist = DotProduct(p->v, brush->planes[i + 2].normal);
502 if (developer.integer >= 100)
504 // validity check - will be disabled later
505 Collision_ValidateBrush(brush);
506 for (i = 0;i < brush->numplanes;i++)
509 for (j = 0, p = brush->points;j < brush->numpoints;j++, p++)
510 if (DotProduct(p->v, brush->planes[i].normal) > brush->planes[i].dist + COLLISION_PLANE_DIST_EPSILON)
511 Con_Printf("Error in brush plane generation, plane %i\n", i);
516 colbrushf_t *Collision_AllocBrushFromPermanentPolygonFloat(mempool_t *mempool, int numpoints, float *points, int supercontents)
519 brush = (colbrushf_t *)Mem_Alloc(mempool, sizeof(colbrushf_t) + sizeof(colplanef_t) * (numpoints + 2));
520 brush->supercontents = supercontents;
521 brush->numpoints = numpoints;
522 brush->numplanes = numpoints + 2;
523 brush->planes = (colplanef_t *)(brush + 1);
524 brush->points = (colpointf_t *)points;
525 Sys_Error("Collision_AllocBrushFromPermanentPolygonFloat: FIXME: this code needs to be updated to generate a mesh...");
529 // NOTE: start and end of each brush pair must have same numplanes/numpoints
530 void Collision_TraceBrushBrushFloat(trace_t *trace, const colbrushf_t *thisbrush_start, const colbrushf_t *thisbrush_end, const colbrushf_t *thatbrush_start, const colbrushf_t *thatbrush_end)
532 int nplane, nplane2, hitq3surfaceflags = 0;
533 float enterfrac = -1, leavefrac = 1, d1, d2, f, imove, newimpactnormal[3], enterfrac2 = -1;
534 const colplanef_t *startplane, *endplane;
535 texture_t *hittexture = NULL;
537 VectorClear(newimpactnormal);
539 for (nplane = 0;nplane < thatbrush_start->numplanes + thisbrush_start->numplanes;nplane++)
542 if (nplane2 >= thatbrush_start->numplanes)
544 nplane2 -= thatbrush_start->numplanes;
545 startplane = thisbrush_start->planes + nplane2;
546 endplane = thisbrush_end->planes + nplane2;
547 if (developer.integer >= 100)
549 // any brush with degenerate planes is not worth handling
550 if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
552 Con_Print("Collision_TraceBrushBrushFloat: degenerate thisbrush plane!\n");
555 f = furthestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints);
556 if (fabs(f - startplane->dist) > COLLISION_PLANE_DIST_EPSILON)
557 Con_Printf("startplane->dist %f != calculated %f (thisbrush_start)\n", startplane->dist, f);
559 d1 = nearestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints) - furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints) - collision_startnudge.value;
560 d2 = nearestplanedist_float(endplane->normal, thisbrush_end->points, thisbrush_end->numpoints) - furthestplanedist_float(endplane->normal, thatbrush_end->points, thatbrush_end->numpoints) - collision_endnudge.value;
564 startplane = thatbrush_start->planes + nplane2;
565 endplane = thatbrush_end->planes + nplane2;
566 if (developer.integer >= 100)
568 // any brush with degenerate planes is not worth handling
569 if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
571 Con_Print("Collision_TraceBrushBrushFloat: degenerate thatbrush plane!\n");
574 f = furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints);
575 if (fabs(f - startplane->dist) > COLLISION_PLANE_DIST_EPSILON)
576 Con_Printf("startplane->dist %f != calculated %f (thatbrush_start)\n", startplane->dist, f);
578 d1 = nearestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints) - startplane->dist - collision_startnudge.value;
579 d2 = nearestplanedist_float(endplane->normal, thisbrush_end->points, thisbrush_end->numpoints) - endplane->dist - collision_endnudge.value;
581 //Con_Printf("%c%i: d1 = %f, d2 = %f, d1 / (d1 - d2) = %f\n", nplane2 != nplane ? 'b' : 'a', nplane2, d1, d2, d1 / (d1 - d2));
591 imove = 1 / (d1 - d2);
592 f = (d1 - collision_enternudge.value) * imove;
593 // check if this will reduce the collision time range
596 // reduced collision time range
598 // if the collision time range is now empty, no collision
599 if (enterfrac > leavefrac)
601 // if the collision would be further away than the trace's
602 // existing collision data, we don't care about this
604 if (enterfrac > trace->realfraction)
606 // calculate the nudged fraction and impact normal we'll
607 // need if we accept this collision later
608 enterfrac2 = f - collision_impactnudge.value * imove;
609 VectorLerp(startplane->normal, enterfrac, endplane->normal, newimpactnormal);
610 hitq3surfaceflags = startplane->q3surfaceflags;
611 hittexture = startplane->texture;
617 // moving out of brush
623 f = (d1 + collision_leavenudge.value) / (d1 - d2);
624 // check if this will reduce the collision time range
627 // reduced collision time range
629 // if the collision time range is now empty, no collision
630 if (enterfrac > leavefrac)
637 // at this point we know the trace overlaps the brush because it was not
638 // rejected at any point in the loop above
640 // see if this brush can block the trace or not according to contents
641 if (trace->hitsupercontentsmask & thatbrush_start->supercontents)
645 trace->startsupercontents |= thatbrush_start->supercontents;
646 trace->startsolid = true;
648 trace->allsolid = true;
650 // store out the impact information
651 trace->hitsupercontents = thatbrush_start->supercontents;
652 trace->hitq3surfaceflags = hitq3surfaceflags;
653 trace->hittexture = hittexture;
654 trace->realfraction = bound(0, enterfrac, 1);
655 trace->fraction = bound(0, enterfrac2, 1);
656 VectorCopy(newimpactnormal, trace->plane.normal);
660 // this brush can not block the trace, but it can update start contents
662 trace->startsupercontents |= thatbrush_start->supercontents;
666 // NOTE: start and end brush pair must have same numplanes/numpoints
667 void Collision_TraceLineBrushFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, const colbrushf_t *thatbrush_start, const colbrushf_t *thatbrush_end)
669 int nplane, hitq3surfaceflags = 0;
670 float enterfrac = -1, leavefrac = 1, d1, d2, f, imove, newimpactnormal[3], enterfrac2 = -1;
671 const colplanef_t *startplane, *endplane;
672 texture_t *hittexture = NULL;
674 VectorClear(newimpactnormal);
676 for (nplane = 0;nplane < thatbrush_start->numplanes;nplane++)
678 startplane = thatbrush_start->planes + nplane;
679 endplane = thatbrush_end->planes + nplane;
680 d1 = DotProduct(startplane->normal, linestart) - startplane->dist - collision_startnudge.value;
681 d2 = DotProduct(endplane->normal, lineend) - endplane->dist - collision_endnudge.value;
682 if (developer.integer >= 100)
684 // any brush with degenerate planes is not worth handling
685 if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
687 Con_Print("Collision_TraceLineBrushFloat: degenerate plane!\n");
690 if (thatbrush_start->numpoints)
692 f = furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints);
693 if (fabs(f - startplane->dist) > COLLISION_PLANE_DIST_EPSILON)
694 Con_Printf("startplane->dist %f != calculated %f\n", startplane->dist, f);
706 imove = 1 / (d1 - d2);
707 f = (d1 - collision_enternudge.value) * imove;
708 // check if this will reduce the collision time range
711 // reduced collision time range
713 // if the collision time range is now empty, no collision
714 if (enterfrac > leavefrac)
716 // if the collision would be further away than the trace's
717 // existing collision data, we don't care about this
719 if (enterfrac > trace->realfraction)
721 // calculate the nudged fraction and impact normal we'll
722 // need if we accept this collision later
723 enterfrac2 = f - collision_impactnudge.value * imove;
724 VectorLerp(startplane->normal, enterfrac, endplane->normal, newimpactnormal);
725 hitq3surfaceflags = startplane->q3surfaceflags;
726 hittexture = startplane->texture;
732 // moving out of brush
738 f = (d1 + collision_leavenudge.value) / (d1 - d2);
739 // check if this will reduce the collision time range
742 // reduced collision time range
744 // if the collision time range is now empty, no collision
745 if (enterfrac > leavefrac)
752 // at this point we know the trace overlaps the brush because it was not
753 // rejected at any point in the loop above
755 // see if this brush can block the trace or not according to contents
756 if (trace->hitsupercontentsmask & thatbrush_start->supercontents)
760 trace->startsupercontents |= thatbrush_start->supercontents;
761 trace->startsolid = true;
763 trace->allsolid = true;
765 // store out the impact information
766 trace->hitsupercontents = thatbrush_start->supercontents;
767 trace->hitq3surfaceflags = hitq3surfaceflags;
768 trace->hittexture = hittexture;
769 trace->realfraction = bound(0, enterfrac, 1);
770 trace->fraction = bound(0, enterfrac2, 1);
771 VectorCopy(newimpactnormal, trace->plane.normal);
775 // this brush can not block the trace, but it can update start contents
777 trace->startsupercontents |= thatbrush_start->supercontents;
781 void Collision_TracePointBrushFloat(trace_t *trace, const vec3_t point, const colbrushf_t *thatbrush)
784 const colplanef_t *plane;
786 for (nplane = 0, plane = thatbrush->planes;nplane < thatbrush->numplanes;nplane++, plane++)
787 if (DotProduct(plane->normal, point) > plane->dist)
790 trace->startsupercontents |= thatbrush->supercontents;
791 if (trace->hitsupercontentsmask & thatbrush->supercontents)
793 trace->startsolid = true;
794 trace->allsolid = true;
798 static colpointf_t polyf_points[256];
799 static colplanef_t polyf_planes[256 + 2];
800 static colbrushf_t polyf_brush;
802 void Collision_SnapCopyPoints(int numpoints, const colpointf_t *in, colpointf_t *out, float fractionprecision, float invfractionprecision)
806 out->v[0] = floor(in->v[0] * fractionprecision + 0.5f) * invfractionprecision;
807 out->v[1] = floor(in->v[1] * fractionprecision + 0.5f) * invfractionprecision;
808 out->v[2] = floor(in->v[2] * fractionprecision + 0.5f) * invfractionprecision;
812 void Collision_TraceBrushPolygonFloat(trace_t *trace, const colbrushf_t *thisbrush_start, const colbrushf_t *thisbrush_end, int numpoints, const float *points, int supercontents)
816 Con_Print("Polygon with more than 256 points not supported yet (fixme!)\n");
819 polyf_brush.numpoints = numpoints;
820 polyf_brush.numplanes = numpoints + 2;
821 //polyf_brush.points = (colpointf_t *)points;
822 polyf_brush.planes = polyf_planes;
823 polyf_brush.supercontents = supercontents;
824 polyf_brush.points = polyf_points;
825 Collision_SnapCopyPoints(numpoints, (colpointf_t *)points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
826 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
827 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
828 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brush, &polyf_brush);
831 void Collision_TraceBrushTriangleMeshFloat(trace_t *trace, const colbrushf_t *thisbrush_start, const colbrushf_t *thisbrush_end, int numtriangles, const int *element3i, const float *vertex3f, int supercontents, int q3surfaceflags, texture_t *texture, const vec3_t segmentmins, const vec3_t segmentmaxs)
834 float facemins[3], facemaxs[3];
835 polyf_brush.numpoints = 3;
836 polyf_brush.numplanes = 5;
837 polyf_brush.points = polyf_points;
838 polyf_brush.planes = polyf_planes;
839 polyf_brush.supercontents = supercontents;
840 for (i = 0;i < polyf_brush.numplanes;i++)
842 polyf_brush.planes[i].q3surfaceflags = q3surfaceflags;
843 polyf_brush.planes[i].texture = texture;
845 for (i = 0;i < numtriangles;i++, element3i += 3)
847 VectorCopy(vertex3f + element3i[0] * 3, polyf_points[0].v);
848 VectorCopy(vertex3f + element3i[1] * 3, polyf_points[1].v);
849 VectorCopy(vertex3f + element3i[2] * 3, polyf_points[2].v);
850 Collision_SnapCopyPoints(3, polyf_points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
851 facemins[0] = min(polyf_points[0].v[0], min(polyf_points[1].v[0], polyf_points[2].v[0]));
852 facemins[1] = min(polyf_points[0].v[1], min(polyf_points[1].v[1], polyf_points[2].v[1]));
853 facemins[2] = min(polyf_points[0].v[2], min(polyf_points[1].v[2], polyf_points[2].v[2]));
854 facemaxs[0] = max(polyf_points[0].v[0], max(polyf_points[1].v[0], polyf_points[2].v[0]));
855 facemaxs[1] = max(polyf_points[0].v[1], max(polyf_points[1].v[1], polyf_points[2].v[1]));
856 facemaxs[2] = max(polyf_points[0].v[2], max(polyf_points[1].v[2], polyf_points[2].v[2]));
857 if (BoxesOverlap(segmentmins, segmentmaxs, facemins, facemaxs))
859 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
860 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
861 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brush, &polyf_brush);
866 void Collision_TraceLinePolygonFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, int numpoints, const float *points, int supercontents)
870 Con_Print("Polygon with more than 256 points not supported yet (fixme!)\n");
873 polyf_brush.numpoints = numpoints;
874 polyf_brush.numplanes = numpoints + 2;
875 //polyf_brush.points = (colpointf_t *)points;
876 polyf_brush.points = polyf_points;
877 Collision_SnapCopyPoints(numpoints, (colpointf_t *)points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
878 polyf_brush.planes = polyf_planes;
879 polyf_brush.supercontents = supercontents;
880 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
881 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
882 Collision_TraceLineBrushFloat(trace, linestart, lineend, &polyf_brush, &polyf_brush);
885 void Collision_TraceLineTriangleMeshFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, int numtriangles, const int *element3i, const float *vertex3f, int supercontents, int q3surfaceflags, texture_t *texture, const vec3_t segmentmins, const vec3_t segmentmaxs)
889 // FIXME: snap vertices?
890 for (i = 0;i < numtriangles;i++, element3i += 3)
891 Collision_TraceLineTriangleFloat(trace, linestart, lineend, vertex3f + element3i[0] * 3, vertex3f + element3i[1] * 3, vertex3f + element3i[2] * 3, supercontents, q3surfaceflags, texture);
893 polyf_brush.numpoints = 3;
894 polyf_brush.numplanes = 5;
895 polyf_brush.points = polyf_points;
896 polyf_brush.planes = polyf_planes;
897 polyf_brush.supercontents = supercontents;
898 for (i = 0;i < polyf_brush.numplanes;i++)
900 polyf_brush.planes[i].supercontents = supercontents;
901 polyf_brush.planes[i].q3surfaceflags = q3surfaceflags;
902 polyf_brush.planes[i].texture = texture;
904 for (i = 0;i < numtriangles;i++, element3i += 3)
906 float facemins[3], facemaxs[3];
907 VectorCopy(vertex3f + element3i[0] * 3, polyf_points[0].v);
908 VectorCopy(vertex3f + element3i[1] * 3, polyf_points[1].v);
909 VectorCopy(vertex3f + element3i[2] * 3, polyf_points[2].v);
910 Collision_SnapCopyPoints(numpoints, polyf_points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
911 facemins[0] = min(polyf_points[0].v[0], min(polyf_points[1].v[0], polyf_points[2].v[0]));
912 facemins[1] = min(polyf_points[0].v[1], min(polyf_points[1].v[1], polyf_points[2].v[1]));
913 facemins[2] = min(polyf_points[0].v[2], min(polyf_points[1].v[2], polyf_points[2].v[2]));
914 facemaxs[0] = max(polyf_points[0].v[0], max(polyf_points[1].v[0], polyf_points[2].v[0]));
915 facemaxs[1] = max(polyf_points[0].v[1], max(polyf_points[1].v[1], polyf_points[2].v[1]));
916 facemaxs[2] = max(polyf_points[0].v[2], max(polyf_points[1].v[2], polyf_points[2].v[2]));
917 if (BoxesOverlap(segmentmins, segmentmaxs, facemins, facemaxs))
919 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
920 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
921 Collision_TraceLineBrushFloat(trace, linestart, lineend, &polyf_brush, &polyf_brush);
928 static colpointf_t polyf_pointsstart[256], polyf_pointsend[256];
929 static colplanef_t polyf_planesstart[256 + 2], polyf_planesend[256 + 2];
930 static colbrushf_t polyf_brushstart, polyf_brushend;
932 void Collision_TraceBrushPolygonTransformFloat(trace_t *trace, const colbrushf_t *thisbrush_start, const colbrushf_t *thisbrush_end, int numpoints, const float *points, const matrix4x4_t *polygonmatrixstart, const matrix4x4_t *polygonmatrixend, int supercontents, int q3surfaceflags, texture_t *texture)
937 Con_Print("Polygon with more than 256 points not supported yet (fixme!)\n");
940 polyf_brushstart.numpoints = numpoints;
941 polyf_brushstart.numplanes = numpoints + 2;
942 polyf_brushstart.points = polyf_pointsstart;//(colpointf_t *)points;
943 polyf_brushstart.planes = polyf_planesstart;
944 polyf_brushstart.supercontents = supercontents;
945 for (i = 0;i < numpoints;i++)
946 Matrix4x4_Transform(polygonmatrixstart, points + i * 3, polyf_brushstart.points[i].v);
947 polyf_brushend.numpoints = numpoints;
948 polyf_brushend.numplanes = numpoints + 2;
949 polyf_brushend.points = polyf_pointsend;//(colpointf_t *)points;
950 polyf_brushend.planes = polyf_planesend;
951 polyf_brushend.supercontents = supercontents;
952 for (i = 0;i < numpoints;i++)
953 Matrix4x4_Transform(polygonmatrixend, points + i * 3, polyf_brushend.points[i].v);
954 for (i = 0;i < polyf_brushstart.numplanes;i++)
956 polyf_brushstart.planes[i].q3surfaceflags = q3surfaceflags;
957 polyf_brushstart.planes[i].texture = texture;
959 Collision_SnapCopyPoints(numpoints, polyf_pointsstart, polyf_pointsstart, COLLISION_SNAPSCALE, COLLISION_SNAP);
960 Collision_SnapCopyPoints(numpoints, polyf_pointsend, polyf_pointsend, COLLISION_SNAPSCALE, COLLISION_SNAP);
961 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brushstart);
962 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brushend);
964 //Collision_PrintBrushAsQHull(&polyf_brushstart, "polyf_brushstart");
965 //Collision_PrintBrushAsQHull(&polyf_brushend, "polyf_brushend");
967 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brushstart, &polyf_brushend);
972 #define MAX_BRUSHFORBOX 16
973 static int brushforbox_index = 0;
974 static colpointf_t brushforbox_point[MAX_BRUSHFORBOX*8];
975 static colplanef_t brushforbox_plane[MAX_BRUSHFORBOX*6];
976 static colbrushf_t brushforbox_brush[MAX_BRUSHFORBOX];
977 static colbrushf_t brushforpoint_brush[MAX_BRUSHFORBOX];
979 void Collision_InitBrushForBox(void)
982 for (i = 0;i < MAX_BRUSHFORBOX;i++)
984 brushforbox_brush[i].numpoints = 8;
985 brushforbox_brush[i].numplanes = 6;
986 brushforbox_brush[i].points = brushforbox_point + i * 8;
987 brushforbox_brush[i].planes = brushforbox_plane + i * 6;
988 brushforpoint_brush[i].numpoints = 1;
989 brushforpoint_brush[i].numplanes = 0;
990 brushforpoint_brush[i].points = brushforbox_point + i * 8;
991 brushforpoint_brush[i].planes = brushforbox_plane + i * 6;
995 colbrushf_t *Collision_BrushForBox(const matrix4x4_t *matrix, const vec3_t mins, const vec3_t maxs, int supercontents, int q3surfaceflags, texture_t *texture)
1000 if (brushforbox_brush[0].numpoints == 0)
1001 Collision_InitBrushForBox();
1002 // FIXME: these probably don't actually need to be normalized if the collision code does not care
1003 if (VectorCompare(mins, maxs))
1006 brush = brushforpoint_brush + ((brushforbox_index++) % MAX_BRUSHFORBOX);
1007 VectorCopy(mins, brush->points->v);
1011 brush = brushforbox_brush + ((brushforbox_index++) % MAX_BRUSHFORBOX);
1013 for (i = 0;i < 8;i++)
1015 v[0] = i & 1 ? maxs[0] : mins[0];
1016 v[1] = i & 2 ? maxs[1] : mins[1];
1017 v[2] = i & 4 ? maxs[2] : mins[2];
1018 Matrix4x4_Transform(matrix, v, brush->points[i].v);
1021 for (i = 0;i < 6;i++)
1024 v[i >> 1] = i & 1 ? 1 : -1;
1025 Matrix4x4_Transform3x3(matrix, v, brush->planes[i].normal);
1026 VectorNormalize(brush->planes[i].normal);
1029 brush->supercontents = supercontents;
1030 for (j = 0;j < brush->numplanes;j++)
1032 brush->planes[j].q3surfaceflags = q3surfaceflags;
1033 brush->planes[j].texture = texture;
1034 brush->planes[j].dist = furthestplanedist_float(brush->planes[j].normal, brush->points, brush->numpoints);
1036 VectorCopy(brush->points[0].v, brush->mins);
1037 VectorCopy(brush->points[0].v, brush->maxs);
1038 for (j = 1;j < brush->numpoints;j++)
1040 brush->mins[0] = min(brush->mins[0], brush->points[j].v[0]);
1041 brush->mins[1] = min(brush->mins[1], brush->points[j].v[1]);
1042 brush->mins[2] = min(brush->mins[2], brush->points[j].v[2]);
1043 brush->maxs[0] = max(brush->maxs[0], brush->points[j].v[0]);
1044 brush->maxs[1] = max(brush->maxs[1], brush->points[j].v[1]);
1045 brush->maxs[2] = max(brush->maxs[2], brush->points[j].v[2]);
1047 brush->mins[0] -= 1;
1048 brush->mins[1] -= 1;
1049 brush->mins[2] -= 1;
1050 brush->maxs[0] += 1;
1051 brush->maxs[1] += 1;
1052 brush->maxs[2] += 1;
1053 Collision_ValidateBrush(brush);
1057 void Collision_ClipTrace_BrushBox(trace_t *trace, const vec3_t cmins, const vec3_t cmaxs, const vec3_t start, const vec3_t mins, const vec3_t maxs, const vec3_t end, int hitsupercontentsmask, int supercontents, int q3surfaceflags, texture_t *texture)
1059 colbrushf_t *boxbrush, *thisbrush_start, *thisbrush_end;
1060 vec3_t startmins, startmaxs, endmins, endmaxs;
1062 // create brushes for the collision
1063 VectorAdd(start, mins, startmins);
1064 VectorAdd(start, maxs, startmaxs);
1065 VectorAdd(end, mins, endmins);
1066 VectorAdd(end, maxs, endmaxs);
1067 boxbrush = Collision_BrushForBox(&identitymatrix, cmins, cmaxs, supercontents, q3surfaceflags, texture);
1068 thisbrush_start = Collision_BrushForBox(&identitymatrix, startmins, startmaxs, 0, 0, NULL);
1069 thisbrush_end = Collision_BrushForBox(&identitymatrix, endmins, endmaxs, 0, 0, NULL);
1071 memset(trace, 0, sizeof(trace_t));
1072 trace->hitsupercontentsmask = hitsupercontentsmask;
1073 trace->fraction = 1;
1074 trace->realfraction = 1;
1075 trace->allsolid = true;
1076 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, boxbrush, boxbrush);
1079 //pseudocode for detecting line/sphere overlap without calculating an impact point
1080 //linesphereorigin = sphereorigin - linestart;linediff = lineend - linestart;linespherefrac = DotProduct(linesphereorigin, linediff) / DotProduct(linediff, linediff);return VectorLength2(linesphereorigin - bound(0, linespherefrac, 1) * linediff) >= sphereradius*sphereradius;
1082 // LordHavoc: currently unused, but tested
1083 // note: this can be used for tracing a moving sphere vs a stationary sphere,
1084 // by simply adding the moving sphere's radius to the sphereradius parameter,
1085 // all the results are correct (impactpoint, impactnormal, and fraction)
1086 float Collision_ClipTrace_Line_Sphere(double *linestart, double *lineend, double *sphereorigin, double sphereradius, double *impactpoint, double *impactnormal)
1088 double dir[3], scale, v[3], deviationdist, impactdist, linelength;
1089 // make sure the impactpoint and impactnormal are valid even if there is
1091 VectorCopy(lineend, impactpoint);
1092 VectorClear(impactnormal);
1093 // calculate line direction
1094 VectorSubtract(lineend, linestart, dir);
1095 // normalize direction
1096 linelength = VectorLength(dir);
1099 scale = 1.0 / linelength;
1100 VectorScale(dir, scale, dir);
1102 // this dotproduct calculates the distance along the line at which the
1103 // sphere origin is (nearest point to the sphere origin on the line)
1104 impactdist = DotProduct(sphereorigin, dir) - DotProduct(linestart, dir);
1105 // calculate point on line at that distance, and subtract the
1106 // sphereorigin from it, so we have a vector to measure for the distance
1107 // of the line from the sphereorigin (deviation, how off-center it is)
1108 VectorMA(linestart, impactdist, dir, v);
1109 VectorSubtract(v, sphereorigin, v);
1110 deviationdist = VectorLength2(v);
1111 // if outside the radius, it's a miss for sure
1112 // (we do this comparison using squared radius to avoid a sqrt)
1113 if (deviationdist > sphereradius*sphereradius)
1114 return 1; // miss (off to the side)
1115 // nudge back to find the correct impact distance
1116 impactdist += deviationdist - sphereradius;
1117 if (impactdist >= linelength)
1118 return 1; // miss (not close enough)
1120 return 1; // miss (linestart is past or inside sphere)
1121 // calculate new impactpoint
1122 VectorMA(linestart, impactdist, dir, impactpoint);
1123 // calculate impactnormal (surface normal at point of impact)
1124 VectorSubtract(impactpoint, sphereorigin, impactnormal);
1125 // normalize impactnormal
1126 VectorNormalize(impactnormal);
1127 // return fraction of movement distance
1128 return impactdist / linelength;
1131 void Collision_TraceLineTriangleFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, const float *point0, const float *point1, const float *point2, int supercontents, int q3surfaceflags, texture_t *texture)
1135 float d1, d2, d, f, impact[3], edgenormal[3], faceplanenormal[3], faceplanedist, faceplanenormallength2, edge01[3], edge21[3], edge02[3];
1137 // this function executes:
1138 // 32 ops when line starts behind triangle
1139 // 38 ops when line ends infront of triangle
1140 // 43 ops when line fraction is already closer than this triangle
1141 // 72 ops when line is outside edge 01
1142 // 92 ops when line is outside edge 21
1143 // 115 ops when line is outside edge 02
1144 // 123 ops when line impacts triangle and updates trace results
1146 // this code is designed for clockwise triangles, conversion to
1147 // counterclockwise would require swapping some things around...
1148 // it is easier to simply swap the point0 and point2 parameters to this
1149 // function when calling it than it is to rewire the internals.
1151 // calculate the faceplanenormal of the triangle, this represents the front side
1153 VectorSubtract(point0, point1, edge01);
1154 VectorSubtract(point2, point1, edge21);
1155 CrossProduct(edge01, edge21, faceplanenormal);
1156 // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
1158 faceplanenormallength2 = DotProduct(faceplanenormal, faceplanenormal);
1159 if (faceplanenormallength2 < 0.0001f)
1161 // calculate the distance
1163 faceplanedist = DotProduct(point0, faceplanenormal);
1165 // if start point is on the back side there is no collision
1166 // (we don't care about traces going through the triangle the wrong way)
1168 // calculate the start distance
1170 d1 = DotProduct(faceplanenormal, linestart);
1171 if (d1 <= faceplanedist)
1174 // calculate the end distance
1176 d2 = DotProduct(faceplanenormal, lineend);
1177 // if both are in front, there is no collision
1178 if (d2 >= faceplanedist)
1181 // from here on we know d1 is >= 0 and d2 is < 0
1182 // this means the line starts infront and ends behind, passing through it
1184 // calculate the recipricol of the distance delta,
1185 // so we can use it multiple times cheaply (instead of division)
1187 d = 1.0f / (d1 - d2);
1188 // calculate the impact fraction by taking the start distance (> 0)
1189 // and subtracting the face plane distance (this is the distance of the
1190 // triangle along that same normal)
1191 // then multiply by the recipricol distance delta
1193 f = (d1 - faceplanedist) * d;
1194 // skip out if this impact is further away than previous ones
1196 if (f > trace->realfraction)
1198 // calculate the perfect impact point for classification of insidedness
1200 impact[0] = linestart[0] + f * (lineend[0] - linestart[0]);
1201 impact[1] = linestart[1] + f * (lineend[1] - linestart[1]);
1202 impact[2] = linestart[2] + f * (lineend[2] - linestart[2]);
1204 // calculate the edge normal and reject if impact is outside triangle
1205 // (an edge normal faces away from the triangle, to get the desired normal
1206 // a crossproduct with the faceplanenormal is used, and because of the way
1207 // the insidedness comparison is written it does not need to be normalized)
1209 // first use the two edges from the triangle plane math
1210 // the other edge only gets calculated if the point survives that long
1213 CrossProduct(edge01, faceplanenormal, edgenormal);
1214 if (DotProduct(impact, edgenormal) > DotProduct(point1, edgenormal))
1218 CrossProduct(faceplanenormal, edge21, edgenormal);
1219 if (DotProduct(impact, edgenormal) > DotProduct(point2, edgenormal))
1223 VectorSubtract(point0, point2, edge02);
1224 CrossProduct(faceplanenormal, edge02, edgenormal);
1225 if (DotProduct(impact, edgenormal) > DotProduct(point0, edgenormal))
1230 // store the new trace fraction
1231 trace->realfraction = f;
1233 // calculate a nudged fraction to keep it out of the surface
1234 // (the main fraction remains perfect)
1235 trace->fraction = f - collision_impactnudge.value * d;
1237 // store the new trace plane (because collisions only happen from
1238 // the front this is always simply the triangle normal, never flipped)
1239 d = 1.0 / sqrt(faceplanenormallength2);
1240 VectorScale(faceplanenormal, d, trace->plane.normal);
1241 trace->plane.dist = faceplanedist * d;
1243 trace->hitsupercontents = supercontents;
1244 trace->hitq3surfaceflags = q3surfaceflags;
1245 trace->hittexture = texture;
1247 float d1, d2, d, f, fnudged, impact[3], edgenormal[3], faceplanenormal[3], faceplanedist, edge[3];
1249 // this code is designed for clockwise triangles, conversion to
1250 // counterclockwise would require swapping some things around...
1251 // it is easier to simply swap the point0 and point2 parameters to this
1252 // function when calling it than it is to rewire the internals.
1254 // calculate the unnormalized faceplanenormal of the triangle,
1255 // this represents the front side
1256 TriangleNormal(point0, point1, point2, faceplanenormal);
1257 // there's no point in processing a degenerate triangle
1258 // (GIGO - Garbage In, Garbage Out)
1259 if (DotProduct(faceplanenormal, faceplanenormal) < 0.0001f)
1261 // calculate the unnormalized distance
1262 faceplanedist = DotProduct(point0, faceplanenormal);
1264 // calculate the unnormalized start distance
1265 d1 = DotProduct(faceplanenormal, linestart) - faceplanedist;
1266 // if start point is on the back side there is no collision
1267 // (we don't care about traces going through the triangle the wrong way)
1271 // calculate the unnormalized end distance
1272 d2 = DotProduct(faceplanenormal, lineend) - faceplanedist;
1273 // if both are in front, there is no collision
1277 // from here on we know d1 is >= 0 and d2 is < 0
1278 // this means the line starts infront and ends behind, passing through it
1280 // calculate the recipricol of the distance delta,
1281 // so we can use it multiple times cheaply (instead of division)
1282 d = 1.0f / (d1 - d2);
1283 // calculate the impact fraction by taking the start distance (> 0)
1284 // and subtracting the face plane distance (this is the distance of the
1285 // triangle along that same normal)
1286 // then multiply by the recipricol distance delta
1288 // skip out if this impact is further away than previous ones
1289 if (f > trace->realfraction)
1291 // calculate the perfect impact point for classification of insidedness
1292 impact[0] = linestart[0] + f * (lineend[0] - linestart[0]);
1293 impact[1] = linestart[1] + f * (lineend[1] - linestart[1]);
1294 impact[2] = linestart[2] + f * (lineend[2] - linestart[2]);
1296 // calculate the edge normal and reject if impact is outside triangle
1297 // (an edge normal faces away from the triangle, to get the desired normal
1298 // a crossproduct with the faceplanenormal is used, and because of the way
1299 // the insidedness comparison is written it does not need to be normalized)
1301 VectorSubtract(point2, point0, edge);
1302 CrossProduct(edge, faceplanenormal, edgenormal);
1303 if (DotProduct(impact, edgenormal) > DotProduct(point0, edgenormal))
1306 VectorSubtract(point0, point1, edge);
1307 CrossProduct(edge, faceplanenormal, edgenormal);
1308 if (DotProduct(impact, edgenormal) > DotProduct(point1, edgenormal))
1311 VectorSubtract(point1, point2, edge);
1312 CrossProduct(edge, faceplanenormal, edgenormal);
1313 if (DotProduct(impact, edgenormal) > DotProduct(point2, edgenormal))
1316 // store the new trace fraction
1317 trace->realfraction = bound(0, f, 1);
1319 // store the new trace plane (because collisions only happen from
1320 // the front this is always simply the triangle normal, never flipped)
1321 VectorNormalize(faceplanenormal);
1322 VectorCopy(faceplanenormal, trace->plane.normal);
1323 trace->plane.dist = DotProduct(point0, faceplanenormal);
1325 // calculate the normalized start and end distances
1326 d1 = DotProduct(trace->plane.normal, linestart) - trace->plane.dist;
1327 d2 = DotProduct(trace->plane.normal, lineend) - trace->plane.dist;
1329 // calculate a nudged fraction to keep it out of the surface
1330 // (the main fraction remains perfect)
1331 fnudged = (d1 - collision_impactnudge.value) / (d1 - d2);
1332 trace->fraction = bound(0, fnudged, 1);
1334 // store the new trace endpos
1335 // not needed, it's calculated later when the trace is finished
1336 //trace->endpos[0] = linestart[0] + fnudged * (lineend[0] - linestart[0]);
1337 //trace->endpos[1] = linestart[1] + fnudged * (lineend[1] - linestart[1]);
1338 //trace->endpos[2] = linestart[2] + fnudged * (lineend[2] - linestart[2]);
1339 trace->hitsupercontents = supercontents;
1340 trace->hitq3surfaceflags = q3surfaceflags;
1341 trace->hittexture = texture;
1345 typedef struct colbspnode_s
1348 struct colbspnode_s *children[2];
1349 // the node is reallocated or split if max is reached
1352 colbrushf_t **colbrushflist;
1355 //colbrushd_t **colbrushdlist;
1359 typedef struct colbsp_s
1362 colbspnode_t *nodes;
1366 colbsp_t *Collision_CreateCollisionBSP(mempool_t *mempool)
1369 bsp = (colbsp_t *)Mem_Alloc(mempool, sizeof(colbsp_t));
1370 bsp->mempool = mempool;
1371 bsp->nodes = (colbspnode_t *)Mem_Alloc(bsp->mempool, sizeof(colbspnode_t));
1375 void Collision_FreeCollisionBSPNode(colbspnode_t *node)
1377 if (node->children[0])
1378 Collision_FreeCollisionBSPNode(node->children[0]);
1379 if (node->children[1])
1380 Collision_FreeCollisionBSPNode(node->children[1]);
1381 while (--node->numcolbrushf)
1382 Mem_Free(node->colbrushflist[node->numcolbrushf]);
1383 //while (--node->numcolbrushd)
1384 // Mem_Free(node->colbrushdlist[node->numcolbrushd]);
1388 void Collision_FreeCollisionBSP(colbsp_t *bsp)
1390 Collision_FreeCollisionBSPNode(bsp->nodes);
1394 void Collision_BoundingBoxOfBrushTraceSegment(const colbrushf_t *start, const colbrushf_t *end, vec3_t mins, vec3_t maxs, float startfrac, float endfrac)
1397 colpointf_t *ps, *pe;
1398 float tempstart[3], tempend[3];
1399 VectorLerp(start->points[0].v, startfrac, end->points[0].v, mins);
1400 VectorCopy(mins, maxs);
1401 for (i = 0, ps = start->points, pe = end->points;i < start->numpoints;i++, ps++, pe++)
1403 VectorLerp(ps->v, startfrac, pe->v, tempstart);
1404 VectorLerp(ps->v, endfrac, pe->v, tempend);
1405 mins[0] = min(mins[0], min(tempstart[0], tempend[0]));
1406 mins[1] = min(mins[1], min(tempstart[1], tempend[1]));
1407 mins[2] = min(mins[2], min(tempstart[2], tempend[2]));
1408 maxs[0] = min(maxs[0], min(tempstart[0], tempend[0]));
1409 maxs[1] = min(maxs[1], min(tempstart[1], tempend[1]));
1410 maxs[2] = min(maxs[2], min(tempstart[2], tempend[2]));