Fixed a warning (unused variable)
[divverent/darkplaces.git] / collision.c
1
2 #include "quakedef.h"
3 #include "winding.h"
4
5 #define COLLISION_SNAPSCALE (32.0f)
6 #define COLLISION_SNAP (1.0f / COLLISION_SNAPSCALE)
7
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"};
13
14 #if 0
15 typedef struct
16 {
17         // the hull we're tracing through
18         const hull_t *hull;
19
20         // the trace structure to fill in
21         trace_t *trace;
22
23         // start and end of the trace (in model space)
24         double start[3];
25         double end[3];
26
27         // end - start
28         double dist[3];
29
30         // overrides the CONTENTS_SOLID in the box bsp tree
31         int boxsupercontents;
32 }
33 RecursiveHullCheckTraceInfo_t;
34
35 #define HULLCHECKSTATE_EMPTY 0
36 #define HULLCHECKSTATE_SOLID 1
37 #define HULLCHECKSTATE_DONE 2
38
39 static int RecursiveHullCheck(RecursiveHullCheckTraceInfo_t *t, int num, double p1f, double p2f, double p1[3], double p2[3])
40 {
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)
44         int ret;
45         mplane_t *plane;
46         double t1, t2;
47
48         // variables that need to be stored on the stack when recursing
49         dclipnode_t *node;
50         int side;
51         double midf, mid[3];
52
53         // LordHavoc: a goto!  everyone flee in terror... :)
54 loc0:
55         // check for empty
56         if (num < 0)
57         {
58                 num = Mod_Q1BSP_SuperContentsFromNativeContents(NULL, num);
59                 if (!t->trace->startfound)
60                 {
61                         t->trace->startfound = true;
62                         t->trace->startsupercontents |= num;
63                 }
64                 if (num & SUPERCONTENTS_LIQUIDSMASK)
65                         t->trace->inwater = true;
66                 if (num == 0)
67                         t->trace->inopen = true;
68                 if (num & t->trace->hitsupercontentsmask)
69                 {
70                         // if the first leaf is solid, set startsolid
71                         if (t->trace->allsolid)
72                                 t->trace->startsolid = true;
73 #if COLLISIONPARANOID >= 3
74                         Con_Print("S");
75 #endif
76                         return HULLCHECKSTATE_SOLID;
77                 }
78                 else
79                 {
80                         t->trace->allsolid = false;
81 #if COLLISIONPARANOID >= 3
82                         Con_Print("E");
83 #endif
84                         return HULLCHECKSTATE_EMPTY;
85                 }
86         }
87
88         // find the point distances
89         node = t->hull->clipnodes + num;
90
91         plane = t->hull->planes + node->planenum;
92         if (plane->type < 3)
93         {
94                 t1 = p1[plane->type] - plane->dist;
95                 t2 = p2[plane->type] - plane->dist;
96         }
97         else
98         {
99                 t1 = DotProduct (plane->normal, p1) - plane->dist;
100                 t2 = DotProduct (plane->normal, p2) - plane->dist;
101         }
102
103         if (t1 < 0)
104         {
105                 if (t2 < 0)
106                 {
107 #if COLLISIONPARANOID >= 3
108                         Con_Print("<");
109 #endif
110                         num = node->children[1];
111                         goto loc0;
112                 }
113                 side = 1;
114         }
115         else
116         {
117                 if (t2 >= 0)
118                 {
119 #if COLLISIONPARANOID >= 3
120                         Con_Print(">");
121 #endif
122                         num = node->children[0];
123                         goto loc0;
124                 }
125                 side = 0;
126         }
127
128         // the line intersects, find intersection point
129         // LordHavoc: this uses the original trace for maximum accuracy
130 #if COLLISIONPARANOID >= 3
131         Con_Print("M");
132 #endif
133         if (plane->type < 3)
134         {
135                 t1 = t->start[plane->type] - plane->dist;
136                 t2 = t->end[plane->type] - plane->dist;
137         }
138         else
139         {
140                 t1 = DotProduct (plane->normal, t->start) - plane->dist;
141                 t2 = DotProduct (plane->normal, t->end) - plane->dist;
142         }
143
144         midf = t1 / (t1 - t2);
145         midf = bound(p1f, midf, p2f);
146         VectorMA(t->start, midf, t->dist, mid);
147
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)
152                 return ret;
153
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)
157                 return ret;
158
159         // front is air and back is solid, this is the impact point...
160         if (side)
161         {
162                 t->trace->plane.dist = -plane->dist;
163                 VectorNegate (plane->normal, t->trace->plane.normal);
164         }
165         else
166         {
167                 t->trace->plane.dist = plane->dist;
168                 VectorCopy (plane->normal, t->trace->plane.normal);
169         }
170
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);
176
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);
180
181 #if COLLISIONPARANOID >= 3
182         Con_Print("D");
183 #endif
184         return HULLCHECKSTATE_DONE;
185 }
186
187 #if 0
188 // used if start and end are the same
189 static void RecursiveHullCheckPoint (RecursiveHullCheckTraceInfo_t *t, int num)
190 {
191         // If you can read this, you understand BSP trees
192         while (num >= 0)
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];
194
195         // check for empty
196         t->trace->endcontents = num;
197         if (t->trace->thiscontents)
198         {
199                 if (num == t->trace->thiscontents)
200                         t->trace->allsolid = false;
201                 else
202                 {
203                         // if the first leaf is solid, set startsolid
204                         if (t->trace->allsolid)
205                                 t->trace->startsolid = true;
206                 }
207         }
208         else
209         {
210                 if (num != CONTENTS_SOLID)
211                 {
212                         t->trace->allsolid = false;
213                         if (num == CONTENTS_EMPTY)
214                                 t->trace->inopen = true;
215                         else
216                                 t->trace->inwater = true;
217                 }
218                 else
219                 {
220                         // if the first leaf is solid, set startsolid
221                         if (t->trace->allsolid)
222                                 t->trace->startsolid = true;
223                 }
224         }
225 }
226 #endif
227
228 static hull_t box_hull;
229 static dclipnode_t box_clipnodes[6];
230 static mplane_t box_planes[6];
231
232 void Mod_Q1BSP_Collision_Init (void)
233 {
234         int             i;
235         int             side;
236
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.
239
240         box_hull.clipnodes = box_clipnodes;
241         box_hull.planes = box_planes;
242         box_hull.firstclipnode = 0;
243         box_hull.lastclipnode = 5;
244
245         for (i = 0;i < 6;i++)
246         {
247                 box_clipnodes[i].planenum = i;
248
249                 side = i&1;
250
251                 box_clipnodes[i].children[side] = CONTENTS_EMPTY;
252                 if (i != 5)
253                         box_clipnodes[i].children[side^1] = i + 1;
254                 else
255                         box_clipnodes[i].children[side^1] = CONTENTS_SOLID;
256
257                 box_planes[i].type = i>>1;
258                 box_planes[i].normal[i>>1] = 1;
259         }
260 }
261
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)
263 {
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;
280         rhc.trace = trace;
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;
292 }
293 #endif
294
295 void Collision_Init (void)
296 {
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);
302 }
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317 void Collision_PrintBrushAsQHull(colbrushf_t *brush, const char *name)
318 {
319         int i;
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]);
323         // FIXME: optimize!
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);
327 }
328
329 void Collision_ValidateBrush(colbrushf_t *brush)
330 {
331         int j, k, pointsoffplanes, printbrush;
332         float d;
333         printbrush = false;
334         if (!brush->numpoints)
335         {
336                 Con_Print("Collision_ValidateBrush: brush with no points!\n");
337                 printbrush = true;
338         }
339 #if 0
340         // it's ok for a brush to have one point and no planes...
341         if (brush->numplanes == 0 && brush->numpoints != 1)
342         {
343                 Con_Print("Collision_ValidateBrush: brush with no planes and more than one point!\n");
344                 printbrush = true;
345         }
346 #endif
347         if (brush->numplanes)
348         {
349                 pointsoffplanes = 0;
350                 for (k = 0;k < brush->numplanes;k++)
351                 {
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++)
355                         {
356                                 d = DotProduct(brush->points[j].v, brush->planes[k].normal) - brush->planes[k].dist;
357                                 if (d > (1.0f / 8.0f))
358                                 {
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);
360                                         printbrush = true;
361                                 }
362                                 if (fabs(d) > 0.01f)
363                                         pointsoffplanes++;
364                         }
365                 }
366                 if (pointsoffplanes == 0) // all points are on all planes
367                 {
368                         Con_Print("Collision_ValidateBrush: all points lie on all planes (degenerate, no brush volume!)\n");
369                         printbrush = true;
370                 }
371         }
372         if (printbrush)
373                 Collision_PrintBrushAsQHull(brush, "unnamed");
374 }
375
376 float nearestplanedist_float(const float *normal, const colpointf_t *points, int numpoints)
377 {
378         float dist, bestdist;
379         bestdist = DotProduct(points->v, normal);
380         points++;
381         while(--numpoints)
382         {
383                 dist = DotProduct(points->v, normal);
384                 bestdist = min(bestdist, dist);
385                 points++;
386         }
387         return bestdist;
388 }
389
390 float furthestplanedist_float(const float *normal, const colpointf_t *points, int numpoints)
391 {
392         float dist, bestdist;
393         bestdist = DotProduct(points->v, normal);
394         points++;
395         while(--numpoints)
396         {
397                 dist = DotProduct(points->v, normal);
398                 bestdist = max(bestdist, dist);
399                 points++;
400         }
401         return bestdist;
402 }
403
404
405 colbrushf_t *Collision_NewBrushFromPlanes(mempool_t *mempool, int numoriginalplanes, const mplane_t *originalplanes, int supercontents, winding_t *temp1, winding_t *temp2)
406 {
407         int j, k, m;
408         int numpoints, maxpoints, numplanes, maxplanes, numelements, maxelements, numtriangles, numpolypoints, maxpolypoints;
409         winding_t *w, *temp, *othertemp;
410         colbrushf_t *brush;
411         colpointf_t pointsbuf[256];
412         colplanef_t planesbuf[256];
413         int elementsbuf[1024];
414         int polypointbuf[256];
415         // construct a collision brush (points, planes, and renderable mesh) from
416         // a set of planes, this also optimizes out any unnecessary planes (ones
417         // whose polygon is clipped away by the other planes)
418         numpoints = 0;maxpoints = 256;
419         numplanes = 0;maxplanes = 256;
420         numelements = 0;maxelements = 1024;
421         numtriangles = 0;
422         maxpolypoints = 256;
423         for (j = 0;j < numoriginalplanes;j++)
424         {
425                 // add the plane uniquely (no duplicates)
426                 for (k = 0;k < numplanes;k++)
427                         if (VectorCompare(planesbuf[k].normal, originalplanes[j].normal) && planesbuf[k].dist == originalplanes[j].dist)
428                                 break;
429                 // if the plane is a duplicate, skip it
430                 if (k < numplanes)
431                         continue;
432                 // check if there are too many and skip the brush
433                 if (numplanes >= 256)
434                 {
435                         Con_Print("Mod_Q3BSP_LoadBrushes: failed to build collision brush: too many planes for buffer\n");
436                         return NULL;
437                 }
438
439                 // create a large polygon from the plane
440                 w = temp1;
441                 othertemp = temp2;
442                 BufWinding_NewFromPlane(w, originalplanes[j].normal[0], originalplanes[j].normal[1], originalplanes[j].normal[2], originalplanes[j].dist);
443                 // clip it by all other planes
444                 for (k = 0;k < numoriginalplanes && w->numpoints;k++)
445                 {
446                         if (k != j)
447                         {
448                                 // we want to keep the inside of the brush plane so we flip
449                                 // the cutting plane
450                                 BufWinding_Divide(w, -originalplanes[k].normal[0], -originalplanes[k].normal[1], -originalplanes[k].normal[2], -originalplanes[k].dist, othertemp, NULL, NULL, NULL);
451                                 temp = w;
452                                 w = othertemp;
453                                 othertemp = temp;
454                         }
455                 }
456                 // if nothing is left, skip it
457                 if (!w->numpoints)
458                         continue;
459
460                 // copy off the number of points for later when the winding is freed
461                 numpolypoints = w->numpoints;
462
463                 // check if there are too many polygon vertices for buffer
464                 if (numpolypoints > maxpolypoints)
465                 {
466                         Con_Print("Collision_NewBrushFromPlanes: failed to build collision brush: too many points for buffer\n");
467                         return NULL;
468                 }
469
470                 // check if there are too many triangle elements for buffer
471                 if (numelements + (w->numpoints - 2) * 3 > maxelements)
472                 {
473                         Con_Print("Collision_NewBrushFromPlanes: failed to build collision brush: too many triangle elements for buffer\n");
474                         return NULL;
475                 }
476
477                 for (k = 0;k < w->numpoints;k++)
478                 {
479                         // check if there is already a matching point (no duplicates)
480                         for (m = 0;m < numpoints;m++)
481                                 if (VectorDistance2(w->points[k], pointsbuf[m].v) < COLLISION_SNAP)
482                                         break;
483
484                         // if there is no match, add a new one
485                         if (m == numpoints)
486                         {
487                                 // check if there are too many and skip the brush
488                                 if (numpoints >= 256)
489                                 {
490                                         Con_Print("Collision_NewBrushFromPlanes: failed to build collision brush: too many points for buffer\n");
491                                         Winding_Free(w);
492                                         return NULL;
493                                 }
494                                 // add the new one
495                                 VectorCopy(w->points[k], pointsbuf[numpoints].v);
496                                 numpoints++;
497                         }
498
499                         // store the index into a buffer
500                         polypointbuf[k] = m;
501                 }
502                 w = NULL;
503                 othertemp = NULL;
504                 temp = NULL;
505
506                 // add the triangles for the polygon
507                 // (this particular code makes a triangle fan)
508                 for (k = 0;k < numpolypoints - 2;k++)
509                 {
510                         numtriangles++;
511                         elementsbuf[numelements++] = polypointbuf[0];
512                         elementsbuf[numelements++] = polypointbuf[k + 1];
513                         elementsbuf[numelements++] = polypointbuf[k + 2];
514                 }
515
516                 // add the new plane
517                 VectorCopy(originalplanes[j].normal, planesbuf[numplanes].normal);
518                 planesbuf[numplanes].dist = originalplanes[j].dist;
519                 numplanes++;
520         }
521
522         // if nothing is left, there's nothing to allocate
523         if (numtriangles < 4 || numplanes < 4 || numpoints < 4)
524                 return NULL;
525
526         // allocate the brush and copy to it
527         brush = Collision_AllocBrushFloat(mempool, numpoints, numplanes, numtriangles, supercontents);
528         memcpy(brush->points, pointsbuf, numpoints * sizeof(colpointf_t));
529         memcpy(brush->planes, planesbuf, numplanes * sizeof(colplanef_t));
530         memcpy(brush->elements, elementsbuf, numtriangles * sizeof(int[3]));
531         // recalc distances
532         for (j = 0;j < brush->numplanes;j++)
533                 brush->planes[j].dist = furthestplanedist_float(brush->planes[j].normal, brush->points, brush->numpoints);
534         VectorCopy(brush->points[0].v, brush->mins);
535         VectorCopy(brush->points[0].v, brush->maxs);
536         for (j = 1;j < brush->numpoints;j++)
537         {
538                 brush->mins[0] = min(brush->mins[0], brush->points[j].v[0]);
539                 brush->mins[1] = min(brush->mins[1], brush->points[j].v[1]);
540                 brush->mins[2] = min(brush->mins[2], brush->points[j].v[2]);
541                 brush->maxs[0] = max(brush->maxs[0], brush->points[j].v[0]);
542                 brush->maxs[1] = max(brush->maxs[1], brush->points[j].v[1]);
543                 brush->maxs[2] = max(brush->maxs[2], brush->points[j].v[2]);
544         }
545         brush->mins[0] -= 1;
546         brush->mins[1] -= 1;
547         brush->mins[2] -= 1;
548         brush->maxs[0] += 1;
549         brush->maxs[1] += 1;
550         brush->maxs[2] += 1;
551         Collision_ValidateBrush(brush);
552         return brush;
553 }
554
555
556
557 colbrushf_t *Collision_AllocBrushFloat(mempool_t *mempool, int numpoints, int numplanes, int numtriangles, int supercontents)
558 {
559         colbrushf_t *brush;
560         brush = Mem_Alloc(mempool, sizeof(colbrushf_t) + sizeof(colpointf_t) * numpoints + sizeof(colplanef_t) * numplanes + sizeof(int[3]) * numtriangles);
561         brush->supercontents = supercontents;
562         brush->numplanes = numplanes;
563         brush->numpoints = numpoints;
564         brush->numtriangles = numtriangles;
565         brush->planes = (void *)(brush + 1);
566         brush->points = (void *)(brush->planes + brush->numplanes);
567         brush->elements = (void *)(brush->points + brush->numpoints);
568         return brush;
569 }
570
571 void Collision_CalcPlanesForPolygonBrushFloat(colbrushf_t *brush)
572 {
573         int i;
574         float edge0[3], edge1[3], edge2[3], normal[3], dist, bestdist;
575         colpointf_t *p, *p2;
576
577         if (brush->numpoints == 3)
578         {
579                 // optimized triangle case
580                 TriangleNormal(brush->points[0].v, brush->points[1].v, brush->points[2].v, brush->planes[0].normal);
581                 if (DotProduct(brush->planes[0].normal, brush->planes[0].normal) < 0.0001f)
582                 {
583                         // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
584                         brush->numplanes = 0;
585                         return;
586                 }
587                 else
588                 {
589                         brush->numplanes = 5;
590                         VectorNormalize(brush->planes[0].normal);
591                         brush->planes[0].dist = DotProduct(brush->points->v, brush->planes[0].normal);
592                         VectorNegate(brush->planes[0].normal, brush->planes[1].normal);
593                         brush->planes[1].dist = -brush->planes[0].dist;
594                         VectorSubtract(brush->points[2].v, brush->points[0].v, edge0);
595                         VectorSubtract(brush->points[0].v, brush->points[1].v, edge1);
596                         VectorSubtract(brush->points[1].v, brush->points[2].v, edge2);
597 #if 1
598                         {
599                                 float projectionnormal[3], projectionedge0[3], projectionedge1[3], projectionedge2[3];
600                                 int i, best;
601                                 float dist, bestdist;
602                                 bestdist = fabs(brush->planes[0].normal[0]);
603                                 best = 0;
604                                 for (i = 1;i < 3;i++)
605                                 {
606                                         dist = fabs(brush->planes[0].normal[i]);
607                                         if (bestdist < dist)
608                                         {
609                                                 bestdist = dist;
610                                                 best = i;
611                                         }
612                                 }
613                                 VectorClear(projectionnormal);
614                                 if (brush->planes[0].normal[best] < 0)
615                                         projectionnormal[best] = -1;
616                                 else
617                                         projectionnormal[best] = 1;
618                                 VectorCopy(edge0, projectionedge0);
619                                 VectorCopy(edge1, projectionedge1);
620                                 VectorCopy(edge2, projectionedge2);
621                                 projectionedge0[best] = 0;
622                                 projectionedge1[best] = 0;
623                                 projectionedge2[best] = 0;
624                                 CrossProduct(projectionedge0, projectionnormal, brush->planes[2].normal);
625                                 CrossProduct(projectionedge1, projectionnormal, brush->planes[3].normal);
626                                 CrossProduct(projectionedge2, projectionnormal, brush->planes[4].normal);
627                         }
628 #else
629                         CrossProduct(edge0, brush->planes->normal, brush->planes[2].normal);
630                         CrossProduct(edge1, brush->planes->normal, brush->planes[3].normal);
631                         CrossProduct(edge2, brush->planes->normal, brush->planes[4].normal);
632 #endif
633                         VectorNormalize(brush->planes[2].normal);
634                         VectorNormalize(brush->planes[3].normal);
635                         VectorNormalize(brush->planes[4].normal);
636                         brush->planes[2].dist = DotProduct(brush->points[2].v, brush->planes[2].normal);
637                         brush->planes[3].dist = DotProduct(brush->points[0].v, brush->planes[3].normal);
638                         brush->planes[4].dist = DotProduct(brush->points[1].v, brush->planes[4].normal);
639
640                         if (developer.integer)
641                         {
642                                 // validation code
643 #if 0
644                                 float temp[3];
645
646                                 VectorSubtract(brush->points[0].v, brush->points[1].v, edge0);
647                                 VectorSubtract(brush->points[2].v, brush->points[1].v, edge1);
648                                 CrossProduct(edge0, edge1, normal);
649                                 VectorNormalize(normal);
650                                 VectorSubtract(normal, brush->planes[0].normal, temp);
651                                 if (VectorLength(temp) > 0.01f)
652                                         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]);
653                                 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)
654                                         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);
655 #if 0
656                                 if (fabs(DotProduct(brush->planes[2].normal, brush->planes[0].normal)) > 0.01f)
657                                         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);
658                                 if (fabs(DotProduct(brush->planes[3].normal, brush->planes[0].normal)) > 0.01f)
659                                         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);
660                                 if (fabs(DotProduct(brush->planes[4].normal, brush->planes[0].normal)) > 0.01f)
661                                         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);
662                                 if (fabs(DotProduct(brush->planes[2].normal, edge0)) > 0.01f)
663                                         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]);
664                                 if (fabs(DotProduct(brush->planes[3].normal, edge1)) > 0.01f)
665                                         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]);
666                                 if (fabs(DotProduct(brush->planes[4].normal, edge2)) > 0.01f)
667                                         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]);
668 #endif
669 #endif
670                                 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)
671                                         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);
672                                 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)
673                                         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);
674                                 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)
675                                         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);
676                                 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)
677                                         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);
678                                 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)
679                                         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);
680                         }
681                 }
682         }
683         else
684         {
685                 // choose best surface normal for polygon's plane
686                 bestdist = 0;
687                 for (i = 0, p = brush->points + 1;i < brush->numpoints - 2;i++, p++)
688                 {
689                         VectorSubtract(p[-1].v, p[0].v, edge0);
690                         VectorSubtract(p[1].v, p[0].v, edge1);
691                         CrossProduct(edge0, edge1, normal);
692                         //TriangleNormal(p[-1].v, p[0].v, p[1].v, normal);
693                         dist = DotProduct(normal, normal);
694                         if (i == 0 || bestdist < dist)
695                         {
696                                 bestdist = dist;
697                                 VectorCopy(normal, brush->planes->normal);
698                         }
699                 }
700                 if (bestdist < 0.0001f)
701                 {
702                         // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
703                         brush->numplanes = 0;
704                         return;
705                 }
706                 else
707                 {
708                         brush->numplanes = brush->numpoints + 2;
709                         VectorNormalize(brush->planes->normal);
710                         brush->planes->dist = DotProduct(brush->points->v, brush->planes->normal);
711
712                         // negate plane to create other side
713                         VectorNegate(brush->planes[0].normal, brush->planes[1].normal);
714                         brush->planes[1].dist = -brush->planes[0].dist;
715                         for (i = 0, p = brush->points + (brush->numpoints - 1), p2 = brush->points;i < brush->numpoints;i++, p = p2, p2++)
716                         {
717                                 VectorSubtract(p->v, p2->v, edge0);
718                                 CrossProduct(edge0, brush->planes->normal, brush->planes[i + 2].normal);
719                                 VectorNormalize(brush->planes[i + 2].normal);
720                                 brush->planes[i + 2].dist = DotProduct(p->v, brush->planes[i + 2].normal);
721                         }
722                 }
723         }
724
725         if (developer.integer)
726         {
727                 // validity check - will be disabled later
728                 Collision_ValidateBrush(brush);
729                 for (i = 0;i < brush->numplanes;i++)
730                 {
731                         int j;
732                         for (j = 0, p = brush->points;j < brush->numpoints;j++, p++)
733                                 if (DotProduct(p->v, brush->planes[i].normal) > brush->planes[i].dist + (1.0 / 32.0))
734                                         Con_Printf("Error in brush plane generation, plane %i\n", i);
735                 }
736         }
737 }
738
739 colbrushf_t *Collision_AllocBrushFromPermanentPolygonFloat(mempool_t *mempool, int numpoints, float *points, int supercontents)
740 {
741         colbrushf_t *brush;
742         brush = Mem_Alloc(mempool, sizeof(colbrushf_t) + sizeof(colplanef_t) * (numpoints + 2));
743         brush->supercontents = supercontents;
744         brush->numpoints = numpoints;
745         brush->numplanes = numpoints + 2;
746         brush->planes = (void *)(brush + 1);
747         brush->points = (colpointf_t *)points;
748         Host_Error("Collision_AllocBrushFromPermanentPolygonFloat: FIXME: this code needs to be updated to generate a mesh...\n");
749         return brush;
750 }
751
752 // NOTE: start and end of each brush pair must have same numplanes/numpoints
753 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)
754 {
755         int nplane, nplane2, fstartsolid, fendsolid, brushsolid;
756         float enterfrac, leavefrac, d1, d2, f, move, imove, newimpactnormal[3], enterfrac2;
757         const colplanef_t *startplane, *endplane;
758
759         enterfrac = -1;
760         enterfrac2 = -1;
761         leavefrac = 1;
762         fstartsolid = true;
763         fendsolid = true;
764
765         for (nplane = 0;nplane < thatbrush_start->numplanes + thisbrush_start->numplanes;nplane++)
766         {
767                 nplane2 = nplane;
768                 if (nplane2 >= thatbrush_start->numplanes)
769                 {
770                         nplane2 -= thatbrush_start->numplanes;
771                         startplane = thisbrush_start->planes + nplane2;
772                         endplane = thisbrush_end->planes + nplane2;
773                         if (developer.integer)
774                         {
775                                 // any brush with degenerate planes is not worth handling
776                                 if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
777                                 {
778                                         Con_Print("Collision_TraceBrushBrushFloat: degenerate thisbrush plane!\n");
779                                         return;
780                                 }
781                                 f = furthestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints);
782                                 if (fabs(f - startplane->dist) > 0.01f)
783                                         Con_Printf("startplane->dist %f != calculated %f (thisbrush_start)\n", startplane->dist, f);
784                         }
785                         d1 = nearestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints) - furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints) - collision_startnudge.value;
786                         d2 = nearestplanedist_float(endplane->normal, thisbrush_end->points, thisbrush_end->numpoints) - furthestplanedist_float(endplane->normal, thatbrush_end->points, thatbrush_end->numpoints) - collision_endnudge.value;
787                 }
788                 else
789                 {
790                         startplane = thatbrush_start->planes + nplane2;
791                         endplane = thatbrush_end->planes + nplane2;
792                         if (developer.integer)
793                         {
794                                 // any brush with degenerate planes is not worth handling
795                                 if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
796                                 {
797                                         Con_Print("Collision_TraceBrushBrushFloat: degenerate thatbrush plane!\n");
798                                         return;
799                                 }
800                                 f = furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints);
801                                 if (fabs(f - startplane->dist) > 0.01f)
802                                         Con_Printf("startplane->dist %f != calculated %f (thatbrush_start)\n", startplane->dist, f);
803                         }
804                         d1 = nearestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints) - startplane->dist - collision_startnudge.value;
805                         d2 = nearestplanedist_float(endplane->normal, thisbrush_end->points, thisbrush_end->numpoints) - endplane->dist - collision_endnudge.value;
806                 }
807                 //Con_Printf("%c%i: d1 = %f, d2 = %f, d1 / (d1 - d2) = %f\n", nplane2 != nplane ? 'b' : 'a', nplane2, d1, d2, d1 / (d1 - d2));
808
809                 move = d1 - d2;
810                 if (move > 0)
811                 {
812                         // moving into brush
813                         if (d2 > collision_enternudge.value)
814                                 return;
815                         if (d1 < 0)
816                                 continue;
817                         // enter
818                         fstartsolid = false;
819                         imove = 1 / move;
820                         f = (d1 - collision_enternudge.value) * imove;
821                         f = bound(0, f, 1);
822                         if (enterfrac < f)
823                         {
824                                 enterfrac = f;
825                                 enterfrac2 = f - collision_impactnudge.value * imove;
826                                 enterfrac2 = bound(0, enterfrac2, 1);
827                                 VectorLerp(startplane->normal, enterfrac, endplane->normal, newimpactnormal);
828                         }
829                 }
830                 else if (move < 0)
831                 {
832                         // moving out of brush
833                         if (d1 > collision_leavenudge.value)
834                                 return;
835                         if (d2 < 0)
836                                 continue;
837                         // leave
838                         fendsolid = false;
839                         f = (d1 + collision_leavenudge.value) / move;
840                         f = bound(0, f, 1);
841                         if (leavefrac > f)
842                                 leavefrac = f;
843                 }
844                 else
845                 {
846                         // sliding along plane
847                         if (d1 > 0)
848                                 return;
849                 }
850         }
851
852         brushsolid = trace->hitsupercontentsmask & thatbrush_start->supercontents;
853         if (fstartsolid)
854         {
855                 trace->startsupercontents |= thatbrush_start->supercontents;
856                 if (brushsolid)
857                 {
858                         trace->startsolid = true;
859                         if (fendsolid)
860                                 trace->allsolid = true;
861                 }
862         }
863
864         // LordHavoc: we need an epsilon nudge here because for a point trace the
865         // penetrating line segment is normally zero length if this brush was
866         // generated from a polygon (infinitely thin), and could even be slightly
867         // positive or negative due to rounding errors in that case.
868         if (brushsolid && enterfrac > -1 && enterfrac < trace->realfraction && enterfrac - (1.0f / 1024.0f) <= leavefrac)
869         {
870 #if 0
871                 // broken
872                 if (thatbrush_start->ispolygon)
873                 {
874                         d1 = nearestplanedist_float(thatbrush_start->planes[0].normal, thisbrush_start->points, thisbrush_start->numpoints) - thatbrush_start->planes[0].dist - collision_startnudge.value;
875                         d2 = nearestplanedist_float(thatbrush_end->planes[0].normal, thisbrush_end->points, thisbrush_end->numpoints) - thatbrush_end->planes[0].dist - collision_endnudge.value;
876                         move = d1 - d2;
877                         if (move <= 0 || d2 > collision_enternudge.value || d1 < 0)
878                                 return;
879                         // enter
880                         imove = 1 / move;
881                         enterfrac = (d1 - collision_enternudge.value) * imove;
882                         if (enterfrac < trace->realfraction)
883                         {
884                                 enterfrac2 = enterfrac - collision_impactnudge.value * imove;
885                                 trace->realfraction = bound(0, enterfrac, 1);
886                                 trace->fraction = bound(0, enterfrac2, 1);
887                                 VectorLerp(thatbrush_start->planes[0].normal, enterfrac, thatbrush_end->planes[0].normal, trace->plane.normal);
888                         }
889                 }
890                 else
891 #endif
892                 {
893                         trace->realfraction = bound(0, enterfrac, 1);
894                         trace->fraction = bound(0, enterfrac2, 1);
895                         VectorCopy(newimpactnormal, trace->plane.normal);
896                 }
897         }
898 }
899
900 // NOTE: start and end brush pair must have same numplanes/numpoints
901 void Collision_TraceLineBrushFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, const colbrushf_t *thatbrush_start, const colbrushf_t *thatbrush_end)
902 {
903         int nplane, fstartsolid, fendsolid, brushsolid;
904         float enterfrac, leavefrac, d1, d2, f, move, imove, newimpactnormal[3], enterfrac2;
905         const colplanef_t *startplane, *endplane;
906
907         enterfrac = -1;
908         enterfrac2 = -1;
909         leavefrac = 1;
910         fstartsolid = true;
911         fendsolid = true;
912
913         for (nplane = 0;nplane < thatbrush_start->numplanes;nplane++)
914         {
915                 startplane = thatbrush_start->planes + nplane;
916                 endplane = thatbrush_end->planes + nplane;
917                 d1 = DotProduct(startplane->normal, linestart) - startplane->dist - collision_startnudge.value;
918                 d2 = DotProduct(endplane->normal, lineend) - endplane->dist - collision_endnudge.value;
919                 if (developer.integer)
920                 {
921                         // any brush with degenerate planes is not worth handling
922                         if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
923                         {
924                                 Con_Print("Collision_TraceLineBrushFloat: degenerate plane!\n");
925                                 return;
926                         }
927                         if (thatbrush_start->numpoints)
928                         {
929                                 f = furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints);
930                                 if (fabs(f - startplane->dist) > 0.01f)
931                                         Con_Printf("startplane->dist %f != calculated %f\n", startplane->dist, f);
932                         }
933                 }
934
935                 move = d1 - d2;
936                 if (move > 0)
937                 {
938                         // moving into brush
939                         if (d2 >= 0)
940                                 return;
941                         if (d1 <= 0)
942                                 continue;
943                         // enter
944                         fstartsolid = false;
945                         imove = 1 / move;
946                         f = (d1 - collision_enternudge.value) * imove;
947                         if (enterfrac < f)
948                         {
949                                 enterfrac = f;
950                                 enterfrac2 = f - collision_impactnudge.value * imove;
951                                 VectorLerp(startplane->normal, enterfrac, endplane->normal, newimpactnormal);
952                         }
953                 }
954                 else
955                 {
956                         // moving out of brush
957                         if (d1 >= 0)
958                                 return;
959                         if (d2 <= 0)
960                                 continue;
961                         // leave
962                         fendsolid = false;
963                         f = (d1 - collision_leavenudge.value) / move;
964                         if (leavefrac > f)
965                                 leavefrac = f;
966                 }
967         }
968
969         brushsolid = trace->hitsupercontentsmask & thatbrush_start->supercontents;
970         if (fstartsolid)
971         {
972                 trace->startsupercontents |= thatbrush_start->supercontents;
973                 if (brushsolid)
974                 {
975                         trace->startsolid = true;
976                         if (fendsolid)
977                                 trace->allsolid = true;
978                 }
979         }
980
981         // LordHavoc: we need an epsilon nudge here because for a point trace the
982         // penetrating line segment is normally zero length if this brush was
983         // generated from a polygon (infinitely thin), and could even be slightly
984         // positive or negative due to rounding errors in that case.
985         if (brushsolid && enterfrac > -1 && enterfrac < trace->realfraction && enterfrac - (1.0f / 1024.0f) <= leavefrac)
986         {
987 #if 0
988                 // broken
989                 if (thatbrush_start->ispolygon)
990                 {
991                         d1 = DotProduct(thatbrush_start->planes[0].normal, linestart) - thatbrush_start->planes[0].dist - collision_startnudge.value;
992                         d2 = DotProduct(thatbrush_end->planes[0].normal, lineend) - thatbrush_end->planes[0].dist - collision_endnudge.value;
993                         move = d1 - d2;
994                         if (move <= 0 || d2 > collision_enternudge.value || d1 < 0)
995                                 return;
996                         // enter
997                         imove = 1 / move;
998                         enterfrac = (d1 - collision_enternudge.value) * imove;
999                         if (enterfrac < trace->realfraction)
1000                         {
1001                                 enterfrac2 = enterfrac - collision_impactnudge.value * imove;
1002                                 trace->realfraction = bound(0, enterfrac, 1);
1003                                 trace->fraction = bound(0, enterfrac2, 1);
1004                                 VectorLerp(thatbrush_start->planes[0].normal, enterfrac, thatbrush_end->planes[0].normal, trace->plane.normal);
1005                         }
1006                 }
1007                 else
1008 #endif
1009                 {
1010                         trace->realfraction = bound(0, enterfrac, 1);
1011                         trace->fraction = bound(0, enterfrac2, 1);
1012                         VectorCopy(newimpactnormal, trace->plane.normal);
1013                 }
1014         }
1015 }
1016
1017 void Collision_TracePointBrushFloat(trace_t *trace, const vec3_t point, const colbrushf_t *thatbrush)
1018 {
1019         int nplane;
1020         const colplanef_t *plane;
1021
1022         for (nplane = 0, plane = thatbrush->planes;nplane < thatbrush->numplanes;nplane++, plane++)
1023                 if (DotProduct(plane->normal, point) > plane->dist)
1024                         return;
1025
1026         trace->startsupercontents |= thatbrush->supercontents;
1027         if (trace->hitsupercontentsmask & thatbrush->supercontents)
1028         {
1029                 trace->startsolid = true;
1030                 trace->allsolid = true;
1031         }
1032 }
1033
1034 static colpointf_t polyf_points[256];
1035 static colplanef_t polyf_planes[256 + 2];
1036 static colbrushf_t polyf_brush;
1037
1038 void Collision_SnapCopyPoints(int numpoints, const colpointf_t *in, colpointf_t *out, float fractionprecision, float invfractionprecision)
1039 {
1040         while (numpoints--)
1041         {
1042                 out->v[0] = floor(in->v[0] * fractionprecision + 0.5f) * invfractionprecision;
1043                 out->v[1] = floor(in->v[1] * fractionprecision + 0.5f) * invfractionprecision;
1044                 out->v[2] = floor(in->v[2] * fractionprecision + 0.5f) * invfractionprecision;
1045         }
1046 }
1047
1048 void Collision_TraceBrushPolygonFloat(trace_t *trace, const colbrushf_t *thisbrush_start, const colbrushf_t *thisbrush_end, int numpoints, const float *points, int supercontents)
1049 {
1050         if (numpoints > 256)
1051         {
1052                 Con_Print("Polygon with more than 256 points not supported yet (fixme!)\n");
1053                 return;
1054         }
1055         polyf_brush.numpoints = numpoints;
1056         polyf_brush.numplanes = numpoints + 2;
1057         //polyf_brush.points = (colpointf_t *)points;
1058         polyf_brush.planes = polyf_planes;
1059         polyf_brush.supercontents = supercontents;
1060         polyf_brush.points = polyf_points;
1061         Collision_SnapCopyPoints(numpoints, (colpointf_t *)points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
1062         Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
1063         //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
1064         Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brush, &polyf_brush);
1065 }
1066
1067 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)
1068 {
1069         int i;
1070         float facemins[3], facemaxs[3];
1071         polyf_brush.numpoints = 3;
1072         polyf_brush.numplanes = 5;
1073         polyf_brush.points = polyf_points;
1074         polyf_brush.planes = polyf_planes;
1075         polyf_brush.supercontents = supercontents;
1076         for (i = 0;i < numtriangles;i++, element3i += 3)
1077         {
1078                 VectorCopy(vertex3f + element3i[0] * 3, polyf_points[0].v);
1079                 VectorCopy(vertex3f + element3i[1] * 3, polyf_points[1].v);
1080                 VectorCopy(vertex3f + element3i[2] * 3, polyf_points[2].v);
1081                 Collision_SnapCopyPoints(3, polyf_points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
1082                 facemins[0] = min(polyf_points[0].v[0], min(polyf_points[1].v[0], polyf_points[2].v[0])) - 1;
1083                 facemins[1] = min(polyf_points[0].v[1], min(polyf_points[1].v[1], polyf_points[2].v[1])) - 1;
1084                 facemins[2] = min(polyf_points[0].v[2], min(polyf_points[1].v[2], polyf_points[2].v[2])) - 1;
1085                 facemaxs[0] = max(polyf_points[0].v[0], max(polyf_points[1].v[0], polyf_points[2].v[0])) + 1;
1086                 facemaxs[1] = max(polyf_points[0].v[1], max(polyf_points[1].v[1], polyf_points[2].v[1])) + 1;
1087                 facemaxs[2] = max(polyf_points[0].v[2], max(polyf_points[1].v[2], polyf_points[2].v[2])) + 1;
1088                 if (BoxesOverlap(segmentmins, segmentmaxs, facemins, facemaxs))
1089                 {
1090                         Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
1091                         //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
1092                         Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brush, &polyf_brush);
1093                 }
1094         }
1095 }
1096
1097 void Collision_TraceLinePolygonFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, int numpoints, const float *points, int supercontents)
1098 {
1099         if (numpoints > 256)
1100         {
1101                 Con_Print("Polygon with more than 256 points not supported yet (fixme!)\n");
1102                 return;
1103         }
1104         polyf_brush.numpoints = numpoints;
1105         polyf_brush.numplanes = numpoints + 2;
1106         //polyf_brush.points = (colpointf_t *)points;
1107         polyf_brush.points = polyf_points;
1108         Collision_SnapCopyPoints(numpoints, (colpointf_t *)points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
1109         polyf_brush.planes = polyf_planes;
1110         polyf_brush.supercontents = supercontents;
1111         Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
1112         //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
1113         Collision_TraceLineBrushFloat(trace, linestart, lineend, &polyf_brush, &polyf_brush);
1114 }
1115
1116 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)
1117 {
1118         int i;
1119 #if 1
1120         // FIXME: snap vertices?
1121         for (i = 0;i < numtriangles;i++, element3i += 3)
1122                 Collision_TraceLineTriangleFloat(trace, linestart, lineend, vertex3f + element3i[0] * 3, vertex3f + element3i[1] * 3, vertex3f + element3i[2] * 3);
1123 #else
1124         polyf_brush.numpoints = 3;
1125         polyf_brush.numplanes = 5;
1126         polyf_brush.points = polyf_points;
1127         polyf_brush.planes = polyf_planes;
1128         polyf_brush.supercontents = supercontents;
1129         for (i = 0;i < numtriangles;i++, element3i += 3)
1130         {
1131                 float facemins[3], facemaxs[3];
1132                 VectorCopy(vertex3f + element3i[0] * 3, polyf_points[0].v);
1133                 VectorCopy(vertex3f + element3i[1] * 3, polyf_points[1].v);
1134                 VectorCopy(vertex3f + element3i[2] * 3, polyf_points[2].v);
1135                 Collision_SnapCopyPoints(numpoints, polyf_points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
1136                 facemins[0] = min(polyf_points[0].v[0], min(polyf_points[1].v[0], polyf_points[2].v[0])) - 1;
1137                 facemins[1] = min(polyf_points[0].v[1], min(polyf_points[1].v[1], polyf_points[2].v[1])) - 1;
1138                 facemins[2] = min(polyf_points[0].v[2], min(polyf_points[1].v[2], polyf_points[2].v[2])) - 1;
1139                 facemaxs[0] = max(polyf_points[0].v[0], max(polyf_points[1].v[0], polyf_points[2].v[0])) + 1;
1140                 facemaxs[1] = max(polyf_points[0].v[1], max(polyf_points[1].v[1], polyf_points[2].v[1])) + 1;
1141                 facemaxs[2] = max(polyf_points[0].v[2], max(polyf_points[1].v[2], polyf_points[2].v[2])) + 1;
1142                 if (BoxesOverlap(segmentmins, segmentmaxs, facemins, facemaxs))
1143                 {
1144                         Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
1145                         //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
1146                         Collision_TraceLineBrushFloat(trace, linestart, lineend, &polyf_brush, &polyf_brush);
1147                 }
1148         }
1149 #endif
1150 }
1151
1152
1153 static colpointf_t polyf_pointsstart[256], polyf_pointsend[256];
1154 static colplanef_t polyf_planesstart[256 + 2], polyf_planesend[256 + 2];
1155 static colbrushf_t polyf_brushstart, polyf_brushend;
1156
1157 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)
1158 {
1159         int i;
1160         if (numpoints > 256)
1161         {
1162                 Con_Print("Polygon with more than 256 points not supported yet (fixme!)\n");
1163                 return;
1164         }
1165         polyf_brushstart.numpoints = numpoints;
1166         polyf_brushstart.numplanes = numpoints + 2;
1167         polyf_brushstart.points = polyf_pointsstart;//(colpointf_t *)points;
1168         polyf_brushstart.planes = polyf_planesstart;
1169         polyf_brushstart.supercontents = supercontents;
1170         for (i = 0;i < numpoints;i++)
1171                 Matrix4x4_Transform(polygonmatrixstart, points + i * 3, polyf_brushstart.points[i].v);
1172         polyf_brushend.numpoints = numpoints;
1173         polyf_brushend.numplanes = numpoints + 2;
1174         polyf_brushend.points = polyf_pointsend;//(colpointf_t *)points;
1175         polyf_brushend.planes = polyf_planesend;
1176         polyf_brushend.supercontents = supercontents;
1177         for (i = 0;i < numpoints;i++)
1178                 Matrix4x4_Transform(polygonmatrixend, points + i * 3, polyf_brushend.points[i].v);
1179         Collision_SnapCopyPoints(numpoints, polyf_pointsstart, polyf_pointsstart, COLLISION_SNAPSCALE, COLLISION_SNAP);
1180         Collision_SnapCopyPoints(numpoints, polyf_pointsend, polyf_pointsend, COLLISION_SNAPSCALE, COLLISION_SNAP);
1181         Collision_CalcPlanesForPolygonBrushFloat(&polyf_brushstart);
1182         Collision_CalcPlanesForPolygonBrushFloat(&polyf_brushend);
1183
1184         //Collision_PrintBrushAsQHull(&polyf_brushstart, "polyf_brushstart");
1185         //Collision_PrintBrushAsQHull(&polyf_brushend, "polyf_brushend");
1186
1187         Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brushstart, &polyf_brushend);
1188 }
1189
1190
1191
1192 #define MAX_BRUSHFORBOX 16
1193 static int brushforbox_index = 0;
1194 static colpointf_t brushforbox_point[MAX_BRUSHFORBOX*8];
1195 static colplanef_t brushforbox_plane[MAX_BRUSHFORBOX*6];
1196 static colbrushf_t brushforbox_brush[MAX_BRUSHFORBOX];
1197 static colbrushf_t brushforpoint_brush[MAX_BRUSHFORBOX];
1198
1199 void Collision_InitBrushForBox(void)
1200 {
1201         int i;
1202         for (i = 0;i < MAX_BRUSHFORBOX;i++)
1203         {
1204                 brushforbox_brush[i].supercontents = SUPERCONTENTS_SOLID;
1205                 brushforbox_brush[i].numpoints = 8;
1206                 brushforbox_brush[i].numplanes = 6;
1207                 brushforbox_brush[i].points = brushforbox_point + i * 8;
1208                 brushforbox_brush[i].planes = brushforbox_plane + i * 6;
1209                 brushforpoint_brush[i].supercontents = SUPERCONTENTS_SOLID;
1210                 brushforpoint_brush[i].numpoints = 1;
1211                 brushforpoint_brush[i].numplanes = 0;
1212                 brushforpoint_brush[i].points = brushforbox_point + i * 8;
1213                 brushforpoint_brush[i].planes = brushforbox_plane + i * 6;
1214         }
1215 }
1216
1217 colbrushf_t *Collision_BrushForBox(const matrix4x4_t *matrix, const vec3_t mins, const vec3_t maxs)
1218 {
1219         int i, j;
1220         vec3_t v;
1221         colbrushf_t *brush;
1222         if (brushforbox_brush[0].numpoints == 0)
1223                 Collision_InitBrushForBox();
1224         if (VectorCompare(mins, maxs))
1225         {
1226                 // point brush
1227                 brush = brushforpoint_brush + ((brushforbox_index++) % MAX_BRUSHFORBOX);
1228                 VectorCopy(mins, brush->points->v);
1229         }
1230         else
1231         {
1232                 brush = brushforbox_brush + ((brushforbox_index++) % MAX_BRUSHFORBOX);
1233                 // FIXME: optimize
1234                 for (i = 0;i < 8;i++)
1235                 {
1236                         v[0] = i & 1 ? maxs[0] : mins[0];
1237                         v[1] = i & 2 ? maxs[1] : mins[1];
1238                         v[2] = i & 4 ? maxs[2] : mins[2];
1239                         Matrix4x4_Transform(matrix, v, brush->points[i].v);
1240                 }
1241                 // FIXME: optimize!
1242                 for (i = 0;i < 6;i++)
1243                 {
1244                         VectorClear(v);
1245                         v[i >> 1] = i & 1 ? 1 : -1;
1246                         Matrix4x4_Transform3x3(matrix, v, brush->planes[i].normal);
1247                         VectorNormalize(brush->planes[i].normal);
1248                 }
1249         }
1250         for (j = 0;j < brush->numplanes;j++)
1251                 brush->planes[j].dist = furthestplanedist_float(brush->planes[j].normal, brush->points, brush->numpoints);
1252         VectorCopy(brush->points[0].v, brush->mins);
1253         VectorCopy(brush->points[0].v, brush->maxs);
1254         for (j = 1;j < brush->numpoints;j++)
1255         {
1256                 brush->mins[0] = min(brush->mins[0], brush->points[j].v[0]);
1257                 brush->mins[1] = min(brush->mins[1], brush->points[j].v[1]);
1258                 brush->mins[2] = min(brush->mins[2], brush->points[j].v[2]);
1259                 brush->maxs[0] = max(brush->maxs[0], brush->points[j].v[0]);
1260                 brush->maxs[1] = max(brush->maxs[1], brush->points[j].v[1]);
1261                 brush->maxs[2] = max(brush->maxs[2], brush->points[j].v[2]);
1262         }
1263         brush->mins[0] -= 1;
1264         brush->mins[1] -= 1;
1265         brush->mins[2] -= 1;
1266         brush->maxs[0] += 1;
1267         brush->maxs[1] += 1;
1268         brush->maxs[2] += 1;
1269         Collision_ValidateBrush(brush);
1270         return brush;
1271 }
1272
1273 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)
1274 {
1275         colbrushf_t *boxbrush, *thisbrush_start, *thisbrush_end;
1276         matrix4x4_t identitymatrix;
1277         vec3_t startmins, startmaxs, endmins, endmaxs;
1278
1279         // create brushes for the collision
1280         VectorAdd(start, mins, startmins);
1281         VectorAdd(start, maxs, startmaxs);
1282         VectorAdd(end, mins, endmins);
1283         VectorAdd(end, maxs, endmaxs);
1284         Matrix4x4_CreateIdentity(&identitymatrix);
1285         boxbrush = Collision_BrushForBox(&identitymatrix, cmins, cmaxs);
1286         thisbrush_start = Collision_BrushForBox(&identitymatrix, startmins, startmaxs);
1287         thisbrush_end = Collision_BrushForBox(&identitymatrix, endmins, endmaxs);
1288
1289         memset(trace, 0, sizeof(trace_t));
1290         trace->hitsupercontentsmask = hitsupercontentsmask;
1291         trace->fraction = 1;
1292         trace->realfraction = 1;
1293         trace->allsolid = true;
1294         Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, boxbrush, boxbrush);
1295 }
1296
1297 // LordHavoc: currently unused and not yet tested
1298 // note: this can be used for tracing a moving sphere vs a stationary sphere,
1299 // by simply adding the moving sphere's radius to the sphereradius parameter,
1300 // all the results are correct (impactpoint, impactnormal, and fraction)
1301 float Collision_ClipTrace_Line_Sphere(double *linestart, double *lineend, double *sphereorigin, double sphereradius, double *impactpoint, double *impactnormal)
1302 {
1303         double dir[3], scale, v[3], deviationdist, impactdist, linelength;
1304         // make sure the impactpoint and impactnormal are valid even if there is
1305         // no collision
1306         impactpoint[0] = lineend[0];
1307         impactpoint[1] = lineend[1];
1308         impactpoint[2] = lineend[2];
1309         impactnormal[0] = 0;
1310         impactnormal[1] = 0;
1311         impactnormal[2] = 0;
1312         // calculate line direction
1313         dir[0] = lineend[0] - linestart[0];
1314         dir[1] = lineend[1] - linestart[1];
1315         dir[2] = lineend[2] - linestart[2];
1316         // normalize direction
1317         linelength = sqrt(dir[0] * dir[0] + dir[1] * dir[1] + dir[2] * dir[2]);
1318         if (linelength)
1319         {
1320                 scale = 1.0 / linelength;
1321                 dir[0] *= scale;
1322                 dir[1] *= scale;
1323                 dir[2] *= scale;
1324         }
1325         // this dotproduct calculates the distance along the line at which the
1326         // sphere origin is (nearest point to the sphere origin on the line)
1327         impactdist = dir[0] * (sphereorigin[0] - linestart[0]) + dir[1] * (sphereorigin[1] - linestart[1]) + dir[2] * (sphereorigin[2] - linestart[2]);
1328         // calculate point on line at that distance, and subtract the
1329         // sphereorigin from it, so we have a vector to measure for the distance
1330         // of the line from the sphereorigin (deviation, how off-center it is)
1331         v[0] = linestart[0] + impactdist * dir[0] - sphereorigin[0];
1332         v[1] = linestart[1] + impactdist * dir[1] - sphereorigin[1];
1333         v[2] = linestart[2] + impactdist * dir[2] - sphereorigin[2];
1334         deviationdist = v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
1335         // if outside the radius, it's a miss for sure
1336         // (we do this comparison using squared radius to avoid a sqrt)
1337         if (deviationdist > sphereradius*sphereradius)
1338                 return 1; // miss (off to the side)
1339         // nudge back to find the correct impact distance
1340         impactdist += (sqrt(deviationdist) - sphereradius);
1341         if (impactdist >= linelength)
1342                 return 1; // miss (not close enough)
1343         if (impactdist < 0)
1344                 return 1; // miss (linestart is past or inside sphere)
1345         // calculate new impactpoint
1346         impactpoint[0] = linestart[0] + impactdist * dir[0];
1347         impactpoint[1] = linestart[1] + impactdist * dir[1];
1348         impactpoint[2] = linestart[2] + impactdist * dir[2];
1349         // calculate impactnormal (surface normal at point of impact)
1350         impactnormal[0] = impactpoint[0] - sphereorigin[0];
1351         impactnormal[1] = impactpoint[1] - sphereorigin[1];
1352         impactnormal[2] = impactpoint[2] - sphereorigin[2];
1353         // normalize impactnormal
1354         scale = impactnormal[0] * impactnormal[0] + impactnormal[1] * impactnormal[1] + impactnormal[2] * impactnormal[2];
1355         if (scale)
1356         {
1357                 scale = 1.0 / sqrt(scale);
1358                 impactnormal[0] *= scale;
1359                 impactnormal[1] *= scale;
1360                 impactnormal[2] *= scale;
1361         }
1362         // return fraction of movement distance
1363         return impactdist / linelength;
1364 }
1365
1366 void Collision_TraceLineTriangleFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, const float *point0, const float *point1, const float *point2)
1367 {
1368         float d1, d2, d, f, fnudged, impact[3], edgenormal[3], faceplanenormal[3], faceplanedist, edge[3];
1369
1370         // this code is designed for clockwise triangles, conversion to
1371         // counterclockwise would require swapping some things around...
1372         // it is easier to simply swap the point0 and point2 parameters to this
1373         // function when calling it than it is to rewire the internals.
1374
1375         // calculate the faceplanenormal of the triangle, this represents the front side
1376         TriangleNormal(point0, point1, point2, faceplanenormal);
1377         // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
1378         if (DotProduct(faceplanenormal, faceplanenormal) < 0.0001f)
1379                 return;
1380         // normalize the normal
1381         VectorNormalize(faceplanenormal);
1382         // calculate the distance
1383         faceplanedist = DotProduct(point0, faceplanenormal);
1384
1385         // calculate the start distance
1386         d1 = DotProduct(faceplanenormal, linestart) - faceplanedist;
1387         // if start point is on the back side there is no collision
1388         // (we don't care about traces going through the triangle the wrong way)
1389         if (d1 < 0)
1390                 return;
1391
1392         // calculate the end distance
1393         d2 = DotProduct(faceplanenormal, lineend) - faceplanedist;
1394         // if both are in front, there is no collision
1395         if (d2 >= 0)
1396                 return;
1397
1398         // from here on we know d1 is >= 0 and d2 is < 0
1399         // this means the line starts infront and ends behind, passing through it
1400
1401         // calculate the recipricol of the distance delta,
1402         // so we can use it multiple times cheaply (instead of division)
1403         d = 1.0f / (d1 - d2);
1404         // calculate the impact fraction by taking the start distance (> 0)
1405         // and subtracting the face plane distance (this is the distance of the
1406         // triangle along that same normal)
1407         // then multiply by the recipricol distance delta
1408         f = d1 * d;
1409         // skip out if this impact is further away than previous ones
1410         if (f > trace->realfraction)
1411                 return;
1412         // calculate the perfect impact point for classification of insidedness
1413         impact[0] = linestart[0] + f * (lineend[0] - linestart[0]);
1414         impact[1] = linestart[1] + f * (lineend[1] - linestart[1]);
1415         impact[2] = linestart[2] + f * (lineend[2] - linestart[2]);
1416
1417         // calculate the edge normal and reject if impact is outside triangle
1418         // (an edge normal faces away from the triangle, to get the desired normal
1419         //  a crossproduct with the faceplanenormal is used, and because of the way
1420         // the insidedness comparison is written it does not need to be normalized)
1421         
1422         VectorSubtract(point2, point0, edge);
1423         CrossProduct(edge, faceplanenormal, edgenormal);
1424         if (DotProduct(impact, edgenormal) > DotProduct(point0, edgenormal))
1425                 return;
1426
1427         VectorSubtract(point0, point1, edge);
1428         CrossProduct(edge, faceplanenormal, edgenormal);
1429         if (DotProduct(impact, edgenormal) > DotProduct(point1, edgenormal))
1430                 return;
1431
1432         VectorSubtract(point1, point2, edge);
1433         CrossProduct(edge, faceplanenormal, edgenormal);
1434         if (DotProduct(impact, edgenormal) > DotProduct(point2, edgenormal))
1435                 return;
1436
1437         // store the new trace fraction
1438         trace->realfraction = bound(0, f, 1);
1439
1440         // calculate a nudged fraction to keep it out of the surface
1441         // (the main fraction remains perfect)
1442         fnudged = (d1 - collision_impactnudge.value) * d;
1443         trace->fraction = bound(0, fnudged, 1);
1444
1445         // store the new trace endpos
1446         // not needed, it's calculated later when the trace is finished
1447         //trace->endpos[0] = linestart[0] + fnudged * (lineend[0] - linestart[0]);
1448         //trace->endpos[1] = linestart[1] + fnudged * (lineend[1] - linestart[1]);
1449         //trace->endpos[2] = linestart[2] + fnudged * (lineend[2] - linestart[2]);
1450
1451         // store the new trace plane (because collisions only happen from
1452         // the front this is always simply the triangle normal, never flipped)
1453         VectorCopy(faceplanenormal, trace->plane.normal);
1454         trace->plane.dist = faceplanedist;
1455 }
1456
1457 typedef struct colbspnode_s
1458 {
1459         mplane_t plane;
1460         struct colbspnode_s *children[2];
1461         // the node is reallocated or split if max is reached
1462         int numcolbrushf;
1463         int maxcolbrushf;
1464         colbrushf_t **colbrushflist;
1465         //int numcolbrushd;
1466         //int maxcolbrushd;
1467         //colbrushd_t **colbrushdlist;
1468 }
1469 colbspnode_t;
1470
1471 typedef struct colbsp_s
1472 {
1473         mempool_t *mempool;
1474         colbspnode_t *nodes;
1475 }
1476 colbsp_t;
1477
1478 colbsp_t *Collision_CreateCollisionBSP(mempool_t *mempool)
1479 {
1480         colbsp_t *bsp;
1481         bsp = Mem_Alloc(mempool, sizeof(colbsp_t));
1482         bsp->mempool = mempool;
1483         bsp->nodes = Mem_Alloc(bsp->mempool, sizeof(colbspnode_t));
1484         return bsp;
1485 }
1486
1487 void Collision_FreeCollisionBSPNode(colbspnode_t *node)
1488 {
1489         if (node->children[0])
1490                 Collision_FreeCollisionBSPNode(node->children[0]);
1491         if (node->children[1])
1492                 Collision_FreeCollisionBSPNode(node->children[1]);
1493         while (--node->numcolbrushf)
1494                 Mem_Free(node->colbrushflist[node->numcolbrushf]);
1495         //while (--node->numcolbrushd)
1496         //      Mem_Free(node->colbrushdlist[node->numcolbrushd]);
1497         Mem_Free(node);
1498 }
1499
1500 void Collision_FreeCollisionBSP(colbsp_t *bsp)
1501 {
1502         Collision_FreeCollisionBSPNode(bsp->nodes);
1503         Mem_Free(bsp);
1504 }
1505
1506 void Collision_BoundingBoxOfBrushTraceSegment(const colbrushf_t *start, const colbrushf_t *end, vec3_t mins, vec3_t maxs, float startfrac, float endfrac)
1507 {
1508         int i;
1509         colpointf_t *ps, *pe;
1510         float tempstart[3], tempend[3];
1511         VectorLerp(start->points[0].v, startfrac, end->points[0].v, mins);
1512         VectorCopy(mins, maxs);
1513         for (i = 0, ps = start->points, pe = end->points;i < start->numpoints;i++, ps++, pe++)
1514         {
1515                 VectorLerp(ps->v, startfrac, pe->v, tempstart);
1516                 VectorLerp(ps->v, endfrac, pe->v, tempend);
1517                 mins[0] = min(mins[0], min(tempstart[0], tempend[0]));
1518                 mins[1] = min(mins[1], min(tempstart[1], tempend[1]));
1519                 mins[2] = min(mins[2], min(tempstart[2], tempend[2]));
1520                 maxs[0] = min(maxs[0], min(tempstart[0], tempend[0]));
1521                 maxs[1] = min(maxs[1], min(tempstart[1], tempend[1]));
1522                 maxs[2] = min(maxs[2], min(tempstart[2], tempend[2]));
1523         }
1524         mins[0] -= 1;
1525         mins[1] -= 1;
1526         mins[2] -= 1;
1527         maxs[0] += 1;
1528         maxs[1] += 1;
1529         maxs[2] += 1;
1530 }
1531