5 #define COLLISION_SNAPSCALE (32.0f)
6 #define COLLISION_SNAP (1.0f / COLLISION_SNAPSCALE)
8 cvar_t collision_impactnudge = {0, "collision_impactnudge", "0.03125"};
9 cvar_t collision_startnudge = {0, "collision_startnudge", "0"};
10 cvar_t collision_endnudge = {0, "collision_endnudge", "0"};
11 cvar_t collision_enternudge = {0, "collision_enternudge", "0"};
12 cvar_t collision_leavenudge = {0, "collision_leavenudge", "0"};
17 // the hull we're tracing through
20 // the trace structure to fill in
23 // start and end of the trace (in model space)
30 // overrides the CONTENTS_SOLID in the box bsp tree
33 RecursiveHullCheckTraceInfo_t;
35 #define HULLCHECKSTATE_EMPTY 0
36 #define HULLCHECKSTATE_SOLID 1
37 #define HULLCHECKSTATE_DONE 2
39 static int RecursiveHullCheck(RecursiveHullCheckTraceInfo_t *t, int num, double p1f, double p2f, double p1[3], double p2[3])
41 // status variables, these don't need to be saved on the stack when
42 // recursing... but are because this should be thread-safe
43 // (note: tracing against a bbox is not thread-safe, yet)
48 // variables that need to be stored on the stack when recursing
53 // LordHavoc: a goto! everyone flee in terror... :)
58 num = Mod_Q1BSP_SuperContentsFromNativeContents(NULL, num);
59 if (!t->trace->startfound)
61 t->trace->startfound = true;
62 t->trace->startsupercontents |= num;
64 if (num & SUPERCONTENTS_LIQUIDSMASK)
65 t->trace->inwater = true;
67 t->trace->inopen = true;
68 if (num & t->trace->hitsupercontentsmask)
70 // if the first leaf is solid, set startsolid
71 if (t->trace->allsolid)
72 t->trace->startsolid = true;
73 #if COLLISIONPARANOID >= 3
76 return HULLCHECKSTATE_SOLID;
80 t->trace->allsolid = false;
81 #if COLLISIONPARANOID >= 3
84 return HULLCHECKSTATE_EMPTY;
88 // find the point distances
89 node = t->hull->clipnodes + num;
91 plane = t->hull->planes + node->planenum;
94 t1 = p1[plane->type] - plane->dist;
95 t2 = p2[plane->type] - plane->dist;
99 t1 = DotProduct (plane->normal, p1) - plane->dist;
100 t2 = DotProduct (plane->normal, p2) - plane->dist;
107 #if COLLISIONPARANOID >= 3
110 num = node->children[1];
119 #if COLLISIONPARANOID >= 3
122 num = node->children[0];
128 // the line intersects, find intersection point
129 // LordHavoc: this uses the original trace for maximum accuracy
130 #if COLLISIONPARANOID >= 3
135 t1 = t->start[plane->type] - plane->dist;
136 t2 = t->end[plane->type] - plane->dist;
140 t1 = DotProduct (plane->normal, t->start) - plane->dist;
141 t2 = DotProduct (plane->normal, t->end) - plane->dist;
144 midf = t1 / (t1 - t2);
145 midf = bound(p1f, midf, p2f);
146 VectorMA(t->start, midf, t->dist, mid);
148 // recurse both sides, front side first
149 ret = RecursiveHullCheck (t, node->children[side], p1f, midf, p1, mid);
150 // if this side is not empty, return what it is (solid or done)
151 if (ret != HULLCHECKSTATE_EMPTY)
154 ret = RecursiveHullCheck (t, node->children[side ^ 1], midf, p2f, mid, p2);
155 // if other side is not solid, return what it is (empty or done)
156 if (ret != HULLCHECKSTATE_SOLID)
159 // front is air and back is solid, this is the impact point...
162 t->trace->plane.dist = -plane->dist;
163 VectorNegate (plane->normal, t->trace->plane.normal);
167 t->trace->plane.dist = plane->dist;
168 VectorCopy (plane->normal, t->trace->plane.normal);
171 // calculate the true fraction
172 t1 = DotProduct(t->trace->plane.normal, t->start) - t->trace->plane.dist - collision_startnudge.value;
173 t2 = DotProduct(t->trace->plane.normal, t->end) - t->trace->plane.dist - collision_endnudge.value;
174 midf = t1 / (t1 - t2);
175 t->trace->realfraction = bound(0, midf, 1);
177 // calculate the return fraction which is nudged off the surface a bit
178 midf = (t1 - collision_impactnudge.value) / (t1 - t2);
179 t->trace->fraction = bound(0, midf, 1);
181 #if COLLISIONPARANOID >= 3
184 return HULLCHECKSTATE_DONE;
188 // used if start and end are the same
189 static void RecursiveHullCheckPoint (RecursiveHullCheckTraceInfo_t *t, int num)
191 // If you can read this, you understand BSP trees
193 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];
196 t->trace->endcontents = num;
197 if (t->trace->thiscontents)
199 if (num == t->trace->thiscontents)
200 t->trace->allsolid = false;
203 // if the first leaf is solid, set startsolid
204 if (t->trace->allsolid)
205 t->trace->startsolid = true;
210 if (num != CONTENTS_SOLID)
212 t->trace->allsolid = false;
213 if (num == CONTENTS_EMPTY)
214 t->trace->inopen = true;
216 t->trace->inwater = true;
220 // if the first leaf is solid, set startsolid
221 if (t->trace->allsolid)
222 t->trace->startsolid = true;
228 static hull_t box_hull;
229 static dclipnode_t box_clipnodes[6];
230 static mplane_t box_planes[6];
232 void Mod_Q1BSP_Collision_Init (void)
237 //Set up the planes and clipnodes so that the six floats of a bounding box
238 //can just be stored out and get a proper hull_t structure.
240 box_hull.clipnodes = box_clipnodes;
241 box_hull.planes = box_planes;
242 box_hull.firstclipnode = 0;
243 box_hull.lastclipnode = 5;
245 for (i = 0;i < 6;i++)
247 box_clipnodes[i].planenum = i;
251 box_clipnodes[i].children[side] = CONTENTS_EMPTY;
253 box_clipnodes[i].children[side^1] = i + 1;
255 box_clipnodes[i].children[side^1] = CONTENTS_SOLID;
257 box_planes[i].type = i>>1;
258 box_planes[i].normal[i>>1] = 1;
262 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)
264 RecursiveHullCheckTraceInfo_t rhc;
265 // fill in a default trace
266 memset(&rhc, 0, sizeof(rhc));
267 memset(trace, 0, sizeof(trace_t));
268 //To keep everything totally uniform, bounding boxes are turned into small
269 //BSP trees instead of being compared directly.
270 // create a temp hull from bounding box sizes
271 box_planes[0].dist = cmaxs[0] - mins[0];
272 box_planes[1].dist = cmins[0] - maxs[0];
273 box_planes[2].dist = cmaxs[1] - mins[1];
274 box_planes[3].dist = cmins[1] - maxs[1];
275 box_planes[4].dist = cmaxs[2] - mins[2];
276 box_planes[5].dist = cmins[2] - maxs[2];
277 // trace a line through the generated clipping hull
278 rhc.boxsupercontents = boxsupercontents;
279 rhc.hull = &box_hull;
281 rhc.trace->hitsupercontentsmask = hitsupercontentsmask;
282 rhc.trace->fraction = 1;
283 rhc.trace->realfraction = 1;
284 rhc.trace->allsolid = true;
285 VectorCopy(start, rhc.start);
286 VectorCopy(end, rhc.end);
287 VectorSubtract(rhc.end, rhc.start, rhc.dist);
288 Mod_Q1BSP_RecursiveHullCheck(&rhc, rhc.hull->firstclipnode, 0, 1, rhc.start, rhc.end);
289 VectorMA(rhc.start, rhc.trace->fraction, rhc.dist, rhc.trace->endpos);
290 if (rhc.trace->startsupercontents)
291 rhc.trace->startsupercontents = boxsupercontents;
295 void Collision_Init (void)
297 Cvar_RegisterVariable(&collision_impactnudge);
298 Cvar_RegisterVariable(&collision_startnudge);
299 Cvar_RegisterVariable(&collision_endnudge);
300 Cvar_RegisterVariable(&collision_enternudge);
301 Cvar_RegisterVariable(&collision_leavenudge);
317 void Collision_PrintBrushAsQHull(colbrushf_t *brush, const char *name)
320 Con_Printf("3 %s\n%i\n", name, brush->numpoints);
321 for (i = 0;i < brush->numpoints;i++)
322 Con_Printf("%f %f %f\n", brush->points[i].v[0], brush->points[i].v[1], brush->points[i].v[2]);
324 Con_Printf("4\n%i\n", brush->numplanes);
325 for (i = 0;i < brush->numplanes;i++)
326 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);
329 void Collision_ValidateBrush(colbrushf_t *brush)
331 int j, k, pointsoffplanes, printbrush;
334 if (!brush->numpoints)
336 Con_Print("Collision_ValidateBrush: brush with no points!\n");
340 // it's ok for a brush to have one point and no planes...
341 if (brush->numplanes == 0 && brush->numpoints != 1)
343 Con_Print("Collision_ValidateBrush: brush with no planes and more than one point!\n");
347 if (brush->numplanes)
350 for (k = 0;k < brush->numplanes;k++)
352 if (DotProduct(brush->planes[k].normal, brush->planes[k].normal) < 0.0001f)
353 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);
354 for (j = 0;j < brush->numpoints;j++)
356 d = DotProduct(brush->points[j].v, brush->planes[k].normal) - brush->planes[k].dist;
357 if (d > (1.0f / 8.0f))
359 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);
366 if (pointsoffplanes == 0) // all points are on all planes
368 Con_Print("Collision_ValidateBrush: all points lie on all planes (degenerate, no brush volume!)\n");
373 Collision_PrintBrushAsQHull(brush, "unnamed");
376 float nearestplanedist_float(const float *normal, const colpointf_t *points, int numpoints)
378 float dist, bestdist;
379 bestdist = DotProduct(points->v, normal);
383 dist = DotProduct(points->v, normal);
384 bestdist = min(bestdist, dist);
390 float furthestplanedist_float(const float *normal, const colpointf_t *points, int numpoints)
392 float dist, bestdist;
393 bestdist = DotProduct(points->v, normal);
397 dist = DotProduct(points->v, normal);
398 bestdist = max(bestdist, dist);
405 colbrushf_t *Collision_NewBrushFromPlanes(mempool_t *mempool, int numoriginalplanes, const mplane_t *originalplanes, int supercontents)
408 int numpoints = 0, maxpoints = 256, numplanes = 0, maxplanes = 256, numelements = 0, maxelements = 256, numtriangles = 0;
410 colpointf_t pointsbuf[256];
411 colplanef_t planesbuf[256];
412 int elementsbuf[1024];
413 int polypointbuf[256];
417 // construct a collision brush (points, planes, and renderable mesh) from
418 // a set of planes, this also optimizes out any unnecessary planes (ones
419 // whose polygon is clipped away by the other planes)
420 for (j = 0;j < numoriginalplanes;j++)
422 // add the plane uniquely (no duplicates)
423 for (k = 0;k < numplanes;k++)
424 if (VectorCompare(planesbuf[k].normal, originalplanes[j].normal) && planesbuf[k].dist == originalplanes[j].dist)
426 // if the plane is a duplicate, skip it
429 // check if there are too many and skip the brush
430 if (numplanes >= maxplanes)
432 Con_Print("Mod_Q3BSP_LoadBrushes: failed to build collision brush: too many planes for buffer\n");
436 // create a large polygon from the plane
438 PolygonF_QuadForPlane(p[w], originalplanes[j].normal[0], originalplanes[j].normal[1], originalplanes[j].normal[2], originalplanes[j].dist, 1024.0*1024.0*1024.0);
440 // clip it by all other planes
441 for (k = 0;k < numoriginalplanes && pnumpoints && pnumpoints <= pmaxpoints;k++)
445 // we want to keep the inside of the brush plane so we flip
447 PolygonF_Divide(pnumpoints, p[w], -originalplanes[k].normal[0], -originalplanes[k].normal[1], -originalplanes[k].normal[2], -originalplanes[k].dist, 1.0/32.0, maxpoints, p[!w], &pnumpoints, 0, NULL, NULL);
451 // if nothing is left, skip it
455 // check if there are too many polygon vertices for buffer
456 if (pnumpoints > pmaxpoints)
458 Con_Print("Collision_NewBrushFromPlanes: failed to build collision brush: too many points for buffer\n");
462 // check if there are too many triangle elements for buffer
463 if (numelements + (pnumpoints - 2) * 3 > maxelements)
465 Con_Print("Collision_NewBrushFromPlanes: failed to build collision brush: too many triangle elements for buffer\n");
469 for (k = 0;k < pnumpoints;k++)
471 // check if there is already a matching point (no duplicates)
472 for (m = 0;m < numpoints;m++)
473 if (VectorDistance2(&p[w][k*3], pointsbuf[m].v) < COLLISION_SNAP)
476 // if there is no match, add a new one
479 // check if there are too many and skip the brush
480 if (numpoints >= maxpoints)
482 Con_Print("Collision_NewBrushFromPlanes: failed to build collision brush: too many points for buffer\n");
486 VectorCopy(&p[w][k*3], pointsbuf[numpoints].v);
490 // store the index into a buffer
494 // add the triangles for the polygon
495 // (this particular code makes a triangle fan)
496 for (k = 0;k < pnumpoints - 2;k++)
499 elementsbuf[numelements++] = polypointbuf[0];
500 elementsbuf[numelements++] = polypointbuf[k + 1];
501 elementsbuf[numelements++] = polypointbuf[k + 2];
505 VectorCopy(originalplanes[j].normal, planesbuf[numplanes].normal);
506 planesbuf[numplanes].dist = originalplanes[j].dist;
510 // if nothing is left, there's nothing to allocate
511 if (numtriangles < 4 || numplanes < 4 || numpoints < 4)
514 // allocate the brush and copy to it
515 brush = Collision_AllocBrushFloat(mempool, numpoints, numplanes, numtriangles, supercontents);
516 memcpy(brush->points, pointsbuf, numpoints * sizeof(colpointf_t));
517 memcpy(brush->planes, planesbuf, numplanes * sizeof(colplanef_t));
518 memcpy(brush->elements, elementsbuf, numtriangles * sizeof(int[3]));
520 for (j = 0;j < brush->numplanes;j++)
521 brush->planes[j].dist = furthestplanedist_float(brush->planes[j].normal, brush->points, brush->numpoints);
522 VectorCopy(brush->points[0].v, brush->mins);
523 VectorCopy(brush->points[0].v, brush->maxs);
524 for (j = 1;j < brush->numpoints;j++)
526 brush->mins[0] = min(brush->mins[0], brush->points[j].v[0]);
527 brush->mins[1] = min(brush->mins[1], brush->points[j].v[1]);
528 brush->mins[2] = min(brush->mins[2], brush->points[j].v[2]);
529 brush->maxs[0] = max(brush->maxs[0], brush->points[j].v[0]);
530 brush->maxs[1] = max(brush->maxs[1], brush->points[j].v[1]);
531 brush->maxs[2] = max(brush->maxs[2], brush->points[j].v[2]);
539 Collision_ValidateBrush(brush);
545 colbrushf_t *Collision_AllocBrushFloat(mempool_t *mempool, int numpoints, int numplanes, int numtriangles, int supercontents)
548 brush = Mem_Alloc(mempool, sizeof(colbrushf_t) + sizeof(colpointf_t) * numpoints + sizeof(colplanef_t) * numplanes + sizeof(int[3]) * numtriangles);
549 brush->supercontents = supercontents;
550 brush->numplanes = numplanes;
551 brush->numpoints = numpoints;
552 brush->numtriangles = numtriangles;
553 brush->planes = (void *)(brush + 1);
554 brush->points = (void *)(brush->planes + brush->numplanes);
555 brush->elements = (void *)(brush->points + brush->numpoints);
559 void Collision_CalcPlanesForPolygonBrushFloat(colbrushf_t *brush)
562 float edge0[3], edge1[3], edge2[3], normal[3], dist, bestdist;
565 if (brush->numpoints == 3)
567 // optimized triangle case
568 TriangleNormal(brush->points[0].v, brush->points[1].v, brush->points[2].v, brush->planes[0].normal);
569 if (DotProduct(brush->planes[0].normal, brush->planes[0].normal) < 0.0001f)
571 // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
572 brush->numplanes = 0;
577 brush->numplanes = 5;
578 VectorNormalize(brush->planes[0].normal);
579 brush->planes[0].dist = DotProduct(brush->points->v, brush->planes[0].normal);
580 VectorNegate(brush->planes[0].normal, brush->planes[1].normal);
581 brush->planes[1].dist = -brush->planes[0].dist;
582 VectorSubtract(brush->points[2].v, brush->points[0].v, edge0);
583 VectorSubtract(brush->points[0].v, brush->points[1].v, edge1);
584 VectorSubtract(brush->points[1].v, brush->points[2].v, edge2);
587 float projectionnormal[3], projectionedge0[3], projectionedge1[3], projectionedge2[3];
589 float dist, bestdist;
590 bestdist = fabs(brush->planes[0].normal[0]);
592 for (i = 1;i < 3;i++)
594 dist = fabs(brush->planes[0].normal[i]);
601 VectorClear(projectionnormal);
602 if (brush->planes[0].normal[best] < 0)
603 projectionnormal[best] = -1;
605 projectionnormal[best] = 1;
606 VectorCopy(edge0, projectionedge0);
607 VectorCopy(edge1, projectionedge1);
608 VectorCopy(edge2, projectionedge2);
609 projectionedge0[best] = 0;
610 projectionedge1[best] = 0;
611 projectionedge2[best] = 0;
612 CrossProduct(projectionedge0, projectionnormal, brush->planes[2].normal);
613 CrossProduct(projectionedge1, projectionnormal, brush->planes[3].normal);
614 CrossProduct(projectionedge2, projectionnormal, brush->planes[4].normal);
617 CrossProduct(edge0, brush->planes->normal, brush->planes[2].normal);
618 CrossProduct(edge1, brush->planes->normal, brush->planes[3].normal);
619 CrossProduct(edge2, brush->planes->normal, brush->planes[4].normal);
621 VectorNormalize(brush->planes[2].normal);
622 VectorNormalize(brush->planes[3].normal);
623 VectorNormalize(brush->planes[4].normal);
624 brush->planes[2].dist = DotProduct(brush->points[2].v, brush->planes[2].normal);
625 brush->planes[3].dist = DotProduct(brush->points[0].v, brush->planes[3].normal);
626 brush->planes[4].dist = DotProduct(brush->points[1].v, brush->planes[4].normal);
628 if (developer.integer)
634 VectorSubtract(brush->points[0].v, brush->points[1].v, edge0);
635 VectorSubtract(brush->points[2].v, brush->points[1].v, edge1);
636 CrossProduct(edge0, edge1, normal);
637 VectorNormalize(normal);
638 VectorSubtract(normal, brush->planes[0].normal, temp);
639 if (VectorLength(temp) > 0.01f)
640 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]);
641 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)
642 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);
644 if (fabs(DotProduct(brush->planes[2].normal, brush->planes[0].normal)) > 0.01f)
645 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);
646 if (fabs(DotProduct(brush->planes[3].normal, brush->planes[0].normal)) > 0.01f)
647 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);
648 if (fabs(DotProduct(brush->planes[4].normal, brush->planes[0].normal)) > 0.01f)
649 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);
650 if (fabs(DotProduct(brush->planes[2].normal, edge0)) > 0.01f)
651 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]);
652 if (fabs(DotProduct(brush->planes[3].normal, edge1)) > 0.01f)
653 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]);
654 if (fabs(DotProduct(brush->planes[4].normal, edge2)) > 0.01f)
655 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]);
658 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)
659 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);
660 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)
661 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);
662 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)
663 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);
664 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)
665 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);
666 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)
667 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);
673 // choose best surface normal for polygon's plane
675 for (i = 0, p = brush->points + 1;i < brush->numpoints - 2;i++, p++)
677 VectorSubtract(p[-1].v, p[0].v, edge0);
678 VectorSubtract(p[1].v, p[0].v, edge1);
679 CrossProduct(edge0, edge1, normal);
680 //TriangleNormal(p[-1].v, p[0].v, p[1].v, normal);
681 dist = DotProduct(normal, normal);
682 if (i == 0 || bestdist < dist)
685 VectorCopy(normal, brush->planes->normal);
688 if (bestdist < 0.0001f)
690 // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
691 brush->numplanes = 0;
696 brush->numplanes = brush->numpoints + 2;
697 VectorNormalize(brush->planes->normal);
698 brush->planes->dist = DotProduct(brush->points->v, brush->planes->normal);
700 // negate plane to create other side
701 VectorNegate(brush->planes[0].normal, brush->planes[1].normal);
702 brush->planes[1].dist = -brush->planes[0].dist;
703 for (i = 0, p = brush->points + (brush->numpoints - 1), p2 = brush->points;i < brush->numpoints;i++, p = p2, p2++)
705 VectorSubtract(p->v, p2->v, edge0);
706 CrossProduct(edge0, brush->planes->normal, brush->planes[i + 2].normal);
707 VectorNormalize(brush->planes[i + 2].normal);
708 brush->planes[i + 2].dist = DotProduct(p->v, brush->planes[i + 2].normal);
713 if (developer.integer)
715 // validity check - will be disabled later
716 Collision_ValidateBrush(brush);
717 for (i = 0;i < brush->numplanes;i++)
720 for (j = 0, p = brush->points;j < brush->numpoints;j++, p++)
721 if (DotProduct(p->v, brush->planes[i].normal) > brush->planes[i].dist + (1.0 / 32.0))
722 Con_Printf("Error in brush plane generation, plane %i\n", i);
727 colbrushf_t *Collision_AllocBrushFromPermanentPolygonFloat(mempool_t *mempool, int numpoints, float *points, int supercontents)
730 brush = Mem_Alloc(mempool, sizeof(colbrushf_t) + sizeof(colplanef_t) * (numpoints + 2));
731 brush->supercontents = supercontents;
732 brush->numpoints = numpoints;
733 brush->numplanes = numpoints + 2;
734 brush->planes = (void *)(brush + 1);
735 brush->points = (colpointf_t *)points;
736 Host_Error("Collision_AllocBrushFromPermanentPolygonFloat: FIXME: this code needs to be updated to generate a mesh...\n");
740 // NOTE: start and end of each brush pair must have same numplanes/numpoints
741 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)
743 int nplane, nplane2, fstartsolid, fendsolid, brushsolid;
744 float enterfrac, leavefrac, d1, d2, f, move, imove, newimpactnormal[3], enterfrac2;
745 const colplanef_t *startplane, *endplane;
753 for (nplane = 0;nplane < thatbrush_start->numplanes + thisbrush_start->numplanes;nplane++)
756 if (nplane2 >= thatbrush_start->numplanes)
758 nplane2 -= thatbrush_start->numplanes;
759 startplane = thisbrush_start->planes + nplane2;
760 endplane = thisbrush_end->planes + nplane2;
761 if (developer.integer)
763 // any brush with degenerate planes is not worth handling
764 if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
766 Con_Print("Collision_TraceBrushBrushFloat: degenerate thisbrush plane!\n");
769 f = furthestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints);
770 if (fabs(f - startplane->dist) > 0.01f)
771 Con_Printf("startplane->dist %f != calculated %f (thisbrush_start)\n", startplane->dist, f);
773 d1 = nearestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints) - furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints) - collision_startnudge.value;
774 d2 = nearestplanedist_float(endplane->normal, thisbrush_end->points, thisbrush_end->numpoints) - furthestplanedist_float(endplane->normal, thatbrush_end->points, thatbrush_end->numpoints) - collision_endnudge.value;
778 startplane = thatbrush_start->planes + nplane2;
779 endplane = thatbrush_end->planes + nplane2;
780 if (developer.integer)
782 // any brush with degenerate planes is not worth handling
783 if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
785 Con_Print("Collision_TraceBrushBrushFloat: degenerate thatbrush plane!\n");
788 f = furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints);
789 if (fabs(f - startplane->dist) > 0.01f)
790 Con_Printf("startplane->dist %f != calculated %f (thatbrush_start)\n", startplane->dist, f);
792 d1 = nearestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints) - startplane->dist - collision_startnudge.value;
793 d2 = nearestplanedist_float(endplane->normal, thisbrush_end->points, thisbrush_end->numpoints) - endplane->dist - collision_endnudge.value;
795 //Con_Printf("%c%i: d1 = %f, d2 = %f, d1 / (d1 - d2) = %f\n", nplane2 != nplane ? 'b' : 'a', nplane2, d1, d2, d1 / (d1 - d2));
801 if (d2 > collision_enternudge.value)
808 f = (d1 - collision_enternudge.value) * imove;
813 enterfrac2 = f - collision_impactnudge.value * imove;
814 enterfrac2 = bound(0, enterfrac2, 1);
815 VectorLerp(startplane->normal, enterfrac, endplane->normal, newimpactnormal);
820 // moving out of brush
821 if (d1 > collision_leavenudge.value)
827 f = (d1 + collision_leavenudge.value) / move;
834 // sliding along plane
840 brushsolid = trace->hitsupercontentsmask & thatbrush_start->supercontents;
843 trace->startsupercontents |= thatbrush_start->supercontents;
846 trace->startsolid = true;
848 trace->allsolid = true;
852 // LordHavoc: we need an epsilon nudge here because for a point trace the
853 // penetrating line segment is normally zero length if this brush was
854 // generated from a polygon (infinitely thin), and could even be slightly
855 // positive or negative due to rounding errors in that case.
856 if (brushsolid && enterfrac > -1 && enterfrac < trace->realfraction && enterfrac - (1.0f / 1024.0f) <= leavefrac)
860 if (thatbrush_start->ispolygon)
862 d1 = nearestplanedist_float(thatbrush_start->planes[0].normal, thisbrush_start->points, thisbrush_start->numpoints) - thatbrush_start->planes[0].dist - collision_startnudge.value;
863 d2 = nearestplanedist_float(thatbrush_end->planes[0].normal, thisbrush_end->points, thisbrush_end->numpoints) - thatbrush_end->planes[0].dist - collision_endnudge.value;
865 if (move <= 0 || d2 > collision_enternudge.value || d1 < 0)
869 enterfrac = (d1 - collision_enternudge.value) * imove;
870 if (enterfrac < trace->realfraction)
872 enterfrac2 = enterfrac - collision_impactnudge.value * imove;
873 trace->realfraction = bound(0, enterfrac, 1);
874 trace->fraction = bound(0, enterfrac2, 1);
875 VectorLerp(thatbrush_start->planes[0].normal, enterfrac, thatbrush_end->planes[0].normal, trace->plane.normal);
881 trace->realfraction = bound(0, enterfrac, 1);
882 trace->fraction = bound(0, enterfrac2, 1);
883 VectorCopy(newimpactnormal, trace->plane.normal);
888 // NOTE: start and end brush pair must have same numplanes/numpoints
889 void Collision_TraceLineBrushFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, const colbrushf_t *thatbrush_start, const colbrushf_t *thatbrush_end)
891 int nplane, fstartsolid, fendsolid, brushsolid;
892 float enterfrac, leavefrac, d1, d2, f, move, imove, newimpactnormal[3], enterfrac2;
893 const colplanef_t *startplane, *endplane;
901 for (nplane = 0;nplane < thatbrush_start->numplanes;nplane++)
903 startplane = thatbrush_start->planes + nplane;
904 endplane = thatbrush_end->planes + nplane;
905 d1 = DotProduct(startplane->normal, linestart) - startplane->dist - collision_startnudge.value;
906 d2 = DotProduct(endplane->normal, lineend) - endplane->dist - collision_endnudge.value;
907 if (developer.integer)
909 // any brush with degenerate planes is not worth handling
910 if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
912 Con_Print("Collision_TraceLineBrushFloat: degenerate plane!\n");
915 if (thatbrush_start->numpoints)
917 f = furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints);
918 if (fabs(f - startplane->dist) > 0.01f)
919 Con_Printf("startplane->dist %f != calculated %f\n", startplane->dist, f);
934 f = (d1 - collision_enternudge.value) * imove;
938 enterfrac2 = f - collision_impactnudge.value * imove;
939 VectorLerp(startplane->normal, enterfrac, endplane->normal, newimpactnormal);
944 // moving out of brush
951 f = (d1 - collision_leavenudge.value) / move;
957 brushsolid = trace->hitsupercontentsmask & thatbrush_start->supercontents;
960 trace->startsupercontents |= thatbrush_start->supercontents;
963 trace->startsolid = true;
965 trace->allsolid = true;
969 // LordHavoc: we need an epsilon nudge here because for a point trace the
970 // penetrating line segment is normally zero length if this brush was
971 // generated from a polygon (infinitely thin), and could even be slightly
972 // positive or negative due to rounding errors in that case.
973 if (brushsolid && enterfrac > -1 && enterfrac < trace->realfraction && enterfrac - (1.0f / 1024.0f) <= leavefrac)
977 if (thatbrush_start->ispolygon)
979 d1 = DotProduct(thatbrush_start->planes[0].normal, linestart) - thatbrush_start->planes[0].dist - collision_startnudge.value;
980 d2 = DotProduct(thatbrush_end->planes[0].normal, lineend) - thatbrush_end->planes[0].dist - collision_endnudge.value;
982 if (move <= 0 || d2 > collision_enternudge.value || d1 < 0)
986 enterfrac = (d1 - collision_enternudge.value) * imove;
987 if (enterfrac < trace->realfraction)
989 enterfrac2 = enterfrac - collision_impactnudge.value * imove;
990 trace->realfraction = bound(0, enterfrac, 1);
991 trace->fraction = bound(0, enterfrac2, 1);
992 VectorLerp(thatbrush_start->planes[0].normal, enterfrac, thatbrush_end->planes[0].normal, trace->plane.normal);
998 trace->realfraction = bound(0, enterfrac, 1);
999 trace->fraction = bound(0, enterfrac2, 1);
1000 VectorCopy(newimpactnormal, trace->plane.normal);
1005 void Collision_TracePointBrushFloat(trace_t *trace, const vec3_t point, const colbrushf_t *thatbrush)
1008 const colplanef_t *plane;
1010 for (nplane = 0, plane = thatbrush->planes;nplane < thatbrush->numplanes;nplane++, plane++)
1011 if (DotProduct(plane->normal, point) > plane->dist)
1014 trace->startsupercontents |= thatbrush->supercontents;
1015 if (trace->hitsupercontentsmask & thatbrush->supercontents)
1017 trace->startsolid = true;
1018 trace->allsolid = true;
1022 static colpointf_t polyf_points[256];
1023 static colplanef_t polyf_planes[256 + 2];
1024 static colbrushf_t polyf_brush;
1026 void Collision_SnapCopyPoints(int numpoints, const colpointf_t *in, colpointf_t *out, float fractionprecision, float invfractionprecision)
1030 out->v[0] = floor(in->v[0] * fractionprecision + 0.5f) * invfractionprecision;
1031 out->v[1] = floor(in->v[1] * fractionprecision + 0.5f) * invfractionprecision;
1032 out->v[2] = floor(in->v[2] * fractionprecision + 0.5f) * invfractionprecision;
1036 void Collision_TraceBrushPolygonFloat(trace_t *trace, const colbrushf_t *thisbrush_start, const colbrushf_t *thisbrush_end, int numpoints, const float *points, int supercontents)
1038 if (numpoints > 256)
1040 Con_Print("Polygon with more than 256 points not supported yet (fixme!)\n");
1043 polyf_brush.numpoints = numpoints;
1044 polyf_brush.numplanes = numpoints + 2;
1045 //polyf_brush.points = (colpointf_t *)points;
1046 polyf_brush.planes = polyf_planes;
1047 polyf_brush.supercontents = supercontents;
1048 polyf_brush.points = polyf_points;
1049 Collision_SnapCopyPoints(numpoints, (colpointf_t *)points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
1050 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
1051 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
1052 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brush, &polyf_brush);
1055 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)
1058 float facemins[3], facemaxs[3];
1059 polyf_brush.numpoints = 3;
1060 polyf_brush.numplanes = 5;
1061 polyf_brush.points = polyf_points;
1062 polyf_brush.planes = polyf_planes;
1063 polyf_brush.supercontents = supercontents;
1064 for (i = 0;i < numtriangles;i++, element3i += 3)
1066 VectorCopy(vertex3f + element3i[0] * 3, polyf_points[0].v);
1067 VectorCopy(vertex3f + element3i[1] * 3, polyf_points[1].v);
1068 VectorCopy(vertex3f + element3i[2] * 3, polyf_points[2].v);
1069 Collision_SnapCopyPoints(3, polyf_points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
1070 facemins[0] = min(polyf_points[0].v[0], min(polyf_points[1].v[0], polyf_points[2].v[0])) - 1;
1071 facemins[1] = min(polyf_points[0].v[1], min(polyf_points[1].v[1], polyf_points[2].v[1])) - 1;
1072 facemins[2] = min(polyf_points[0].v[2], min(polyf_points[1].v[2], polyf_points[2].v[2])) - 1;
1073 facemaxs[0] = max(polyf_points[0].v[0], max(polyf_points[1].v[0], polyf_points[2].v[0])) + 1;
1074 facemaxs[1] = max(polyf_points[0].v[1], max(polyf_points[1].v[1], polyf_points[2].v[1])) + 1;
1075 facemaxs[2] = max(polyf_points[0].v[2], max(polyf_points[1].v[2], polyf_points[2].v[2])) + 1;
1076 if (BoxesOverlap(segmentmins, segmentmaxs, facemins, facemaxs))
1078 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
1079 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
1080 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brush, &polyf_brush);
1085 void Collision_TraceLinePolygonFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, int numpoints, const float *points, int supercontents)
1087 if (numpoints > 256)
1089 Con_Print("Polygon with more than 256 points not supported yet (fixme!)\n");
1092 polyf_brush.numpoints = numpoints;
1093 polyf_brush.numplanes = numpoints + 2;
1094 //polyf_brush.points = (colpointf_t *)points;
1095 polyf_brush.points = polyf_points;
1096 Collision_SnapCopyPoints(numpoints, (colpointf_t *)points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
1097 polyf_brush.planes = polyf_planes;
1098 polyf_brush.supercontents = supercontents;
1099 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
1100 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
1101 Collision_TraceLineBrushFloat(trace, linestart, lineend, &polyf_brush, &polyf_brush);
1104 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)
1108 // FIXME: snap vertices?
1109 for (i = 0;i < numtriangles;i++, element3i += 3)
1110 Collision_TraceLineTriangleFloat(trace, linestart, lineend, vertex3f + element3i[0] * 3, vertex3f + element3i[1] * 3, vertex3f + element3i[2] * 3);
1112 polyf_brush.numpoints = 3;
1113 polyf_brush.numplanes = 5;
1114 polyf_brush.points = polyf_points;
1115 polyf_brush.planes = polyf_planes;
1116 polyf_brush.supercontents = supercontents;
1117 for (i = 0;i < numtriangles;i++, element3i += 3)
1119 float facemins[3], facemaxs[3];
1120 VectorCopy(vertex3f + element3i[0] * 3, polyf_points[0].v);
1121 VectorCopy(vertex3f + element3i[1] * 3, polyf_points[1].v);
1122 VectorCopy(vertex3f + element3i[2] * 3, polyf_points[2].v);
1123 Collision_SnapCopyPoints(numpoints, polyf_points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
1124 facemins[0] = min(polyf_points[0].v[0], min(polyf_points[1].v[0], polyf_points[2].v[0])) - 1;
1125 facemins[1] = min(polyf_points[0].v[1], min(polyf_points[1].v[1], polyf_points[2].v[1])) - 1;
1126 facemins[2] = min(polyf_points[0].v[2], min(polyf_points[1].v[2], polyf_points[2].v[2])) - 1;
1127 facemaxs[0] = max(polyf_points[0].v[0], max(polyf_points[1].v[0], polyf_points[2].v[0])) + 1;
1128 facemaxs[1] = max(polyf_points[0].v[1], max(polyf_points[1].v[1], polyf_points[2].v[1])) + 1;
1129 facemaxs[2] = max(polyf_points[0].v[2], max(polyf_points[1].v[2], polyf_points[2].v[2])) + 1;
1130 if (BoxesOverlap(segmentmins, segmentmaxs, facemins, facemaxs))
1132 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
1133 //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
1134 Collision_TraceLineBrushFloat(trace, linestart, lineend, &polyf_brush, &polyf_brush);
1141 static colpointf_t polyf_pointsstart[256], polyf_pointsend[256];
1142 static colplanef_t polyf_planesstart[256 + 2], polyf_planesend[256 + 2];
1143 static colbrushf_t polyf_brushstart, polyf_brushend;
1145 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)
1148 if (numpoints > 256)
1150 Con_Print("Polygon with more than 256 points not supported yet (fixme!)\n");
1153 polyf_brushstart.numpoints = numpoints;
1154 polyf_brushstart.numplanes = numpoints + 2;
1155 polyf_brushstart.points = polyf_pointsstart;//(colpointf_t *)points;
1156 polyf_brushstart.planes = polyf_planesstart;
1157 polyf_brushstart.supercontents = supercontents;
1158 for (i = 0;i < numpoints;i++)
1159 Matrix4x4_Transform(polygonmatrixstart, points + i * 3, polyf_brushstart.points[i].v);
1160 polyf_brushend.numpoints = numpoints;
1161 polyf_brushend.numplanes = numpoints + 2;
1162 polyf_brushend.points = polyf_pointsend;//(colpointf_t *)points;
1163 polyf_brushend.planes = polyf_planesend;
1164 polyf_brushend.supercontents = supercontents;
1165 for (i = 0;i < numpoints;i++)
1166 Matrix4x4_Transform(polygonmatrixend, points + i * 3, polyf_brushend.points[i].v);
1167 Collision_SnapCopyPoints(numpoints, polyf_pointsstart, polyf_pointsstart, COLLISION_SNAPSCALE, COLLISION_SNAP);
1168 Collision_SnapCopyPoints(numpoints, polyf_pointsend, polyf_pointsend, COLLISION_SNAPSCALE, COLLISION_SNAP);
1169 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brushstart);
1170 Collision_CalcPlanesForPolygonBrushFloat(&polyf_brushend);
1172 //Collision_PrintBrushAsQHull(&polyf_brushstart, "polyf_brushstart");
1173 //Collision_PrintBrushAsQHull(&polyf_brushend, "polyf_brushend");
1175 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brushstart, &polyf_brushend);
1180 #define MAX_BRUSHFORBOX 16
1181 static int brushforbox_index = 0;
1182 static colpointf_t brushforbox_point[MAX_BRUSHFORBOX*8];
1183 static colplanef_t brushforbox_plane[MAX_BRUSHFORBOX*6];
1184 static colbrushf_t brushforbox_brush[MAX_BRUSHFORBOX];
1185 static colbrushf_t brushforpoint_brush[MAX_BRUSHFORBOX];
1187 void Collision_InitBrushForBox(void)
1190 for (i = 0;i < MAX_BRUSHFORBOX;i++)
1192 brushforbox_brush[i].supercontents = SUPERCONTENTS_SOLID;
1193 brushforbox_brush[i].numpoints = 8;
1194 brushforbox_brush[i].numplanes = 6;
1195 brushforbox_brush[i].points = brushforbox_point + i * 8;
1196 brushforbox_brush[i].planes = brushforbox_plane + i * 6;
1197 brushforpoint_brush[i].supercontents = SUPERCONTENTS_SOLID;
1198 brushforpoint_brush[i].numpoints = 1;
1199 brushforpoint_brush[i].numplanes = 0;
1200 brushforpoint_brush[i].points = brushforbox_point + i * 8;
1201 brushforpoint_brush[i].planes = brushforbox_plane + i * 6;
1205 colbrushf_t *Collision_BrushForBox(const matrix4x4_t *matrix, const vec3_t mins, const vec3_t maxs)
1210 if (brushforbox_brush[0].numpoints == 0)
1211 Collision_InitBrushForBox();
1212 if (VectorCompare(mins, maxs))
1215 brush = brushforpoint_brush + ((brushforbox_index++) % MAX_BRUSHFORBOX);
1216 VectorCopy(mins, brush->points->v);
1220 brush = brushforbox_brush + ((brushforbox_index++) % MAX_BRUSHFORBOX);
1222 for (i = 0;i < 8;i++)
1224 v[0] = i & 1 ? maxs[0] : mins[0];
1225 v[1] = i & 2 ? maxs[1] : mins[1];
1226 v[2] = i & 4 ? maxs[2] : mins[2];
1227 Matrix4x4_Transform(matrix, v, brush->points[i].v);
1230 for (i = 0;i < 6;i++)
1233 v[i >> 1] = i & 1 ? 1 : -1;
1234 Matrix4x4_Transform3x3(matrix, v, brush->planes[i].normal);
1235 VectorNormalize(brush->planes[i].normal);
1238 for (j = 0;j < brush->numplanes;j++)
1239 brush->planes[j].dist = furthestplanedist_float(brush->planes[j].normal, brush->points, brush->numpoints);
1240 VectorCopy(brush->points[0].v, brush->mins);
1241 VectorCopy(brush->points[0].v, brush->maxs);
1242 for (j = 1;j < brush->numpoints;j++)
1244 brush->mins[0] = min(brush->mins[0], brush->points[j].v[0]);
1245 brush->mins[1] = min(brush->mins[1], brush->points[j].v[1]);
1246 brush->mins[2] = min(brush->mins[2], brush->points[j].v[2]);
1247 brush->maxs[0] = max(brush->maxs[0], brush->points[j].v[0]);
1248 brush->maxs[1] = max(brush->maxs[1], brush->points[j].v[1]);
1249 brush->maxs[2] = max(brush->maxs[2], brush->points[j].v[2]);
1251 brush->mins[0] -= 1;
1252 brush->mins[1] -= 1;
1253 brush->mins[2] -= 1;
1254 brush->maxs[0] += 1;
1255 brush->maxs[1] += 1;
1256 brush->maxs[2] += 1;
1257 Collision_ValidateBrush(brush);
1261 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)
1263 colbrushf_t *boxbrush, *thisbrush_start, *thisbrush_end;
1264 matrix4x4_t identitymatrix;
1265 vec3_t startmins, startmaxs, endmins, endmaxs;
1267 // create brushes for the collision
1268 VectorAdd(start, mins, startmins);
1269 VectorAdd(start, maxs, startmaxs);
1270 VectorAdd(end, mins, endmins);
1271 VectorAdd(end, maxs, endmaxs);
1272 Matrix4x4_CreateIdentity(&identitymatrix);
1273 boxbrush = Collision_BrushForBox(&identitymatrix, cmins, cmaxs);
1274 thisbrush_start = Collision_BrushForBox(&identitymatrix, startmins, startmaxs);
1275 thisbrush_end = Collision_BrushForBox(&identitymatrix, endmins, endmaxs);
1277 memset(trace, 0, sizeof(trace_t));
1278 trace->hitsupercontentsmask = hitsupercontentsmask;
1279 trace->fraction = 1;
1280 trace->realfraction = 1;
1281 trace->allsolid = true;
1282 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, boxbrush, boxbrush);
1285 // LordHavoc: currently unused and not yet tested
1286 // note: this can be used for tracing a moving sphere vs a stationary sphere,
1287 // by simply adding the moving sphere's radius to the sphereradius parameter,
1288 // all the results are correct (impactpoint, impactnormal, and fraction)
1289 float Collision_ClipTrace_Line_Sphere(double *linestart, double *lineend, double *sphereorigin, double sphereradius, double *impactpoint, double *impactnormal)
1291 double dir[3], scale, v[3], deviationdist, impactdist, linelength;
1292 // make sure the impactpoint and impactnormal are valid even if there is
1294 impactpoint[0] = lineend[0];
1295 impactpoint[1] = lineend[1];
1296 impactpoint[2] = lineend[2];
1297 impactnormal[0] = 0;
1298 impactnormal[1] = 0;
1299 impactnormal[2] = 0;
1300 // calculate line direction
1301 dir[0] = lineend[0] - linestart[0];
1302 dir[1] = lineend[1] - linestart[1];
1303 dir[2] = lineend[2] - linestart[2];
1304 // normalize direction
1305 linelength = sqrt(dir[0] * dir[0] + dir[1] * dir[1] + dir[2] * dir[2]);
1308 scale = 1.0 / linelength;
1313 // this dotproduct calculates the distance along the line at which the
1314 // sphere origin is (nearest point to the sphere origin on the line)
1315 impactdist = dir[0] * (sphereorigin[0] - linestart[0]) + dir[1] * (sphereorigin[1] - linestart[1]) + dir[2] * (sphereorigin[2] - linestart[2]);
1316 // calculate point on line at that distance, and subtract the
1317 // sphereorigin from it, so we have a vector to measure for the distance
1318 // of the line from the sphereorigin (deviation, how off-center it is)
1319 v[0] = linestart[0] + impactdist * dir[0] - sphereorigin[0];
1320 v[1] = linestart[1] + impactdist * dir[1] - sphereorigin[1];
1321 v[2] = linestart[2] + impactdist * dir[2] - sphereorigin[2];
1322 deviationdist = v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
1323 // if outside the radius, it's a miss for sure
1324 // (we do this comparison using squared radius to avoid a sqrt)
1325 if (deviationdist > sphereradius*sphereradius)
1326 return 1; // miss (off to the side)
1327 // nudge back to find the correct impact distance
1328 impactdist += (sqrt(deviationdist) - sphereradius);
1329 if (impactdist >= linelength)
1330 return 1; // miss (not close enough)
1332 return 1; // miss (linestart is past or inside sphere)
1333 // calculate new impactpoint
1334 impactpoint[0] = linestart[0] + impactdist * dir[0];
1335 impactpoint[1] = linestart[1] + impactdist * dir[1];
1336 impactpoint[2] = linestart[2] + impactdist * dir[2];
1337 // calculate impactnormal (surface normal at point of impact)
1338 impactnormal[0] = impactpoint[0] - sphereorigin[0];
1339 impactnormal[1] = impactpoint[1] - sphereorigin[1];
1340 impactnormal[2] = impactpoint[2] - sphereorigin[2];
1341 // normalize impactnormal
1342 scale = impactnormal[0] * impactnormal[0] + impactnormal[1] * impactnormal[1] + impactnormal[2] * impactnormal[2];
1345 scale = 1.0 / sqrt(scale);
1346 impactnormal[0] *= scale;
1347 impactnormal[1] *= scale;
1348 impactnormal[2] *= scale;
1350 // return fraction of movement distance
1351 return impactdist / linelength;
1354 void Collision_TraceLineTriangleFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, const float *point0, const float *point1, const float *point2)
1356 float d1, d2, d, f, fnudged, impact[3], edgenormal[3], faceplanenormal[3], faceplanedist, edge[3];
1358 // this code is designed for clockwise triangles, conversion to
1359 // counterclockwise would require swapping some things around...
1360 // it is easier to simply swap the point0 and point2 parameters to this
1361 // function when calling it than it is to rewire the internals.
1363 // calculate the faceplanenormal of the triangle, this represents the front side
1364 TriangleNormal(point0, point1, point2, faceplanenormal);
1365 // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
1366 if (DotProduct(faceplanenormal, faceplanenormal) < 0.0001f)
1368 // normalize the normal
1369 VectorNormalize(faceplanenormal);
1370 // calculate the distance
1371 faceplanedist = DotProduct(point0, faceplanenormal);
1373 // calculate the start distance
1374 d1 = DotProduct(faceplanenormal, linestart) - faceplanedist;
1375 // if start point is on the back side there is no collision
1376 // (we don't care about traces going through the triangle the wrong way)
1380 // calculate the end distance
1381 d2 = DotProduct(faceplanenormal, lineend) - faceplanedist;
1382 // if both are in front, there is no collision
1386 // from here on we know d1 is >= 0 and d2 is < 0
1387 // this means the line starts infront and ends behind, passing through it
1389 // calculate the recipricol of the distance delta,
1390 // so we can use it multiple times cheaply (instead of division)
1391 d = 1.0f / (d1 - d2);
1392 // calculate the impact fraction by taking the start distance (> 0)
1393 // and subtracting the face plane distance (this is the distance of the
1394 // triangle along that same normal)
1395 // then multiply by the recipricol distance delta
1397 // skip out if this impact is further away than previous ones
1398 if (f > trace->realfraction)
1400 // calculate the perfect impact point for classification of insidedness
1401 impact[0] = linestart[0] + f * (lineend[0] - linestart[0]);
1402 impact[1] = linestart[1] + f * (lineend[1] - linestart[1]);
1403 impact[2] = linestart[2] + f * (lineend[2] - linestart[2]);
1405 // calculate the edge normal and reject if impact is outside triangle
1406 // (an edge normal faces away from the triangle, to get the desired normal
1407 // a crossproduct with the faceplanenormal is used, and because of the way
1408 // the insidedness comparison is written it does not need to be normalized)
1410 VectorSubtract(point2, point0, edge);
1411 CrossProduct(edge, faceplanenormal, edgenormal);
1412 if (DotProduct(impact, edgenormal) > DotProduct(point0, edgenormal))
1415 VectorSubtract(point0, point1, edge);
1416 CrossProduct(edge, faceplanenormal, edgenormal);
1417 if (DotProduct(impact, edgenormal) > DotProduct(point1, edgenormal))
1420 VectorSubtract(point1, point2, edge);
1421 CrossProduct(edge, faceplanenormal, edgenormal);
1422 if (DotProduct(impact, edgenormal) > DotProduct(point2, edgenormal))
1425 // store the new trace fraction
1426 trace->realfraction = bound(0, f, 1);
1428 // calculate a nudged fraction to keep it out of the surface
1429 // (the main fraction remains perfect)
1430 fnudged = (d1 - collision_impactnudge.value) * d;
1431 trace->fraction = bound(0, fnudged, 1);
1433 // store the new trace endpos
1434 // not needed, it's calculated later when the trace is finished
1435 //trace->endpos[0] = linestart[0] + fnudged * (lineend[0] - linestart[0]);
1436 //trace->endpos[1] = linestart[1] + fnudged * (lineend[1] - linestart[1]);
1437 //trace->endpos[2] = linestart[2] + fnudged * (lineend[2] - linestart[2]);
1439 // store the new trace plane (because collisions only happen from
1440 // the front this is always simply the triangle normal, never flipped)
1441 VectorCopy(faceplanenormal, trace->plane.normal);
1442 trace->plane.dist = faceplanedist;
1445 typedef struct colbspnode_s
1448 struct colbspnode_s *children[2];
1449 // the node is reallocated or split if max is reached
1452 colbrushf_t **colbrushflist;
1455 //colbrushd_t **colbrushdlist;
1459 typedef struct colbsp_s
1462 colbspnode_t *nodes;
1466 colbsp_t *Collision_CreateCollisionBSP(mempool_t *mempool)
1469 bsp = Mem_Alloc(mempool, sizeof(colbsp_t));
1470 bsp->mempool = mempool;
1471 bsp->nodes = Mem_Alloc(bsp->mempool, sizeof(colbspnode_t));
1475 void Collision_FreeCollisionBSPNode(colbspnode_t *node)
1477 if (node->children[0])
1478 Collision_FreeCollisionBSPNode(node->children[0]);
1479 if (node->children[1])
1480 Collision_FreeCollisionBSPNode(node->children[1]);
1481 while (--node->numcolbrushf)
1482 Mem_Free(node->colbrushflist[node->numcolbrushf]);
1483 //while (--node->numcolbrushd)
1484 // Mem_Free(node->colbrushdlist[node->numcolbrushd]);
1488 void Collision_FreeCollisionBSP(colbsp_t *bsp)
1490 Collision_FreeCollisionBSPNode(bsp->nodes);
1494 void Collision_BoundingBoxOfBrushTraceSegment(const colbrushf_t *start, const colbrushf_t *end, vec3_t mins, vec3_t maxs, float startfrac, float endfrac)
1497 colpointf_t *ps, *pe;
1498 float tempstart[3], tempend[3];
1499 VectorLerp(start->points[0].v, startfrac, end->points[0].v, mins);
1500 VectorCopy(mins, maxs);
1501 for (i = 0, ps = start->points, pe = end->points;i < start->numpoints;i++, ps++, pe++)
1503 VectorLerp(ps->v, startfrac, pe->v, tempstart);
1504 VectorLerp(ps->v, endfrac, pe->v, tempend);
1505 mins[0] = min(mins[0], min(tempstart[0], tempend[0]));
1506 mins[1] = min(mins[1], min(tempstart[1], tempend[1]));
1507 mins[2] = min(mins[2], min(tempstart[2], tempend[2]));
1508 maxs[0] = min(maxs[0], min(tempstart[0], tempend[0]));
1509 maxs[1] = min(maxs[1], min(tempstart[1], tempend[1]));
1510 maxs[2] = min(maxs[2], min(tempstart[2], tempend[2]));