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