5 #define COLLISION_SNAPSCALE (8.0f)
6 #define COLLISION_SNAP (1.0f / COLLISION_SNAPSCALE)
8 cvar_t collision_impactnudge = {0, "collision_impactnudge", "0.03125", "how much to back off from the impact"};
9 cvar_t collision_startnudge = {0, "collision_startnudge", "0", "how much to bias collision trace start"};
10 cvar_t collision_endnudge = {0, "collision_endnudge", "0", "how much to bias collision trace end"};
11 cvar_t collision_enternudge = {0, "collision_enternudge", "0", "how much to bias collision entry fraction"};
12 cvar_t collision_leavenudge = {0, "collision_leavenudge", "0", "how much to bias collision exit fraction"};
14 void Collision_Init (void)
16 Cvar_RegisterVariable(&collision_impactnudge);
17 Cvar_RegisterVariable(&collision_startnudge);
18 Cvar_RegisterVariable(&collision_endnudge);
19 Cvar_RegisterVariable(&collision_enternudge);
20 Cvar_RegisterVariable(&collision_leavenudge);
36 void Collision_PrintBrushAsQHull(colbrushf_t *brush, const char *name)
39 Con_Printf("3 %s\n%i\n", name, brush->numpoints);
40 for (i = 0;i < brush->numpoints;i++)
41 Con_Printf("%f %f %f\n", brush->points[i].v[0], brush->points[i].v[1], brush->points[i].v[2]);
43 Con_Printf("4\n%i\n", brush->numplanes);
44 for (i = 0;i < brush->numplanes;i++)
45 Con_Printf("%f %f %f %f\n", brush->planes[i].normal[0], brush->planes[i].normal[1], brush->planes[i].normal[2], brush->planes[i].dist);
48 void Collision_ValidateBrush(colbrushf_t *brush)
50 int j, k, pointsoffplanes, pointonplanes, pointswithinsufficientplanes, printbrush;
53 if (!brush->numpoints)
55 Con_Print("Collision_ValidateBrush: brush with no points!\n");
59 // it's ok for a brush to have one point and no planes...
60 if (brush->numplanes == 0 && brush->numpoints != 1)
62 Con_Print("Collision_ValidateBrush: brush with no planes and more than one point!\n");
69 pointswithinsufficientplanes = 0;
70 for (k = 0;k < brush->numplanes;k++)
71 if (DotProduct(brush->planes[k].normal, brush->planes[k].normal) < 0.0001f)
72 Con_Printf("Collision_ValidateBrush: plane #%i (%f %f %f %f) is degenerate\n", k, brush->planes[k].normal[0], brush->planes[k].normal[1], brush->planes[k].normal[2], brush->planes[k].dist);
73 for (j = 0;j < brush->numpoints;j++)
76 for (k = 0;k < brush->numplanes;k++)
78 d = DotProduct(brush->points[j].v, brush->planes[k].normal) - brush->planes[k].dist;
79 if (d > (1.0f / 8.0f))
81 Con_Printf("Collision_ValidateBrush: point #%i (%f %f %f) infront of plane #%i (%f %f %f %f)\n", j, brush->points[j].v[0], brush->points[j].v[1], brush->points[j].v[2], k, brush->planes[k].normal[0], brush->planes[k].normal[1], brush->planes[k].normal[2], brush->planes[k].dist);
89 if (pointonplanes < 3)
90 pointswithinsufficientplanes++;
92 if (pointswithinsufficientplanes)
94 Con_Print("Collision_ValidateBrush: some points have insufficient planes, every point must be on at least 3 planes to form a corner.\n");
97 if (pointsoffplanes == 0) // all points are on all planes
99 Con_Print("Collision_ValidateBrush: all points lie on all planes (degenerate, no brush volume!)\n");
104 Collision_PrintBrushAsQHull(brush, "unnamed");
107 float nearestplanedist_float(const float *normal, const colpointf_t *points, int numpoints)
109 float dist, bestdist;
110 bestdist = DotProduct(points->v, normal);
114 dist = DotProduct(points->v, normal);
115 bestdist = min(bestdist, dist);
121 float furthestplanedist_float(const float *normal, const colpointf_t *points, int numpoints)
123 float dist, bestdist;
124 bestdist = DotProduct(points->v, normal);
128 dist = DotProduct(points->v, normal);
129 bestdist = max(bestdist, dist);
136 colbrushf_t *Collision_NewBrushFromPlanes(mempool_t *mempool, int numoriginalplanes, const colplanef_t *originalplanes)
138 // TODO: planesbuf could be replaced by a remapping table
140 int numpointsbuf = 0, maxpointsbuf = 256, numplanesbuf = 0, maxplanesbuf = 256, numelementsbuf = 0, maxelementsbuf = 256;
142 colpointf_t pointsbuf[256];
143 colplanef_t planesbuf[256];
144 int elementsbuf[1024];
145 int polypointbuf[256];
150 // enable these if debugging to avoid seeing garbage in unused data
151 memset(pointsbuf, 0, sizeof(pointsbuf));
152 memset(planesbuf, 0, sizeof(planesbuf));
153 memset(elementsbuf, 0, sizeof(elementsbuf));
154 memset(polypointbuf, 0, sizeof(polypointbuf));
155 memset(p, 0, sizeof(p));
157 // construct a collision brush (points, planes, and renderable mesh) from
158 // a set of planes, this also optimizes out any unnecessary planes (ones
159 // whose polygon is clipped away by the other planes)
160 for (j = 0;j < numoriginalplanes;j++)
162 // add the plane uniquely (no duplicates)
163 for (k = 0;k < numplanesbuf;k++)
164 if (VectorCompare(planesbuf[k].normal, originalplanes[j].normal) && planesbuf[k].dist == originalplanes[j].dist)
166 // if the plane is a duplicate, skip it
167 if (k < numplanesbuf)
169 // check if there are too many and skip the brush
170 if (numplanesbuf >= maxplanesbuf)
172 Con_Print("Collision_NewBrushFromPlanes: failed to build collision brush: too many planes for buffer\n");
176 // create a large polygon from the plane
178 PolygonD_QuadForPlane(p[w], originalplanes[j].normal[0], originalplanes[j].normal[1], originalplanes[j].normal[2], originalplanes[j].dist, 1024.0*1024.0*1024.0);
180 // clip it by all other planes
181 for (k = 0;k < numoriginalplanes && pnumpoints && pnumpoints <= pmaxpoints;k++)
185 // we want to keep the inside of the brush plane so we flip
187 PolygonD_Divide(pnumpoints, p[w], -originalplanes[k].normal[0], -originalplanes[k].normal[1], -originalplanes[k].normal[2], -originalplanes[k].dist, 1.0/32.0, pmaxpoints, p[!w], &pnumpoints, 0, NULL, NULL, NULL);
191 // if nothing is left, skip it
194 //Con_Printf("Collision_NewBrushFromPlanes: warning: polygon for plane %f %f %f %f clipped away\n", originalplanes[j].normal[0], originalplanes[j].normal[1], originalplanes[j].normal[2], originalplanes[j].dist);
198 for (k = 0;k < pnumpoints;k++)
202 for (l = 0;l < numoriginalplanes;l++)
203 if (fabs(DotProduct(&p[w][k*3], originalplanes[l].normal) - originalplanes[l].dist) < 1.0/8.0)
210 Con_Printf("Collision_NewBrushFromPlanes: warning: polygon point does not lie on at least 3 planes\n");
214 // check if there are too many polygon vertices for buffer
215 if (pnumpoints > pmaxpoints)
217 Con_Print("Collision_NewBrushFromPlanes: failed to build collision brush: too many points for buffer\n");
221 // check if there are too many triangle elements for buffer
222 if (numelementsbuf + (pnumpoints - 2) * 3 > maxelementsbuf)
224 Con_Print("Collision_NewBrushFromPlanes: failed to build collision brush: too many triangle elements for buffer\n");
228 for (k = 0;k < pnumpoints;k++)
230 // check if there is already a matching point (no duplicates)
231 for (m = 0;m < numpointsbuf;m++)
232 if (VectorDistance2(&p[w][k*3], pointsbuf[m].v) < COLLISION_SNAP)
235 // if there is no match, add a new one
236 if (m == numpointsbuf)
238 // check if there are too many and skip the brush
239 if (numpointsbuf >= maxpointsbuf)
241 Con_Print("Collision_NewBrushFromPlanes: failed to build collision brush: too many points for buffer\n");
245 VectorCopy(&p[w][k*3], pointsbuf[numpointsbuf].v);
249 // store the index into a buffer
253 // add the triangles for the polygon
254 // (this particular code makes a triangle fan)
255 for (k = 0;k < pnumpoints - 2;k++)
257 elementsbuf[numelementsbuf++] = polypointbuf[0];
258 elementsbuf[numelementsbuf++] = polypointbuf[k + 1];
259 elementsbuf[numelementsbuf++] = polypointbuf[k + 2];
263 VectorCopy(originalplanes[j].normal, planesbuf[numplanesbuf].normal);
264 planesbuf[numplanesbuf].dist = originalplanes[j].dist;
265 planesbuf[numplanesbuf].supercontents = originalplanes[j].supercontents;
266 planesbuf[numplanesbuf].q3surfaceflags = originalplanes[j].q3surfaceflags;
267 planesbuf[numplanesbuf].texture = originalplanes[j].texture;
271 // validate plane distances
272 for (j = 0;j < numplanesbuf;j++)
274 float d = furthestplanedist_float(planesbuf[j].normal, pointsbuf, numpointsbuf);
275 if (fabs(planesbuf[j].dist - d) > (1.0f/32.0f))
276 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);
279 // if nothing is left, there's nothing to allocate
280 if (numelementsbuf < 12 || numplanesbuf < 4 || numpointsbuf < 4)
282 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);
286 // allocate the brush and copy to it
287 brush = (colbrushf_t *)Mem_Alloc(mempool, sizeof(colbrushf_t) + sizeof(colpointf_t) * numpointsbuf + sizeof(colplanef_t) * numplanesbuf + sizeof(int) * numelementsbuf);
288 brush->numplanes = numplanesbuf;
289 brush->numpoints = numpointsbuf;
290 brush->numtriangles = numelementsbuf / 3;
291 brush->planes = (colplanef_t *)(brush + 1);
292 brush->points = (colpointf_t *)(brush->planes + brush->numplanes);
293 brush->elements = (int *)(brush->points + brush->numpoints);
294 for (j = 0;j < brush->numpoints;j++)
296 brush->points[j].v[0] = pointsbuf[j].v[0];
297 brush->points[j].v[1] = pointsbuf[j].v[1];
298 brush->points[j].v[2] = pointsbuf[j].v[2];
300 for (j = 0;j < brush->numplanes;j++)
302 brush->planes[j].normal[0] = planesbuf[j].normal[0];
303 brush->planes[j].normal[1] = planesbuf[j].normal[1];
304 brush->planes[j].normal[2] = planesbuf[j].normal[2];
305 brush->planes[j].dist = planesbuf[j].dist;
306 brush->planes[j].supercontents = planesbuf[j].supercontents;
307 brush->planes[j].q3surfaceflags = planesbuf[j].q3surfaceflags;
308 brush->planes[j].texture = planesbuf[j].texture;
309 brush->supercontents |= brush->planes[j].supercontents;
311 for (j = 0;j < brush->numtriangles * 3;j++)
312 brush->elements[j] = elementsbuf[j];
313 VectorCopy(brush->points[0].v, brush->mins);
314 VectorCopy(brush->points[0].v, brush->maxs);
315 for (j = 1;j < brush->numpoints;j++)
317 brush->mins[0] = min(brush->mins[0], brush->points[j].v[0]);
318 brush->mins[1] = min(brush->mins[1], brush->points[j].v[1]);
319 brush->mins[2] = min(brush->mins[2], brush->points[j].v[2]);
320 brush->maxs[0] = max(brush->maxs[0], brush->points[j].v[0]);
321 brush->maxs[1] = max(brush->maxs[1], brush->points[j].v[1]);
322 brush->maxs[2] = max(brush->maxs[2], brush->points[j].v[2]);
330 Collision_ValidateBrush(brush);
336 void Collision_CalcPlanesForPolygonBrushFloat(colbrushf_t *brush)
339 float edge0[3], edge1[3], edge2[3], normal[3], dist, bestdist;
342 // FIXME: these probably don't actually need to be normalized if the collision code does not care
343 if (brush->numpoints == 3)
345 // optimized triangle case
346 TriangleNormal(brush->points[0].v, brush->points[1].v, brush->points[2].v, brush->planes[0].normal);
347 if (DotProduct(brush->planes[0].normal, brush->planes[0].normal) < 0.0001f)
349 // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
350 brush->numplanes = 0;
355 brush->numplanes = 5;
356 VectorNormalize(brush->planes[0].normal);
357 brush->planes[0].dist = DotProduct(brush->points->v, brush->planes[0].normal);
358 VectorNegate(brush->planes[0].normal, brush->planes[1].normal);
359 brush->planes[1].dist = -brush->planes[0].dist;
360 VectorSubtract(brush->points[2].v, brush->points[0].v, edge0);
361 VectorSubtract(brush->points[0].v, brush->points[1].v, edge1);
362 VectorSubtract(brush->points[1].v, brush->points[2].v, edge2);
365 float projectionnormal[3], projectionedge0[3], projectionedge1[3], projectionedge2[3];
367 float dist, bestdist;
368 bestdist = fabs(brush->planes[0].normal[0]);
370 for (i = 1;i < 3;i++)
372 dist = fabs(brush->planes[0].normal[i]);
379 VectorClear(projectionnormal);
380 if (brush->planes[0].normal[best] < 0)
381 projectionnormal[best] = -1;
383 projectionnormal[best] = 1;
384 VectorCopy(edge0, projectionedge0);
385 VectorCopy(edge1, projectionedge1);
386 VectorCopy(edge2, projectionedge2);
387 projectionedge0[best] = 0;
388 projectionedge1[best] = 0;
389 projectionedge2[best] = 0;
390 CrossProduct(projectionedge0, projectionnormal, brush->planes[2].normal);
391 CrossProduct(projectionedge1, projectionnormal, brush->planes[3].normal);
392 CrossProduct(projectionedge2, projectionnormal, brush->planes[4].normal);
395 CrossProduct(edge0, brush->planes->normal, brush->planes[2].normal);
396 CrossProduct(edge1, brush->planes->normal, brush->planes[3].normal);
397 CrossProduct(edge2, brush->planes->normal, brush->planes[4].normal);
399 VectorNormalize(brush->planes[2].normal);
400 VectorNormalize(brush->planes[3].normal);
401 VectorNormalize(brush->planes[4].normal);
402 brush->planes[2].dist = DotProduct(brush->points[2].v, brush->planes[2].normal);
403 brush->planes[3].dist = DotProduct(brush->points[0].v, brush->planes[3].normal);
404 brush->planes[4].dist = DotProduct(brush->points[1].v, brush->planes[4].normal);
406 if (developer.integer >= 100)
412 VectorSubtract(brush->points[0].v, brush->points[1].v, edge0);
413 VectorSubtract(brush->points[2].v, brush->points[1].v, edge1);
414 CrossProduct(edge0, edge1, normal);
415 VectorNormalize(normal);
416 VectorSubtract(normal, brush->planes[0].normal, temp);
417 if (VectorLength(temp) > 0.01f)
418 Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: TriangleNormal gave wrong answer (%f %f %f != correct answer %f %f %f)\n", brush->planes->normal[0], brush->planes->normal[1], brush->planes->normal[2], normal[0], normal[1], normal[2]);
419 if (fabs(DotProduct(brush->planes[1].normal, brush->planes[0].normal) - -1.0f) > 0.01f || fabs(brush->planes[1].dist - -brush->planes[0].dist) > 0.01f)
420 Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: plane 1 (%f %f %f %f) is not opposite plane 0 (%f %f %f %f)\n", brush->planes[1].normal[0], brush->planes[1].normal[1], brush->planes[1].normal[2], brush->planes[1].dist, brush->planes[0].normal[0], brush->planes[0].normal[1], brush->planes[0].normal[2], brush->planes[0].dist);
422 if (fabs(DotProduct(brush->planes[2].normal, brush->planes[0].normal)) > 0.01f)
423 Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: plane 2 (%f %f %f %f) is not perpendicular to plane 0 (%f %f %f %f)\n", brush->planes[2].normal[0], brush->planes[2].normal[1], brush->planes[2].normal[2], brush->planes[2].dist, brush->planes[0].normal[0], brush->planes[0].normal[1], brush->planes[0].normal[2], brush->planes[2].dist);
424 if (fabs(DotProduct(brush->planes[3].normal, brush->planes[0].normal)) > 0.01f)
425 Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: plane 3 (%f %f %f %f) is not perpendicular to plane 0 (%f %f %f %f)\n", brush->planes[3].normal[0], brush->planes[3].normal[1], brush->planes[3].normal[2], brush->planes[3].dist, brush->planes[0].normal[0], brush->planes[0].normal[1], brush->planes[0].normal[2], brush->planes[3].dist);
426 if (fabs(DotProduct(brush->planes[4].normal, brush->planes[0].normal)) > 0.01f)
427 Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: plane 4 (%f %f %f %f) is not perpendicular to plane 0 (%f %f %f %f)\n", brush->planes[4].normal[0], brush->planes[4].normal[1], brush->planes[4].normal[2], brush->planes[4].dist, brush->planes[0].normal[0], brush->planes[0].normal[1], brush->planes[0].normal[2], brush->planes[4].dist);
428 if (fabs(DotProduct(brush->planes[2].normal, edge0)) > 0.01f)
429 Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: plane 2 (%f %f %f %f) is not perpendicular to edge 0 (%f %f %f to %f %f %f)\n", brush->planes[2].normal[0], brush->planes[2].normal[1], brush->planes[2].normal[2], brush->planes[2].dist, brush->points[2].v[0], brush->points[2].v[1], brush->points[2].v[2], brush->points[0].v[0], brush->points[0].v[1], brush->points[0].v[2]);
430 if (fabs(DotProduct(brush->planes[3].normal, edge1)) > 0.01f)
431 Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: plane 3 (%f %f %f %f) is not perpendicular to edge 1 (%f %f %f to %f %f %f)\n", brush->planes[3].normal[0], brush->planes[3].normal[1], brush->planes[3].normal[2], brush->planes[3].dist, brush->points[0].v[0], brush->points[0].v[1], brush->points[0].v[2], brush->points[1].v[0], brush->points[1].v[1], brush->points[1].v[2]);
432 if (fabs(DotProduct(brush->planes[4].normal, edge2)) > 0.01f)
433 Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: plane 4 (%f %f %f %f) is not perpendicular to edge 2 (%f %f %f to %f %f %f)\n", brush->planes[4].normal[0], brush->planes[4].normal[1], brush->planes[4].normal[2], brush->planes[4].dist, brush->points[1].v[0], brush->points[1].v[1], brush->points[1].v[2], brush->points[2].v[0], brush->points[2].v[1], brush->points[2].v[2]);
436 if (fabs(DotProduct(brush->points[0].v, brush->planes[0].normal) - brush->planes[0].dist) > 0.01f || fabs(DotProduct(brush->points[1].v, brush->planes[0].normal) - brush->planes[0].dist) > 0.01f || fabs(DotProduct(brush->points[2].v, brush->planes[0].normal) - brush->planes[0].dist) > 0.01f)
437 Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: edges (%f %f %f to %f %f %f to %f %f %f) off front plane 0 (%f %f %f %f)\n", brush->points[0].v[0], brush->points[0].v[1], brush->points[0].v[2], brush->points[1].v[0], brush->points[1].v[1], brush->points[1].v[2], brush->points[2].v[0], brush->points[2].v[1], brush->points[2].v[2], brush->planes[0].normal[0], brush->planes[0].normal[1], brush->planes[0].normal[2], brush->planes[0].dist);
438 if (fabs(DotProduct(brush->points[0].v, brush->planes[1].normal) - brush->planes[1].dist) > 0.01f || fabs(DotProduct(brush->points[1].v, brush->planes[1].normal) - brush->planes[1].dist) > 0.01f || fabs(DotProduct(brush->points[2].v, brush->planes[1].normal) - brush->planes[1].dist) > 0.01f)
439 Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: edges (%f %f %f to %f %f %f to %f %f %f) off back plane 1 (%f %f %f %f)\n", brush->points[0].v[0], brush->points[0].v[1], brush->points[0].v[2], brush->points[1].v[0], brush->points[1].v[1], brush->points[1].v[2], brush->points[2].v[0], brush->points[2].v[1], brush->points[2].v[2], brush->planes[1].normal[0], brush->planes[1].normal[1], brush->planes[1].normal[2], brush->planes[1].dist);
440 if (fabs(DotProduct(brush->points[2].v, brush->planes[2].normal) - brush->planes[2].dist) > 0.01f || fabs(DotProduct(brush->points[0].v, brush->planes[2].normal) - brush->planes[2].dist) > 0.01f)
441 Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: edge 0 (%f %f %f to %f %f %f) off front plane 2 (%f %f %f %f)\n", brush->points[2].v[0], brush->points[2].v[1], brush->points[2].v[2], brush->points[0].v[0], brush->points[0].v[1], brush->points[0].v[2], brush->planes[2].normal[0], brush->planes[2].normal[1], brush->planes[2].normal[2], brush->planes[2].dist);
442 if (fabs(DotProduct(brush->points[0].v, brush->planes[3].normal) - brush->planes[3].dist) > 0.01f || fabs(DotProduct(brush->points[1].v, brush->planes[3].normal) - brush->planes[3].dist) > 0.01f)
443 Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: edge 0 (%f %f %f to %f %f %f) off front plane 2 (%f %f %f %f)\n", brush->points[0].v[0], brush->points[0].v[1], brush->points[0].v[2], brush->points[1].v[0], brush->points[1].v[1], brush->points[1].v[2], brush->planes[3].normal[0], brush->planes[3].normal[1], brush->planes[3].normal[2], brush->planes[3].dist);
444 if (fabs(DotProduct(brush->points[1].v, brush->planes[4].normal) - brush->planes[4].dist) > 0.01f || fabs(DotProduct(brush->points[2].v, brush->planes[4].normal) - brush->planes[4].dist) > 0.01f)
445 Con_Printf("Collision_CalcPlanesForPolygonBrushFloat: edge 0 (%f %f %f to %f %f %f) off front plane 2 (%f %f %f %f)\n", brush->points[1].v[0], brush->points[1].v[1], brush->points[1].v[2], brush->points[2].v[0], brush->points[2].v[1], brush->points[2].v[2], brush->planes[4].normal[0], brush->planes[4].normal[1], brush->planes[4].normal[2], brush->planes[4].dist);
451 // choose best surface normal for polygon's plane
453 for (i = 0, p = brush->points + 1;i < brush->numpoints - 2;i++, p++)
455 VectorSubtract(p[-1].v, p[0].v, edge0);
456 VectorSubtract(p[1].v, p[0].v, edge1);
457 CrossProduct(edge0, edge1, normal);
458 //TriangleNormal(p[-1].v, p[0].v, p[1].v, normal);
459 dist = DotProduct(normal, normal);
460 if (i == 0 || bestdist < dist)
463 VectorCopy(normal, brush->planes->normal);
466 if (bestdist < 0.0001f)
468 // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
469 brush->numplanes = 0;
474 brush->numplanes = brush->numpoints + 2;
475 VectorNormalize(brush->planes->normal);
476 brush->planes->dist = DotProduct(brush->points->v, brush->planes->normal);
478 // negate plane to create other side
479 VectorNegate(brush->planes[0].normal, brush->planes[1].normal);
480 brush->planes[1].dist = -brush->planes[0].dist;
481 for (i = 0, p = brush->points + (brush->numpoints - 1), p2 = brush->points;i < brush->numpoints;i++, p = p2, p2++)
483 VectorSubtract(p->v, p2->v, edge0);
484 CrossProduct(edge0, brush->planes->normal, brush->planes[i + 2].normal);
485 VectorNormalize(brush->planes[i + 2].normal);
486 brush->planes[i + 2].dist = DotProduct(p->v, brush->planes[i + 2].normal);
491 if (developer.integer >= 100)
493 // validity check - will be disabled later
494 Collision_ValidateBrush(brush);
495 for (i = 0;i < brush->numplanes;i++)
498 for (j = 0, p = brush->points;j < brush->numpoints;j++, p++)
499 if (DotProduct(p->v, brush->planes[i].normal) > brush->planes[i].dist + (1.0 / 32.0))
500 Con_Printf("Error in brush plane generation, plane %i\n", i);
505 colbrushf_t *Collision_AllocBrushFromPermanentPolygonFloat(mempool_t *mempool, int numpoints, float *points, int supercontents)
508 brush = (colbrushf_t *)Mem_Alloc(mempool, sizeof(colbrushf_t) + sizeof(colplanef_t) * (numpoints + 2));
509 brush->supercontents = supercontents;
510 brush->numpoints = numpoints;
511 brush->numplanes = numpoints + 2;
512 brush->planes = (colplanef_t *)(brush + 1);
513 brush->points = (colpointf_t *)points;
514 Sys_Error("Collision_AllocBrushFromPermanentPolygonFloat: FIXME: this code needs to be updated to generate a mesh...");
518 // NOTE: start and end of each brush pair must have same numplanes/numpoints
519 void Collision_TraceBrushBrushFloat(trace_t *trace, const colbrushf_t *thisbrush_start, const colbrushf_t *thisbrush_end, const colbrushf_t *thatbrush_start, const colbrushf_t *thatbrush_end)
521 int nplane, nplane2, fstartsolid = true, fendsolid = true, brushsolid, hitsupercontents = 0, hitq3surfaceflags = 0;
522 float enterfrac = -1, leavefrac = 1, d1, d2, f, imove, newimpactnormal[3], enterfrac2 = -1;
523 const colplanef_t *startplane, *endplane;
524 texture_t *hittexture = NULL;
526 VectorClear(newimpactnormal);
528 for (nplane = 0;nplane < thatbrush_start->numplanes + thisbrush_start->numplanes;nplane++)
531 if (nplane2 >= thatbrush_start->numplanes)
533 nplane2 -= thatbrush_start->numplanes;
534 startplane = thisbrush_start->planes + nplane2;
535 endplane = thisbrush_end->planes + nplane2;
536 if (developer.integer >= 100)
538 // any brush with degenerate planes is not worth handling
539 if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
541 Con_Print("Collision_TraceBrushBrushFloat: degenerate thisbrush plane!\n");
544 f = furthestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints);
545 if (fabs(f - startplane->dist) > 0.125f)
546 Con_Printf("startplane->dist %f != calculated %f (thisbrush_start)\n", startplane->dist, f);
548 d1 = nearestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints) - furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints) - collision_startnudge.value;
549 d2 = nearestplanedist_float(endplane->normal, thisbrush_end->points, thisbrush_end->numpoints) - furthestplanedist_float(endplane->normal, thatbrush_end->points, thatbrush_end->numpoints) - collision_endnudge.value;
553 startplane = thatbrush_start->planes + nplane2;
554 endplane = thatbrush_end->planes + nplane2;
555 if (developer.integer >= 100)
557 // any brush with degenerate planes is not worth handling
558 if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
560 Con_Print("Collision_TraceBrushBrushFloat: degenerate thatbrush plane!\n");
563 f = furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints);
564 if (fabs(f - startplane->dist) > 0.125f)
565 Con_Printf("startplane->dist %f != calculated %f (thatbrush_start)\n", startplane->dist, f);
567 d1 = nearestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints) - startplane->dist - collision_startnudge.value;
568 d2 = nearestplanedist_float(endplane->normal, thisbrush_end->points, thisbrush_end->numpoints) - endplane->dist - collision_endnudge.value;
570 //Con_Printf("%c%i: d1 = %f, d2 = %f, d1 / (d1 - d2) = %f\n", nplane2 != nplane ? 'b' : 'a', nplane2, d1, d2, d1 / (d1 - d2));
581 imove = 1 / (d1 - d2);
582 f = (d1 - collision_enternudge.value) * imove;
586 enterfrac2 = f - collision_impactnudge.value * imove;
587 VectorLerp(startplane->normal, enterfrac, endplane->normal, newimpactnormal);
588 hitsupercontents = startplane->supercontents;
589 hitq3surfaceflags = startplane->q3surfaceflags;
590 hittexture = startplane->texture;
596 // moving out of brush
603 f = (d1 + collision_leavenudge.value) / (d1 - d2);
610 brushsolid = trace->hitsupercontentsmask & thatbrush_start->supercontents;
613 trace->startsupercontents |= thatbrush_start->supercontents;
616 trace->startsolid = true;
618 trace->allsolid = true;
622 // LordHavoc: we need an epsilon nudge here because for a point trace the
623 // penetrating line segment is normally zero length if this brush was
624 // generated from a polygon (infinitely thin), and could even be slightly
625 // positive or negative due to rounding errors in that case.
626 if (brushsolid && enterfrac > -1 && enterfrac < trace->realfraction && enterfrac - (1.0f / 1024.0f) <= leavefrac)
630 if (thatbrush_start->ispolygon)
632 d1 = nearestplanedist_float(thatbrush_start->planes[0].normal, thisbrush_start->points, thisbrush_start->numpoints) - thatbrush_start->planes[0].dist - collision_startnudge.value;
633 d2 = nearestplanedist_float(thatbrush_end->planes[0].normal, thisbrush_end->points, thisbrush_end->numpoints) - thatbrush_end->planes[0].dist - collision_endnudge.value;
635 if (move <= 0 || d2 > collision_enternudge.value || d1 < 0)
639 enterfrac = (d1 - collision_enternudge.value) * imove;
640 if (enterfrac < trace->realfraction)
642 enterfrac2 = enterfrac - collision_impactnudge.value * imove;
643 trace->hitsupercontents = thatbrush_start->planes[0].supercontents;
644 trace->hitq3surfaceflags = thatbrush_start->planes[0].q3surfaceflags;
645 trace->hittexture = thatbrush_start->planes[0].texture;
646 trace->realfraction = bound(0, enterfrac, 1);
647 trace->fraction = bound(0, enterfrac2, 1);
648 VectorLerp(thatbrush_start->planes[0].normal, enterfrac, thatbrush_end->planes[0].normal, trace->plane.normal);
654 trace->hitsupercontents = hitsupercontents;
655 trace->hitq3surfaceflags = hitq3surfaceflags;
656 trace->hittexture = hittexture;
657 trace->realfraction = bound(0, enterfrac, 1);
658 trace->fraction = bound(0, enterfrac2, 1);
659 VectorCopy(newimpactnormal, trace->plane.normal);
664 // NOTE: start and end brush pair must have same numplanes/numpoints
665 void Collision_TraceLineBrushFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, const colbrushf_t *thatbrush_start, const colbrushf_t *thatbrush_end)
667 int nplane, fstartsolid = true, fendsolid = true, brushsolid, hitsupercontents = 0, hitq3surfaceflags = 0;
668 float enterfrac = -1, leavefrac = 1, d1, d2, f, imove, newimpactnormal[3], enterfrac2 = -1;
669 const colplanef_t *startplane, *endplane;
670 texture_t *hittexture = NULL;
672 VectorClear(newimpactnormal);
674 for (nplane = 0;nplane < thatbrush_start->numplanes;nplane++)
676 startplane = thatbrush_start->planes + nplane;
677 endplane = thatbrush_end->planes + nplane;
678 d1 = DotProduct(startplane->normal, linestart) - startplane->dist - collision_startnudge.value;
679 d2 = DotProduct(endplane->normal, lineend) - endplane->dist - collision_endnudge.value;
680 if (developer.integer >= 100)
682 // any brush with degenerate planes is not worth handling
683 if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
685 Con_Print("Collision_TraceLineBrushFloat: degenerate plane!\n");
688 if (thatbrush_start->numpoints)
690 f = furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints);
691 if (fabs(f - startplane->dist) > 0.125f)
692 Con_Printf("startplane->dist %f != calculated %f\n", startplane->dist, f);
705 imove = 1 / (d1 - d2);
706 f = (d1 - collision_enternudge.value) * imove;
710 enterfrac2 = f - collision_impactnudge.value * imove;
711 VectorLerp(startplane->normal, enterfrac, endplane->normal, newimpactnormal);
712 hitsupercontents = startplane->supercontents;
713 hitq3surfaceflags = startplane->q3surfaceflags;
714 hittexture = startplane->texture;
720 // moving out of brush
727 f = (d1 + collision_leavenudge.value) / (d1 - d2);
734 brushsolid = trace->hitsupercontentsmask & thatbrush_start->supercontents;
737 trace->startsupercontents |= thatbrush_start->supercontents;
740 trace->startsolid = true;
742 trace->allsolid = true;
746 // LordHavoc: we need an epsilon nudge here because for a point trace the
747 // penetrating line segment is normally zero length if this brush was
748 // generated from a polygon (infinitely thin), and could even be slightly
749 // positive or negative due to rounding errors in that case.
750 if (brushsolid && enterfrac > -1 && enterfrac < trace->realfraction && enterfrac <= leavefrac)
754 if (thatbrush_start->ispolygon)
756 d1 = DotProduct(thatbrush_start->planes[0].normal, linestart) - thatbrush_start->planes[0].dist - collision_startnudge.value;
757 d2 = DotProduct(thatbrush_end->planes[0].normal, lineend) - thatbrush_end->planes[0].dist - collision_endnudge.value;
759 if (move <= 0 || d2 > collision_enternudge.value || d1 < 0)
763 enterfrac = (d1 - collision_enternudge.value) * imove;
764 if (enterfrac < trace->realfraction)
766 enterfrac2 = enterfrac - collision_impactnudge.value * imove;
767 trace->hitsupercontents = hitsupercontents;
768 trace->hitq3surfaceflags = hitq3surfaceflags;
769 trace->hittexture = hittexture;
770 trace->realfraction = bound(0, enterfrac, 1);
771 trace->fraction = bound(0, enterfrac2, 1);
772 VectorLerp(thatbrush_start->planes[0].normal, enterfrac, thatbrush_end->planes[0].normal, trace->plane.normal);
778 trace->hitsupercontents = hitsupercontents;
779 trace->hitq3surfaceflags = hitq3surfaceflags;
780 trace->hittexture = hittexture;
781 trace->realfraction = bound(0, enterfrac, 1);
782 trace->fraction = bound(0, enterfrac2, 1);
783 VectorCopy(newimpactnormal, trace->plane.normal);
788 void Collision_TracePointBrushFloat(trace_t *trace, const vec3_t point, const colbrushf_t *thatbrush)
791 const colplanef_t *plane;
793 for (nplane = 0, plane = thatbrush->planes;nplane < thatbrush->numplanes;nplane++, plane++)
794 if (DotProduct(plane->normal, point) > plane->dist)
797 trace->startsupercontents |= thatbrush->supercontents;
798 if (trace->hitsupercontentsmask & thatbrush->supercontents)
800 trace->startsolid = true;
801 trace->allsolid = true;
805 static colpointf_t polyf_points[256];
806 static colplanef_t polyf_planes[256 + 2];
807 static colbrushf_t polyf_brush;
809 void Collision_SnapCopyPoints(int numpoints, const colpointf_t *in, colpointf_t *out, float fractionprecision, float invfractionprecision)
813 out->v[0] = floor(in->v[0] * fractionprecision + 0.5f) * invfractionprecision;
814 out->v[1] = floor(in->v[1] * fractionprecision + 0.5f) * invfractionprecision;
815 out->v[2] = floor(in->v[2] * fractionprecision + 0.5f) * invfractionprecision;
819 void Collision_TraceBrushPolygonFloat(trace_t *trace, const colbrushf_t *thisbrush_start, const colbrushf_t *thisbrush_end, int numpoints, const float *points, int supercontents)
823 Con_Print("Polygon with more than 256 points not supported yet (fixme!)\n");
826 polyf_brush.numpoints = numpoints;
827 polyf_brush.numplanes = numpoints + 2;
828 //polyf_brush.points = (colpointf_t *)points;
829 polyf_brush.planes = polyf_planes;
830 polyf_brush.supercontents = supercontents;
831 polyf_brush.points = polyf_points;
832 Collision_SnapCopyPoints(numpoints, (colpointf_t *)points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
833 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
834 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
835 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brush, &polyf_brush);
838 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)
841 float facemins[3], facemaxs[3];
842 polyf_brush.numpoints = 3;
843 polyf_brush.numplanes = 5;
844 polyf_brush.points = polyf_points;
845 polyf_brush.planes = polyf_planes;
846 polyf_brush.supercontents = supercontents;
847 for (i = 0;i < polyf_brush.numplanes;i++)
849 polyf_brush.planes[i].supercontents = supercontents;
850 polyf_brush.planes[i].q3surfaceflags = q3surfaceflags;
851 polyf_brush.planes[i].texture = texture;
853 for (i = 0;i < numtriangles;i++, element3i += 3)
855 VectorCopy(vertex3f + element3i[0] * 3, polyf_points[0].v);
856 VectorCopy(vertex3f + element3i[1] * 3, polyf_points[1].v);
857 VectorCopy(vertex3f + element3i[2] * 3, polyf_points[2].v);
858 Collision_SnapCopyPoints(3, polyf_points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
859 facemins[0] = min(polyf_points[0].v[0], min(polyf_points[1].v[0], polyf_points[2].v[0]));
860 facemins[1] = min(polyf_points[0].v[1], min(polyf_points[1].v[1], polyf_points[2].v[1]));
861 facemins[2] = min(polyf_points[0].v[2], min(polyf_points[1].v[2], polyf_points[2].v[2]));
862 facemaxs[0] = max(polyf_points[0].v[0], max(polyf_points[1].v[0], polyf_points[2].v[0]));
863 facemaxs[1] = max(polyf_points[0].v[1], max(polyf_points[1].v[1], polyf_points[2].v[1]));
864 facemaxs[2] = max(polyf_points[0].v[2], max(polyf_points[1].v[2], polyf_points[2].v[2]));
865 if (BoxesOverlap(segmentmins, segmentmaxs, facemins, facemaxs))
867 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
868 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
869 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brush, &polyf_brush);
874 void Collision_TraceLinePolygonFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, int numpoints, const float *points, int supercontents)
878 Con_Print("Polygon with more than 256 points not supported yet (fixme!)\n");
881 polyf_brush.numpoints = numpoints;
882 polyf_brush.numplanes = numpoints + 2;
883 //polyf_brush.points = (colpointf_t *)points;
884 polyf_brush.points = polyf_points;
885 Collision_SnapCopyPoints(numpoints, (colpointf_t *)points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
886 polyf_brush.planes = polyf_planes;
887 polyf_brush.supercontents = supercontents;
888 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
889 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
890 Collision_TraceLineBrushFloat(trace, linestart, lineend, &polyf_brush, &polyf_brush);
893 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)
897 // FIXME: snap vertices?
898 for (i = 0;i < numtriangles;i++, element3i += 3)
899 Collision_TraceLineTriangleFloat(trace, linestart, lineend, vertex3f + element3i[0] * 3, vertex3f + element3i[1] * 3, vertex3f + element3i[2] * 3, supercontents, q3surfaceflags, texture);
901 polyf_brush.numpoints = 3;
902 polyf_brush.numplanes = 5;
903 polyf_brush.points = polyf_points;
904 polyf_brush.planes = polyf_planes;
905 polyf_brush.supercontents = supercontents;
906 for (i = 0;i < polyf_brush.numplanes;i++)
908 polyf_brush.planes[i].supercontents = supercontents;
909 polyf_brush.planes[i].q3surfaceflags = q3surfaceflags;
910 polyf_brush.planes[i].texture = texture;
912 for (i = 0;i < numtriangles;i++, element3i += 3)
914 float facemins[3], facemaxs[3];
915 VectorCopy(vertex3f + element3i[0] * 3, polyf_points[0].v);
916 VectorCopy(vertex3f + element3i[1] * 3, polyf_points[1].v);
917 VectorCopy(vertex3f + element3i[2] * 3, polyf_points[2].v);
918 Collision_SnapCopyPoints(numpoints, polyf_points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
919 facemins[0] = min(polyf_points[0].v[0], min(polyf_points[1].v[0], polyf_points[2].v[0]));
920 facemins[1] = min(polyf_points[0].v[1], min(polyf_points[1].v[1], polyf_points[2].v[1]));
921 facemins[2] = min(polyf_points[0].v[2], min(polyf_points[1].v[2], polyf_points[2].v[2]));
922 facemaxs[0] = max(polyf_points[0].v[0], max(polyf_points[1].v[0], polyf_points[2].v[0]));
923 facemaxs[1] = max(polyf_points[0].v[1], max(polyf_points[1].v[1], polyf_points[2].v[1]));
924 facemaxs[2] = max(polyf_points[0].v[2], max(polyf_points[1].v[2], polyf_points[2].v[2]));
925 if (BoxesOverlap(segmentmins, segmentmaxs, facemins, facemaxs))
927 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
928 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
929 Collision_TraceLineBrushFloat(trace, linestart, lineend, &polyf_brush, &polyf_brush);
936 static colpointf_t polyf_pointsstart[256], polyf_pointsend[256];
937 static colplanef_t polyf_planesstart[256 + 2], polyf_planesend[256 + 2];
938 static colbrushf_t polyf_brushstart, polyf_brushend;
940 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)
945 Con_Print("Polygon with more than 256 points not supported yet (fixme!)\n");
948 polyf_brushstart.numpoints = numpoints;
949 polyf_brushstart.numplanes = numpoints + 2;
950 polyf_brushstart.points = polyf_pointsstart;//(colpointf_t *)points;
951 polyf_brushstart.planes = polyf_planesstart;
952 polyf_brushstart.supercontents = supercontents;
953 for (i = 0;i < numpoints;i++)
954 Matrix4x4_Transform(polygonmatrixstart, points + i * 3, polyf_brushstart.points[i].v);
955 polyf_brushend.numpoints = numpoints;
956 polyf_brushend.numplanes = numpoints + 2;
957 polyf_brushend.points = polyf_pointsend;//(colpointf_t *)points;
958 polyf_brushend.planes = polyf_planesend;
959 polyf_brushend.supercontents = supercontents;
960 for (i = 0;i < numpoints;i++)
961 Matrix4x4_Transform(polygonmatrixend, points + i * 3, polyf_brushend.points[i].v);
962 for (i = 0;i < polyf_brushstart.numplanes;i++)
964 polyf_brushstart.planes[i].supercontents = supercontents;
965 polyf_brushstart.planes[i].q3surfaceflags = q3surfaceflags;
966 polyf_brushstart.planes[i].texture = texture;
968 Collision_SnapCopyPoints(numpoints, polyf_pointsstart, polyf_pointsstart, COLLISION_SNAPSCALE, COLLISION_SNAP);
969 Collision_SnapCopyPoints(numpoints, polyf_pointsend, polyf_pointsend, COLLISION_SNAPSCALE, COLLISION_SNAP);
970 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brushstart);
971 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brushend);
973 //Collision_PrintBrushAsQHull(&polyf_brushstart, "polyf_brushstart");
974 //Collision_PrintBrushAsQHull(&polyf_brushend, "polyf_brushend");
976 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brushstart, &polyf_brushend);
981 #define MAX_BRUSHFORBOX 16
982 static int brushforbox_index = 0;
983 static colpointf_t brushforbox_point[MAX_BRUSHFORBOX*8];
984 static colplanef_t brushforbox_plane[MAX_BRUSHFORBOX*6];
985 static colbrushf_t brushforbox_brush[MAX_BRUSHFORBOX];
986 static colbrushf_t brushforpoint_brush[MAX_BRUSHFORBOX];
988 void Collision_InitBrushForBox(void)
991 for (i = 0;i < MAX_BRUSHFORBOX;i++)
993 brushforbox_brush[i].numpoints = 8;
994 brushforbox_brush[i].numplanes = 6;
995 brushforbox_brush[i].points = brushforbox_point + i * 8;
996 brushforbox_brush[i].planes = brushforbox_plane + i * 6;
997 brushforpoint_brush[i].numpoints = 1;
998 brushforpoint_brush[i].numplanes = 0;
999 brushforpoint_brush[i].points = brushforbox_point + i * 8;
1000 brushforpoint_brush[i].planes = brushforbox_plane + i * 6;
1004 colbrushf_t *Collision_BrushForBox(const matrix4x4_t *matrix, const vec3_t mins, const vec3_t maxs, int supercontents, int q3surfaceflags, texture_t *texture)
1009 if (brushforbox_brush[0].numpoints == 0)
1010 Collision_InitBrushForBox();
1011 // FIXME: these probably don't actually need to be normalized if the collision code does not care
1012 if (VectorCompare(mins, maxs))
1015 brush = brushforpoint_brush + ((brushforbox_index++) % MAX_BRUSHFORBOX);
1016 VectorCopy(mins, brush->points->v);
1020 brush = brushforbox_brush + ((brushforbox_index++) % MAX_BRUSHFORBOX);
1022 for (i = 0;i < 8;i++)
1024 v[0] = i & 1 ? maxs[0] : mins[0];
1025 v[1] = i & 2 ? maxs[1] : mins[1];
1026 v[2] = i & 4 ? maxs[2] : mins[2];
1027 Matrix4x4_Transform(matrix, v, brush->points[i].v);
1030 for (i = 0;i < 6;i++)
1033 v[i >> 1] = i & 1 ? 1 : -1;
1034 Matrix4x4_Transform3x3(matrix, v, brush->planes[i].normal);
1035 VectorNormalize(brush->planes[i].normal);
1038 brush->supercontents = supercontents;
1039 for (j = 0;j < brush->numplanes;j++)
1041 brush->planes[j].supercontents = supercontents;
1042 brush->planes[j].q3surfaceflags = q3surfaceflags;
1043 brush->planes[j].texture = texture;
1044 brush->planes[j].dist = furthestplanedist_float(brush->planes[j].normal, brush->points, brush->numpoints);
1046 VectorCopy(brush->points[0].v, brush->mins);
1047 VectorCopy(brush->points[0].v, brush->maxs);
1048 for (j = 1;j < brush->numpoints;j++)
1050 brush->mins[0] = min(brush->mins[0], brush->points[j].v[0]);
1051 brush->mins[1] = min(brush->mins[1], brush->points[j].v[1]);
1052 brush->mins[2] = min(brush->mins[2], brush->points[j].v[2]);
1053 brush->maxs[0] = max(brush->maxs[0], brush->points[j].v[0]);
1054 brush->maxs[1] = max(brush->maxs[1], brush->points[j].v[1]);
1055 brush->maxs[2] = max(brush->maxs[2], brush->points[j].v[2]);
1057 brush->mins[0] -= 1;
1058 brush->mins[1] -= 1;
1059 brush->mins[2] -= 1;
1060 brush->maxs[0] += 1;
1061 brush->maxs[1] += 1;
1062 brush->maxs[2] += 1;
1063 Collision_ValidateBrush(brush);
1067 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)
1069 colbrushf_t *boxbrush, *thisbrush_start, *thisbrush_end;
1070 vec3_t startmins, startmaxs, endmins, endmaxs;
1072 // create brushes for the collision
1073 VectorAdd(start, mins, startmins);
1074 VectorAdd(start, maxs, startmaxs);
1075 VectorAdd(end, mins, endmins);
1076 VectorAdd(end, maxs, endmaxs);
1077 boxbrush = Collision_BrushForBox(&identitymatrix, cmins, cmaxs, supercontents, q3surfaceflags, texture);
1078 thisbrush_start = Collision_BrushForBox(&identitymatrix, startmins, startmaxs, 0, 0, NULL);
1079 thisbrush_end = Collision_BrushForBox(&identitymatrix, endmins, endmaxs, 0, 0, NULL);
1081 memset(trace, 0, sizeof(trace_t));
1082 trace->hitsupercontentsmask = hitsupercontentsmask;
1083 trace->fraction = 1;
1084 trace->realfraction = 1;
1085 trace->allsolid = true;
1086 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, boxbrush, boxbrush);
1089 //pseudocode for detecting line/sphere overlap without calculating an impact point
1090 //linesphereorigin = sphereorigin - linestart;linediff = lineend - linestart;linespherefrac = DotProduct(linesphereorigin, linediff) / DotProduct(linediff, linediff);return VectorLength2(linesphereorigin - bound(0, linespherefrac, 1) * linediff) >= sphereradius*sphereradius;
1092 // LordHavoc: currently unused, but tested
1093 // note: this can be used for tracing a moving sphere vs a stationary sphere,
1094 // by simply adding the moving sphere's radius to the sphereradius parameter,
1095 // all the results are correct (impactpoint, impactnormal, and fraction)
1096 float Collision_ClipTrace_Line_Sphere(double *linestart, double *lineend, double *sphereorigin, double sphereradius, double *impactpoint, double *impactnormal)
1098 double dir[3], scale, v[3], deviationdist, impactdist, linelength;
1099 // make sure the impactpoint and impactnormal are valid even if there is
1101 VectorCopy(lineend, impactpoint);
1102 VectorClear(impactnormal);
1103 // calculate line direction
1104 VectorSubtract(lineend, linestart, dir);
1105 // normalize direction
1106 linelength = VectorLength(dir);
1109 scale = 1.0 / linelength;
1110 VectorScale(dir, scale, dir);
1112 // this dotproduct calculates the distance along the line at which the
1113 // sphere origin is (nearest point to the sphere origin on the line)
1114 impactdist = DotProduct(sphereorigin, dir) - DotProduct(linestart, dir);
1115 // calculate point on line at that distance, and subtract the
1116 // sphereorigin from it, so we have a vector to measure for the distance
1117 // of the line from the sphereorigin (deviation, how off-center it is)
1118 VectorMA(linestart, impactdist, dir, v);
1119 VectorSubtract(v, sphereorigin, v);
1120 deviationdist = VectorLength2(v);
1121 // if outside the radius, it's a miss for sure
1122 // (we do this comparison using squared radius to avoid a sqrt)
1123 if (deviationdist > sphereradius*sphereradius)
1124 return 1; // miss (off to the side)
1125 // nudge back to find the correct impact distance
1126 impactdist += deviationdist - sphereradius;
1127 if (impactdist >= linelength)
1128 return 1; // miss (not close enough)
1130 return 1; // miss (linestart is past or inside sphere)
1131 // calculate new impactpoint
1132 VectorMA(linestart, impactdist, dir, impactpoint);
1133 // calculate impactnormal (surface normal at point of impact)
1134 VectorSubtract(impactpoint, sphereorigin, impactnormal);
1135 // normalize impactnormal
1136 VectorNormalize(impactnormal);
1137 // return fraction of movement distance
1138 return impactdist / linelength;
1141 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)
1145 float d1, d2, d, f, impact[3], edgenormal[3], faceplanenormal[3], faceplanedist, faceplanenormallength2, edge01[3], edge21[3], edge02[3];
1147 // this function executes:
1148 // 32 ops when line starts behind triangle
1149 // 38 ops when line ends infront of triangle
1150 // 43 ops when line fraction is already closer than this triangle
1151 // 72 ops when line is outside edge 01
1152 // 92 ops when line is outside edge 21
1153 // 115 ops when line is outside edge 02
1154 // 123 ops when line impacts triangle and updates trace results
1156 // this code is designed for clockwise triangles, conversion to
1157 // counterclockwise would require swapping some things around...
1158 // it is easier to simply swap the point0 and point2 parameters to this
1159 // function when calling it than it is to rewire the internals.
1161 // calculate the faceplanenormal of the triangle, this represents the front side
1163 VectorSubtract(point0, point1, edge01);
1164 VectorSubtract(point2, point1, edge21);
1165 CrossProduct(edge01, edge21, faceplanenormal);
1166 // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
1168 faceplanenormallength2 = DotProduct(faceplanenormal, faceplanenormal);
1169 if (faceplanenormallength2 < 0.0001f)
1171 // calculate the distance
1173 faceplanedist = DotProduct(point0, faceplanenormal);
1175 // if start point is on the back side there is no collision
1176 // (we don't care about traces going through the triangle the wrong way)
1178 // calculate the start distance
1180 d1 = DotProduct(faceplanenormal, linestart);
1181 if (d1 <= faceplanedist)
1184 // calculate the end distance
1186 d2 = DotProduct(faceplanenormal, lineend);
1187 // if both are in front, there is no collision
1188 if (d2 >= faceplanedist)
1191 // from here on we know d1 is >= 0 and d2 is < 0
1192 // this means the line starts infront and ends behind, passing through it
1194 // calculate the recipricol of the distance delta,
1195 // so we can use it multiple times cheaply (instead of division)
1197 d = 1.0f / (d1 - d2);
1198 // calculate the impact fraction by taking the start distance (> 0)
1199 // and subtracting the face plane distance (this is the distance of the
1200 // triangle along that same normal)
1201 // then multiply by the recipricol distance delta
1203 f = (d1 - faceplanedist) * d;
1204 // skip out if this impact is further away than previous ones
1206 if (f > trace->realfraction)
1208 // calculate the perfect impact point for classification of insidedness
1210 impact[0] = linestart[0] + f * (lineend[0] - linestart[0]);
1211 impact[1] = linestart[1] + f * (lineend[1] - linestart[1]);
1212 impact[2] = linestart[2] + f * (lineend[2] - linestart[2]);
1214 // calculate the edge normal and reject if impact is outside triangle
1215 // (an edge normal faces away from the triangle, to get the desired normal
1216 // a crossproduct with the faceplanenormal is used, and because of the way
1217 // the insidedness comparison is written it does not need to be normalized)
1219 // first use the two edges from the triangle plane math
1220 // the other edge only gets calculated if the point survives that long
1223 CrossProduct(edge01, faceplanenormal, edgenormal);
1224 if (DotProduct(impact, edgenormal) > DotProduct(point1, edgenormal))
1228 CrossProduct(faceplanenormal, edge21, edgenormal);
1229 if (DotProduct(impact, edgenormal) > DotProduct(point2, edgenormal))
1233 VectorSubtract(point0, point2, edge02);
1234 CrossProduct(faceplanenormal, edge02, edgenormal);
1235 if (DotProduct(impact, edgenormal) > DotProduct(point0, edgenormal))
1240 // store the new trace fraction
1241 trace->realfraction = f;
1243 // calculate a nudged fraction to keep it out of the surface
1244 // (the main fraction remains perfect)
1245 trace->fraction = f - collision_impactnudge.value * d;
1247 // store the new trace plane (because collisions only happen from
1248 // the front this is always simply the triangle normal, never flipped)
1249 d = 1.0 / sqrt(faceplanenormallength2);
1250 VectorScale(faceplanenormal, d, trace->plane.normal);
1251 trace->plane.dist = faceplanedist * d;
1253 trace->hitsupercontents = supercontents;
1254 trace->hitq3surfaceflags = q3surfaceflags;
1255 trace->hittexture = texture;
1257 float d1, d2, d, f, fnudged, impact[3], edgenormal[3], faceplanenormal[3], faceplanedist, edge[3];
1259 // this code is designed for clockwise triangles, conversion to
1260 // counterclockwise would require swapping some things around...
1261 // it is easier to simply swap the point0 and point2 parameters to this
1262 // function when calling it than it is to rewire the internals.
1264 // calculate the unnormalized faceplanenormal of the triangle,
1265 // this represents the front side
1266 TriangleNormal(point0, point1, point2, faceplanenormal);
1267 // there's no point in processing a degenerate triangle
1268 // (GIGO - Garbage In, Garbage Out)
1269 if (DotProduct(faceplanenormal, faceplanenormal) < 0.0001f)
1271 // calculate the unnormalized distance
1272 faceplanedist = DotProduct(point0, faceplanenormal);
1274 // calculate the unnormalized start distance
1275 d1 = DotProduct(faceplanenormal, linestart) - faceplanedist;
1276 // if start point is on the back side there is no collision
1277 // (we don't care about traces going through the triangle the wrong way)
1281 // calculate the unnormalized end distance
1282 d2 = DotProduct(faceplanenormal, lineend) - faceplanedist;
1283 // if both are in front, there is no collision
1287 // from here on we know d1 is >= 0 and d2 is < 0
1288 // this means the line starts infront and ends behind, passing through it
1290 // calculate the recipricol of the distance delta,
1291 // so we can use it multiple times cheaply (instead of division)
1292 d = 1.0f / (d1 - d2);
1293 // calculate the impact fraction by taking the start distance (> 0)
1294 // and subtracting the face plane distance (this is the distance of the
1295 // triangle along that same normal)
1296 // then multiply by the recipricol distance delta
1298 // skip out if this impact is further away than previous ones
1299 if (f > trace->realfraction)
1301 // calculate the perfect impact point for classification of insidedness
1302 impact[0] = linestart[0] + f * (lineend[0] - linestart[0]);
1303 impact[1] = linestart[1] + f * (lineend[1] - linestart[1]);
1304 impact[2] = linestart[2] + f * (lineend[2] - linestart[2]);
1306 // calculate the edge normal and reject if impact is outside triangle
1307 // (an edge normal faces away from the triangle, to get the desired normal
1308 // a crossproduct with the faceplanenormal is used, and because of the way
1309 // the insidedness comparison is written it does not need to be normalized)
1311 VectorSubtract(point2, point0, edge);
1312 CrossProduct(edge, faceplanenormal, edgenormal);
1313 if (DotProduct(impact, edgenormal) > DotProduct(point0, edgenormal))
1316 VectorSubtract(point0, point1, edge);
1317 CrossProduct(edge, faceplanenormal, edgenormal);
1318 if (DotProduct(impact, edgenormal) > DotProduct(point1, edgenormal))
1321 VectorSubtract(point1, point2, edge);
1322 CrossProduct(edge, faceplanenormal, edgenormal);
1323 if (DotProduct(impact, edgenormal) > DotProduct(point2, edgenormal))
1326 // store the new trace fraction
1327 trace->realfraction = bound(0, f, 1);
1329 // store the new trace plane (because collisions only happen from
1330 // the front this is always simply the triangle normal, never flipped)
1331 VectorNormalize(faceplanenormal);
1332 VectorCopy(faceplanenormal, trace->plane.normal);
1333 trace->plane.dist = DotProduct(point0, faceplanenormal);
1335 // calculate the normalized start and end distances
1336 d1 = DotProduct(trace->plane.normal, linestart) - trace->plane.dist;
1337 d2 = DotProduct(trace->plane.normal, lineend) - trace->plane.dist;
1339 // calculate a nudged fraction to keep it out of the surface
1340 // (the main fraction remains perfect)
1341 fnudged = (d1 - collision_impactnudge.value) / (d1 - d2);
1342 trace->fraction = bound(0, fnudged, 1);
1344 // store the new trace endpos
1345 // not needed, it's calculated later when the trace is finished
1346 //trace->endpos[0] = linestart[0] + fnudged * (lineend[0] - linestart[0]);
1347 //trace->endpos[1] = linestart[1] + fnudged * (lineend[1] - linestart[1]);
1348 //trace->endpos[2] = linestart[2] + fnudged * (lineend[2] - linestart[2]);
1349 trace->hitsupercontents = supercontents;
1350 trace->hitq3surfaceflags = q3surfaceflags;
1351 trace->hittexture = texture;
1355 typedef struct colbspnode_s
1358 struct colbspnode_s *children[2];
1359 // the node is reallocated or split if max is reached
1362 colbrushf_t **colbrushflist;
1365 //colbrushd_t **colbrushdlist;
1369 typedef struct colbsp_s
1372 colbspnode_t *nodes;
1376 colbsp_t *Collision_CreateCollisionBSP(mempool_t *mempool)
1379 bsp = (colbsp_t *)Mem_Alloc(mempool, sizeof(colbsp_t));
1380 bsp->mempool = mempool;
1381 bsp->nodes = (colbspnode_t *)Mem_Alloc(bsp->mempool, sizeof(colbspnode_t));
1385 void Collision_FreeCollisionBSPNode(colbspnode_t *node)
1387 if (node->children[0])
1388 Collision_FreeCollisionBSPNode(node->children[0]);
1389 if (node->children[1])
1390 Collision_FreeCollisionBSPNode(node->children[1]);
1391 while (--node->numcolbrushf)
1392 Mem_Free(node->colbrushflist[node->numcolbrushf]);
1393 //while (--node->numcolbrushd)
1394 // Mem_Free(node->colbrushdlist[node->numcolbrushd]);
1398 void Collision_FreeCollisionBSP(colbsp_t *bsp)
1400 Collision_FreeCollisionBSPNode(bsp->nodes);
1404 void Collision_BoundingBoxOfBrushTraceSegment(const colbrushf_t *start, const colbrushf_t *end, vec3_t mins, vec3_t maxs, float startfrac, float endfrac)
1407 colpointf_t *ps, *pe;
1408 float tempstart[3], tempend[3];
1409 VectorLerp(start->points[0].v, startfrac, end->points[0].v, mins);
1410 VectorCopy(mins, maxs);
1411 for (i = 0, ps = start->points, pe = end->points;i < start->numpoints;i++, ps++, pe++)
1413 VectorLerp(ps->v, startfrac, pe->v, tempstart);
1414 VectorLerp(ps->v, endfrac, pe->v, tempend);
1415 mins[0] = min(mins[0], min(tempstart[0], tempend[0]));
1416 mins[1] = min(mins[1], min(tempstart[1], tempend[1]));
1417 mins[2] = min(mins[2], min(tempstart[2], tempend[2]));
1418 maxs[0] = min(maxs[0], min(tempstart[0], tempend[0]));
1419 maxs[1] = min(maxs[1], min(tempstart[1], tempend[1]));
1420 maxs[2] = min(maxs[2], min(tempstart[2], tempend[2]));