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