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