6 // the hull we're tracing through
9 // the trace structure to fill in
12 // start and end of the trace (in model space)
19 RecursiveHullCheckTraceInfo_t;
21 // 1/32 epsilon to keep floating point happy
22 #define DIST_EPSILON (0.03125)
24 #define HULLCHECKSTATE_EMPTY 0
25 #define HULLCHECKSTATE_SOLID 1
26 #define HULLCHECKSTATE_DONE 2
28 static int RecursiveHullCheck(RecursiveHullCheckTraceInfo_t *t, int num, double p1f, double p2f, double p1[3], double p2[3])
30 // status variables, these don't need to be saved on the stack when
31 // recursing... but are because this should be thread-safe
32 // (note: tracing against a bbox is not thread-safe, yet)
37 // variables that need to be stored on the stack when recursing
42 // LordHavoc: a goto! everyone flee in terror... :)
47 t->trace->endcontents = num;
48 if (t->trace->thiscontents)
50 if (num == t->trace->thiscontents)
51 t->trace->allsolid = false;
54 // if the first leaf is solid, set startsolid
55 if (t->trace->allsolid)
56 t->trace->startsolid = true;
57 return HULLCHECKSTATE_SOLID;
59 return HULLCHECKSTATE_EMPTY;
63 if (num != CONTENTS_SOLID)
65 t->trace->allsolid = false;
66 if (num == CONTENTS_EMPTY)
67 t->trace->inopen = true;
69 t->trace->inwater = true;
73 // if the first leaf is solid, set startsolid
74 if (t->trace->allsolid)
75 t->trace->startsolid = true;
76 return HULLCHECKSTATE_SOLID;
78 return HULLCHECKSTATE_EMPTY;
82 // find the point distances
83 node = t->hull->clipnodes + num;
85 plane = t->hull->planes + node->planenum;
88 t1 = p1[plane->type] - plane->dist;
89 t2 = p2[plane->type] - plane->dist;
93 t1 = DotProduct (plane->normal, p1) - plane->dist;
94 t2 = DotProduct (plane->normal, p2) - plane->dist;
101 num = node->children[1];
110 num = node->children[0];
116 // the line intersects, find intersection point
117 // LordHavoc: this uses the original trace for maximum accuracy
120 t1 = t->start[plane->type] - plane->dist;
121 t2 = t->end[plane->type] - plane->dist;
125 t1 = DotProduct (plane->normal, t->start) - plane->dist;
126 t2 = DotProduct (plane->normal, t->end) - plane->dist;
129 midf = t1 / (t1 - t2);
130 midf = bound(p1f, midf, p2f);
131 VectorMA(t->start, midf, t->dist, mid);
133 // recurse both sides, front side first
134 ret = RecursiveHullCheck (t, node->children[side], p1f, midf, p1, mid);
135 // if this side is not empty, return what it is (solid or done)
136 if (ret != HULLCHECKSTATE_EMPTY)
139 ret = RecursiveHullCheck (t, node->children[side ^ 1], midf, p2f, mid, p2);
140 // if other side is not solid, return what it is (empty or done)
141 if (ret != HULLCHECKSTATE_SOLID)
144 // front is air and back is solid, this is the impact point...
147 t->trace->plane.dist = -plane->dist;
148 VectorNegate (plane->normal, t->trace->plane.normal);
152 t->trace->plane.dist = plane->dist;
153 VectorCopy (plane->normal, t->trace->plane.normal);
156 // bias away from surface a bit
157 t1 = DotProduct(t->trace->plane.normal, t->start) - (t->trace->plane.dist + DIST_EPSILON);
158 t2 = DotProduct(t->trace->plane.normal, t->end) - (t->trace->plane.dist + DIST_EPSILON);
160 midf = t1 / (t1 - t2);
161 t->trace->fraction = bound(0.0f, midf, 1.0);
163 VectorMA(t->start, t->trace->fraction, t->dist, t->trace->endpos);
165 return HULLCHECKSTATE_DONE;
169 // used if start and end are the same
170 static void RecursiveHullCheckPoint (RecursiveHullCheckTraceInfo_t *t, int num)
172 // If you can read this, you understand BSP trees
174 num = t->hull->clipnodes[num].children[((t->hull->planes[t->hull->clipnodes[num].planenum].type < 3) ? (t->start[t->hull->planes[t->hull->clipnodes[num].planenum].type]) : (DotProduct(t->hull->planes[t->hull->clipnodes[num].planenum].normal, t->start))) < t->hull->planes[t->hull->clipnodes[num].planenum].dist];
177 t->trace->endcontents = num;
178 if (t->trace->thiscontents)
180 if (num == t->trace->thiscontents)
181 t->trace->allsolid = false;
184 // if the first leaf is solid, set startsolid
185 if (t->trace->allsolid)
186 t->trace->startsolid = true;
191 if (num != CONTENTS_SOLID)
193 t->trace->allsolid = false;
194 if (num == CONTENTS_EMPTY)
195 t->trace->inopen = true;
197 t->trace->inwater = true;
201 // if the first leaf is solid, set startsolid
202 if (t->trace->allsolid)
203 t->trace->startsolid = true;
209 static hull_t box_hull;
210 static dclipnode_t box_clipnodes[6];
211 static mplane_t box_planes[6];
213 void Collision_Init (void)
218 //Set up the planes and clipnodes so that the six floats of a bounding box
219 //can just be stored out and get a proper hull_t structure.
221 box_hull.clipnodes = box_clipnodes;
222 box_hull.planes = box_planes;
223 box_hull.firstclipnode = 0;
224 box_hull.lastclipnode = 5;
226 for (i = 0;i < 6;i++)
228 box_clipnodes[i].planenum = i;
232 box_clipnodes[i].children[side] = CONTENTS_EMPTY;
234 box_clipnodes[i].children[side^1] = i + 1;
236 box_clipnodes[i].children[side^1] = CONTENTS_SOLID;
238 box_planes[i].type = i>>1;
239 box_planes[i].normal[i>>1] = 1;
243 void Collision_ClipTrace_Box(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)
245 RecursiveHullCheckTraceInfo_t rhc;
246 // fill in a default trace
247 memset(&rhc, 0, sizeof(rhc));
248 memset(trace, 0, sizeof(trace_t));
249 //To keep everything totally uniform, bounding boxes are turned into small
250 //BSP trees instead of being compared directly.
251 // create a temp hull from bounding box sizes
252 box_planes[0].dist = cmaxs[0] - mins[0];
253 box_planes[1].dist = cmins[0] - maxs[0];
254 box_planes[2].dist = cmaxs[1] - mins[1];
255 box_planes[3].dist = cmins[1] - maxs[1];
256 box_planes[4].dist = cmaxs[2] - mins[2];
257 box_planes[5].dist = cmins[2] - maxs[2];
258 // trace a line through the generated clipping hull
259 rhc.hull = &box_hull;
261 rhc.trace->fraction = 1;
262 rhc.trace->allsolid = true;
263 VectorCopy(start, rhc.start);
264 VectorCopy(end, rhc.end);
265 VectorSubtract(rhc.end, rhc.start, rhc.dist);
266 RecursiveHullCheck(&rhc, rhc.hull->firstclipnode, 0, 1, rhc.start, rhc.end);
283 void Collision_PrintBrushAsQHull(colbrushf_t *brush, const char *name)
286 Con_Printf("3 %s\n%i\n", name, brush->numpoints);
287 for (i = 0;i < brush->numpoints;i++)
288 Con_Printf("%g %g %g\n", brush->points[i].v[0], brush->points[i].v[1], brush->points[i].v[2]);
290 Con_Printf("4\n%i\n", brush->numplanes);
291 for (i = 0;i < brush->numplanes;i++)
292 Con_Printf("%g %g %g %g\n", brush->planes[i].normal[0], brush->planes[i].normal[1], brush->planes[i].normal[2], brush->planes[i].dist);
296 colbrushf_t *Collision_AllocBrushFloat(mempool_t *mempool, int numpoints, int numplanes)
299 brush = Mem_Alloc(mempool, sizeof(colbrushf_t) + sizeof(colpointf_t) * numpoints + sizeof(colplanef_t) * numplanes);
300 brush->numpoints = numpoints;
301 brush->numplanes = numplanes;
302 brush->planes = (void *)(brush + 1);
303 brush->points = (void *)(brush->planes + brush->numplanes);
307 void Collision_CalcPlanesForPolygonBrushFloat(colbrushf_t *brush)
310 float edge0[3], edge1[3], normal[3], dist, bestdist;
313 // choose best surface normal for polygon's plane
315 for (i = 0, p = brush->points + 1;i < brush->numpoints - 2;i++, p++)
317 VectorSubtract(p[-1].v, p[0].v, edge0);
318 VectorSubtract(p[1].v, p[0].v, edge1);
319 CrossProduct(edge0, edge1, normal);
320 dist = DotProduct(normal, normal);
321 if (i == 0 || bestdist < dist)
324 VectorCopy(normal, brush->planes->normal);
328 VectorNormalize(brush->planes->normal);
329 brush->planes->dist = DotProduct(brush->points->v, brush->planes->normal);
331 // negate plane to create other side
332 VectorNegate(brush->planes[0].normal, brush->planes[1].normal);
333 brush->planes[1].dist = -brush->planes[0].dist;
334 for (i = 0, p = brush->points + (brush->numpoints - 1), p2 = brush->points;i < brush->numpoints;i++, p = p2, p2++)
336 VectorSubtract(p->v, p2->v, edge0);
337 CrossProduct(edge0, brush->planes->normal, brush->planes[i + 2].normal);
338 VectorNormalize(brush->planes[i + 2].normal);
339 brush->planes[i + 2].dist = DotProduct(p->v, brush->planes[i + 2].normal);
343 // validity check - will be disabled later
344 for (i = 0;i < brush->numplanes;i++)
347 for (j = 0, p = brush->points;j < brush->numpoints;j++, p++)
348 if (DotProduct(p->v, brush->planes[i].normal) > brush->planes[i].dist + (1.0 / 32.0))
349 Con_Printf("Error in brush plane generation, plane %i\n", i);
354 colbrushf_t *Collision_AllocBrushFromPermanentPolygonFloat(mempool_t *mempool, int numpoints, float *points)
357 brush = Mem_Alloc(mempool, sizeof(colbrushf_t) + sizeof(colplanef_t) * (numpoints + 2));
358 brush->numpoints = numpoints;
359 brush->numplanes = numpoints + 2;
360 brush->planes = (void *)(brush + 1);
361 brush->points = (colpointf_t *)points;
365 float nearestplanedist_float(const float *normal, const colpointf_t *points, int numpoints)
367 float dist, bestdist;
368 bestdist = DotProduct(points->v, normal);
372 dist = DotProduct(points->v, normal);
380 float furthestplanedist_float(const float *normal, const colpointf_t *points, int numpoints)
382 float dist, bestdist;
383 bestdist = DotProduct(points->v, normal);
387 dist = DotProduct(points->v, normal);
395 #define COLLISIONEPSILON (1.0f / 32.0f)
396 #define COLLISIONEPSILON2 0//(1.0f / 32.0f)
398 // NOTE: start and end of each brush pair must have same numplanes/numpoints
399 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)
401 int nplane, nplane2, fstartsolid, fendsolid;
402 float enterfrac, leavefrac, d1, d2, f, newimpactnormal[3];
403 const colplanef_t *startplane, *endplane;
410 for (nplane = 0;nplane < thatbrush_start->numplanes + thisbrush_start->numplanes;nplane++)
413 if (nplane2 >= thatbrush_start->numplanes)
415 nplane2 -= thatbrush_start->numplanes;
416 startplane = thisbrush_start->planes + nplane2;
417 endplane = thisbrush_end->planes + nplane2;
421 startplane = thatbrush_start->planes + nplane2;
422 endplane = thatbrush_end->planes + nplane2;
424 d1 = nearestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints) - furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints);
425 d2 = nearestplanedist_float(endplane->normal, thisbrush_end->points, thisbrush_end->numpoints) - furthestplanedist_float(endplane->normal, thatbrush_end->points, thatbrush_end->numpoints) - COLLISIONEPSILON2;
426 //Con_Printf("%c%i: d1 = %f, d2 = %f, d1 / (d1 - d2) = %f\n", nplane2 != nplane ? 'b' : 'a', nplane2, d1, d2, d1 / (d1 - d2));
438 f = (d1 - COLLISIONEPSILON) / f;
443 VectorBlend(startplane->normal, endplane->normal, enterfrac, newimpactnormal);
448 // moving out of brush
455 f = (d1 + COLLISIONEPSILON) / f;
464 trace->startsolid = true;
466 trace->allsolid = true;
469 // LordHavoc: we need an epsilon nudge here because for a point trace the
470 // penetrating line segment is normally zero length if this brush was
471 // generated from a polygon (infinitely thin), and could even be slightly
472 // positive or negative due to rounding errors in that case.
473 if (enterfrac > -1 && enterfrac < trace->fraction && enterfrac - (1.0f / 1024.0f) <= leavefrac)
475 trace->fraction = bound(0, enterfrac, 1);
476 VectorCopy(newimpactnormal, trace->plane.normal);
480 static colplanef_t polyf_planes[256 + 2];
481 static colbrushf_t polyf_brush;
483 void Collision_TraceBrushPolygonFloat(trace_t *trace, const colbrushf_t *thisbrush_start, const colbrushf_t *thisbrush_end, int numpoints, const float *points)
487 Con_Printf("Polygon with more than 256 points not supported yet (fixme!)\n");
490 polyf_brush.numpoints = numpoints;
491 polyf_brush.numplanes = numpoints + 2;
492 polyf_brush.points = (colpointf_t *)points;
493 polyf_brush.planes = polyf_planes;
494 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
495 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
496 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brush, &polyf_brush);
499 static colpointf_t polyf_pointsstart[256], polyf_pointsend[256];
500 static colplanef_t polyf_planesstart[256 + 2], polyf_planesend[256 + 2];
501 static colbrushf_t polyf_brushstart, polyf_brushend;
503 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)
508 Con_Printf("Polygon with more than 256 points not supported yet (fixme!)\n");
511 polyf_brushstart.numpoints = numpoints;
512 polyf_brushstart.numplanes = numpoints + 2;
513 polyf_brushstart.points = polyf_pointsstart;//(colpointf_t *)points;
514 polyf_brushstart.planes = polyf_planesstart;
515 for (i = 0;i < numpoints;i++)
516 Matrix4x4_Transform(polygonmatrixstart, points + i * 3, polyf_brushstart.points[i].v);
517 polyf_brushend.numpoints = numpoints;
518 polyf_brushend.numplanes = numpoints + 2;
519 polyf_brushend.points = polyf_pointsend;//(colpointf_t *)points;
520 polyf_brushend.planes = polyf_planesend;
521 for (i = 0;i < numpoints;i++)
522 Matrix4x4_Transform(polygonmatrixend, points + i * 3, polyf_brushend.points[i].v);
523 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brushstart);
524 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brushend);
526 //Collision_PrintBrushAsQHull(&polyf_brushstart, "polyf_brushstart");
527 //Collision_PrintBrushAsQHull(&polyf_brushend, "polyf_brushend");
529 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brushstart, &polyf_brushend);
534 #define MAX_BRUSHFORBOX 16
535 static int brushforbox_index = 0;
536 static colpointf_t brushforbox_point[MAX_BRUSHFORBOX*8];
537 static colplanef_t brushforbox_plane[MAX_BRUSHFORBOX*6];
538 static colbrushf_t brushforbox_brush[MAX_BRUSHFORBOX];
540 void Collision_InitBrushForBox(void)
543 for (i = 0;i < MAX_BRUSHFORBOX;i++)
545 brushforbox_brush[i].numpoints = 8;
546 brushforbox_brush[i].numplanes = 6;
547 brushforbox_brush[i].points = brushforbox_point + i * 8;
548 brushforbox_brush[i].planes = brushforbox_plane + i * 6;
552 colbrushf_t *Collision_BrushForBox(const matrix4x4_t *matrix, const vec3_t mins, const vec3_t maxs)
557 if (brushforbox_brush[0].numpoints == 0)
558 Collision_InitBrushForBox();
559 brush = brushforbox_brush + ((brushforbox_index++) % MAX_BRUSHFORBOX);
561 for (i = 0;i < 8;i++)
563 v[0] = i & 1 ? maxs[0] : mins[0];
564 v[1] = i & 2 ? maxs[1] : mins[1];
565 v[2] = i & 4 ? maxs[2] : mins[2];
566 Matrix4x4_Transform(matrix, v, brush->points[i].v);
569 for (i = 0;i < 6;i++)
572 v[i >> 1] = i & 1 ? 1 : -1;
573 Matrix4x4_Transform3x3(matrix, v, brush->planes[i].normal);
574 VectorNormalize(brush->planes[i].normal);
575 brush->planes[i].dist = furthestplanedist_float(brush->planes[i].normal, brush->points, brush->numpoints);
580 // LordHavoc: currently unused and not yet tested
581 // note: this can be used for tracing a moving sphere vs a stationary sphere,
582 // by simply adding the moving sphere's radius to the sphereradius parameter,
583 // all the results are correct (impactpoint, impactnormal, and fraction)
584 float Collision_ClipTrace_Line_Sphere(double *linestart, double *lineend, double *sphereorigin, double sphereradius, double *impactpoint, double *impactnormal)
586 double dir[3], scale, v[3], deviationdist, impactdist, linelength;
587 // make sure the impactpoint and impactnormal are valid even if there is
589 impactpoint[0] = lineend[0];
590 impactpoint[1] = lineend[1];
591 impactpoint[2] = lineend[2];
595 // calculate line direction
596 dir[0] = lineend[0] - linestart[0];
597 dir[1] = lineend[1] - linestart[1];
598 dir[2] = lineend[2] - linestart[2];
599 // normalize direction
600 linelength = sqrt(dir[0] * dir[0] + dir[1] * dir[1] + dir[2] * dir[2]);
603 scale = 1.0 / linelength;
608 // this dotproduct calculates the distance along the line at which the
609 // sphere origin is (nearest point to the sphere origin on the line)
610 impactdist = dir[0] * (sphereorigin[0] - linestart[0]) + dir[1] * (sphereorigin[1] - linestart[1]) + dir[2] * (sphereorigin[2] - linestart[2]);
611 // calculate point on line at that distance, and subtract the
612 // sphereorigin from it, so we have a vector to measure for the distance
613 // of the line from the sphereorigin (deviation, how off-center it is)
614 v[0] = linestart[0] + impactdist * dir[0] - sphereorigin[0];
615 v[1] = linestart[1] + impactdist * dir[1] - sphereorigin[1];
616 v[2] = linestart[2] + impactdist * dir[2] - sphereorigin[2];
617 deviationdist = v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
618 // if outside the radius, it's a miss for sure
619 // (we do this comparison using squared radius to avoid a sqrt)
620 if (deviationdist > sphereradius*sphereradius)
621 return 1; // miss (off to the side)
622 // nudge back to find the correct impact distance
623 impactdist += (sqrt(deviationdist) - sphereradius);
624 if (impactdist >= linelength)
625 return 1; // miss (not close enough)
627 return 1; // miss (linestart is past or inside sphere)
628 // calculate new impactpoint
629 impactpoint[0] = linestart[0] + impactdist * dir[0];
630 impactpoint[1] = linestart[1] + impactdist * dir[1];
631 impactpoint[2] = linestart[2] + impactdist * dir[2];
632 // calculate impactnormal (surface normal at point of impact)
633 impactnormal[0] = impactpoint[0] - sphereorigin[0];
634 impactnormal[1] = impactpoint[1] - sphereorigin[1];
635 impactnormal[2] = impactpoint[2] - sphereorigin[2];
636 // normalize impactnormal
637 scale = impactnormal[0] * impactnormal[0] + impactnormal[1] * impactnormal[1] + impactnormal[2] * impactnormal[2];
640 scale = 1.0 / sqrt(scale);
641 impactnormal[0] *= scale;
642 impactnormal[1] *= scale;
643 impactnormal[2] *= scale;
645 // return fraction of movement distance
646 return impactdist / linelength;