7 // the hull we're tracing through
10 // the trace structure to fill in
13 // start and end of the trace (in model space)
20 // overrides the CONTENTS_SOLID in the box bsp tree
23 RecursiveHullCheckTraceInfo_t;
25 // 1/32 epsilon to keep floating point happy
26 #define DIST_EPSILON (0.03125)
28 #define HULLCHECKSTATE_EMPTY 0
29 #define HULLCHECKSTATE_SOLID 1
30 #define HULLCHECKSTATE_DONE 2
32 static int RecursiveHullCheck(RecursiveHullCheckTraceInfo_t *t, int num, double p1f, double p2f, double p1[3], double p2[3])
34 // status variables, these don't need to be saved on the stack when
35 // recursing... but are because this should be thread-safe
36 // (note: tracing against a bbox is not thread-safe, yet)
41 // variables that need to be stored on the stack when recursing
46 // LordHavoc: a goto! everyone flee in terror... :)
51 // translate the fake CONTENTS values in the box bsp tree
52 if (num == CONTENTS_SOLID)
53 num = t->boxsupercontents;
56 if (!t->trace->startfound)
58 t->trace->startfound = true;
59 t->trace->startsupercontents |= num;
61 if (num & t->trace->hitsupercontentsmask)
63 // if the first leaf is solid, set startsolid
64 if (t->trace->allsolid)
65 t->trace->startsolid = true;
66 return HULLCHECKSTATE_SOLID;
70 t->trace->allsolid = false;
71 return HULLCHECKSTATE_EMPTY;
75 // find the point distances
76 node = t->hull->clipnodes + num;
78 plane = t->hull->planes + node->planenum;
81 t1 = p1[plane->type] - plane->dist;
82 t2 = p2[plane->type] - plane->dist;
86 t1 = DotProduct (plane->normal, p1) - plane->dist;
87 t2 = DotProduct (plane->normal, p2) - plane->dist;
94 num = node->children[1];
103 num = node->children[0];
109 // the line intersects, find intersection point
110 // LordHavoc: this uses the original trace for maximum accuracy
113 t1 = t->start[plane->type] - plane->dist;
114 t2 = t->end[plane->type] - plane->dist;
118 t1 = DotProduct (plane->normal, t->start) - plane->dist;
119 t2 = DotProduct (plane->normal, t->end) - plane->dist;
122 midf = t1 / (t1 - t2);
123 midf = bound(p1f, midf, p2f);
124 VectorMA(t->start, midf, t->dist, mid);
126 // recurse both sides, front side first
127 ret = RecursiveHullCheck (t, node->children[side], p1f, midf, p1, mid);
128 // if this side is not empty, return what it is (solid or done)
129 if (ret != HULLCHECKSTATE_EMPTY)
132 ret = RecursiveHullCheck (t, node->children[side ^ 1], midf, p2f, mid, p2);
133 // if other side is not solid, return what it is (empty or done)
134 if (ret != HULLCHECKSTATE_SOLID)
137 // front is air and back is solid, this is the impact point...
140 t->trace->plane.dist = -plane->dist;
141 VectorNegate (plane->normal, t->trace->plane.normal);
145 t->trace->plane.dist = plane->dist;
146 VectorCopy (plane->normal, t->trace->plane.normal);
149 // bias away from surface a bit
150 t1 = DotProduct(t->trace->plane.normal, t->start) - (t->trace->plane.dist + DIST_EPSILON);
151 t2 = DotProduct(t->trace->plane.normal, t->end) - (t->trace->plane.dist + DIST_EPSILON);
153 midf = t1 / (t1 - t2);
154 t->trace->fraction = bound(0.0f, midf, 1.0);
156 VectorMA(t->start, t->trace->fraction, t->dist, t->trace->endpos);
158 return HULLCHECKSTATE_DONE;
162 // used if start and end are the same
163 static void RecursiveHullCheckPoint (RecursiveHullCheckTraceInfo_t *t, int num)
165 // If you can read this, you understand BSP trees
167 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];
170 t->trace->endcontents = num;
171 if (t->trace->thiscontents)
173 if (num == t->trace->thiscontents)
174 t->trace->allsolid = false;
177 // if the first leaf is solid, set startsolid
178 if (t->trace->allsolid)
179 t->trace->startsolid = true;
184 if (num != CONTENTS_SOLID)
186 t->trace->allsolid = false;
187 if (num == CONTENTS_EMPTY)
188 t->trace->inopen = true;
190 t->trace->inwater = true;
194 // if the first leaf is solid, set startsolid
195 if (t->trace->allsolid)
196 t->trace->startsolid = true;
202 static hull_t box_hull;
203 static dclipnode_t box_clipnodes[6];
204 static mplane_t box_planes[6];
206 void Collision_Init (void)
211 //Set up the planes and clipnodes so that the six floats of a bounding box
212 //can just be stored out and get a proper hull_t structure.
214 box_hull.clipnodes = box_clipnodes;
215 box_hull.planes = box_planes;
216 box_hull.firstclipnode = 0;
217 box_hull.lastclipnode = 5;
219 for (i = 0;i < 6;i++)
221 box_clipnodes[i].planenum = i;
225 box_clipnodes[i].children[side] = CONTENTS_EMPTY;
227 box_clipnodes[i].children[side^1] = i + 1;
229 box_clipnodes[i].children[side^1] = CONTENTS_SOLID;
231 box_planes[i].type = i>>1;
232 box_planes[i].normal[i>>1] = 1;
236 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)
238 RecursiveHullCheckTraceInfo_t rhc;
239 // fill in a default trace
240 memset(&rhc, 0, sizeof(rhc));
241 memset(trace, 0, sizeof(trace_t));
242 //To keep everything totally uniform, bounding boxes are turned into small
243 //BSP trees instead of being compared directly.
244 // create a temp hull from bounding box sizes
245 box_planes[0].dist = cmaxs[0] - mins[0];
246 box_planes[1].dist = cmins[0] - maxs[0];
247 box_planes[2].dist = cmaxs[1] - mins[1];
248 box_planes[3].dist = cmins[1] - maxs[1];
249 box_planes[4].dist = cmaxs[2] - mins[2];
250 box_planes[5].dist = cmins[2] - maxs[2];
251 // trace a line through the generated clipping hull
252 rhc.boxsupercontents = boxsupercontents;
253 rhc.hull = &box_hull;
255 rhc.trace->hitsupercontentsmask = hitsupercontentsmask;
256 rhc.trace->fraction = 1;
257 rhc.trace->allsolid = true;
258 VectorCopy(start, rhc.start);
259 VectorCopy(end, rhc.end);
260 VectorSubtract(rhc.end, rhc.start, rhc.dist);
261 RecursiveHullCheck(&rhc, rhc.hull->firstclipnode, 0, 1, rhc.start, rhc.end);
278 void Collision_PrintBrushAsQHull(colbrushf_t *brush, const char *name)
281 Con_Printf("3 %s\n%i\n", name, brush->numpoints);
282 for (i = 0;i < brush->numpoints;i++)
283 Con_Printf("%f %f %f\n", brush->points[i].v[0], brush->points[i].v[1], brush->points[i].v[2]);
285 Con_Printf("4\n%i\n", brush->numplanes);
286 for (i = 0;i < brush->numplanes;i++)
287 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);
290 void Collision_ValidateBrush(colbrushf_t *brush)
292 int j, k, pointsoffplanes, printbrush;
295 if (!brush->numpoints)
297 Con_Printf("Collision_ValidateBrush: brush with no points!\n");
301 // it's ok for a brush to have one point and no planes...
302 if (brush->numplanes == 0 && brush->numpoints != 1)
304 Con_Printf("Collision_ValidateBrush: brush with no planes and more than one point!\n");
308 if (brush->numplanes)
311 for (k = 0;k < brush->numplanes;k++)
313 if (DotProduct(brush->planes[k].normal, brush->planes[k].normal) < 0.0001f)
314 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);
315 for (j = 0;j < brush->numpoints;j++)
317 d = DotProduct(brush->points[j].v, brush->planes[k].normal) - brush->planes[k].dist;
318 if (d > (1.0f / 8.0f))
320 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);
327 if (pointsoffplanes == 0) // all points are on all planes
329 Con_Printf("Collision_ValidateBrush: all points lie on all planes (degenerate, no brush volume!)\n");
334 Collision_PrintBrushAsQHull(brush, "unnamed");
337 float nearestplanedist_float(const float *normal, const colpointf_t *points, int numpoints)
339 float dist, bestdist;
340 bestdist = DotProduct(points->v, normal);
344 dist = DotProduct(points->v, normal);
345 bestdist = min(bestdist, dist);
351 float furthestplanedist_float(const float *normal, const colpointf_t *points, int numpoints)
353 float dist, bestdist;
354 bestdist = DotProduct(points->v, normal);
358 dist = DotProduct(points->v, normal);
359 bestdist = max(bestdist, dist);
366 colbrushf_t *Collision_NewBrushFromPlanes(mempool_t *mempool, int numoriginalplanes, const mplane_t *originalplanes, int supercontents)
369 int numpoints, maxpoints, numplanes, maxplanes, numelements, maxelements, numtriangles, numpolypoints, maxpolypoints;
372 colpointf_t pointsbuf[256];
373 colplanef_t planesbuf[256];
374 int elementsbuf[1024];
375 int polypointbuf[256];
376 float mins[3], maxs[3];
377 // construct a collision brush (points, planes, and renderable mesh) from
378 // a set of planes, this also optimizes out any unnecessary planes (ones
379 // whose polygon is clipped away by the other planes)
380 numpoints = 0;maxpoints = 256;
381 numplanes = 0;maxplanes = 256;
382 numelements = 0;maxelements = 1024;
385 for (j = 0;j < numoriginalplanes;j++)
387 // add the plane uniquely (no duplicates)
388 for (k = 0;k < numplanes;k++)
389 if (VectorCompare(planesbuf[k].normal, originalplanes[j].normal) && planesbuf[k].dist == originalplanes[j].dist)
391 // if the plane is a duplicate, skip it
394 // check if there are too many and skip the brush
395 if (numplanes >= 256)
397 Con_Printf("Mod_Q3BSP_LoadBrushes: failed to build collision brush: too many planes for buffer\n");
401 // create a large polygon from the plane
402 w = Winding_NewFromPlane(originalplanes[j].normal[0], originalplanes[j].normal[1], originalplanes[j].normal[2], originalplanes[j].dist);
403 // clip it by all other planes
404 for (k = 0;k < numoriginalplanes && w;k++)
408 // we want to keep the inside of the brush plane so we flip
410 w = Winding_Clip(w, -originalplanes[k].normal[0], -originalplanes[k].normal[1], -originalplanes[k].normal[2], -originalplanes[k].dist, true);
413 // if nothing is left, skip it
417 // copy off the number of points for later when the winding is freed
418 numpolypoints = w->numpoints;
420 // check if there are too many polygon vertices for buffer
421 if (numpolypoints > maxpolypoints)
423 Con_Printf("Collision_NewBrushFromPlanes: failed to build collision brush: too many points for buffer\n");
427 // check if there are too many triangle elements for buffer
428 if (numelements + (w->numpoints - 2) * 3 > maxelements)
430 Con_Printf("Collision_NewBrushFromPlanes: failed to build collision brush: too many triangle elements for buffer\n");
434 for (k = 0;k < w->numpoints;k++)
436 // check if there is already a matching point (no duplicates)
437 for (m = 0;m < numpoints;m++)
438 if (VectorDistance2(w->points[k], pointsbuf[m].v) < DIST_EPSILON)
441 // if there is no match, add a new one
444 // check if there are too many and skip the brush
445 if (numpoints >= 256)
447 Con_Printf("Collision_NewBrushFromPlanes: failed to build collision brush: too many points for buffer\n");
452 VectorCopy(w->points[k], pointsbuf[numpoints].v);
456 // store the index into a buffer
462 // add the triangles for the polygon
463 // (this particular code makes a triangle fan)
464 for (k = 0;k < numpolypoints - 2;k++)
467 elementsbuf[numelements++] = polypointbuf[0];
468 elementsbuf[numelements++] = polypointbuf[k + 1];
469 elementsbuf[numelements++] = polypointbuf[k + 2];
473 VectorCopy(originalplanes[j].normal, planesbuf[numplanes].normal);
474 planesbuf[numplanes].dist = originalplanes[j].dist;
479 for (j = 0;j < numplanes;j++)
480 planesbuf[j].dist = furthestplanedist_float(planesbuf[j].normal, pointsbuf, numpoints);
484 VectorCopy(pointsbuf[0].v, mins);
485 VectorCopy(pointsbuf[0].v, maxs);
486 for (j = 1;j < numpoints;j++)
488 mins[0] = min(mins[0], pointsbuf[j].v[0]);
489 mins[1] = min(mins[1], pointsbuf[j].v[1]);
490 mins[2] = min(mins[2], pointsbuf[j].v[2]);
491 maxs[0] = max(maxs[0], pointsbuf[j].v[0]);
492 maxs[1] = max(maxs[1], pointsbuf[j].v[1]);
493 maxs[2] = max(maxs[2], pointsbuf[j].v[2]);
497 // if nothing is left, there's nothing to allocate
498 if (numtriangles < 4 || numplanes < 4 || numpoints < 4)
501 // allocate the brush and copy to it
502 brush = Collision_AllocBrushFloat(mempool, numpoints, numplanes, numtriangles, supercontents);
503 memcpy(brush->points, pointsbuf, numpoints * sizeof(colpointf_t));
504 memcpy(brush->planes, planesbuf, numplanes * sizeof(colplanef_t));
505 memcpy(brush->elements, elementsbuf, numtriangles * sizeof(int[3]));
506 VectorCopy(mins, brush->mins);
507 VectorCopy(maxs, brush->maxs);
508 Collision_ValidateBrush(brush);
514 colbrushf_t *Collision_AllocBrushFloat(mempool_t *mempool, int numpoints, int numplanes, int numtriangles, int supercontents)
517 brush = Mem_Alloc(mempool, sizeof(colbrushf_t) + sizeof(colpointf_t) * numpoints + sizeof(colplanef_t) * numplanes + sizeof(int[3]) * numtriangles);
518 brush->supercontents = supercontents;
519 brush->numplanes = numplanes;
520 brush->numpoints = numpoints;
521 brush->numtriangles = numtriangles;
522 brush->planes = (void *)(brush + 1);
523 brush->points = (void *)(brush->planes + brush->numplanes);
524 brush->elements = (void *)(brush->points + brush->numpoints);
528 void Collision_CalcPlanesForPolygonBrushFloat(colbrushf_t *brush)
531 float edge0[3], edge1[3], edge2[3], normal[3], dist, bestdist, temp[3];
534 if (brush->numpoints == 3)
536 // optimized triangle case
537 TriangleNormal(brush->points[0].v, brush->points[1].v, brush->points[2].v, brush->planes[0].normal);
538 if (DotProduct(brush->planes[0].normal, brush->planes[0].normal) < 0.0001f)
540 // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
541 brush->numplanes = 0;
546 brush->numplanes = 5;
547 VectorNormalize(brush->planes[0].normal);
548 brush->planes[0].dist = DotProduct(brush->points->v, brush->planes[0].normal);
549 VectorNegate(brush->planes[0].normal, brush->planes[1].normal);
550 brush->planes[1].dist = -brush->planes[0].dist;
551 VectorSubtract(brush->points[2].v, brush->points[0].v, edge0);
552 VectorSubtract(brush->points[0].v, brush->points[1].v, edge1);
553 VectorSubtract(brush->points[1].v, brush->points[2].v, edge2);
554 CrossProduct(edge0, brush->planes->normal, brush->planes[2].normal);
555 CrossProduct(edge1, brush->planes->normal, brush->planes[3].normal);
556 CrossProduct(edge2, brush->planes->normal, brush->planes[4].normal);
557 VectorNormalize(brush->planes[2].normal);
558 VectorNormalize(brush->planes[3].normal);
559 VectorNormalize(brush->planes[4].normal);
560 brush->planes[2].dist = DotProduct(brush->points[2].v, brush->planes[2].normal);
561 brush->planes[3].dist = DotProduct(brush->points[0].v, brush->planes[3].normal);
562 brush->planes[4].dist = DotProduct(brush->points[1].v, brush->planes[4].normal);
564 if (developer.integer)
567 //VectorSubtract(brush->points[0].v, brush->points[1].v, edge0);
568 //VectorSubtract(brush->points[2].v, brush->points[1].v, edge1);
569 CrossProduct(edge1, edge0, normal);
570 VectorNormalize(normal);
571 VectorSubtract(normal, brush->planes[0].normal, temp);
572 if (VectorLength(temp) > 0.01f)
573 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]);
574 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)
575 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);
576 if (fabs(DotProduct(brush->planes[2].normal, brush->planes[0].normal)) > 0.01f)
577 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);
578 if (fabs(DotProduct(brush->planes[3].normal, brush->planes[0].normal)) > 0.01f)
579 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);
580 if (fabs(DotProduct(brush->planes[4].normal, brush->planes[0].normal)) > 0.01f)
581 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);
582 if (fabs(DotProduct(brush->planes[2].normal, edge0)) > 0.01f)
583 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]);
584 if (fabs(DotProduct(brush->planes[3].normal, edge1)) > 0.01f)
585 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]);
586 if (fabs(DotProduct(brush->planes[4].normal, edge2)) > 0.01f)
587 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]);
588 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)
589 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);
590 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)
591 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);
592 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)
593 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);
594 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)
595 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);
596 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)
597 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);
603 // choose best surface normal for polygon's plane
605 for (i = 0, p = brush->points + 1;i < brush->numpoints - 2;i++, p++)
607 VectorSubtract(p[-1].v, p[0].v, edge0);
608 VectorSubtract(p[1].v, p[0].v, edge1);
609 CrossProduct(edge0, edge1, normal);
610 //TriangleNormal(p[-1].v, p[0].v, p[1].v, normal);
611 dist = DotProduct(normal, normal);
612 if (i == 0 || bestdist < dist)
615 VectorCopy(normal, brush->planes->normal);
618 if (bestdist < 0.0001f)
620 // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
621 brush->numplanes = 0;
626 brush->numplanes = brush->numpoints + 2;
627 VectorNormalize(brush->planes->normal);
628 brush->planes->dist = DotProduct(brush->points->v, brush->planes->normal);
630 // negate plane to create other side
631 VectorNegate(brush->planes[0].normal, brush->planes[1].normal);
632 brush->planes[1].dist = -brush->planes[0].dist;
633 for (i = 0, p = brush->points + (brush->numpoints - 1), p2 = brush->points;i < brush->numpoints;i++, p = p2, p2++)
635 VectorSubtract(p->v, p2->v, edge0);
636 CrossProduct(edge0, brush->planes->normal, brush->planes[i + 2].normal);
637 VectorNormalize(brush->planes[i + 2].normal);
638 brush->planes[i + 2].dist = DotProduct(p->v, brush->planes[i + 2].normal);
643 if (developer.integer)
645 // validity check - will be disabled later
646 Collision_ValidateBrush(brush);
647 for (i = 0;i < brush->numplanes;i++)
650 for (j = 0, p = brush->points;j < brush->numpoints;j++, p++)
651 if (DotProduct(p->v, brush->planes[i].normal) > brush->planes[i].dist + (1.0 / 32.0))
652 Con_Printf("Error in brush plane generation, plane %i\n", i);
657 colbrushf_t *Collision_AllocBrushFromPermanentPolygonFloat(mempool_t *mempool, int numpoints, float *points, int supercontents)
660 brush = Mem_Alloc(mempool, sizeof(colbrushf_t) + sizeof(colplanef_t) * (numpoints + 2));
661 brush->supercontents = supercontents;
662 brush->numpoints = numpoints;
663 brush->numplanes = numpoints + 2;
664 brush->planes = (void *)(brush + 1);
665 brush->points = (colpointf_t *)points;
666 Host_Error("Collision_AllocBrushFromPermanentPolygonFloat: FIXME: this code needs to be updated to generate a mesh...\n");
670 #define COLLISIONEPSILON (1.0f / 32.0f)
671 #define COLLISIONEPSILON2 0//(1.0f / 32.0f)
673 // NOTE: start and end of each brush pair must have same numplanes/numpoints
674 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)
676 int nplane, nplane2, fstartsolid, fendsolid, brushsolid;
677 float enterfrac, leavefrac, d1, d2, f, newimpactnormal[3];
678 const colplanef_t *startplane, *endplane;
685 for (nplane = 0;nplane < thatbrush_start->numplanes + thisbrush_start->numplanes;nplane++)
688 if (nplane2 >= thatbrush_start->numplanes)
690 nplane2 -= thatbrush_start->numplanes;
691 startplane = thisbrush_start->planes + nplane2;
692 endplane = thisbrush_end->planes + nplane2;
693 if (developer.integer)
695 // any brush with degenerate planes is not worth handling
696 if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
698 Con_Printf("Collision_TraceBrushBrushFloat: degenerate thisbrush plane!\n");
701 f = furthestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints);
702 if (fabs(f - startplane->dist) > 0.01f)
703 Con_Printf("startplane->dist %f != calculated %f (thisbrush_start)\n", startplane->dist, f);
705 d1 = nearestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints) - furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints);
706 d2 = nearestplanedist_float(endplane->normal, thisbrush_end->points, thisbrush_end->numpoints) - furthestplanedist_float(endplane->normal, thatbrush_end->points, thatbrush_end->numpoints) - COLLISIONEPSILON2;
710 startplane = thatbrush_start->planes + nplane2;
711 endplane = thatbrush_end->planes + nplane2;
712 if (developer.integer)
714 // any brush with degenerate planes is not worth handling
715 if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
717 Con_Printf("Collision_TraceBrushBrushFloat: degenerate thatbrush plane!\n");
720 f = furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints);
721 if (fabs(f - startplane->dist) > 0.01f)
722 Con_Printf("startplane->dist %f != calculated %f (thatbrush_start)\n", startplane->dist, f);
724 d1 = nearestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints) - startplane->dist;
725 d2 = nearestplanedist_float(endplane->normal, thisbrush_end->points, thisbrush_end->numpoints) - startplane->dist - COLLISIONEPSILON2;
727 //Con_Printf("%c%i: d1 = %f, d2 = %f, d1 / (d1 - d2) = %f\n", nplane2 != nplane ? 'b' : 'a', nplane2, d1, d2, d1 / (d1 - d2));
739 f = (d1 - COLLISIONEPSILON) / f;
744 VectorLerp(startplane->normal, enterfrac, endplane->normal, newimpactnormal);
749 // moving out of brush
756 f = (d1 + COLLISIONEPSILON) / f;
763 brushsolid = trace->hitsupercontentsmask & thatbrush_start->supercontents;
766 trace->startsupercontents |= thatbrush_start->supercontents;
769 trace->startsolid = true;
771 trace->allsolid = true;
775 // LordHavoc: we need an epsilon nudge here because for a point trace the
776 // penetrating line segment is normally zero length if this brush was
777 // generated from a polygon (infinitely thin), and could even be slightly
778 // positive or negative due to rounding errors in that case.
779 if (brushsolid && enterfrac > -1 && enterfrac < trace->fraction && enterfrac - (1.0f / 1024.0f) <= leavefrac)
781 trace->fraction = bound(0, enterfrac, 1);
782 VectorCopy(newimpactnormal, trace->plane.normal);
786 // NOTE: start and end brush pair must have same numplanes/numpoints
787 void Collision_TraceLineBrushFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, const colbrushf_t *thatbrush_start, const colbrushf_t *thatbrush_end)
789 int nplane, fstartsolid, fendsolid, brushsolid;
790 float enterfrac, leavefrac, d1, d2, f, newimpactnormal[3];
791 const colplanef_t *startplane, *endplane;
798 for (nplane = 0;nplane < thatbrush_start->numplanes;nplane++)
800 startplane = thatbrush_start->planes + nplane;
801 endplane = thatbrush_end->planes + nplane;
802 d1 = DotProduct(startplane->normal, linestart) - startplane->dist;
803 d2 = DotProduct(endplane->normal, lineend) - endplane->dist - COLLISIONEPSILON2;
804 if (developer.integer)
806 // any brush with degenerate planes is not worth handling
807 if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
809 Con_Printf("Collision_TraceLineBrushFloat: degenerate plane!\n");
812 f = furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints);
813 if (fabs(f - startplane->dist) > 0.01f)
814 Con_Printf("startplane->dist %f != calculated %f\n", startplane->dist, f);
827 f = (d1 - COLLISIONEPSILON) / f;
832 VectorLerp(startplane->normal, enterfrac, endplane->normal, newimpactnormal);
837 // moving out of brush
844 f = (d1 + COLLISIONEPSILON) / f;
851 brushsolid = trace->hitsupercontentsmask & thatbrush_start->supercontents;
854 trace->startsupercontents |= thatbrush_start->supercontents;
857 trace->startsolid = true;
859 trace->allsolid = true;
863 // LordHavoc: we need an epsilon nudge here because for a point trace the
864 // penetrating line segment is normally zero length if this brush was
865 // generated from a polygon (infinitely thin), and could even be slightly
866 // positive or negative due to rounding errors in that case.
867 if (brushsolid && enterfrac > -1 && enterfrac < trace->fraction && enterfrac - (1.0f / 1024.0f) <= leavefrac)
869 trace->fraction = bound(0, enterfrac, 1);
870 VectorCopy(newimpactnormal, trace->plane.normal);
874 static colpointf_t polyf_points[256];
875 static colplanef_t polyf_planes[256 + 2];
876 static colbrushf_t polyf_brush;
878 void Collision_TraceBrushPolygonFloat(trace_t *trace, const colbrushf_t *thisbrush_start, const colbrushf_t *thisbrush_end, int numpoints, const float *points, int supercontents)
882 Con_Printf("Polygon with more than 256 points not supported yet (fixme!)\n");
885 polyf_brush.numpoints = numpoints;
886 polyf_brush.numplanes = numpoints + 2;
887 polyf_brush.points = (colpointf_t *)points;
888 polyf_brush.planes = polyf_planes;
889 polyf_brush.supercontents = supercontents;
890 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
891 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
892 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brush, &polyf_brush);
895 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)
898 float facemins[3], facemaxs[3];
899 polyf_brush.numpoints = 3;
900 polyf_brush.numplanes = 5;
901 polyf_brush.points = polyf_points;
902 polyf_brush.planes = polyf_planes;
903 polyf_brush.supercontents = supercontents;
904 for (i = 0;i < numtriangles;i++, element3i += 3)
906 VectorCopy(vertex3f + element3i[0] * 3, polyf_points[0].v);
907 VectorCopy(vertex3f + element3i[1] * 3, polyf_points[1].v);
908 VectorCopy(vertex3f + element3i[2] * 3, polyf_points[2].v);
909 facemins[0] = min(polyf_points[0].v[0], min(polyf_points[1].v[0], polyf_points[2].v[0]));
910 facemins[1] = min(polyf_points[0].v[1], min(polyf_points[1].v[1], polyf_points[2].v[1]));
911 facemins[2] = min(polyf_points[0].v[2], min(polyf_points[1].v[2], polyf_points[2].v[2]));
912 facemaxs[0] = max(polyf_points[0].v[0], max(polyf_points[1].v[0], polyf_points[2].v[0]));
913 facemaxs[1] = max(polyf_points[0].v[1], max(polyf_points[1].v[1], polyf_points[2].v[1]));
914 facemaxs[2] = max(polyf_points[0].v[2], max(polyf_points[1].v[2], polyf_points[2].v[2]));
915 if (BoxesOverlap(segmentmins, segmentmaxs, facemins, facemaxs))
917 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
918 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
919 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brush, &polyf_brush);
924 void Collision_TraceLinePolygonFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, int numpoints, const float *points, int supercontents)
928 Con_Printf("Polygon with more than 256 points not supported yet (fixme!)\n");
931 polyf_brush.numpoints = numpoints;
932 polyf_brush.numplanes = numpoints + 2;
933 polyf_brush.points = (colpointf_t *)points;
934 polyf_brush.planes = polyf_planes;
935 polyf_brush.supercontents = supercontents;
936 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
937 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
938 Collision_TraceLineBrushFloat(trace, linestart, lineend, &polyf_brush, &polyf_brush);
941 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)
944 float facemins[3], facemaxs[3];
945 polyf_brush.numpoints = 3;
946 polyf_brush.numplanes = 5;
947 polyf_brush.points = polyf_points;
948 polyf_brush.planes = polyf_planes;
949 polyf_brush.supercontents = supercontents;
950 for (i = 0;i < numtriangles;i++, element3i += 3)
952 VectorCopy(vertex3f + element3i[0] * 3, polyf_points[0].v);
953 VectorCopy(vertex3f + element3i[1] * 3, polyf_points[1].v);
954 VectorCopy(vertex3f + element3i[2] * 3, polyf_points[2].v);
955 facemins[0] = min(polyf_points[0].v[0], min(polyf_points[1].v[0], polyf_points[2].v[0]));
956 facemins[1] = min(polyf_points[0].v[1], min(polyf_points[1].v[1], polyf_points[2].v[1]));
957 facemins[2] = min(polyf_points[0].v[2], min(polyf_points[1].v[2], polyf_points[2].v[2]));
958 facemaxs[0] = max(polyf_points[0].v[0], max(polyf_points[1].v[0], polyf_points[2].v[0]));
959 facemaxs[1] = max(polyf_points[0].v[1], max(polyf_points[1].v[1], polyf_points[2].v[1]));
960 facemaxs[2] = max(polyf_points[0].v[2], max(polyf_points[1].v[2], polyf_points[2].v[2]));
961 if (BoxesOverlap(segmentmins, segmentmaxs, facemins, facemaxs))
963 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
964 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
965 Collision_TraceLineBrushFloat(trace, linestart, lineend, &polyf_brush, &polyf_brush);
971 static colpointf_t polyf_pointsstart[256], polyf_pointsend[256];
972 static colplanef_t polyf_planesstart[256 + 2], polyf_planesend[256 + 2];
973 static colbrushf_t polyf_brushstart, polyf_brushend;
975 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)
980 Con_Printf("Polygon with more than 256 points not supported yet (fixme!)\n");
983 polyf_brushstart.numpoints = numpoints;
984 polyf_brushstart.numplanes = numpoints + 2;
985 polyf_brushstart.points = polyf_pointsstart;//(colpointf_t *)points;
986 polyf_brushstart.planes = polyf_planesstart;
987 polyf_brushstart.supercontents = supercontents;
988 for (i = 0;i < numpoints;i++)
989 Matrix4x4_Transform(polygonmatrixstart, points + i * 3, polyf_brushstart.points[i].v);
990 polyf_brushend.numpoints = numpoints;
991 polyf_brushend.numplanes = numpoints + 2;
992 polyf_brushend.points = polyf_pointsend;//(colpointf_t *)points;
993 polyf_brushend.planes = polyf_planesend;
994 polyf_brushend.supercontents = supercontents;
995 for (i = 0;i < numpoints;i++)
996 Matrix4x4_Transform(polygonmatrixend, points + i * 3, polyf_brushend.points[i].v);
997 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brushstart);
998 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brushend);
1000 //Collision_PrintBrushAsQHull(&polyf_brushstart, "polyf_brushstart");
1001 //Collision_PrintBrushAsQHull(&polyf_brushend, "polyf_brushend");
1003 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brushstart, &polyf_brushend);
1008 #define MAX_BRUSHFORBOX 16
1009 static int brushforbox_index = 0;
1010 static colpointf_t brushforbox_point[MAX_BRUSHFORBOX*8];
1011 static colplanef_t brushforbox_plane[MAX_BRUSHFORBOX*6];
1012 static colbrushf_t brushforbox_brush[MAX_BRUSHFORBOX];
1013 static colbrushf_t brushforpoint_brush[MAX_BRUSHFORBOX];
1015 void Collision_InitBrushForBox(void)
1018 for (i = 0;i < MAX_BRUSHFORBOX;i++)
1020 brushforbox_brush[i].supercontents = SUPERCONTENTS_SOLID;
1021 brushforbox_brush[i].numpoints = 8;
1022 brushforbox_brush[i].numplanes = 6;
1023 brushforbox_brush[i].points = brushforbox_point + i * 8;
1024 brushforbox_brush[i].planes = brushforbox_plane + i * 6;
1025 brushforpoint_brush[i].supercontents = SUPERCONTENTS_SOLID;
1026 brushforpoint_brush[i].numpoints = 1;
1027 brushforpoint_brush[i].numplanes = 0;
1028 brushforpoint_brush[i].points = brushforbox_point + i * 8;
1029 brushforpoint_brush[i].planes = brushforbox_plane + i * 6;
1033 colbrushf_t *Collision_BrushForBox(const matrix4x4_t *matrix, const vec3_t mins, const vec3_t maxs)
1038 if (brushforbox_brush[0].numpoints == 0)
1039 Collision_InitBrushForBox();
1040 if (VectorCompare(mins, maxs))
1043 brush = brushforpoint_brush + ((brushforbox_index++) % MAX_BRUSHFORBOX);
1044 VectorCopy(mins, brush->points->v);
1048 brush = brushforbox_brush + ((brushforbox_index++) % MAX_BRUSHFORBOX);
1050 for (i = 0;i < 8;i++)
1052 v[0] = i & 1 ? maxs[0] : mins[0];
1053 v[1] = i & 2 ? maxs[1] : mins[1];
1054 v[2] = i & 4 ? maxs[2] : mins[2];
1055 Matrix4x4_Transform(matrix, v, brush->points[i].v);
1058 for (i = 0;i < 6;i++)
1061 v[i >> 1] = i & 1 ? 1 : -1;
1062 Matrix4x4_Transform3x3(matrix, v, brush->planes[i].normal);
1063 VectorNormalize(brush->planes[i].normal);
1064 brush->planes[i].dist = furthestplanedist_float(brush->planes[i].normal, brush->points, brush->numpoints);
1067 Collision_ValidateBrush(brush);
1071 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)
1073 colbrushf_t *boxbrush, *thisbrush_start, *thisbrush_end;
1074 matrix4x4_t identitymatrix;
1075 vec3_t startmins, startmaxs, endmins, endmaxs;
1077 // create brushes for the collision
1078 VectorAdd(start, mins, startmins);
1079 VectorAdd(start, maxs, startmaxs);
1080 VectorAdd(end, mins, endmins);
1081 VectorAdd(end, maxs, endmaxs);
1082 Matrix4x4_CreateIdentity(&identitymatrix);
1083 boxbrush = Collision_BrushForBox(&identitymatrix, cmins, cmaxs);
1084 thisbrush_start = Collision_BrushForBox(&identitymatrix, startmins, startmaxs);
1085 thisbrush_end = Collision_BrushForBox(&identitymatrix, endmins, endmaxs);
1087 memset(trace, 0, sizeof(trace_t));
1088 trace->hitsupercontentsmask = hitsupercontentsmask;
1089 trace->fraction = 1;
1090 trace->allsolid = true;
1091 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, boxbrush, boxbrush);
1094 // LordHavoc: currently unused and not yet tested
1095 // note: this can be used for tracing a moving sphere vs a stationary sphere,
1096 // by simply adding the moving sphere's radius to the sphereradius parameter,
1097 // all the results are correct (impactpoint, impactnormal, and fraction)
1098 float Collision_ClipTrace_Line_Sphere(double *linestart, double *lineend, double *sphereorigin, double sphereradius, double *impactpoint, double *impactnormal)
1100 double dir[3], scale, v[3], deviationdist, impactdist, linelength;
1101 // make sure the impactpoint and impactnormal are valid even if there is
1103 impactpoint[0] = lineend[0];
1104 impactpoint[1] = lineend[1];
1105 impactpoint[2] = lineend[2];
1106 impactnormal[0] = 0;
1107 impactnormal[1] = 0;
1108 impactnormal[2] = 0;
1109 // calculate line direction
1110 dir[0] = lineend[0] - linestart[0];
1111 dir[1] = lineend[1] - linestart[1];
1112 dir[2] = lineend[2] - linestart[2];
1113 // normalize direction
1114 linelength = sqrt(dir[0] * dir[0] + dir[1] * dir[1] + dir[2] * dir[2]);
1117 scale = 1.0 / linelength;
1122 // this dotproduct calculates the distance along the line at which the
1123 // sphere origin is (nearest point to the sphere origin on the line)
1124 impactdist = dir[0] * (sphereorigin[0] - linestart[0]) + dir[1] * (sphereorigin[1] - linestart[1]) + dir[2] * (sphereorigin[2] - linestart[2]);
1125 // calculate point on line at that distance, and subtract the
1126 // sphereorigin from it, so we have a vector to measure for the distance
1127 // of the line from the sphereorigin (deviation, how off-center it is)
1128 v[0] = linestart[0] + impactdist * dir[0] - sphereorigin[0];
1129 v[1] = linestart[1] + impactdist * dir[1] - sphereorigin[1];
1130 v[2] = linestart[2] + impactdist * dir[2] - sphereorigin[2];
1131 deviationdist = v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
1132 // if outside the radius, it's a miss for sure
1133 // (we do this comparison using squared radius to avoid a sqrt)
1134 if (deviationdist > sphereradius*sphereradius)
1135 return 1; // miss (off to the side)
1136 // nudge back to find the correct impact distance
1137 impactdist += (sqrt(deviationdist) - sphereradius);
1138 if (impactdist >= linelength)
1139 return 1; // miss (not close enough)
1141 return 1; // miss (linestart is past or inside sphere)
1142 // calculate new impactpoint
1143 impactpoint[0] = linestart[0] + impactdist * dir[0];
1144 impactpoint[1] = linestart[1] + impactdist * dir[1];
1145 impactpoint[2] = linestart[2] + impactdist * dir[2];
1146 // calculate impactnormal (surface normal at point of impact)
1147 impactnormal[0] = impactpoint[0] - sphereorigin[0];
1148 impactnormal[1] = impactpoint[1] - sphereorigin[1];
1149 impactnormal[2] = impactpoint[2] - sphereorigin[2];
1150 // normalize impactnormal
1151 scale = impactnormal[0] * impactnormal[0] + impactnormal[1] * impactnormal[1] + impactnormal[2] * impactnormal[2];
1154 scale = 1.0 / sqrt(scale);
1155 impactnormal[0] *= scale;
1156 impactnormal[1] *= scale;
1157 impactnormal[2] *= scale;
1159 // return fraction of movement distance
1160 return impactdist / linelength;
1163 typedef struct colbspnode_s
1166 struct colbspnode_s *children[2];
1167 // the node is reallocated or split if max is reached
1170 colbrushf_t **colbrushflist;
1173 //colbrushd_t **colbrushdlist;
1177 typedef struct colbsp_s
1180 colbspnode_t *nodes;
1184 colbsp_t *Collision_CreateCollisionBSP(mempool_t *mempool)
1187 bsp = Mem_Alloc(mempool, sizeof(colbsp_t));
1188 bsp->mempool = mempool;
1189 bsp->nodes = Mem_Alloc(bsp->mempool, sizeof(colbspnode_t));
1193 void Collision_FreeCollisionBSPNode(colbspnode_t *node)
1195 if (node->children[0])
1196 Collision_FreeCollisionBSPNode(node->children[0]);
1197 if (node->children[1])
1198 Collision_FreeCollisionBSPNode(node->children[1]);
1199 while (--node->numcolbrushf)
1200 Mem_Free(node->colbrushflist[node->numcolbrushf]);
1201 //while (--node->numcolbrushd)
1202 // Mem_Free(node->colbrushdlist[node->numcolbrushd]);
1206 void Collision_FreeCollisionBSP(colbsp_t *bsp)
1208 Collision_FreeCollisionBSPNode(bsp->nodes);
1212 void Collision_BoundingBoxOfBrushTraceSegment(const colbrushf_t *start, const colbrushf_t *end, vec3_t mins, vec3_t maxs, float startfrac, float endfrac)
1215 colpointf_t *ps, *pe;
1216 float tempstart[3], tempend[3];
1217 VectorLerp(start->points[0].v, startfrac, end->points[0].v, mins);
1218 VectorCopy(mins, maxs);
1219 for (i = 0, ps = start->points, pe = end->points;i < start->numpoints;i++, ps++, pe++)
1221 VectorLerp(ps->v, startfrac, pe->v, tempstart);
1222 VectorLerp(ps->v, endfrac, pe->v, tempend);
1223 mins[0] = min(mins[0], min(tempstart[0], tempend[0]));
1224 mins[1] = min(mins[1], min(tempstart[1], tempend[1]));
1225 mins[2] = min(mins[2], min(tempstart[2], tempend[2]));
1226 maxs[0] = min(maxs[0], min(tempstart[0], tempend[0]));
1227 maxs[1] = min(maxs[1], min(tempstart[1], tempend[1]));
1228 maxs[2] = min(maxs[2], min(tempstart[2], tempend[2]));