5 // 1/32 epsilon to keep floating point happy
6 #define DIST_EPSILON (0.03125)
11 // the hull we're tracing through
14 // the trace structure to fill in
17 // start and end of the trace (in model space)
24 // overrides the CONTENTS_SOLID in the box bsp tree
27 RecursiveHullCheckTraceInfo_t;
29 #define HULLCHECKSTATE_EMPTY 0
30 #define HULLCHECKSTATE_SOLID 1
31 #define HULLCHECKSTATE_DONE 2
33 static int RecursiveHullCheck(RecursiveHullCheckTraceInfo_t *t, int num, double p1f, double p2f, double p1[3], double p2[3])
35 // status variables, these don't need to be saved on the stack when
36 // recursing... but are because this should be thread-safe
37 // (note: tracing against a bbox is not thread-safe, yet)
42 // variables that need to be stored on the stack when recursing
47 // LordHavoc: a goto! everyone flee in terror... :)
52 num = Mod_Q1BSP_SuperContentsFromNativeContents(NULL, num);
53 if (!t->trace->startfound)
55 t->trace->startfound = true;
56 t->trace->startsupercontents |= num;
58 if (num & SUPERCONTENTS_LIQUIDSMASK)
59 t->trace->inwater = true;
61 t->trace->inopen = true;
62 if (num & t->trace->hitsupercontentsmask)
64 // if the first leaf is solid, set startsolid
65 if (t->trace->allsolid)
66 t->trace->startsolid = true;
67 #if COLLISIONPARANOID >= 3
70 return HULLCHECKSTATE_SOLID;
74 t->trace->allsolid = false;
75 #if COLLISIONPARANOID >= 3
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 #if COLLISIONPARANOID >= 3
104 num = node->children[1];
113 #if COLLISIONPARANOID >= 3
116 num = node->children[0];
122 // the line intersects, find intersection point
123 // LordHavoc: this uses the original trace for maximum accuracy
124 #if COLLISIONPARANOID >= 3
129 t1 = t->start[plane->type] - plane->dist;
130 t2 = t->end[plane->type] - plane->dist;
134 t1 = DotProduct (plane->normal, t->start) - plane->dist;
135 t2 = DotProduct (plane->normal, t->end) - plane->dist;
138 midf = t1 / (t1 - t2);
139 midf = bound(p1f, midf, p2f);
140 VectorMA(t->start, midf, t->dist, mid);
142 // recurse both sides, front side first
143 ret = RecursiveHullCheck (t, node->children[side], p1f, midf, p1, mid);
144 // if this side is not empty, return what it is (solid or done)
145 if (ret != HULLCHECKSTATE_EMPTY)
148 ret = RecursiveHullCheck (t, node->children[side ^ 1], midf, p2f, mid, p2);
149 // if other side is not solid, return what it is (empty or done)
150 if (ret != HULLCHECKSTATE_SOLID)
153 // front is air and back is solid, this is the impact point...
156 t->trace->plane.dist = -plane->dist;
157 VectorNegate (plane->normal, t->trace->plane.normal);
161 t->trace->plane.dist = plane->dist;
162 VectorCopy (plane->normal, t->trace->plane.normal);
165 // bias away from surface a bit
166 t1 = DotProduct(t->trace->plane.normal, t->start) - (t->trace->plane.dist + DIST_EPSILON);
167 t2 = DotProduct(t->trace->plane.normal, t->end) - (t->trace->plane.dist + DIST_EPSILON);
169 midf = t1 / (t1 - t2);
170 t->trace->fraction = bound(0.0f, midf, 1.0);
172 #if COLLISIONPARANOID >= 3
175 return HULLCHECKSTATE_DONE;
179 // used if start and end are the same
180 static void RecursiveHullCheckPoint (RecursiveHullCheckTraceInfo_t *t, int num)
182 // If you can read this, you understand BSP trees
184 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];
187 t->trace->endcontents = num;
188 if (t->trace->thiscontents)
190 if (num == t->trace->thiscontents)
191 t->trace->allsolid = false;
194 // if the first leaf is solid, set startsolid
195 if (t->trace->allsolid)
196 t->trace->startsolid = true;
201 if (num != CONTENTS_SOLID)
203 t->trace->allsolid = false;
204 if (num == CONTENTS_EMPTY)
205 t->trace->inopen = true;
207 t->trace->inwater = true;
211 // if the first leaf is solid, set startsolid
212 if (t->trace->allsolid)
213 t->trace->startsolid = true;
219 static hull_t box_hull;
220 static dclipnode_t box_clipnodes[6];
221 static mplane_t box_planes[6];
223 void Mod_Q1BSP_Collision_Init (void)
228 //Set up the planes and clipnodes so that the six floats of a bounding box
229 //can just be stored out and get a proper hull_t structure.
231 box_hull.clipnodes = box_clipnodes;
232 box_hull.planes = box_planes;
233 box_hull.firstclipnode = 0;
234 box_hull.lastclipnode = 5;
236 for (i = 0;i < 6;i++)
238 box_clipnodes[i].planenum = i;
242 box_clipnodes[i].children[side] = CONTENTS_EMPTY;
244 box_clipnodes[i].children[side^1] = i + 1;
246 box_clipnodes[i].children[side^1] = CONTENTS_SOLID;
248 box_planes[i].type = i>>1;
249 box_planes[i].normal[i>>1] = 1;
253 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, int hitsupercontentsmask, int boxsupercontents)
255 RecursiveHullCheckTraceInfo_t rhc;
256 // fill in a default trace
257 memset(&rhc, 0, sizeof(rhc));
258 memset(trace, 0, sizeof(trace_t));
259 //To keep everything totally uniform, bounding boxes are turned into small
260 //BSP trees instead of being compared directly.
261 // create a temp hull from bounding box sizes
262 box_planes[0].dist = cmaxs[0] - mins[0];
263 box_planes[1].dist = cmins[0] - maxs[0];
264 box_planes[2].dist = cmaxs[1] - mins[1];
265 box_planes[3].dist = cmins[1] - maxs[1];
266 box_planes[4].dist = cmaxs[2] - mins[2];
267 box_planes[5].dist = cmins[2] - maxs[2];
268 // trace a line through the generated clipping hull
269 rhc.boxsupercontents = boxsupercontents;
270 rhc.hull = &box_hull;
272 rhc.trace->hitsupercontentsmask = hitsupercontentsmask;
273 rhc.trace->fraction = 1;
274 rhc.trace->allsolid = true;
275 VectorCopy(start, rhc.start);
276 VectorCopy(end, rhc.end);
277 VectorSubtract(rhc.end, rhc.start, rhc.dist);
278 Mod_Q1BSP_RecursiveHullCheck(&rhc, rhc.hull->firstclipnode, 0, 1, rhc.start, rhc.end);
279 VectorMA(rhc.start, rhc.trace->fraction, rhc.dist, rhc.trace->endpos);
280 if (rhc.trace->startsupercontents)
281 rhc.trace->startsupercontents = boxsupercontents;
285 void Collision_Init (void)
302 void Collision_PrintBrushAsQHull(colbrushf_t *brush, const char *name)
305 Con_Printf("3 %s\n%i\n", name, brush->numpoints);
306 for (i = 0;i < brush->numpoints;i++)
307 Con_Printf("%f %f %f\n", brush->points[i].v[0], brush->points[i].v[1], brush->points[i].v[2]);
309 Con_Printf("4\n%i\n", brush->numplanes);
310 for (i = 0;i < brush->numplanes;i++)
311 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);
314 void Collision_ValidateBrush(colbrushf_t *brush)
316 int j, k, pointsoffplanes, printbrush;
319 if (!brush->numpoints)
321 Con_Printf("Collision_ValidateBrush: brush with no points!\n");
325 // it's ok for a brush to have one point and no planes...
326 if (brush->numplanes == 0 && brush->numpoints != 1)
328 Con_Printf("Collision_ValidateBrush: brush with no planes and more than one point!\n");
332 if (brush->numplanes)
335 for (k = 0;k < brush->numplanes;k++)
337 if (DotProduct(brush->planes[k].normal, brush->planes[k].normal) < 0.0001f)
338 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);
339 for (j = 0;j < brush->numpoints;j++)
341 d = DotProduct(brush->points[j].v, brush->planes[k].normal) - brush->planes[k].dist;
342 if (d > (1.0f / 8.0f))
344 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);
351 if (pointsoffplanes == 0) // all points are on all planes
353 Con_Printf("Collision_ValidateBrush: all points lie on all planes (degenerate, no brush volume!)\n");
358 Collision_PrintBrushAsQHull(brush, "unnamed");
361 float nearestplanedist_float(const float *normal, const colpointf_t *points, int numpoints)
363 float dist, bestdist;
364 bestdist = DotProduct(points->v, normal);
368 dist = DotProduct(points->v, normal);
369 bestdist = min(bestdist, dist);
375 float furthestplanedist_float(const float *normal, const colpointf_t *points, int numpoints)
377 float dist, bestdist;
378 bestdist = DotProduct(points->v, normal);
382 dist = DotProduct(points->v, normal);
383 bestdist = max(bestdist, dist);
390 colbrushf_t *Collision_NewBrushFromPlanes(mempool_t *mempool, int numoriginalplanes, const mplane_t *originalplanes, int supercontents, winding_t *temp1, winding_t *temp2)
393 int numpoints, maxpoints, numplanes, maxplanes, numelements, maxelements, numtriangles, numpolypoints, maxpolypoints;
394 winding_t *w, *temp, *othertemp;
396 colpointf_t pointsbuf[256];
397 colplanef_t planesbuf[256];
398 int elementsbuf[1024];
399 int polypointbuf[256];
400 float mins[3], maxs[3];
401 // construct a collision brush (points, planes, and renderable mesh) from
402 // a set of planes, this also optimizes out any unnecessary planes (ones
403 // whose polygon is clipped away by the other planes)
404 numpoints = 0;maxpoints = 256;
405 numplanes = 0;maxplanes = 256;
406 numelements = 0;maxelements = 1024;
409 for (j = 0;j < numoriginalplanes;j++)
411 // add the plane uniquely (no duplicates)
412 for (k = 0;k < numplanes;k++)
413 if (VectorCompare(planesbuf[k].normal, originalplanes[j].normal) && planesbuf[k].dist == originalplanes[j].dist)
415 // if the plane is a duplicate, skip it
418 // check if there are too many and skip the brush
419 if (numplanes >= 256)
421 Con_Printf("Mod_Q3BSP_LoadBrushes: failed to build collision brush: too many planes for buffer\n");
425 // create a large polygon from the plane
428 BufWinding_NewFromPlane(w, originalplanes[j].normal[0], originalplanes[j].normal[1], originalplanes[j].normal[2], originalplanes[j].dist);
429 // clip it by all other planes
430 for (k = 0;k < numoriginalplanes && w->numpoints;k++)
434 // we want to keep the inside of the brush plane so we flip
436 BufWinding_Divide(w, -originalplanes[k].normal[0], -originalplanes[k].normal[1], -originalplanes[k].normal[2], -originalplanes[k].dist, othertemp, NULL, NULL, NULL);
442 // if nothing is left, skip it
446 // copy off the number of points for later when the winding is freed
447 numpolypoints = w->numpoints;
449 // check if there are too many polygon vertices for buffer
450 if (numpolypoints > maxpolypoints)
452 Con_Printf("Collision_NewBrushFromPlanes: failed to build collision brush: too many points for buffer\n");
456 // check if there are too many triangle elements for buffer
457 if (numelements + (w->numpoints - 2) * 3 > maxelements)
459 Con_Printf("Collision_NewBrushFromPlanes: failed to build collision brush: too many triangle elements for buffer\n");
463 for (k = 0;k < w->numpoints;k++)
465 // check if there is already a matching point (no duplicates)
466 for (m = 0;m < numpoints;m++)
467 if (VectorDistance2(w->points[k], pointsbuf[m].v) < DIST_EPSILON)
470 // if there is no match, add a new one
473 // check if there are too many and skip the brush
474 if (numpoints >= 256)
476 Con_Printf("Collision_NewBrushFromPlanes: failed to build collision brush: too many points for buffer\n");
481 VectorCopy(w->points[k], pointsbuf[numpoints].v);
485 // store the index into a buffer
492 // add the triangles for the polygon
493 // (this particular code makes a triangle fan)
494 for (k = 0;k < numpolypoints - 2;k++)
497 elementsbuf[numelements++] = polypointbuf[0];
498 elementsbuf[numelements++] = polypointbuf[k + 1];
499 elementsbuf[numelements++] = polypointbuf[k + 2];
503 VectorCopy(originalplanes[j].normal, planesbuf[numplanes].normal);
504 planesbuf[numplanes].dist = originalplanes[j].dist;
509 for (j = 0;j < numplanes;j++)
510 planesbuf[j].dist = furthestplanedist_float(planesbuf[j].normal, pointsbuf, numpoints);
514 VectorCopy(pointsbuf[0].v, mins);
515 VectorCopy(pointsbuf[0].v, maxs);
516 for (j = 1;j < numpoints;j++)
518 mins[0] = min(mins[0], pointsbuf[j].v[0]);
519 mins[1] = min(mins[1], pointsbuf[j].v[1]);
520 mins[2] = min(mins[2], pointsbuf[j].v[2]);
521 maxs[0] = max(maxs[0], pointsbuf[j].v[0]);
522 maxs[1] = max(maxs[1], pointsbuf[j].v[1]);
523 maxs[2] = max(maxs[2], pointsbuf[j].v[2]);
527 // if nothing is left, there's nothing to allocate
528 if (numtriangles < 4 || numplanes < 4 || numpoints < 4)
531 // allocate the brush and copy to it
532 brush = Collision_AllocBrushFloat(mempool, numpoints, numplanes, numtriangles, supercontents);
533 memcpy(brush->points, pointsbuf, numpoints * sizeof(colpointf_t));
534 memcpy(brush->planes, planesbuf, numplanes * sizeof(colplanef_t));
535 memcpy(brush->elements, elementsbuf, numtriangles * sizeof(int[3]));
536 VectorCopy(mins, brush->mins);
537 VectorCopy(maxs, brush->maxs);
538 Collision_ValidateBrush(brush);
544 colbrushf_t *Collision_AllocBrushFloat(mempool_t *mempool, int numpoints, int numplanes, int numtriangles, int supercontents)
547 brush = Mem_Alloc(mempool, sizeof(colbrushf_t) + sizeof(colpointf_t) * numpoints + sizeof(colplanef_t) * numplanes + sizeof(int[3]) * numtriangles);
548 brush->supercontents = supercontents;
549 brush->numplanes = numplanes;
550 brush->numpoints = numpoints;
551 brush->numtriangles = numtriangles;
552 brush->planes = (void *)(brush + 1);
553 brush->points = (void *)(brush->planes + brush->numplanes);
554 brush->elements = (void *)(brush->points + brush->numpoints);
558 void Collision_CalcPlanesForPolygonBrushFloat(colbrushf_t *brush)
561 float edge0[3], edge1[3], edge2[3], normal[3], dist, bestdist, temp[3];
564 if (brush->numpoints == 3)
566 // optimized triangle case
567 TriangleNormal(brush->points[0].v, brush->points[1].v, brush->points[2].v, brush->planes[0].normal);
568 if (DotProduct(brush->planes[0].normal, brush->planes[0].normal) < 0.0001f)
570 // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
571 brush->numplanes = 0;
576 brush->numplanes = 5;
577 VectorNormalize(brush->planes[0].normal);
578 brush->planes[0].dist = DotProduct(brush->points->v, brush->planes[0].normal);
579 VectorNegate(brush->planes[0].normal, brush->planes[1].normal);
580 brush->planes[1].dist = -brush->planes[0].dist;
581 VectorSubtract(brush->points[2].v, brush->points[0].v, edge0);
582 VectorSubtract(brush->points[0].v, brush->points[1].v, edge1);
583 VectorSubtract(brush->points[1].v, brush->points[2].v, edge2);
584 CrossProduct(edge0, brush->planes->normal, brush->planes[2].normal);
585 CrossProduct(edge1, brush->planes->normal, brush->planes[3].normal);
586 CrossProduct(edge2, brush->planes->normal, brush->planes[4].normal);
587 VectorNormalize(brush->planes[2].normal);
588 VectorNormalize(brush->planes[3].normal);
589 VectorNormalize(brush->planes[4].normal);
590 brush->planes[2].dist = DotProduct(brush->points[2].v, brush->planes[2].normal);
591 brush->planes[3].dist = DotProduct(brush->points[0].v, brush->planes[3].normal);
592 brush->planes[4].dist = DotProduct(brush->points[1].v, brush->planes[4].normal);
594 if (developer.integer)
597 //VectorSubtract(brush->points[0].v, brush->points[1].v, edge0);
598 //VectorSubtract(brush->points[2].v, brush->points[1].v, edge1);
599 CrossProduct(edge1, edge0, normal);
600 VectorNormalize(normal);
601 VectorSubtract(normal, brush->planes[0].normal, temp);
602 if (VectorLength(temp) > 0.01f)
603 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]);
604 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)
605 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);
606 if (fabs(DotProduct(brush->planes[2].normal, brush->planes[0].normal)) > 0.01f)
607 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);
608 if (fabs(DotProduct(brush->planes[3].normal, brush->planes[0].normal)) > 0.01f)
609 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);
610 if (fabs(DotProduct(brush->planes[4].normal, brush->planes[0].normal)) > 0.01f)
611 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);
612 if (fabs(DotProduct(brush->planes[2].normal, edge0)) > 0.01f)
613 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]);
614 if (fabs(DotProduct(brush->planes[3].normal, edge1)) > 0.01f)
615 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]);
616 if (fabs(DotProduct(brush->planes[4].normal, edge2)) > 0.01f)
617 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]);
618 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)
619 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);
620 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)
621 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);
622 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)
623 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);
624 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)
625 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);
626 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)
627 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);
633 // choose best surface normal for polygon's plane
635 for (i = 0, p = brush->points + 1;i < brush->numpoints - 2;i++, p++)
637 VectorSubtract(p[-1].v, p[0].v, edge0);
638 VectorSubtract(p[1].v, p[0].v, edge1);
639 CrossProduct(edge0, edge1, normal);
640 //TriangleNormal(p[-1].v, p[0].v, p[1].v, normal);
641 dist = DotProduct(normal, normal);
642 if (i == 0 || bestdist < dist)
645 VectorCopy(normal, brush->planes->normal);
648 if (bestdist < 0.0001f)
650 // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
651 brush->numplanes = 0;
656 brush->numplanes = brush->numpoints + 2;
657 VectorNormalize(brush->planes->normal);
658 brush->planes->dist = DotProduct(brush->points->v, brush->planes->normal);
660 // negate plane to create other side
661 VectorNegate(brush->planes[0].normal, brush->planes[1].normal);
662 brush->planes[1].dist = -brush->planes[0].dist;
663 for (i = 0, p = brush->points + (brush->numpoints - 1), p2 = brush->points;i < brush->numpoints;i++, p = p2, p2++)
665 VectorSubtract(p->v, p2->v, edge0);
666 CrossProduct(edge0, brush->planes->normal, brush->planes[i + 2].normal);
667 VectorNormalize(brush->planes[i + 2].normal);
668 brush->planes[i + 2].dist = DotProduct(p->v, brush->planes[i + 2].normal);
673 if (developer.integer)
675 // validity check - will be disabled later
676 Collision_ValidateBrush(brush);
677 for (i = 0;i < brush->numplanes;i++)
680 for (j = 0, p = brush->points;j < brush->numpoints;j++, p++)
681 if (DotProduct(p->v, brush->planes[i].normal) > brush->planes[i].dist + (1.0 / 32.0))
682 Con_Printf("Error in brush plane generation, plane %i\n", i);
687 colbrushf_t *Collision_AllocBrushFromPermanentPolygonFloat(mempool_t *mempool, int numpoints, float *points, int supercontents)
690 brush = Mem_Alloc(mempool, sizeof(colbrushf_t) + sizeof(colplanef_t) * (numpoints + 2));
691 brush->supercontents = supercontents;
692 brush->numpoints = numpoints;
693 brush->numplanes = numpoints + 2;
694 brush->planes = (void *)(brush + 1);
695 brush->points = (colpointf_t *)points;
696 Host_Error("Collision_AllocBrushFromPermanentPolygonFloat: FIXME: this code needs to be updated to generate a mesh...\n");
700 #define COLLISIONEPSILON (1.0f / 32.0f)
701 #define COLLISIONEPSILON2 0//(1.0f / 32.0f)
703 // NOTE: start and end of each brush pair must have same numplanes/numpoints
704 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)
706 int nplane, nplane2, fstartsolid, fendsolid, brushsolid;
707 float enterfrac, leavefrac, d1, d2, f, newimpactnormal[3];
708 const colplanef_t *startplane, *endplane;
715 for (nplane = 0;nplane < thatbrush_start->numplanes + thisbrush_start->numplanes;nplane++)
718 if (nplane2 >= thatbrush_start->numplanes)
720 nplane2 -= thatbrush_start->numplanes;
721 startplane = thisbrush_start->planes + nplane2;
722 endplane = thisbrush_end->planes + nplane2;
723 if (developer.integer)
725 // any brush with degenerate planes is not worth handling
726 if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
728 Con_Printf("Collision_TraceBrushBrushFloat: degenerate thisbrush plane!\n");
731 f = furthestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints);
732 if (fabs(f - startplane->dist) > 0.01f)
733 Con_Printf("startplane->dist %f != calculated %f (thisbrush_start)\n", startplane->dist, f);
735 d1 = nearestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints) - furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints);
736 d2 = nearestplanedist_float(endplane->normal, thisbrush_end->points, thisbrush_end->numpoints) - furthestplanedist_float(endplane->normal, thatbrush_end->points, thatbrush_end->numpoints) - COLLISIONEPSILON2;
740 startplane = thatbrush_start->planes + nplane2;
741 endplane = thatbrush_end->planes + nplane2;
742 if (developer.integer)
744 // any brush with degenerate planes is not worth handling
745 if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
747 Con_Printf("Collision_TraceBrushBrushFloat: degenerate thatbrush plane!\n");
750 f = furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints);
751 if (fabs(f - startplane->dist) > 0.01f)
752 Con_Printf("startplane->dist %f != calculated %f (thatbrush_start)\n", startplane->dist, f);
754 d1 = nearestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints) - startplane->dist;
755 d2 = nearestplanedist_float(endplane->normal, thisbrush_end->points, thisbrush_end->numpoints) - startplane->dist - COLLISIONEPSILON2;
757 //Con_Printf("%c%i: d1 = %f, d2 = %f, d1 / (d1 - d2) = %f\n", nplane2 != nplane ? 'b' : 'a', nplane2, d1, d2, d1 / (d1 - d2));
769 f = (d1 - COLLISIONEPSILON) / f;
774 VectorLerp(startplane->normal, enterfrac, endplane->normal, newimpactnormal);
779 // moving out of brush
786 f = (d1 + COLLISIONEPSILON) / f;
793 brushsolid = trace->hitsupercontentsmask & thatbrush_start->supercontents;
796 trace->startsupercontents |= thatbrush_start->supercontents;
799 trace->startsolid = true;
801 trace->allsolid = true;
805 // LordHavoc: we need an epsilon nudge here because for a point trace the
806 // penetrating line segment is normally zero length if this brush was
807 // generated from a polygon (infinitely thin), and could even be slightly
808 // positive or negative due to rounding errors in that case.
809 if (brushsolid && enterfrac > -1 && enterfrac < trace->fraction && enterfrac - (1.0f / 1024.0f) <= leavefrac)
811 trace->fraction = bound(0, enterfrac, 1);
812 VectorCopy(newimpactnormal, trace->plane.normal);
816 // NOTE: start and end brush pair must have same numplanes/numpoints
817 void Collision_TraceLineBrushFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, const colbrushf_t *thatbrush_start, const colbrushf_t *thatbrush_end)
819 int nplane, fstartsolid, fendsolid, brushsolid;
820 float enterfrac, leavefrac, d1, d2, f, newimpactnormal[3];
821 const colplanef_t *startplane, *endplane;
828 for (nplane = 0;nplane < thatbrush_start->numplanes;nplane++)
830 startplane = thatbrush_start->planes + nplane;
831 endplane = thatbrush_end->planes + nplane;
832 d1 = DotProduct(startplane->normal, linestart) - startplane->dist;
833 d2 = DotProduct(endplane->normal, lineend) - endplane->dist - COLLISIONEPSILON2;
834 if (developer.integer)
836 // any brush with degenerate planes is not worth handling
837 if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
839 Con_Printf("Collision_TraceLineBrushFloat: degenerate plane!\n");
842 if (thatbrush_start->numpoints)
844 f = furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints);
845 if (fabs(f - startplane->dist) > 0.01f)
846 Con_Printf("startplane->dist %f != calculated %f\n", startplane->dist, f);
860 f = (d1 - COLLISIONEPSILON) / f;
865 VectorLerp(startplane->normal, enterfrac, endplane->normal, newimpactnormal);
870 // moving out of brush
877 f = (d1 + COLLISIONEPSILON) / f;
884 brushsolid = trace->hitsupercontentsmask & thatbrush_start->supercontents;
887 trace->startsupercontents |= thatbrush_start->supercontents;
890 trace->startsolid = true;
892 trace->allsolid = true;
896 // LordHavoc: we need an epsilon nudge here because for a point trace the
897 // penetrating line segment is normally zero length if this brush was
898 // generated from a polygon (infinitely thin), and could even be slightly
899 // positive or negative due to rounding errors in that case.
900 if (brushsolid && enterfrac > -1 && enterfrac < trace->fraction && enterfrac - (1.0f / 1024.0f) <= leavefrac)
902 trace->fraction = bound(0, enterfrac, 1);
903 VectorCopy(newimpactnormal, trace->plane.normal);
907 static colpointf_t polyf_points[256];
908 static colplanef_t polyf_planes[256 + 2];
909 static colbrushf_t polyf_brush;
911 void Collision_TraceBrushPolygonFloat(trace_t *trace, const colbrushf_t *thisbrush_start, const colbrushf_t *thisbrush_end, int numpoints, const float *points, int supercontents)
915 Con_Printf("Polygon with more than 256 points not supported yet (fixme!)\n");
918 polyf_brush.numpoints = numpoints;
919 polyf_brush.numplanes = numpoints + 2;
920 polyf_brush.points = (colpointf_t *)points;
921 polyf_brush.planes = polyf_planes;
922 polyf_brush.supercontents = supercontents;
923 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
924 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
925 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brush, &polyf_brush);
928 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, const vec3_t segmentmins, const vec3_t segmentmaxs)
931 float facemins[3], facemaxs[3];
932 polyf_brush.numpoints = 3;
933 polyf_brush.numplanes = 5;
934 polyf_brush.points = polyf_points;
935 polyf_brush.planes = polyf_planes;
936 polyf_brush.supercontents = supercontents;
937 for (i = 0;i < numtriangles;i++, element3i += 3)
939 VectorCopy(vertex3f + element3i[0] * 3, polyf_points[0].v);
940 VectorCopy(vertex3f + element3i[1] * 3, polyf_points[1].v);
941 VectorCopy(vertex3f + element3i[2] * 3, polyf_points[2].v);
942 facemins[0] = min(polyf_points[0].v[0], min(polyf_points[1].v[0], polyf_points[2].v[0]));
943 facemins[1] = min(polyf_points[0].v[1], min(polyf_points[1].v[1], polyf_points[2].v[1]));
944 facemins[2] = min(polyf_points[0].v[2], min(polyf_points[1].v[2], polyf_points[2].v[2]));
945 facemaxs[0] = max(polyf_points[0].v[0], max(polyf_points[1].v[0], polyf_points[2].v[0]));
946 facemaxs[1] = max(polyf_points[0].v[1], max(polyf_points[1].v[1], polyf_points[2].v[1]));
947 facemaxs[2] = max(polyf_points[0].v[2], max(polyf_points[1].v[2], polyf_points[2].v[2]));
948 if (BoxesOverlap(segmentmins, segmentmaxs, facemins, facemaxs))
950 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
951 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
952 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brush, &polyf_brush);
957 void Collision_TraceLinePolygonFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, int numpoints, const float *points, int supercontents)
961 Con_Printf("Polygon with more than 256 points not supported yet (fixme!)\n");
964 polyf_brush.numpoints = numpoints;
965 polyf_brush.numplanes = numpoints + 2;
966 polyf_brush.points = (colpointf_t *)points;
967 polyf_brush.planes = polyf_planes;
968 polyf_brush.supercontents = supercontents;
969 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
970 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
971 Collision_TraceLineBrushFloat(trace, linestart, lineend, &polyf_brush, &polyf_brush);
974 void Collision_TraceLineTriangleMeshFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, int numtriangles, const int *element3i, const float *vertex3f, int supercontents, const vec3_t segmentmins, const vec3_t segmentmaxs)
977 float facemins[3], facemaxs[3];
978 polyf_brush.numpoints = 3;
979 polyf_brush.numplanes = 5;
980 polyf_brush.points = polyf_points;
981 polyf_brush.planes = polyf_planes;
982 polyf_brush.supercontents = supercontents;
983 for (i = 0;i < numtriangles;i++, element3i += 3)
985 VectorCopy(vertex3f + element3i[0] * 3, polyf_points[0].v);
986 VectorCopy(vertex3f + element3i[1] * 3, polyf_points[1].v);
987 VectorCopy(vertex3f + element3i[2] * 3, polyf_points[2].v);
988 facemins[0] = min(polyf_points[0].v[0], min(polyf_points[1].v[0], polyf_points[2].v[0]));
989 facemins[1] = min(polyf_points[0].v[1], min(polyf_points[1].v[1], polyf_points[2].v[1]));
990 facemins[2] = min(polyf_points[0].v[2], min(polyf_points[1].v[2], polyf_points[2].v[2]));
991 facemaxs[0] = max(polyf_points[0].v[0], max(polyf_points[1].v[0], polyf_points[2].v[0]));
992 facemaxs[1] = max(polyf_points[0].v[1], max(polyf_points[1].v[1], polyf_points[2].v[1]));
993 facemaxs[2] = max(polyf_points[0].v[2], max(polyf_points[1].v[2], polyf_points[2].v[2]));
994 if (BoxesOverlap(segmentmins, segmentmaxs, facemins, facemaxs))
996 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
997 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
998 Collision_TraceLineBrushFloat(trace, linestart, lineend, &polyf_brush, &polyf_brush);
1004 static colpointf_t polyf_pointsstart[256], polyf_pointsend[256];
1005 static colplanef_t polyf_planesstart[256 + 2], polyf_planesend[256 + 2];
1006 static colbrushf_t polyf_brushstart, polyf_brushend;
1008 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)
1011 if (numpoints > 256)
1013 Con_Printf("Polygon with more than 256 points not supported yet (fixme!)\n");
1016 polyf_brushstart.numpoints = numpoints;
1017 polyf_brushstart.numplanes = numpoints + 2;
1018 polyf_brushstart.points = polyf_pointsstart;//(colpointf_t *)points;
1019 polyf_brushstart.planes = polyf_planesstart;
1020 polyf_brushstart.supercontents = supercontents;
1021 for (i = 0;i < numpoints;i++)
1022 Matrix4x4_Transform(polygonmatrixstart, points + i * 3, polyf_brushstart.points[i].v);
1023 polyf_brushend.numpoints = numpoints;
1024 polyf_brushend.numplanes = numpoints + 2;
1025 polyf_brushend.points = polyf_pointsend;//(colpointf_t *)points;
1026 polyf_brushend.planes = polyf_planesend;
1027 polyf_brushend.supercontents = supercontents;
1028 for (i = 0;i < numpoints;i++)
1029 Matrix4x4_Transform(polygonmatrixend, points + i * 3, polyf_brushend.points[i].v);
1030 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brushstart);
1031 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brushend);
1033 //Collision_PrintBrushAsQHull(&polyf_brushstart, "polyf_brushstart");
1034 //Collision_PrintBrushAsQHull(&polyf_brushend, "polyf_brushend");
1036 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brushstart, &polyf_brushend);
1041 #define MAX_BRUSHFORBOX 16
1042 static int brushforbox_index = 0;
1043 static colpointf_t brushforbox_point[MAX_BRUSHFORBOX*8];
1044 static colplanef_t brushforbox_plane[MAX_BRUSHFORBOX*6];
1045 static colbrushf_t brushforbox_brush[MAX_BRUSHFORBOX];
1046 static colbrushf_t brushforpoint_brush[MAX_BRUSHFORBOX];
1048 void Collision_InitBrushForBox(void)
1051 for (i = 0;i < MAX_BRUSHFORBOX;i++)
1053 brushforbox_brush[i].supercontents = SUPERCONTENTS_SOLID;
1054 brushforbox_brush[i].numpoints = 8;
1055 brushforbox_brush[i].numplanes = 6;
1056 brushforbox_brush[i].points = brushforbox_point + i * 8;
1057 brushforbox_brush[i].planes = brushforbox_plane + i * 6;
1058 brushforpoint_brush[i].supercontents = SUPERCONTENTS_SOLID;
1059 brushforpoint_brush[i].numpoints = 1;
1060 brushforpoint_brush[i].numplanes = 0;
1061 brushforpoint_brush[i].points = brushforbox_point + i * 8;
1062 brushforpoint_brush[i].planes = brushforbox_plane + i * 6;
1066 colbrushf_t *Collision_BrushForBox(const matrix4x4_t *matrix, const vec3_t mins, const vec3_t maxs)
1071 if (brushforbox_brush[0].numpoints == 0)
1072 Collision_InitBrushForBox();
1073 if (VectorCompare(mins, maxs))
1076 brush = brushforpoint_brush + ((brushforbox_index++) % MAX_BRUSHFORBOX);
1077 VectorCopy(mins, brush->points->v);
1081 brush = brushforbox_brush + ((brushforbox_index++) % MAX_BRUSHFORBOX);
1083 for (i = 0;i < 8;i++)
1085 v[0] = i & 1 ? maxs[0] : mins[0];
1086 v[1] = i & 2 ? maxs[1] : mins[1];
1087 v[2] = i & 4 ? maxs[2] : mins[2];
1088 Matrix4x4_Transform(matrix, v, brush->points[i].v);
1091 for (i = 0;i < 6;i++)
1094 v[i >> 1] = i & 1 ? 1 : -1;
1095 Matrix4x4_Transform3x3(matrix, v, brush->planes[i].normal);
1096 VectorNormalize(brush->planes[i].normal);
1097 brush->planes[i].dist = furthestplanedist_float(brush->planes[i].normal, brush->points, brush->numpoints);
1100 Collision_ValidateBrush(brush);
1104 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)
1106 colbrushf_t *boxbrush, *thisbrush_start, *thisbrush_end;
1107 matrix4x4_t identitymatrix;
1108 vec3_t startmins, startmaxs, endmins, endmaxs;
1110 // create brushes for the collision
1111 VectorAdd(start, mins, startmins);
1112 VectorAdd(start, maxs, startmaxs);
1113 VectorAdd(end, mins, endmins);
1114 VectorAdd(end, maxs, endmaxs);
1115 Matrix4x4_CreateIdentity(&identitymatrix);
1116 boxbrush = Collision_BrushForBox(&identitymatrix, cmins, cmaxs);
1117 thisbrush_start = Collision_BrushForBox(&identitymatrix, startmins, startmaxs);
1118 thisbrush_end = Collision_BrushForBox(&identitymatrix, endmins, endmaxs);
1120 memset(trace, 0, sizeof(trace_t));
1121 trace->hitsupercontentsmask = hitsupercontentsmask;
1122 trace->fraction = 1;
1123 trace->allsolid = true;
1124 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, boxbrush, boxbrush);
1127 // LordHavoc: currently unused and not yet tested
1128 // note: this can be used for tracing a moving sphere vs a stationary sphere,
1129 // by simply adding the moving sphere's radius to the sphereradius parameter,
1130 // all the results are correct (impactpoint, impactnormal, and fraction)
1131 float Collision_ClipTrace_Line_Sphere(double *linestart, double *lineend, double *sphereorigin, double sphereradius, double *impactpoint, double *impactnormal)
1133 double dir[3], scale, v[3], deviationdist, impactdist, linelength;
1134 // make sure the impactpoint and impactnormal are valid even if there is
1136 impactpoint[0] = lineend[0];
1137 impactpoint[1] = lineend[1];
1138 impactpoint[2] = lineend[2];
1139 impactnormal[0] = 0;
1140 impactnormal[1] = 0;
1141 impactnormal[2] = 0;
1142 // calculate line direction
1143 dir[0] = lineend[0] - linestart[0];
1144 dir[1] = lineend[1] - linestart[1];
1145 dir[2] = lineend[2] - linestart[2];
1146 // normalize direction
1147 linelength = sqrt(dir[0] * dir[0] + dir[1] * dir[1] + dir[2] * dir[2]);
1150 scale = 1.0 / linelength;
1155 // this dotproduct calculates the distance along the line at which the
1156 // sphere origin is (nearest point to the sphere origin on the line)
1157 impactdist = dir[0] * (sphereorigin[0] - linestart[0]) + dir[1] * (sphereorigin[1] - linestart[1]) + dir[2] * (sphereorigin[2] - linestart[2]);
1158 // calculate point on line at that distance, and subtract the
1159 // sphereorigin from it, so we have a vector to measure for the distance
1160 // of the line from the sphereorigin (deviation, how off-center it is)
1161 v[0] = linestart[0] + impactdist * dir[0] - sphereorigin[0];
1162 v[1] = linestart[1] + impactdist * dir[1] - sphereorigin[1];
1163 v[2] = linestart[2] + impactdist * dir[2] - sphereorigin[2];
1164 deviationdist = v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
1165 // if outside the radius, it's a miss for sure
1166 // (we do this comparison using squared radius to avoid a sqrt)
1167 if (deviationdist > sphereradius*sphereradius)
1168 return 1; // miss (off to the side)
1169 // nudge back to find the correct impact distance
1170 impactdist += (sqrt(deviationdist) - sphereradius);
1171 if (impactdist >= linelength)
1172 return 1; // miss (not close enough)
1174 return 1; // miss (linestart is past or inside sphere)
1175 // calculate new impactpoint
1176 impactpoint[0] = linestart[0] + impactdist * dir[0];
1177 impactpoint[1] = linestart[1] + impactdist * dir[1];
1178 impactpoint[2] = linestart[2] + impactdist * dir[2];
1179 // calculate impactnormal (surface normal at point of impact)
1180 impactnormal[0] = impactpoint[0] - sphereorigin[0];
1181 impactnormal[1] = impactpoint[1] - sphereorigin[1];
1182 impactnormal[2] = impactpoint[2] - sphereorigin[2];
1183 // normalize impactnormal
1184 scale = impactnormal[0] * impactnormal[0] + impactnormal[1] * impactnormal[1] + impactnormal[2] * impactnormal[2];
1187 scale = 1.0 / sqrt(scale);
1188 impactnormal[0] *= scale;
1189 impactnormal[1] *= scale;
1190 impactnormal[2] *= scale;
1192 // return fraction of movement distance
1193 return impactdist / linelength;
1196 typedef struct colbspnode_s
1199 struct colbspnode_s *children[2];
1200 // the node is reallocated or split if max is reached
1203 colbrushf_t **colbrushflist;
1206 //colbrushd_t **colbrushdlist;
1210 typedef struct colbsp_s
1213 colbspnode_t *nodes;
1217 colbsp_t *Collision_CreateCollisionBSP(mempool_t *mempool)
1220 bsp = Mem_Alloc(mempool, sizeof(colbsp_t));
1221 bsp->mempool = mempool;
1222 bsp->nodes = Mem_Alloc(bsp->mempool, sizeof(colbspnode_t));
1226 void Collision_FreeCollisionBSPNode(colbspnode_t *node)
1228 if (node->children[0])
1229 Collision_FreeCollisionBSPNode(node->children[0]);
1230 if (node->children[1])
1231 Collision_FreeCollisionBSPNode(node->children[1]);
1232 while (--node->numcolbrushf)
1233 Mem_Free(node->colbrushflist[node->numcolbrushf]);
1234 //while (--node->numcolbrushd)
1235 // Mem_Free(node->colbrushdlist[node->numcolbrushd]);
1239 void Collision_FreeCollisionBSP(colbsp_t *bsp)
1241 Collision_FreeCollisionBSPNode(bsp->nodes);
1245 void Collision_BoundingBoxOfBrushTraceSegment(const colbrushf_t *start, const colbrushf_t *end, vec3_t mins, vec3_t maxs, float startfrac, float endfrac)
1248 colpointf_t *ps, *pe;
1249 float tempstart[3], tempend[3];
1250 VectorLerp(start->points[0].v, startfrac, end->points[0].v, mins);
1251 VectorCopy(mins, maxs);
1252 for (i = 0, ps = start->points, pe = end->points;i < start->numpoints;i++, ps++, pe++)
1254 VectorLerp(ps->v, startfrac, pe->v, tempstart);
1255 VectorLerp(ps->v, endfrac, pe->v, tempend);
1256 mins[0] = min(mins[0], min(tempstart[0], tempend[0]));
1257 mins[1] = min(mins[1], min(tempstart[1], tempend[1]));
1258 mins[2] = min(mins[2], min(tempstart[2], tempend[2]));
1259 maxs[0] = min(maxs[0], min(tempstart[0], tempend[0]));
1260 maxs[1] = min(maxs[1], min(tempstart[1], tempend[1]));
1261 maxs[2] = min(maxs[2], min(tempstart[2], tempend[2]));