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 // calculate the true fraction
166 t1 = DotProduct(t->trace->plane.normal, t->start) - t->trace->plane.dist;
167 t2 = DotProduct(t->trace->plane.normal, t->end) - t->trace->plane.dist;
168 midf = t1 / (t1 - t2);
169 t->trace->realfraction = bound(0, midf, 1);
171 // calculate the return fraction which is nudged off the surface a bit
172 midf = (t1 - DIST_EPSILON) / (t1 - t2);
173 t->trace->fraction = bound(0, midf, 1);
175 #if COLLISIONPARANOID >= 3
178 return HULLCHECKSTATE_DONE;
182 // used if start and end are the same
183 static void RecursiveHullCheckPoint (RecursiveHullCheckTraceInfo_t *t, int num)
185 // If you can read this, you understand BSP trees
187 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];
190 t->trace->endcontents = num;
191 if (t->trace->thiscontents)
193 if (num == t->trace->thiscontents)
194 t->trace->allsolid = false;
197 // if the first leaf is solid, set startsolid
198 if (t->trace->allsolid)
199 t->trace->startsolid = true;
204 if (num != CONTENTS_SOLID)
206 t->trace->allsolid = false;
207 if (num == CONTENTS_EMPTY)
208 t->trace->inopen = true;
210 t->trace->inwater = true;
214 // if the first leaf is solid, set startsolid
215 if (t->trace->allsolid)
216 t->trace->startsolid = true;
222 static hull_t box_hull;
223 static dclipnode_t box_clipnodes[6];
224 static mplane_t box_planes[6];
226 void Mod_Q1BSP_Collision_Init (void)
231 //Set up the planes and clipnodes so that the six floats of a bounding box
232 //can just be stored out and get a proper hull_t structure.
234 box_hull.clipnodes = box_clipnodes;
235 box_hull.planes = box_planes;
236 box_hull.firstclipnode = 0;
237 box_hull.lastclipnode = 5;
239 for (i = 0;i < 6;i++)
241 box_clipnodes[i].planenum = i;
245 box_clipnodes[i].children[side] = CONTENTS_EMPTY;
247 box_clipnodes[i].children[side^1] = i + 1;
249 box_clipnodes[i].children[side^1] = CONTENTS_SOLID;
251 box_planes[i].type = i>>1;
252 box_planes[i].normal[i>>1] = 1;
256 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)
258 RecursiveHullCheckTraceInfo_t rhc;
259 // fill in a default trace
260 memset(&rhc, 0, sizeof(rhc));
261 memset(trace, 0, sizeof(trace_t));
262 //To keep everything totally uniform, bounding boxes are turned into small
263 //BSP trees instead of being compared directly.
264 // create a temp hull from bounding box sizes
265 box_planes[0].dist = cmaxs[0] - mins[0];
266 box_planes[1].dist = cmins[0] - maxs[0];
267 box_planes[2].dist = cmaxs[1] - mins[1];
268 box_planes[3].dist = cmins[1] - maxs[1];
269 box_planes[4].dist = cmaxs[2] - mins[2];
270 box_planes[5].dist = cmins[2] - maxs[2];
271 // trace a line through the generated clipping hull
272 rhc.boxsupercontents = boxsupercontents;
273 rhc.hull = &box_hull;
275 rhc.trace->hitsupercontentsmask = hitsupercontentsmask;
276 rhc.trace->fraction = 1;
277 rhc.trace->realfraction = 1;
278 rhc.trace->allsolid = true;
279 VectorCopy(start, rhc.start);
280 VectorCopy(end, rhc.end);
281 VectorSubtract(rhc.end, rhc.start, rhc.dist);
282 Mod_Q1BSP_RecursiveHullCheck(&rhc, rhc.hull->firstclipnode, 0, 1, rhc.start, rhc.end);
283 VectorMA(rhc.start, rhc.trace->fraction, rhc.dist, rhc.trace->endpos);
284 if (rhc.trace->startsupercontents)
285 rhc.trace->startsupercontents = boxsupercontents;
289 void Collision_Init (void)
306 void Collision_PrintBrushAsQHull(colbrushf_t *brush, const char *name)
309 Con_Printf("3 %s\n%i\n", name, brush->numpoints);
310 for (i = 0;i < brush->numpoints;i++)
311 Con_Printf("%f %f %f\n", brush->points[i].v[0], brush->points[i].v[1], brush->points[i].v[2]);
313 Con_Printf("4\n%i\n", brush->numplanes);
314 for (i = 0;i < brush->numplanes;i++)
315 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);
318 void Collision_ValidateBrush(colbrushf_t *brush)
320 int j, k, pointsoffplanes, printbrush;
323 if (!brush->numpoints)
325 Con_Printf("Collision_ValidateBrush: brush with no points!\n");
329 // it's ok for a brush to have one point and no planes...
330 if (brush->numplanes == 0 && brush->numpoints != 1)
332 Con_Printf("Collision_ValidateBrush: brush with no planes and more than one point!\n");
336 if (brush->numplanes)
339 for (k = 0;k < brush->numplanes;k++)
341 if (DotProduct(brush->planes[k].normal, brush->planes[k].normal) < 0.0001f)
342 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);
343 for (j = 0;j < brush->numpoints;j++)
345 d = DotProduct(brush->points[j].v, brush->planes[k].normal) - brush->planes[k].dist;
346 if (d > (1.0f / 8.0f))
348 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);
355 if (pointsoffplanes == 0) // all points are on all planes
357 Con_Printf("Collision_ValidateBrush: all points lie on all planes (degenerate, no brush volume!)\n");
362 Collision_PrintBrushAsQHull(brush, "unnamed");
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);
373 bestdist = min(bestdist, dist);
379 float furthestplanedist_float(const float *normal, const colpointf_t *points, int numpoints)
381 float dist, bestdist;
382 bestdist = DotProduct(points->v, normal);
386 dist = DotProduct(points->v, normal);
387 bestdist = max(bestdist, dist);
394 colbrushf_t *Collision_NewBrushFromPlanes(mempool_t *mempool, int numoriginalplanes, const mplane_t *originalplanes, int supercontents, winding_t *temp1, winding_t *temp2)
397 int numpoints, maxpoints, numplanes, maxplanes, numelements, maxelements, numtriangles, numpolypoints, maxpolypoints;
398 winding_t *w, *temp, *othertemp;
400 colpointf_t pointsbuf[256];
401 colplanef_t planesbuf[256];
402 int elementsbuf[1024];
403 int polypointbuf[256];
404 // construct a collision brush (points, planes, and renderable mesh) from
405 // a set of planes, this also optimizes out any unnecessary planes (ones
406 // whose polygon is clipped away by the other planes)
407 numpoints = 0;maxpoints = 256;
408 numplanes = 0;maxplanes = 256;
409 numelements = 0;maxelements = 1024;
412 for (j = 0;j < numoriginalplanes;j++)
414 // add the plane uniquely (no duplicates)
415 for (k = 0;k < numplanes;k++)
416 if (VectorCompare(planesbuf[k].normal, originalplanes[j].normal) && planesbuf[k].dist == originalplanes[j].dist)
418 // if the plane is a duplicate, skip it
421 // check if there are too many and skip the brush
422 if (numplanes >= 256)
424 Con_Printf("Mod_Q3BSP_LoadBrushes: failed to build collision brush: too many planes for buffer\n");
428 // create a large polygon from the plane
431 BufWinding_NewFromPlane(w, originalplanes[j].normal[0], originalplanes[j].normal[1], originalplanes[j].normal[2], originalplanes[j].dist);
432 // clip it by all other planes
433 for (k = 0;k < numoriginalplanes && w->numpoints;k++)
437 // we want to keep the inside of the brush plane so we flip
439 BufWinding_Divide(w, -originalplanes[k].normal[0], -originalplanes[k].normal[1], -originalplanes[k].normal[2], -originalplanes[k].dist, othertemp, NULL, NULL, NULL);
445 // if nothing is left, skip it
449 // copy off the number of points for later when the winding is freed
450 numpolypoints = w->numpoints;
452 // check if there are too many polygon vertices for buffer
453 if (numpolypoints > maxpolypoints)
455 Con_Printf("Collision_NewBrushFromPlanes: failed to build collision brush: too many points for buffer\n");
459 // check if there are too many triangle elements for buffer
460 if (numelements + (w->numpoints - 2) * 3 > maxelements)
462 Con_Printf("Collision_NewBrushFromPlanes: failed to build collision brush: too many triangle elements for buffer\n");
466 for (k = 0;k < w->numpoints;k++)
468 // check if there is already a matching point (no duplicates)
469 for (m = 0;m < numpoints;m++)
470 if (VectorDistance2(w->points[k], pointsbuf[m].v) < DIST_EPSILON)
473 // if there is no match, add a new one
476 // check if there are too many and skip the brush
477 if (numpoints >= 256)
479 Con_Printf("Collision_NewBrushFromPlanes: failed to build collision brush: too many points for buffer\n");
484 VectorCopy(w->points[k], pointsbuf[numpoints].v);
488 // store the index into a buffer
495 // add the triangles for the polygon
496 // (this particular code makes a triangle fan)
497 for (k = 0;k < numpolypoints - 2;k++)
500 elementsbuf[numelements++] = polypointbuf[0];
501 elementsbuf[numelements++] = polypointbuf[k + 1];
502 elementsbuf[numelements++] = polypointbuf[k + 2];
506 VectorCopy(originalplanes[j].normal, planesbuf[numplanes].normal);
507 planesbuf[numplanes].dist = originalplanes[j].dist;
511 // if nothing is left, there's nothing to allocate
512 if (numtriangles < 4 || numplanes < 4 || numpoints < 4)
515 // allocate the brush and copy to it
516 brush = Collision_AllocBrushFloat(mempool, numpoints, numplanes, numtriangles, supercontents);
517 memcpy(brush->points, pointsbuf, numpoints * sizeof(colpointf_t));
518 memcpy(brush->planes, planesbuf, numplanes * sizeof(colplanef_t));
519 memcpy(brush->elements, elementsbuf, numtriangles * sizeof(int[3]));
521 for (j = 0;j < brush->numplanes;j++)
522 brush->planes[j].dist = furthestplanedist_float(brush->planes[j].normal, brush->points, brush->numpoints);
523 VectorCopy(brush->points[0].v, brush->mins);
524 VectorCopy(brush->points[0].v, brush->maxs);
525 for (j = 1;j < brush->numpoints;j++)
527 brush->mins[0] = min(brush->mins[0], brush->points[j].v[0]);
528 brush->mins[1] = min(brush->mins[1], brush->points[j].v[1]);
529 brush->mins[2] = min(brush->mins[2], brush->points[j].v[2]);
530 brush->maxs[0] = max(brush->maxs[0], brush->points[j].v[0]);
531 brush->maxs[1] = max(brush->maxs[1], brush->points[j].v[1]);
532 brush->maxs[2] = max(brush->maxs[2], brush->points[j].v[2]);
534 Collision_ValidateBrush(brush);
540 colbrushf_t *Collision_AllocBrushFloat(mempool_t *mempool, int numpoints, int numplanes, int numtriangles, int supercontents)
543 brush = Mem_Alloc(mempool, sizeof(colbrushf_t) + sizeof(colpointf_t) * numpoints + sizeof(colplanef_t) * numplanes + sizeof(int[3]) * numtriangles);
544 brush->supercontents = supercontents;
545 brush->numplanes = numplanes;
546 brush->numpoints = numpoints;
547 brush->numtriangles = numtriangles;
548 brush->planes = (void *)(brush + 1);
549 brush->points = (void *)(brush->planes + brush->numplanes);
550 brush->elements = (void *)(brush->points + brush->numpoints);
554 void Collision_CalcPlanesForPolygonBrushFloat(colbrushf_t *brush)
557 float edge0[3], edge1[3], edge2[3], normal[3], dist, bestdist, temp[3];
560 if (brush->numpoints == 3)
562 // optimized triangle case
563 TriangleNormal(brush->points[0].v, brush->points[1].v, brush->points[2].v, brush->planes[0].normal);
564 if (DotProduct(brush->planes[0].normal, brush->planes[0].normal) < 0.0001f)
566 // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
567 brush->numplanes = 0;
572 brush->numplanes = 5;
573 VectorNormalize(brush->planes[0].normal);
574 brush->planes[0].dist = DotProduct(brush->points->v, brush->planes[0].normal);
575 VectorNegate(brush->planes[0].normal, brush->planes[1].normal);
576 brush->planes[1].dist = -brush->planes[0].dist;
577 VectorSubtract(brush->points[2].v, brush->points[0].v, edge0);
578 VectorSubtract(brush->points[0].v, brush->points[1].v, edge1);
579 VectorSubtract(brush->points[1].v, brush->points[2].v, edge2);
580 CrossProduct(edge0, brush->planes->normal, brush->planes[2].normal);
581 CrossProduct(edge1, brush->planes->normal, brush->planes[3].normal);
582 CrossProduct(edge2, brush->planes->normal, brush->planes[4].normal);
583 VectorNormalize(brush->planes[2].normal);
584 VectorNormalize(brush->planes[3].normal);
585 VectorNormalize(brush->planes[4].normal);
586 brush->planes[2].dist = DotProduct(brush->points[2].v, brush->planes[2].normal);
587 brush->planes[3].dist = DotProduct(brush->points[0].v, brush->planes[3].normal);
588 brush->planes[4].dist = DotProduct(brush->points[1].v, brush->planes[4].normal);
590 if (developer.integer)
593 //VectorSubtract(brush->points[0].v, brush->points[1].v, edge0);
594 //VectorSubtract(brush->points[2].v, brush->points[1].v, edge1);
595 CrossProduct(edge1, edge0, normal);
596 VectorNormalize(normal);
597 VectorSubtract(normal, brush->planes[0].normal, temp);
598 if (VectorLength(temp) > 0.01f)
599 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]);
600 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)
601 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);
602 if (fabs(DotProduct(brush->planes[2].normal, brush->planes[0].normal)) > 0.01f)
603 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);
604 if (fabs(DotProduct(brush->planes[3].normal, brush->planes[0].normal)) > 0.01f)
605 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);
606 if (fabs(DotProduct(brush->planes[4].normal, brush->planes[0].normal)) > 0.01f)
607 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);
608 if (fabs(DotProduct(brush->planes[2].normal, edge0)) > 0.01f)
609 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]);
610 if (fabs(DotProduct(brush->planes[3].normal, edge1)) > 0.01f)
611 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]);
612 if (fabs(DotProduct(brush->planes[4].normal, edge2)) > 0.01f)
613 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]);
614 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)
615 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);
616 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)
617 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);
618 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)
619 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);
620 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)
621 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);
622 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)
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[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);
629 // choose best surface normal for polygon's plane
631 for (i = 0, p = brush->points + 1;i < brush->numpoints - 2;i++, p++)
633 VectorSubtract(p[-1].v, p[0].v, edge0);
634 VectorSubtract(p[1].v, p[0].v, edge1);
635 CrossProduct(edge0, edge1, normal);
636 //TriangleNormal(p[-1].v, p[0].v, p[1].v, normal);
637 dist = DotProduct(normal, normal);
638 if (i == 0 || bestdist < dist)
641 VectorCopy(normal, brush->planes->normal);
644 if (bestdist < 0.0001f)
646 // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
647 brush->numplanes = 0;
652 brush->numplanes = brush->numpoints + 2;
653 VectorNormalize(brush->planes->normal);
654 brush->planes->dist = DotProduct(brush->points->v, brush->planes->normal);
656 // negate plane to create other side
657 VectorNegate(brush->planes[0].normal, brush->planes[1].normal);
658 brush->planes[1].dist = -brush->planes[0].dist;
659 for (i = 0, p = brush->points + (brush->numpoints - 1), p2 = brush->points;i < brush->numpoints;i++, p = p2, p2++)
661 VectorSubtract(p->v, p2->v, edge0);
662 CrossProduct(edge0, brush->planes->normal, brush->planes[i + 2].normal);
663 VectorNormalize(brush->planes[i + 2].normal);
664 brush->planes[i + 2].dist = DotProduct(p->v, brush->planes[i + 2].normal);
669 if (developer.integer)
671 // validity check - will be disabled later
672 Collision_ValidateBrush(brush);
673 for (i = 0;i < brush->numplanes;i++)
676 for (j = 0, p = brush->points;j < brush->numpoints;j++, p++)
677 if (DotProduct(p->v, brush->planes[i].normal) > brush->planes[i].dist + (1.0 / 32.0))
678 Con_Printf("Error in brush plane generation, plane %i\n", i);
683 colbrushf_t *Collision_AllocBrushFromPermanentPolygonFloat(mempool_t *mempool, int numpoints, float *points, int supercontents)
686 brush = Mem_Alloc(mempool, sizeof(colbrushf_t) + sizeof(colplanef_t) * (numpoints + 2));
687 brush->supercontents = supercontents;
688 brush->numpoints = numpoints;
689 brush->numplanes = numpoints + 2;
690 brush->planes = (void *)(brush + 1);
691 brush->points = (colpointf_t *)points;
692 Host_Error("Collision_AllocBrushFromPermanentPolygonFloat: FIXME: this code needs to be updated to generate a mesh...\n");
696 #define COLLISIONEPSILON (1.0f / 32.0f)
697 #define COLLISIONEPSILON2 0//(1.0f / 32.0f)
699 // NOTE: start and end of each brush pair must have same numplanes/numpoints
700 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)
702 int nplane, nplane2, fstartsolid, fendsolid, brushsolid;
703 float enterfrac, leavefrac, d1, d2, f, newimpactnormal[3], enterfrac2;
704 const colplanef_t *startplane, *endplane;
712 for (nplane = 0;nplane < thatbrush_start->numplanes + thisbrush_start->numplanes;nplane++)
715 if (nplane2 >= thatbrush_start->numplanes)
717 nplane2 -= thatbrush_start->numplanes;
718 startplane = thisbrush_start->planes + nplane2;
719 endplane = thisbrush_end->planes + nplane2;
720 if (developer.integer)
722 // any brush with degenerate planes is not worth handling
723 if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
725 Con_Printf("Collision_TraceBrushBrushFloat: degenerate thisbrush plane!\n");
728 f = furthestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints);
729 if (fabs(f - startplane->dist) > 0.01f)
730 Con_Printf("startplane->dist %f != calculated %f (thisbrush_start)\n", startplane->dist, f);
732 d1 = nearestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints) - furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints);
733 d2 = nearestplanedist_float(endplane->normal, thisbrush_end->points, thisbrush_end->numpoints) - furthestplanedist_float(endplane->normal, thatbrush_end->points, thatbrush_end->numpoints) - COLLISIONEPSILON2;
737 startplane = thatbrush_start->planes + nplane2;
738 endplane = thatbrush_end->planes + nplane2;
739 if (developer.integer)
741 // any brush with degenerate planes is not worth handling
742 if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
744 Con_Printf("Collision_TraceBrushBrushFloat: degenerate thatbrush plane!\n");
747 f = furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints);
748 if (fabs(f - startplane->dist) > 0.01f)
749 Con_Printf("startplane->dist %f != calculated %f (thatbrush_start)\n", startplane->dist, f);
751 d1 = nearestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints) - startplane->dist;
752 d2 = nearestplanedist_float(endplane->normal, thisbrush_end->points, thisbrush_end->numpoints) - startplane->dist - COLLISIONEPSILON2;
754 //Con_Printf("%c%i: d1 = %f, d2 = %f, d1 / (d1 - d2) = %f\n", nplane2 != nplane ? 'b' : 'a', nplane2, d1, d2, d1 / (d1 - d2));
770 enterfrac2 = (d1 - COLLISIONEPSILON) / (d1 - d2);
771 VectorLerp(startplane->normal, enterfrac, endplane->normal, newimpactnormal);
776 // moving out of brush
789 brushsolid = trace->hitsupercontentsmask & thatbrush_start->supercontents;
792 trace->startsupercontents |= thatbrush_start->supercontents;
795 trace->startsolid = true;
797 trace->allsolid = true;
801 // LordHavoc: we need an epsilon nudge here because for a point trace the
802 // penetrating line segment is normally zero length if this brush was
803 // generated from a polygon (infinitely thin), and could even be slightly
804 // positive or negative due to rounding errors in that case.
805 if (brushsolid && enterfrac > -1 && enterfrac < trace->realfraction && enterfrac - (1.0f / 1024.0f) <= leavefrac)
807 trace->realfraction = bound(0, enterfrac, 1);
808 trace->fraction = bound(0, enterfrac2, 1);
809 VectorCopy(newimpactnormal, trace->plane.normal);
813 // NOTE: start and end brush pair must have same numplanes/numpoints
814 void Collision_TraceLineBrushFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, const colbrushf_t *thatbrush_start, const colbrushf_t *thatbrush_end)
816 int nplane, fstartsolid, fendsolid, brushsolid;
817 float enterfrac, leavefrac, d1, d2, f, newimpactnormal[3], enterfrac2;
818 const colplanef_t *startplane, *endplane;
826 for (nplane = 0;nplane < thatbrush_start->numplanes;nplane++)
828 startplane = thatbrush_start->planes + nplane;
829 endplane = thatbrush_end->planes + nplane;
830 d1 = DotProduct(startplane->normal, linestart) - startplane->dist;
831 d2 = DotProduct(endplane->normal, lineend) - endplane->dist;
832 if (developer.integer)
834 // any brush with degenerate planes is not worth handling
835 if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
837 Con_Printf("Collision_TraceLineBrushFloat: degenerate plane!\n");
840 if (thatbrush_start->numpoints)
842 f = furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints);
843 if (fabs(f - startplane->dist) > 0.01f)
844 Con_Printf("startplane->dist %f != calculated %f\n", startplane->dist, f);
862 enterfrac2 = (d1 - COLLISIONEPSILON) / (d1 - d2);
863 VectorLerp(startplane->normal, enterfrac, endplane->normal, newimpactnormal);
868 // moving out of brush
881 brushsolid = trace->hitsupercontentsmask & thatbrush_start->supercontents;
884 trace->startsupercontents |= thatbrush_start->supercontents;
887 trace->startsolid = true;
889 trace->allsolid = true;
893 // LordHavoc: we need an epsilon nudge here because for a point trace the
894 // penetrating line segment is normally zero length if this brush was
895 // generated from a polygon (infinitely thin), and could even be slightly
896 // positive or negative due to rounding errors in that case.
897 if (brushsolid && enterfrac > -1 && enterfrac < trace->realfraction && enterfrac - (1.0f / 1024.0f) <= leavefrac)
899 trace->realfraction = bound(0, enterfrac, 1);
900 trace->fraction = bound(0, enterfrac2, 1);
901 VectorCopy(newimpactnormal, trace->plane.normal);
905 void Collision_TracePointBrushFloat(trace_t *trace, const vec3_t point, const colbrushf_t *thatbrush)
908 const colplanef_t *plane;
910 for (nplane = 0, plane = thatbrush->planes;nplane < thatbrush->numplanes;nplane++, plane++)
911 if (DotProduct(plane->normal, point) > plane->dist)
914 trace->startsupercontents |= thatbrush->supercontents;
915 if (trace->hitsupercontentsmask & thatbrush->supercontents)
917 trace->startsolid = true;
918 trace->allsolid = true;
922 static colpointf_t polyf_points[256];
923 static colplanef_t polyf_planes[256 + 2];
924 static colbrushf_t polyf_brush;
926 void Collision_TraceBrushPolygonFloat(trace_t *trace, const colbrushf_t *thisbrush_start, const colbrushf_t *thisbrush_end, int numpoints, const float *points, int supercontents)
930 Con_Printf("Polygon with more than 256 points not supported yet (fixme!)\n");
933 polyf_brush.numpoints = numpoints;
934 polyf_brush.numplanes = numpoints + 2;
935 polyf_brush.points = (colpointf_t *)points;
936 polyf_brush.planes = polyf_planes;
937 polyf_brush.supercontents = supercontents;
938 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
939 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
940 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brush, &polyf_brush);
943 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)
946 float facemins[3], facemaxs[3];
947 polyf_brush.numpoints = 3;
948 polyf_brush.numplanes = 5;
949 polyf_brush.points = polyf_points;
950 polyf_brush.planes = polyf_planes;
951 polyf_brush.supercontents = supercontents;
952 for (i = 0;i < numtriangles;i++, element3i += 3)
954 VectorCopy(vertex3f + element3i[0] * 3, polyf_points[0].v);
955 VectorCopy(vertex3f + element3i[1] * 3, polyf_points[1].v);
956 VectorCopy(vertex3f + element3i[2] * 3, polyf_points[2].v);
957 facemins[0] = min(polyf_points[0].v[0], min(polyf_points[1].v[0], polyf_points[2].v[0]));
958 facemins[1] = min(polyf_points[0].v[1], min(polyf_points[1].v[1], polyf_points[2].v[1]));
959 facemins[2] = min(polyf_points[0].v[2], min(polyf_points[1].v[2], polyf_points[2].v[2]));
960 facemaxs[0] = max(polyf_points[0].v[0], max(polyf_points[1].v[0], polyf_points[2].v[0]));
961 facemaxs[1] = max(polyf_points[0].v[1], max(polyf_points[1].v[1], polyf_points[2].v[1]));
962 facemaxs[2] = max(polyf_points[0].v[2], max(polyf_points[1].v[2], polyf_points[2].v[2]));
963 if (BoxesOverlap(segmentmins, segmentmaxs, facemins, facemaxs))
965 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
966 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
967 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brush, &polyf_brush);
972 void Collision_TraceLinePolygonFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, int numpoints, const float *points, int supercontents)
976 Con_Printf("Polygon with more than 256 points not supported yet (fixme!)\n");
979 polyf_brush.numpoints = numpoints;
980 polyf_brush.numplanes = numpoints + 2;
981 polyf_brush.points = (colpointf_t *)points;
982 polyf_brush.planes = polyf_planes;
983 polyf_brush.supercontents = supercontents;
984 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
985 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
986 Collision_TraceLineBrushFloat(trace, linestart, lineend, &polyf_brush, &polyf_brush);
989 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)
993 for (i = 0;i < numtriangles;i++, element3i += 3)
994 Collision_TraceLineTriangleFloat(trace, linestart, lineend, vertex3f + element3i[0] * 3, vertex3f + element3i[1] * 3, vertex3f + element3i[2] * 3);
996 polyf_brush.numpoints = 3;
997 polyf_brush.numplanes = 5;
998 polyf_brush.points = polyf_points;
999 polyf_brush.planes = polyf_planes;
1000 polyf_brush.supercontents = supercontents;
1001 for (i = 0;i < numtriangles;i++, element3i += 3)
1003 float facemins[3], facemaxs[3];
1004 VectorCopy(vertex3f + element3i[0] * 3, polyf_points[0].v);
1005 VectorCopy(vertex3f + element3i[1] * 3, polyf_points[1].v);
1006 VectorCopy(vertex3f + element3i[2] * 3, polyf_points[2].v);
1007 facemins[0] = min(polyf_points[0].v[0], min(polyf_points[1].v[0], polyf_points[2].v[0]));
1008 facemins[1] = min(polyf_points[0].v[1], min(polyf_points[1].v[1], polyf_points[2].v[1]));
1009 facemins[2] = min(polyf_points[0].v[2], min(polyf_points[1].v[2], polyf_points[2].v[2]));
1010 facemaxs[0] = max(polyf_points[0].v[0], max(polyf_points[1].v[0], polyf_points[2].v[0]));
1011 facemaxs[1] = max(polyf_points[0].v[1], max(polyf_points[1].v[1], polyf_points[2].v[1]));
1012 facemaxs[2] = max(polyf_points[0].v[2], max(polyf_points[1].v[2], polyf_points[2].v[2]));
1013 if (BoxesOverlap(segmentmins, segmentmaxs, facemins, facemaxs))
1015 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
1016 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
1017 Collision_TraceLineBrushFloat(trace, linestart, lineend, &polyf_brush, &polyf_brush);
1024 static colpointf_t polyf_pointsstart[256], polyf_pointsend[256];
1025 static colplanef_t polyf_planesstart[256 + 2], polyf_planesend[256 + 2];
1026 static colbrushf_t polyf_brushstart, polyf_brushend;
1028 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)
1031 if (numpoints > 256)
1033 Con_Printf("Polygon with more than 256 points not supported yet (fixme!)\n");
1036 polyf_brushstart.numpoints = numpoints;
1037 polyf_brushstart.numplanes = numpoints + 2;
1038 polyf_brushstart.points = polyf_pointsstart;//(colpointf_t *)points;
1039 polyf_brushstart.planes = polyf_planesstart;
1040 polyf_brushstart.supercontents = supercontents;
1041 for (i = 0;i < numpoints;i++)
1042 Matrix4x4_Transform(polygonmatrixstart, points + i * 3, polyf_brushstart.points[i].v);
1043 polyf_brushend.numpoints = numpoints;
1044 polyf_brushend.numplanes = numpoints + 2;
1045 polyf_brushend.points = polyf_pointsend;//(colpointf_t *)points;
1046 polyf_brushend.planes = polyf_planesend;
1047 polyf_brushend.supercontents = supercontents;
1048 for (i = 0;i < numpoints;i++)
1049 Matrix4x4_Transform(polygonmatrixend, points + i * 3, polyf_brushend.points[i].v);
1050 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brushstart);
1051 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brushend);
1053 //Collision_PrintBrushAsQHull(&polyf_brushstart, "polyf_brushstart");
1054 //Collision_PrintBrushAsQHull(&polyf_brushend, "polyf_brushend");
1056 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brushstart, &polyf_brushend);
1061 #define MAX_BRUSHFORBOX 16
1062 static int brushforbox_index = 0;
1063 static colpointf_t brushforbox_point[MAX_BRUSHFORBOX*8];
1064 static colplanef_t brushforbox_plane[MAX_BRUSHFORBOX*6];
1065 static colbrushf_t brushforbox_brush[MAX_BRUSHFORBOX];
1066 static colbrushf_t brushforpoint_brush[MAX_BRUSHFORBOX];
1068 void Collision_InitBrushForBox(void)
1071 for (i = 0;i < MAX_BRUSHFORBOX;i++)
1073 brushforbox_brush[i].supercontents = SUPERCONTENTS_SOLID;
1074 brushforbox_brush[i].numpoints = 8;
1075 brushforbox_brush[i].numplanes = 6;
1076 brushforbox_brush[i].points = brushforbox_point + i * 8;
1077 brushforbox_brush[i].planes = brushforbox_plane + i * 6;
1078 brushforpoint_brush[i].supercontents = SUPERCONTENTS_SOLID;
1079 brushforpoint_brush[i].numpoints = 1;
1080 brushforpoint_brush[i].numplanes = 0;
1081 brushforpoint_brush[i].points = brushforbox_point + i * 8;
1082 brushforpoint_brush[i].planes = brushforbox_plane + i * 6;
1086 colbrushf_t *Collision_BrushForBox(const matrix4x4_t *matrix, const vec3_t mins, const vec3_t maxs)
1091 if (brushforbox_brush[0].numpoints == 0)
1092 Collision_InitBrushForBox();
1093 if (VectorCompare(mins, maxs))
1096 brush = brushforpoint_brush + ((brushforbox_index++) % MAX_BRUSHFORBOX);
1097 VectorCopy(mins, brush->points->v);
1101 brush = brushforbox_brush + ((brushforbox_index++) % MAX_BRUSHFORBOX);
1103 for (i = 0;i < 8;i++)
1105 v[0] = i & 1 ? maxs[0] : mins[0];
1106 v[1] = i & 2 ? maxs[1] : mins[1];
1107 v[2] = i & 4 ? maxs[2] : mins[2];
1108 Matrix4x4_Transform(matrix, v, brush->points[i].v);
1111 for (i = 0;i < 6;i++)
1114 v[i >> 1] = i & 1 ? 1 : -1;
1115 Matrix4x4_Transform3x3(matrix, v, brush->planes[i].normal);
1116 VectorNormalize(brush->planes[i].normal);
1119 for (j = 0;j < brush->numplanes;j++)
1120 brush->planes[j].dist = furthestplanedist_float(brush->planes[j].normal, brush->points, brush->numpoints);
1121 VectorCopy(brush->points[0].v, brush->mins);
1122 VectorCopy(brush->points[0].v, brush->maxs);
1123 for (j = 1;j < brush->numpoints;j++)
1125 brush->mins[0] = min(brush->mins[0], brush->points[j].v[0]);
1126 brush->mins[1] = min(brush->mins[1], brush->points[j].v[1]);
1127 brush->mins[2] = min(brush->mins[2], brush->points[j].v[2]);
1128 brush->maxs[0] = max(brush->maxs[0], brush->points[j].v[0]);
1129 brush->maxs[1] = max(brush->maxs[1], brush->points[j].v[1]);
1130 brush->maxs[2] = max(brush->maxs[2], brush->points[j].v[2]);
1132 Collision_ValidateBrush(brush);
1136 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)
1138 colbrushf_t *boxbrush, *thisbrush_start, *thisbrush_end;
1139 matrix4x4_t identitymatrix;
1140 vec3_t startmins, startmaxs, endmins, endmaxs;
1142 // create brushes for the collision
1143 VectorAdd(start, mins, startmins);
1144 VectorAdd(start, maxs, startmaxs);
1145 VectorAdd(end, mins, endmins);
1146 VectorAdd(end, maxs, endmaxs);
1147 Matrix4x4_CreateIdentity(&identitymatrix);
1148 boxbrush = Collision_BrushForBox(&identitymatrix, cmins, cmaxs);
1149 thisbrush_start = Collision_BrushForBox(&identitymatrix, startmins, startmaxs);
1150 thisbrush_end = Collision_BrushForBox(&identitymatrix, endmins, endmaxs);
1152 memset(trace, 0, sizeof(trace_t));
1153 trace->hitsupercontentsmask = hitsupercontentsmask;
1154 trace->fraction = 1;
1155 trace->realfraction = 1;
1156 trace->allsolid = true;
1157 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, boxbrush, boxbrush);
1160 // LordHavoc: currently unused and not yet tested
1161 // note: this can be used for tracing a moving sphere vs a stationary sphere,
1162 // by simply adding the moving sphere's radius to the sphereradius parameter,
1163 // all the results are correct (impactpoint, impactnormal, and fraction)
1164 float Collision_ClipTrace_Line_Sphere(double *linestart, double *lineend, double *sphereorigin, double sphereradius, double *impactpoint, double *impactnormal)
1166 double dir[3], scale, v[3], deviationdist, impactdist, linelength;
1167 // make sure the impactpoint and impactnormal are valid even if there is
1169 impactpoint[0] = lineend[0];
1170 impactpoint[1] = lineend[1];
1171 impactpoint[2] = lineend[2];
1172 impactnormal[0] = 0;
1173 impactnormal[1] = 0;
1174 impactnormal[2] = 0;
1175 // calculate line direction
1176 dir[0] = lineend[0] - linestart[0];
1177 dir[1] = lineend[1] - linestart[1];
1178 dir[2] = lineend[2] - linestart[2];
1179 // normalize direction
1180 linelength = sqrt(dir[0] * dir[0] + dir[1] * dir[1] + dir[2] * dir[2]);
1183 scale = 1.0 / linelength;
1188 // this dotproduct calculates the distance along the line at which the
1189 // sphere origin is (nearest point to the sphere origin on the line)
1190 impactdist = dir[0] * (sphereorigin[0] - linestart[0]) + dir[1] * (sphereorigin[1] - linestart[1]) + dir[2] * (sphereorigin[2] - linestart[2]);
1191 // calculate point on line at that distance, and subtract the
1192 // sphereorigin from it, so we have a vector to measure for the distance
1193 // of the line from the sphereorigin (deviation, how off-center it is)
1194 v[0] = linestart[0] + impactdist * dir[0] - sphereorigin[0];
1195 v[1] = linestart[1] + impactdist * dir[1] - sphereorigin[1];
1196 v[2] = linestart[2] + impactdist * dir[2] - sphereorigin[2];
1197 deviationdist = v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
1198 // if outside the radius, it's a miss for sure
1199 // (we do this comparison using squared radius to avoid a sqrt)
1200 if (deviationdist > sphereradius*sphereradius)
1201 return 1; // miss (off to the side)
1202 // nudge back to find the correct impact distance
1203 impactdist += (sqrt(deviationdist) - sphereradius);
1204 if (impactdist >= linelength)
1205 return 1; // miss (not close enough)
1207 return 1; // miss (linestart is past or inside sphere)
1208 // calculate new impactpoint
1209 impactpoint[0] = linestart[0] + impactdist * dir[0];
1210 impactpoint[1] = linestart[1] + impactdist * dir[1];
1211 impactpoint[2] = linestart[2] + impactdist * dir[2];
1212 // calculate impactnormal (surface normal at point of impact)
1213 impactnormal[0] = impactpoint[0] - sphereorigin[0];
1214 impactnormal[1] = impactpoint[1] - sphereorigin[1];
1215 impactnormal[2] = impactpoint[2] - sphereorigin[2];
1216 // normalize impactnormal
1217 scale = impactnormal[0] * impactnormal[0] + impactnormal[1] * impactnormal[1] + impactnormal[2] * impactnormal[2];
1220 scale = 1.0 / sqrt(scale);
1221 impactnormal[0] *= scale;
1222 impactnormal[1] *= scale;
1223 impactnormal[2] *= scale;
1225 // return fraction of movement distance
1226 return impactdist / linelength;
1229 void Collision_TraceLineTriangleFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, const float *point0, const float *point1, const float *point2)
1231 float d1, d2, d, f, fnudged, impact[3], edgenormal[3], faceplanenormal[3], faceplanedist, edge[3];
1233 // this code is designed for clockwise triangles, conversion to
1234 // counterclockwise would require swapping some things around...
1235 // it is easier to simply swap the point0 and point2 parameters to this
1236 // function when calling it than it is to rewire the internals.
1238 // calculate the faceplanenormal of the triangle, this represents the front side
1239 TriangleNormal(point0, point1, point2, faceplanenormal);
1240 // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
1241 if (DotProduct(faceplanenormal, faceplanenormal) < 0.0001f)
1243 // normalize the normal
1244 VectorNormalize(faceplanenormal);
1245 // calculate the distance
1246 faceplanedist = DotProduct(point0, faceplanenormal);
1248 // calculate the start distance
1249 d1 = DotProduct(faceplanenormal, linestart) - faceplanedist;
1250 // if start point is on the back side there is no collision
1251 // (we don't care about traces going through the triangle the wrong way)
1255 // calculate the end distance
1256 d2 = DotProduct(faceplanenormal, lineend) - faceplanedist;
1257 // if both are in front, there is no collision
1261 // from here on we know d1 is >= 0 and d2 is < 0
1262 // this means the line starts infront and ends behind, passing through it
1264 // calculate the recipricol of the distance delta,
1265 // so we can use it multiple times cheaply (instead of division)
1266 d = 1.0f / (d1 - d2);
1267 // calculate the impact fraction by taking the start distance (> 0)
1268 // and subtracting the face plane distance (this is the distance of the
1269 // triangle along that same normal)
1270 // then multiply by the recipricol distance delta
1272 // skip out if this impact is further away than previous ones
1273 if (f > trace->realfraction)
1275 // calculate the perfect impact point for classification of insidedness
1276 impact[0] = linestart[0] + f * (lineend[0] - linestart[0]);
1277 impact[1] = linestart[1] + f * (lineend[1] - linestart[1]);
1278 impact[2] = linestart[2] + f * (lineend[2] - linestart[2]);
1280 // calculate the edge normal and reject if impact is outside triangle
1281 // (an edge normal faces away from the triangle, to get the desired normal
1282 // a crossproduct with the faceplanenormal is used, and because of the way
1283 // the insidedness comparison is written it does not need to be normalized)
1285 VectorSubtract(point2, point0, edge);
1286 CrossProduct(edge, faceplanenormal, edgenormal);
1287 if (DotProduct(impact, edgenormal) > DotProduct(point0, edgenormal))
1290 VectorSubtract(point0, point1, edge);
1291 CrossProduct(edge, faceplanenormal, edgenormal);
1292 if (DotProduct(impact, edgenormal) > DotProduct(point1, edgenormal))
1295 VectorSubtract(point1, point2, edge);
1296 CrossProduct(edge, faceplanenormal, edgenormal);
1297 if (DotProduct(impact, edgenormal) > DotProduct(point2, edgenormal))
1300 // store the new trace fraction
1301 trace->realfraction = bound(0, f, 1);
1303 // calculate a nudged fraction to keep it out of the surface
1304 // (the main fraction remains perfect)
1305 fnudged = (d1 - COLLISIONEPSILON) * d;
1306 trace->fraction = bound(0, fnudged, 1);
1308 // store the new trace endpos
1309 // not needed, it's calculated later when the trace is finished
1310 //trace->endpos[0] = linestart[0] + fnudged * (lineend[0] - linestart[0]);
1311 //trace->endpos[1] = linestart[1] + fnudged * (lineend[1] - linestart[1]);
1312 //trace->endpos[2] = linestart[2] + fnudged * (lineend[2] - linestart[2]);
1314 // store the new trace plane (because collisions only happen from
1315 // the front this is always simply the triangle normal, never flipped)
1316 VectorCopy(faceplanenormal, trace->plane.normal);
1317 trace->plane.dist = faceplanedist;
1320 typedef struct colbspnode_s
1323 struct colbspnode_s *children[2];
1324 // the node is reallocated or split if max is reached
1327 colbrushf_t **colbrushflist;
1330 //colbrushd_t **colbrushdlist;
1334 typedef struct colbsp_s
1337 colbspnode_t *nodes;
1341 colbsp_t *Collision_CreateCollisionBSP(mempool_t *mempool)
1344 bsp = Mem_Alloc(mempool, sizeof(colbsp_t));
1345 bsp->mempool = mempool;
1346 bsp->nodes = Mem_Alloc(bsp->mempool, sizeof(colbspnode_t));
1350 void Collision_FreeCollisionBSPNode(colbspnode_t *node)
1352 if (node->children[0])
1353 Collision_FreeCollisionBSPNode(node->children[0]);
1354 if (node->children[1])
1355 Collision_FreeCollisionBSPNode(node->children[1]);
1356 while (--node->numcolbrushf)
1357 Mem_Free(node->colbrushflist[node->numcolbrushf]);
1358 //while (--node->numcolbrushd)
1359 // Mem_Free(node->colbrushdlist[node->numcolbrushd]);
1363 void Collision_FreeCollisionBSP(colbsp_t *bsp)
1365 Collision_FreeCollisionBSPNode(bsp->nodes);
1369 void Collision_BoundingBoxOfBrushTraceSegment(const colbrushf_t *start, const colbrushf_t *end, vec3_t mins, vec3_t maxs, float startfrac, float endfrac)
1372 colpointf_t *ps, *pe;
1373 float tempstart[3], tempend[3];
1374 VectorLerp(start->points[0].v, startfrac, end->points[0].v, mins);
1375 VectorCopy(mins, maxs);
1376 for (i = 0, ps = start->points, pe = end->points;i < start->numpoints;i++, ps++, pe++)
1378 VectorLerp(ps->v, startfrac, pe->v, tempstart);
1379 VectorLerp(ps->v, endfrac, pe->v, tempend);
1380 mins[0] = min(mins[0], min(tempstart[0], tempend[0]));
1381 mins[1] = min(mins[1], min(tempstart[1], tempend[1]));
1382 mins[2] = min(mins[2], min(tempstart[2], tempend[2]));
1383 maxs[0] = min(maxs[0], min(tempstart[0], tempend[0]));
1384 maxs[1] = min(maxs[1], min(tempstart[1], tempend[1]));
1385 maxs[2] = min(maxs[2], min(tempstart[2], tempend[2]));