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