5 #define COLLISION_SNAPSCALE (8.0f)
6 #define COLLISION_SNAP (1.0f / COLLISION_SNAPSCALE)
8 cvar_t collision_impactnudge = {0, "collision_impactnudge", "0.03125", "how much to back off from the impact"};
9 cvar_t collision_startnudge = {0, "collision_startnudge", "0", "how much to bias collision trace start"};
10 cvar_t collision_endnudge = {0, "collision_endnudge", "0", "how much to bias collision trace end"};
11 cvar_t collision_enternudge = {0, "collision_enternudge", "0", "how much to bias collision entry fraction"};
12 cvar_t collision_leavenudge = {0, "collision_leavenudge", "0", "how much to bias collision exit fraction"};
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 colplanef_t *originalplanes, int supercontents)
138 // TODO: planesbuf could be replaced by a remapping table
140 int numpointsbuf = 0, maxpointsbuf = 256, numplanesbuf = 0, maxplanesbuf = 256, numelementsbuf = 0, maxelementsbuf = 256;
142 colpointf_t pointsbuf[256];
143 colplanef_t planesbuf[256];
144 int elementsbuf[1024];
145 int polypointbuf[256];
150 // enable these if debugging to avoid seeing garbage in unused data
151 memset(pointsbuf, 0, sizeof(pointsbuf));
152 memset(planesbuf, 0, sizeof(planesbuf));
153 memset(elementsbuf, 0, sizeof(elementsbuf));
154 memset(polypointbuf, 0, sizeof(polypointbuf));
155 memset(p, 0, sizeof(p));
157 // construct a collision brush (points, planes, and renderable mesh) from
158 // a set of planes, this also optimizes out any unnecessary planes (ones
159 // whose polygon is clipped away by the other planes)
160 for (j = 0;j < numoriginalplanes;j++)
162 // add the plane uniquely (no duplicates)
163 for (k = 0;k < numplanesbuf;k++)
164 if (VectorCompare(planesbuf[k].normal, originalplanes[j].normal) && planesbuf[k].dist == originalplanes[j].dist)
166 // if the plane is a duplicate, skip it
167 if (k < numplanesbuf)
169 // check if there are too many and skip the brush
170 if (numplanesbuf >= maxplanesbuf)
172 Con_Print("Collision_NewBrushFromPlanes: failed to build collision brush: too many planes for buffer\n");
176 // create a large polygon from the plane
178 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);
180 // clip it by all other planes
181 for (k = 0;k < numoriginalplanes && pnumpoints && pnumpoints <= pmaxpoints;k++)
185 // we want to keep the inside of the brush plane so we flip
187 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, NULL);
191 // if nothing is left, skip it
194 //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);
198 for (k = 0;k < pnumpoints;k++)
202 for (l = 0;l < numoriginalplanes;l++)
203 if (fabs(DotProduct(&p[w][k*3], originalplanes[l].normal) - originalplanes[l].dist) < 1.0/8.0)
210 Con_Printf("Collision_NewBrushFromPlanes: warning: polygon point does not lie on at least 3 planes\n");
214 // check if there are too many polygon vertices for buffer
215 if (pnumpoints > pmaxpoints)
217 Con_Print("Collision_NewBrushFromPlanes: failed to build collision brush: too many points for buffer\n");
221 // check if there are too many triangle elements for buffer
222 if (numelementsbuf + (pnumpoints - 2) * 3 > maxelementsbuf)
224 Con_Print("Collision_NewBrushFromPlanes: failed to build collision brush: too many triangle elements for buffer\n");
228 for (k = 0;k < pnumpoints;k++)
230 // check if there is already a matching point (no duplicates)
231 for (m = 0;m < numpointsbuf;m++)
232 if (VectorDistance2(&p[w][k*3], pointsbuf[m].v) < COLLISION_SNAP)
235 // if there is no match, add a new one
236 if (m == numpointsbuf)
238 // check if there are too many and skip the brush
239 if (numpointsbuf >= maxpointsbuf)
241 Con_Print("Collision_NewBrushFromPlanes: failed to build collision brush: too many points for buffer\n");
245 VectorCopy(&p[w][k*3], pointsbuf[numpointsbuf].v);
249 // store the index into a buffer
253 // add the triangles for the polygon
254 // (this particular code makes a triangle fan)
255 for (k = 0;k < pnumpoints - 2;k++)
257 elementsbuf[numelementsbuf++] = polypointbuf[0];
258 elementsbuf[numelementsbuf++] = polypointbuf[k + 1];
259 elementsbuf[numelementsbuf++] = polypointbuf[k + 2];
263 VectorCopy(originalplanes[j].normal, planesbuf[numplanesbuf].normal);
264 planesbuf[numplanesbuf].dist = originalplanes[j].dist;
265 planesbuf[numplanesbuf].q3surfaceflags = originalplanes[j].q3surfaceflags;
266 planesbuf[numplanesbuf].texture = originalplanes[j].texture;
270 // validate plane distances
271 for (j = 0;j < numplanesbuf;j++)
273 float d = furthestplanedist_float(planesbuf[j].normal, pointsbuf, numpointsbuf);
274 if (fabs(planesbuf[j].dist - d) > (1.0f/32.0f))
275 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);
278 // if nothing is left, there's nothing to allocate
279 if (numelementsbuf < 12 || numplanesbuf < 4 || numpointsbuf < 4)
281 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);
285 // allocate the brush and copy to it
286 brush = (colbrushf_t *)Mem_Alloc(mempool, sizeof(colbrushf_t) + sizeof(colpointf_t) * numpointsbuf + sizeof(colplanef_t) * numplanesbuf + sizeof(int) * numelementsbuf);
287 brush->supercontents = supercontents;
288 brush->numplanes = numplanesbuf;
289 brush->numpoints = numpointsbuf;
290 brush->numtriangles = numelementsbuf / 3;
291 brush->planes = (colplanef_t *)(brush + 1);
292 brush->points = (colpointf_t *)(brush->planes + brush->numplanes);
293 brush->elements = (int *)(brush->points + brush->numpoints);
294 for (j = 0;j < brush->numpoints;j++)
296 brush->points[j].v[0] = pointsbuf[j].v[0];
297 brush->points[j].v[1] = pointsbuf[j].v[1];
298 brush->points[j].v[2] = pointsbuf[j].v[2];
300 for (j = 0;j < brush->numplanes;j++)
302 brush->planes[j].normal[0] = planesbuf[j].normal[0];
303 brush->planes[j].normal[1] = planesbuf[j].normal[1];
304 brush->planes[j].normal[2] = planesbuf[j].normal[2];
305 brush->planes[j].dist = planesbuf[j].dist;
306 brush->planes[j].q3surfaceflags = planesbuf[j].q3surfaceflags;
307 brush->planes[j].texture = planesbuf[j].texture;
309 for (j = 0;j < brush->numtriangles * 3;j++)
310 brush->elements[j] = elementsbuf[j];
311 VectorCopy(brush->points[0].v, brush->mins);
312 VectorCopy(brush->points[0].v, brush->maxs);
313 for (j = 1;j < brush->numpoints;j++)
315 brush->mins[0] = min(brush->mins[0], brush->points[j].v[0]);
316 brush->mins[1] = min(brush->mins[1], brush->points[j].v[1]);
317 brush->mins[2] = min(brush->mins[2], brush->points[j].v[2]);
318 brush->maxs[0] = max(brush->maxs[0], brush->points[j].v[0]);
319 brush->maxs[1] = max(brush->maxs[1], brush->points[j].v[1]);
320 brush->maxs[2] = max(brush->maxs[2], brush->points[j].v[2]);
328 Collision_ValidateBrush(brush);
334 void Collision_CalcPlanesForPolygonBrushFloat(colbrushf_t *brush)
337 float edge0[3], edge1[3], edge2[3], normal[3], dist, bestdist;
340 // FIXME: these probably don't actually need to be normalized if the collision code does not care
341 if (brush->numpoints == 3)
343 // optimized triangle case
344 TriangleNormal(brush->points[0].v, brush->points[1].v, brush->points[2].v, brush->planes[0].normal);
345 if (DotProduct(brush->planes[0].normal, brush->planes[0].normal) < 0.0001f)
347 // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
348 brush->numplanes = 0;
353 brush->numplanes = 5;
354 VectorNormalize(brush->planes[0].normal);
355 brush->planes[0].dist = DotProduct(brush->points->v, brush->planes[0].normal);
356 VectorNegate(brush->planes[0].normal, brush->planes[1].normal);
357 brush->planes[1].dist = -brush->planes[0].dist;
358 VectorSubtract(brush->points[2].v, brush->points[0].v, edge0);
359 VectorSubtract(brush->points[0].v, brush->points[1].v, edge1);
360 VectorSubtract(brush->points[1].v, brush->points[2].v, edge2);
363 float projectionnormal[3], projectionedge0[3], projectionedge1[3], projectionedge2[3];
365 float dist, bestdist;
366 bestdist = fabs(brush->planes[0].normal[0]);
368 for (i = 1;i < 3;i++)
370 dist = fabs(brush->planes[0].normal[i]);
377 VectorClear(projectionnormal);
378 if (brush->planes[0].normal[best] < 0)
379 projectionnormal[best] = -1;
381 projectionnormal[best] = 1;
382 VectorCopy(edge0, projectionedge0);
383 VectorCopy(edge1, projectionedge1);
384 VectorCopy(edge2, projectionedge2);
385 projectionedge0[best] = 0;
386 projectionedge1[best] = 0;
387 projectionedge2[best] = 0;
388 CrossProduct(projectionedge0, projectionnormal, brush->planes[2].normal);
389 CrossProduct(projectionedge1, projectionnormal, brush->planes[3].normal);
390 CrossProduct(projectionedge2, projectionnormal, brush->planes[4].normal);
393 CrossProduct(edge0, brush->planes->normal, brush->planes[2].normal);
394 CrossProduct(edge1, brush->planes->normal, brush->planes[3].normal);
395 CrossProduct(edge2, brush->planes->normal, brush->planes[4].normal);
397 VectorNormalize(brush->planes[2].normal);
398 VectorNormalize(brush->planes[3].normal);
399 VectorNormalize(brush->planes[4].normal);
400 brush->planes[2].dist = DotProduct(brush->points[2].v, brush->planes[2].normal);
401 brush->planes[3].dist = DotProduct(brush->points[0].v, brush->planes[3].normal);
402 brush->planes[4].dist = DotProduct(brush->points[1].v, brush->planes[4].normal);
404 if (developer.integer >= 100)
410 VectorSubtract(brush->points[0].v, brush->points[1].v, edge0);
411 VectorSubtract(brush->points[2].v, brush->points[1].v, edge1);
412 CrossProduct(edge0, edge1, normal);
413 VectorNormalize(normal);
414 VectorSubtract(normal, brush->planes[0].normal, temp);
415 if (VectorLength(temp) > 0.01f)
416 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]);
417 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)
418 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);
420 if (fabs(DotProduct(brush->planes[2].normal, brush->planes[0].normal)) > 0.01f)
421 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);
422 if (fabs(DotProduct(brush->planes[3].normal, brush->planes[0].normal)) > 0.01f)
423 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);
424 if (fabs(DotProduct(brush->planes[4].normal, brush->planes[0].normal)) > 0.01f)
425 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);
426 if (fabs(DotProduct(brush->planes[2].normal, edge0)) > 0.01f)
427 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]);
428 if (fabs(DotProduct(brush->planes[3].normal, edge1)) > 0.01f)
429 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]);
430 if (fabs(DotProduct(brush->planes[4].normal, edge2)) > 0.01f)
431 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]);
434 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)
435 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);
436 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)
437 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);
438 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)
439 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);
440 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)
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[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);
442 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)
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[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);
449 // choose best surface normal for polygon's plane
451 for (i = 0, p = brush->points + 1;i < brush->numpoints - 2;i++, p++)
453 VectorSubtract(p[-1].v, p[0].v, edge0);
454 VectorSubtract(p[1].v, p[0].v, edge1);
455 CrossProduct(edge0, edge1, normal);
456 //TriangleNormal(p[-1].v, p[0].v, p[1].v, normal);
457 dist = DotProduct(normal, normal);
458 if (i == 0 || bestdist < dist)
461 VectorCopy(normal, brush->planes->normal);
464 if (bestdist < 0.0001f)
466 // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
467 brush->numplanes = 0;
472 brush->numplanes = brush->numpoints + 2;
473 VectorNormalize(brush->planes->normal);
474 brush->planes->dist = DotProduct(brush->points->v, brush->planes->normal);
476 // negate plane to create other side
477 VectorNegate(brush->planes[0].normal, brush->planes[1].normal);
478 brush->planes[1].dist = -brush->planes[0].dist;
479 for (i = 0, p = brush->points + (brush->numpoints - 1), p2 = brush->points;i < brush->numpoints;i++, p = p2, p2++)
481 VectorSubtract(p->v, p2->v, edge0);
482 CrossProduct(edge0, brush->planes->normal, brush->planes[i + 2].normal);
483 VectorNormalize(brush->planes[i + 2].normal);
484 brush->planes[i + 2].dist = DotProduct(p->v, brush->planes[i + 2].normal);
489 if (developer.integer >= 100)
491 // validity check - will be disabled later
492 Collision_ValidateBrush(brush);
493 for (i = 0;i < brush->numplanes;i++)
496 for (j = 0, p = brush->points;j < brush->numpoints;j++, p++)
497 if (DotProduct(p->v, brush->planes[i].normal) > brush->planes[i].dist + (1.0 / 32.0))
498 Con_Printf("Error in brush plane generation, plane %i\n", i);
503 colbrushf_t *Collision_AllocBrushFromPermanentPolygonFloat(mempool_t *mempool, int numpoints, float *points, int supercontents)
506 brush = (colbrushf_t *)Mem_Alloc(mempool, sizeof(colbrushf_t) + sizeof(colplanef_t) * (numpoints + 2));
507 brush->supercontents = supercontents;
508 brush->numpoints = numpoints;
509 brush->numplanes = numpoints + 2;
510 brush->planes = (colplanef_t *)(brush + 1);
511 brush->points = (colpointf_t *)points;
512 Sys_Error("Collision_AllocBrushFromPermanentPolygonFloat: FIXME: this code needs to be updated to generate a mesh...");
516 // NOTE: start and end of each brush pair must have same numplanes/numpoints
517 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)
519 int nplane, nplane2, fstartsolid = true, fendsolid = true, brushsolid, hitq3surfaceflags = 0;
520 float enterfrac = -1, leavefrac = 1, d1, d2, f, imove, newimpactnormal[3], enterfrac2 = -1;
521 const colplanef_t *startplane, *endplane;
522 texture_t *hittexture = NULL;
524 VectorClear(newimpactnormal);
526 for (nplane = 0;nplane < thatbrush_start->numplanes + thisbrush_start->numplanes;nplane++)
529 if (nplane2 >= thatbrush_start->numplanes)
531 nplane2 -= thatbrush_start->numplanes;
532 startplane = thisbrush_start->planes + nplane2;
533 endplane = thisbrush_end->planes + nplane2;
534 if (developer.integer >= 100)
536 // any brush with degenerate planes is not worth handling
537 if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
539 Con_Print("Collision_TraceBrushBrushFloat: degenerate thisbrush plane!\n");
542 f = furthestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints);
543 if (fabs(f - startplane->dist) > 0.125f)
544 Con_Printf("startplane->dist %f != calculated %f (thisbrush_start)\n", startplane->dist, f);
546 d1 = nearestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints) - furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints) - collision_startnudge.value;
547 d2 = nearestplanedist_float(endplane->normal, thisbrush_end->points, thisbrush_end->numpoints) - furthestplanedist_float(endplane->normal, thatbrush_end->points, thatbrush_end->numpoints) - collision_endnudge.value;
551 startplane = thatbrush_start->planes + nplane2;
552 endplane = thatbrush_end->planes + nplane2;
553 if (developer.integer >= 100)
555 // any brush with degenerate planes is not worth handling
556 if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
558 Con_Print("Collision_TraceBrushBrushFloat: degenerate thatbrush plane!\n");
561 f = furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints);
562 if (fabs(f - startplane->dist) > 0.125f)
563 Con_Printf("startplane->dist %f != calculated %f (thatbrush_start)\n", startplane->dist, f);
565 d1 = nearestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints) - startplane->dist - collision_startnudge.value;
566 d2 = nearestplanedist_float(endplane->normal, thisbrush_end->points, thisbrush_end->numpoints) - endplane->dist - collision_endnudge.value;
568 //Con_Printf("%c%i: d1 = %f, d2 = %f, d1 / (d1 - d2) = %f\n", nplane2 != nplane ? 'b' : 'a', nplane2, d1, d2, d1 / (d1 - d2));
579 imove = 1 / (d1 - d2);
580 f = (d1 - collision_enternudge.value) * imove;
584 enterfrac2 = f - collision_impactnudge.value * imove;
585 VectorLerp(startplane->normal, enterfrac, endplane->normal, newimpactnormal);
586 hitq3surfaceflags = startplane->q3surfaceflags;
587 hittexture = startplane->texture;
593 // moving out of brush
600 f = (d1 + collision_leavenudge.value) / (d1 - d2);
607 brushsolid = trace->hitsupercontentsmask & thatbrush_start->supercontents;
610 trace->startsupercontents |= thatbrush_start->supercontents;
613 trace->startsolid = true;
615 trace->allsolid = true;
619 // LordHavoc: we need an epsilon nudge here because for a point trace the
620 // penetrating line segment is normally zero length if this brush was
621 // generated from a polygon (infinitely thin), and could even be slightly
622 // positive or negative due to rounding errors in that case.
623 if (brushsolid && enterfrac > -1 && enterfrac < trace->realfraction && enterfrac - (1.0f / 1024.0f) <= leavefrac)
627 if (thatbrush_start->ispolygon)
629 d1 = nearestplanedist_float(thatbrush_start->planes[0].normal, thisbrush_start->points, thisbrush_start->numpoints) - thatbrush_start->planes[0].dist - collision_startnudge.value;
630 d2 = nearestplanedist_float(thatbrush_end->planes[0].normal, thisbrush_end->points, thisbrush_end->numpoints) - thatbrush_end->planes[0].dist - collision_endnudge.value;
632 if (move <= 0 || d2 > collision_enternudge.value || d1 < 0)
636 enterfrac = (d1 - collision_enternudge.value) * imove;
637 if (enterfrac < trace->realfraction)
639 enterfrac2 = enterfrac - collision_impactnudge.value * imove;
640 trace->hitsupercontents = thatbrush_start->supercontents;
641 trace->hitq3surfaceflags = thatbrush_start->planes[0].q3surfaceflags;
642 trace->hittexture = thatbrush_start->planes[0].texture;
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->hitsupercontents = thatbrush_start->supercontents;
652 trace->hitq3surfaceflags = hitq3surfaceflags;
653 trace->hittexture = hittexture;
654 trace->realfraction = bound(0, enterfrac, 1);
655 trace->fraction = bound(0, enterfrac2, 1);
656 VectorCopy(newimpactnormal, trace->plane.normal);
661 // NOTE: start and end brush pair must have same numplanes/numpoints
662 void Collision_TraceLineBrushFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, const colbrushf_t *thatbrush_start, const colbrushf_t *thatbrush_end)
664 int nplane, fstartsolid = true, fendsolid = true, brushsolid, hitq3surfaceflags = 0;
665 float enterfrac = -1, leavefrac = 1, d1, d2, f, imove, newimpactnormal[3], enterfrac2 = -1;
666 const colplanef_t *startplane, *endplane;
667 texture_t *hittexture = NULL;
669 VectorClear(newimpactnormal);
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 >= 100)
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);
709 hitq3surfaceflags = startplane->q3surfaceflags;
710 hittexture = startplane->texture;
716 // moving out of brush
723 f = (d1 + collision_leavenudge.value) / (d1 - d2);
730 brushsolid = trace->hitsupercontentsmask & thatbrush_start->supercontents;
733 trace->startsupercontents |= thatbrush_start->supercontents;
736 trace->startsolid = true;
738 trace->allsolid = true;
742 // LordHavoc: we need an epsilon nudge here because for a point trace the
743 // penetrating line segment is normally zero length if this brush was
744 // generated from a polygon (infinitely thin), and could even be slightly
745 // positive or negative due to rounding errors in that case.
746 if (brushsolid && enterfrac > -1 && enterfrac < trace->realfraction && enterfrac <= leavefrac)
750 if (thatbrush_start->ispolygon)
752 d1 = DotProduct(thatbrush_start->planes[0].normal, linestart) - thatbrush_start->planes[0].dist - collision_startnudge.value;
753 d2 = DotProduct(thatbrush_end->planes[0].normal, lineend) - thatbrush_end->planes[0].dist - collision_endnudge.value;
755 if (move <= 0 || d2 > collision_enternudge.value || d1 < 0)
759 enterfrac = (d1 - collision_enternudge.value) * imove;
760 if (enterfrac < trace->realfraction)
762 enterfrac2 = enterfrac - collision_impactnudge.value * imove;
763 trace->hitsupercontents = thatbrush_start->supercontents;
764 trace->hitq3surfaceflags = hitq3surfaceflags;
765 trace->hittexture = hittexture;
766 trace->realfraction = bound(0, enterfrac, 1);
767 trace->fraction = bound(0, enterfrac2, 1);
768 VectorLerp(thatbrush_start->planes[0].normal, enterfrac, thatbrush_end->planes[0].normal, trace->plane.normal);
774 trace->hitsupercontents = thatbrush_start->supercontents;
775 trace->hitq3surfaceflags = hitq3surfaceflags;
776 trace->hittexture = hittexture;
777 trace->realfraction = bound(0, enterfrac, 1);
778 trace->fraction = bound(0, enterfrac2, 1);
779 VectorCopy(newimpactnormal, trace->plane.normal);
784 void Collision_TracePointBrushFloat(trace_t *trace, const vec3_t point, const colbrushf_t *thatbrush)
787 const colplanef_t *plane;
789 for (nplane = 0, plane = thatbrush->planes;nplane < thatbrush->numplanes;nplane++, plane++)
790 if (DotProduct(plane->normal, point) > plane->dist)
793 trace->startsupercontents |= thatbrush->supercontents;
794 if (trace->hitsupercontentsmask & thatbrush->supercontents)
796 trace->startsolid = true;
797 trace->allsolid = true;
801 static colpointf_t polyf_points[256];
802 static colplanef_t polyf_planes[256 + 2];
803 static colbrushf_t polyf_brush;
805 void Collision_SnapCopyPoints(int numpoints, const colpointf_t *in, colpointf_t *out, float fractionprecision, float invfractionprecision)
809 out->v[0] = floor(in->v[0] * fractionprecision + 0.5f) * invfractionprecision;
810 out->v[1] = floor(in->v[1] * fractionprecision + 0.5f) * invfractionprecision;
811 out->v[2] = floor(in->v[2] * fractionprecision + 0.5f) * invfractionprecision;
815 void Collision_TraceBrushPolygonFloat(trace_t *trace, const colbrushf_t *thisbrush_start, const colbrushf_t *thisbrush_end, int numpoints, const float *points, int supercontents)
819 Con_Print("Polygon with more than 256 points not supported yet (fixme!)\n");
822 polyf_brush.numpoints = numpoints;
823 polyf_brush.numplanes = numpoints + 2;
824 //polyf_brush.points = (colpointf_t *)points;
825 polyf_brush.planes = polyf_planes;
826 polyf_brush.supercontents = supercontents;
827 polyf_brush.points = polyf_points;
828 Collision_SnapCopyPoints(numpoints, (colpointf_t *)points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
829 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
830 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
831 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brush, &polyf_brush);
834 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)
837 float facemins[3], facemaxs[3];
838 polyf_brush.numpoints = 3;
839 polyf_brush.numplanes = 5;
840 polyf_brush.points = polyf_points;
841 polyf_brush.planes = polyf_planes;
842 polyf_brush.supercontents = supercontents;
843 for (i = 0;i < polyf_brush.numplanes;i++)
845 polyf_brush.planes[i].q3surfaceflags = q3surfaceflags;
846 polyf_brush.planes[i].texture = texture;
848 for (i = 0;i < numtriangles;i++, element3i += 3)
850 VectorCopy(vertex3f + element3i[0] * 3, polyf_points[0].v);
851 VectorCopy(vertex3f + element3i[1] * 3, polyf_points[1].v);
852 VectorCopy(vertex3f + element3i[2] * 3, polyf_points[2].v);
853 Collision_SnapCopyPoints(3, polyf_points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
854 facemins[0] = min(polyf_points[0].v[0], min(polyf_points[1].v[0], polyf_points[2].v[0]));
855 facemins[1] = min(polyf_points[0].v[1], min(polyf_points[1].v[1], polyf_points[2].v[1]));
856 facemins[2] = min(polyf_points[0].v[2], min(polyf_points[1].v[2], polyf_points[2].v[2]));
857 facemaxs[0] = max(polyf_points[0].v[0], max(polyf_points[1].v[0], polyf_points[2].v[0]));
858 facemaxs[1] = max(polyf_points[0].v[1], max(polyf_points[1].v[1], polyf_points[2].v[1]));
859 facemaxs[2] = max(polyf_points[0].v[2], max(polyf_points[1].v[2], polyf_points[2].v[2]));
860 if (BoxesOverlap(segmentmins, segmentmaxs, facemins, facemaxs))
862 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
863 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
864 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brush, &polyf_brush);
869 void Collision_TraceLinePolygonFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, int numpoints, const float *points, int supercontents)
873 Con_Print("Polygon with more than 256 points not supported yet (fixme!)\n");
876 polyf_brush.numpoints = numpoints;
877 polyf_brush.numplanes = numpoints + 2;
878 //polyf_brush.points = (colpointf_t *)points;
879 polyf_brush.points = polyf_points;
880 Collision_SnapCopyPoints(numpoints, (colpointf_t *)points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
881 polyf_brush.planes = polyf_planes;
882 polyf_brush.supercontents = supercontents;
883 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
884 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
885 Collision_TraceLineBrushFloat(trace, linestart, lineend, &polyf_brush, &polyf_brush);
888 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)
892 // FIXME: snap vertices?
893 for (i = 0;i < numtriangles;i++, element3i += 3)
894 Collision_TraceLineTriangleFloat(trace, linestart, lineend, vertex3f + element3i[0] * 3, vertex3f + element3i[1] * 3, vertex3f + element3i[2] * 3, supercontents, q3surfaceflags, texture);
896 polyf_brush.numpoints = 3;
897 polyf_brush.numplanes = 5;
898 polyf_brush.points = polyf_points;
899 polyf_brush.planes = polyf_planes;
900 polyf_brush.supercontents = supercontents;
901 for (i = 0;i < polyf_brush.numplanes;i++)
903 polyf_brush.planes[i].supercontents = supercontents;
904 polyf_brush.planes[i].q3surfaceflags = q3surfaceflags;
905 polyf_brush.planes[i].texture = texture;
907 for (i = 0;i < numtriangles;i++, element3i += 3)
909 float facemins[3], facemaxs[3];
910 VectorCopy(vertex3f + element3i[0] * 3, polyf_points[0].v);
911 VectorCopy(vertex3f + element3i[1] * 3, polyf_points[1].v);
912 VectorCopy(vertex3f + element3i[2] * 3, polyf_points[2].v);
913 Collision_SnapCopyPoints(numpoints, polyf_points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
914 facemins[0] = min(polyf_points[0].v[0], min(polyf_points[1].v[0], polyf_points[2].v[0]));
915 facemins[1] = min(polyf_points[0].v[1], min(polyf_points[1].v[1], polyf_points[2].v[1]));
916 facemins[2] = min(polyf_points[0].v[2], min(polyf_points[1].v[2], polyf_points[2].v[2]));
917 facemaxs[0] = max(polyf_points[0].v[0], max(polyf_points[1].v[0], polyf_points[2].v[0]));
918 facemaxs[1] = max(polyf_points[0].v[1], max(polyf_points[1].v[1], polyf_points[2].v[1]));
919 facemaxs[2] = max(polyf_points[0].v[2], max(polyf_points[1].v[2], polyf_points[2].v[2]));
920 if (BoxesOverlap(segmentmins, segmentmaxs, facemins, facemaxs))
922 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
923 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
924 Collision_TraceLineBrushFloat(trace, linestart, lineend, &polyf_brush, &polyf_brush);
931 static colpointf_t polyf_pointsstart[256], polyf_pointsend[256];
932 static colplanef_t polyf_planesstart[256 + 2], polyf_planesend[256 + 2];
933 static colbrushf_t polyf_brushstart, polyf_brushend;
935 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)
940 Con_Print("Polygon with more than 256 points not supported yet (fixme!)\n");
943 polyf_brushstart.numpoints = numpoints;
944 polyf_brushstart.numplanes = numpoints + 2;
945 polyf_brushstart.points = polyf_pointsstart;//(colpointf_t *)points;
946 polyf_brushstart.planes = polyf_planesstart;
947 polyf_brushstart.supercontents = supercontents;
948 for (i = 0;i < numpoints;i++)
949 Matrix4x4_Transform(polygonmatrixstart, points + i * 3, polyf_brushstart.points[i].v);
950 polyf_brushend.numpoints = numpoints;
951 polyf_brushend.numplanes = numpoints + 2;
952 polyf_brushend.points = polyf_pointsend;//(colpointf_t *)points;
953 polyf_brushend.planes = polyf_planesend;
954 polyf_brushend.supercontents = supercontents;
955 for (i = 0;i < numpoints;i++)
956 Matrix4x4_Transform(polygonmatrixend, points + i * 3, polyf_brushend.points[i].v);
957 for (i = 0;i < polyf_brushstart.numplanes;i++)
959 polyf_brushstart.planes[i].q3surfaceflags = q3surfaceflags;
960 polyf_brushstart.planes[i].texture = texture;
962 Collision_SnapCopyPoints(numpoints, polyf_pointsstart, polyf_pointsstart, COLLISION_SNAPSCALE, COLLISION_SNAP);
963 Collision_SnapCopyPoints(numpoints, polyf_pointsend, polyf_pointsend, COLLISION_SNAPSCALE, COLLISION_SNAP);
964 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brushstart);
965 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brushend);
967 //Collision_PrintBrushAsQHull(&polyf_brushstart, "polyf_brushstart");
968 //Collision_PrintBrushAsQHull(&polyf_brushend, "polyf_brushend");
970 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brushstart, &polyf_brushend);
975 #define MAX_BRUSHFORBOX 16
976 static int brushforbox_index = 0;
977 static colpointf_t brushforbox_point[MAX_BRUSHFORBOX*8];
978 static colplanef_t brushforbox_plane[MAX_BRUSHFORBOX*6];
979 static colbrushf_t brushforbox_brush[MAX_BRUSHFORBOX];
980 static colbrushf_t brushforpoint_brush[MAX_BRUSHFORBOX];
982 void Collision_InitBrushForBox(void)
985 for (i = 0;i < MAX_BRUSHFORBOX;i++)
987 brushforbox_brush[i].numpoints = 8;
988 brushforbox_brush[i].numplanes = 6;
989 brushforbox_brush[i].points = brushforbox_point + i * 8;
990 brushforbox_brush[i].planes = brushforbox_plane + i * 6;
991 brushforpoint_brush[i].numpoints = 1;
992 brushforpoint_brush[i].numplanes = 0;
993 brushforpoint_brush[i].points = brushforbox_point + i * 8;
994 brushforpoint_brush[i].planes = brushforbox_plane + i * 6;
998 colbrushf_t *Collision_BrushForBox(const matrix4x4_t *matrix, const vec3_t mins, const vec3_t maxs, int supercontents, int q3surfaceflags, texture_t *texture)
1003 if (brushforbox_brush[0].numpoints == 0)
1004 Collision_InitBrushForBox();
1005 // FIXME: these probably don't actually need to be normalized if the collision code does not care
1006 if (VectorCompare(mins, maxs))
1009 brush = brushforpoint_brush + ((brushforbox_index++) % MAX_BRUSHFORBOX);
1010 VectorCopy(mins, brush->points->v);
1014 brush = brushforbox_brush + ((brushforbox_index++) % MAX_BRUSHFORBOX);
1016 for (i = 0;i < 8;i++)
1018 v[0] = i & 1 ? maxs[0] : mins[0];
1019 v[1] = i & 2 ? maxs[1] : mins[1];
1020 v[2] = i & 4 ? maxs[2] : mins[2];
1021 Matrix4x4_Transform(matrix, v, brush->points[i].v);
1024 for (i = 0;i < 6;i++)
1027 v[i >> 1] = i & 1 ? 1 : -1;
1028 Matrix4x4_Transform3x3(matrix, v, brush->planes[i].normal);
1029 VectorNormalize(brush->planes[i].normal);
1032 brush->supercontents = supercontents;
1033 for (j = 0;j < brush->numplanes;j++)
1035 brush->planes[j].q3surfaceflags = q3surfaceflags;
1036 brush->planes[j].texture = texture;
1037 brush->planes[j].dist = furthestplanedist_float(brush->planes[j].normal, brush->points, brush->numpoints);
1039 VectorCopy(brush->points[0].v, brush->mins);
1040 VectorCopy(brush->points[0].v, brush->maxs);
1041 for (j = 1;j < brush->numpoints;j++)
1043 brush->mins[0] = min(brush->mins[0], brush->points[j].v[0]);
1044 brush->mins[1] = min(brush->mins[1], brush->points[j].v[1]);
1045 brush->mins[2] = min(brush->mins[2], brush->points[j].v[2]);
1046 brush->maxs[0] = max(brush->maxs[0], brush->points[j].v[0]);
1047 brush->maxs[1] = max(brush->maxs[1], brush->points[j].v[1]);
1048 brush->maxs[2] = max(brush->maxs[2], brush->points[j].v[2]);
1050 brush->mins[0] -= 1;
1051 brush->mins[1] -= 1;
1052 brush->mins[2] -= 1;
1053 brush->maxs[0] += 1;
1054 brush->maxs[1] += 1;
1055 brush->maxs[2] += 1;
1056 Collision_ValidateBrush(brush);
1060 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)
1062 colbrushf_t *boxbrush, *thisbrush_start, *thisbrush_end;
1063 vec3_t startmins, startmaxs, endmins, endmaxs;
1065 // create brushes for the collision
1066 VectorAdd(start, mins, startmins);
1067 VectorAdd(start, maxs, startmaxs);
1068 VectorAdd(end, mins, endmins);
1069 VectorAdd(end, maxs, endmaxs);
1070 boxbrush = Collision_BrushForBox(&identitymatrix, cmins, cmaxs, supercontents, q3surfaceflags, texture);
1071 thisbrush_start = Collision_BrushForBox(&identitymatrix, startmins, startmaxs, 0, 0, NULL);
1072 thisbrush_end = Collision_BrushForBox(&identitymatrix, endmins, endmaxs, 0, 0, NULL);
1074 memset(trace, 0, sizeof(trace_t));
1075 trace->hitsupercontentsmask = hitsupercontentsmask;
1076 trace->fraction = 1;
1077 trace->realfraction = 1;
1078 trace->allsolid = true;
1079 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, boxbrush, boxbrush);
1082 //pseudocode for detecting line/sphere overlap without calculating an impact point
1083 //linesphereorigin = sphereorigin - linestart;linediff = lineend - linestart;linespherefrac = DotProduct(linesphereorigin, linediff) / DotProduct(linediff, linediff);return VectorLength2(linesphereorigin - bound(0, linespherefrac, 1) * linediff) >= sphereradius*sphereradius;
1085 // LordHavoc: currently unused, but tested
1086 // note: this can be used for tracing a moving sphere vs a stationary sphere,
1087 // by simply adding the moving sphere's radius to the sphereradius parameter,
1088 // all the results are correct (impactpoint, impactnormal, and fraction)
1089 float Collision_ClipTrace_Line_Sphere(double *linestart, double *lineend, double *sphereorigin, double sphereradius, double *impactpoint, double *impactnormal)
1091 double dir[3], scale, v[3], deviationdist, impactdist, linelength;
1092 // make sure the impactpoint and impactnormal are valid even if there is
1094 VectorCopy(lineend, impactpoint);
1095 VectorClear(impactnormal);
1096 // calculate line direction
1097 VectorSubtract(lineend, linestart, dir);
1098 // normalize direction
1099 linelength = VectorLength(dir);
1102 scale = 1.0 / linelength;
1103 VectorScale(dir, scale, dir);
1105 // this dotproduct calculates the distance along the line at which the
1106 // sphere origin is (nearest point to the sphere origin on the line)
1107 impactdist = DotProduct(sphereorigin, dir) - DotProduct(linestart, dir);
1108 // calculate point on line at that distance, and subtract the
1109 // sphereorigin from it, so we have a vector to measure for the distance
1110 // of the line from the sphereorigin (deviation, how off-center it is)
1111 VectorMA(linestart, impactdist, dir, v);
1112 VectorSubtract(v, sphereorigin, v);
1113 deviationdist = VectorLength2(v);
1114 // if outside the radius, it's a miss for sure
1115 // (we do this comparison using squared radius to avoid a sqrt)
1116 if (deviationdist > sphereradius*sphereradius)
1117 return 1; // miss (off to the side)
1118 // nudge back to find the correct impact distance
1119 impactdist += deviationdist - sphereradius;
1120 if (impactdist >= linelength)
1121 return 1; // miss (not close enough)
1123 return 1; // miss (linestart is past or inside sphere)
1124 // calculate new impactpoint
1125 VectorMA(linestart, impactdist, dir, impactpoint);
1126 // calculate impactnormal (surface normal at point of impact)
1127 VectorSubtract(impactpoint, sphereorigin, impactnormal);
1128 // normalize impactnormal
1129 VectorNormalize(impactnormal);
1130 // return fraction of movement distance
1131 return impactdist / linelength;
1134 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)
1138 float d1, d2, d, f, impact[3], edgenormal[3], faceplanenormal[3], faceplanedist, faceplanenormallength2, edge01[3], edge21[3], edge02[3];
1140 // this function executes:
1141 // 32 ops when line starts behind triangle
1142 // 38 ops when line ends infront of triangle
1143 // 43 ops when line fraction is already closer than this triangle
1144 // 72 ops when line is outside edge 01
1145 // 92 ops when line is outside edge 21
1146 // 115 ops when line is outside edge 02
1147 // 123 ops when line impacts triangle and updates trace results
1149 // this code is designed for clockwise triangles, conversion to
1150 // counterclockwise would require swapping some things around...
1151 // it is easier to simply swap the point0 and point2 parameters to this
1152 // function when calling it than it is to rewire the internals.
1154 // calculate the faceplanenormal of the triangle, this represents the front side
1156 VectorSubtract(point0, point1, edge01);
1157 VectorSubtract(point2, point1, edge21);
1158 CrossProduct(edge01, edge21, faceplanenormal);
1159 // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
1161 faceplanenormallength2 = DotProduct(faceplanenormal, faceplanenormal);
1162 if (faceplanenormallength2 < 0.0001f)
1164 // calculate the distance
1166 faceplanedist = DotProduct(point0, faceplanenormal);
1168 // if start point is on the back side there is no collision
1169 // (we don't care about traces going through the triangle the wrong way)
1171 // calculate the start distance
1173 d1 = DotProduct(faceplanenormal, linestart);
1174 if (d1 <= faceplanedist)
1177 // calculate the end distance
1179 d2 = DotProduct(faceplanenormal, lineend);
1180 // if both are in front, there is no collision
1181 if (d2 >= faceplanedist)
1184 // from here on we know d1 is >= 0 and d2 is < 0
1185 // this means the line starts infront and ends behind, passing through it
1187 // calculate the recipricol of the distance delta,
1188 // so we can use it multiple times cheaply (instead of division)
1190 d = 1.0f / (d1 - d2);
1191 // calculate the impact fraction by taking the start distance (> 0)
1192 // and subtracting the face plane distance (this is the distance of the
1193 // triangle along that same normal)
1194 // then multiply by the recipricol distance delta
1196 f = (d1 - faceplanedist) * d;
1197 // skip out if this impact is further away than previous ones
1199 if (f > trace->realfraction)
1201 // calculate the perfect impact point for classification of insidedness
1203 impact[0] = linestart[0] + f * (lineend[0] - linestart[0]);
1204 impact[1] = linestart[1] + f * (lineend[1] - linestart[1]);
1205 impact[2] = linestart[2] + f * (lineend[2] - linestart[2]);
1207 // calculate the edge normal and reject if impact is outside triangle
1208 // (an edge normal faces away from the triangle, to get the desired normal
1209 // a crossproduct with the faceplanenormal is used, and because of the way
1210 // the insidedness comparison is written it does not need to be normalized)
1212 // first use the two edges from the triangle plane math
1213 // the other edge only gets calculated if the point survives that long
1216 CrossProduct(edge01, faceplanenormal, edgenormal);
1217 if (DotProduct(impact, edgenormal) > DotProduct(point1, edgenormal))
1221 CrossProduct(faceplanenormal, edge21, edgenormal);
1222 if (DotProduct(impact, edgenormal) > DotProduct(point2, edgenormal))
1226 VectorSubtract(point0, point2, edge02);
1227 CrossProduct(faceplanenormal, edge02, edgenormal);
1228 if (DotProduct(impact, edgenormal) > DotProduct(point0, edgenormal))
1233 // store the new trace fraction
1234 trace->realfraction = f;
1236 // calculate a nudged fraction to keep it out of the surface
1237 // (the main fraction remains perfect)
1238 trace->fraction = f - collision_impactnudge.value * d;
1240 // store the new trace plane (because collisions only happen from
1241 // the front this is always simply the triangle normal, never flipped)
1242 d = 1.0 / sqrt(faceplanenormallength2);
1243 VectorScale(faceplanenormal, d, trace->plane.normal);
1244 trace->plane.dist = faceplanedist * d;
1246 trace->hitsupercontents = supercontents;
1247 trace->hitq3surfaceflags = q3surfaceflags;
1248 trace->hittexture = texture;
1250 float d1, d2, d, f, fnudged, impact[3], edgenormal[3], faceplanenormal[3], faceplanedist, edge[3];
1252 // this code is designed for clockwise triangles, conversion to
1253 // counterclockwise would require swapping some things around...
1254 // it is easier to simply swap the point0 and point2 parameters to this
1255 // function when calling it than it is to rewire the internals.
1257 // calculate the unnormalized faceplanenormal of the triangle,
1258 // this represents the front side
1259 TriangleNormal(point0, point1, point2, faceplanenormal);
1260 // there's no point in processing a degenerate triangle
1261 // (GIGO - Garbage In, Garbage Out)
1262 if (DotProduct(faceplanenormal, faceplanenormal) < 0.0001f)
1264 // calculate the unnormalized distance
1265 faceplanedist = DotProduct(point0, faceplanenormal);
1267 // calculate the unnormalized start distance
1268 d1 = DotProduct(faceplanenormal, linestart) - faceplanedist;
1269 // if start point is on the back side there is no collision
1270 // (we don't care about traces going through the triangle the wrong way)
1274 // calculate the unnormalized end distance
1275 d2 = DotProduct(faceplanenormal, lineend) - faceplanedist;
1276 // if both are in front, there is no collision
1280 // from here on we know d1 is >= 0 and d2 is < 0
1281 // this means the line starts infront and ends behind, passing through it
1283 // calculate the recipricol of the distance delta,
1284 // so we can use it multiple times cheaply (instead of division)
1285 d = 1.0f / (d1 - d2);
1286 // calculate the impact fraction by taking the start distance (> 0)
1287 // and subtracting the face plane distance (this is the distance of the
1288 // triangle along that same normal)
1289 // then multiply by the recipricol distance delta
1291 // skip out if this impact is further away than previous ones
1292 if (f > trace->realfraction)
1294 // calculate the perfect impact point for classification of insidedness
1295 impact[0] = linestart[0] + f * (lineend[0] - linestart[0]);
1296 impact[1] = linestart[1] + f * (lineend[1] - linestart[1]);
1297 impact[2] = linestart[2] + f * (lineend[2] - linestart[2]);
1299 // calculate the edge normal and reject if impact is outside triangle
1300 // (an edge normal faces away from the triangle, to get the desired normal
1301 // a crossproduct with the faceplanenormal is used, and because of the way
1302 // the insidedness comparison is written it does not need to be normalized)
1304 VectorSubtract(point2, point0, edge);
1305 CrossProduct(edge, faceplanenormal, edgenormal);
1306 if (DotProduct(impact, edgenormal) > DotProduct(point0, edgenormal))
1309 VectorSubtract(point0, point1, edge);
1310 CrossProduct(edge, faceplanenormal, edgenormal);
1311 if (DotProduct(impact, edgenormal) > DotProduct(point1, edgenormal))
1314 VectorSubtract(point1, point2, edge);
1315 CrossProduct(edge, faceplanenormal, edgenormal);
1316 if (DotProduct(impact, edgenormal) > DotProduct(point2, edgenormal))
1319 // store the new trace fraction
1320 trace->realfraction = bound(0, f, 1);
1322 // store the new trace plane (because collisions only happen from
1323 // the front this is always simply the triangle normal, never flipped)
1324 VectorNormalize(faceplanenormal);
1325 VectorCopy(faceplanenormal, trace->plane.normal);
1326 trace->plane.dist = DotProduct(point0, faceplanenormal);
1328 // calculate the normalized start and end distances
1329 d1 = DotProduct(trace->plane.normal, linestart) - trace->plane.dist;
1330 d2 = DotProduct(trace->plane.normal, lineend) - trace->plane.dist;
1332 // calculate a nudged fraction to keep it out of the surface
1333 // (the main fraction remains perfect)
1334 fnudged = (d1 - collision_impactnudge.value) / (d1 - d2);
1335 trace->fraction = bound(0, fnudged, 1);
1337 // store the new trace endpos
1338 // not needed, it's calculated later when the trace is finished
1339 //trace->endpos[0] = linestart[0] + fnudged * (lineend[0] - linestart[0]);
1340 //trace->endpos[1] = linestart[1] + fnudged * (lineend[1] - linestart[1]);
1341 //trace->endpos[2] = linestart[2] + fnudged * (lineend[2] - linestart[2]);
1342 trace->hitsupercontents = supercontents;
1343 trace->hitq3surfaceflags = q3surfaceflags;
1344 trace->hittexture = texture;
1348 typedef struct colbspnode_s
1351 struct colbspnode_s *children[2];
1352 // the node is reallocated or split if max is reached
1355 colbrushf_t **colbrushflist;
1358 //colbrushd_t **colbrushdlist;
1362 typedef struct colbsp_s
1365 colbspnode_t *nodes;
1369 colbsp_t *Collision_CreateCollisionBSP(mempool_t *mempool)
1372 bsp = (colbsp_t *)Mem_Alloc(mempool, sizeof(colbsp_t));
1373 bsp->mempool = mempool;
1374 bsp->nodes = (colbspnode_t *)Mem_Alloc(bsp->mempool, sizeof(colbspnode_t));
1378 void Collision_FreeCollisionBSPNode(colbspnode_t *node)
1380 if (node->children[0])
1381 Collision_FreeCollisionBSPNode(node->children[0]);
1382 if (node->children[1])
1383 Collision_FreeCollisionBSPNode(node->children[1]);
1384 while (--node->numcolbrushf)
1385 Mem_Free(node->colbrushflist[node->numcolbrushf]);
1386 //while (--node->numcolbrushd)
1387 // Mem_Free(node->colbrushdlist[node->numcolbrushd]);
1391 void Collision_FreeCollisionBSP(colbsp_t *bsp)
1393 Collision_FreeCollisionBSPNode(bsp->nodes);
1397 void Collision_BoundingBoxOfBrushTraceSegment(const colbrushf_t *start, const colbrushf_t *end, vec3_t mins, vec3_t maxs, float startfrac, float endfrac)
1400 colpointf_t *ps, *pe;
1401 float tempstart[3], tempend[3];
1402 VectorLerp(start->points[0].v, startfrac, end->points[0].v, mins);
1403 VectorCopy(mins, maxs);
1404 for (i = 0, ps = start->points, pe = end->points;i < start->numpoints;i++, ps++, pe++)
1406 VectorLerp(ps->v, startfrac, pe->v, tempstart);
1407 VectorLerp(ps->v, endfrac, pe->v, tempend);
1408 mins[0] = min(mins[0], min(tempstart[0], tempend[0]));
1409 mins[1] = min(mins[1], min(tempstart[1], tempend[1]));
1410 mins[2] = min(mins[2], min(tempstart[2], tempend[2]));
1411 maxs[0] = min(maxs[0], min(tempstart[0], tempend[0]));
1412 maxs[1] = min(maxs[1], min(tempstart[1], tempend[1]));
1413 maxs[2] = min(maxs[2], min(tempstart[2], tempend[2]));