5 #define COLLISION_SNAPSCALE (8.0f)
6 #define COLLISION_SNAP (1.0f / COLLISION_SNAPSCALE)
8 cvar_t collision_impactnudge = {0, "collision_impactnudge", "0.03125"};
9 cvar_t collision_startnudge = {0, "collision_startnudge", "0"};
10 cvar_t collision_endnudge = {0, "collision_endnudge", "0"};
11 cvar_t collision_enternudge = {0, "collision_enternudge", "0"};
12 cvar_t collision_leavenudge = {0, "collision_leavenudge", "0"};
14 void Collision_Init (void)
16 Cvar_RegisterVariable(&collision_impactnudge);
17 Cvar_RegisterVariable(&collision_startnudge);
18 Cvar_RegisterVariable(&collision_endnudge);
19 Cvar_RegisterVariable(&collision_enternudge);
20 Cvar_RegisterVariable(&collision_leavenudge);
36 void Collision_PrintBrushAsQHull(colbrushf_t *brush, const char *name)
39 Con_Printf("3 %s\n%i\n", name, brush->numpoints);
40 for (i = 0;i < brush->numpoints;i++)
41 Con_Printf("%f %f %f\n", brush->points[i].v[0], brush->points[i].v[1], brush->points[i].v[2]);
43 Con_Printf("4\n%i\n", brush->numplanes);
44 for (i = 0;i < brush->numplanes;i++)
45 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);
48 void Collision_ValidateBrush(colbrushf_t *brush)
50 int j, k, pointsoffplanes, pointonplanes, pointswithinsufficientplanes, printbrush;
53 if (!brush->numpoints)
55 Con_Print("Collision_ValidateBrush: brush with no points!\n");
59 // it's ok for a brush to have one point and no planes...
60 if (brush->numplanes == 0 && brush->numpoints != 1)
62 Con_Print("Collision_ValidateBrush: brush with no planes and more than one point!\n");
69 pointswithinsufficientplanes = 0;
70 for (k = 0;k < brush->numplanes;k++)
71 if (DotProduct(brush->planes[k].normal, brush->planes[k].normal) < 0.0001f)
72 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);
73 for (j = 0;j < brush->numpoints;j++)
76 for (k = 0;k < brush->numplanes;k++)
78 d = DotProduct(brush->points[j].v, brush->planes[k].normal) - brush->planes[k].dist;
79 if (d > (1.0f / 8.0f))
81 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);
89 if (pointonplanes < 3)
90 pointswithinsufficientplanes++;
92 if (pointswithinsufficientplanes)
94 Con_Print("Collision_ValidateBrush: some points have insufficient planes, every point must be on at least 3 planes to form a corner.\n");
97 if (pointsoffplanes == 0) // all points are on all planes
99 Con_Print("Collision_ValidateBrush: all points lie on all planes (degenerate, no brush volume!)\n");
104 Collision_PrintBrushAsQHull(brush, "unnamed");
107 float nearestplanedist_float(const float *normal, const colpointf_t *points, int numpoints)
109 float dist, bestdist;
110 bestdist = DotProduct(points->v, normal);
114 dist = DotProduct(points->v, normal);
115 bestdist = min(bestdist, dist);
121 float furthestplanedist_float(const float *normal, const colpointf_t *points, int numpoints)
123 float dist, bestdist;
124 bestdist = DotProduct(points->v, normal);
128 dist = DotProduct(points->v, normal);
129 bestdist = max(bestdist, dist);
136 colbrushf_t *Collision_NewBrushFromPlanes(mempool_t *mempool, int numoriginalplanes, const mplane_t *originalplanes, int supercontents)
139 int numpointsbuf = 0, maxpointsbuf = 256, numplanesbuf = 0, maxplanesbuf = 256, numelementsbuf = 0, maxelementsbuf = 256;
141 colpointf_t pointsbuf[256];
142 colplanef_t planesbuf[256];
143 int elementsbuf[1024];
144 int polypointbuf[256];
149 // enable these if debugging to avoid seeing garbage in unused data
150 memset(pointsbuf, 0, sizeof(pointsbuf));
151 memset(planesbuf, 0, sizeof(planesbuf));
152 memset(elementsbuf, 0, sizeof(elementsbuf));
153 memset(polypointbuf, 0, sizeof(polypointbuf));
154 memset(p, 0, sizeof(p));
156 // construct a collision brush (points, planes, and renderable mesh) from
157 // a set of planes, this also optimizes out any unnecessary planes (ones
158 // whose polygon is clipped away by the other planes)
159 for (j = 0;j < numoriginalplanes;j++)
161 // add the plane uniquely (no duplicates)
162 for (k = 0;k < numplanesbuf;k++)
163 if (VectorCompare(planesbuf[k].normal, originalplanes[j].normal) && planesbuf[k].dist == originalplanes[j].dist)
165 // if the plane is a duplicate, skip it
166 if (k < numplanesbuf)
168 // check if there are too many and skip the brush
169 if (numplanesbuf >= maxplanesbuf)
171 Con_Print("Collision_NewBrushFromPlanes: failed to build collision brush: too many planes for buffer\n");
175 // create a large polygon from the plane
177 PolygonD_QuadForPlane(p[w], originalplanes[j].normal[0], originalplanes[j].normal[1], originalplanes[j].normal[2], originalplanes[j].dist, 1024.0*1024.0*1024.0);
179 // clip it by all other planes
180 for (k = 0;k < numoriginalplanes && pnumpoints && pnumpoints <= pmaxpoints;k++)
184 // we want to keep the inside of the brush plane so we flip
186 PolygonD_Divide(pnumpoints, p[w], -originalplanes[k].normal[0], -originalplanes[k].normal[1], -originalplanes[k].normal[2], -originalplanes[k].dist, 1.0/32.0, pmaxpoints, p[!w], &pnumpoints, 0, NULL, NULL);
190 // if nothing is left, skip it
193 //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);
197 for (k = 0;k < pnumpoints;k++)
201 for (l = 0;l < numoriginalplanes;l++)
202 if (fabs(DotProduct(&p[w][k*3], originalplanes[l].normal) - originalplanes[l].dist) < 1.0/8.0)
209 Con_Printf("Collision_NewBrushFromPlanes: warning: polygon point does not lie on at least 3 planes\n");
213 // check if there are too many polygon vertices for buffer
214 if (pnumpoints > pmaxpoints)
216 Con_Print("Collision_NewBrushFromPlanes: failed to build collision brush: too many points for buffer\n");
220 // check if there are too many triangle elements for buffer
221 if (numelementsbuf + (pnumpoints - 2) * 3 > maxelementsbuf)
223 Con_Print("Collision_NewBrushFromPlanes: failed to build collision brush: too many triangle elements for buffer\n");
227 for (k = 0;k < pnumpoints;k++)
229 // check if there is already a matching point (no duplicates)
230 for (m = 0;m < numpointsbuf;m++)
231 if (VectorDistance2(&p[w][k*3], pointsbuf[m].v) < COLLISION_SNAP)
234 // if there is no match, add a new one
235 if (m == numpointsbuf)
237 // check if there are too many and skip the brush
238 if (numpointsbuf >= maxpointsbuf)
240 Con_Print("Collision_NewBrushFromPlanes: failed to build collision brush: too many points for buffer\n");
244 VectorCopy(&p[w][k*3], pointsbuf[numpointsbuf].v);
248 // store the index into a buffer
252 // add the triangles for the polygon
253 // (this particular code makes a triangle fan)
254 for (k = 0;k < pnumpoints - 2;k++)
256 elementsbuf[numelementsbuf++] = polypointbuf[0];
257 elementsbuf[numelementsbuf++] = polypointbuf[k + 1];
258 elementsbuf[numelementsbuf++] = polypointbuf[k + 2];
262 VectorCopy(originalplanes[j].normal, planesbuf[numplanesbuf].normal);
263 planesbuf[numplanesbuf].dist = originalplanes[j].dist;
267 // validate plane distances
268 for (j = 0;j < numplanesbuf;j++)
270 float d = furthestplanedist_float(planesbuf[j].normal, pointsbuf, numpointsbuf);
271 if (fabs(planesbuf[j].dist - d) > (1.0f/32.0f))
272 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);
275 // if nothing is left, there's nothing to allocate
276 if (numelementsbuf < 12 || numplanesbuf < 4 || numpointsbuf < 4)
278 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);
282 // allocate the brush and copy to it
283 brush = Collision_AllocBrushFloat(mempool, numpointsbuf, numplanesbuf, numelementsbuf / 3, supercontents);
284 for (j = 0;j < brush->numpoints;j++)
286 brush->points[j].v[0] = pointsbuf[j].v[0];
287 brush->points[j].v[1] = pointsbuf[j].v[1];
288 brush->points[j].v[2] = pointsbuf[j].v[2];
290 for (j = 0;j < brush->numplanes;j++)
292 brush->planes[j].normal[0] = planesbuf[j].normal[0];
293 brush->planes[j].normal[1] = planesbuf[j].normal[1];
294 brush->planes[j].normal[2] = planesbuf[j].normal[2];
295 brush->planes[j].dist = planesbuf[j].dist;
297 for (j = 0;j < brush->numtriangles * 3;j++)
298 brush->elements[j] = elementsbuf[j];
299 VectorCopy(brush->points[0].v, brush->mins);
300 VectorCopy(brush->points[0].v, brush->maxs);
301 for (j = 1;j < brush->numpoints;j++)
303 brush->mins[0] = min(brush->mins[0], brush->points[j].v[0]);
304 brush->mins[1] = min(brush->mins[1], brush->points[j].v[1]);
305 brush->mins[2] = min(brush->mins[2], brush->points[j].v[2]);
306 brush->maxs[0] = max(brush->maxs[0], brush->points[j].v[0]);
307 brush->maxs[1] = max(brush->maxs[1], brush->points[j].v[1]);
308 brush->maxs[2] = max(brush->maxs[2], brush->points[j].v[2]);
316 Collision_ValidateBrush(brush);
322 colbrushf_t *Collision_AllocBrushFloat(mempool_t *mempool, int numpoints, int numplanes, int numtriangles, int supercontents)
325 brush = Mem_Alloc(mempool, sizeof(colbrushf_t) + sizeof(colpointf_t) * numpoints + sizeof(colplanef_t) * numplanes + sizeof(int[3]) * numtriangles);
326 brush->supercontents = supercontents;
327 brush->numplanes = numplanes;
328 brush->numpoints = numpoints;
329 brush->numtriangles = numtriangles;
330 brush->planes = (void *)(brush + 1);
331 brush->points = (void *)(brush->planes + brush->numplanes);
332 brush->elements = (void *)(brush->points + brush->numpoints);
336 void Collision_CalcPlanesForPolygonBrushFloat(colbrushf_t *brush)
339 float edge0[3], edge1[3], edge2[3], normal[3], dist, bestdist;
342 // FIXME: these probably don't actually need to be normalized if the collision code does not care
343 if (brush->numpoints == 3)
345 // optimized triangle case
346 TriangleNormal(brush->points[0].v, brush->points[1].v, brush->points[2].v, brush->planes[0].normal);
347 if (DotProduct(brush->planes[0].normal, brush->planes[0].normal) < 0.0001f)
349 // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
350 brush->numplanes = 0;
355 brush->numplanes = 5;
356 VectorNormalize(brush->planes[0].normal);
357 brush->planes[0].dist = DotProduct(brush->points->v, brush->planes[0].normal);
358 VectorNegate(brush->planes[0].normal, brush->planes[1].normal);
359 brush->planes[1].dist = -brush->planes[0].dist;
360 VectorSubtract(brush->points[2].v, brush->points[0].v, edge0);
361 VectorSubtract(brush->points[0].v, brush->points[1].v, edge1);
362 VectorSubtract(brush->points[1].v, brush->points[2].v, edge2);
365 float projectionnormal[3], projectionedge0[3], projectionedge1[3], projectionedge2[3];
367 float dist, bestdist;
368 bestdist = fabs(brush->planes[0].normal[0]);
370 for (i = 1;i < 3;i++)
372 dist = fabs(brush->planes[0].normal[i]);
379 VectorClear(projectionnormal);
380 if (brush->planes[0].normal[best] < 0)
381 projectionnormal[best] = -1;
383 projectionnormal[best] = 1;
384 VectorCopy(edge0, projectionedge0);
385 VectorCopy(edge1, projectionedge1);
386 VectorCopy(edge2, projectionedge2);
387 projectionedge0[best] = 0;
388 projectionedge1[best] = 0;
389 projectionedge2[best] = 0;
390 CrossProduct(projectionedge0, projectionnormal, brush->planes[2].normal);
391 CrossProduct(projectionedge1, projectionnormal, brush->planes[3].normal);
392 CrossProduct(projectionedge2, projectionnormal, brush->planes[4].normal);
395 CrossProduct(edge0, brush->planes->normal, brush->planes[2].normal);
396 CrossProduct(edge1, brush->planes->normal, brush->planes[3].normal);
397 CrossProduct(edge2, brush->planes->normal, brush->planes[4].normal);
399 VectorNormalize(brush->planes[2].normal);
400 VectorNormalize(brush->planes[3].normal);
401 VectorNormalize(brush->planes[4].normal);
402 brush->planes[2].dist = DotProduct(brush->points[2].v, brush->planes[2].normal);
403 brush->planes[3].dist = DotProduct(brush->points[0].v, brush->planes[3].normal);
404 brush->planes[4].dist = DotProduct(brush->points[1].v, brush->planes[4].normal);
406 if (developer.integer)
412 VectorSubtract(brush->points[0].v, brush->points[1].v, edge0);
413 VectorSubtract(brush->points[2].v, brush->points[1].v, edge1);
414 CrossProduct(edge0, edge1, normal);
415 VectorNormalize(normal);
416 VectorSubtract(normal, brush->planes[0].normal, temp);
417 if (VectorLength(temp) > 0.01f)
418 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]);
419 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)
420 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);
422 if (fabs(DotProduct(brush->planes[2].normal, brush->planes[0].normal)) > 0.01f)
423 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);
424 if (fabs(DotProduct(brush->planes[3].normal, brush->planes[0].normal)) > 0.01f)
425 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);
426 if (fabs(DotProduct(brush->planes[4].normal, brush->planes[0].normal)) > 0.01f)
427 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);
428 if (fabs(DotProduct(brush->planes[2].normal, edge0)) > 0.01f)
429 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]);
430 if (fabs(DotProduct(brush->planes[3].normal, edge1)) > 0.01f)
431 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]);
432 if (fabs(DotProduct(brush->planes[4].normal, edge2)) > 0.01f)
433 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]);
436 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)
437 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);
438 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)
439 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);
440 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)
441 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);
442 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)
443 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);
444 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)
445 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);
451 // choose best surface normal for polygon's plane
453 for (i = 0, p = brush->points + 1;i < brush->numpoints - 2;i++, p++)
455 VectorSubtract(p[-1].v, p[0].v, edge0);
456 VectorSubtract(p[1].v, p[0].v, edge1);
457 CrossProduct(edge0, edge1, normal);
458 //TriangleNormal(p[-1].v, p[0].v, p[1].v, normal);
459 dist = DotProduct(normal, normal);
460 if (i == 0 || bestdist < dist)
463 VectorCopy(normal, brush->planes->normal);
466 if (bestdist < 0.0001f)
468 // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
469 brush->numplanes = 0;
474 brush->numplanes = brush->numpoints + 2;
475 VectorNormalize(brush->planes->normal);
476 brush->planes->dist = DotProduct(brush->points->v, brush->planes->normal);
478 // negate plane to create other side
479 VectorNegate(brush->planes[0].normal, brush->planes[1].normal);
480 brush->planes[1].dist = -brush->planes[0].dist;
481 for (i = 0, p = brush->points + (brush->numpoints - 1), p2 = brush->points;i < brush->numpoints;i++, p = p2, p2++)
483 VectorSubtract(p->v, p2->v, edge0);
484 CrossProduct(edge0, brush->planes->normal, brush->planes[i + 2].normal);
485 VectorNormalize(brush->planes[i + 2].normal);
486 brush->planes[i + 2].dist = DotProduct(p->v, brush->planes[i + 2].normal);
491 if (developer.integer)
493 // validity check - will be disabled later
494 Collision_ValidateBrush(brush);
495 for (i = 0;i < brush->numplanes;i++)
498 for (j = 0, p = brush->points;j < brush->numpoints;j++, p++)
499 if (DotProduct(p->v, brush->planes[i].normal) > brush->planes[i].dist + (1.0 / 32.0))
500 Con_Printf("Error in brush plane generation, plane %i\n", i);
505 colbrushf_t *Collision_AllocBrushFromPermanentPolygonFloat(mempool_t *mempool, int numpoints, float *points, int supercontents)
508 brush = Mem_Alloc(mempool, sizeof(colbrushf_t) + sizeof(colplanef_t) * (numpoints + 2));
509 brush->supercontents = supercontents;
510 brush->numpoints = numpoints;
511 brush->numplanes = numpoints + 2;
512 brush->planes = (void *)(brush + 1);
513 brush->points = (colpointf_t *)points;
514 Sys_Error("Collision_AllocBrushFromPermanentPolygonFloat: FIXME: this code needs to be updated to generate a mesh...\n");
518 // NOTE: start and end of each brush pair must have same numplanes/numpoints
519 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)
521 int nplane, nplane2, fstartsolid, fendsolid, brushsolid;
522 float enterfrac, leavefrac, d1, d2, f, imove, newimpactnormal[3], enterfrac2;
523 const colplanef_t *startplane, *endplane;
531 for (nplane = 0;nplane < thatbrush_start->numplanes + thisbrush_start->numplanes;nplane++)
534 if (nplane2 >= thatbrush_start->numplanes)
536 nplane2 -= thatbrush_start->numplanes;
537 startplane = thisbrush_start->planes + nplane2;
538 endplane = thisbrush_end->planes + nplane2;
539 if (developer.integer)
541 // any brush with degenerate planes is not worth handling
542 if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
544 Con_Print("Collision_TraceBrushBrushFloat: degenerate thisbrush plane!\n");
547 f = furthestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints);
548 if (fabs(f - startplane->dist) > 0.125f)
549 Con_Printf("startplane->dist %f != calculated %f (thisbrush_start)\n", startplane->dist, f);
551 d1 = nearestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints) - furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints) - collision_startnudge.value;
552 d2 = nearestplanedist_float(endplane->normal, thisbrush_end->points, thisbrush_end->numpoints) - furthestplanedist_float(endplane->normal, thatbrush_end->points, thatbrush_end->numpoints) - collision_endnudge.value;
556 startplane = thatbrush_start->planes + nplane2;
557 endplane = thatbrush_end->planes + nplane2;
558 if (developer.integer)
560 // any brush with degenerate planes is not worth handling
561 if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
563 Con_Print("Collision_TraceBrushBrushFloat: degenerate thatbrush plane!\n");
566 f = furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints);
567 if (fabs(f - startplane->dist) > 0.125f)
568 Con_Printf("startplane->dist %f != calculated %f (thatbrush_start)\n", startplane->dist, f);
570 d1 = nearestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints) - startplane->dist - collision_startnudge.value;
571 d2 = nearestplanedist_float(endplane->normal, thisbrush_end->points, thisbrush_end->numpoints) - endplane->dist - collision_endnudge.value;
573 //Con_Printf("%c%i: d1 = %f, d2 = %f, d1 / (d1 - d2) = %f\n", nplane2 != nplane ? 'b' : 'a', nplane2, d1, d2, d1 / (d1 - d2));
584 imove = 1 / (d1 - d2);
585 f = (d1 - collision_enternudge.value) * imove;
589 enterfrac2 = f - collision_impactnudge.value * imove;
590 VectorLerp(startplane->normal, enterfrac, endplane->normal, newimpactnormal);
596 // moving out of brush
603 f = (d1 + collision_leavenudge.value) / (d1 - d2);
610 brushsolid = trace->hitsupercontentsmask & thatbrush_start->supercontents;
613 trace->startsupercontents |= thatbrush_start->supercontents;
616 trace->startsolid = true;
618 trace->allsolid = true;
622 // LordHavoc: we need an epsilon nudge here because for a point trace the
623 // penetrating line segment is normally zero length if this brush was
624 // generated from a polygon (infinitely thin), and could even be slightly
625 // positive or negative due to rounding errors in that case.
626 if (brushsolid && enterfrac > -1 && enterfrac < trace->realfraction && enterfrac - (1.0f / 1024.0f) <= leavefrac)
630 if (thatbrush_start->ispolygon)
632 d1 = nearestplanedist_float(thatbrush_start->planes[0].normal, thisbrush_start->points, thisbrush_start->numpoints) - thatbrush_start->planes[0].dist - collision_startnudge.value;
633 d2 = nearestplanedist_float(thatbrush_end->planes[0].normal, thisbrush_end->points, thisbrush_end->numpoints) - thatbrush_end->planes[0].dist - collision_endnudge.value;
635 if (move <= 0 || d2 > collision_enternudge.value || d1 < 0)
639 enterfrac = (d1 - collision_enternudge.value) * imove;
640 if (enterfrac < trace->realfraction)
642 enterfrac2 = enterfrac - collision_impactnudge.value * imove;
643 trace->realfraction = bound(0, enterfrac, 1);
644 trace->fraction = bound(0, enterfrac2, 1);
645 VectorLerp(thatbrush_start->planes[0].normal, enterfrac, thatbrush_end->planes[0].normal, trace->plane.normal);
651 trace->realfraction = bound(0, enterfrac, 1);
652 trace->fraction = bound(0, enterfrac2, 1);
653 VectorCopy(newimpactnormal, trace->plane.normal);
658 // NOTE: start and end brush pair must have same numplanes/numpoints
659 void Collision_TraceLineBrushFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, const colbrushf_t *thatbrush_start, const colbrushf_t *thatbrush_end)
661 int nplane, fstartsolid, fendsolid, brushsolid;
662 float enterfrac, leavefrac, d1, d2, f, imove, newimpactnormal[3], enterfrac2;
663 const colplanef_t *startplane, *endplane;
671 for (nplane = 0;nplane < thatbrush_start->numplanes;nplane++)
673 startplane = thatbrush_start->planes + nplane;
674 endplane = thatbrush_end->planes + nplane;
675 d1 = DotProduct(startplane->normal, linestart) - startplane->dist - collision_startnudge.value;
676 d2 = DotProduct(endplane->normal, lineend) - endplane->dist - collision_endnudge.value;
677 if (developer.integer)
679 // any brush with degenerate planes is not worth handling
680 if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
682 Con_Print("Collision_TraceLineBrushFloat: degenerate plane!\n");
685 if (thatbrush_start->numpoints)
687 f = furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints);
688 if (fabs(f - startplane->dist) > 0.125f)
689 Con_Printf("startplane->dist %f != calculated %f\n", startplane->dist, f);
702 imove = 1 / (d1 - d2);
703 f = (d1 - collision_enternudge.value) * imove;
707 enterfrac2 = f - collision_impactnudge.value * imove;
708 VectorLerp(startplane->normal, enterfrac, endplane->normal, newimpactnormal);
714 // moving out of brush
721 f = (d1 + collision_leavenudge.value) / (d1 - d2);
728 brushsolid = trace->hitsupercontentsmask & thatbrush_start->supercontents;
731 trace->startsupercontents |= thatbrush_start->supercontents;
734 trace->startsolid = true;
736 trace->allsolid = true;
740 // LordHavoc: we need an epsilon nudge here because for a point trace the
741 // penetrating line segment is normally zero length if this brush was
742 // generated from a polygon (infinitely thin), and could even be slightly
743 // positive or negative due to rounding errors in that case.
744 if (brushsolid && enterfrac > -1 && enterfrac < trace->realfraction && enterfrac - (1.0f / 1024.0f) <= leavefrac)
748 if (thatbrush_start->ispolygon)
750 d1 = DotProduct(thatbrush_start->planes[0].normal, linestart) - thatbrush_start->planes[0].dist - collision_startnudge.value;
751 d2 = DotProduct(thatbrush_end->planes[0].normal, lineend) - thatbrush_end->planes[0].dist - collision_endnudge.value;
753 if (move <= 0 || d2 > collision_enternudge.value || d1 < 0)
757 enterfrac = (d1 - collision_enternudge.value) * imove;
758 if (enterfrac < trace->realfraction)
760 enterfrac2 = enterfrac - collision_impactnudge.value * imove;
761 trace->realfraction = bound(0, enterfrac, 1);
762 trace->fraction = bound(0, enterfrac2, 1);
763 VectorLerp(thatbrush_start->planes[0].normal, enterfrac, thatbrush_end->planes[0].normal, trace->plane.normal);
769 trace->realfraction = bound(0, enterfrac, 1);
770 trace->fraction = bound(0, enterfrac2, 1);
771 VectorCopy(newimpactnormal, trace->plane.normal);
776 void Collision_TracePointBrushFloat(trace_t *trace, const vec3_t point, const colbrushf_t *thatbrush)
779 const colplanef_t *plane;
781 for (nplane = 0, plane = thatbrush->planes;nplane < thatbrush->numplanes;nplane++, plane++)
782 if (DotProduct(plane->normal, point) > plane->dist)
785 trace->startsupercontents |= thatbrush->supercontents;
786 if (trace->hitsupercontentsmask & thatbrush->supercontents)
788 trace->startsolid = true;
789 trace->allsolid = true;
793 static colpointf_t polyf_points[256];
794 static colplanef_t polyf_planes[256 + 2];
795 static colbrushf_t polyf_brush;
797 void Collision_SnapCopyPoints(int numpoints, const colpointf_t *in, colpointf_t *out, float fractionprecision, float invfractionprecision)
801 out->v[0] = floor(in->v[0] * fractionprecision + 0.5f) * invfractionprecision;
802 out->v[1] = floor(in->v[1] * fractionprecision + 0.5f) * invfractionprecision;
803 out->v[2] = floor(in->v[2] * fractionprecision + 0.5f) * invfractionprecision;
807 void Collision_TraceBrushPolygonFloat(trace_t *trace, const colbrushf_t *thisbrush_start, const colbrushf_t *thisbrush_end, int numpoints, const float *points, int supercontents)
811 Con_Print("Polygon with more than 256 points not supported yet (fixme!)\n");
814 polyf_brush.numpoints = numpoints;
815 polyf_brush.numplanes = numpoints + 2;
816 //polyf_brush.points = (colpointf_t *)points;
817 polyf_brush.planes = polyf_planes;
818 polyf_brush.supercontents = supercontents;
819 polyf_brush.points = polyf_points;
820 Collision_SnapCopyPoints(numpoints, (colpointf_t *)points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
821 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
822 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
823 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brush, &polyf_brush);
826 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, const vec3_t segmentmins, const vec3_t segmentmaxs)
829 float facemins[3], facemaxs[3];
830 polyf_brush.numpoints = 3;
831 polyf_brush.numplanes = 5;
832 polyf_brush.points = polyf_points;
833 polyf_brush.planes = polyf_planes;
834 polyf_brush.supercontents = supercontents;
835 for (i = 0;i < numtriangles;i++, element3i += 3)
837 VectorCopy(vertex3f + element3i[0] * 3, polyf_points[0].v);
838 VectorCopy(vertex3f + element3i[1] * 3, polyf_points[1].v);
839 VectorCopy(vertex3f + element3i[2] * 3, polyf_points[2].v);
840 Collision_SnapCopyPoints(3, polyf_points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
841 facemins[0] = min(polyf_points[0].v[0], min(polyf_points[1].v[0], polyf_points[2].v[0])) - 1;
842 facemins[1] = min(polyf_points[0].v[1], min(polyf_points[1].v[1], polyf_points[2].v[1])) - 1;
843 facemins[2] = min(polyf_points[0].v[2], min(polyf_points[1].v[2], polyf_points[2].v[2])) - 1;
844 facemaxs[0] = max(polyf_points[0].v[0], max(polyf_points[1].v[0], polyf_points[2].v[0])) + 1;
845 facemaxs[1] = max(polyf_points[0].v[1], max(polyf_points[1].v[1], polyf_points[2].v[1])) + 1;
846 facemaxs[2] = max(polyf_points[0].v[2], max(polyf_points[1].v[2], polyf_points[2].v[2])) + 1;
847 if (BoxesOverlap(segmentmins, segmentmaxs, facemins, facemaxs))
849 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
850 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
851 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brush, &polyf_brush);
856 void Collision_TraceLinePolygonFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, int numpoints, const float *points, int supercontents)
860 Con_Print("Polygon with more than 256 points not supported yet (fixme!)\n");
863 polyf_brush.numpoints = numpoints;
864 polyf_brush.numplanes = numpoints + 2;
865 //polyf_brush.points = (colpointf_t *)points;
866 polyf_brush.points = polyf_points;
867 Collision_SnapCopyPoints(numpoints, (colpointf_t *)points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
868 polyf_brush.planes = polyf_planes;
869 polyf_brush.supercontents = supercontents;
870 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
871 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
872 Collision_TraceLineBrushFloat(trace, linestart, lineend, &polyf_brush, &polyf_brush);
875 void Collision_TraceLineTriangleMeshFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, int numtriangles, const int *element3i, const float *vertex3f, int supercontents, const vec3_t segmentmins, const vec3_t segmentmaxs)
879 // FIXME: snap vertices?
880 for (i = 0;i < numtriangles;i++, element3i += 3)
881 Collision_TraceLineTriangleFloat(trace, linestart, lineend, vertex3f + element3i[0] * 3, vertex3f + element3i[1] * 3, vertex3f + element3i[2] * 3);
883 polyf_brush.numpoints = 3;
884 polyf_brush.numplanes = 5;
885 polyf_brush.points = polyf_points;
886 polyf_brush.planes = polyf_planes;
887 polyf_brush.supercontents = supercontents;
888 for (i = 0;i < numtriangles;i++, element3i += 3)
890 float facemins[3], facemaxs[3];
891 VectorCopy(vertex3f + element3i[0] * 3, polyf_points[0].v);
892 VectorCopy(vertex3f + element3i[1] * 3, polyf_points[1].v);
893 VectorCopy(vertex3f + element3i[2] * 3, polyf_points[2].v);
894 Collision_SnapCopyPoints(numpoints, polyf_points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
895 facemins[0] = min(polyf_points[0].v[0], min(polyf_points[1].v[0], polyf_points[2].v[0])) - 1;
896 facemins[1] = min(polyf_points[0].v[1], min(polyf_points[1].v[1], polyf_points[2].v[1])) - 1;
897 facemins[2] = min(polyf_points[0].v[2], min(polyf_points[1].v[2], polyf_points[2].v[2])) - 1;
898 facemaxs[0] = max(polyf_points[0].v[0], max(polyf_points[1].v[0], polyf_points[2].v[0])) + 1;
899 facemaxs[1] = max(polyf_points[0].v[1], max(polyf_points[1].v[1], polyf_points[2].v[1])) + 1;
900 facemaxs[2] = max(polyf_points[0].v[2], max(polyf_points[1].v[2], polyf_points[2].v[2])) + 1;
901 if (BoxesOverlap(segmentmins, segmentmaxs, facemins, facemaxs))
903 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
904 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
905 Collision_TraceLineBrushFloat(trace, linestart, lineend, &polyf_brush, &polyf_brush);
912 static colpointf_t polyf_pointsstart[256], polyf_pointsend[256];
913 static colplanef_t polyf_planesstart[256 + 2], polyf_planesend[256 + 2];
914 static colbrushf_t polyf_brushstart, polyf_brushend;
916 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)
921 Con_Print("Polygon with more than 256 points not supported yet (fixme!)\n");
924 polyf_brushstart.numpoints = numpoints;
925 polyf_brushstart.numplanes = numpoints + 2;
926 polyf_brushstart.points = polyf_pointsstart;//(colpointf_t *)points;
927 polyf_brushstart.planes = polyf_planesstart;
928 polyf_brushstart.supercontents = supercontents;
929 for (i = 0;i < numpoints;i++)
930 Matrix4x4_Transform(polygonmatrixstart, points + i * 3, polyf_brushstart.points[i].v);
931 polyf_brushend.numpoints = numpoints;
932 polyf_brushend.numplanes = numpoints + 2;
933 polyf_brushend.points = polyf_pointsend;//(colpointf_t *)points;
934 polyf_brushend.planes = polyf_planesend;
935 polyf_brushend.supercontents = supercontents;
936 for (i = 0;i < numpoints;i++)
937 Matrix4x4_Transform(polygonmatrixend, points + i * 3, polyf_brushend.points[i].v);
938 Collision_SnapCopyPoints(numpoints, polyf_pointsstart, polyf_pointsstart, COLLISION_SNAPSCALE, COLLISION_SNAP);
939 Collision_SnapCopyPoints(numpoints, polyf_pointsend, polyf_pointsend, COLLISION_SNAPSCALE, COLLISION_SNAP);
940 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brushstart);
941 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brushend);
943 //Collision_PrintBrushAsQHull(&polyf_brushstart, "polyf_brushstart");
944 //Collision_PrintBrushAsQHull(&polyf_brushend, "polyf_brushend");
946 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brushstart, &polyf_brushend);
951 #define MAX_BRUSHFORBOX 16
952 static int brushforbox_index = 0;
953 static colpointf_t brushforbox_point[MAX_BRUSHFORBOX*8];
954 static colplanef_t brushforbox_plane[MAX_BRUSHFORBOX*6];
955 static colbrushf_t brushforbox_brush[MAX_BRUSHFORBOX];
956 static colbrushf_t brushforpoint_brush[MAX_BRUSHFORBOX];
958 void Collision_InitBrushForBox(void)
961 for (i = 0;i < MAX_BRUSHFORBOX;i++)
963 brushforbox_brush[i].supercontents = SUPERCONTENTS_SOLID;
964 brushforbox_brush[i].numpoints = 8;
965 brushforbox_brush[i].numplanes = 6;
966 brushforbox_brush[i].points = brushforbox_point + i * 8;
967 brushforbox_brush[i].planes = brushforbox_plane + i * 6;
968 brushforpoint_brush[i].supercontents = SUPERCONTENTS_SOLID;
969 brushforpoint_brush[i].numpoints = 1;
970 brushforpoint_brush[i].numplanes = 0;
971 brushforpoint_brush[i].points = brushforbox_point + i * 8;
972 brushforpoint_brush[i].planes = brushforbox_plane + i * 6;
976 colbrushf_t *Collision_BrushForBox(const matrix4x4_t *matrix, const vec3_t mins, const vec3_t maxs)
981 if (brushforbox_brush[0].numpoints == 0)
982 Collision_InitBrushForBox();
983 // FIXME: these probably don't actually need to be normalized if the collision code does not care
984 if (VectorCompare(mins, maxs))
987 brush = brushforpoint_brush + ((brushforbox_index++) % MAX_BRUSHFORBOX);
988 VectorCopy(mins, brush->points->v);
992 brush = brushforbox_brush + ((brushforbox_index++) % MAX_BRUSHFORBOX);
994 for (i = 0;i < 8;i++)
996 v[0] = i & 1 ? maxs[0] : mins[0];
997 v[1] = i & 2 ? maxs[1] : mins[1];
998 v[2] = i & 4 ? maxs[2] : mins[2];
999 Matrix4x4_Transform(matrix, v, brush->points[i].v);
1002 for (i = 0;i < 6;i++)
1005 v[i >> 1] = i & 1 ? 1 : -1;
1006 Matrix4x4_Transform3x3(matrix, v, brush->planes[i].normal);
1007 VectorNormalize(brush->planes[i].normal);
1010 for (j = 0;j < brush->numplanes;j++)
1011 brush->planes[j].dist = furthestplanedist_float(brush->planes[j].normal, brush->points, brush->numpoints);
1012 VectorCopy(brush->points[0].v, brush->mins);
1013 VectorCopy(brush->points[0].v, brush->maxs);
1014 for (j = 1;j < brush->numpoints;j++)
1016 brush->mins[0] = min(brush->mins[0], brush->points[j].v[0]);
1017 brush->mins[1] = min(brush->mins[1], brush->points[j].v[1]);
1018 brush->mins[2] = min(brush->mins[2], brush->points[j].v[2]);
1019 brush->maxs[0] = max(brush->maxs[0], brush->points[j].v[0]);
1020 brush->maxs[1] = max(brush->maxs[1], brush->points[j].v[1]);
1021 brush->maxs[2] = max(brush->maxs[2], brush->points[j].v[2]);
1023 brush->mins[0] -= 1;
1024 brush->mins[1] -= 1;
1025 brush->mins[2] -= 1;
1026 brush->maxs[0] += 1;
1027 brush->maxs[1] += 1;
1028 brush->maxs[2] += 1;
1029 Collision_ValidateBrush(brush);
1033 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)
1035 colbrushf_t *boxbrush, *thisbrush_start, *thisbrush_end;
1036 matrix4x4_t identitymatrix;
1037 vec3_t startmins, startmaxs, endmins, endmaxs;
1039 // create brushes for the collision
1040 VectorAdd(start, mins, startmins);
1041 VectorAdd(start, maxs, startmaxs);
1042 VectorAdd(end, mins, endmins);
1043 VectorAdd(end, maxs, endmaxs);
1044 Matrix4x4_CreateIdentity(&identitymatrix);
1045 boxbrush = Collision_BrushForBox(&identitymatrix, cmins, cmaxs);
1046 thisbrush_start = Collision_BrushForBox(&identitymatrix, startmins, startmaxs);
1047 thisbrush_end = Collision_BrushForBox(&identitymatrix, endmins, endmaxs);
1049 memset(trace, 0, sizeof(trace_t));
1050 trace->hitsupercontentsmask = hitsupercontentsmask;
1051 trace->fraction = 1;
1052 trace->realfraction = 1;
1053 trace->allsolid = true;
1054 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, boxbrush, boxbrush);
1057 // LordHavoc: currently unused and not yet tested
1058 // note: this can be used for tracing a moving sphere vs a stationary sphere,
1059 // by simply adding the moving sphere's radius to the sphereradius parameter,
1060 // all the results are correct (impactpoint, impactnormal, and fraction)
1061 float Collision_ClipTrace_Line_Sphere(double *linestart, double *lineend, double *sphereorigin, double sphereradius, double *impactpoint, double *impactnormal)
1063 double dir[3], scale, v[3], deviationdist, impactdist, linelength;
1064 // make sure the impactpoint and impactnormal are valid even if there is
1066 impactpoint[0] = lineend[0];
1067 impactpoint[1] = lineend[1];
1068 impactpoint[2] = lineend[2];
1069 impactnormal[0] = 0;
1070 impactnormal[1] = 0;
1071 impactnormal[2] = 0;
1072 // calculate line direction
1073 dir[0] = lineend[0] - linestart[0];
1074 dir[1] = lineend[1] - linestart[1];
1075 dir[2] = lineend[2] - linestart[2];
1076 // normalize direction
1077 linelength = sqrt(dir[0] * dir[0] + dir[1] * dir[1] + dir[2] * dir[2]);
1080 scale = 1.0 / linelength;
1085 // this dotproduct calculates the distance along the line at which the
1086 // sphere origin is (nearest point to the sphere origin on the line)
1087 impactdist = dir[0] * (sphereorigin[0] - linestart[0]) + dir[1] * (sphereorigin[1] - linestart[1]) + dir[2] * (sphereorigin[2] - linestart[2]);
1088 // calculate point on line at that distance, and subtract the
1089 // sphereorigin from it, so we have a vector to measure for the distance
1090 // of the line from the sphereorigin (deviation, how off-center it is)
1091 v[0] = linestart[0] + impactdist * dir[0] - sphereorigin[0];
1092 v[1] = linestart[1] + impactdist * dir[1] - sphereorigin[1];
1093 v[2] = linestart[2] + impactdist * dir[2] - sphereorigin[2];
1094 deviationdist = v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
1095 // if outside the radius, it's a miss for sure
1096 // (we do this comparison using squared radius to avoid a sqrt)
1097 if (deviationdist > sphereradius*sphereradius)
1098 return 1; // miss (off to the side)
1099 // nudge back to find the correct impact distance
1100 impactdist += (sqrt(deviationdist) - sphereradius);
1101 if (impactdist >= linelength)
1102 return 1; // miss (not close enough)
1104 return 1; // miss (linestart is past or inside sphere)
1105 // calculate new impactpoint
1106 impactpoint[0] = linestart[0] + impactdist * dir[0];
1107 impactpoint[1] = linestart[1] + impactdist * dir[1];
1108 impactpoint[2] = linestart[2] + impactdist * dir[2];
1109 // calculate impactnormal (surface normal at point of impact)
1110 impactnormal[0] = impactpoint[0] - sphereorigin[0];
1111 impactnormal[1] = impactpoint[1] - sphereorigin[1];
1112 impactnormal[2] = impactpoint[2] - sphereorigin[2];
1113 // normalize impactnormal
1114 scale = impactnormal[0] * impactnormal[0] + impactnormal[1] * impactnormal[1] + impactnormal[2] * impactnormal[2];
1117 scale = 1.0 / sqrt(scale);
1118 impactnormal[0] *= scale;
1119 impactnormal[1] *= scale;
1120 impactnormal[2] *= scale;
1122 // return fraction of movement distance
1123 return impactdist / linelength;
1126 void Collision_TraceLineTriangleFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, const float *point0, const float *point1, const float *point2)
1130 float d1, d2, d, f, impact[3], edgenormal[3], faceplanenormal[3], faceplanedist, faceplanenormallength2, edge01[3], edge21[3], edge02[3];
1132 // this function executes:
1133 // 32 ops when line starts behind triangle
1134 // 38 ops when line ends infront of triangle
1135 // 43 ops when line fraction is already closer than this triangle
1136 // 72 ops when line is outside edge 01
1137 // 92 ops when line is outside edge 21
1138 // 115 ops when line is outside edge 02
1139 // 123 ops when line impacts triangle and updates trace results
1141 // this code is designed for clockwise triangles, conversion to
1142 // counterclockwise would require swapping some things around...
1143 // it is easier to simply swap the point0 and point2 parameters to this
1144 // function when calling it than it is to rewire the internals.
1146 // calculate the faceplanenormal of the triangle, this represents the front side
1148 VectorSubtract(point0, point1, edge01);
1149 VectorSubtract(point2, point1, edge21);
1150 CrossProduct(edge01, edge21, faceplanenormal);
1151 // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
1153 faceplanenormallength2 = DotProduct(faceplanenormal, faceplanenormal);
1154 if (faceplanenormallength2 < 0.0001f)
1156 // calculate the distance
1158 faceplanedist = DotProduct(point0, faceplanenormal);
1160 // if start point is on the back side there is no collision
1161 // (we don't care about traces going through the triangle the wrong way)
1163 // calculate the start distance
1165 d1 = DotProduct(faceplanenormal, linestart);
1166 if (d1 <= faceplanedist)
1169 // calculate the end distance
1171 d2 = DotProduct(faceplanenormal, lineend);
1172 // if both are in front, there is no collision
1173 if (d2 >= faceplanedist)
1176 // from here on we know d1 is >= 0 and d2 is < 0
1177 // this means the line starts infront and ends behind, passing through it
1179 // calculate the recipricol of the distance delta,
1180 // so we can use it multiple times cheaply (instead of division)
1182 d = 1.0f / (d1 - d2);
1183 // calculate the impact fraction by taking the start distance (> 0)
1184 // and subtracting the face plane distance (this is the distance of the
1185 // triangle along that same normal)
1186 // then multiply by the recipricol distance delta
1188 f = (d1 - faceplanedist) * d;
1189 // skip out if this impact is further away than previous ones
1191 if (f > trace->realfraction)
1193 // calculate the perfect impact point for classification of insidedness
1195 impact[0] = linestart[0] + f * (lineend[0] - linestart[0]);
1196 impact[1] = linestart[1] + f * (lineend[1] - linestart[1]);
1197 impact[2] = linestart[2] + f * (lineend[2] - linestart[2]);
1199 // calculate the edge normal and reject if impact is outside triangle
1200 // (an edge normal faces away from the triangle, to get the desired normal
1201 // a crossproduct with the faceplanenormal is used, and because of the way
1202 // the insidedness comparison is written it does not need to be normalized)
1204 // first use the two edges from the triangle plane math
1205 // the other edge only gets calculated if the point survives that long
1208 CrossProduct(edge01, faceplanenormal, edgenormal);
1209 if (DotProduct(impact, edgenormal) > DotProduct(point1, edgenormal))
1213 CrossProduct(faceplanenormal, edge21, edgenormal);
1214 if (DotProduct(impact, edgenormal) > DotProduct(point2, edgenormal))
1218 VectorSubtract(point0, point2, edge02);
1219 CrossProduct(faceplanenormal, edge02, edgenormal);
1220 if (DotProduct(impact, edgenormal) > DotProduct(point0, edgenormal))
1225 // store the new trace fraction
1226 trace->realfraction = f;
1228 // calculate a nudged fraction to keep it out of the surface
1229 // (the main fraction remains perfect)
1230 trace->fraction = f - collision_impactnudge.value * d;
1232 // store the new trace plane (because collisions only happen from
1233 // the front this is always simply the triangle normal, never flipped)
1234 d = 1.0 / sqrt(faceplanenormallength2);
1235 VectorScale(faceplanenormal, d, trace->plane.normal);
1236 trace->plane.dist = faceplanedist * d;
1238 float d1, d2, d, f, fnudged, impact[3], edgenormal[3], faceplanenormal[3], faceplanedist, edge[3];
1240 // this code is designed for clockwise triangles, conversion to
1241 // counterclockwise would require swapping some things around...
1242 // it is easier to simply swap the point0 and point2 parameters to this
1243 // function when calling it than it is to rewire the internals.
1245 // calculate the unnormalized faceplanenormal of the triangle,
1246 // this represents the front side
1247 TriangleNormal(point0, point1, point2, faceplanenormal);
1248 // there's no point in processing a degenerate triangle
1249 // (GIGO - Garbage In, Garbage Out)
1250 if (DotProduct(faceplanenormal, faceplanenormal) < 0.0001f)
1252 // calculate the unnormalized distance
1253 faceplanedist = DotProduct(point0, faceplanenormal);
1255 // calculate the unnormalized start distance
1256 d1 = DotProduct(faceplanenormal, linestart) - faceplanedist;
1257 // if start point is on the back side there is no collision
1258 // (we don't care about traces going through the triangle the wrong way)
1262 // calculate the unnormalized end distance
1263 d2 = DotProduct(faceplanenormal, lineend) - faceplanedist;
1264 // if both are in front, there is no collision
1268 // from here on we know d1 is >= 0 and d2 is < 0
1269 // this means the line starts infront and ends behind, passing through it
1271 // calculate the recipricol of the distance delta,
1272 // so we can use it multiple times cheaply (instead of division)
1273 d = 1.0f / (d1 - d2);
1274 // calculate the impact fraction by taking the start distance (> 0)
1275 // and subtracting the face plane distance (this is the distance of the
1276 // triangle along that same normal)
1277 // then multiply by the recipricol distance delta
1279 // skip out if this impact is further away than previous ones
1280 if (f > trace->realfraction)
1282 // calculate the perfect impact point for classification of insidedness
1283 impact[0] = linestart[0] + f * (lineend[0] - linestart[0]);
1284 impact[1] = linestart[1] + f * (lineend[1] - linestart[1]);
1285 impact[2] = linestart[2] + f * (lineend[2] - linestart[2]);
1287 // calculate the edge normal and reject if impact is outside triangle
1288 // (an edge normal faces away from the triangle, to get the desired normal
1289 // a crossproduct with the faceplanenormal is used, and because of the way
1290 // the insidedness comparison is written it does not need to be normalized)
1292 VectorSubtract(point2, point0, edge);
1293 CrossProduct(edge, faceplanenormal, edgenormal);
1294 if (DotProduct(impact, edgenormal) > DotProduct(point0, edgenormal))
1297 VectorSubtract(point0, point1, edge);
1298 CrossProduct(edge, faceplanenormal, edgenormal);
1299 if (DotProduct(impact, edgenormal) > DotProduct(point1, edgenormal))
1302 VectorSubtract(point1, point2, edge);
1303 CrossProduct(edge, faceplanenormal, edgenormal);
1304 if (DotProduct(impact, edgenormal) > DotProduct(point2, edgenormal))
1307 // store the new trace fraction
1308 trace->realfraction = bound(0, f, 1);
1310 // store the new trace plane (because collisions only happen from
1311 // the front this is always simply the triangle normal, never flipped)
1312 VectorNormalize(faceplanenormal);
1313 VectorCopy(faceplanenormal, trace->plane.normal);
1314 trace->plane.dist = DotProduct(point0, faceplanenormal);
1316 // calculate the normalized start and end distances
1317 d1 = DotProduct(trace->plane.normal, linestart) - trace->plane.dist;
1318 d2 = DotProduct(trace->plane.normal, lineend) - trace->plane.dist;
1320 // calculate a nudged fraction to keep it out of the surface
1321 // (the main fraction remains perfect)
1322 fnudged = (d1 - collision_impactnudge.value) / (d1 - d2);
1323 trace->fraction = bound(0, fnudged, 1);
1325 // store the new trace endpos
1326 // not needed, it's calculated later when the trace is finished
1327 //trace->endpos[0] = linestart[0] + fnudged * (lineend[0] - linestart[0]);
1328 //trace->endpos[1] = linestart[1] + fnudged * (lineend[1] - linestart[1]);
1329 //trace->endpos[2] = linestart[2] + fnudged * (lineend[2] - linestart[2]);
1333 typedef struct colbspnode_s
1336 struct colbspnode_s *children[2];
1337 // the node is reallocated or split if max is reached
1340 colbrushf_t **colbrushflist;
1343 //colbrushd_t **colbrushdlist;
1347 typedef struct colbsp_s
1350 colbspnode_t *nodes;
1354 colbsp_t *Collision_CreateCollisionBSP(mempool_t *mempool)
1357 bsp = Mem_Alloc(mempool, sizeof(colbsp_t));
1358 bsp->mempool = mempool;
1359 bsp->nodes = Mem_Alloc(bsp->mempool, sizeof(colbspnode_t));
1363 void Collision_FreeCollisionBSPNode(colbspnode_t *node)
1365 if (node->children[0])
1366 Collision_FreeCollisionBSPNode(node->children[0]);
1367 if (node->children[1])
1368 Collision_FreeCollisionBSPNode(node->children[1]);
1369 while (--node->numcolbrushf)
1370 Mem_Free(node->colbrushflist[node->numcolbrushf]);
1371 //while (--node->numcolbrushd)
1372 // Mem_Free(node->colbrushdlist[node->numcolbrushd]);
1376 void Collision_FreeCollisionBSP(colbsp_t *bsp)
1378 Collision_FreeCollisionBSPNode(bsp->nodes);
1382 void Collision_BoundingBoxOfBrushTraceSegment(const colbrushf_t *start, const colbrushf_t *end, vec3_t mins, vec3_t maxs, float startfrac, float endfrac)
1385 colpointf_t *ps, *pe;
1386 float tempstart[3], tempend[3];
1387 VectorLerp(start->points[0].v, startfrac, end->points[0].v, mins);
1388 VectorCopy(mins, maxs);
1389 for (i = 0, ps = start->points, pe = end->points;i < start->numpoints;i++, ps++, pe++)
1391 VectorLerp(ps->v, startfrac, pe->v, tempstart);
1392 VectorLerp(ps->v, endfrac, pe->v, tempend);
1393 mins[0] = min(mins[0], min(tempstart[0], tempend[0]));
1394 mins[1] = min(mins[1], min(tempstart[1], tempend[1]));
1395 mins[2] = min(mins[2], min(tempstart[2], tempend[2]));
1396 maxs[0] = min(maxs[0], min(tempstart[0], tempend[0]));
1397 maxs[1] = min(maxs[1], min(tempstart[1], tempend[1]));
1398 maxs[2] = min(maxs[2], min(tempstart[2], tempend[2]));