5 #define COLLISION_SNAPSCALE (32.0f)
6 #define COLLISION_SNAP (1.0f / COLLISION_SNAPSCALE)
7 #define COLLISION_SNAP2 (2.0f / COLLISION_SNAPSCALE)
8 #define COLLISION_PLANE_DIST_EPSILON (2.0f / COLLISION_SNAPSCALE)
10 cvar_t collision_impactnudge = {0, "collision_impactnudge", "0.03125", "how much to back off from the impact"};
11 cvar_t collision_startnudge = {0, "collision_startnudge", "0", "how much to bias collision trace start"};
12 cvar_t collision_endnudge = {0, "collision_endnudge", "0", "how much to bias collision trace end"};
13 cvar_t collision_enternudge = {0, "collision_enternudge", "0", "how much to bias collision entry fraction"};
14 cvar_t collision_leavenudge = {0, "collision_leavenudge", "0", "how much to bias collision exit fraction"};
16 void Collision_Init (void)
18 Cvar_RegisterVariable(&collision_impactnudge);
19 Cvar_RegisterVariable(&collision_startnudge);
20 Cvar_RegisterVariable(&collision_endnudge);
21 Cvar_RegisterVariable(&collision_enternudge);
22 Cvar_RegisterVariable(&collision_leavenudge);
38 void Collision_PrintBrushAsQHull(colbrushf_t *brush, const char *name)
41 Con_Printf("3 %s\n%i\n", name, brush->numpoints);
42 for (i = 0;i < brush->numpoints;i++)
43 Con_Printf("%f %f %f\n", brush->points[i].v[0], brush->points[i].v[1], brush->points[i].v[2]);
45 Con_Printf("4\n%i\n", brush->numplanes);
46 for (i = 0;i < brush->numplanes;i++)
47 Con_Printf("%f %f %f %f\n", brush->planes[i].normal[0], brush->planes[i].normal[1], brush->planes[i].normal[2], brush->planes[i].dist);
50 void Collision_ValidateBrush(colbrushf_t *brush)
52 int j, k, pointsoffplanes, pointonplanes, pointswithinsufficientplanes, printbrush;
55 if (!brush->numpoints)
57 Con_Print("Collision_ValidateBrush: brush with no points!\n");
61 // it's ok for a brush to have one point and no planes...
62 if (brush->numplanes == 0 && brush->numpoints != 1)
64 Con_Print("Collision_ValidateBrush: brush with no planes and more than one point!\n");
71 pointswithinsufficientplanes = 0;
72 for (k = 0;k < brush->numplanes;k++)
73 if (DotProduct(brush->planes[k].normal, brush->planes[k].normal) < 0.0001f)
74 Con_Printf("Collision_ValidateBrush: plane #%i (%f %f %f %f) is degenerate\n", k, brush->planes[k].normal[0], brush->planes[k].normal[1], brush->planes[k].normal[2], brush->planes[k].dist);
75 for (j = 0;j < brush->numpoints;j++)
78 for (k = 0;k < brush->numplanes;k++)
80 d = DotProduct(brush->points[j].v, brush->planes[k].normal) - brush->planes[k].dist;
81 if (d > COLLISION_PLANE_DIST_EPSILON)
83 Con_Printf("Collision_ValidateBrush: point #%i (%f %f %f) infront of plane #%i (%f %f %f %f)\n", j, brush->points[j].v[0], brush->points[j].v[1], brush->points[j].v[2], k, brush->planes[k].normal[0], brush->planes[k].normal[1], brush->planes[k].normal[2], brush->planes[k].dist);
86 if (fabs(d) > COLLISION_PLANE_DIST_EPSILON)
91 if (pointonplanes < 3)
92 pointswithinsufficientplanes++;
94 if (pointswithinsufficientplanes)
96 Con_Print("Collision_ValidateBrush: some points have insufficient planes, every point must be on at least 3 planes to form a corner.\n");
99 if (pointsoffplanes == 0) // all points are on all planes
101 Con_Print("Collision_ValidateBrush: all points lie on all planes (degenerate, no brush volume!)\n");
106 Collision_PrintBrushAsQHull(brush, "unnamed");
109 float nearestplanedist_float(const float *normal, const colpointf_t *points, int numpoints)
111 float dist, bestdist;
112 bestdist = DotProduct(points->v, normal);
116 dist = DotProduct(points->v, normal);
117 bestdist = min(bestdist, dist);
123 float furthestplanedist_float(const float *normal, const colpointf_t *points, int numpoints)
125 float dist, bestdist;
126 bestdist = DotProduct(points->v, normal);
130 dist = DotProduct(points->v, normal);
131 bestdist = max(bestdist, dist);
138 colbrushf_t *Collision_NewBrushFromPlanes(mempool_t *mempool, int numoriginalplanes, const colplanef_t *originalplanes, int supercontents)
140 // TODO: planesbuf could be replaced by a remapping table
142 int numpointsbuf = 0, maxpointsbuf = 256, numplanesbuf = 0, maxplanesbuf = 256, numelementsbuf = 0, maxelementsbuf = 256;
145 colpointf_t pointsbuf[256];
146 colplanef_t planesbuf[256];
147 int elementsbuf[1024];
148 int polypointbuf[256];
153 // enable these if debugging to avoid seeing garbage in unused data
154 memset(pointsbuf, 0, sizeof(pointsbuf));
155 memset(planesbuf, 0, sizeof(planesbuf));
156 memset(elementsbuf, 0, sizeof(elementsbuf));
157 memset(polypointbuf, 0, sizeof(polypointbuf));
158 memset(p, 0, sizeof(p));
160 // figure out how large a bounding box we need to properly compute this brush
162 for (j = 0;j < numoriginalplanes;j++)
163 maxdist = max(maxdist, originalplanes[j].dist);
164 // now make it large enough to enclose the entire brush, and round it off to a reasonable multiple of 1024
165 maxdist = floor(maxdist * (4.0 / 1024.0) + 1) * 1024.0;
166 // construct a collision brush (points, planes, and renderable mesh) from
167 // a set of planes, this also optimizes out any unnecessary planes (ones
168 // whose polygon is clipped away by the other planes)
169 for (j = 0;j < numoriginalplanes;j++)
171 // add the plane uniquely (no duplicates)
172 for (k = 0;k < numplanesbuf;k++)
173 if (VectorCompare(planesbuf[k].normal, originalplanes[j].normal) && planesbuf[k].dist == originalplanes[j].dist)
175 // if the plane is a duplicate, skip it
176 if (k < numplanesbuf)
178 // check if there are too many and skip the brush
179 if (numplanesbuf >= maxplanesbuf)
181 Con_Print("Collision_NewBrushFromPlanes: failed to build collision brush: too many planes for buffer\n");
185 // create a large polygon from the plane
187 PolygonD_QuadForPlane(p[w], originalplanes[j].normal[0], originalplanes[j].normal[1], originalplanes[j].normal[2], originalplanes[j].dist, maxdist);
189 // clip it by all other planes
190 for (k = 0;k < numoriginalplanes && pnumpoints && pnumpoints <= pmaxpoints;k++)
192 // skip the plane this polygon
193 // (nothing happens if it is processed, this is just an optimization)
196 // we want to keep the inside of the brush plane so we flip
198 PolygonD_Divide(pnumpoints, p[w], -originalplanes[k].normal[0], -originalplanes[k].normal[1], -originalplanes[k].normal[2], -originalplanes[k].dist, COLLISION_PLANE_DIST_EPSILON, pmaxpoints, p[!w], &pnumpoints, 0, NULL, NULL, NULL);
202 // if nothing is left, skip it
205 //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);
209 for (k = 0;k < pnumpoints;k++)
213 for (l = 0;l < numoriginalplanes;l++)
214 if (fabs(DotProduct(&p[w][k*3], originalplanes[l].normal) - originalplanes[l].dist) < COLLISION_PLANE_DIST_EPSILON)
221 Con_Printf("Collision_NewBrushFromPlanes: warning: polygon point does not lie on at least 3 planes\n");
225 // check if there are too many polygon vertices for buffer
226 if (pnumpoints > pmaxpoints)
228 Con_Print("Collision_NewBrushFromPlanes: failed to build collision brush: too many points for buffer\n");
232 // check if there are too many triangle elements for buffer
233 if (numelementsbuf + (pnumpoints - 2) * 3 > maxelementsbuf)
235 Con_Print("Collision_NewBrushFromPlanes: failed to build collision brush: too many triangle elements for buffer\n");
239 for (k = 0;k < pnumpoints;k++)
241 // check if there is already a matching point (no duplicates)
242 for (m = 0;m < numpointsbuf;m++)
243 if (VectorDistance2(&p[w][k*3], pointsbuf[m].v) < COLLISION_SNAP2)
246 // if there is no match, add a new one
247 if (m == numpointsbuf)
249 // check if there are too many and skip the brush
250 if (numpointsbuf >= maxpointsbuf)
252 Con_Print("Collision_NewBrushFromPlanes: failed to build collision brush: too many points for buffer\n");
256 VectorCopy(&p[w][k*3], pointsbuf[numpointsbuf].v);
260 // store the index into a buffer
264 // add the triangles for the polygon
265 // (this particular code makes a triangle fan)
266 for (k = 0;k < pnumpoints - 2;k++)
268 elementsbuf[numelementsbuf++] = polypointbuf[0];
269 elementsbuf[numelementsbuf++] = polypointbuf[k + 1];
270 elementsbuf[numelementsbuf++] = polypointbuf[k + 2];
274 VectorCopy(originalplanes[j].normal, planesbuf[numplanesbuf].normal);
275 planesbuf[numplanesbuf].dist = originalplanes[j].dist;
276 planesbuf[numplanesbuf].q3surfaceflags = originalplanes[j].q3surfaceflags;
277 planesbuf[numplanesbuf].texture = originalplanes[j].texture;
281 // validate plane distances
282 for (j = 0;j < numplanesbuf;j++)
284 float d = furthestplanedist_float(planesbuf[j].normal, pointsbuf, numpointsbuf);
285 if (fabs(planesbuf[j].dist - d) > COLLISION_PLANE_DIST_EPSILON)
286 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);
289 // if nothing is left, there's nothing to allocate
290 if (numelementsbuf < 12 || numplanesbuf < 4 || numpointsbuf < 4)
292 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);
296 // allocate the brush and copy to it
297 brush = (colbrushf_t *)Mem_Alloc(mempool, sizeof(colbrushf_t) + sizeof(colpointf_t) * numpointsbuf + sizeof(colplanef_t) * numplanesbuf + sizeof(int) * numelementsbuf);
298 brush->supercontents = supercontents;
299 brush->numplanes = numplanesbuf;
300 brush->numpoints = numpointsbuf;
301 brush->numtriangles = numelementsbuf / 3;
302 brush->planes = (colplanef_t *)(brush + 1);
303 brush->points = (colpointf_t *)(brush->planes + brush->numplanes);
304 brush->elements = (int *)(brush->points + brush->numpoints);
305 for (j = 0;j < brush->numpoints;j++)
307 brush->points[j].v[0] = pointsbuf[j].v[0];
308 brush->points[j].v[1] = pointsbuf[j].v[1];
309 brush->points[j].v[2] = pointsbuf[j].v[2];
311 for (j = 0;j < brush->numplanes;j++)
313 brush->planes[j].normal[0] = planesbuf[j].normal[0];
314 brush->planes[j].normal[1] = planesbuf[j].normal[1];
315 brush->planes[j].normal[2] = planesbuf[j].normal[2];
316 brush->planes[j].dist = planesbuf[j].dist;
317 brush->planes[j].q3surfaceflags = planesbuf[j].q3surfaceflags;
318 brush->planes[j].texture = planesbuf[j].texture;
320 for (j = 0;j < brush->numtriangles * 3;j++)
321 brush->elements[j] = elementsbuf[j];
322 VectorCopy(brush->points[0].v, brush->mins);
323 VectorCopy(brush->points[0].v, brush->maxs);
324 for (j = 1;j < brush->numpoints;j++)
326 brush->mins[0] = min(brush->mins[0], brush->points[j].v[0]);
327 brush->mins[1] = min(brush->mins[1], brush->points[j].v[1]);
328 brush->mins[2] = min(brush->mins[2], brush->points[j].v[2]);
329 brush->maxs[0] = max(brush->maxs[0], brush->points[j].v[0]);
330 brush->maxs[1] = max(brush->maxs[1], brush->points[j].v[1]);
331 brush->maxs[2] = max(brush->maxs[2], brush->points[j].v[2]);
339 Collision_ValidateBrush(brush);
345 void Collision_CalcPlanesForPolygonBrushFloat(colbrushf_t *brush)
348 float edge0[3], edge1[3], edge2[3], normal[3], dist, bestdist;
351 // FIXME: these probably don't actually need to be normalized if the collision code does not care
352 if (brush->numpoints == 3)
354 // optimized triangle case
355 TriangleNormal(brush->points[0].v, brush->points[1].v, brush->points[2].v, brush->planes[0].normal);
356 if (DotProduct(brush->planes[0].normal, brush->planes[0].normal) < 0.0001f)
358 // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
359 brush->numplanes = 0;
364 brush->numplanes = 5;
365 VectorNormalize(brush->planes[0].normal);
366 brush->planes[0].dist = DotProduct(brush->points->v, brush->planes[0].normal);
367 VectorNegate(brush->planes[0].normal, brush->planes[1].normal);
368 brush->planes[1].dist = -brush->planes[0].dist;
369 VectorSubtract(brush->points[2].v, brush->points[0].v, edge0);
370 VectorSubtract(brush->points[0].v, brush->points[1].v, edge1);
371 VectorSubtract(brush->points[1].v, brush->points[2].v, edge2);
374 float projectionnormal[3], projectionedge0[3], projectionedge1[3], projectionedge2[3];
376 float dist, bestdist;
377 bestdist = fabs(brush->planes[0].normal[0]);
379 for (i = 1;i < 3;i++)
381 dist = fabs(brush->planes[0].normal[i]);
388 VectorClear(projectionnormal);
389 if (brush->planes[0].normal[best] < 0)
390 projectionnormal[best] = -1;
392 projectionnormal[best] = 1;
393 VectorCopy(edge0, projectionedge0);
394 VectorCopy(edge1, projectionedge1);
395 VectorCopy(edge2, projectionedge2);
396 projectionedge0[best] = 0;
397 projectionedge1[best] = 0;
398 projectionedge2[best] = 0;
399 CrossProduct(projectionedge0, projectionnormal, brush->planes[2].normal);
400 CrossProduct(projectionedge1, projectionnormal, brush->planes[3].normal);
401 CrossProduct(projectionedge2, projectionnormal, brush->planes[4].normal);
404 CrossProduct(edge0, brush->planes->normal, brush->planes[2].normal);
405 CrossProduct(edge1, brush->planes->normal, brush->planes[3].normal);
406 CrossProduct(edge2, brush->planes->normal, brush->planes[4].normal);
408 VectorNormalize(brush->planes[2].normal);
409 VectorNormalize(brush->planes[3].normal);
410 VectorNormalize(brush->planes[4].normal);
411 brush->planes[2].dist = DotProduct(brush->points[2].v, brush->planes[2].normal);
412 brush->planes[3].dist = DotProduct(brush->points[0].v, brush->planes[3].normal);
413 brush->planes[4].dist = DotProduct(brush->points[1].v, brush->planes[4].normal);
415 if (developer.integer >= 100)
421 VectorSubtract(brush->points[0].v, brush->points[1].v, edge0);
422 VectorSubtract(brush->points[2].v, brush->points[1].v, edge1);
423 CrossProduct(edge0, edge1, normal);
424 VectorNormalize(normal);
425 VectorSubtract(normal, brush->planes[0].normal, temp);
426 if (VectorLength(temp) > 0.01f)
427 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]);
428 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)
429 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);
431 if (fabs(DotProduct(brush->planes[2].normal, brush->planes[0].normal)) > 0.01f)
432 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);
433 if (fabs(DotProduct(brush->planes[3].normal, brush->planes[0].normal)) > 0.01f)
434 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);
435 if (fabs(DotProduct(brush->planes[4].normal, brush->planes[0].normal)) > 0.01f)
436 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);
437 if (fabs(DotProduct(brush->planes[2].normal, edge0)) > 0.01f)
438 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]);
439 if (fabs(DotProduct(brush->planes[3].normal, edge1)) > 0.01f)
440 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]);
441 if (fabs(DotProduct(brush->planes[4].normal, edge2)) > 0.01f)
442 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]);
445 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)
446 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);
447 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)
448 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);
449 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)
450 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);
451 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)
452 Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: edge 0 (%f %f %f to %f %f %f) off front plane 2 (%f %f %f %f)\n", brush->points[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);
453 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)
454 Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: edge 0 (%f %f %f to %f %f %f) off front plane 2 (%f %f %f %f)\n", brush->points[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);
460 // choose best surface normal for polygon's plane
462 for (i = 0, p = brush->points + 1;i < brush->numpoints - 2;i++, p++)
464 VectorSubtract(p[-1].v, p[0].v, edge0);
465 VectorSubtract(p[1].v, p[0].v, edge1);
466 CrossProduct(edge0, edge1, normal);
467 //TriangleNormal(p[-1].v, p[0].v, p[1].v, normal);
468 dist = DotProduct(normal, normal);
469 if (i == 0 || bestdist < dist)
472 VectorCopy(normal, brush->planes->normal);
475 if (bestdist < 0.0001f)
477 // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
478 brush->numplanes = 0;
483 brush->numplanes = brush->numpoints + 2;
484 VectorNormalize(brush->planes->normal);
485 brush->planes->dist = DotProduct(brush->points->v, brush->planes->normal);
487 // negate plane to create other side
488 VectorNegate(brush->planes[0].normal, brush->planes[1].normal);
489 brush->planes[1].dist = -brush->planes[0].dist;
490 for (i = 0, p = brush->points + (brush->numpoints - 1), p2 = brush->points;i < brush->numpoints;i++, p = p2, p2++)
492 VectorSubtract(p->v, p2->v, edge0);
493 CrossProduct(edge0, brush->planes->normal, brush->planes[i + 2].normal);
494 VectorNormalize(brush->planes[i + 2].normal);
495 brush->planes[i + 2].dist = DotProduct(p->v, brush->planes[i + 2].normal);
500 if (developer.integer >= 100)
502 // validity check - will be disabled later
503 Collision_ValidateBrush(brush);
504 for (i = 0;i < brush->numplanes;i++)
507 for (j = 0, p = brush->points;j < brush->numpoints;j++, p++)
508 if (DotProduct(p->v, brush->planes[i].normal) > brush->planes[i].dist + COLLISION_PLANE_DIST_EPSILON)
509 Con_Printf("Error in brush plane generation, plane %i\n", i);
514 colbrushf_t *Collision_AllocBrushFromPermanentPolygonFloat(mempool_t *mempool, int numpoints, float *points, int supercontents)
517 brush = (colbrushf_t *)Mem_Alloc(mempool, sizeof(colbrushf_t) + sizeof(colplanef_t) * (numpoints + 2));
518 brush->supercontents = supercontents;
519 brush->numpoints = numpoints;
520 brush->numplanes = numpoints + 2;
521 brush->planes = (colplanef_t *)(brush + 1);
522 brush->points = (colpointf_t *)points;
523 Sys_Error("Collision_AllocBrushFromPermanentPolygonFloat: FIXME: this code needs to be updated to generate a mesh...");
527 // NOTE: start and end of each brush pair must have same numplanes/numpoints
528 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)
530 int nplane, nplane2, fstartsolid = true, fendsolid = true, brushsolid, hitq3surfaceflags = 0;
531 float enterfrac = -1, leavefrac = 1, d1, d2, f, imove, newimpactnormal[3], enterfrac2 = -1;
532 const colplanef_t *startplane, *endplane;
533 texture_t *hittexture = NULL;
535 VectorClear(newimpactnormal);
537 for (nplane = 0;nplane < thatbrush_start->numplanes + thisbrush_start->numplanes;nplane++)
540 if (nplane2 >= thatbrush_start->numplanes)
542 nplane2 -= thatbrush_start->numplanes;
543 startplane = thisbrush_start->planes + nplane2;
544 endplane = thisbrush_end->planes + nplane2;
545 if (developer.integer >= 100)
547 // any brush with degenerate planes is not worth handling
548 if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
550 Con_Print("Collision_TraceBrushBrushFloat: degenerate thisbrush plane!\n");
553 f = furthestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints);
554 if (fabs(f - startplane->dist) > COLLISION_PLANE_DIST_EPSILON)
555 Con_Printf("startplane->dist %f != calculated %f (thisbrush_start)\n", startplane->dist, f);
557 d1 = nearestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints) - furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints) - collision_startnudge.value;
558 d2 = nearestplanedist_float(endplane->normal, thisbrush_end->points, thisbrush_end->numpoints) - furthestplanedist_float(endplane->normal, thatbrush_end->points, thatbrush_end->numpoints) - collision_endnudge.value;
562 startplane = thatbrush_start->planes + nplane2;
563 endplane = thatbrush_end->planes + nplane2;
564 if (developer.integer >= 100)
566 // any brush with degenerate planes is not worth handling
567 if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
569 Con_Print("Collision_TraceBrushBrushFloat: degenerate thatbrush plane!\n");
572 f = furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints);
573 if (fabs(f - startplane->dist) > COLLISION_PLANE_DIST_EPSILON)
574 Con_Printf("startplane->dist %f != calculated %f (thatbrush_start)\n", startplane->dist, f);
576 d1 = nearestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints) - startplane->dist - collision_startnudge.value;
577 d2 = nearestplanedist_float(endplane->normal, thisbrush_end->points, thisbrush_end->numpoints) - endplane->dist - collision_endnudge.value;
579 //Con_Printf("%c%i: d1 = %f, d2 = %f, d1 / (d1 - d2) = %f\n", nplane2 != nplane ? 'b' : 'a', nplane2, d1, d2, d1 / (d1 - d2));
590 imove = 1 / (d1 - d2);
591 f = (d1 - collision_enternudge.value) * imove;
595 enterfrac2 = f - collision_impactnudge.value * imove;
596 VectorLerp(startplane->normal, enterfrac, endplane->normal, newimpactnormal);
597 hitq3surfaceflags = startplane->q3surfaceflags;
598 hittexture = startplane->texture;
604 // moving out of brush
611 f = (d1 + collision_leavenudge.value) / (d1 - d2);
618 brushsolid = trace->hitsupercontentsmask & thatbrush_start->supercontents;
621 trace->startsupercontents |= thatbrush_start->supercontents;
624 trace->startsolid = true;
626 trace->allsolid = true;
630 // LordHavoc: we need an epsilon nudge here because for a point trace the
631 // penetrating line segment is normally zero length if this brush was
632 // generated from a polygon (infinitely thin), and could even be slightly
633 // positive or negative due to rounding errors in that case.
634 if (brushsolid && enterfrac > -1 && enterfrac < trace->realfraction && enterfrac - (1.0f / 1024.0f) <= leavefrac)
638 if (thatbrush_start->ispolygon)
640 d1 = nearestplanedist_float(thatbrush_start->planes[0].normal, thisbrush_start->points, thisbrush_start->numpoints) - thatbrush_start->planes[0].dist - collision_startnudge.value;
641 d2 = nearestplanedist_float(thatbrush_end->planes[0].normal, thisbrush_end->points, thisbrush_end->numpoints) - thatbrush_end->planes[0].dist - collision_endnudge.value;
643 if (move <= 0 || d2 > collision_enternudge.value || d1 < 0)
647 enterfrac = (d1 - collision_enternudge.value) * imove;
648 if (enterfrac < trace->realfraction)
650 enterfrac2 = enterfrac - collision_impactnudge.value * imove;
651 trace->hitsupercontents = thatbrush_start->supercontents;
652 trace->hitq3surfaceflags = thatbrush_start->planes[0].q3surfaceflags;
653 trace->hittexture = thatbrush_start->planes[0].texture;
654 trace->realfraction = bound(0, enterfrac, 1);
655 trace->fraction = bound(0, enterfrac2, 1);
656 VectorLerp(thatbrush_start->planes[0].normal, enterfrac, thatbrush_end->planes[0].normal, trace->plane.normal);
662 trace->hitsupercontents = thatbrush_start->supercontents;
663 trace->hitq3surfaceflags = hitq3surfaceflags;
664 trace->hittexture = hittexture;
665 trace->realfraction = bound(0, enterfrac, 1);
666 trace->fraction = bound(0, enterfrac2, 1);
667 VectorCopy(newimpactnormal, trace->plane.normal);
672 // NOTE: start and end brush pair must have same numplanes/numpoints
673 void Collision_TraceLineBrushFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, const colbrushf_t *thatbrush_start, const colbrushf_t *thatbrush_end)
675 int nplane, fstartsolid = true, fendsolid = true, brushsolid, hitq3surfaceflags = 0;
676 float enterfrac = -1, leavefrac = 1, d1, d2, f, imove, newimpactnormal[3], enterfrac2 = -1;
677 const colplanef_t *startplane, *endplane;
678 texture_t *hittexture = NULL;
680 VectorClear(newimpactnormal);
682 for (nplane = 0;nplane < thatbrush_start->numplanes;nplane++)
684 startplane = thatbrush_start->planes + nplane;
685 endplane = thatbrush_end->planes + nplane;
686 d1 = DotProduct(startplane->normal, linestart) - startplane->dist - collision_startnudge.value;
687 d2 = DotProduct(endplane->normal, lineend) - endplane->dist - collision_endnudge.value;
688 if (developer.integer >= 100)
690 // any brush with degenerate planes is not worth handling
691 if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
693 Con_Print("Collision_TraceLineBrushFloat: degenerate plane!\n");
696 if (thatbrush_start->numpoints)
698 f = furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints);
699 if (fabs(f - startplane->dist) > COLLISION_PLANE_DIST_EPSILON)
700 Con_Printf("startplane->dist %f != calculated %f\n", startplane->dist, f);
713 imove = 1 / (d1 - d2);
714 f = (d1 - collision_enternudge.value) * imove;
718 enterfrac2 = f - collision_impactnudge.value * imove;
719 VectorLerp(startplane->normal, enterfrac, endplane->normal, newimpactnormal);
720 hitq3surfaceflags = startplane->q3surfaceflags;
721 hittexture = startplane->texture;
727 // moving out of brush
734 f = (d1 + collision_leavenudge.value) / (d1 - d2);
741 brushsolid = trace->hitsupercontentsmask & thatbrush_start->supercontents;
744 trace->startsupercontents |= thatbrush_start->supercontents;
747 trace->startsolid = true;
749 trace->allsolid = true;
753 // LordHavoc: we need an epsilon nudge here because for a point trace the
754 // penetrating line segment is normally zero length if this brush was
755 // generated from a polygon (infinitely thin), and could even be slightly
756 // positive or negative due to rounding errors in that case.
757 if (brushsolid && enterfrac > -1 && enterfrac < trace->realfraction && enterfrac <= leavefrac)
761 if (thatbrush_start->ispolygon)
763 d1 = DotProduct(thatbrush_start->planes[0].normal, linestart) - thatbrush_start->planes[0].dist - collision_startnudge.value;
764 d2 = DotProduct(thatbrush_end->planes[0].normal, lineend) - thatbrush_end->planes[0].dist - collision_endnudge.value;
766 if (move <= 0 || d2 > collision_enternudge.value || d1 < 0)
770 enterfrac = (d1 - collision_enternudge.value) * imove;
771 if (enterfrac < trace->realfraction)
773 enterfrac2 = enterfrac - collision_impactnudge.value * imove;
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 VectorLerp(thatbrush_start->planes[0].normal, enterfrac, thatbrush_end->planes[0].normal, trace->plane.normal);
785 trace->hitsupercontents = thatbrush_start->supercontents;
786 trace->hitq3surfaceflags = hitq3surfaceflags;
787 trace->hittexture = hittexture;
788 trace->realfraction = bound(0, enterfrac, 1);
789 trace->fraction = bound(0, enterfrac2, 1);
790 VectorCopy(newimpactnormal, trace->plane.normal);
795 void Collision_TracePointBrushFloat(trace_t *trace, const vec3_t point, const colbrushf_t *thatbrush)
798 const colplanef_t *plane;
800 for (nplane = 0, plane = thatbrush->planes;nplane < thatbrush->numplanes;nplane++, plane++)
801 if (DotProduct(plane->normal, point) > plane->dist)
804 trace->startsupercontents |= thatbrush->supercontents;
805 if (trace->hitsupercontentsmask & thatbrush->supercontents)
807 trace->startsolid = true;
808 trace->allsolid = true;
812 static colpointf_t polyf_points[256];
813 static colplanef_t polyf_planes[256 + 2];
814 static colbrushf_t polyf_brush;
816 void Collision_SnapCopyPoints(int numpoints, const colpointf_t *in, colpointf_t *out, float fractionprecision, float invfractionprecision)
820 out->v[0] = floor(in->v[0] * fractionprecision + 0.5f) * invfractionprecision;
821 out->v[1] = floor(in->v[1] * fractionprecision + 0.5f) * invfractionprecision;
822 out->v[2] = floor(in->v[2] * fractionprecision + 0.5f) * invfractionprecision;
826 void Collision_TraceBrushPolygonFloat(trace_t *trace, const colbrushf_t *thisbrush_start, const colbrushf_t *thisbrush_end, int numpoints, const float *points, int supercontents)
830 Con_Print("Polygon with more than 256 points not supported yet (fixme!)\n");
833 polyf_brush.numpoints = numpoints;
834 polyf_brush.numplanes = numpoints + 2;
835 //polyf_brush.points = (colpointf_t *)points;
836 polyf_brush.planes = polyf_planes;
837 polyf_brush.supercontents = supercontents;
838 polyf_brush.points = polyf_points;
839 Collision_SnapCopyPoints(numpoints, (colpointf_t *)points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
840 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
841 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
842 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brush, &polyf_brush);
845 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)
848 float facemins[3], facemaxs[3];
849 polyf_brush.numpoints = 3;
850 polyf_brush.numplanes = 5;
851 polyf_brush.points = polyf_points;
852 polyf_brush.planes = polyf_planes;
853 polyf_brush.supercontents = supercontents;
854 for (i = 0;i < polyf_brush.numplanes;i++)
856 polyf_brush.planes[i].q3surfaceflags = q3surfaceflags;
857 polyf_brush.planes[i].texture = texture;
859 for (i = 0;i < numtriangles;i++, element3i += 3)
861 VectorCopy(vertex3f + element3i[0] * 3, polyf_points[0].v);
862 VectorCopy(vertex3f + element3i[1] * 3, polyf_points[1].v);
863 VectorCopy(vertex3f + element3i[2] * 3, polyf_points[2].v);
864 Collision_SnapCopyPoints(3, polyf_points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
865 facemins[0] = min(polyf_points[0].v[0], min(polyf_points[1].v[0], polyf_points[2].v[0]));
866 facemins[1] = min(polyf_points[0].v[1], min(polyf_points[1].v[1], polyf_points[2].v[1]));
867 facemins[2] = min(polyf_points[0].v[2], min(polyf_points[1].v[2], polyf_points[2].v[2]));
868 facemaxs[0] = max(polyf_points[0].v[0], max(polyf_points[1].v[0], polyf_points[2].v[0]));
869 facemaxs[1] = max(polyf_points[0].v[1], max(polyf_points[1].v[1], polyf_points[2].v[1]));
870 facemaxs[2] = max(polyf_points[0].v[2], max(polyf_points[1].v[2], polyf_points[2].v[2]));
871 if (BoxesOverlap(segmentmins, segmentmaxs, facemins, facemaxs))
873 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
874 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
875 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brush, &polyf_brush);
880 void Collision_TraceLinePolygonFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, int numpoints, const float *points, int supercontents)
884 Con_Print("Polygon with more than 256 points not supported yet (fixme!)\n");
887 polyf_brush.numpoints = numpoints;
888 polyf_brush.numplanes = numpoints + 2;
889 //polyf_brush.points = (colpointf_t *)points;
890 polyf_brush.points = polyf_points;
891 Collision_SnapCopyPoints(numpoints, (colpointf_t *)points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
892 polyf_brush.planes = polyf_planes;
893 polyf_brush.supercontents = supercontents;
894 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
895 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
896 Collision_TraceLineBrushFloat(trace, linestart, lineend, &polyf_brush, &polyf_brush);
899 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)
903 // FIXME: snap vertices?
904 for (i = 0;i < numtriangles;i++, element3i += 3)
905 Collision_TraceLineTriangleFloat(trace, linestart, lineend, vertex3f + element3i[0] * 3, vertex3f + element3i[1] * 3, vertex3f + element3i[2] * 3, supercontents, q3surfaceflags, texture);
907 polyf_brush.numpoints = 3;
908 polyf_brush.numplanes = 5;
909 polyf_brush.points = polyf_points;
910 polyf_brush.planes = polyf_planes;
911 polyf_brush.supercontents = supercontents;
912 for (i = 0;i < polyf_brush.numplanes;i++)
914 polyf_brush.planes[i].supercontents = supercontents;
915 polyf_brush.planes[i].q3surfaceflags = q3surfaceflags;
916 polyf_brush.planes[i].texture = texture;
918 for (i = 0;i < numtriangles;i++, element3i += 3)
920 float facemins[3], facemaxs[3];
921 VectorCopy(vertex3f + element3i[0] * 3, polyf_points[0].v);
922 VectorCopy(vertex3f + element3i[1] * 3, polyf_points[1].v);
923 VectorCopy(vertex3f + element3i[2] * 3, polyf_points[2].v);
924 Collision_SnapCopyPoints(numpoints, polyf_points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
925 facemins[0] = min(polyf_points[0].v[0], min(polyf_points[1].v[0], polyf_points[2].v[0]));
926 facemins[1] = min(polyf_points[0].v[1], min(polyf_points[1].v[1], polyf_points[2].v[1]));
927 facemins[2] = min(polyf_points[0].v[2], min(polyf_points[1].v[2], polyf_points[2].v[2]));
928 facemaxs[0] = max(polyf_points[0].v[0], max(polyf_points[1].v[0], polyf_points[2].v[0]));
929 facemaxs[1] = max(polyf_points[0].v[1], max(polyf_points[1].v[1], polyf_points[2].v[1]));
930 facemaxs[2] = max(polyf_points[0].v[2], max(polyf_points[1].v[2], polyf_points[2].v[2]));
931 if (BoxesOverlap(segmentmins, segmentmaxs, facemins, facemaxs))
933 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
934 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
935 Collision_TraceLineBrushFloat(trace, linestart, lineend, &polyf_brush, &polyf_brush);
942 static colpointf_t polyf_pointsstart[256], polyf_pointsend[256];
943 static colplanef_t polyf_planesstart[256 + 2], polyf_planesend[256 + 2];
944 static colbrushf_t polyf_brushstart, polyf_brushend;
946 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)
951 Con_Print("Polygon with more than 256 points not supported yet (fixme!)\n");
954 polyf_brushstart.numpoints = numpoints;
955 polyf_brushstart.numplanes = numpoints + 2;
956 polyf_brushstart.points = polyf_pointsstart;//(colpointf_t *)points;
957 polyf_brushstart.planes = polyf_planesstart;
958 polyf_brushstart.supercontents = supercontents;
959 for (i = 0;i < numpoints;i++)
960 Matrix4x4_Transform(polygonmatrixstart, points + i * 3, polyf_brushstart.points[i].v);
961 polyf_brushend.numpoints = numpoints;
962 polyf_brushend.numplanes = numpoints + 2;
963 polyf_brushend.points = polyf_pointsend;//(colpointf_t *)points;
964 polyf_brushend.planes = polyf_planesend;
965 polyf_brushend.supercontents = supercontents;
966 for (i = 0;i < numpoints;i++)
967 Matrix4x4_Transform(polygonmatrixend, points + i * 3, polyf_brushend.points[i].v);
968 for (i = 0;i < polyf_brushstart.numplanes;i++)
970 polyf_brushstart.planes[i].q3surfaceflags = q3surfaceflags;
971 polyf_brushstart.planes[i].texture = texture;
973 Collision_SnapCopyPoints(numpoints, polyf_pointsstart, polyf_pointsstart, COLLISION_SNAPSCALE, COLLISION_SNAP);
974 Collision_SnapCopyPoints(numpoints, polyf_pointsend, polyf_pointsend, COLLISION_SNAPSCALE, COLLISION_SNAP);
975 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brushstart);
976 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brushend);
978 //Collision_PrintBrushAsQHull(&polyf_brushstart, "polyf_brushstart");
979 //Collision_PrintBrushAsQHull(&polyf_brushend, "polyf_brushend");
981 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brushstart, &polyf_brushend);
986 #define MAX_BRUSHFORBOX 16
987 static int brushforbox_index = 0;
988 static colpointf_t brushforbox_point[MAX_BRUSHFORBOX*8];
989 static colplanef_t brushforbox_plane[MAX_BRUSHFORBOX*6];
990 static colbrushf_t brushforbox_brush[MAX_BRUSHFORBOX];
991 static colbrushf_t brushforpoint_brush[MAX_BRUSHFORBOX];
993 void Collision_InitBrushForBox(void)
996 for (i = 0;i < MAX_BRUSHFORBOX;i++)
998 brushforbox_brush[i].numpoints = 8;
999 brushforbox_brush[i].numplanes = 6;
1000 brushforbox_brush[i].points = brushforbox_point + i * 8;
1001 brushforbox_brush[i].planes = brushforbox_plane + i * 6;
1002 brushforpoint_brush[i].numpoints = 1;
1003 brushforpoint_brush[i].numplanes = 0;
1004 brushforpoint_brush[i].points = brushforbox_point + i * 8;
1005 brushforpoint_brush[i].planes = brushforbox_plane + i * 6;
1009 colbrushf_t *Collision_BrushForBox(const matrix4x4_t *matrix, const vec3_t mins, const vec3_t maxs, int supercontents, int q3surfaceflags, texture_t *texture)
1014 if (brushforbox_brush[0].numpoints == 0)
1015 Collision_InitBrushForBox();
1016 // FIXME: these probably don't actually need to be normalized if the collision code does not care
1017 if (VectorCompare(mins, maxs))
1020 brush = brushforpoint_brush + ((brushforbox_index++) % MAX_BRUSHFORBOX);
1021 VectorCopy(mins, brush->points->v);
1025 brush = brushforbox_brush + ((brushforbox_index++) % MAX_BRUSHFORBOX);
1027 for (i = 0;i < 8;i++)
1029 v[0] = i & 1 ? maxs[0] : mins[0];
1030 v[1] = i & 2 ? maxs[1] : mins[1];
1031 v[2] = i & 4 ? maxs[2] : mins[2];
1032 Matrix4x4_Transform(matrix, v, brush->points[i].v);
1035 for (i = 0;i < 6;i++)
1038 v[i >> 1] = i & 1 ? 1 : -1;
1039 Matrix4x4_Transform3x3(matrix, v, brush->planes[i].normal);
1040 VectorNormalize(brush->planes[i].normal);
1043 brush->supercontents = supercontents;
1044 for (j = 0;j < brush->numplanes;j++)
1046 brush->planes[j].q3surfaceflags = q3surfaceflags;
1047 brush->planes[j].texture = texture;
1048 brush->planes[j].dist = furthestplanedist_float(brush->planes[j].normal, brush->points, brush->numpoints);
1050 VectorCopy(brush->points[0].v, brush->mins);
1051 VectorCopy(brush->points[0].v, brush->maxs);
1052 for (j = 1;j < brush->numpoints;j++)
1054 brush->mins[0] = min(brush->mins[0], brush->points[j].v[0]);
1055 brush->mins[1] = min(brush->mins[1], brush->points[j].v[1]);
1056 brush->mins[2] = min(brush->mins[2], brush->points[j].v[2]);
1057 brush->maxs[0] = max(brush->maxs[0], brush->points[j].v[0]);
1058 brush->maxs[1] = max(brush->maxs[1], brush->points[j].v[1]);
1059 brush->maxs[2] = max(brush->maxs[2], brush->points[j].v[2]);
1061 brush->mins[0] -= 1;
1062 brush->mins[1] -= 1;
1063 brush->mins[2] -= 1;
1064 brush->maxs[0] += 1;
1065 brush->maxs[1] += 1;
1066 brush->maxs[2] += 1;
1067 Collision_ValidateBrush(brush);
1071 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)
1073 colbrushf_t *boxbrush, *thisbrush_start, *thisbrush_end;
1074 vec3_t startmins, startmaxs, endmins, endmaxs;
1076 // create brushes for the collision
1077 VectorAdd(start, mins, startmins);
1078 VectorAdd(start, maxs, startmaxs);
1079 VectorAdd(end, mins, endmins);
1080 VectorAdd(end, maxs, endmaxs);
1081 boxbrush = Collision_BrushForBox(&identitymatrix, cmins, cmaxs, supercontents, q3surfaceflags, texture);
1082 thisbrush_start = Collision_BrushForBox(&identitymatrix, startmins, startmaxs, 0, 0, NULL);
1083 thisbrush_end = Collision_BrushForBox(&identitymatrix, endmins, endmaxs, 0, 0, NULL);
1085 memset(trace, 0, sizeof(trace_t));
1086 trace->hitsupercontentsmask = hitsupercontentsmask;
1087 trace->fraction = 1;
1088 trace->realfraction = 1;
1089 trace->allsolid = true;
1090 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, boxbrush, boxbrush);
1093 //pseudocode for detecting line/sphere overlap without calculating an impact point
1094 //linesphereorigin = sphereorigin - linestart;linediff = lineend - linestart;linespherefrac = DotProduct(linesphereorigin, linediff) / DotProduct(linediff, linediff);return VectorLength2(linesphereorigin - bound(0, linespherefrac, 1) * linediff) >= sphereradius*sphereradius;
1096 // LordHavoc: currently unused, but tested
1097 // note: this can be used for tracing a moving sphere vs a stationary sphere,
1098 // by simply adding the moving sphere's radius to the sphereradius parameter,
1099 // all the results are correct (impactpoint, impactnormal, and fraction)
1100 float Collision_ClipTrace_Line_Sphere(double *linestart, double *lineend, double *sphereorigin, double sphereradius, double *impactpoint, double *impactnormal)
1102 double dir[3], scale, v[3], deviationdist, impactdist, linelength;
1103 // make sure the impactpoint and impactnormal are valid even if there is
1105 VectorCopy(lineend, impactpoint);
1106 VectorClear(impactnormal);
1107 // calculate line direction
1108 VectorSubtract(lineend, linestart, dir);
1109 // normalize direction
1110 linelength = VectorLength(dir);
1113 scale = 1.0 / linelength;
1114 VectorScale(dir, scale, dir);
1116 // this dotproduct calculates the distance along the line at which the
1117 // sphere origin is (nearest point to the sphere origin on the line)
1118 impactdist = DotProduct(sphereorigin, dir) - DotProduct(linestart, dir);
1119 // calculate point on line at that distance, and subtract the
1120 // sphereorigin from it, so we have a vector to measure for the distance
1121 // of the line from the sphereorigin (deviation, how off-center it is)
1122 VectorMA(linestart, impactdist, dir, v);
1123 VectorSubtract(v, sphereorigin, v);
1124 deviationdist = VectorLength2(v);
1125 // if outside the radius, it's a miss for sure
1126 // (we do this comparison using squared radius to avoid a sqrt)
1127 if (deviationdist > sphereradius*sphereradius)
1128 return 1; // miss (off to the side)
1129 // nudge back to find the correct impact distance
1130 impactdist += deviationdist - sphereradius;
1131 if (impactdist >= linelength)
1132 return 1; // miss (not close enough)
1134 return 1; // miss (linestart is past or inside sphere)
1135 // calculate new impactpoint
1136 VectorMA(linestart, impactdist, dir, impactpoint);
1137 // calculate impactnormal (surface normal at point of impact)
1138 VectorSubtract(impactpoint, sphereorigin, impactnormal);
1139 // normalize impactnormal
1140 VectorNormalize(impactnormal);
1141 // return fraction of movement distance
1142 return impactdist / linelength;
1145 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)
1149 float d1, d2, d, f, impact[3], edgenormal[3], faceplanenormal[3], faceplanedist, faceplanenormallength2, edge01[3], edge21[3], edge02[3];
1151 // this function executes:
1152 // 32 ops when line starts behind triangle
1153 // 38 ops when line ends infront of triangle
1154 // 43 ops when line fraction is already closer than this triangle
1155 // 72 ops when line is outside edge 01
1156 // 92 ops when line is outside edge 21
1157 // 115 ops when line is outside edge 02
1158 // 123 ops when line impacts triangle and updates trace results
1160 // this code is designed for clockwise triangles, conversion to
1161 // counterclockwise would require swapping some things around...
1162 // it is easier to simply swap the point0 and point2 parameters to this
1163 // function when calling it than it is to rewire the internals.
1165 // calculate the faceplanenormal of the triangle, this represents the front side
1167 VectorSubtract(point0, point1, edge01);
1168 VectorSubtract(point2, point1, edge21);
1169 CrossProduct(edge01, edge21, faceplanenormal);
1170 // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
1172 faceplanenormallength2 = DotProduct(faceplanenormal, faceplanenormal);
1173 if (faceplanenormallength2 < 0.0001f)
1175 // calculate the distance
1177 faceplanedist = DotProduct(point0, faceplanenormal);
1179 // if start point is on the back side there is no collision
1180 // (we don't care about traces going through the triangle the wrong way)
1182 // calculate the start distance
1184 d1 = DotProduct(faceplanenormal, linestart);
1185 if (d1 <= faceplanedist)
1188 // calculate the end distance
1190 d2 = DotProduct(faceplanenormal, lineend);
1191 // if both are in front, there is no collision
1192 if (d2 >= faceplanedist)
1195 // from here on we know d1 is >= 0 and d2 is < 0
1196 // this means the line starts infront and ends behind, passing through it
1198 // calculate the recipricol of the distance delta,
1199 // so we can use it multiple times cheaply (instead of division)
1201 d = 1.0f / (d1 - d2);
1202 // calculate the impact fraction by taking the start distance (> 0)
1203 // and subtracting the face plane distance (this is the distance of the
1204 // triangle along that same normal)
1205 // then multiply by the recipricol distance delta
1207 f = (d1 - faceplanedist) * d;
1208 // skip out if this impact is further away than previous ones
1210 if (f > trace->realfraction)
1212 // calculate the perfect impact point for classification of insidedness
1214 impact[0] = linestart[0] + f * (lineend[0] - linestart[0]);
1215 impact[1] = linestart[1] + f * (lineend[1] - linestart[1]);
1216 impact[2] = linestart[2] + f * (lineend[2] - linestart[2]);
1218 // calculate the edge normal and reject if impact is outside triangle
1219 // (an edge normal faces away from the triangle, to get the desired normal
1220 // a crossproduct with the faceplanenormal is used, and because of the way
1221 // the insidedness comparison is written it does not need to be normalized)
1223 // first use the two edges from the triangle plane math
1224 // the other edge only gets calculated if the point survives that long
1227 CrossProduct(edge01, faceplanenormal, edgenormal);
1228 if (DotProduct(impact, edgenormal) > DotProduct(point1, edgenormal))
1232 CrossProduct(faceplanenormal, edge21, edgenormal);
1233 if (DotProduct(impact, edgenormal) > DotProduct(point2, edgenormal))
1237 VectorSubtract(point0, point2, edge02);
1238 CrossProduct(faceplanenormal, edge02, edgenormal);
1239 if (DotProduct(impact, edgenormal) > DotProduct(point0, edgenormal))
1244 // store the new trace fraction
1245 trace->realfraction = f;
1247 // calculate a nudged fraction to keep it out of the surface
1248 // (the main fraction remains perfect)
1249 trace->fraction = f - collision_impactnudge.value * d;
1251 // store the new trace plane (because collisions only happen from
1252 // the front this is always simply the triangle normal, never flipped)
1253 d = 1.0 / sqrt(faceplanenormallength2);
1254 VectorScale(faceplanenormal, d, trace->plane.normal);
1255 trace->plane.dist = faceplanedist * d;
1257 trace->hitsupercontents = supercontents;
1258 trace->hitq3surfaceflags = q3surfaceflags;
1259 trace->hittexture = texture;
1261 float d1, d2, d, f, fnudged, impact[3], edgenormal[3], faceplanenormal[3], faceplanedist, edge[3];
1263 // this code is designed for clockwise triangles, conversion to
1264 // counterclockwise would require swapping some things around...
1265 // it is easier to simply swap the point0 and point2 parameters to this
1266 // function when calling it than it is to rewire the internals.
1268 // calculate the unnormalized faceplanenormal of the triangle,
1269 // this represents the front side
1270 TriangleNormal(point0, point1, point2, faceplanenormal);
1271 // there's no point in processing a degenerate triangle
1272 // (GIGO - Garbage In, Garbage Out)
1273 if (DotProduct(faceplanenormal, faceplanenormal) < 0.0001f)
1275 // calculate the unnormalized distance
1276 faceplanedist = DotProduct(point0, faceplanenormal);
1278 // calculate the unnormalized start distance
1279 d1 = DotProduct(faceplanenormal, linestart) - faceplanedist;
1280 // if start point is on the back side there is no collision
1281 // (we don't care about traces going through the triangle the wrong way)
1285 // calculate the unnormalized end distance
1286 d2 = DotProduct(faceplanenormal, lineend) - faceplanedist;
1287 // if both are in front, there is no collision
1291 // from here on we know d1 is >= 0 and d2 is < 0
1292 // this means the line starts infront and ends behind, passing through it
1294 // calculate the recipricol of the distance delta,
1295 // so we can use it multiple times cheaply (instead of division)
1296 d = 1.0f / (d1 - d2);
1297 // calculate the impact fraction by taking the start distance (> 0)
1298 // and subtracting the face plane distance (this is the distance of the
1299 // triangle along that same normal)
1300 // then multiply by the recipricol distance delta
1302 // skip out if this impact is further away than previous ones
1303 if (f > trace->realfraction)
1305 // calculate the perfect impact point for classification of insidedness
1306 impact[0] = linestart[0] + f * (lineend[0] - linestart[0]);
1307 impact[1] = linestart[1] + f * (lineend[1] - linestart[1]);
1308 impact[2] = linestart[2] + f * (lineend[2] - linestart[2]);
1310 // calculate the edge normal and reject if impact is outside triangle
1311 // (an edge normal faces away from the triangle, to get the desired normal
1312 // a crossproduct with the faceplanenormal is used, and because of the way
1313 // the insidedness comparison is written it does not need to be normalized)
1315 VectorSubtract(point2, point0, edge);
1316 CrossProduct(edge, faceplanenormal, edgenormal);
1317 if (DotProduct(impact, edgenormal) > DotProduct(point0, edgenormal))
1320 VectorSubtract(point0, point1, edge);
1321 CrossProduct(edge, faceplanenormal, edgenormal);
1322 if (DotProduct(impact, edgenormal) > DotProduct(point1, edgenormal))
1325 VectorSubtract(point1, point2, edge);
1326 CrossProduct(edge, faceplanenormal, edgenormal);
1327 if (DotProduct(impact, edgenormal) > DotProduct(point2, edgenormal))
1330 // store the new trace fraction
1331 trace->realfraction = bound(0, f, 1);
1333 // store the new trace plane (because collisions only happen from
1334 // the front this is always simply the triangle normal, never flipped)
1335 VectorNormalize(faceplanenormal);
1336 VectorCopy(faceplanenormal, trace->plane.normal);
1337 trace->plane.dist = DotProduct(point0, faceplanenormal);
1339 // calculate the normalized start and end distances
1340 d1 = DotProduct(trace->plane.normal, linestart) - trace->plane.dist;
1341 d2 = DotProduct(trace->plane.normal, lineend) - trace->plane.dist;
1343 // calculate a nudged fraction to keep it out of the surface
1344 // (the main fraction remains perfect)
1345 fnudged = (d1 - collision_impactnudge.value) / (d1 - d2);
1346 trace->fraction = bound(0, fnudged, 1);
1348 // store the new trace endpos
1349 // not needed, it's calculated later when the trace is finished
1350 //trace->endpos[0] = linestart[0] + fnudged * (lineend[0] - linestart[0]);
1351 //trace->endpos[1] = linestart[1] + fnudged * (lineend[1] - linestart[1]);
1352 //trace->endpos[2] = linestart[2] + fnudged * (lineend[2] - linestart[2]);
1353 trace->hitsupercontents = supercontents;
1354 trace->hitq3surfaceflags = q3surfaceflags;
1355 trace->hittexture = texture;
1359 typedef struct colbspnode_s
1362 struct colbspnode_s *children[2];
1363 // the node is reallocated or split if max is reached
1366 colbrushf_t **colbrushflist;
1369 //colbrushd_t **colbrushdlist;
1373 typedef struct colbsp_s
1376 colbspnode_t *nodes;
1380 colbsp_t *Collision_CreateCollisionBSP(mempool_t *mempool)
1383 bsp = (colbsp_t *)Mem_Alloc(mempool, sizeof(colbsp_t));
1384 bsp->mempool = mempool;
1385 bsp->nodes = (colbspnode_t *)Mem_Alloc(bsp->mempool, sizeof(colbspnode_t));
1389 void Collision_FreeCollisionBSPNode(colbspnode_t *node)
1391 if (node->children[0])
1392 Collision_FreeCollisionBSPNode(node->children[0]);
1393 if (node->children[1])
1394 Collision_FreeCollisionBSPNode(node->children[1]);
1395 while (--node->numcolbrushf)
1396 Mem_Free(node->colbrushflist[node->numcolbrushf]);
1397 //while (--node->numcolbrushd)
1398 // Mem_Free(node->colbrushdlist[node->numcolbrushd]);
1402 void Collision_FreeCollisionBSP(colbsp_t *bsp)
1404 Collision_FreeCollisionBSPNode(bsp->nodes);
1408 void Collision_BoundingBoxOfBrushTraceSegment(const colbrushf_t *start, const colbrushf_t *end, vec3_t mins, vec3_t maxs, float startfrac, float endfrac)
1411 colpointf_t *ps, *pe;
1412 float tempstart[3], tempend[3];
1413 VectorLerp(start->points[0].v, startfrac, end->points[0].v, mins);
1414 VectorCopy(mins, maxs);
1415 for (i = 0, ps = start->points, pe = end->points;i < start->numpoints;i++, ps++, pe++)
1417 VectorLerp(ps->v, startfrac, pe->v, tempstart);
1418 VectorLerp(ps->v, endfrac, pe->v, tempend);
1419 mins[0] = min(mins[0], min(tempstart[0], tempend[0]));
1420 mins[1] = min(mins[1], min(tempstart[1], tempend[1]));
1421 mins[2] = min(mins[2], min(tempstart[2], tempend[2]));
1422 maxs[0] = min(maxs[0], min(tempstart[0], tempend[0]));
1423 maxs[1] = min(maxs[1], min(tempstart[1], tempend[1]));
1424 maxs[2] = min(maxs[2], min(tempstart[2], tempend[2]));