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