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"};
15 cvar_t collision_prefernudgedfraction = {0, "collision_prefernudgedfraction", "1", "whether to sort collision events by nudged fraction (1) or real fraction (0)"};
17 void Collision_Init (void)
19 Cvar_RegisterVariable(&collision_impactnudge);
20 Cvar_RegisterVariable(&collision_startnudge);
21 Cvar_RegisterVariable(&collision_endnudge);
22 Cvar_RegisterVariable(&collision_enternudge);
23 Cvar_RegisterVariable(&collision_leavenudge);
24 Cvar_RegisterVariable(&collision_prefernudgedfraction);
40 void Collision_PrintBrushAsQHull(colbrushf_t *brush, const char *name)
43 Con_Printf("3 %s\n%i\n", name, brush->numpoints);
44 for (i = 0;i < brush->numpoints;i++)
45 Con_Printf("%f %f %f\n", brush->points[i].v[0], brush->points[i].v[1], brush->points[i].v[2]);
47 Con_Printf("4\n%i\n", brush->numplanes);
48 for (i = 0;i < brush->numplanes;i++)
49 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);
52 void Collision_ValidateBrush(colbrushf_t *brush)
54 int j, k, pointsoffplanes, pointonplanes, pointswithinsufficientplanes, printbrush;
57 if (!brush->numpoints)
59 Con_Print("Collision_ValidateBrush: brush with no points!\n");
63 // it's ok for a brush to have one point and no planes...
64 if (brush->numplanes == 0 && brush->numpoints != 1)
66 Con_Print("Collision_ValidateBrush: brush with no planes and more than one point!\n");
73 pointswithinsufficientplanes = 0;
74 for (k = 0;k < brush->numplanes;k++)
75 if (DotProduct(brush->planes[k].normal, brush->planes[k].normal) < 0.0001f)
76 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);
77 for (j = 0;j < brush->numpoints;j++)
80 for (k = 0;k < brush->numplanes;k++)
82 d = DotProduct(brush->points[j].v, brush->planes[k].normal) - brush->planes[k].dist;
83 if (d > COLLISION_PLANE_DIST_EPSILON)
85 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);
88 if (fabs(d) > COLLISION_PLANE_DIST_EPSILON)
93 if (pointonplanes < 3)
94 pointswithinsufficientplanes++;
96 if (pointswithinsufficientplanes)
98 Con_Print("Collision_ValidateBrush: some points have insufficient planes, every point must be on at least 3 planes to form a corner.\n");
101 if (pointsoffplanes == 0) // all points are on all planes
103 Con_Print("Collision_ValidateBrush: all points lie on all planes (degenerate, no brush volume!)\n");
108 Collision_PrintBrushAsQHull(brush, "unnamed");
111 float nearestplanedist_float(const float *normal, const colpointf_t *points, int numpoints)
113 float dist, bestdist;
116 bestdist = DotProduct(points->v, normal);
120 dist = DotProduct(points->v, normal);
121 bestdist = min(bestdist, dist);
127 float furthestplanedist_float(const float *normal, const colpointf_t *points, int numpoints)
129 float dist, bestdist;
132 bestdist = DotProduct(points->v, normal);
136 dist = DotProduct(points->v, normal);
137 bestdist = max(bestdist, dist);
144 colbrushf_t *Collision_NewBrushFromPlanes(mempool_t *mempool, int numoriginalplanes, const colplanef_t *originalplanes, int supercontents)
146 // TODO: planesbuf could be replaced by a remapping table
147 int j, k, m, w, xyzflags;
148 int numpointsbuf = 0, maxpointsbuf = 256, numplanesbuf = 0, maxplanesbuf = 256, numelementsbuf = 0, maxelementsbuf = 256;
151 colpointf_t pointsbuf[256];
152 colplanef_t planesbuf[256];
153 int elementsbuf[1024];
154 int polypointbuf[256];
159 // enable these if debugging to avoid seeing garbage in unused data
160 memset(pointsbuf, 0, sizeof(pointsbuf));
161 memset(planesbuf, 0, sizeof(planesbuf));
162 memset(elementsbuf, 0, sizeof(elementsbuf));
163 memset(polypointbuf, 0, sizeof(polypointbuf));
164 memset(p, 0, sizeof(p));
166 // figure out how large a bounding box we need to properly compute this brush
168 for (j = 0;j < numoriginalplanes;j++)
169 maxdist = max(maxdist, fabs(originalplanes[j].dist));
170 // now make it large enough to enclose the entire brush, and round it off to a reasonable multiple of 1024
171 maxdist = floor(maxdist * (4.0 / 1024.0) + 2) * 1024.0;
172 // construct a collision brush (points, planes, and renderable mesh) from
173 // a set of planes, this also optimizes out any unnecessary planes (ones
174 // whose polygon is clipped away by the other planes)
175 for (j = 0;j < numoriginalplanes;j++)
177 // add the plane uniquely (no duplicates)
178 for (k = 0;k < numplanesbuf;k++)
179 if (VectorCompare(planesbuf[k].normal, originalplanes[j].normal) && planesbuf[k].dist == originalplanes[j].dist)
181 // if the plane is a duplicate, skip it
182 if (k < numplanesbuf)
184 // check if there are too many and skip the brush
185 if (numplanesbuf >= maxplanesbuf)
187 Con_DPrint("Collision_NewBrushFromPlanes: failed to build collision brush: too many planes for buffer\n");
192 VectorCopy(originalplanes[j].normal, planesbuf[numplanesbuf].normal);
193 planesbuf[numplanesbuf].dist = originalplanes[j].dist;
194 planesbuf[numplanesbuf].q3surfaceflags = originalplanes[j].q3surfaceflags;
195 planesbuf[numplanesbuf].texture = originalplanes[j].texture;
198 // create a large polygon from the plane
200 PolygonD_QuadForPlane(p[w], originalplanes[j].normal[0], originalplanes[j].normal[1], originalplanes[j].normal[2], originalplanes[j].dist, maxdist);
202 // clip it by all other planes
203 for (k = 0;k < numoriginalplanes && pnumpoints >= 3 && pnumpoints <= pmaxpoints;k++)
205 // skip the plane this polygon
206 // (nothing happens if it is processed, this is just an optimization)
209 // we want to keep the inside of the brush plane so we flip
211 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);
216 // if nothing is left, skip it
219 //Con_DPrintf("Collision_NewBrushFromPlanes: warning: polygon for plane %f %f %f %f clipped away\n", originalplanes[j].normal[0], originalplanes[j].normal[1], originalplanes[j].normal[2], originalplanes[j].dist);
223 for (k = 0;k < pnumpoints;k++)
227 for (l = 0;l < numoriginalplanes;l++)
228 if (fabs(DotProduct(&p[w][k*3], originalplanes[l].normal) - originalplanes[l].dist) < COLLISION_PLANE_DIST_EPSILON)
235 Con_DPrintf("Collision_NewBrushFromPlanes: warning: polygon point does not lie on at least 3 planes\n");
239 // check if there are too many polygon vertices for buffer
240 if (pnumpoints > pmaxpoints)
242 Con_DPrint("Collision_NewBrushFromPlanes: failed to build collision brush: too many points for buffer\n");
246 // check if there are too many triangle elements for buffer
247 if (numelementsbuf + (pnumpoints - 2) * 3 > maxelementsbuf)
249 Con_DPrint("Collision_NewBrushFromPlanes: failed to build collision brush: too many triangle elements for buffer\n");
253 for (k = 0;k < pnumpoints;k++)
256 // downgrade to float precision before comparing
257 VectorCopy(&p[w][k*3], v);
259 // check if there is already a matching point (no duplicates)
260 for (m = 0;m < numpointsbuf;m++)
261 if (VectorDistance2(v, pointsbuf[m].v) < COLLISION_SNAP2)
264 // if there is no match, add a new one
265 if (m == numpointsbuf)
267 // check if there are too many and skip the brush
268 if (numpointsbuf >= maxpointsbuf)
270 Con_DPrint("Collision_NewBrushFromPlanes: failed to build collision brush: too many points for buffer\n");
274 VectorCopy(&p[w][k*3], pointsbuf[numpointsbuf].v);
278 // store the index into a buffer
282 // add the triangles for the polygon
283 // (this particular code makes a triangle fan)
284 for (k = 0;k < pnumpoints - 2;k++)
286 elementsbuf[numelementsbuf++] = polypointbuf[0];
287 elementsbuf[numelementsbuf++] = polypointbuf[k + 1];
288 elementsbuf[numelementsbuf++] = polypointbuf[k + 2];
292 // if nothing is left, there's nothing to allocate
293 if (numplanesbuf < 4)
295 Con_DPrintf("Collision_NewBrushFromPlanes: failed to build collision brush: %i triangles, %i planes (input was %i planes), %i vertices\n", numelementsbuf / 3, numplanesbuf, numoriginalplanes, numpointsbuf);
299 // if no triangles or points could be constructed, then this routine failed but the brush is not discarded
300 if (numelementsbuf < 12 || numpointsbuf < 4)
301 Con_DPrintf("Collision_NewBrushFromPlanes: unable to rebuild triangles/points for collision brush: %i triangles, %i planes (input was %i planes), %i vertices\n", numelementsbuf / 3, numplanesbuf, numoriginalplanes, numpointsbuf);
303 // validate plane distances
304 for (j = 0;j < numplanesbuf;j++)
306 float d = furthestplanedist_float(planesbuf[j].normal, pointsbuf, numpointsbuf);
307 if (fabs(planesbuf[j].dist - d) > COLLISION_PLANE_DIST_EPSILON)
308 Con_DPrintf("plane %f %f %f %f mismatches dist %f\n", planesbuf[j].normal[0], planesbuf[j].normal[1], planesbuf[j].normal[2], planesbuf[j].dist, d);
311 // allocate the brush and copy to it
312 brush = (colbrushf_t *)Mem_Alloc(mempool, sizeof(colbrushf_t) + sizeof(colpointf_t) * numpointsbuf + sizeof(colplanef_t) * numplanesbuf + sizeof(int) * numelementsbuf);
313 brush->supercontents = supercontents;
314 brush->numplanes = numplanesbuf;
315 brush->numpoints = numpointsbuf;
316 brush->numtriangles = numelementsbuf / 3;
317 brush->planes = (colplanef_t *)(brush + 1);
318 brush->points = (colpointf_t *)(brush->planes + brush->numplanes);
319 brush->elements = (int *)(brush->points + brush->numpoints);
320 for (j = 0;j < brush->numpoints;j++)
322 brush->points[j].v[0] = pointsbuf[j].v[0];
323 brush->points[j].v[1] = pointsbuf[j].v[1];
324 brush->points[j].v[2] = pointsbuf[j].v[2];
326 for (j = 0;j < brush->numplanes;j++)
328 brush->planes[j].normal[0] = planesbuf[j].normal[0];
329 brush->planes[j].normal[1] = planesbuf[j].normal[1];
330 brush->planes[j].normal[2] = planesbuf[j].normal[2];
331 brush->planes[j].dist = planesbuf[j].dist;
332 brush->planes[j].q3surfaceflags = planesbuf[j].q3surfaceflags;
333 brush->planes[j].texture = planesbuf[j].texture;
335 for (j = 0;j < brush->numtriangles * 3;j++)
336 brush->elements[j] = elementsbuf[j];
339 VectorClear(brush->mins);
340 VectorClear(brush->maxs);
341 for (j = 0;j < min(6, numoriginalplanes);j++)
343 if (originalplanes[j].normal[0] == 1) {xyzflags |= 1;brush->maxs[0] = originalplanes[j].dist;}
344 else if (originalplanes[j].normal[0] == -1) {xyzflags |= 2;brush->mins[0] = -originalplanes[j].dist;}
345 else if (originalplanes[j].normal[1] == 1) {xyzflags |= 4;brush->maxs[1] = originalplanes[j].dist;}
346 else if (originalplanes[j].normal[1] == -1) {xyzflags |= 8;brush->mins[1] = -originalplanes[j].dist;}
347 else if (originalplanes[j].normal[2] == 1) {xyzflags |= 16;brush->maxs[2] = originalplanes[j].dist;}
348 else if (originalplanes[j].normal[2] == -1) {xyzflags |= 32;brush->mins[2] = -originalplanes[j].dist;}
350 // if not all xyzflags were set, then this is not a brush from q3map/q3map2, and needs reconstruction of the bounding box
351 // (this case works for any brush with valid points, but sometimes brushes are not reconstructed properly and hence the points are not valid, so this is reserved as a fallback case)
354 VectorCopy(brush->points[0].v, brush->mins);
355 VectorCopy(brush->points[0].v, brush->maxs);
356 for (j = 1;j < brush->numpoints;j++)
358 brush->mins[0] = min(brush->mins[0], brush->points[j].v[0]);
359 brush->mins[1] = min(brush->mins[1], brush->points[j].v[1]);
360 brush->mins[2] = min(brush->mins[2], brush->points[j].v[2]);
361 brush->maxs[0] = max(brush->maxs[0], brush->points[j].v[0]);
362 brush->maxs[1] = max(brush->maxs[1], brush->points[j].v[1]);
363 brush->maxs[2] = max(brush->maxs[2], brush->points[j].v[2]);
372 Collision_ValidateBrush(brush);
378 void Collision_CalcPlanesForPolygonBrushFloat(colbrushf_t *brush)
381 float edge0[3], edge1[3], edge2[3], normal[3], dist, bestdist;
384 // FIXME: these probably don't actually need to be normalized if the collision code does not care
385 if (brush->numpoints == 3)
387 // optimized triangle case
388 TriangleNormal(brush->points[0].v, brush->points[1].v, brush->points[2].v, brush->planes[0].normal);
389 if (DotProduct(brush->planes[0].normal, brush->planes[0].normal) < 0.0001f)
391 // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
392 brush->numplanes = 0;
397 brush->numplanes = 5;
398 VectorNormalize(brush->planes[0].normal);
399 brush->planes[0].dist = DotProduct(brush->points->v, brush->planes[0].normal);
400 VectorNegate(brush->planes[0].normal, brush->planes[1].normal);
401 brush->planes[1].dist = -brush->planes[0].dist;
402 VectorSubtract(brush->points[2].v, brush->points[0].v, edge0);
403 VectorSubtract(brush->points[0].v, brush->points[1].v, edge1);
404 VectorSubtract(brush->points[1].v, brush->points[2].v, edge2);
407 float projectionnormal[3], projectionedge0[3], projectionedge1[3], projectionedge2[3];
409 float dist, bestdist;
410 bestdist = fabs(brush->planes[0].normal[0]);
412 for (i = 1;i < 3;i++)
414 dist = fabs(brush->planes[0].normal[i]);
421 VectorClear(projectionnormal);
422 if (brush->planes[0].normal[best] < 0)
423 projectionnormal[best] = -1;
425 projectionnormal[best] = 1;
426 VectorCopy(edge0, projectionedge0);
427 VectorCopy(edge1, projectionedge1);
428 VectorCopy(edge2, projectionedge2);
429 projectionedge0[best] = 0;
430 projectionedge1[best] = 0;
431 projectionedge2[best] = 0;
432 CrossProduct(projectionedge0, projectionnormal, brush->planes[2].normal);
433 CrossProduct(projectionedge1, projectionnormal, brush->planes[3].normal);
434 CrossProduct(projectionedge2, projectionnormal, brush->planes[4].normal);
437 CrossProduct(edge0, brush->planes->normal, brush->planes[2].normal);
438 CrossProduct(edge1, brush->planes->normal, brush->planes[3].normal);
439 CrossProduct(edge2, brush->planes->normal, brush->planes[4].normal);
441 VectorNormalize(brush->planes[2].normal);
442 VectorNormalize(brush->planes[3].normal);
443 VectorNormalize(brush->planes[4].normal);
444 brush->planes[2].dist = DotProduct(brush->points[2].v, brush->planes[2].normal);
445 brush->planes[3].dist = DotProduct(brush->points[0].v, brush->planes[3].normal);
446 brush->planes[4].dist = DotProduct(brush->points[1].v, brush->planes[4].normal);
448 if (developer.integer >= 100)
454 VectorSubtract(brush->points[0].v, brush->points[1].v, edge0);
455 VectorSubtract(brush->points[2].v, brush->points[1].v, edge1);
456 CrossProduct(edge0, edge1, normal);
457 VectorNormalize(normal);
458 VectorSubtract(normal, brush->planes[0].normal, temp);
459 if (VectorLength(temp) > 0.01f)
460 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]);
461 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)
462 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);
464 if (fabs(DotProduct(brush->planes[2].normal, brush->planes[0].normal)) > 0.01f)
465 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);
466 if (fabs(DotProduct(brush->planes[3].normal, brush->planes[0].normal)) > 0.01f)
467 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);
468 if (fabs(DotProduct(brush->planes[4].normal, brush->planes[0].normal)) > 0.01f)
469 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);
470 if (fabs(DotProduct(brush->planes[2].normal, edge0)) > 0.01f)
471 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]);
472 if (fabs(DotProduct(brush->planes[3].normal, edge1)) > 0.01f)
473 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]);
474 if (fabs(DotProduct(brush->planes[4].normal, edge2)) > 0.01f)
475 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]);
478 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)
479 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);
480 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)
481 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);
482 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)
483 Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: edge 0 (%f %f %f to %f %f %f) off front plane 2 (%f %f %f %f)\n", brush->points[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);
484 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)
485 Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: edge 0 (%f %f %f to %f %f %f) off front plane 2 (%f %f %f %f)\n", brush->points[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);
486 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)
487 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);
493 // choose best surface normal for polygon's plane
495 for (i = 0, p = brush->points + 1;i < brush->numpoints - 2;i++, p++)
497 VectorSubtract(p[-1].v, p[0].v, edge0);
498 VectorSubtract(p[1].v, p[0].v, edge1);
499 CrossProduct(edge0, edge1, normal);
500 //TriangleNormal(p[-1].v, p[0].v, p[1].v, normal);
501 dist = DotProduct(normal, normal);
502 if (i == 0 || bestdist < dist)
505 VectorCopy(normal, brush->planes->normal);
508 if (bestdist < 0.0001f)
510 // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
511 brush->numplanes = 0;
516 brush->numplanes = brush->numpoints + 2;
517 VectorNormalize(brush->planes->normal);
518 brush->planes->dist = DotProduct(brush->points->v, brush->planes->normal);
520 // negate plane to create other side
521 VectorNegate(brush->planes[0].normal, brush->planes[1].normal);
522 brush->planes[1].dist = -brush->planes[0].dist;
523 for (i = 0, p = brush->points + (brush->numpoints - 1), p2 = brush->points;i < brush->numpoints;i++, p = p2, p2++)
525 VectorSubtract(p->v, p2->v, edge0);
526 CrossProduct(edge0, brush->planes->normal, brush->planes[i + 2].normal);
527 VectorNormalize(brush->planes[i + 2].normal);
528 brush->planes[i + 2].dist = DotProduct(p->v, brush->planes[i + 2].normal);
533 if (developer.integer >= 100)
535 // validity check - will be disabled later
536 Collision_ValidateBrush(brush);
537 for (i = 0;i < brush->numplanes;i++)
540 for (j = 0, p = brush->points;j < brush->numpoints;j++, p++)
541 if (DotProduct(p->v, brush->planes[i].normal) > brush->planes[i].dist + COLLISION_PLANE_DIST_EPSILON)
542 Con_Printf("Error in brush plane generation, plane %i\n", i);
547 colbrushf_t *Collision_AllocBrushFromPermanentPolygonFloat(mempool_t *mempool, int numpoints, float *points, int supercontents)
550 brush = (colbrushf_t *)Mem_Alloc(mempool, sizeof(colbrushf_t) + sizeof(colplanef_t) * (numpoints + 2));
551 brush->supercontents = supercontents;
552 brush->numpoints = numpoints;
553 brush->numplanes = numpoints + 2;
554 brush->planes = (colplanef_t *)(brush + 1);
555 brush->points = (colpointf_t *)points;
556 Sys_Error("Collision_AllocBrushFromPermanentPolygonFloat: FIXME: this code needs to be updated to generate a mesh...");
560 // NOTE: start and end of each brush pair must have same numplanes/numpoints
561 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)
563 int nplane, nplane2, hitq3surfaceflags = 0;
564 float enterfrac = -1, leavefrac = 1, d1, d2, f, imove, newimpactnormal[3], enterfrac2 = -1;
565 const colplanef_t *startplane, *endplane;
566 texture_t *hittexture = NULL;
568 VectorClear(newimpactnormal);
570 for (nplane = 0;nplane < thatbrush_start->numplanes + thisbrush_start->numplanes;nplane++)
573 if (nplane2 >= thatbrush_start->numplanes)
575 nplane2 -= thatbrush_start->numplanes;
576 startplane = thisbrush_start->planes + nplane2;
577 endplane = thisbrush_end->planes + nplane2;
578 if (developer.integer >= 100)
580 // any brush with degenerate planes is not worth handling
581 if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
583 Con_Print("Collision_TraceBrushBrushFloat: degenerate thisbrush plane!\n");
586 f = furthestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints);
587 if (fabs(f - startplane->dist) > COLLISION_PLANE_DIST_EPSILON)
588 Con_Printf("startplane->dist %f != calculated %f (thisbrush_start)\n", startplane->dist, f);
590 d1 = nearestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints) - furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints) - collision_startnudge.value;
591 d2 = nearestplanedist_float(endplane->normal, thisbrush_end->points, thisbrush_end->numpoints) - furthestplanedist_float(endplane->normal, thatbrush_end->points, thatbrush_end->numpoints) - collision_endnudge.value;
595 startplane = thatbrush_start->planes + nplane2;
596 endplane = thatbrush_end->planes + nplane2;
597 if (developer.integer >= 100)
599 // any brush with degenerate planes is not worth handling
600 if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
602 Con_Print("Collision_TraceBrushBrushFloat: degenerate thatbrush plane!\n");
605 f = furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints);
606 if (fabs(f - startplane->dist) > COLLISION_PLANE_DIST_EPSILON)
607 Con_Printf("startplane->dist %f != calculated %f (thatbrush_start)\n", startplane->dist, f);
609 d1 = nearestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints) - startplane->dist - collision_startnudge.value;
610 d2 = nearestplanedist_float(endplane->normal, thisbrush_end->points, thisbrush_end->numpoints) - endplane->dist - collision_endnudge.value;
612 //Con_Printf("%c%i: d1 = %f, d2 = %f, d1 / (d1 - d2) = %f\n", nplane2 != nplane ? 'b' : 'a', nplane2, d1, d2, d1 / (d1 - d2));
622 imove = 1 / (d1 - d2);
623 f = (d1 - collision_enternudge.value) * imove;
624 // check if this will reduce the collision time range
627 // reduced collision time range
629 // if the collision time range is now empty, no collision
630 if (enterfrac > leavefrac)
632 // if the collision would be further away than the trace's
633 // existing collision data, we don't care about this
635 if (enterfrac > trace->realfraction)
637 // calculate the nudged fraction and impact normal we'll
638 // need if we accept this collision later
639 enterfrac2 = f - collision_impactnudge.value * imove;
640 VectorLerp(startplane->normal, enterfrac, endplane->normal, newimpactnormal);
641 hitq3surfaceflags = startplane->q3surfaceflags;
642 hittexture = startplane->texture;
648 // moving out of brush
654 f = (d1 + collision_leavenudge.value) / (d1 - d2);
655 // check if this will reduce the collision time range
658 // reduced collision time range
660 // if the collision time range is now empty, no collision
661 if (enterfrac > leavefrac)
668 // at this point we know the trace overlaps the brush because it was not
669 // rejected at any point in the loop above
671 // see if the trace started outside the brush or not
674 // started outside, and overlaps, therefore there is a collision here
675 // store out the impact information
676 if (trace->hitsupercontentsmask & thatbrush_start->supercontents)
678 trace->hitsupercontents = thatbrush_start->supercontents;
679 trace->hitq3surfaceflags = hitq3surfaceflags;
680 trace->hittexture = hittexture;
681 trace->realfraction = bound(0, enterfrac, 1);
682 trace->fraction = bound(0, enterfrac2, 1);
683 if (collision_prefernudgedfraction.integer)
684 trace->realfraction = trace->fraction;
685 VectorCopy(newimpactnormal, trace->plane.normal);
690 // started inside, update startsolid and friends
691 trace->startsupercontents |= thatbrush_start->supercontents;
692 if (trace->hitsupercontentsmask & thatbrush_start->supercontents)
694 trace->startsolid = true;
696 trace->allsolid = true;
701 // NOTE: start and end brush pair must have same numplanes/numpoints
702 void Collision_TraceLineBrushFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, const colbrushf_t *thatbrush_start, const colbrushf_t *thatbrush_end)
704 int nplane, hitq3surfaceflags = 0;
705 float enterfrac = -1, leavefrac = 1, d1, d2, f, imove, newimpactnormal[3], enterfrac2 = -1;
706 const colplanef_t *startplane, *endplane;
707 texture_t *hittexture = NULL;
709 VectorClear(newimpactnormal);
711 for (nplane = 0;nplane < thatbrush_start->numplanes;nplane++)
713 startplane = thatbrush_start->planes + nplane;
714 endplane = thatbrush_end->planes + nplane;
715 d1 = DotProduct(startplane->normal, linestart) - startplane->dist - collision_startnudge.value;
716 d2 = DotProduct(endplane->normal, lineend) - endplane->dist - collision_endnudge.value;
717 if (developer.integer >= 100)
719 // any brush with degenerate planes is not worth handling
720 if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
722 Con_Print("Collision_TraceLineBrushFloat: degenerate plane!\n");
725 if (thatbrush_start->numpoints)
727 f = furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints);
728 if (fabs(f - startplane->dist) > COLLISION_PLANE_DIST_EPSILON)
729 Con_Printf("startplane->dist %f != calculated %f\n", startplane->dist, f);
741 imove = 1 / (d1 - d2);
742 f = (d1 - collision_enternudge.value) * imove;
743 // check if this will reduce the collision time range
746 // reduced collision time range
748 // if the collision time range is now empty, no collision
749 if (enterfrac > leavefrac)
751 // if the collision would be further away than the trace's
752 // existing collision data, we don't care about this
754 if (enterfrac > trace->realfraction)
756 // calculate the nudged fraction and impact normal we'll
757 // need if we accept this collision later
758 enterfrac2 = f - collision_impactnudge.value * imove;
759 VectorLerp(startplane->normal, enterfrac, endplane->normal, newimpactnormal);
760 hitq3surfaceflags = startplane->q3surfaceflags;
761 hittexture = startplane->texture;
767 // moving out of brush
773 f = (d1 + collision_leavenudge.value) / (d1 - d2);
774 // check if this will reduce the collision time range
777 // reduced collision time range
779 // if the collision time range is now empty, no collision
780 if (enterfrac > leavefrac)
787 // at this point we know the trace overlaps the brush because it was not
788 // rejected at any point in the loop above
790 // see if the trace started outside the brush or not
793 // started outside, and overlaps, therefore there is a collision here
794 // store out the impact information
795 if (trace->hitsupercontentsmask & thatbrush_start->supercontents)
797 trace->hitsupercontents = thatbrush_start->supercontents;
798 trace->hitq3surfaceflags = hitq3surfaceflags;
799 trace->hittexture = hittexture;
800 trace->realfraction = bound(0, enterfrac, 1);
801 trace->fraction = bound(0, enterfrac2, 1);
802 if (collision_prefernudgedfraction.integer)
803 trace->realfraction = trace->fraction;
804 VectorCopy(newimpactnormal, trace->plane.normal);
809 // started inside, update startsolid and friends
810 trace->startsupercontents |= thatbrush_start->supercontents;
811 if (trace->hitsupercontentsmask & thatbrush_start->supercontents)
813 trace->startsolid = true;
815 trace->allsolid = true;
820 void Collision_TracePointBrushFloat(trace_t *trace, const vec3_t point, const colbrushf_t *thatbrush)
823 const colplanef_t *plane;
825 for (nplane = 0, plane = thatbrush->planes;nplane < thatbrush->numplanes;nplane++, plane++)
826 if (DotProduct(plane->normal, point) > plane->dist)
829 trace->startsupercontents |= thatbrush->supercontents;
830 if (trace->hitsupercontentsmask & thatbrush->supercontents)
832 trace->startsolid = true;
833 trace->allsolid = true;
837 static colpointf_t polyf_points[256];
838 static colplanef_t polyf_planes[256 + 2];
839 static colbrushf_t polyf_brush;
841 void Collision_SnapCopyPoints(int numpoints, const colpointf_t *in, colpointf_t *out, float fractionprecision, float invfractionprecision)
844 for (i = 0;i < numpoints;i++)
846 out[i].v[0] = floor(in[i].v[0] * fractionprecision + 0.5f) * invfractionprecision;
847 out[i].v[1] = floor(in[i].v[1] * fractionprecision + 0.5f) * invfractionprecision;
848 out[i].v[2] = floor(in[i].v[2] * fractionprecision + 0.5f) * invfractionprecision;
852 void Collision_TraceBrushPolygonFloat(trace_t *trace, const colbrushf_t *thisbrush_start, const colbrushf_t *thisbrush_end, int numpoints, const float *points, int supercontents)
856 Con_Print("Polygon with more than 256 points not supported yet (fixme!)\n");
859 polyf_brush.numpoints = numpoints;
860 polyf_brush.numplanes = numpoints + 2;
861 //polyf_brush.points = (colpointf_t *)points;
862 polyf_brush.planes = polyf_planes;
863 polyf_brush.supercontents = supercontents;
864 polyf_brush.points = polyf_points;
865 Collision_SnapCopyPoints(polyf_brush.numpoints, (colpointf_t *)points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
866 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
867 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
868 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brush, &polyf_brush);
871 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)
874 polyf_brush.numpoints = 3;
875 polyf_brush.numplanes = 5;
876 polyf_brush.points = polyf_points;
877 polyf_brush.planes = polyf_planes;
878 polyf_brush.supercontents = supercontents;
879 for (i = 0;i < polyf_brush.numplanes;i++)
881 polyf_brush.planes[i].q3surfaceflags = q3surfaceflags;
882 polyf_brush.planes[i].texture = texture;
884 for (i = 0;i < numtriangles;i++, element3i += 3)
886 if (segmentmaxs[0] >= min(vertex3f[element3i[0]*3+0], min(vertex3f[element3i[1]*3+0], vertex3f[element3i[2]*3+0]))
887 && segmentmins[0] <= max(vertex3f[element3i[0]*3+0], max(vertex3f[element3i[1]*3+0], vertex3f[element3i[2]*3+0]))
888 && segmentmaxs[1] >= min(vertex3f[element3i[0]*3+1], min(vertex3f[element3i[1]*3+1], vertex3f[element3i[2]*3+1]))
889 && segmentmins[1] <= max(vertex3f[element3i[0]*3+1], max(vertex3f[element3i[1]*3+1], vertex3f[element3i[2]*3+1]))
890 && segmentmaxs[2] >= min(vertex3f[element3i[0]*3+2], min(vertex3f[element3i[1]*3+2], vertex3f[element3i[2]*3+2]))
891 && segmentmins[2] <= max(vertex3f[element3i[0]*3+2], max(vertex3f[element3i[1]*3+2], vertex3f[element3i[2]*3+2])))
893 VectorCopy(vertex3f + element3i[0] * 3, polyf_points[0].v);
894 VectorCopy(vertex3f + element3i[1] * 3, polyf_points[1].v);
895 VectorCopy(vertex3f + element3i[2] * 3, polyf_points[2].v);
896 Collision_SnapCopyPoints(polyf_brush.numpoints, polyf_points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
897 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
898 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
899 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brush, &polyf_brush);
904 void Collision_TraceLinePolygonFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, int numpoints, const float *points, int supercontents)
908 Con_Print("Polygon with more than 256 points not supported yet (fixme!)\n");
911 polyf_brush.numpoints = numpoints;
912 polyf_brush.numplanes = numpoints + 2;
913 //polyf_brush.points = (colpointf_t *)points;
914 polyf_brush.points = polyf_points;
915 Collision_SnapCopyPoints(polyf_brush.numpoints, (colpointf_t *)points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
916 polyf_brush.planes = polyf_planes;
917 polyf_brush.supercontents = supercontents;
918 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
919 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
920 Collision_TraceLineBrushFloat(trace, linestart, lineend, &polyf_brush, &polyf_brush);
923 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)
927 // FIXME: snap vertices?
928 for (i = 0;i < numtriangles;i++, element3i += 3)
929 Collision_TraceLineTriangleFloat(trace, linestart, lineend, vertex3f + element3i[0] * 3, vertex3f + element3i[1] * 3, vertex3f + element3i[2] * 3, supercontents, q3surfaceflags, texture);
931 polyf_brush.numpoints = 3;
932 polyf_brush.numplanes = 5;
933 polyf_brush.points = polyf_points;
934 polyf_brush.planes = polyf_planes;
935 polyf_brush.supercontents = supercontents;
936 for (i = 0;i < polyf_brush.numplanes;i++)
938 polyf_brush.planes[i].supercontents = supercontents;
939 polyf_brush.planes[i].q3surfaceflags = q3surfaceflags;
940 polyf_brush.planes[i].texture = texture;
942 for (i = 0;i < numtriangles;i++, element3i += 3)
944 if (segmentmaxs[0] >= min(vertex3f[element3i[0]*3+0], min(vertex3f[element3i[1]*3+0], vertex3f[element3i[2]*3+0]))
945 && segmentmins[0] <= max(vertex3f[element3i[0]*3+0], max(vertex3f[element3i[1]*3+0], vertex3f[element3i[2]*3+0]))
946 && segmentmaxs[1] >= min(vertex3f[element3i[0]*3+1], min(vertex3f[element3i[1]*3+1], vertex3f[element3i[2]*3+1]))
947 && segmentmins[1] <= max(vertex3f[element3i[0]*3+1], max(vertex3f[element3i[1]*3+1], vertex3f[element3i[2]*3+1]))
948 && segmentmaxs[2] >= min(vertex3f[element3i[0]*3+2], min(vertex3f[element3i[1]*3+2], vertex3f[element3i[2]*3+2]))
949 && segmentmins[2] <= max(vertex3f[element3i[0]*3+2], max(vertex3f[element3i[1]*3+2], vertex3f[element3i[2]*3+2])))
951 VectorCopy(vertex3f + element3i[0] * 3, polyf_points[0].v);
952 VectorCopy(vertex3f + element3i[1] * 3, polyf_points[1].v);
953 VectorCopy(vertex3f + element3i[2] * 3, polyf_points[2].v);
954 Collision_SnapCopyPoints(polyf_brush.numpoints, polyf_points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
955 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
956 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
957 Collision_TraceLineBrushFloat(trace, linestart, lineend, &polyf_brush, &polyf_brush);
964 static colpointf_t polyf_pointsstart[256], polyf_pointsend[256];
965 static colplanef_t polyf_planesstart[256 + 2], polyf_planesend[256 + 2];
966 static colbrushf_t polyf_brushstart, polyf_brushend;
968 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)
973 Con_Print("Polygon with more than 256 points not supported yet (fixme!)\n");
976 polyf_brushstart.numpoints = numpoints;
977 polyf_brushstart.numplanes = numpoints + 2;
978 polyf_brushstart.points = polyf_pointsstart;//(colpointf_t *)points;
979 polyf_brushstart.planes = polyf_planesstart;
980 polyf_brushstart.supercontents = supercontents;
981 for (i = 0;i < numpoints;i++)
982 Matrix4x4_Transform(polygonmatrixstart, points + i * 3, polyf_brushstart.points[i].v);
983 polyf_brushend.numpoints = numpoints;
984 polyf_brushend.numplanes = numpoints + 2;
985 polyf_brushend.points = polyf_pointsend;//(colpointf_t *)points;
986 polyf_brushend.planes = polyf_planesend;
987 polyf_brushend.supercontents = supercontents;
988 for (i = 0;i < numpoints;i++)
989 Matrix4x4_Transform(polygonmatrixend, points + i * 3, polyf_brushend.points[i].v);
990 for (i = 0;i < polyf_brushstart.numplanes;i++)
992 polyf_brushstart.planes[i].q3surfaceflags = q3surfaceflags;
993 polyf_brushstart.planes[i].texture = texture;
995 Collision_SnapCopyPoints(polyf_brushstart.numpoints, polyf_pointsstart, polyf_pointsstart, COLLISION_SNAPSCALE, COLLISION_SNAP);
996 Collision_SnapCopyPoints(polyf_brushend.numpoints, polyf_pointsend, polyf_pointsend, COLLISION_SNAPSCALE, COLLISION_SNAP);
997 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brushstart);
998 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brushend);
1000 //Collision_PrintBrushAsQHull(&polyf_brushstart, "polyf_brushstart");
1001 //Collision_PrintBrushAsQHull(&polyf_brushend, "polyf_brushend");
1003 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brushstart, &polyf_brushend);
1008 #define MAX_BRUSHFORBOX 16
1009 static int brushforbox_index = 0;
1010 static colpointf_t brushforbox_point[MAX_BRUSHFORBOX*8];
1011 static colplanef_t brushforbox_plane[MAX_BRUSHFORBOX*6];
1012 static colbrushf_t brushforbox_brush[MAX_BRUSHFORBOX];
1013 static colbrushf_t brushforpoint_brush[MAX_BRUSHFORBOX];
1015 void Collision_InitBrushForBox(void)
1018 for (i = 0;i < MAX_BRUSHFORBOX;i++)
1020 brushforbox_brush[i].numpoints = 8;
1021 brushforbox_brush[i].numplanes = 6;
1022 brushforbox_brush[i].points = brushforbox_point + i * 8;
1023 brushforbox_brush[i].planes = brushforbox_plane + i * 6;
1024 brushforpoint_brush[i].numpoints = 1;
1025 brushforpoint_brush[i].numplanes = 0;
1026 brushforpoint_brush[i].points = brushforbox_point + i * 8;
1027 brushforpoint_brush[i].planes = brushforbox_plane + i * 6;
1031 colbrushf_t *Collision_BrushForBox(const matrix4x4_t *matrix, const vec3_t mins, const vec3_t maxs, int supercontents, int q3surfaceflags, texture_t *texture)
1036 if (brushforbox_brush[0].numpoints == 0)
1037 Collision_InitBrushForBox();
1038 // FIXME: these probably don't actually need to be normalized if the collision code does not care
1039 if (VectorCompare(mins, maxs))
1042 brush = brushforpoint_brush + ((brushforbox_index++) % MAX_BRUSHFORBOX);
1043 VectorCopy(mins, brush->points->v);
1047 brush = brushforbox_brush + ((brushforbox_index++) % MAX_BRUSHFORBOX);
1049 for (i = 0;i < 8;i++)
1051 v[0] = i & 1 ? maxs[0] : mins[0];
1052 v[1] = i & 2 ? maxs[1] : mins[1];
1053 v[2] = i & 4 ? maxs[2] : mins[2];
1054 Matrix4x4_Transform(matrix, v, brush->points[i].v);
1057 for (i = 0;i < 6;i++)
1060 v[i >> 1] = i & 1 ? 1 : -1;
1061 Matrix4x4_Transform3x3(matrix, v, brush->planes[i].normal);
1062 VectorNormalize(brush->planes[i].normal);
1065 brush->supercontents = supercontents;
1066 for (j = 0;j < brush->numplanes;j++)
1068 brush->planes[j].q3surfaceflags = q3surfaceflags;
1069 brush->planes[j].texture = texture;
1070 brush->planes[j].dist = furthestplanedist_float(brush->planes[j].normal, brush->points, brush->numpoints);
1072 VectorCopy(brush->points[0].v, brush->mins);
1073 VectorCopy(brush->points[0].v, brush->maxs);
1074 for (j = 1;j < brush->numpoints;j++)
1076 brush->mins[0] = min(brush->mins[0], brush->points[j].v[0]);
1077 brush->mins[1] = min(brush->mins[1], brush->points[j].v[1]);
1078 brush->mins[2] = min(brush->mins[2], brush->points[j].v[2]);
1079 brush->maxs[0] = max(brush->maxs[0], brush->points[j].v[0]);
1080 brush->maxs[1] = max(brush->maxs[1], brush->points[j].v[1]);
1081 brush->maxs[2] = max(brush->maxs[2], brush->points[j].v[2]);
1083 brush->mins[0] -= 1;
1084 brush->mins[1] -= 1;
1085 brush->mins[2] -= 1;
1086 brush->maxs[0] += 1;
1087 brush->maxs[1] += 1;
1088 brush->maxs[2] += 1;
1089 Collision_ValidateBrush(brush);
1093 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)
1095 colbrushf_t *boxbrush, *thisbrush_start, *thisbrush_end;
1096 vec3_t startmins, startmaxs, endmins, endmaxs;
1098 // create brushes for the collision
1099 VectorAdd(start, mins, startmins);
1100 VectorAdd(start, maxs, startmaxs);
1101 VectorAdd(end, mins, endmins);
1102 VectorAdd(end, maxs, endmaxs);
1103 boxbrush = Collision_BrushForBox(&identitymatrix, cmins, cmaxs, supercontents, q3surfaceflags, texture);
1104 thisbrush_start = Collision_BrushForBox(&identitymatrix, startmins, startmaxs, 0, 0, NULL);
1105 thisbrush_end = Collision_BrushForBox(&identitymatrix, endmins, endmaxs, 0, 0, NULL);
1107 memset(trace, 0, sizeof(trace_t));
1108 trace->hitsupercontentsmask = hitsupercontentsmask;
1109 trace->fraction = 1;
1110 trace->realfraction = 1;
1111 trace->allsolid = true;
1112 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, boxbrush, boxbrush);
1115 //pseudocode for detecting line/sphere overlap without calculating an impact point
1116 //linesphereorigin = sphereorigin - linestart;linediff = lineend - linestart;linespherefrac = DotProduct(linesphereorigin, linediff) / DotProduct(linediff, linediff);return VectorLength2(linesphereorigin - bound(0, linespherefrac, 1) * linediff) >= sphereradius*sphereradius;
1118 // LordHavoc: currently unused, but tested
1119 // note: this can be used for tracing a moving sphere vs a stationary sphere,
1120 // by simply adding the moving sphere's radius to the sphereradius parameter,
1121 // all the results are correct (impactpoint, impactnormal, and fraction)
1122 float Collision_ClipTrace_Line_Sphere(double *linestart, double *lineend, double *sphereorigin, double sphereradius, double *impactpoint, double *impactnormal)
1124 double dir[3], scale, v[3], deviationdist, impactdist, linelength;
1125 // make sure the impactpoint and impactnormal are valid even if there is
1127 VectorCopy(lineend, impactpoint);
1128 VectorClear(impactnormal);
1129 // calculate line direction
1130 VectorSubtract(lineend, linestart, dir);
1131 // normalize direction
1132 linelength = VectorLength(dir);
1135 scale = 1.0 / linelength;
1136 VectorScale(dir, scale, dir);
1138 // this dotproduct calculates the distance along the line at which the
1139 // sphere origin is (nearest point to the sphere origin on the line)
1140 impactdist = DotProduct(sphereorigin, dir) - DotProduct(linestart, dir);
1141 // calculate point on line at that distance, and subtract the
1142 // sphereorigin from it, so we have a vector to measure for the distance
1143 // of the line from the sphereorigin (deviation, how off-center it is)
1144 VectorMA(linestart, impactdist, dir, v);
1145 VectorSubtract(v, sphereorigin, v);
1146 deviationdist = VectorLength2(v);
1147 // if outside the radius, it's a miss for sure
1148 // (we do this comparison using squared radius to avoid a sqrt)
1149 if (deviationdist > sphereradius*sphereradius)
1150 return 1; // miss (off to the side)
1151 // nudge back to find the correct impact distance
1152 impactdist += deviationdist - sphereradius;
1153 if (impactdist >= linelength)
1154 return 1; // miss (not close enough)
1156 return 1; // miss (linestart is past or inside sphere)
1157 // calculate new impactpoint
1158 VectorMA(linestart, impactdist, dir, impactpoint);
1159 // calculate impactnormal (surface normal at point of impact)
1160 VectorSubtract(impactpoint, sphereorigin, impactnormal);
1161 // normalize impactnormal
1162 VectorNormalize(impactnormal);
1163 // return fraction of movement distance
1164 return impactdist / linelength;
1167 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)
1171 float d1, d2, d, f, impact[3], edgenormal[3], faceplanenormal[3], faceplanedist, faceplanenormallength2, edge01[3], edge21[3], edge02[3];
1173 // this function executes:
1174 // 32 ops when line starts behind triangle
1175 // 38 ops when line ends infront of triangle
1176 // 43 ops when line fraction is already closer than this triangle
1177 // 72 ops when line is outside edge 01
1178 // 92 ops when line is outside edge 21
1179 // 115 ops when line is outside edge 02
1180 // 123 ops when line impacts triangle and updates trace results
1182 // this code is designed for clockwise triangles, conversion to
1183 // counterclockwise would require swapping some things around...
1184 // it is easier to simply swap the point0 and point2 parameters to this
1185 // function when calling it than it is to rewire the internals.
1187 // calculate the faceplanenormal of the triangle, this represents the front side
1189 VectorSubtract(point0, point1, edge01);
1190 VectorSubtract(point2, point1, edge21);
1191 CrossProduct(edge01, edge21, faceplanenormal);
1192 // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
1194 faceplanenormallength2 = DotProduct(faceplanenormal, faceplanenormal);
1195 if (faceplanenormallength2 < 0.0001f)
1197 // calculate the distance
1199 faceplanedist = DotProduct(point0, faceplanenormal);
1201 // if start point is on the back side there is no collision
1202 // (we don't care about traces going through the triangle the wrong way)
1204 // calculate the start distance
1206 d1 = DotProduct(faceplanenormal, linestart);
1207 if (d1 <= faceplanedist)
1210 // calculate the end distance
1212 d2 = DotProduct(faceplanenormal, lineend);
1213 // if both are in front, there is no collision
1214 if (d2 >= faceplanedist)
1217 // from here on we know d1 is >= 0 and d2 is < 0
1218 // this means the line starts infront and ends behind, passing through it
1220 // calculate the recipricol of the distance delta,
1221 // so we can use it multiple times cheaply (instead of division)
1223 d = 1.0f / (d1 - d2);
1224 // calculate the impact fraction by taking the start distance (> 0)
1225 // and subtracting the face plane distance (this is the distance of the
1226 // triangle along that same normal)
1227 // then multiply by the recipricol distance delta
1229 f = (d1 - faceplanedist) * d;
1230 // skip out if this impact is further away than previous ones
1232 if (f > trace->realfraction)
1234 // calculate the perfect impact point for classification of insidedness
1236 impact[0] = linestart[0] + f * (lineend[0] - linestart[0]);
1237 impact[1] = linestart[1] + f * (lineend[1] - linestart[1]);
1238 impact[2] = linestart[2] + f * (lineend[2] - linestart[2]);
1240 // calculate the edge normal and reject if impact is outside triangle
1241 // (an edge normal faces away from the triangle, to get the desired normal
1242 // a crossproduct with the faceplanenormal is used, and because of the way
1243 // the insidedness comparison is written it does not need to be normalized)
1245 // first use the two edges from the triangle plane math
1246 // the other edge only gets calculated if the point survives that long
1249 CrossProduct(edge01, faceplanenormal, edgenormal);
1250 if (DotProduct(impact, edgenormal) > DotProduct(point1, edgenormal))
1254 CrossProduct(faceplanenormal, edge21, edgenormal);
1255 if (DotProduct(impact, edgenormal) > DotProduct(point2, edgenormal))
1259 VectorSubtract(point0, point2, edge02);
1260 CrossProduct(faceplanenormal, edge02, edgenormal);
1261 if (DotProduct(impact, edgenormal) > DotProduct(point0, edgenormal))
1266 // store the new trace fraction
1267 trace->realfraction = f;
1269 // calculate a nudged fraction to keep it out of the surface
1270 // (the main fraction remains perfect)
1271 trace->fraction = f - collision_impactnudge.value * d;
1273 if (collision_prefernudgedfraction.integer)
1274 trace->realfraction = trace->fraction;
1276 // store the new trace plane (because collisions only happen from
1277 // the front this is always simply the triangle normal, never flipped)
1278 d = 1.0 / sqrt(faceplanenormallength2);
1279 VectorScale(faceplanenormal, d, trace->plane.normal);
1280 trace->plane.dist = faceplanedist * d;
1282 trace->hitsupercontents = supercontents;
1283 trace->hitq3surfaceflags = q3surfaceflags;
1284 trace->hittexture = texture;
1286 float d1, d2, d, f, fnudged, impact[3], edgenormal[3], faceplanenormal[3], faceplanedist, edge[3];
1288 // this code is designed for clockwise triangles, conversion to
1289 // counterclockwise would require swapping some things around...
1290 // it is easier to simply swap the point0 and point2 parameters to this
1291 // function when calling it than it is to rewire the internals.
1293 // calculate the unnormalized faceplanenormal of the triangle,
1294 // this represents the front side
1295 TriangleNormal(point0, point1, point2, faceplanenormal);
1296 // there's no point in processing a degenerate triangle
1297 // (GIGO - Garbage In, Garbage Out)
1298 if (DotProduct(faceplanenormal, faceplanenormal) < 0.0001f)
1300 // calculate the unnormalized distance
1301 faceplanedist = DotProduct(point0, faceplanenormal);
1303 // calculate the unnormalized start distance
1304 d1 = DotProduct(faceplanenormal, linestart) - faceplanedist;
1305 // if start point is on the back side there is no collision
1306 // (we don't care about traces going through the triangle the wrong way)
1310 // calculate the unnormalized end distance
1311 d2 = DotProduct(faceplanenormal, lineend) - faceplanedist;
1312 // if both are in front, there is no collision
1316 // from here on we know d1 is >= 0 and d2 is < 0
1317 // this means the line starts infront and ends behind, passing through it
1319 // calculate the recipricol of the distance delta,
1320 // so we can use it multiple times cheaply (instead of division)
1321 d = 1.0f / (d1 - d2);
1322 // calculate the impact fraction by taking the start distance (> 0)
1323 // and subtracting the face plane distance (this is the distance of the
1324 // triangle along that same normal)
1325 // then multiply by the recipricol distance delta
1327 // skip out if this impact is further away than previous ones
1328 if (f > trace->realfraction)
1330 // calculate the perfect impact point for classification of insidedness
1331 impact[0] = linestart[0] + f * (lineend[0] - linestart[0]);
1332 impact[1] = linestart[1] + f * (lineend[1] - linestart[1]);
1333 impact[2] = linestart[2] + f * (lineend[2] - linestart[2]);
1335 // calculate the edge normal and reject if impact is outside triangle
1336 // (an edge normal faces away from the triangle, to get the desired normal
1337 // a crossproduct with the faceplanenormal is used, and because of the way
1338 // the insidedness comparison is written it does not need to be normalized)
1340 VectorSubtract(point2, point0, edge);
1341 CrossProduct(edge, faceplanenormal, edgenormal);
1342 if (DotProduct(impact, edgenormal) > DotProduct(point0, edgenormal))
1345 VectorSubtract(point0, point1, edge);
1346 CrossProduct(edge, faceplanenormal, edgenormal);
1347 if (DotProduct(impact, edgenormal) > DotProduct(point1, edgenormal))
1350 VectorSubtract(point1, point2, edge);
1351 CrossProduct(edge, faceplanenormal, edgenormal);
1352 if (DotProduct(impact, edgenormal) > DotProduct(point2, edgenormal))
1355 // store the new trace fraction
1356 trace->realfraction = bound(0, f, 1);
1358 // store the new trace plane (because collisions only happen from
1359 // the front this is always simply the triangle normal, never flipped)
1360 VectorNormalize(faceplanenormal);
1361 VectorCopy(faceplanenormal, trace->plane.normal);
1362 trace->plane.dist = DotProduct(point0, faceplanenormal);
1364 // calculate the normalized start and end distances
1365 d1 = DotProduct(trace->plane.normal, linestart) - trace->plane.dist;
1366 d2 = DotProduct(trace->plane.normal, lineend) - trace->plane.dist;
1368 // calculate a nudged fraction to keep it out of the surface
1369 // (the main fraction remains perfect)
1370 fnudged = (d1 - collision_impactnudge.value) / (d1 - d2);
1371 trace->fraction = bound(0, fnudged, 1);
1373 // store the new trace endpos
1374 // not needed, it's calculated later when the trace is finished
1375 //trace->endpos[0] = linestart[0] + fnudged * (lineend[0] - linestart[0]);
1376 //trace->endpos[1] = linestart[1] + fnudged * (lineend[1] - linestart[1]);
1377 //trace->endpos[2] = linestart[2] + fnudged * (lineend[2] - linestart[2]);
1378 trace->hitsupercontents = supercontents;
1379 trace->hitq3surfaceflags = q3surfaceflags;
1380 trace->hittexture = texture;
1384 typedef struct colbspnode_s
1387 struct colbspnode_s *children[2];
1388 // the node is reallocated or split if max is reached
1391 colbrushf_t **colbrushflist;
1394 //colbrushd_t **colbrushdlist;
1398 typedef struct colbsp_s
1401 colbspnode_t *nodes;
1405 colbsp_t *Collision_CreateCollisionBSP(mempool_t *mempool)
1408 bsp = (colbsp_t *)Mem_Alloc(mempool, sizeof(colbsp_t));
1409 bsp->mempool = mempool;
1410 bsp->nodes = (colbspnode_t *)Mem_Alloc(bsp->mempool, sizeof(colbspnode_t));
1414 void Collision_FreeCollisionBSPNode(colbspnode_t *node)
1416 if (node->children[0])
1417 Collision_FreeCollisionBSPNode(node->children[0]);
1418 if (node->children[1])
1419 Collision_FreeCollisionBSPNode(node->children[1]);
1420 while (--node->numcolbrushf)
1421 Mem_Free(node->colbrushflist[node->numcolbrushf]);
1422 //while (--node->numcolbrushd)
1423 // Mem_Free(node->colbrushdlist[node->numcolbrushd]);
1427 void Collision_FreeCollisionBSP(colbsp_t *bsp)
1429 Collision_FreeCollisionBSPNode(bsp->nodes);
1433 void Collision_BoundingBoxOfBrushTraceSegment(const colbrushf_t *start, const colbrushf_t *end, vec3_t mins, vec3_t maxs, float startfrac, float endfrac)
1436 colpointf_t *ps, *pe;
1437 float tempstart[3], tempend[3];
1438 VectorLerp(start->points[0].v, startfrac, end->points[0].v, mins);
1439 VectorCopy(mins, maxs);
1440 for (i = 0, ps = start->points, pe = end->points;i < start->numpoints;i++, ps++, pe++)
1442 VectorLerp(ps->v, startfrac, pe->v, tempstart);
1443 VectorLerp(ps->v, endfrac, pe->v, tempend);
1444 mins[0] = min(mins[0], min(tempstart[0], tempend[0]));
1445 mins[1] = min(mins[1], min(tempstart[1], tempend[1]));
1446 mins[2] = min(mins[2], min(tempstart[2], tempend[2]));
1447 maxs[0] = min(maxs[0], min(tempstart[0], tempend[0]));
1448 maxs[1] = min(maxs[1], min(tempstart[1], tempend[1]));
1449 maxs[2] = min(maxs[2], min(tempstart[2], tempend[2]));
1459 //===========================================
1461 void Collision_ClipToGenericEntity(trace_t *trace, model_t *model, int frame, const vec3_t bodymins, const vec3_t bodymaxs, int bodysupercontents, matrix4x4_t *matrix, matrix4x4_t *inversematrix, const vec3_t start, const vec3_t mins, const vec3_t maxs, const vec3_t end, int hitsupercontentsmask)
1463 float tempnormal[3], starttransformed[3], endtransformed[3];
1465 memset(trace, 0, sizeof(*trace));
1466 trace->fraction = trace->realfraction = 1;
1467 VectorCopy(end, trace->endpos);
1469 Matrix4x4_Transform(inversematrix, start, starttransformed);
1470 Matrix4x4_Transform(inversematrix, end, endtransformed);
1471 #if COLLISIONPARANOID >= 3
1472 Con_Printf("trans(%f %f %f -> %f %f %f, %f %f %f -> %f %f %f)", start[0], start[1], start[2], starttransformed[0], starttransformed[1], starttransformed[2], end[0], end[1], end[2], endtransformed[0], endtransformed[1], endtransformed[2]);
1475 if (model && model->TraceBox)
1476 model->TraceBox(model, bound(0, frame, (model->numframes - 1)), trace, starttransformed, mins, maxs, endtransformed, hitsupercontentsmask);
1478 Collision_ClipTrace_Box(trace, bodymins, bodymaxs, starttransformed, mins, maxs, endtransformed, hitsupercontentsmask, bodysupercontents, 0, NULL);
1479 trace->fraction = bound(0, trace->fraction, 1);
1480 trace->realfraction = bound(0, trace->realfraction, 1);
1482 if (trace->fraction < 1)
1484 VectorLerp(start, trace->fraction, end, trace->endpos);
1485 VectorCopy(trace->plane.normal, tempnormal);
1486 Matrix4x4_Transform3x3(matrix, tempnormal, trace->plane.normal);
1487 // FIXME: should recalc trace->plane.dist
1491 void Collision_ClipToWorld(trace_t *trace, model_t *model, const vec3_t start, const vec3_t mins, const vec3_t maxs, const vec3_t end, int hitsupercontents)
1493 memset(trace, 0, sizeof(*trace));
1494 trace->fraction = trace->realfraction = 1;
1495 if (model && model->TraceBox)
1496 model->TraceBox(model, 0, trace, start, mins, maxs, end, hitsupercontents);
1497 trace->fraction = bound(0, trace->fraction, 1);
1498 trace->realfraction = bound(0, trace->realfraction, 1);
1499 VectorLerp(start, trace->fraction, end, trace->endpos);
1502 void Collision_CombineTraces(trace_t *cliptrace, const trace_t *trace, void *touch, qboolean isbmodel)
1504 // take the 'best' answers from the new trace and combine with existing data
1505 if (trace->allsolid)
1506 cliptrace->allsolid = true;
1507 if (trace->startsolid)
1510 cliptrace->bmodelstartsolid = true;
1511 cliptrace->startsolid = true;
1512 if (cliptrace->realfraction == 1)
1513 cliptrace->ent = touch;
1515 // don't set this except on the world, because it can easily confuse
1516 // monsters underwater if there's a bmodel involved in the trace
1517 // (inopen && inwater is how they check water visibility)
1518 //if (trace->inopen)
1519 // cliptrace->inopen = true;
1521 cliptrace->inwater = true;
1522 if (trace->realfraction < cliptrace->realfraction)
1524 cliptrace->fraction = trace->fraction;
1525 cliptrace->realfraction = trace->realfraction;
1526 VectorCopy(trace->endpos, cliptrace->endpos);
1527 cliptrace->plane = trace->plane;
1528 cliptrace->ent = touch;
1529 cliptrace->hitsupercontents = trace->hitsupercontents;
1530 cliptrace->hitq3surfaceflags = trace->hitq3surfaceflags;
1531 cliptrace->hittexture = trace->hittexture;
1533 cliptrace->startsupercontents |= trace->startsupercontents;