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;
114 bestdist = DotProduct(points->v, normal);
118 dist = DotProduct(points->v, normal);
119 bestdist = min(bestdist, dist);
125 float furthestplanedist_float(const float *normal, const colpointf_t *points, int numpoints)
127 float dist, bestdist;
130 bestdist = DotProduct(points->v, normal);
134 dist = DotProduct(points->v, normal);
135 bestdist = max(bestdist, dist);
142 colbrushf_t *Collision_NewBrushFromPlanes(mempool_t *mempool, int numoriginalplanes, const colplanef_t *originalplanes, int supercontents)
144 // TODO: planesbuf could be replaced by a remapping table
145 int j, k, m, w, xyzflags;
146 int numpointsbuf = 0, maxpointsbuf = 256, numplanesbuf = 0, maxplanesbuf = 256, numelementsbuf = 0, maxelementsbuf = 256;
149 colpointf_t pointsbuf[256];
150 colplanef_t planesbuf[256];
151 int elementsbuf[1024];
152 int polypointbuf[256];
157 // enable these if debugging to avoid seeing garbage in unused data
158 memset(pointsbuf, 0, sizeof(pointsbuf));
159 memset(planesbuf, 0, sizeof(planesbuf));
160 memset(elementsbuf, 0, sizeof(elementsbuf));
161 memset(polypointbuf, 0, sizeof(polypointbuf));
162 memset(p, 0, sizeof(p));
164 // figure out how large a bounding box we need to properly compute this brush
166 for (j = 0;j < numoriginalplanes;j++)
167 maxdist = max(maxdist, fabs(originalplanes[j].dist));
168 // now make it large enough to enclose the entire brush, and round it off to a reasonable multiple of 1024
169 maxdist = floor(maxdist * (4.0 / 1024.0) + 2) * 1024.0;
170 // construct a collision brush (points, planes, and renderable mesh) from
171 // a set of planes, this also optimizes out any unnecessary planes (ones
172 // whose polygon is clipped away by the other planes)
173 for (j = 0;j < numoriginalplanes;j++)
175 // add the plane uniquely (no duplicates)
176 for (k = 0;k < numplanesbuf;k++)
177 if (VectorCompare(planesbuf[k].normal, originalplanes[j].normal) && planesbuf[k].dist == originalplanes[j].dist)
179 // if the plane is a duplicate, skip it
180 if (k < numplanesbuf)
182 // check if there are too many and skip the brush
183 if (numplanesbuf >= maxplanesbuf)
185 Con_DPrint("Collision_NewBrushFromPlanes: failed to build collision brush: too many planes for buffer\n");
190 VectorCopy(originalplanes[j].normal, planesbuf[numplanesbuf].normal);
191 planesbuf[numplanesbuf].dist = originalplanes[j].dist;
192 planesbuf[numplanesbuf].q3surfaceflags = originalplanes[j].q3surfaceflags;
193 planesbuf[numplanesbuf].texture = originalplanes[j].texture;
196 // create a large polygon from the plane
198 PolygonD_QuadForPlane(p[w], originalplanes[j].normal[0], originalplanes[j].normal[1], originalplanes[j].normal[2], originalplanes[j].dist, maxdist);
200 // clip it by all other planes
201 for (k = 0;k < numoriginalplanes && pnumpoints >= 3 && pnumpoints <= pmaxpoints;k++)
203 // skip the plane this polygon
204 // (nothing happens if it is processed, this is just an optimization)
207 // we want to keep the inside of the brush plane so we flip
209 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);
214 // if nothing is left, skip it
217 //Con_DPrintf("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);
221 for (k = 0;k < pnumpoints;k++)
225 for (l = 0;l < numoriginalplanes;l++)
226 if (fabs(DotProduct(&p[w][k*3], originalplanes[l].normal) - originalplanes[l].dist) < COLLISION_PLANE_DIST_EPSILON)
233 Con_DPrintf("Collision_NewBrushFromPlanes: warning: polygon point does not lie on at least 3 planes\n");
237 // check if there are too many polygon vertices for buffer
238 if (pnumpoints > pmaxpoints)
240 Con_DPrint("Collision_NewBrushFromPlanes: failed to build collision brush: too many points for buffer\n");
244 // check if there are too many triangle elements for buffer
245 if (numelementsbuf + (pnumpoints - 2) * 3 > maxelementsbuf)
247 Con_DPrint("Collision_NewBrushFromPlanes: failed to build collision brush: too many triangle elements for buffer\n");
251 for (k = 0;k < pnumpoints;k++)
254 // downgrade to float precision before comparing
255 VectorCopy(&p[w][k*3], v);
257 // check if there is already a matching point (no duplicates)
258 for (m = 0;m < numpointsbuf;m++)
259 if (VectorDistance2(v, pointsbuf[m].v) < COLLISION_SNAP2)
262 // if there is no match, add a new one
263 if (m == numpointsbuf)
265 // check if there are too many and skip the brush
266 if (numpointsbuf >= maxpointsbuf)
268 Con_DPrint("Collision_NewBrushFromPlanes: failed to build collision brush: too many points for buffer\n");
272 VectorCopy(&p[w][k*3], pointsbuf[numpointsbuf].v);
276 // store the index into a buffer
280 // add the triangles for the polygon
281 // (this particular code makes a triangle fan)
282 for (k = 0;k < pnumpoints - 2;k++)
284 elementsbuf[numelementsbuf++] = polypointbuf[0];
285 elementsbuf[numelementsbuf++] = polypointbuf[k + 1];
286 elementsbuf[numelementsbuf++] = polypointbuf[k + 2];
290 // if nothing is left, there's nothing to allocate
291 if (numplanesbuf < 4)
293 Con_DPrintf("Collision_NewBrushFromPlanes: failed to build collision brush: %i triangles, %i planes (input was %i planes), %i vertices\n", numelementsbuf / 3, numplanesbuf, numoriginalplanes, numpointsbuf);
297 // if no triangles or points could be constructed, then this routine failed but the brush is not discarded
298 if (numelementsbuf < 12 || numpointsbuf < 4)
299 Con_DPrintf("Collision_NewBrushFromPlanes: unable to rebuild triangles/points for collision brush: %i triangles, %i planes (input was %i planes), %i vertices\n", numelementsbuf / 3, numplanesbuf, numoriginalplanes, numpointsbuf);
301 // validate plane distances
302 for (j = 0;j < numplanesbuf;j++)
304 float d = furthestplanedist_float(planesbuf[j].normal, pointsbuf, numpointsbuf);
305 if (fabs(planesbuf[j].dist - d) > COLLISION_PLANE_DIST_EPSILON)
306 Con_DPrintf("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);
309 // allocate the brush and copy to it
310 brush = (colbrushf_t *)Mem_Alloc(mempool, sizeof(colbrushf_t) + sizeof(colpointf_t) * numpointsbuf + sizeof(colplanef_t) * numplanesbuf + sizeof(int) * numelementsbuf);
311 brush->supercontents = supercontents;
312 brush->numplanes = numplanesbuf;
313 brush->numpoints = numpointsbuf;
314 brush->numtriangles = numelementsbuf / 3;
315 brush->planes = (colplanef_t *)(brush + 1);
316 brush->points = (colpointf_t *)(brush->planes + brush->numplanes);
317 brush->elements = (int *)(brush->points + brush->numpoints);
318 for (j = 0;j < brush->numpoints;j++)
320 brush->points[j].v[0] = pointsbuf[j].v[0];
321 brush->points[j].v[1] = pointsbuf[j].v[1];
322 brush->points[j].v[2] = pointsbuf[j].v[2];
324 for (j = 0;j < brush->numplanes;j++)
326 brush->planes[j].normal[0] = planesbuf[j].normal[0];
327 brush->planes[j].normal[1] = planesbuf[j].normal[1];
328 brush->planes[j].normal[2] = planesbuf[j].normal[2];
329 brush->planes[j].dist = planesbuf[j].dist;
330 brush->planes[j].q3surfaceflags = planesbuf[j].q3surfaceflags;
331 brush->planes[j].texture = planesbuf[j].texture;
333 for (j = 0;j < brush->numtriangles * 3;j++)
334 brush->elements[j] = elementsbuf[j];
337 VectorClear(brush->mins);
338 VectorClear(brush->maxs);
339 for (j = 0;j < min(6, numoriginalplanes);j++)
341 if (originalplanes[j].normal[0] == 1) {xyzflags |= 1;brush->maxs[0] = originalplanes[j].dist;}
342 else if (originalplanes[j].normal[0] == -1) {xyzflags |= 2;brush->mins[0] = -originalplanes[j].dist;}
343 else if (originalplanes[j].normal[1] == 1) {xyzflags |= 4;brush->maxs[1] = originalplanes[j].dist;}
344 else if (originalplanes[j].normal[1] == -1) {xyzflags |= 8;brush->mins[1] = -originalplanes[j].dist;}
345 else if (originalplanes[j].normal[2] == 1) {xyzflags |= 16;brush->maxs[2] = originalplanes[j].dist;}
346 else if (originalplanes[j].normal[2] == -1) {xyzflags |= 32;brush->mins[2] = -originalplanes[j].dist;}
348 // if not all xyzflags were set, then this is not a brush from q3map/q3map2, and needs reconstruction of the bounding box
349 // (this case works for any brush with valid points, but sometimes brushes are not reconstructed properly and hence the points are not valid, so this is reserved as a fallback case)
352 VectorCopy(brush->points[0].v, brush->mins);
353 VectorCopy(brush->points[0].v, brush->maxs);
354 for (j = 1;j < brush->numpoints;j++)
356 brush->mins[0] = min(brush->mins[0], brush->points[j].v[0]);
357 brush->mins[1] = min(brush->mins[1], brush->points[j].v[1]);
358 brush->mins[2] = min(brush->mins[2], brush->points[j].v[2]);
359 brush->maxs[0] = max(brush->maxs[0], brush->points[j].v[0]);
360 brush->maxs[1] = max(brush->maxs[1], brush->points[j].v[1]);
361 brush->maxs[2] = max(brush->maxs[2], brush->points[j].v[2]);
370 Collision_ValidateBrush(brush);
376 void Collision_CalcPlanesForPolygonBrushFloat(colbrushf_t *brush)
379 float edge0[3], edge1[3], edge2[3], normal[3], dist, bestdist;
382 // FIXME: these probably don't actually need to be normalized if the collision code does not care
383 if (brush->numpoints == 3)
385 // optimized triangle case
386 TriangleNormal(brush->points[0].v, brush->points[1].v, brush->points[2].v, brush->planes[0].normal);
387 if (DotProduct(brush->planes[0].normal, brush->planes[0].normal) < 0.0001f)
389 // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
390 brush->numplanes = 0;
395 brush->numplanes = 5;
396 VectorNormalize(brush->planes[0].normal);
397 brush->planes[0].dist = DotProduct(brush->points->v, brush->planes[0].normal);
398 VectorNegate(brush->planes[0].normal, brush->planes[1].normal);
399 brush->planes[1].dist = -brush->planes[0].dist;
400 VectorSubtract(brush->points[2].v, brush->points[0].v, edge0);
401 VectorSubtract(brush->points[0].v, brush->points[1].v, edge1);
402 VectorSubtract(brush->points[1].v, brush->points[2].v, edge2);
405 float projectionnormal[3], projectionedge0[3], projectionedge1[3], projectionedge2[3];
407 float dist, bestdist;
408 bestdist = fabs(brush->planes[0].normal[0]);
410 for (i = 1;i < 3;i++)
412 dist = fabs(brush->planes[0].normal[i]);
419 VectorClear(projectionnormal);
420 if (brush->planes[0].normal[best] < 0)
421 projectionnormal[best] = -1;
423 projectionnormal[best] = 1;
424 VectorCopy(edge0, projectionedge0);
425 VectorCopy(edge1, projectionedge1);
426 VectorCopy(edge2, projectionedge2);
427 projectionedge0[best] = 0;
428 projectionedge1[best] = 0;
429 projectionedge2[best] = 0;
430 CrossProduct(projectionedge0, projectionnormal, brush->planes[2].normal);
431 CrossProduct(projectionedge1, projectionnormal, brush->planes[3].normal);
432 CrossProduct(projectionedge2, projectionnormal, brush->planes[4].normal);
435 CrossProduct(edge0, brush->planes->normal, brush->planes[2].normal);
436 CrossProduct(edge1, brush->planes->normal, brush->planes[3].normal);
437 CrossProduct(edge2, brush->planes->normal, brush->planes[4].normal);
439 VectorNormalize(brush->planes[2].normal);
440 VectorNormalize(brush->planes[3].normal);
441 VectorNormalize(brush->planes[4].normal);
442 brush->planes[2].dist = DotProduct(brush->points[2].v, brush->planes[2].normal);
443 brush->planes[3].dist = DotProduct(brush->points[0].v, brush->planes[3].normal);
444 brush->planes[4].dist = DotProduct(brush->points[1].v, brush->planes[4].normal);
446 if (developer.integer >= 100)
452 VectorSubtract(brush->points[0].v, brush->points[1].v, edge0);
453 VectorSubtract(brush->points[2].v, brush->points[1].v, edge1);
454 CrossProduct(edge0, edge1, normal);
455 VectorNormalize(normal);
456 VectorSubtract(normal, brush->planes[0].normal, temp);
457 if (VectorLength(temp) > 0.01f)
458 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]);
459 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)
460 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);
462 if (fabs(DotProduct(brush->planes[2].normal, brush->planes[0].normal)) > 0.01f)
463 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);
464 if (fabs(DotProduct(brush->planes[3].normal, brush->planes[0].normal)) > 0.01f)
465 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);
466 if (fabs(DotProduct(brush->planes[4].normal, brush->planes[0].normal)) > 0.01f)
467 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);
468 if (fabs(DotProduct(brush->planes[2].normal, edge0)) > 0.01f)
469 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]);
470 if (fabs(DotProduct(brush->planes[3].normal, edge1)) > 0.01f)
471 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]);
472 if (fabs(DotProduct(brush->planes[4].normal, edge2)) > 0.01f)
473 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]);
476 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)
477 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);
478 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)
479 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);
480 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)
481 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);
482 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)
483 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);
484 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)
485 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);
491 // choose best surface normal for polygon's plane
493 for (i = 0, p = brush->points + 1;i < brush->numpoints - 2;i++, p++)
495 VectorSubtract(p[-1].v, p[0].v, edge0);
496 VectorSubtract(p[1].v, p[0].v, edge1);
497 CrossProduct(edge0, edge1, normal);
498 //TriangleNormal(p[-1].v, p[0].v, p[1].v, normal);
499 dist = DotProduct(normal, normal);
500 if (i == 0 || bestdist < dist)
503 VectorCopy(normal, brush->planes->normal);
506 if (bestdist < 0.0001f)
508 // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
509 brush->numplanes = 0;
514 brush->numplanes = brush->numpoints + 2;
515 VectorNormalize(brush->planes->normal);
516 brush->planes->dist = DotProduct(brush->points->v, brush->planes->normal);
518 // negate plane to create other side
519 VectorNegate(brush->planes[0].normal, brush->planes[1].normal);
520 brush->planes[1].dist = -brush->planes[0].dist;
521 for (i = 0, p = brush->points + (brush->numpoints - 1), p2 = brush->points;i < brush->numpoints;i++, p = p2, p2++)
523 VectorSubtract(p->v, p2->v, edge0);
524 CrossProduct(edge0, brush->planes->normal, brush->planes[i + 2].normal);
525 VectorNormalize(brush->planes[i + 2].normal);
526 brush->planes[i + 2].dist = DotProduct(p->v, brush->planes[i + 2].normal);
531 if (developer.integer >= 100)
533 // validity check - will be disabled later
534 Collision_ValidateBrush(brush);
535 for (i = 0;i < brush->numplanes;i++)
538 for (j = 0, p = brush->points;j < brush->numpoints;j++, p++)
539 if (DotProduct(p->v, brush->planes[i].normal) > brush->planes[i].dist + COLLISION_PLANE_DIST_EPSILON)
540 Con_Printf("Error in brush plane generation, plane %i\n", i);
545 colbrushf_t *Collision_AllocBrushFromPermanentPolygonFloat(mempool_t *mempool, int numpoints, float *points, int supercontents)
548 brush = (colbrushf_t *)Mem_Alloc(mempool, sizeof(colbrushf_t) + sizeof(colplanef_t) * (numpoints + 2));
549 brush->supercontents = supercontents;
550 brush->numpoints = numpoints;
551 brush->numplanes = numpoints + 2;
552 brush->planes = (colplanef_t *)(brush + 1);
553 brush->points = (colpointf_t *)points;
554 Sys_Error("Collision_AllocBrushFromPermanentPolygonFloat: FIXME: this code needs to be updated to generate a mesh...");
558 // NOTE: start and end of each brush pair must have same numplanes/numpoints
559 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)
561 int nplane, nplane2, hitq3surfaceflags = 0;
562 float enterfrac = -1, leavefrac = 1, d1, d2, f, imove, newimpactnormal[3], enterfrac2 = -1;
563 const colplanef_t *startplane, *endplane;
564 texture_t *hittexture = NULL;
566 VectorClear(newimpactnormal);
568 for (nplane = 0;nplane < thatbrush_start->numplanes + thisbrush_start->numplanes;nplane++)
571 if (nplane2 >= thatbrush_start->numplanes)
573 nplane2 -= thatbrush_start->numplanes;
574 startplane = thisbrush_start->planes + nplane2;
575 endplane = thisbrush_end->planes + nplane2;
576 if (developer.integer >= 100)
578 // any brush with degenerate planes is not worth handling
579 if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
581 Con_Print("Collision_TraceBrushBrushFloat: degenerate thisbrush plane!\n");
584 f = furthestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints);
585 if (fabs(f - startplane->dist) > COLLISION_PLANE_DIST_EPSILON)
586 Con_Printf("startplane->dist %f != calculated %f (thisbrush_start)\n", startplane->dist, f);
588 d1 = nearestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints) - furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints) - collision_startnudge.value;
589 d2 = nearestplanedist_float(endplane->normal, thisbrush_end->points, thisbrush_end->numpoints) - furthestplanedist_float(endplane->normal, thatbrush_end->points, thatbrush_end->numpoints) - collision_endnudge.value;
593 startplane = thatbrush_start->planes + nplane2;
594 endplane = thatbrush_end->planes + nplane2;
595 if (developer.integer >= 100)
597 // any brush with degenerate planes is not worth handling
598 if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
600 Con_Print("Collision_TraceBrushBrushFloat: degenerate thatbrush plane!\n");
603 f = furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints);
604 if (fabs(f - startplane->dist) > COLLISION_PLANE_DIST_EPSILON)
605 Con_Printf("startplane->dist %f != calculated %f (thatbrush_start)\n", startplane->dist, f);
607 d1 = nearestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints) - startplane->dist - collision_startnudge.value;
608 d2 = nearestplanedist_float(endplane->normal, thisbrush_end->points, thisbrush_end->numpoints) - endplane->dist - collision_endnudge.value;
610 //Con_Printf("%c%i: d1 = %f, d2 = %f, d1 / (d1 - d2) = %f\n", nplane2 != nplane ? 'b' : 'a', nplane2, d1, d2, d1 / (d1 - d2));
620 imove = 1 / (d1 - d2);
621 f = (d1 - collision_enternudge.value) * imove;
622 // check if this will reduce the collision time range
625 // reduced collision time range
627 // if the collision time range is now empty, no collision
628 if (enterfrac > leavefrac)
630 // if the collision would be further away than the trace's
631 // existing collision data, we don't care about this
633 if (enterfrac > trace->realfraction)
635 // calculate the nudged fraction and impact normal we'll
636 // need if we accept this collision later
637 enterfrac2 = f - collision_impactnudge.value * imove;
638 VectorLerp(startplane->normal, enterfrac, endplane->normal, newimpactnormal);
639 hitq3surfaceflags = startplane->q3surfaceflags;
640 hittexture = startplane->texture;
646 // moving out of brush
652 f = (d1 + collision_leavenudge.value) / (d1 - d2);
653 // check if this will reduce the collision time range
656 // reduced collision time range
658 // if the collision time range is now empty, no collision
659 if (enterfrac > leavefrac)
666 // at this point we know the trace overlaps the brush because it was not
667 // rejected at any point in the loop above
669 // see if this brush can block the trace or not according to contents
670 if (trace->hitsupercontentsmask & thatbrush_start->supercontents)
674 trace->startsupercontents |= thatbrush_start->supercontents;
675 trace->startsolid = true;
677 trace->allsolid = true;
679 // store out the impact information
680 trace->hitsupercontents = thatbrush_start->supercontents;
681 trace->hitq3surfaceflags = hitq3surfaceflags;
682 trace->hittexture = hittexture;
683 trace->realfraction = bound(0, enterfrac, 1);
684 trace->fraction = bound(0, enterfrac2, 1);
685 VectorCopy(newimpactnormal, trace->plane.normal);
689 // this brush can not block the trace, but it can update start contents
691 trace->startsupercontents |= thatbrush_start->supercontents;
695 // NOTE: start and end brush pair must have same numplanes/numpoints
696 void Collision_TraceLineBrushFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, const colbrushf_t *thatbrush_start, const colbrushf_t *thatbrush_end)
698 int nplane, hitq3surfaceflags = 0;
699 float enterfrac = -1, leavefrac = 1, d1, d2, f, imove, newimpactnormal[3], enterfrac2 = -1;
700 const colplanef_t *startplane, *endplane;
701 texture_t *hittexture = NULL;
703 VectorClear(newimpactnormal);
705 for (nplane = 0;nplane < thatbrush_start->numplanes;nplane++)
707 startplane = thatbrush_start->planes + nplane;
708 endplane = thatbrush_end->planes + nplane;
709 d1 = DotProduct(startplane->normal, linestart) - startplane->dist - collision_startnudge.value;
710 d2 = DotProduct(endplane->normal, lineend) - endplane->dist - collision_endnudge.value;
711 if (developer.integer >= 100)
713 // any brush with degenerate planes is not worth handling
714 if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
716 Con_Print("Collision_TraceLineBrushFloat: degenerate plane!\n");
719 if (thatbrush_start->numpoints)
721 f = furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints);
722 if (fabs(f - startplane->dist) > COLLISION_PLANE_DIST_EPSILON)
723 Con_Printf("startplane->dist %f != calculated %f\n", startplane->dist, f);
735 imove = 1 / (d1 - d2);
736 f = (d1 - collision_enternudge.value) * imove;
737 // check if this will reduce the collision time range
740 // reduced collision time range
742 // if the collision time range is now empty, no collision
743 if (enterfrac > leavefrac)
745 // if the collision would be further away than the trace's
746 // existing collision data, we don't care about this
748 if (enterfrac > trace->realfraction)
750 // calculate the nudged fraction and impact normal we'll
751 // need if we accept this collision later
752 enterfrac2 = f - collision_impactnudge.value * imove;
753 VectorLerp(startplane->normal, enterfrac, endplane->normal, newimpactnormal);
754 hitq3surfaceflags = startplane->q3surfaceflags;
755 hittexture = startplane->texture;
761 // moving out of brush
767 f = (d1 + collision_leavenudge.value) / (d1 - d2);
768 // check if this will reduce the collision time range
771 // reduced collision time range
773 // if the collision time range is now empty, no collision
774 if (enterfrac > leavefrac)
781 // at this point we know the trace overlaps the brush because it was not
782 // rejected at any point in the loop above
784 // see if this brush can block the trace or not according to contents
785 if (trace->hitsupercontentsmask & thatbrush_start->supercontents)
789 trace->startsupercontents |= thatbrush_start->supercontents;
790 trace->startsolid = true;
792 trace->allsolid = true;
794 // store out the impact information
795 trace->hitsupercontents = thatbrush_start->supercontents;
796 trace->hitq3surfaceflags = hitq3surfaceflags;
797 trace->hittexture = hittexture;
798 trace->realfraction = bound(0, enterfrac, 1);
799 trace->fraction = bound(0, enterfrac2, 1);
800 VectorCopy(newimpactnormal, trace->plane.normal);
804 // this brush can not block the trace, but it can update start contents
806 trace->startsupercontents |= thatbrush_start->supercontents;
810 void Collision_TracePointBrushFloat(trace_t *trace, const vec3_t point, const colbrushf_t *thatbrush)
813 const colplanef_t *plane;
815 for (nplane = 0, plane = thatbrush->planes;nplane < thatbrush->numplanes;nplane++, plane++)
816 if (DotProduct(plane->normal, point) > plane->dist)
819 trace->startsupercontents |= thatbrush->supercontents;
820 if (trace->hitsupercontentsmask & thatbrush->supercontents)
822 trace->startsolid = true;
823 trace->allsolid = true;
827 static colpointf_t polyf_points[256];
828 static colplanef_t polyf_planes[256 + 2];
829 static colbrushf_t polyf_brush;
831 void Collision_SnapCopyPoints(int numpoints, const colpointf_t *in, colpointf_t *out, float fractionprecision, float invfractionprecision)
835 out->v[0] = floor(in->v[0] * fractionprecision + 0.5f) * invfractionprecision;
836 out->v[1] = floor(in->v[1] * fractionprecision + 0.5f) * invfractionprecision;
837 out->v[2] = floor(in->v[2] * fractionprecision + 0.5f) * invfractionprecision;
841 void Collision_TraceBrushPolygonFloat(trace_t *trace, const colbrushf_t *thisbrush_start, const colbrushf_t *thisbrush_end, int numpoints, const float *points, int supercontents)
845 Con_Print("Polygon with more than 256 points not supported yet (fixme!)\n");
848 polyf_brush.numpoints = numpoints;
849 polyf_brush.numplanes = numpoints + 2;
850 //polyf_brush.points = (colpointf_t *)points;
851 polyf_brush.planes = polyf_planes;
852 polyf_brush.supercontents = supercontents;
853 polyf_brush.points = polyf_points;
854 Collision_SnapCopyPoints(numpoints, (colpointf_t *)points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
855 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
856 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
857 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brush, &polyf_brush);
860 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)
863 float facemins[3], facemaxs[3];
864 polyf_brush.numpoints = 3;
865 polyf_brush.numplanes = 5;
866 polyf_brush.points = polyf_points;
867 polyf_brush.planes = polyf_planes;
868 polyf_brush.supercontents = supercontents;
869 for (i = 0;i < polyf_brush.numplanes;i++)
871 polyf_brush.planes[i].q3surfaceflags = q3surfaceflags;
872 polyf_brush.planes[i].texture = texture;
874 for (i = 0;i < numtriangles;i++, element3i += 3)
876 VectorCopy(vertex3f + element3i[0] * 3, polyf_points[0].v);
877 VectorCopy(vertex3f + element3i[1] * 3, polyf_points[1].v);
878 VectorCopy(vertex3f + element3i[2] * 3, polyf_points[2].v);
879 Collision_SnapCopyPoints(3, polyf_points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
880 facemins[0] = min(polyf_points[0].v[0], min(polyf_points[1].v[0], polyf_points[2].v[0]));
881 facemins[1] = min(polyf_points[0].v[1], min(polyf_points[1].v[1], polyf_points[2].v[1]));
882 facemins[2] = min(polyf_points[0].v[2], min(polyf_points[1].v[2], polyf_points[2].v[2]));
883 facemaxs[0] = max(polyf_points[0].v[0], max(polyf_points[1].v[0], polyf_points[2].v[0]));
884 facemaxs[1] = max(polyf_points[0].v[1], max(polyf_points[1].v[1], polyf_points[2].v[1]));
885 facemaxs[2] = max(polyf_points[0].v[2], max(polyf_points[1].v[2], polyf_points[2].v[2]));
886 if (BoxesOverlap(segmentmins, segmentmaxs, facemins, facemaxs))
888 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
889 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
890 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brush, &polyf_brush);
895 void Collision_TraceLinePolygonFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, int numpoints, const float *points, int supercontents)
899 Con_Print("Polygon with more than 256 points not supported yet (fixme!)\n");
902 polyf_brush.numpoints = numpoints;
903 polyf_brush.numplanes = numpoints + 2;
904 //polyf_brush.points = (colpointf_t *)points;
905 polyf_brush.points = polyf_points;
906 Collision_SnapCopyPoints(numpoints, (colpointf_t *)points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
907 polyf_brush.planes = polyf_planes;
908 polyf_brush.supercontents = supercontents;
909 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
910 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
911 Collision_TraceLineBrushFloat(trace, linestart, lineend, &polyf_brush, &polyf_brush);
914 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)
918 // FIXME: snap vertices?
919 for (i = 0;i < numtriangles;i++, element3i += 3)
920 Collision_TraceLineTriangleFloat(trace, linestart, lineend, vertex3f + element3i[0] * 3, vertex3f + element3i[1] * 3, vertex3f + element3i[2] * 3, supercontents, q3surfaceflags, texture);
922 polyf_brush.numpoints = 3;
923 polyf_brush.numplanes = 5;
924 polyf_brush.points = polyf_points;
925 polyf_brush.planes = polyf_planes;
926 polyf_brush.supercontents = supercontents;
927 for (i = 0;i < polyf_brush.numplanes;i++)
929 polyf_brush.planes[i].supercontents = supercontents;
930 polyf_brush.planes[i].q3surfaceflags = q3surfaceflags;
931 polyf_brush.planes[i].texture = texture;
933 for (i = 0;i < numtriangles;i++, element3i += 3)
935 float facemins[3], facemaxs[3];
936 VectorCopy(vertex3f + element3i[0] * 3, polyf_points[0].v);
937 VectorCopy(vertex3f + element3i[1] * 3, polyf_points[1].v);
938 VectorCopy(vertex3f + element3i[2] * 3, polyf_points[2].v);
939 Collision_SnapCopyPoints(numpoints, polyf_points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
940 facemins[0] = min(polyf_points[0].v[0], min(polyf_points[1].v[0], polyf_points[2].v[0]));
941 facemins[1] = min(polyf_points[0].v[1], min(polyf_points[1].v[1], polyf_points[2].v[1]));
942 facemins[2] = min(polyf_points[0].v[2], min(polyf_points[1].v[2], polyf_points[2].v[2]));
943 facemaxs[0] = max(polyf_points[0].v[0], max(polyf_points[1].v[0], polyf_points[2].v[0]));
944 facemaxs[1] = max(polyf_points[0].v[1], max(polyf_points[1].v[1], polyf_points[2].v[1]));
945 facemaxs[2] = max(polyf_points[0].v[2], max(polyf_points[1].v[2], polyf_points[2].v[2]));
946 if (BoxesOverlap(segmentmins, segmentmaxs, facemins, facemaxs))
948 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
949 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
950 Collision_TraceLineBrushFloat(trace, linestart, lineend, &polyf_brush, &polyf_brush);
957 static colpointf_t polyf_pointsstart[256], polyf_pointsend[256];
958 static colplanef_t polyf_planesstart[256 + 2], polyf_planesend[256 + 2];
959 static colbrushf_t polyf_brushstart, polyf_brushend;
961 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)
966 Con_Print("Polygon with more than 256 points not supported yet (fixme!)\n");
969 polyf_brushstart.numpoints = numpoints;
970 polyf_brushstart.numplanes = numpoints + 2;
971 polyf_brushstart.points = polyf_pointsstart;//(colpointf_t *)points;
972 polyf_brushstart.planes = polyf_planesstart;
973 polyf_brushstart.supercontents = supercontents;
974 for (i = 0;i < numpoints;i++)
975 Matrix4x4_Transform(polygonmatrixstart, points + i * 3, polyf_brushstart.points[i].v);
976 polyf_brushend.numpoints = numpoints;
977 polyf_brushend.numplanes = numpoints + 2;
978 polyf_brushend.points = polyf_pointsend;//(colpointf_t *)points;
979 polyf_brushend.planes = polyf_planesend;
980 polyf_brushend.supercontents = supercontents;
981 for (i = 0;i < numpoints;i++)
982 Matrix4x4_Transform(polygonmatrixend, points + i * 3, polyf_brushend.points[i].v);
983 for (i = 0;i < polyf_brushstart.numplanes;i++)
985 polyf_brushstart.planes[i].q3surfaceflags = q3surfaceflags;
986 polyf_brushstart.planes[i].texture = texture;
988 Collision_SnapCopyPoints(numpoints, polyf_pointsstart, polyf_pointsstart, COLLISION_SNAPSCALE, COLLISION_SNAP);
989 Collision_SnapCopyPoints(numpoints, polyf_pointsend, polyf_pointsend, COLLISION_SNAPSCALE, COLLISION_SNAP);
990 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brushstart);
991 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brushend);
993 //Collision_PrintBrushAsQHull(&polyf_brushstart, "polyf_brushstart");
994 //Collision_PrintBrushAsQHull(&polyf_brushend, "polyf_brushend");
996 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brushstart, &polyf_brushend);
1001 #define MAX_BRUSHFORBOX 16
1002 static int brushforbox_index = 0;
1003 static colpointf_t brushforbox_point[MAX_BRUSHFORBOX*8];
1004 static colplanef_t brushforbox_plane[MAX_BRUSHFORBOX*6];
1005 static colbrushf_t brushforbox_brush[MAX_BRUSHFORBOX];
1006 static colbrushf_t brushforpoint_brush[MAX_BRUSHFORBOX];
1008 void Collision_InitBrushForBox(void)
1011 for (i = 0;i < MAX_BRUSHFORBOX;i++)
1013 brushforbox_brush[i].numpoints = 8;
1014 brushforbox_brush[i].numplanes = 6;
1015 brushforbox_brush[i].points = brushforbox_point + i * 8;
1016 brushforbox_brush[i].planes = brushforbox_plane + i * 6;
1017 brushforpoint_brush[i].numpoints = 1;
1018 brushforpoint_brush[i].numplanes = 0;
1019 brushforpoint_brush[i].points = brushforbox_point + i * 8;
1020 brushforpoint_brush[i].planes = brushforbox_plane + i * 6;
1024 colbrushf_t *Collision_BrushForBox(const matrix4x4_t *matrix, const vec3_t mins, const vec3_t maxs, int supercontents, int q3surfaceflags, texture_t *texture)
1029 if (brushforbox_brush[0].numpoints == 0)
1030 Collision_InitBrushForBox();
1031 // FIXME: these probably don't actually need to be normalized if the collision code does not care
1032 if (VectorCompare(mins, maxs))
1035 brush = brushforpoint_brush + ((brushforbox_index++) % MAX_BRUSHFORBOX);
1036 VectorCopy(mins, brush->points->v);
1040 brush = brushforbox_brush + ((brushforbox_index++) % MAX_BRUSHFORBOX);
1042 for (i = 0;i < 8;i++)
1044 v[0] = i & 1 ? maxs[0] : mins[0];
1045 v[1] = i & 2 ? maxs[1] : mins[1];
1046 v[2] = i & 4 ? maxs[2] : mins[2];
1047 Matrix4x4_Transform(matrix, v, brush->points[i].v);
1050 for (i = 0;i < 6;i++)
1053 v[i >> 1] = i & 1 ? 1 : -1;
1054 Matrix4x4_Transform3x3(matrix, v, brush->planes[i].normal);
1055 VectorNormalize(brush->planes[i].normal);
1058 brush->supercontents = supercontents;
1059 for (j = 0;j < brush->numplanes;j++)
1061 brush->planes[j].q3surfaceflags = q3surfaceflags;
1062 brush->planes[j].texture = texture;
1063 brush->planes[j].dist = furthestplanedist_float(brush->planes[j].normal, brush->points, brush->numpoints);
1065 VectorCopy(brush->points[0].v, brush->mins);
1066 VectorCopy(brush->points[0].v, brush->maxs);
1067 for (j = 1;j < brush->numpoints;j++)
1069 brush->mins[0] = min(brush->mins[0], brush->points[j].v[0]);
1070 brush->mins[1] = min(brush->mins[1], brush->points[j].v[1]);
1071 brush->mins[2] = min(brush->mins[2], brush->points[j].v[2]);
1072 brush->maxs[0] = max(brush->maxs[0], brush->points[j].v[0]);
1073 brush->maxs[1] = max(brush->maxs[1], brush->points[j].v[1]);
1074 brush->maxs[2] = max(brush->maxs[2], brush->points[j].v[2]);
1076 brush->mins[0] -= 1;
1077 brush->mins[1] -= 1;
1078 brush->mins[2] -= 1;
1079 brush->maxs[0] += 1;
1080 brush->maxs[1] += 1;
1081 brush->maxs[2] += 1;
1082 Collision_ValidateBrush(brush);
1086 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)
1088 colbrushf_t *boxbrush, *thisbrush_start, *thisbrush_end;
1089 vec3_t startmins, startmaxs, endmins, endmaxs;
1091 // create brushes for the collision
1092 VectorAdd(start, mins, startmins);
1093 VectorAdd(start, maxs, startmaxs);
1094 VectorAdd(end, mins, endmins);
1095 VectorAdd(end, maxs, endmaxs);
1096 boxbrush = Collision_BrushForBox(&identitymatrix, cmins, cmaxs, supercontents, q3surfaceflags, texture);
1097 thisbrush_start = Collision_BrushForBox(&identitymatrix, startmins, startmaxs, 0, 0, NULL);
1098 thisbrush_end = Collision_BrushForBox(&identitymatrix, endmins, endmaxs, 0, 0, NULL);
1100 memset(trace, 0, sizeof(trace_t));
1101 trace->hitsupercontentsmask = hitsupercontentsmask;
1102 trace->fraction = 1;
1103 trace->realfraction = 1;
1104 trace->allsolid = true;
1105 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, boxbrush, boxbrush);
1108 //pseudocode for detecting line/sphere overlap without calculating an impact point
1109 //linesphereorigin = sphereorigin - linestart;linediff = lineend - linestart;linespherefrac = DotProduct(linesphereorigin, linediff) / DotProduct(linediff, linediff);return VectorLength2(linesphereorigin - bound(0, linespherefrac, 1) * linediff) >= sphereradius*sphereradius;
1111 // LordHavoc: currently unused, but tested
1112 // note: this can be used for tracing a moving sphere vs a stationary sphere,
1113 // by simply adding the moving sphere's radius to the sphereradius parameter,
1114 // all the results are correct (impactpoint, impactnormal, and fraction)
1115 float Collision_ClipTrace_Line_Sphere(double *linestart, double *lineend, double *sphereorigin, double sphereradius, double *impactpoint, double *impactnormal)
1117 double dir[3], scale, v[3], deviationdist, impactdist, linelength;
1118 // make sure the impactpoint and impactnormal are valid even if there is
1120 VectorCopy(lineend, impactpoint);
1121 VectorClear(impactnormal);
1122 // calculate line direction
1123 VectorSubtract(lineend, linestart, dir);
1124 // normalize direction
1125 linelength = VectorLength(dir);
1128 scale = 1.0 / linelength;
1129 VectorScale(dir, scale, dir);
1131 // this dotproduct calculates the distance along the line at which the
1132 // sphere origin is (nearest point to the sphere origin on the line)
1133 impactdist = DotProduct(sphereorigin, dir) - DotProduct(linestart, dir);
1134 // calculate point on line at that distance, and subtract the
1135 // sphereorigin from it, so we have a vector to measure for the distance
1136 // of the line from the sphereorigin (deviation, how off-center it is)
1137 VectorMA(linestart, impactdist, dir, v);
1138 VectorSubtract(v, sphereorigin, v);
1139 deviationdist = VectorLength2(v);
1140 // if outside the radius, it's a miss for sure
1141 // (we do this comparison using squared radius to avoid a sqrt)
1142 if (deviationdist > sphereradius*sphereradius)
1143 return 1; // miss (off to the side)
1144 // nudge back to find the correct impact distance
1145 impactdist += deviationdist - sphereradius;
1146 if (impactdist >= linelength)
1147 return 1; // miss (not close enough)
1149 return 1; // miss (linestart is past or inside sphere)
1150 // calculate new impactpoint
1151 VectorMA(linestart, impactdist, dir, impactpoint);
1152 // calculate impactnormal (surface normal at point of impact)
1153 VectorSubtract(impactpoint, sphereorigin, impactnormal);
1154 // normalize impactnormal
1155 VectorNormalize(impactnormal);
1156 // return fraction of movement distance
1157 return impactdist / linelength;
1160 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)
1164 float d1, d2, d, f, impact[3], edgenormal[3], faceplanenormal[3], faceplanedist, faceplanenormallength2, edge01[3], edge21[3], edge02[3];
1166 // this function executes:
1167 // 32 ops when line starts behind triangle
1168 // 38 ops when line ends infront of triangle
1169 // 43 ops when line fraction is already closer than this triangle
1170 // 72 ops when line is outside edge 01
1171 // 92 ops when line is outside edge 21
1172 // 115 ops when line is outside edge 02
1173 // 123 ops when line impacts triangle and updates trace results
1175 // this code is designed for clockwise triangles, conversion to
1176 // counterclockwise would require swapping some things around...
1177 // it is easier to simply swap the point0 and point2 parameters to this
1178 // function when calling it than it is to rewire the internals.
1180 // calculate the faceplanenormal of the triangle, this represents the front side
1182 VectorSubtract(point0, point1, edge01);
1183 VectorSubtract(point2, point1, edge21);
1184 CrossProduct(edge01, edge21, faceplanenormal);
1185 // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
1187 faceplanenormallength2 = DotProduct(faceplanenormal, faceplanenormal);
1188 if (faceplanenormallength2 < 0.0001f)
1190 // calculate the distance
1192 faceplanedist = DotProduct(point0, faceplanenormal);
1194 // if start point is on the back side there is no collision
1195 // (we don't care about traces going through the triangle the wrong way)
1197 // calculate the start distance
1199 d1 = DotProduct(faceplanenormal, linestart);
1200 if (d1 <= faceplanedist)
1203 // calculate the end distance
1205 d2 = DotProduct(faceplanenormal, lineend);
1206 // if both are in front, there is no collision
1207 if (d2 >= faceplanedist)
1210 // from here on we know d1 is >= 0 and d2 is < 0
1211 // this means the line starts infront and ends behind, passing through it
1213 // calculate the recipricol of the distance delta,
1214 // so we can use it multiple times cheaply (instead of division)
1216 d = 1.0f / (d1 - d2);
1217 // calculate the impact fraction by taking the start distance (> 0)
1218 // and subtracting the face plane distance (this is the distance of the
1219 // triangle along that same normal)
1220 // then multiply by the recipricol distance delta
1222 f = (d1 - faceplanedist) * d;
1223 // skip out if this impact is further away than previous ones
1225 if (f > trace->realfraction)
1227 // calculate the perfect impact point for classification of insidedness
1229 impact[0] = linestart[0] + f * (lineend[0] - linestart[0]);
1230 impact[1] = linestart[1] + f * (lineend[1] - linestart[1]);
1231 impact[2] = linestart[2] + f * (lineend[2] - linestart[2]);
1233 // calculate the edge normal and reject if impact is outside triangle
1234 // (an edge normal faces away from the triangle, to get the desired normal
1235 // a crossproduct with the faceplanenormal is used, and because of the way
1236 // the insidedness comparison is written it does not need to be normalized)
1238 // first use the two edges from the triangle plane math
1239 // the other edge only gets calculated if the point survives that long
1242 CrossProduct(edge01, faceplanenormal, edgenormal);
1243 if (DotProduct(impact, edgenormal) > DotProduct(point1, edgenormal))
1247 CrossProduct(faceplanenormal, edge21, edgenormal);
1248 if (DotProduct(impact, edgenormal) > DotProduct(point2, edgenormal))
1252 VectorSubtract(point0, point2, edge02);
1253 CrossProduct(faceplanenormal, edge02, edgenormal);
1254 if (DotProduct(impact, edgenormal) > DotProduct(point0, edgenormal))
1259 // store the new trace fraction
1260 trace->realfraction = f;
1262 // calculate a nudged fraction to keep it out of the surface
1263 // (the main fraction remains perfect)
1264 trace->fraction = f - collision_impactnudge.value * d;
1266 // store the new trace plane (because collisions only happen from
1267 // the front this is always simply the triangle normal, never flipped)
1268 d = 1.0 / sqrt(faceplanenormallength2);
1269 VectorScale(faceplanenormal, d, trace->plane.normal);
1270 trace->plane.dist = faceplanedist * d;
1272 trace->hitsupercontents = supercontents;
1273 trace->hitq3surfaceflags = q3surfaceflags;
1274 trace->hittexture = texture;
1276 float d1, d2, d, f, fnudged, impact[3], edgenormal[3], faceplanenormal[3], faceplanedist, edge[3];
1278 // this code is designed for clockwise triangles, conversion to
1279 // counterclockwise would require swapping some things around...
1280 // it is easier to simply swap the point0 and point2 parameters to this
1281 // function when calling it than it is to rewire the internals.
1283 // calculate the unnormalized faceplanenormal of the triangle,
1284 // this represents the front side
1285 TriangleNormal(point0, point1, point2, faceplanenormal);
1286 // there's no point in processing a degenerate triangle
1287 // (GIGO - Garbage In, Garbage Out)
1288 if (DotProduct(faceplanenormal, faceplanenormal) < 0.0001f)
1290 // calculate the unnormalized distance
1291 faceplanedist = DotProduct(point0, faceplanenormal);
1293 // calculate the unnormalized start distance
1294 d1 = DotProduct(faceplanenormal, linestart) - faceplanedist;
1295 // if start point is on the back side there is no collision
1296 // (we don't care about traces going through the triangle the wrong way)
1300 // calculate the unnormalized end distance
1301 d2 = DotProduct(faceplanenormal, lineend) - faceplanedist;
1302 // if both are in front, there is no collision
1306 // from here on we know d1 is >= 0 and d2 is < 0
1307 // this means the line starts infront and ends behind, passing through it
1309 // calculate the recipricol of the distance delta,
1310 // so we can use it multiple times cheaply (instead of division)
1311 d = 1.0f / (d1 - d2);
1312 // calculate the impact fraction by taking the start distance (> 0)
1313 // and subtracting the face plane distance (this is the distance of the
1314 // triangle along that same normal)
1315 // then multiply by the recipricol distance delta
1317 // skip out if this impact is further away than previous ones
1318 if (f > trace->realfraction)
1320 // calculate the perfect impact point for classification of insidedness
1321 impact[0] = linestart[0] + f * (lineend[0] - linestart[0]);
1322 impact[1] = linestart[1] + f * (lineend[1] - linestart[1]);
1323 impact[2] = linestart[2] + f * (lineend[2] - linestart[2]);
1325 // calculate the edge normal and reject if impact is outside triangle
1326 // (an edge normal faces away from the triangle, to get the desired normal
1327 // a crossproduct with the faceplanenormal is used, and because of the way
1328 // the insidedness comparison is written it does not need to be normalized)
1330 VectorSubtract(point2, point0, edge);
1331 CrossProduct(edge, faceplanenormal, edgenormal);
1332 if (DotProduct(impact, edgenormal) > DotProduct(point0, edgenormal))
1335 VectorSubtract(point0, point1, edge);
1336 CrossProduct(edge, faceplanenormal, edgenormal);
1337 if (DotProduct(impact, edgenormal) > DotProduct(point1, edgenormal))
1340 VectorSubtract(point1, point2, edge);
1341 CrossProduct(edge, faceplanenormal, edgenormal);
1342 if (DotProduct(impact, edgenormal) > DotProduct(point2, edgenormal))
1345 // store the new trace fraction
1346 trace->realfraction = bound(0, f, 1);
1348 // store the new trace plane (because collisions only happen from
1349 // the front this is always simply the triangle normal, never flipped)
1350 VectorNormalize(faceplanenormal);
1351 VectorCopy(faceplanenormal, trace->plane.normal);
1352 trace->plane.dist = DotProduct(point0, faceplanenormal);
1354 // calculate the normalized start and end distances
1355 d1 = DotProduct(trace->plane.normal, linestart) - trace->plane.dist;
1356 d2 = DotProduct(trace->plane.normal, lineend) - trace->plane.dist;
1358 // calculate a nudged fraction to keep it out of the surface
1359 // (the main fraction remains perfect)
1360 fnudged = (d1 - collision_impactnudge.value) / (d1 - d2);
1361 trace->fraction = bound(0, fnudged, 1);
1363 // store the new trace endpos
1364 // not needed, it's calculated later when the trace is finished
1365 //trace->endpos[0] = linestart[0] + fnudged * (lineend[0] - linestart[0]);
1366 //trace->endpos[1] = linestart[1] + fnudged * (lineend[1] - linestart[1]);
1367 //trace->endpos[2] = linestart[2] + fnudged * (lineend[2] - linestart[2]);
1368 trace->hitsupercontents = supercontents;
1369 trace->hitq3surfaceflags = q3surfaceflags;
1370 trace->hittexture = texture;
1374 typedef struct colbspnode_s
1377 struct colbspnode_s *children[2];
1378 // the node is reallocated or split if max is reached
1381 colbrushf_t **colbrushflist;
1384 //colbrushd_t **colbrushdlist;
1388 typedef struct colbsp_s
1391 colbspnode_t *nodes;
1395 colbsp_t *Collision_CreateCollisionBSP(mempool_t *mempool)
1398 bsp = (colbsp_t *)Mem_Alloc(mempool, sizeof(colbsp_t));
1399 bsp->mempool = mempool;
1400 bsp->nodes = (colbspnode_t *)Mem_Alloc(bsp->mempool, sizeof(colbspnode_t));
1404 void Collision_FreeCollisionBSPNode(colbspnode_t *node)
1406 if (node->children[0])
1407 Collision_FreeCollisionBSPNode(node->children[0]);
1408 if (node->children[1])
1409 Collision_FreeCollisionBSPNode(node->children[1]);
1410 while (--node->numcolbrushf)
1411 Mem_Free(node->colbrushflist[node->numcolbrushf]);
1412 //while (--node->numcolbrushd)
1413 // Mem_Free(node->colbrushdlist[node->numcolbrushd]);
1417 void Collision_FreeCollisionBSP(colbsp_t *bsp)
1419 Collision_FreeCollisionBSPNode(bsp->nodes);
1423 void Collision_BoundingBoxOfBrushTraceSegment(const colbrushf_t *start, const colbrushf_t *end, vec3_t mins, vec3_t maxs, float startfrac, float endfrac)
1426 colpointf_t *ps, *pe;
1427 float tempstart[3], tempend[3];
1428 VectorLerp(start->points[0].v, startfrac, end->points[0].v, mins);
1429 VectorCopy(mins, maxs);
1430 for (i = 0, ps = start->points, pe = end->points;i < start->numpoints;i++, ps++, pe++)
1432 VectorLerp(ps->v, startfrac, pe->v, tempstart);
1433 VectorLerp(ps->v, endfrac, pe->v, tempend);
1434 mins[0] = min(mins[0], min(tempstart[0], tempend[0]));
1435 mins[1] = min(mins[1], min(tempstart[1], tempend[1]));
1436 mins[2] = min(mins[2], min(tempstart[2], tempend[2]));
1437 maxs[0] = min(maxs[0], min(tempstart[0], tempend[0]));
1438 maxs[1] = min(maxs[1], min(tempstart[1], tempend[1]));
1439 maxs[2] = min(maxs[2], min(tempstart[2], tempend[2]));