5 #define COLLISION_SNAPSCALE (32.0f)
6 #define COLLISION_SNAP (1.0f / COLLISION_SNAPSCALE)
7 #define COLLISION_PLANE_DIST_EPSILON (1.0f / 32.0f)
9 cvar_t collision_impactnudge = {0, "collision_impactnudge", "0.03125", "how much to back off from the impact"};
10 cvar_t collision_startnudge = {0, "collision_startnudge", "0", "how much to bias collision trace start"};
11 cvar_t collision_endnudge = {0, "collision_endnudge", "0", "how much to bias collision trace end"};
12 cvar_t collision_enternudge = {0, "collision_enternudge", "0", "how much to bias collision entry fraction"};
13 cvar_t collision_leavenudge = {0, "collision_leavenudge", "0", "how much to bias collision exit fraction"};
15 void Collision_Init (void)
17 Cvar_RegisterVariable(&collision_impactnudge);
18 Cvar_RegisterVariable(&collision_startnudge);
19 Cvar_RegisterVariable(&collision_endnudge);
20 Cvar_RegisterVariable(&collision_enternudge);
21 Cvar_RegisterVariable(&collision_leavenudge);
37 void Collision_PrintBrushAsQHull(colbrushf_t *brush, const char *name)
40 Con_Printf("3 %s\n%i\n", name, brush->numpoints);
41 for (i = 0;i < brush->numpoints;i++)
42 Con_Printf("%f %f %f\n", brush->points[i].v[0], brush->points[i].v[1], brush->points[i].v[2]);
44 Con_Printf("4\n%i\n", brush->numplanes);
45 for (i = 0;i < brush->numplanes;i++)
46 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);
49 void Collision_ValidateBrush(colbrushf_t *brush)
51 int j, k, pointsoffplanes, pointonplanes, pointswithinsufficientplanes, printbrush;
54 if (!brush->numpoints)
56 Con_Print("Collision_ValidateBrush: brush with no points!\n");
60 // it's ok for a brush to have one point and no planes...
61 if (brush->numplanes == 0 && brush->numpoints != 1)
63 Con_Print("Collision_ValidateBrush: brush with no planes and more than one point!\n");
70 pointswithinsufficientplanes = 0;
71 for (k = 0;k < brush->numplanes;k++)
72 if (DotProduct(brush->planes[k].normal, brush->planes[k].normal) < 0.0001f)
73 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);
74 for (j = 0;j < brush->numpoints;j++)
77 for (k = 0;k < brush->numplanes;k++)
79 d = DotProduct(brush->points[j].v, brush->planes[k].normal) - brush->planes[k].dist;
80 if (d > COLLISION_PLANE_DIST_EPSILON)
82 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);
85 if (fabs(d) > COLLISION_PLANE_DIST_EPSILON)
90 if (pointonplanes < 3)
91 pointswithinsufficientplanes++;
93 if (pointswithinsufficientplanes)
95 Con_Print("Collision_ValidateBrush: some points have insufficient planes, every point must be on at least 3 planes to form a corner.\n");
98 if (pointsoffplanes == 0) // all points are on all planes
100 Con_Print("Collision_ValidateBrush: all points lie on all planes (degenerate, no brush volume!)\n");
105 Collision_PrintBrushAsQHull(brush, "unnamed");
108 float nearestplanedist_float(const float *normal, const colpointf_t *points, int numpoints)
110 float dist, bestdist;
111 bestdist = DotProduct(points->v, normal);
115 dist = DotProduct(points->v, normal);
116 bestdist = min(bestdist, dist);
122 float furthestplanedist_float(const float *normal, const colpointf_t *points, int numpoints)
124 float dist, bestdist;
125 bestdist = DotProduct(points->v, normal);
129 dist = DotProduct(points->v, normal);
130 bestdist = max(bestdist, dist);
137 colbrushf_t *Collision_NewBrushFromPlanes(mempool_t *mempool, int numoriginalplanes, const colplanef_t *originalplanes, int supercontents)
139 // TODO: planesbuf could be replaced by a remapping table
141 int numpointsbuf = 0, maxpointsbuf = 256, numplanesbuf = 0, maxplanesbuf = 256, numelementsbuf = 0, maxelementsbuf = 256;
144 colpointf_t pointsbuf[256];
145 colplanef_t planesbuf[256];
146 int elementsbuf[1024];
147 int polypointbuf[256];
152 // enable these if debugging to avoid seeing garbage in unused data
153 memset(pointsbuf, 0, sizeof(pointsbuf));
154 memset(planesbuf, 0, sizeof(planesbuf));
155 memset(elementsbuf, 0, sizeof(elementsbuf));
156 memset(polypointbuf, 0, sizeof(polypointbuf));
157 memset(p, 0, sizeof(p));
159 // figure out how large a bounding box we need to properly compute this brush
161 for (j = 0;j < numoriginalplanes;j++)
162 maxdist = max(maxdist, originalplanes[j].dist);
163 // now make it large enough to enclose the entire brush, and round it off to a reasonable multiple of 1024
164 maxdist = floor(maxdist * (4.0 / 1024.0) + 1) * 1024.0;
165 // construct a collision brush (points, planes, and renderable mesh) from
166 // a set of planes, this also optimizes out any unnecessary planes (ones
167 // whose polygon is clipped away by the other planes)
168 for (j = 0;j < numoriginalplanes;j++)
170 // add the plane uniquely (no duplicates)
171 for (k = 0;k < numplanesbuf;k++)
172 if (VectorCompare(planesbuf[k].normal, originalplanes[j].normal) && planesbuf[k].dist == originalplanes[j].dist)
174 // if the plane is a duplicate, skip it
175 if (k < numplanesbuf)
177 // check if there are too many and skip the brush
178 if (numplanesbuf >= maxplanesbuf)
180 Con_Print("Collision_NewBrushFromPlanes: failed to build collision brush: too many planes for buffer\n");
184 // create a large polygon from the plane
186 PolygonD_QuadForPlane(p[w], originalplanes[j].normal[0], originalplanes[j].normal[1], originalplanes[j].normal[2], originalplanes[j].dist, maxdist);
188 // clip it by all other planes
189 for (k = 0;k < numoriginalplanes && pnumpoints && pnumpoints <= pmaxpoints;k++)
193 // we want to keep the inside of the brush plane so we flip
195 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);
199 // if nothing is left, skip it
202 //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);
206 for (k = 0;k < pnumpoints;k++)
210 for (l = 0;l < numoriginalplanes;l++)
211 if (fabs(DotProduct(&p[w][k*3], originalplanes[l].normal) - originalplanes[l].dist) < COLLISION_PLANE_DIST_EPSILON)
218 Con_Printf("Collision_NewBrushFromPlanes: warning: polygon point does not lie on at least 3 planes\n");
222 // check if there are too many polygon vertices for buffer
223 if (pnumpoints > pmaxpoints)
225 Con_Print("Collision_NewBrushFromPlanes: failed to build collision brush: too many points for buffer\n");
229 // check if there are too many triangle elements for buffer
230 if (numelementsbuf + (pnumpoints - 2) * 3 > maxelementsbuf)
232 Con_Print("Collision_NewBrushFromPlanes: failed to build collision brush: too many triangle elements for buffer\n");
236 for (k = 0;k < pnumpoints;k++)
238 // check if there is already a matching point (no duplicates)
239 for (m = 0;m < numpointsbuf;m++)
240 if (VectorDistance2(&p[w][k*3], pointsbuf[m].v) < COLLISION_SNAP)
243 // if there is no match, add a new one
244 if (m == numpointsbuf)
246 // check if there are too many and skip the brush
247 if (numpointsbuf >= maxpointsbuf)
249 Con_Print("Collision_NewBrushFromPlanes: failed to build collision brush: too many points for buffer\n");
253 VectorCopy(&p[w][k*3], pointsbuf[numpointsbuf].v);
257 // store the index into a buffer
261 // add the triangles for the polygon
262 // (this particular code makes a triangle fan)
263 for (k = 0;k < pnumpoints - 2;k++)
265 elementsbuf[numelementsbuf++] = polypointbuf[0];
266 elementsbuf[numelementsbuf++] = polypointbuf[k + 1];
267 elementsbuf[numelementsbuf++] = polypointbuf[k + 2];
271 VectorCopy(originalplanes[j].normal, planesbuf[numplanesbuf].normal);
272 planesbuf[numplanesbuf].dist = originalplanes[j].dist;
273 planesbuf[numplanesbuf].q3surfaceflags = originalplanes[j].q3surfaceflags;
274 planesbuf[numplanesbuf].texture = originalplanes[j].texture;
278 // validate plane distances
279 for (j = 0;j < numplanesbuf;j++)
281 float d = furthestplanedist_float(planesbuf[j].normal, pointsbuf, numpointsbuf);
282 if (fabs(planesbuf[j].dist - d) > COLLISION_PLANE_DIST_EPSILON)
283 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);
286 // if nothing is left, there's nothing to allocate
287 if (numelementsbuf < 12 || numplanesbuf < 4 || numpointsbuf < 4)
289 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);
293 // allocate the brush and copy to it
294 brush = (colbrushf_t *)Mem_Alloc(mempool, sizeof(colbrushf_t) + sizeof(colpointf_t) * numpointsbuf + sizeof(colplanef_t) * numplanesbuf + sizeof(int) * numelementsbuf);
295 brush->supercontents = supercontents;
296 brush->numplanes = numplanesbuf;
297 brush->numpoints = numpointsbuf;
298 brush->numtriangles = numelementsbuf / 3;
299 brush->planes = (colplanef_t *)(brush + 1);
300 brush->points = (colpointf_t *)(brush->planes + brush->numplanes);
301 brush->elements = (int *)(brush->points + brush->numpoints);
302 for (j = 0;j < brush->numpoints;j++)
304 brush->points[j].v[0] = pointsbuf[j].v[0];
305 brush->points[j].v[1] = pointsbuf[j].v[1];
306 brush->points[j].v[2] = pointsbuf[j].v[2];
308 for (j = 0;j < brush->numplanes;j++)
310 brush->planes[j].normal[0] = planesbuf[j].normal[0];
311 brush->planes[j].normal[1] = planesbuf[j].normal[1];
312 brush->planes[j].normal[2] = planesbuf[j].normal[2];
313 brush->planes[j].dist = planesbuf[j].dist;
314 brush->planes[j].q3surfaceflags = planesbuf[j].q3surfaceflags;
315 brush->planes[j].texture = planesbuf[j].texture;
317 for (j = 0;j < brush->numtriangles * 3;j++)
318 brush->elements[j] = elementsbuf[j];
319 VectorCopy(brush->points[0].v, brush->mins);
320 VectorCopy(brush->points[0].v, brush->maxs);
321 for (j = 1;j < brush->numpoints;j++)
323 brush->mins[0] = min(brush->mins[0], brush->points[j].v[0]);
324 brush->mins[1] = min(brush->mins[1], brush->points[j].v[1]);
325 brush->mins[2] = min(brush->mins[2], brush->points[j].v[2]);
326 brush->maxs[0] = max(brush->maxs[0], brush->points[j].v[0]);
327 brush->maxs[1] = max(brush->maxs[1], brush->points[j].v[1]);
328 brush->maxs[2] = max(brush->maxs[2], brush->points[j].v[2]);
336 Collision_ValidateBrush(brush);
342 void Collision_CalcPlanesForPolygonBrushFloat(colbrushf_t *brush)
345 float edge0[3], edge1[3], edge2[3], normal[3], dist, bestdist;
348 // FIXME: these probably don't actually need to be normalized if the collision code does not care
349 if (brush->numpoints == 3)
351 // optimized triangle case
352 TriangleNormal(brush->points[0].v, brush->points[1].v, brush->points[2].v, brush->planes[0].normal);
353 if (DotProduct(brush->planes[0].normal, brush->planes[0].normal) < 0.0001f)
355 // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
356 brush->numplanes = 0;
361 brush->numplanes = 5;
362 VectorNormalize(brush->planes[0].normal);
363 brush->planes[0].dist = DotProduct(brush->points->v, brush->planes[0].normal);
364 VectorNegate(brush->planes[0].normal, brush->planes[1].normal);
365 brush->planes[1].dist = -brush->planes[0].dist;
366 VectorSubtract(brush->points[2].v, brush->points[0].v, edge0);
367 VectorSubtract(brush->points[0].v, brush->points[1].v, edge1);
368 VectorSubtract(brush->points[1].v, brush->points[2].v, edge2);
371 float projectionnormal[3], projectionedge0[3], projectionedge1[3], projectionedge2[3];
373 float dist, bestdist;
374 bestdist = fabs(brush->planes[0].normal[0]);
376 for (i = 1;i < 3;i++)
378 dist = fabs(brush->planes[0].normal[i]);
385 VectorClear(projectionnormal);
386 if (brush->planes[0].normal[best] < 0)
387 projectionnormal[best] = -1;
389 projectionnormal[best] = 1;
390 VectorCopy(edge0, projectionedge0);
391 VectorCopy(edge1, projectionedge1);
392 VectorCopy(edge2, projectionedge2);
393 projectionedge0[best] = 0;
394 projectionedge1[best] = 0;
395 projectionedge2[best] = 0;
396 CrossProduct(projectionedge0, projectionnormal, brush->planes[2].normal);
397 CrossProduct(projectionedge1, projectionnormal, brush->planes[3].normal);
398 CrossProduct(projectionedge2, projectionnormal, brush->planes[4].normal);
401 CrossProduct(edge0, brush->planes->normal, brush->planes[2].normal);
402 CrossProduct(edge1, brush->planes->normal, brush->planes[3].normal);
403 CrossProduct(edge2, brush->planes->normal, brush->planes[4].normal);
405 VectorNormalize(brush->planes[2].normal);
406 VectorNormalize(brush->planes[3].normal);
407 VectorNormalize(brush->planes[4].normal);
408 brush->planes[2].dist = DotProduct(brush->points[2].v, brush->planes[2].normal);
409 brush->planes[3].dist = DotProduct(brush->points[0].v, brush->planes[3].normal);
410 brush->planes[4].dist = DotProduct(brush->points[1].v, brush->planes[4].normal);
412 if (developer.integer >= 100)
418 VectorSubtract(brush->points[0].v, brush->points[1].v, edge0);
419 VectorSubtract(brush->points[2].v, brush->points[1].v, edge1);
420 CrossProduct(edge0, edge1, normal);
421 VectorNormalize(normal);
422 VectorSubtract(normal, brush->planes[0].normal, temp);
423 if (VectorLength(temp) > 0.01f)
424 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]);
425 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)
426 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);
428 if (fabs(DotProduct(brush->planes[2].normal, brush->planes[0].normal)) > 0.01f)
429 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);
430 if (fabs(DotProduct(brush->planes[3].normal, brush->planes[0].normal)) > 0.01f)
431 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);
432 if (fabs(DotProduct(brush->planes[4].normal, brush->planes[0].normal)) > 0.01f)
433 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);
434 if (fabs(DotProduct(brush->planes[2].normal, edge0)) > 0.01f)
435 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]);
436 if (fabs(DotProduct(brush->planes[3].normal, edge1)) > 0.01f)
437 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]);
438 if (fabs(DotProduct(brush->planes[4].normal, edge2)) > 0.01f)
439 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]);
442 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)
443 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);
444 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)
445 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);
446 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)
447 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);
448 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)
449 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);
450 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)
451 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);
457 // choose best surface normal for polygon's plane
459 for (i = 0, p = brush->points + 1;i < brush->numpoints - 2;i++, p++)
461 VectorSubtract(p[-1].v, p[0].v, edge0);
462 VectorSubtract(p[1].v, p[0].v, edge1);
463 CrossProduct(edge0, edge1, normal);
464 //TriangleNormal(p[-1].v, p[0].v, p[1].v, normal);
465 dist = DotProduct(normal, normal);
466 if (i == 0 || bestdist < dist)
469 VectorCopy(normal, brush->planes->normal);
472 if (bestdist < 0.0001f)
474 // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
475 brush->numplanes = 0;
480 brush->numplanes = brush->numpoints + 2;
481 VectorNormalize(brush->planes->normal);
482 brush->planes->dist = DotProduct(brush->points->v, brush->planes->normal);
484 // negate plane to create other side
485 VectorNegate(brush->planes[0].normal, brush->planes[1].normal);
486 brush->planes[1].dist = -brush->planes[0].dist;
487 for (i = 0, p = brush->points + (brush->numpoints - 1), p2 = brush->points;i < brush->numpoints;i++, p = p2, p2++)
489 VectorSubtract(p->v, p2->v, edge0);
490 CrossProduct(edge0, brush->planes->normal, brush->planes[i + 2].normal);
491 VectorNormalize(brush->planes[i + 2].normal);
492 brush->planes[i + 2].dist = DotProduct(p->v, brush->planes[i + 2].normal);
497 if (developer.integer >= 100)
499 // validity check - will be disabled later
500 Collision_ValidateBrush(brush);
501 for (i = 0;i < brush->numplanes;i++)
504 for (j = 0, p = brush->points;j < brush->numpoints;j++, p++)
505 if (DotProduct(p->v, brush->planes[i].normal) > brush->planes[i].dist + COLLISION_PLANE_DIST_EPSILON)
506 Con_Printf("Error in brush plane generation, plane %i\n", i);
511 colbrushf_t *Collision_AllocBrushFromPermanentPolygonFloat(mempool_t *mempool, int numpoints, float *points, int supercontents)
514 brush = (colbrushf_t *)Mem_Alloc(mempool, sizeof(colbrushf_t) + sizeof(colplanef_t) * (numpoints + 2));
515 brush->supercontents = supercontents;
516 brush->numpoints = numpoints;
517 brush->numplanes = numpoints + 2;
518 brush->planes = (colplanef_t *)(brush + 1);
519 brush->points = (colpointf_t *)points;
520 Sys_Error("Collision_AllocBrushFromPermanentPolygonFloat: FIXME: this code needs to be updated to generate a mesh...");
524 // NOTE: start and end of each brush pair must have same numplanes/numpoints
525 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)
527 int nplane, nplane2, fstartsolid = true, fendsolid = true, brushsolid, hitq3surfaceflags = 0;
528 float enterfrac = -1, leavefrac = 1, d1, d2, f, imove, newimpactnormal[3], enterfrac2 = -1;
529 const colplanef_t *startplane, *endplane;
530 texture_t *hittexture = NULL;
532 VectorClear(newimpactnormal);
534 for (nplane = 0;nplane < thatbrush_start->numplanes + thisbrush_start->numplanes;nplane++)
537 if (nplane2 >= thatbrush_start->numplanes)
539 nplane2 -= thatbrush_start->numplanes;
540 startplane = thisbrush_start->planes + nplane2;
541 endplane = thisbrush_end->planes + nplane2;
542 if (developer.integer >= 100)
544 // any brush with degenerate planes is not worth handling
545 if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
547 Con_Print("Collision_TraceBrushBrushFloat: degenerate thisbrush plane!\n");
550 f = furthestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints);
551 if (fabs(f - startplane->dist) > COLLISION_PLANE_DIST_EPSILON)
552 Con_Printf("startplane->dist %f != calculated %f (thisbrush_start)\n", startplane->dist, f);
554 d1 = nearestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints) - furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints) - collision_startnudge.value;
555 d2 = nearestplanedist_float(endplane->normal, thisbrush_end->points, thisbrush_end->numpoints) - furthestplanedist_float(endplane->normal, thatbrush_end->points, thatbrush_end->numpoints) - collision_endnudge.value;
559 startplane = thatbrush_start->planes + nplane2;
560 endplane = thatbrush_end->planes + nplane2;
561 if (developer.integer >= 100)
563 // any brush with degenerate planes is not worth handling
564 if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
566 Con_Print("Collision_TraceBrushBrushFloat: degenerate thatbrush plane!\n");
569 f = furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints);
570 if (fabs(f - startplane->dist) > COLLISION_PLANE_DIST_EPSILON)
571 Con_Printf("startplane->dist %f != calculated %f (thatbrush_start)\n", startplane->dist, f);
573 d1 = nearestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints) - startplane->dist - collision_startnudge.value;
574 d2 = nearestplanedist_float(endplane->normal, thisbrush_end->points, thisbrush_end->numpoints) - endplane->dist - collision_endnudge.value;
576 //Con_Printf("%c%i: d1 = %f, d2 = %f, d1 / (d1 - d2) = %f\n", nplane2 != nplane ? 'b' : 'a', nplane2, d1, d2, d1 / (d1 - d2));
587 imove = 1 / (d1 - d2);
588 f = (d1 - collision_enternudge.value) * imove;
592 enterfrac2 = f - collision_impactnudge.value * imove;
593 VectorLerp(startplane->normal, enterfrac, endplane->normal, newimpactnormal);
594 hitq3surfaceflags = startplane->q3surfaceflags;
595 hittexture = startplane->texture;
601 // moving out of brush
608 f = (d1 + collision_leavenudge.value) / (d1 - d2);
615 brushsolid = trace->hitsupercontentsmask & thatbrush_start->supercontents;
618 trace->startsupercontents |= thatbrush_start->supercontents;
621 trace->startsolid = true;
623 trace->allsolid = true;
627 // LordHavoc: we need an epsilon nudge here because for a point trace the
628 // penetrating line segment is normally zero length if this brush was
629 // generated from a polygon (infinitely thin), and could even be slightly
630 // positive or negative due to rounding errors in that case.
631 if (brushsolid && enterfrac > -1 && enterfrac < trace->realfraction && enterfrac - (1.0f / 1024.0f) <= leavefrac)
635 if (thatbrush_start->ispolygon)
637 d1 = nearestplanedist_float(thatbrush_start->planes[0].normal, thisbrush_start->points, thisbrush_start->numpoints) - thatbrush_start->planes[0].dist - collision_startnudge.value;
638 d2 = nearestplanedist_float(thatbrush_end->planes[0].normal, thisbrush_end->points, thisbrush_end->numpoints) - thatbrush_end->planes[0].dist - collision_endnudge.value;
640 if (move <= 0 || d2 > collision_enternudge.value || d1 < 0)
644 enterfrac = (d1 - collision_enternudge.value) * imove;
645 if (enterfrac < trace->realfraction)
647 enterfrac2 = enterfrac - collision_impactnudge.value * imove;
648 trace->hitsupercontents = thatbrush_start->supercontents;
649 trace->hitq3surfaceflags = thatbrush_start->planes[0].q3surfaceflags;
650 trace->hittexture = thatbrush_start->planes[0].texture;
651 trace->realfraction = bound(0, enterfrac, 1);
652 trace->fraction = bound(0, enterfrac2, 1);
653 VectorLerp(thatbrush_start->planes[0].normal, enterfrac, thatbrush_end->planes[0].normal, trace->plane.normal);
659 trace->hitsupercontents = thatbrush_start->supercontents;
660 trace->hitq3surfaceflags = hitq3surfaceflags;
661 trace->hittexture = hittexture;
662 trace->realfraction = bound(0, enterfrac, 1);
663 trace->fraction = bound(0, enterfrac2, 1);
664 VectorCopy(newimpactnormal, trace->plane.normal);
669 // NOTE: start and end brush pair must have same numplanes/numpoints
670 void Collision_TraceLineBrushFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, const colbrushf_t *thatbrush_start, const colbrushf_t *thatbrush_end)
672 int nplane, fstartsolid = true, fendsolid = true, brushsolid, hitq3surfaceflags = 0;
673 float enterfrac = -1, leavefrac = 1, d1, d2, f, imove, newimpactnormal[3], enterfrac2 = -1;
674 const colplanef_t *startplane, *endplane;
675 texture_t *hittexture = NULL;
677 VectorClear(newimpactnormal);
679 for (nplane = 0;nplane < thatbrush_start->numplanes;nplane++)
681 startplane = thatbrush_start->planes + nplane;
682 endplane = thatbrush_end->planes + nplane;
683 d1 = DotProduct(startplane->normal, linestart) - startplane->dist - collision_startnudge.value;
684 d2 = DotProduct(endplane->normal, lineend) - endplane->dist - collision_endnudge.value;
685 if (developer.integer >= 100)
687 // any brush with degenerate planes is not worth handling
688 if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
690 Con_Print("Collision_TraceLineBrushFloat: degenerate plane!\n");
693 if (thatbrush_start->numpoints)
695 f = furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints);
696 if (fabs(f - startplane->dist) > COLLISION_PLANE_DIST_EPSILON)
697 Con_Printf("startplane->dist %f != calculated %f\n", startplane->dist, f);
710 imove = 1 / (d1 - d2);
711 f = (d1 - collision_enternudge.value) * imove;
715 enterfrac2 = f - collision_impactnudge.value * imove;
716 VectorLerp(startplane->normal, enterfrac, endplane->normal, newimpactnormal);
717 hitq3surfaceflags = startplane->q3surfaceflags;
718 hittexture = startplane->texture;
724 // moving out of brush
731 f = (d1 + collision_leavenudge.value) / (d1 - d2);
738 brushsolid = trace->hitsupercontentsmask & thatbrush_start->supercontents;
741 trace->startsupercontents |= thatbrush_start->supercontents;
744 trace->startsolid = true;
746 trace->allsolid = true;
750 // LordHavoc: we need an epsilon nudge here because for a point trace the
751 // penetrating line segment is normally zero length if this brush was
752 // generated from a polygon (infinitely thin), and could even be slightly
753 // positive or negative due to rounding errors in that case.
754 if (brushsolid && enterfrac > -1 && enterfrac < trace->realfraction && enterfrac <= leavefrac)
758 if (thatbrush_start->ispolygon)
760 d1 = DotProduct(thatbrush_start->planes[0].normal, linestart) - thatbrush_start->planes[0].dist - collision_startnudge.value;
761 d2 = DotProduct(thatbrush_end->planes[0].normal, lineend) - thatbrush_end->planes[0].dist - collision_endnudge.value;
763 if (move <= 0 || d2 > collision_enternudge.value || d1 < 0)
767 enterfrac = (d1 - collision_enternudge.value) * imove;
768 if (enterfrac < trace->realfraction)
770 enterfrac2 = enterfrac - collision_impactnudge.value * imove;
771 trace->hitsupercontents = thatbrush_start->supercontents;
772 trace->hitq3surfaceflags = hitq3surfaceflags;
773 trace->hittexture = hittexture;
774 trace->realfraction = bound(0, enterfrac, 1);
775 trace->fraction = bound(0, enterfrac2, 1);
776 VectorLerp(thatbrush_start->planes[0].normal, enterfrac, thatbrush_end->planes[0].normal, trace->plane.normal);
782 trace->hitsupercontents = thatbrush_start->supercontents;
783 trace->hitq3surfaceflags = hitq3surfaceflags;
784 trace->hittexture = hittexture;
785 trace->realfraction = bound(0, enterfrac, 1);
786 trace->fraction = bound(0, enterfrac2, 1);
787 VectorCopy(newimpactnormal, trace->plane.normal);
792 void Collision_TracePointBrushFloat(trace_t *trace, const vec3_t point, const colbrushf_t *thatbrush)
795 const colplanef_t *plane;
797 for (nplane = 0, plane = thatbrush->planes;nplane < thatbrush->numplanes;nplane++, plane++)
798 if (DotProduct(plane->normal, point) > plane->dist)
801 trace->startsupercontents |= thatbrush->supercontents;
802 if (trace->hitsupercontentsmask & thatbrush->supercontents)
804 trace->startsolid = true;
805 trace->allsolid = true;
809 static colpointf_t polyf_points[256];
810 static colplanef_t polyf_planes[256 + 2];
811 static colbrushf_t polyf_brush;
813 void Collision_SnapCopyPoints(int numpoints, const colpointf_t *in, colpointf_t *out, float fractionprecision, float invfractionprecision)
817 out->v[0] = floor(in->v[0] * fractionprecision + 0.5f) * invfractionprecision;
818 out->v[1] = floor(in->v[1] * fractionprecision + 0.5f) * invfractionprecision;
819 out->v[2] = floor(in->v[2] * fractionprecision + 0.5f) * invfractionprecision;
823 void Collision_TraceBrushPolygonFloat(trace_t *trace, const colbrushf_t *thisbrush_start, const colbrushf_t *thisbrush_end, int numpoints, const float *points, int supercontents)
827 Con_Print("Polygon with more than 256 points not supported yet (fixme!)\n");
830 polyf_brush.numpoints = numpoints;
831 polyf_brush.numplanes = numpoints + 2;
832 //polyf_brush.points = (colpointf_t *)points;
833 polyf_brush.planes = polyf_planes;
834 polyf_brush.supercontents = supercontents;
835 polyf_brush.points = polyf_points;
836 Collision_SnapCopyPoints(numpoints, (colpointf_t *)points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
837 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
838 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
839 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brush, &polyf_brush);
842 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)
845 float facemins[3], facemaxs[3];
846 polyf_brush.numpoints = 3;
847 polyf_brush.numplanes = 5;
848 polyf_brush.points = polyf_points;
849 polyf_brush.planes = polyf_planes;
850 polyf_brush.supercontents = supercontents;
851 for (i = 0;i < polyf_brush.numplanes;i++)
853 polyf_brush.planes[i].q3surfaceflags = q3surfaceflags;
854 polyf_brush.planes[i].texture = texture;
856 for (i = 0;i < numtriangles;i++, element3i += 3)
858 VectorCopy(vertex3f + element3i[0] * 3, polyf_points[0].v);
859 VectorCopy(vertex3f + element3i[1] * 3, polyf_points[1].v);
860 VectorCopy(vertex3f + element3i[2] * 3, polyf_points[2].v);
861 Collision_SnapCopyPoints(3, polyf_points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
862 facemins[0] = min(polyf_points[0].v[0], min(polyf_points[1].v[0], polyf_points[2].v[0]));
863 facemins[1] = min(polyf_points[0].v[1], min(polyf_points[1].v[1], polyf_points[2].v[1]));
864 facemins[2] = min(polyf_points[0].v[2], min(polyf_points[1].v[2], polyf_points[2].v[2]));
865 facemaxs[0] = max(polyf_points[0].v[0], max(polyf_points[1].v[0], polyf_points[2].v[0]));
866 facemaxs[1] = max(polyf_points[0].v[1], max(polyf_points[1].v[1], polyf_points[2].v[1]));
867 facemaxs[2] = max(polyf_points[0].v[2], max(polyf_points[1].v[2], polyf_points[2].v[2]));
868 if (BoxesOverlap(segmentmins, segmentmaxs, facemins, facemaxs))
870 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
871 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
872 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brush, &polyf_brush);
877 void Collision_TraceLinePolygonFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, int numpoints, const float *points, int supercontents)
881 Con_Print("Polygon with more than 256 points not supported yet (fixme!)\n");
884 polyf_brush.numpoints = numpoints;
885 polyf_brush.numplanes = numpoints + 2;
886 //polyf_brush.points = (colpointf_t *)points;
887 polyf_brush.points = polyf_points;
888 Collision_SnapCopyPoints(numpoints, (colpointf_t *)points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
889 polyf_brush.planes = polyf_planes;
890 polyf_brush.supercontents = supercontents;
891 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
892 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
893 Collision_TraceLineBrushFloat(trace, linestart, lineend, &polyf_brush, &polyf_brush);
896 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)
900 // FIXME: snap vertices?
901 for (i = 0;i < numtriangles;i++, element3i += 3)
902 Collision_TraceLineTriangleFloat(trace, linestart, lineend, vertex3f + element3i[0] * 3, vertex3f + element3i[1] * 3, vertex3f + element3i[2] * 3, supercontents, q3surfaceflags, texture);
904 polyf_brush.numpoints = 3;
905 polyf_brush.numplanes = 5;
906 polyf_brush.points = polyf_points;
907 polyf_brush.planes = polyf_planes;
908 polyf_brush.supercontents = supercontents;
909 for (i = 0;i < polyf_brush.numplanes;i++)
911 polyf_brush.planes[i].supercontents = supercontents;
912 polyf_brush.planes[i].q3surfaceflags = q3surfaceflags;
913 polyf_brush.planes[i].texture = texture;
915 for (i = 0;i < numtriangles;i++, element3i += 3)
917 float facemins[3], facemaxs[3];
918 VectorCopy(vertex3f + element3i[0] * 3, polyf_points[0].v);
919 VectorCopy(vertex3f + element3i[1] * 3, polyf_points[1].v);
920 VectorCopy(vertex3f + element3i[2] * 3, polyf_points[2].v);
921 Collision_SnapCopyPoints(numpoints, polyf_points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
922 facemins[0] = min(polyf_points[0].v[0], min(polyf_points[1].v[0], polyf_points[2].v[0]));
923 facemins[1] = min(polyf_points[0].v[1], min(polyf_points[1].v[1], polyf_points[2].v[1]));
924 facemins[2] = min(polyf_points[0].v[2], min(polyf_points[1].v[2], polyf_points[2].v[2]));
925 facemaxs[0] = max(polyf_points[0].v[0], max(polyf_points[1].v[0], polyf_points[2].v[0]));
926 facemaxs[1] = max(polyf_points[0].v[1], max(polyf_points[1].v[1], polyf_points[2].v[1]));
927 facemaxs[2] = max(polyf_points[0].v[2], max(polyf_points[1].v[2], polyf_points[2].v[2]));
928 if (BoxesOverlap(segmentmins, segmentmaxs, facemins, facemaxs))
930 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
931 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
932 Collision_TraceLineBrushFloat(trace, linestart, lineend, &polyf_brush, &polyf_brush);
939 static colpointf_t polyf_pointsstart[256], polyf_pointsend[256];
940 static colplanef_t polyf_planesstart[256 + 2], polyf_planesend[256 + 2];
941 static colbrushf_t polyf_brushstart, polyf_brushend;
943 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)
948 Con_Print("Polygon with more than 256 points not supported yet (fixme!)\n");
951 polyf_brushstart.numpoints = numpoints;
952 polyf_brushstart.numplanes = numpoints + 2;
953 polyf_brushstart.points = polyf_pointsstart;//(colpointf_t *)points;
954 polyf_brushstart.planes = polyf_planesstart;
955 polyf_brushstart.supercontents = supercontents;
956 for (i = 0;i < numpoints;i++)
957 Matrix4x4_Transform(polygonmatrixstart, points + i * 3, polyf_brushstart.points[i].v);
958 polyf_brushend.numpoints = numpoints;
959 polyf_brushend.numplanes = numpoints + 2;
960 polyf_brushend.points = polyf_pointsend;//(colpointf_t *)points;
961 polyf_brushend.planes = polyf_planesend;
962 polyf_brushend.supercontents = supercontents;
963 for (i = 0;i < numpoints;i++)
964 Matrix4x4_Transform(polygonmatrixend, points + i * 3, polyf_brushend.points[i].v);
965 for (i = 0;i < polyf_brushstart.numplanes;i++)
967 polyf_brushstart.planes[i].q3surfaceflags = q3surfaceflags;
968 polyf_brushstart.planes[i].texture = texture;
970 Collision_SnapCopyPoints(numpoints, polyf_pointsstart, polyf_pointsstart, COLLISION_SNAPSCALE, COLLISION_SNAP);
971 Collision_SnapCopyPoints(numpoints, polyf_pointsend, polyf_pointsend, COLLISION_SNAPSCALE, COLLISION_SNAP);
972 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brushstart);
973 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brushend);
975 //Collision_PrintBrushAsQHull(&polyf_brushstart, "polyf_brushstart");
976 //Collision_PrintBrushAsQHull(&polyf_brushend, "polyf_brushend");
978 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brushstart, &polyf_brushend);
983 #define MAX_BRUSHFORBOX 16
984 static int brushforbox_index = 0;
985 static colpointf_t brushforbox_point[MAX_BRUSHFORBOX*8];
986 static colplanef_t brushforbox_plane[MAX_BRUSHFORBOX*6];
987 static colbrushf_t brushforbox_brush[MAX_BRUSHFORBOX];
988 static colbrushf_t brushforpoint_brush[MAX_BRUSHFORBOX];
990 void Collision_InitBrushForBox(void)
993 for (i = 0;i < MAX_BRUSHFORBOX;i++)
995 brushforbox_brush[i].numpoints = 8;
996 brushforbox_brush[i].numplanes = 6;
997 brushforbox_brush[i].points = brushforbox_point + i * 8;
998 brushforbox_brush[i].planes = brushforbox_plane + i * 6;
999 brushforpoint_brush[i].numpoints = 1;
1000 brushforpoint_brush[i].numplanes = 0;
1001 brushforpoint_brush[i].points = brushforbox_point + i * 8;
1002 brushforpoint_brush[i].planes = brushforbox_plane + i * 6;
1006 colbrushf_t *Collision_BrushForBox(const matrix4x4_t *matrix, const vec3_t mins, const vec3_t maxs, int supercontents, int q3surfaceflags, texture_t *texture)
1011 if (brushforbox_brush[0].numpoints == 0)
1012 Collision_InitBrushForBox();
1013 // FIXME: these probably don't actually need to be normalized if the collision code does not care
1014 if (VectorCompare(mins, maxs))
1017 brush = brushforpoint_brush + ((brushforbox_index++) % MAX_BRUSHFORBOX);
1018 VectorCopy(mins, brush->points->v);
1022 brush = brushforbox_brush + ((brushforbox_index++) % MAX_BRUSHFORBOX);
1024 for (i = 0;i < 8;i++)
1026 v[0] = i & 1 ? maxs[0] : mins[0];
1027 v[1] = i & 2 ? maxs[1] : mins[1];
1028 v[2] = i & 4 ? maxs[2] : mins[2];
1029 Matrix4x4_Transform(matrix, v, brush->points[i].v);
1032 for (i = 0;i < 6;i++)
1035 v[i >> 1] = i & 1 ? 1 : -1;
1036 Matrix4x4_Transform3x3(matrix, v, brush->planes[i].normal);
1037 VectorNormalize(brush->planes[i].normal);
1040 brush->supercontents = supercontents;
1041 for (j = 0;j < brush->numplanes;j++)
1043 brush->planes[j].q3surfaceflags = q3surfaceflags;
1044 brush->planes[j].texture = texture;
1045 brush->planes[j].dist = furthestplanedist_float(brush->planes[j].normal, brush->points, brush->numpoints);
1047 VectorCopy(brush->points[0].v, brush->mins);
1048 VectorCopy(brush->points[0].v, brush->maxs);
1049 for (j = 1;j < brush->numpoints;j++)
1051 brush->mins[0] = min(brush->mins[0], brush->points[j].v[0]);
1052 brush->mins[1] = min(brush->mins[1], brush->points[j].v[1]);
1053 brush->mins[2] = min(brush->mins[2], brush->points[j].v[2]);
1054 brush->maxs[0] = max(brush->maxs[0], brush->points[j].v[0]);
1055 brush->maxs[1] = max(brush->maxs[1], brush->points[j].v[1]);
1056 brush->maxs[2] = max(brush->maxs[2], brush->points[j].v[2]);
1058 brush->mins[0] -= 1;
1059 brush->mins[1] -= 1;
1060 brush->mins[2] -= 1;
1061 brush->maxs[0] += 1;
1062 brush->maxs[1] += 1;
1063 brush->maxs[2] += 1;
1064 Collision_ValidateBrush(brush);
1068 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)
1070 colbrushf_t *boxbrush, *thisbrush_start, *thisbrush_end;
1071 vec3_t startmins, startmaxs, endmins, endmaxs;
1073 // create brushes for the collision
1074 VectorAdd(start, mins, startmins);
1075 VectorAdd(start, maxs, startmaxs);
1076 VectorAdd(end, mins, endmins);
1077 VectorAdd(end, maxs, endmaxs);
1078 boxbrush = Collision_BrushForBox(&identitymatrix, cmins, cmaxs, supercontents, q3surfaceflags, texture);
1079 thisbrush_start = Collision_BrushForBox(&identitymatrix, startmins, startmaxs, 0, 0, NULL);
1080 thisbrush_end = Collision_BrushForBox(&identitymatrix, endmins, endmaxs, 0, 0, NULL);
1082 memset(trace, 0, sizeof(trace_t));
1083 trace->hitsupercontentsmask = hitsupercontentsmask;
1084 trace->fraction = 1;
1085 trace->realfraction = 1;
1086 trace->allsolid = true;
1087 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, boxbrush, boxbrush);
1090 //pseudocode for detecting line/sphere overlap without calculating an impact point
1091 //linesphereorigin = sphereorigin - linestart;linediff = lineend - linestart;linespherefrac = DotProduct(linesphereorigin, linediff) / DotProduct(linediff, linediff);return VectorLength2(linesphereorigin - bound(0, linespherefrac, 1) * linediff) >= sphereradius*sphereradius;
1093 // LordHavoc: currently unused, but tested
1094 // note: this can be used for tracing a moving sphere vs a stationary sphere,
1095 // by simply adding the moving sphere's radius to the sphereradius parameter,
1096 // all the results are correct (impactpoint, impactnormal, and fraction)
1097 float Collision_ClipTrace_Line_Sphere(double *linestart, double *lineend, double *sphereorigin, double sphereradius, double *impactpoint, double *impactnormal)
1099 double dir[3], scale, v[3], deviationdist, impactdist, linelength;
1100 // make sure the impactpoint and impactnormal are valid even if there is
1102 VectorCopy(lineend, impactpoint);
1103 VectorClear(impactnormal);
1104 // calculate line direction
1105 VectorSubtract(lineend, linestart, dir);
1106 // normalize direction
1107 linelength = VectorLength(dir);
1110 scale = 1.0 / linelength;
1111 VectorScale(dir, scale, dir);
1113 // this dotproduct calculates the distance along the line at which the
1114 // sphere origin is (nearest point to the sphere origin on the line)
1115 impactdist = DotProduct(sphereorigin, dir) - DotProduct(linestart, dir);
1116 // calculate point on line at that distance, and subtract the
1117 // sphereorigin from it, so we have a vector to measure for the distance
1118 // of the line from the sphereorigin (deviation, how off-center it is)
1119 VectorMA(linestart, impactdist, dir, v);
1120 VectorSubtract(v, sphereorigin, v);
1121 deviationdist = VectorLength2(v);
1122 // if outside the radius, it's a miss for sure
1123 // (we do this comparison using squared radius to avoid a sqrt)
1124 if (deviationdist > sphereradius*sphereradius)
1125 return 1; // miss (off to the side)
1126 // nudge back to find the correct impact distance
1127 impactdist += deviationdist - sphereradius;
1128 if (impactdist >= linelength)
1129 return 1; // miss (not close enough)
1131 return 1; // miss (linestart is past or inside sphere)
1132 // calculate new impactpoint
1133 VectorMA(linestart, impactdist, dir, impactpoint);
1134 // calculate impactnormal (surface normal at point of impact)
1135 VectorSubtract(impactpoint, sphereorigin, impactnormal);
1136 // normalize impactnormal
1137 VectorNormalize(impactnormal);
1138 // return fraction of movement distance
1139 return impactdist / linelength;
1142 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)
1146 float d1, d2, d, f, impact[3], edgenormal[3], faceplanenormal[3], faceplanedist, faceplanenormallength2, edge01[3], edge21[3], edge02[3];
1148 // this function executes:
1149 // 32 ops when line starts behind triangle
1150 // 38 ops when line ends infront of triangle
1151 // 43 ops when line fraction is already closer than this triangle
1152 // 72 ops when line is outside edge 01
1153 // 92 ops when line is outside edge 21
1154 // 115 ops when line is outside edge 02
1155 // 123 ops when line impacts triangle and updates trace results
1157 // this code is designed for clockwise triangles, conversion to
1158 // counterclockwise would require swapping some things around...
1159 // it is easier to simply swap the point0 and point2 parameters to this
1160 // function when calling it than it is to rewire the internals.
1162 // calculate the faceplanenormal of the triangle, this represents the front side
1164 VectorSubtract(point0, point1, edge01);
1165 VectorSubtract(point2, point1, edge21);
1166 CrossProduct(edge01, edge21, faceplanenormal);
1167 // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
1169 faceplanenormallength2 = DotProduct(faceplanenormal, faceplanenormal);
1170 if (faceplanenormallength2 < 0.0001f)
1172 // calculate the distance
1174 faceplanedist = DotProduct(point0, faceplanenormal);
1176 // if start point is on the back side there is no collision
1177 // (we don't care about traces going through the triangle the wrong way)
1179 // calculate the start distance
1181 d1 = DotProduct(faceplanenormal, linestart);
1182 if (d1 <= faceplanedist)
1185 // calculate the end distance
1187 d2 = DotProduct(faceplanenormal, lineend);
1188 // if both are in front, there is no collision
1189 if (d2 >= faceplanedist)
1192 // from here on we know d1 is >= 0 and d2 is < 0
1193 // this means the line starts infront and ends behind, passing through it
1195 // calculate the recipricol of the distance delta,
1196 // so we can use it multiple times cheaply (instead of division)
1198 d = 1.0f / (d1 - d2);
1199 // calculate the impact fraction by taking the start distance (> 0)
1200 // and subtracting the face plane distance (this is the distance of the
1201 // triangle along that same normal)
1202 // then multiply by the recipricol distance delta
1204 f = (d1 - faceplanedist) * d;
1205 // skip out if this impact is further away than previous ones
1207 if (f > trace->realfraction)
1209 // calculate the perfect impact point for classification of insidedness
1211 impact[0] = linestart[0] + f * (lineend[0] - linestart[0]);
1212 impact[1] = linestart[1] + f * (lineend[1] - linestart[1]);
1213 impact[2] = linestart[2] + f * (lineend[2] - linestart[2]);
1215 // calculate the edge normal and reject if impact is outside triangle
1216 // (an edge normal faces away from the triangle, to get the desired normal
1217 // a crossproduct with the faceplanenormal is used, and because of the way
1218 // the insidedness comparison is written it does not need to be normalized)
1220 // first use the two edges from the triangle plane math
1221 // the other edge only gets calculated if the point survives that long
1224 CrossProduct(edge01, faceplanenormal, edgenormal);
1225 if (DotProduct(impact, edgenormal) > DotProduct(point1, edgenormal))
1229 CrossProduct(faceplanenormal, edge21, edgenormal);
1230 if (DotProduct(impact, edgenormal) > DotProduct(point2, edgenormal))
1234 VectorSubtract(point0, point2, edge02);
1235 CrossProduct(faceplanenormal, edge02, edgenormal);
1236 if (DotProduct(impact, edgenormal) > DotProduct(point0, edgenormal))
1241 // store the new trace fraction
1242 trace->realfraction = f;
1244 // calculate a nudged fraction to keep it out of the surface
1245 // (the main fraction remains perfect)
1246 trace->fraction = f - collision_impactnudge.value * d;
1248 // store the new trace plane (because collisions only happen from
1249 // the front this is always simply the triangle normal, never flipped)
1250 d = 1.0 / sqrt(faceplanenormallength2);
1251 VectorScale(faceplanenormal, d, trace->plane.normal);
1252 trace->plane.dist = faceplanedist * d;
1254 trace->hitsupercontents = supercontents;
1255 trace->hitq3surfaceflags = q3surfaceflags;
1256 trace->hittexture = texture;
1258 float d1, d2, d, f, fnudged, impact[3], edgenormal[3], faceplanenormal[3], faceplanedist, edge[3];
1260 // this code is designed for clockwise triangles, conversion to
1261 // counterclockwise would require swapping some things around...
1262 // it is easier to simply swap the point0 and point2 parameters to this
1263 // function when calling it than it is to rewire the internals.
1265 // calculate the unnormalized faceplanenormal of the triangle,
1266 // this represents the front side
1267 TriangleNormal(point0, point1, point2, faceplanenormal);
1268 // there's no point in processing a degenerate triangle
1269 // (GIGO - Garbage In, Garbage Out)
1270 if (DotProduct(faceplanenormal, faceplanenormal) < 0.0001f)
1272 // calculate the unnormalized distance
1273 faceplanedist = DotProduct(point0, faceplanenormal);
1275 // calculate the unnormalized start distance
1276 d1 = DotProduct(faceplanenormal, linestart) - faceplanedist;
1277 // if start point is on the back side there is no collision
1278 // (we don't care about traces going through the triangle the wrong way)
1282 // calculate the unnormalized end distance
1283 d2 = DotProduct(faceplanenormal, lineend) - faceplanedist;
1284 // if both are in front, there is no collision
1288 // from here on we know d1 is >= 0 and d2 is < 0
1289 // this means the line starts infront and ends behind, passing through it
1291 // calculate the recipricol of the distance delta,
1292 // so we can use it multiple times cheaply (instead of division)
1293 d = 1.0f / (d1 - d2);
1294 // calculate the impact fraction by taking the start distance (> 0)
1295 // and subtracting the face plane distance (this is the distance of the
1296 // triangle along that same normal)
1297 // then multiply by the recipricol distance delta
1299 // skip out if this impact is further away than previous ones
1300 if (f > trace->realfraction)
1302 // calculate the perfect impact point for classification of insidedness
1303 impact[0] = linestart[0] + f * (lineend[0] - linestart[0]);
1304 impact[1] = linestart[1] + f * (lineend[1] - linestart[1]);
1305 impact[2] = linestart[2] + f * (lineend[2] - linestart[2]);
1307 // calculate the edge normal and reject if impact is outside triangle
1308 // (an edge normal faces away from the triangle, to get the desired normal
1309 // a crossproduct with the faceplanenormal is used, and because of the way
1310 // the insidedness comparison is written it does not need to be normalized)
1312 VectorSubtract(point2, point0, edge);
1313 CrossProduct(edge, faceplanenormal, edgenormal);
1314 if (DotProduct(impact, edgenormal) > DotProduct(point0, edgenormal))
1317 VectorSubtract(point0, point1, edge);
1318 CrossProduct(edge, faceplanenormal, edgenormal);
1319 if (DotProduct(impact, edgenormal) > DotProduct(point1, edgenormal))
1322 VectorSubtract(point1, point2, edge);
1323 CrossProduct(edge, faceplanenormal, edgenormal);
1324 if (DotProduct(impact, edgenormal) > DotProduct(point2, edgenormal))
1327 // store the new trace fraction
1328 trace->realfraction = bound(0, f, 1);
1330 // store the new trace plane (because collisions only happen from
1331 // the front this is always simply the triangle normal, never flipped)
1332 VectorNormalize(faceplanenormal);
1333 VectorCopy(faceplanenormal, trace->plane.normal);
1334 trace->plane.dist = DotProduct(point0, faceplanenormal);
1336 // calculate the normalized start and end distances
1337 d1 = DotProduct(trace->plane.normal, linestart) - trace->plane.dist;
1338 d2 = DotProduct(trace->plane.normal, lineend) - trace->plane.dist;
1340 // calculate a nudged fraction to keep it out of the surface
1341 // (the main fraction remains perfect)
1342 fnudged = (d1 - collision_impactnudge.value) / (d1 - d2);
1343 trace->fraction = bound(0, fnudged, 1);
1345 // store the new trace endpos
1346 // not needed, it's calculated later when the trace is finished
1347 //trace->endpos[0] = linestart[0] + fnudged * (lineend[0] - linestart[0]);
1348 //trace->endpos[1] = linestart[1] + fnudged * (lineend[1] - linestart[1]);
1349 //trace->endpos[2] = linestart[2] + fnudged * (lineend[2] - linestart[2]);
1350 trace->hitsupercontents = supercontents;
1351 trace->hitq3surfaceflags = q3surfaceflags;
1352 trace->hittexture = texture;
1356 typedef struct colbspnode_s
1359 struct colbspnode_s *children[2];
1360 // the node is reallocated or split if max is reached
1363 colbrushf_t **colbrushflist;
1366 //colbrushd_t **colbrushdlist;
1370 typedef struct colbsp_s
1373 colbspnode_t *nodes;
1377 colbsp_t *Collision_CreateCollisionBSP(mempool_t *mempool)
1380 bsp = (colbsp_t *)Mem_Alloc(mempool, sizeof(colbsp_t));
1381 bsp->mempool = mempool;
1382 bsp->nodes = (colbspnode_t *)Mem_Alloc(bsp->mempool, sizeof(colbspnode_t));
1386 void Collision_FreeCollisionBSPNode(colbspnode_t *node)
1388 if (node->children[0])
1389 Collision_FreeCollisionBSPNode(node->children[0]);
1390 if (node->children[1])
1391 Collision_FreeCollisionBSPNode(node->children[1]);
1392 while (--node->numcolbrushf)
1393 Mem_Free(node->colbrushflist[node->numcolbrushf]);
1394 //while (--node->numcolbrushd)
1395 // Mem_Free(node->colbrushdlist[node->numcolbrushd]);
1399 void Collision_FreeCollisionBSP(colbsp_t *bsp)
1401 Collision_FreeCollisionBSPNode(bsp->nodes);
1405 void Collision_BoundingBoxOfBrushTraceSegment(const colbrushf_t *start, const colbrushf_t *end, vec3_t mins, vec3_t maxs, float startfrac, float endfrac)
1408 colpointf_t *ps, *pe;
1409 float tempstart[3], tempend[3];
1410 VectorLerp(start->points[0].v, startfrac, end->points[0].v, mins);
1411 VectorCopy(mins, maxs);
1412 for (i = 0, ps = start->points, pe = end->points;i < start->numpoints;i++, ps++, pe++)
1414 VectorLerp(ps->v, startfrac, pe->v, tempstart);
1415 VectorLerp(ps->v, endfrac, pe->v, tempend);
1416 mins[0] = min(mins[0], min(tempstart[0], tempend[0]));
1417 mins[1] = min(mins[1], min(tempstart[1], tempend[1]));
1418 mins[2] = min(mins[2], min(tempstart[2], tempend[2]));
1419 maxs[0] = min(maxs[0], min(tempstart[0], tempend[0]));
1420 maxs[1] = min(maxs[1], min(tempstart[1], tempend[1]));
1421 maxs[2] = min(maxs[2], min(tempstart[2], tempend[2]));