]> icculus.org git repositories - divverent/darkplaces.git/blob - collision.c
engine now defaults to 44100hz sound
[divverent/darkplaces.git] / collision.c
1
2 #include "quakedef.h"
3 #include "winding.h"
4
5 typedef struct
6 {
7         // the hull we're tracing through
8         const hull_t *hull;
9
10         // the trace structure to fill in
11         trace_t *trace;
12
13         // start and end of the trace (in model space)
14         double start[3];
15         double end[3];
16
17         // end - start
18         double dist[3];
19
20         // overrides the CONTENTS_SOLID in the box bsp tree
21         int boxsupercontents;
22 }
23 RecursiveHullCheckTraceInfo_t;
24
25 // 1/32 epsilon to keep floating point happy
26 #define DIST_EPSILON (0.03125)
27
28 #define HULLCHECKSTATE_EMPTY 0
29 #define HULLCHECKSTATE_SOLID 1
30 #define HULLCHECKSTATE_DONE 2
31
32 static int RecursiveHullCheck(RecursiveHullCheckTraceInfo_t *t, int num, double p1f, double p2f, double p1[3], double p2[3])
33 {
34         // status variables, these don't need to be saved on the stack when
35         // recursing...  but are because this should be thread-safe
36         // (note: tracing against a bbox is not thread-safe, yet)
37         int ret;
38         mplane_t *plane;
39         double t1, t2;
40
41         // variables that need to be stored on the stack when recursing
42         dclipnode_t *node;
43         int side;
44         double midf, mid[3];
45
46         // LordHavoc: a goto!  everyone flee in terror... :)
47 loc0:
48         // check for empty
49         if (num < 0)
50         {
51                 // translate the fake CONTENTS values in the box bsp tree
52                 if (num == CONTENTS_SOLID)
53                         num = t->boxsupercontents;
54                 else
55                         num = 0;
56                 if (!t->trace->startfound)
57                 {
58                         t->trace->startfound = true;
59                         t->trace->startsupercontents |= num;
60                 }
61                 if (num & t->trace->hitsupercontentsmask)
62                 {
63                         // if the first leaf is solid, set startsolid
64                         if (t->trace->allsolid)
65                                 t->trace->startsolid = true;
66                         return HULLCHECKSTATE_SOLID;
67                 }
68                 else
69                 {
70                         t->trace->allsolid = false;
71                         return HULLCHECKSTATE_EMPTY;
72                 }
73         }
74
75         // find the point distances
76         node = t->hull->clipnodes + num;
77
78         plane = t->hull->planes + node->planenum;
79         if (plane->type < 3)
80         {
81                 t1 = p1[plane->type] - plane->dist;
82                 t2 = p2[plane->type] - plane->dist;
83         }
84         else
85         {
86                 t1 = DotProduct (plane->normal, p1) - plane->dist;
87                 t2 = DotProduct (plane->normal, p2) - plane->dist;
88         }
89
90         if (t1 < 0)
91         {
92                 if (t2 < 0)
93                 {
94                         num = node->children[1];
95                         goto loc0;
96                 }
97                 side = 1;
98         }
99         else
100         {
101                 if (t2 >= 0)
102                 {
103                         num = node->children[0];
104                         goto loc0;
105                 }
106                 side = 0;
107         }
108
109         // the line intersects, find intersection point
110         // LordHavoc: this uses the original trace for maximum accuracy
111         if (plane->type < 3)
112         {
113                 t1 = t->start[plane->type] - plane->dist;
114                 t2 = t->end[plane->type] - plane->dist;
115         }
116         else
117         {
118                 t1 = DotProduct (plane->normal, t->start) - plane->dist;
119                 t2 = DotProduct (plane->normal, t->end) - plane->dist;
120         }
121
122         midf = t1 / (t1 - t2);
123         midf = bound(p1f, midf, p2f);
124         VectorMA(t->start, midf, t->dist, mid);
125
126         // recurse both sides, front side first
127         ret = RecursiveHullCheck (t, node->children[side], p1f, midf, p1, mid);
128         // if this side is not empty, return what it is (solid or done)
129         if (ret != HULLCHECKSTATE_EMPTY)
130                 return ret;
131
132         ret = RecursiveHullCheck (t, node->children[side ^ 1], midf, p2f, mid, p2);
133         // if other side is not solid, return what it is (empty or done)
134         if (ret != HULLCHECKSTATE_SOLID)
135                 return ret;
136
137         // front is air and back is solid, this is the impact point...
138         if (side)
139         {
140                 t->trace->plane.dist = -plane->dist;
141                 VectorNegate (plane->normal, t->trace->plane.normal);
142         }
143         else
144         {
145                 t->trace->plane.dist = plane->dist;
146                 VectorCopy (plane->normal, t->trace->plane.normal);
147         }
148
149         // bias away from surface a bit
150         t1 = DotProduct(t->trace->plane.normal, t->start) - (t->trace->plane.dist + DIST_EPSILON);
151         t2 = DotProduct(t->trace->plane.normal, t->end) - (t->trace->plane.dist + DIST_EPSILON);
152
153         midf = t1 / (t1 - t2);
154         t->trace->fraction = bound(0.0f, midf, 1.0);
155
156         VectorMA(t->start, t->trace->fraction, t->dist, t->trace->endpos);
157
158         return HULLCHECKSTATE_DONE;
159 }
160
161 #if 0
162 // used if start and end are the same
163 static void RecursiveHullCheckPoint (RecursiveHullCheckTraceInfo_t *t, int num)
164 {
165         // If you can read this, you understand BSP trees
166         while (num >= 0)
167                 num = t->hull->clipnodes[num].children[((t->hull->planes[t->hull->clipnodes[num].planenum].type < 3) ? (t->start[t->hull->planes[t->hull->clipnodes[num].planenum].type]) : (DotProduct(t->hull->planes[t->hull->clipnodes[num].planenum].normal, t->start))) < t->hull->planes[t->hull->clipnodes[num].planenum].dist];
168
169         // check for empty
170         t->trace->endcontents = num;
171         if (t->trace->thiscontents)
172         {
173                 if (num == t->trace->thiscontents)
174                         t->trace->allsolid = false;
175                 else
176                 {
177                         // if the first leaf is solid, set startsolid
178                         if (t->trace->allsolid)
179                                 t->trace->startsolid = true;
180                 }
181         }
182         else
183         {
184                 if (num != CONTENTS_SOLID)
185                 {
186                         t->trace->allsolid = false;
187                         if (num == CONTENTS_EMPTY)
188                                 t->trace->inopen = true;
189                         else
190                                 t->trace->inwater = true;
191                 }
192                 else
193                 {
194                         // if the first leaf is solid, set startsolid
195                         if (t->trace->allsolid)
196                                 t->trace->startsolid = true;
197                 }
198         }
199 }
200 #endif
201
202 static hull_t box_hull;
203 static dclipnode_t box_clipnodes[6];
204 static mplane_t box_planes[6];
205
206 void Collision_Init (void)
207 {
208         int             i;
209         int             side;
210
211         //Set up the planes and clipnodes so that the six floats of a bounding box
212         //can just be stored out and get a proper hull_t structure.
213
214         box_hull.clipnodes = box_clipnodes;
215         box_hull.planes = box_planes;
216         box_hull.firstclipnode = 0;
217         box_hull.lastclipnode = 5;
218
219         for (i = 0;i < 6;i++)
220         {
221                 box_clipnodes[i].planenum = i;
222
223                 side = i&1;
224
225                 box_clipnodes[i].children[side] = CONTENTS_EMPTY;
226                 if (i != 5)
227                         box_clipnodes[i].children[side^1] = i + 1;
228                 else
229                         box_clipnodes[i].children[side^1] = CONTENTS_SOLID;
230
231                 box_planes[i].type = i>>1;
232                 box_planes[i].normal[i>>1] = 1;
233         }
234 }
235
236 void Collision_ClipTrace_Box(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 boxsupercontents)
237 {
238         RecursiveHullCheckTraceInfo_t rhc;
239         // fill in a default trace
240         memset(&rhc, 0, sizeof(rhc));
241         memset(trace, 0, sizeof(trace_t));
242         //To keep everything totally uniform, bounding boxes are turned into small
243         //BSP trees instead of being compared directly.
244         // create a temp hull from bounding box sizes
245         box_planes[0].dist = cmaxs[0] - mins[0];
246         box_planes[1].dist = cmins[0] - maxs[0];
247         box_planes[2].dist = cmaxs[1] - mins[1];
248         box_planes[3].dist = cmins[1] - maxs[1];
249         box_planes[4].dist = cmaxs[2] - mins[2];
250         box_planes[5].dist = cmins[2] - maxs[2];
251         // trace a line through the generated clipping hull
252         rhc.boxsupercontents = boxsupercontents;
253         rhc.hull = &box_hull;
254         rhc.trace = trace;
255         rhc.trace->hitsupercontentsmask = hitsupercontentsmask;
256         rhc.trace->fraction = 1;
257         rhc.trace->allsolid = true;
258         VectorCopy(start, rhc.start);
259         VectorCopy(end, rhc.end);
260         VectorSubtract(rhc.end, rhc.start, rhc.dist);
261         RecursiveHullCheck(&rhc, rhc.hull->firstclipnode, 0, 1, rhc.start, rhc.end);
262 }
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278 void Collision_PrintBrushAsQHull(colbrushf_t *brush, const char *name)
279 {
280         int i;
281         Con_Printf("3 %s\n%i\n", name, brush->numpoints);
282         for (i = 0;i < brush->numpoints;i++)
283                 Con_Printf("%f %f %f\n", brush->points[i].v[0], brush->points[i].v[1], brush->points[i].v[2]);
284         // FIXME: optimize!
285         Con_Printf("4\n%i\n", brush->numplanes);
286         for (i = 0;i < brush->numplanes;i++)
287                 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);
288 }
289
290 void Collision_ValidateBrush(colbrushf_t *brush)
291 {
292         int j, k, pointsoffplanes, printbrush;
293         float d;
294         printbrush = false;
295         if (!brush->numpoints)
296         {
297                 Con_Printf("Collision_ValidateBrush: brush with no points!\n");
298                 printbrush = true;
299         }
300 #if 0
301         // it's ok for a brush to have one point and no planes...
302         if (brush->numplanes == 0 && brush->numpoints != 1)
303         {
304                 Con_Printf("Collision_ValidateBrush: brush with no planes and more than one point!\n");
305                 printbrush = true;
306         }
307 #endif
308         if (brush->numplanes)
309         {
310                 pointsoffplanes = 0;
311                 for (k = 0;k < brush->numplanes;k++)
312                 {
313                         if (DotProduct(brush->planes[k].normal, brush->planes[k].normal) < 0.0001f)
314                                 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);
315                         for (j = 0;j < brush->numpoints;j++)
316                         {
317                                 d = DotProduct(brush->points[j].v, brush->planes[k].normal) - brush->planes[k].dist;
318                                 if (d > (1.0f / 8.0f))
319                                 {
320                                         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);
321                                         printbrush = true;
322                                 }
323                                 if (fabs(d) > 0.01f)
324                                         pointsoffplanes++;
325                         }
326                 }
327                 if (pointsoffplanes == 0) // all points are on all planes
328                 {
329                         Con_Printf("Collision_ValidateBrush: all points lie on all planes (degenerate, no brush volume!)\n");
330                         printbrush = true;
331                 }
332         }
333         if (printbrush)
334                 Collision_PrintBrushAsQHull(brush, "unnamed");
335 }
336
337 float nearestplanedist_float(const float *normal, const colpointf_t *points, int numpoints)
338 {
339         float dist, bestdist;
340         bestdist = DotProduct(points->v, normal);
341         points++;
342         while(--numpoints)
343         {
344                 dist = DotProduct(points->v, normal);
345                 bestdist = min(bestdist, dist);
346                 points++;
347         }
348         return bestdist;
349 }
350
351 float furthestplanedist_float(const float *normal, const colpointf_t *points, int numpoints)
352 {
353         float dist, bestdist;
354         bestdist = DotProduct(points->v, normal);
355         points++;
356         while(--numpoints)
357         {
358                 dist = DotProduct(points->v, normal);
359                 bestdist = max(bestdist, dist);
360                 points++;
361         }
362         return bestdist;
363 }
364
365
366 colbrushf_t *Collision_NewBrushFromPlanes(mempool_t *mempool, int numoriginalplanes, const mplane_t *originalplanes, int supercontents)
367 {
368         int j, k, m;
369         int numpoints, maxpoints, numplanes, maxplanes, numelements, maxelements, numtriangles, numpolypoints, maxpolypoints;
370         winding_t *w;
371         colbrushf_t *brush;
372         colpointf_t pointsbuf[256];
373         colplanef_t planesbuf[256];
374         int elementsbuf[1024];
375         int polypointbuf[256];
376         float mins[3], maxs[3];
377         // construct a collision brush (points, planes, and renderable mesh) from
378         // a set of planes, this also optimizes out any unnecessary planes (ones
379         // whose polygon is clipped away by the other planes)
380         numpoints = 0;maxpoints = 256;
381         numplanes = 0;maxplanes = 256;
382         numelements = 0;maxelements = 1024;
383         numtriangles = 0;
384         maxpolypoints = 256;
385         for (j = 0;j < numoriginalplanes;j++)
386         {
387                 // add the plane uniquely (no duplicates)
388                 for (k = 0;k < numplanes;k++)
389                         if (VectorCompare(planesbuf[k].normal, originalplanes[j].normal) && planesbuf[k].dist == originalplanes[j].dist)
390                                 break;
391                 // if the plane is a duplicate, skip it
392                 if (k < numplanes)
393                         continue;
394                 // check if there are too many and skip the brush
395                 if (numplanes >= 256)
396                 {
397                         Con_Printf("Mod_Q3BSP_LoadBrushes: failed to build collision brush: too many planes for buffer\n");
398                         return NULL;
399                 }
400
401                 // create a large polygon from the plane
402                 w = Winding_NewFromPlane(originalplanes[j].normal[0], originalplanes[j].normal[1], originalplanes[j].normal[2], originalplanes[j].dist);
403                 // clip it by all other planes
404                 for (k = 0;k < numoriginalplanes && w;k++)
405                 {
406                         if (k != j)
407                         {
408                                 // we want to keep the inside of the brush plane so we flip
409                                 // the cutting plane
410                                 w = Winding_Clip(w, -originalplanes[k].normal[0], -originalplanes[k].normal[1], -originalplanes[k].normal[2], -originalplanes[k].dist, true);
411                         }
412                 }
413                 // if nothing is left, skip it
414                 if (!w)
415                         continue;
416
417                 // copy off the number of points for later when the winding is freed
418                 numpolypoints = w->numpoints;
419
420                 // check if there are too many polygon vertices for buffer
421                 if (numpolypoints > maxpolypoints)
422                 {
423                         Con_Printf("Collision_NewBrushFromPlanes: failed to build collision brush: too many points for buffer\n");
424                         return NULL;
425                 }
426
427                 // check if there are too many triangle elements for buffer
428                 if (numelements + (w->numpoints - 2) * 3 > maxelements)
429                 {
430                         Con_Printf("Collision_NewBrushFromPlanes: failed to build collision brush: too many triangle elements for buffer\n");
431                         return NULL;
432                 }
433
434                 for (k = 0;k < w->numpoints;k++)
435                 {
436                         // check if there is already a matching point (no duplicates)
437                         for (m = 0;m < numpoints;m++)
438                                 if (VectorDistance2(w->points[k], pointsbuf[m].v) < DIST_EPSILON)
439                                         break;
440
441                         // if there is no match, add a new one
442                         if (m == numpoints)
443                         {
444                                 // check if there are too many and skip the brush
445                                 if (numpoints >= 256)
446                                 {
447                                         Con_Printf("Collision_NewBrushFromPlanes: failed to build collision brush: too many points for buffer\n");
448                                         Winding_Free(w);
449                                         return NULL;
450                                 }
451                                 // add the new one
452                                 VectorCopy(w->points[k], pointsbuf[numpoints].v);
453                                 numpoints++;
454                         }
455
456                         // store the index into a buffer
457                         polypointbuf[k] = m;
458                 }
459                 Winding_Free(w);
460                 w = NULL;
461
462                 // add the triangles for the polygon
463                 // (this particular code makes a triangle fan)
464                 for (k = 0;k < numpolypoints - 2;k++)
465                 {
466                         numtriangles++;
467                         elementsbuf[numelements++] = polypointbuf[0];
468                         elementsbuf[numelements++] = polypointbuf[k + 1];
469                         elementsbuf[numelements++] = polypointbuf[k + 2];
470                 }
471
472                 // add the new plane
473                 VectorCopy(originalplanes[j].normal, planesbuf[numplanes].normal);
474                 planesbuf[numplanes].dist = originalplanes[j].dist;
475                 numplanes++;
476         }
477
478         // recalc distances
479         for (j = 0;j < numplanes;j++)
480                 planesbuf[j].dist = furthestplanedist_float(planesbuf[j].normal, pointsbuf, numpoints);
481
482         if (numpoints)
483         {
484                 VectorCopy(pointsbuf[0].v, mins);
485                 VectorCopy(pointsbuf[0].v, maxs);
486                 for (j = 1;j < numpoints;j++)
487                 {
488                         mins[0] = min(mins[0], pointsbuf[j].v[0]);
489                         mins[1] = min(mins[1], pointsbuf[j].v[1]);
490                         mins[2] = min(mins[2], pointsbuf[j].v[2]);
491                         maxs[0] = max(maxs[0], pointsbuf[j].v[0]);
492                         maxs[1] = max(maxs[1], pointsbuf[j].v[1]);
493                         maxs[2] = max(maxs[2], pointsbuf[j].v[2]);
494                 }
495         }
496
497         // if nothing is left, there's nothing to allocate
498         if (numtriangles < 4 || numplanes < 4 || numpoints < 4)
499                 return NULL;
500
501         // allocate the brush and copy to it
502         brush = Collision_AllocBrushFloat(mempool, numpoints, numplanes, numtriangles, supercontents);
503         memcpy(brush->points, pointsbuf, numpoints * sizeof(colpointf_t));
504         memcpy(brush->planes, planesbuf, numplanes * sizeof(colplanef_t));
505         memcpy(brush->elements, elementsbuf, numtriangles * sizeof(int[3]));
506         VectorCopy(mins, brush->mins);
507         VectorCopy(maxs, brush->maxs);
508         Collision_ValidateBrush(brush);
509         return brush;
510 }
511
512
513
514 colbrushf_t *Collision_AllocBrushFloat(mempool_t *mempool, int numpoints, int numplanes, int numtriangles, int supercontents)
515 {
516         colbrushf_t *brush;
517         brush = Mem_Alloc(mempool, sizeof(colbrushf_t) + sizeof(colpointf_t) * numpoints + sizeof(colplanef_t) * numplanes + sizeof(int[3]) * numtriangles);
518         brush->supercontents = supercontents;
519         brush->numplanes = numplanes;
520         brush->numpoints = numpoints;
521         brush->numtriangles = numtriangles;
522         brush->planes = (void *)(brush + 1);
523         brush->points = (void *)(brush->planes + brush->numplanes);
524         brush->elements = (void *)(brush->points + brush->numpoints);
525         return brush;
526 }
527
528 void Collision_CalcPlanesForPolygonBrushFloat(colbrushf_t *brush)
529 {
530         int i;
531         float edge0[3], edge1[3], edge2[3], normal[3], dist, bestdist, temp[3];
532         colpointf_t *p, *p2;
533
534         if (brush->numpoints == 3)
535         {
536                 // optimized triangle case
537                 TriangleNormal(brush->points[0].v, brush->points[1].v, brush->points[2].v, brush->planes[0].normal);
538                 if (DotProduct(brush->planes[0].normal, brush->planes[0].normal) < 0.0001f)
539                 {
540                         // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
541                         brush->numplanes = 0;
542                         return;
543                 }
544                 else
545                 {
546                         brush->numplanes = 5;
547                         VectorNormalize(brush->planes[0].normal);
548                         brush->planes[0].dist = DotProduct(brush->points->v, brush->planes[0].normal);
549                         VectorNegate(brush->planes[0].normal, brush->planes[1].normal);
550                         brush->planes[1].dist = -brush->planes[0].dist;
551                         VectorSubtract(brush->points[2].v, brush->points[0].v, edge0);
552                         VectorSubtract(brush->points[0].v, brush->points[1].v, edge1);
553                         VectorSubtract(brush->points[1].v, brush->points[2].v, edge2);
554                         CrossProduct(edge0, brush->planes->normal, brush->planes[2].normal);
555                         CrossProduct(edge1, brush->planes->normal, brush->planes[3].normal);
556                         CrossProduct(edge2, brush->planes->normal, brush->planes[4].normal);
557                         VectorNormalize(brush->planes[2].normal);
558                         VectorNormalize(brush->planes[3].normal);
559                         VectorNormalize(brush->planes[4].normal);
560                         brush->planes[2].dist = DotProduct(brush->points[2].v, brush->planes[2].normal);
561                         brush->planes[3].dist = DotProduct(brush->points[0].v, brush->planes[3].normal);
562                         brush->planes[4].dist = DotProduct(brush->points[1].v, brush->planes[4].normal);
563
564                         if (developer.integer)
565                         {
566                                 // validation code
567                                 //VectorSubtract(brush->points[0].v, brush->points[1].v, edge0);
568                                 //VectorSubtract(brush->points[2].v, brush->points[1].v, edge1);
569                                 CrossProduct(edge1, edge0, normal);
570                                 VectorNormalize(normal);
571                                 VectorSubtract(normal, brush->planes[0].normal, temp);
572                                 if (VectorLength(temp) > 0.01f)
573                                         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]);
574                                 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)
575                                         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);
576                                 if (fabs(DotProduct(brush->planes[2].normal, brush->planes[0].normal)) > 0.01f)
577                                         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);
578                                 if (fabs(DotProduct(brush->planes[3].normal, brush->planes[0].normal)) > 0.01f)
579                                         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);
580                                 if (fabs(DotProduct(brush->planes[4].normal, brush->planes[0].normal)) > 0.01f)
581                                         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);
582                                 if (fabs(DotProduct(brush->planes[2].normal, edge0)) > 0.01f)
583                                         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]);
584                                 if (fabs(DotProduct(brush->planes[3].normal, edge1)) > 0.01f)
585                                         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]);
586                                 if (fabs(DotProduct(brush->planes[4].normal, edge2)) > 0.01f)
587                                         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]);
588                                 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)
589                                         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);
590                                 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)
591                                         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);
592                                 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)
593                                         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);
594                                 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)
595                                         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);
596                                 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)
597                                         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);
598                         }
599                 }
600         }
601         else
602         {
603                 // choose best surface normal for polygon's plane
604                 bestdist = 0;
605                 for (i = 0, p = brush->points + 1;i < brush->numpoints - 2;i++, p++)
606                 {
607                         VectorSubtract(p[-1].v, p[0].v, edge0);
608                         VectorSubtract(p[1].v, p[0].v, edge1);
609                         CrossProduct(edge0, edge1, normal);
610                         //TriangleNormal(p[-1].v, p[0].v, p[1].v, normal);
611                         dist = DotProduct(normal, normal);
612                         if (i == 0 || bestdist < dist)
613                         {
614                                 bestdist = dist;
615                                 VectorCopy(normal, brush->planes->normal);
616                         }
617                 }
618                 if (bestdist < 0.0001f)
619                 {
620                         // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
621                         brush->numplanes = 0;
622                         return;
623                 }
624                 else
625                 {
626                         brush->numplanes = brush->numpoints + 2;
627                         VectorNormalize(brush->planes->normal);
628                         brush->planes->dist = DotProduct(brush->points->v, brush->planes->normal);
629
630                         // negate plane to create other side
631                         VectorNegate(brush->planes[0].normal, brush->planes[1].normal);
632                         brush->planes[1].dist = -brush->planes[0].dist;
633                         for (i = 0, p = brush->points + (brush->numpoints - 1), p2 = brush->points;i < brush->numpoints;i++, p = p2, p2++)
634                         {
635                                 VectorSubtract(p->v, p2->v, edge0);
636                                 CrossProduct(edge0, brush->planes->normal, brush->planes[i + 2].normal);
637                                 VectorNormalize(brush->planes[i + 2].normal);
638                                 brush->planes[i + 2].dist = DotProduct(p->v, brush->planes[i + 2].normal);
639                         }
640                 }
641         }
642
643         if (developer.integer)
644         {
645                 // validity check - will be disabled later
646                 Collision_ValidateBrush(brush);
647                 for (i = 0;i < brush->numplanes;i++)
648                 {
649                         int j;
650                         for (j = 0, p = brush->points;j < brush->numpoints;j++, p++)
651                                 if (DotProduct(p->v, brush->planes[i].normal) > brush->planes[i].dist + (1.0 / 32.0))
652                                         Con_Printf("Error in brush plane generation, plane %i\n", i);
653                 }
654         }
655 }
656
657 colbrushf_t *Collision_AllocBrushFromPermanentPolygonFloat(mempool_t *mempool, int numpoints, float *points, int supercontents)
658 {
659         colbrushf_t *brush;
660         brush = Mem_Alloc(mempool, sizeof(colbrushf_t) + sizeof(colplanef_t) * (numpoints + 2));
661         brush->supercontents = supercontents;
662         brush->numpoints = numpoints;
663         brush->numplanes = numpoints + 2;
664         brush->planes = (void *)(brush + 1);
665         brush->points = (colpointf_t *)points;
666         Host_Error("Collision_AllocBrushFromPermanentPolygonFloat: FIXME: this code needs to be updated to generate a mesh...\n");
667         return brush;
668 }
669
670 #define COLLISIONEPSILON (1.0f / 32.0f)
671 #define COLLISIONEPSILON2 0//(1.0f / 32.0f)
672
673 // NOTE: start and end of each brush pair must have same numplanes/numpoints
674 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)
675 {
676         int nplane, nplane2, fstartsolid, fendsolid, brushsolid;
677         float enterfrac, leavefrac, d1, d2, f, newimpactnormal[3];
678         const colplanef_t *startplane, *endplane;
679
680         enterfrac = -1;
681         leavefrac = 1;
682         fstartsolid = true;
683         fendsolid = true;
684
685         for (nplane = 0;nplane < thatbrush_start->numplanes + thisbrush_start->numplanes;nplane++)
686         {
687                 nplane2 = nplane;
688                 if (nplane2 >= thatbrush_start->numplanes)
689                 {
690                         nplane2 -= thatbrush_start->numplanes;
691                         startplane = thisbrush_start->planes + nplane2;
692                         endplane = thisbrush_end->planes + nplane2;
693                         if (developer.integer)
694                         {
695                                 // any brush with degenerate planes is not worth handling
696                                 if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
697                                 {
698                                         Con_Printf("Collision_TraceBrushBrushFloat: degenerate thisbrush plane!\n");
699                                         return;
700                                 }
701                                 f = furthestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints);
702                                 if (fabs(f - startplane->dist) > 0.01f)
703                                         Con_Printf("startplane->dist %f != calculated %f (thisbrush_start)\n", startplane->dist, f);
704                         }
705                         d1 = nearestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints) - furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints);
706                         d2 = nearestplanedist_float(endplane->normal, thisbrush_end->points, thisbrush_end->numpoints) - furthestplanedist_float(endplane->normal, thatbrush_end->points, thatbrush_end->numpoints) - COLLISIONEPSILON2;
707                 }
708                 else
709                 {
710                         startplane = thatbrush_start->planes + nplane2;
711                         endplane = thatbrush_end->planes + nplane2;
712                         if (developer.integer)
713                         {
714                                 // any brush with degenerate planes is not worth handling
715                                 if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
716                                 {
717                                         Con_Printf("Collision_TraceBrushBrushFloat: degenerate thatbrush plane!\n");
718                                         return;
719                                 }
720                                 f = furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints);
721                                 if (fabs(f - startplane->dist) > 0.01f)
722                                         Con_Printf("startplane->dist %f != calculated %f (thatbrush_start)\n", startplane->dist, f);
723                         }
724                         d1 = nearestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints) - startplane->dist;
725                         d2 = nearestplanedist_float(endplane->normal, thisbrush_end->points, thisbrush_end->numpoints) - startplane->dist - COLLISIONEPSILON2;
726                 }
727                 //Con_Printf("%c%i: d1 = %f, d2 = %f, d1 / (d1 - d2) = %f\n", nplane2 != nplane ? 'b' : 'a', nplane2, d1, d2, d1 / (d1 - d2));
728
729                 f = d1 - d2;
730                 if (f >= 0)
731                 {
732                         // moving into brush
733                         if (d2 > 0)
734                                 return;
735                         if (d1 < 0)
736                                 continue;
737                         // enter
738                         fstartsolid = false;
739                         f = (d1 - COLLISIONEPSILON) / f;
740                         f = bound(0, f, 1);
741                         if (enterfrac < f)
742                         {
743                                 enterfrac = f;
744                                 VectorLerp(startplane->normal, enterfrac, endplane->normal, newimpactnormal);
745                         }
746                 }
747                 else
748                 {
749                         // moving out of brush
750                         if (d1 > 0)
751                                 return;
752                         if (d2 < 0)
753                                 continue;
754                         // leave
755                         fendsolid = false;
756                         f = (d1 + COLLISIONEPSILON) / f;
757                         f = bound(0, f, 1);
758                         if (leavefrac > f)
759                                 leavefrac = f;
760                 }
761         }
762
763         brushsolid = trace->hitsupercontentsmask & thatbrush_start->supercontents;
764         if (fstartsolid)
765         {
766                 trace->startsupercontents |= thatbrush_start->supercontents;
767                 if (brushsolid)
768                 {
769                         trace->startsolid = true;
770                         if (fendsolid)
771                                 trace->allsolid = true;
772                 }
773         }
774
775         // LordHavoc: we need an epsilon nudge here because for a point trace the
776         // penetrating line segment is normally zero length if this brush was
777         // generated from a polygon (infinitely thin), and could even be slightly
778         // positive or negative due to rounding errors in that case.
779         if (brushsolid && enterfrac > -1 && enterfrac < trace->fraction && enterfrac - (1.0f / 1024.0f) <= leavefrac)
780         {
781                 trace->fraction = bound(0, enterfrac, 1);
782                 VectorCopy(newimpactnormal, trace->plane.normal);
783         }
784 }
785
786 // NOTE: start and end brush pair must have same numplanes/numpoints
787 void Collision_TraceLineBrushFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, const colbrushf_t *thatbrush_start, const colbrushf_t *thatbrush_end)
788 {
789         int nplane, fstartsolid, fendsolid, brushsolid;
790         float enterfrac, leavefrac, d1, d2, f, newimpactnormal[3];
791         const colplanef_t *startplane, *endplane;
792
793         enterfrac = -1;
794         leavefrac = 1;
795         fstartsolid = true;
796         fendsolid = true;
797
798         for (nplane = 0;nplane < thatbrush_start->numplanes;nplane++)
799         {
800                 startplane = thatbrush_start->planes + nplane;
801                 endplane = thatbrush_end->planes + nplane;
802                 d1 = DotProduct(startplane->normal, linestart) - startplane->dist;
803                 d2 = DotProduct(endplane->normal, lineend) - endplane->dist - COLLISIONEPSILON2;
804                 if (developer.integer)
805                 {
806                         // any brush with degenerate planes is not worth handling
807                         if (DotProduct(startplane->normal, startplane->normal) < 0.9f || DotProduct(endplane->normal, endplane->normal) < 0.9f)
808                         {
809                                 Con_Printf("Collision_TraceLineBrushFloat: degenerate plane!\n");
810                                 return;
811                         }
812                         f = furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints);
813                         if (fabs(f - startplane->dist) > 0.01f)
814                                 Con_Printf("startplane->dist %f != calculated %f\n", startplane->dist, f);
815                 }
816
817                 f = d1 - d2;
818                 if (f >= 0)
819                 {
820                         // moving into brush
821                         if (d2 > 0)
822                                 return;
823                         if (d1 < 0)
824                                 continue;
825                         // enter
826                         fstartsolid = false;
827                         f = (d1 - COLLISIONEPSILON) / f;
828                         f = bound(0, f, 1);
829                         if (enterfrac < f)
830                         {
831                                 enterfrac = f;
832                                 VectorLerp(startplane->normal, enterfrac, endplane->normal, newimpactnormal);
833                         }
834                 }
835                 else
836                 {
837                         // moving out of brush
838                         if (d1 > 0)
839                                 return;
840                         if (d2 < 0)
841                                 continue;
842                         // leave
843                         fendsolid = false;
844                         f = (d1 + COLLISIONEPSILON) / f;
845                         f = bound(0, f, 1);
846                         if (leavefrac > f)
847                                 leavefrac = f;
848                 }
849         }
850
851         brushsolid = trace->hitsupercontentsmask & thatbrush_start->supercontents;
852         if (fstartsolid)
853         {
854                 trace->startsupercontents |= thatbrush_start->supercontents;
855                 if (brushsolid)
856                 {
857                         trace->startsolid = true;
858                         if (fendsolid)
859                                 trace->allsolid = true;
860                 }
861         }
862
863         // LordHavoc: we need an epsilon nudge here because for a point trace the
864         // penetrating line segment is normally zero length if this brush was
865         // generated from a polygon (infinitely thin), and could even be slightly
866         // positive or negative due to rounding errors in that case.
867         if (brushsolid && enterfrac > -1 && enterfrac < trace->fraction && enterfrac - (1.0f / 1024.0f) <= leavefrac)
868         {
869                 trace->fraction = bound(0, enterfrac, 1);
870                 VectorCopy(newimpactnormal, trace->plane.normal);
871         }
872 }
873
874 static colpointf_t polyf_points[256];
875 static colplanef_t polyf_planes[256 + 2];
876 static colbrushf_t polyf_brush;
877
878 void Collision_TraceBrushPolygonFloat(trace_t *trace, const colbrushf_t *thisbrush_start, const colbrushf_t *thisbrush_end, int numpoints, const float *points, int supercontents)
879 {
880         if (numpoints > 256)
881         {
882                 Con_Printf("Polygon with more than 256 points not supported yet (fixme!)\n");
883                 return;
884         }
885         polyf_brush.numpoints = numpoints;
886         polyf_brush.numplanes = numpoints + 2;
887         polyf_brush.points = (colpointf_t *)points;
888         polyf_brush.planes = polyf_planes;
889         polyf_brush.supercontents = supercontents;
890         Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
891         //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
892         Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brush, &polyf_brush);
893 }
894
895 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, const vec3_t segmentmins, const vec3_t segmentmaxs)
896 {
897         int i;
898         float facemins[3], facemaxs[3];
899         polyf_brush.numpoints = 3;
900         polyf_brush.numplanes = 5;
901         polyf_brush.points = polyf_points;
902         polyf_brush.planes = polyf_planes;
903         polyf_brush.supercontents = supercontents;
904         for (i = 0;i < numtriangles;i++, element3i += 3)
905         {
906                 VectorCopy(vertex3f + element3i[0] * 3, polyf_points[0].v);
907                 VectorCopy(vertex3f + element3i[1] * 3, polyf_points[1].v);
908                 VectorCopy(vertex3f + element3i[2] * 3, polyf_points[2].v);
909                 facemins[0] = min(polyf_points[0].v[0], min(polyf_points[1].v[0], polyf_points[2].v[0]));
910                 facemins[1] = min(polyf_points[0].v[1], min(polyf_points[1].v[1], polyf_points[2].v[1]));
911                 facemins[2] = min(polyf_points[0].v[2], min(polyf_points[1].v[2], polyf_points[2].v[2]));
912                 facemaxs[0] = max(polyf_points[0].v[0], max(polyf_points[1].v[0], polyf_points[2].v[0]));
913                 facemaxs[1] = max(polyf_points[0].v[1], max(polyf_points[1].v[1], polyf_points[2].v[1]));
914                 facemaxs[2] = max(polyf_points[0].v[2], max(polyf_points[1].v[2], polyf_points[2].v[2]));
915                 if (BoxesOverlap(segmentmins, segmentmaxs, facemins, facemaxs))
916                 {
917                         Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
918                         //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
919                         Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brush, &polyf_brush);
920                 }
921         }
922 }
923
924 void Collision_TraceLinePolygonFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, int numpoints, const float *points, int supercontents)
925 {
926         if (numpoints > 256)
927         {
928                 Con_Printf("Polygon with more than 256 points not supported yet (fixme!)\n");
929                 return;
930         }
931         polyf_brush.numpoints = numpoints;
932         polyf_brush.numplanes = numpoints + 2;
933         polyf_brush.points = (colpointf_t *)points;
934         polyf_brush.planes = polyf_planes;
935         polyf_brush.supercontents = supercontents;
936         Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
937         //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
938         Collision_TraceLineBrushFloat(trace, linestart, lineend, &polyf_brush, &polyf_brush);
939 }
940
941 void Collision_TraceLineTriangleMeshFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, int numtriangles, const int *element3i, const float *vertex3f, int supercontents, const vec3_t segmentmins, const vec3_t segmentmaxs)
942 {
943         int i;
944         float facemins[3], facemaxs[3];
945         polyf_brush.numpoints = 3;
946         polyf_brush.numplanes = 5;
947         polyf_brush.points = polyf_points;
948         polyf_brush.planes = polyf_planes;
949         polyf_brush.supercontents = supercontents;
950         for (i = 0;i < numtriangles;i++, element3i += 3)
951         {
952                 VectorCopy(vertex3f + element3i[0] * 3, polyf_points[0].v);
953                 VectorCopy(vertex3f + element3i[1] * 3, polyf_points[1].v);
954                 VectorCopy(vertex3f + element3i[2] * 3, polyf_points[2].v);
955                 facemins[0] = min(polyf_points[0].v[0], min(polyf_points[1].v[0], polyf_points[2].v[0]));
956                 facemins[1] = min(polyf_points[0].v[1], min(polyf_points[1].v[1], polyf_points[2].v[1]));
957                 facemins[2] = min(polyf_points[0].v[2], min(polyf_points[1].v[2], polyf_points[2].v[2]));
958                 facemaxs[0] = max(polyf_points[0].v[0], max(polyf_points[1].v[0], polyf_points[2].v[0]));
959                 facemaxs[1] = max(polyf_points[0].v[1], max(polyf_points[1].v[1], polyf_points[2].v[1]));
960                 facemaxs[2] = max(polyf_points[0].v[2], max(polyf_points[1].v[2], polyf_points[2].v[2]));
961                 if (BoxesOverlap(segmentmins, segmentmaxs, facemins, facemaxs))
962                 {
963                         Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
964                         //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
965                         Collision_TraceLineBrushFloat(trace, linestart, lineend, &polyf_brush, &polyf_brush);
966                 }
967         }
968 }
969
970
971 static colpointf_t polyf_pointsstart[256], polyf_pointsend[256];
972 static colplanef_t polyf_planesstart[256 + 2], polyf_planesend[256 + 2];
973 static colbrushf_t polyf_brushstart, polyf_brushend;
974
975 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)
976 {
977         int i;
978         if (numpoints > 256)
979         {
980                 Con_Printf("Polygon with more than 256 points not supported yet (fixme!)\n");
981                 return;
982         }
983         polyf_brushstart.numpoints = numpoints;
984         polyf_brushstart.numplanes = numpoints + 2;
985         polyf_brushstart.points = polyf_pointsstart;//(colpointf_t *)points;
986         polyf_brushstart.planes = polyf_planesstart;
987         polyf_brushstart.supercontents = supercontents;
988         for (i = 0;i < numpoints;i++)
989                 Matrix4x4_Transform(polygonmatrixstart, points + i * 3, polyf_brushstart.points[i].v);
990         polyf_brushend.numpoints = numpoints;
991         polyf_brushend.numplanes = numpoints + 2;
992         polyf_brushend.points = polyf_pointsend;//(colpointf_t *)points;
993         polyf_brushend.planes = polyf_planesend;
994         polyf_brushend.supercontents = supercontents;
995         for (i = 0;i < numpoints;i++)
996                 Matrix4x4_Transform(polygonmatrixend, points + i * 3, polyf_brushend.points[i].v);
997         Collision_CalcPlanesForPolygonBrushFloat(&polyf_brushstart);
998         Collision_CalcPlanesForPolygonBrushFloat(&polyf_brushend);
999
1000         //Collision_PrintBrushAsQHull(&polyf_brushstart, "polyf_brushstart");
1001         //Collision_PrintBrushAsQHull(&polyf_brushend, "polyf_brushend");
1002
1003         Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brushstart, &polyf_brushend);
1004 }
1005
1006
1007
1008 #define MAX_BRUSHFORBOX 16
1009 static int brushforbox_index = 0;
1010 static colpointf_t brushforbox_point[MAX_BRUSHFORBOX*8];
1011 static colplanef_t brushforbox_plane[MAX_BRUSHFORBOX*6];
1012 static colbrushf_t brushforbox_brush[MAX_BRUSHFORBOX];
1013 static colbrushf_t brushforpoint_brush[MAX_BRUSHFORBOX];
1014
1015 void Collision_InitBrushForBox(void)
1016 {
1017         int i;
1018         for (i = 0;i < MAX_BRUSHFORBOX;i++)
1019         {
1020                 brushforbox_brush[i].supercontents = SUPERCONTENTS_SOLID;
1021                 brushforbox_brush[i].numpoints = 8;
1022                 brushforbox_brush[i].numplanes = 6;
1023                 brushforbox_brush[i].points = brushforbox_point + i * 8;
1024                 brushforbox_brush[i].planes = brushforbox_plane + i * 6;
1025                 brushforpoint_brush[i].supercontents = SUPERCONTENTS_SOLID;
1026                 brushforpoint_brush[i].numpoints = 1;
1027                 brushforpoint_brush[i].numplanes = 0;
1028                 brushforpoint_brush[i].points = brushforbox_point + i * 8;
1029                 brushforpoint_brush[i].planes = brushforbox_plane + i * 6;
1030         }
1031 }
1032
1033 colbrushf_t *Collision_BrushForBox(const matrix4x4_t *matrix, const vec3_t mins, const vec3_t maxs)
1034 {
1035         int i;
1036         vec3_t v;
1037         colbrushf_t *brush;
1038         if (brushforbox_brush[0].numpoints == 0)
1039                 Collision_InitBrushForBox();
1040         if (VectorCompare(mins, maxs))
1041         {
1042                 // point brush
1043                 brush = brushforpoint_brush + ((brushforbox_index++) % MAX_BRUSHFORBOX);
1044                 VectorCopy(mins, brush->points->v);
1045         }
1046         else
1047         {
1048                 brush = brushforbox_brush + ((brushforbox_index++) % MAX_BRUSHFORBOX);
1049                 // FIXME: optimize
1050                 for (i = 0;i < 8;i++)
1051                 {
1052                         v[0] = i & 1 ? maxs[0] : mins[0];
1053                         v[1] = i & 2 ? maxs[1] : mins[1];
1054                         v[2] = i & 4 ? maxs[2] : mins[2];
1055                         Matrix4x4_Transform(matrix, v, brush->points[i].v);
1056                 }
1057                 // FIXME: optimize!
1058                 for (i = 0;i < 6;i++)
1059                 {
1060                         VectorClear(v);
1061                         v[i >> 1] = i & 1 ? 1 : -1;
1062                         Matrix4x4_Transform3x3(matrix, v, brush->planes[i].normal);
1063                         VectorNormalize(brush->planes[i].normal);
1064                         brush->planes[i].dist = furthestplanedist_float(brush->planes[i].normal, brush->points, brush->numpoints);
1065                 }
1066         }
1067         Collision_ValidateBrush(brush);
1068         return brush;
1069 }
1070
1071 void Collision_ClipTrace_BrushBox(trace_t *trace, const vec3_t cmins, const vec3_t cmaxs, const vec3_t start, const vec3_t mins, const vec3_t maxs, const vec3_t end, int hitsupercontentsmask)
1072 {
1073         colbrushf_t *boxbrush, *thisbrush_start, *thisbrush_end;
1074         matrix4x4_t identitymatrix;
1075         vec3_t startmins, startmaxs, endmins, endmaxs;
1076
1077         // create brushes for the collision
1078         VectorAdd(start, mins, startmins);
1079         VectorAdd(start, maxs, startmaxs);
1080         VectorAdd(end, mins, endmins);
1081         VectorAdd(end, maxs, endmaxs);
1082         Matrix4x4_CreateIdentity(&identitymatrix);
1083         boxbrush = Collision_BrushForBox(&identitymatrix, cmins, cmaxs);
1084         thisbrush_start = Collision_BrushForBox(&identitymatrix, startmins, startmaxs);
1085         thisbrush_end = Collision_BrushForBox(&identitymatrix, endmins, endmaxs);
1086
1087         memset(trace, 0, sizeof(trace_t));
1088         trace->hitsupercontentsmask = hitsupercontentsmask;
1089         trace->fraction = 1;
1090         trace->allsolid = true;
1091         Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, boxbrush, boxbrush);
1092 }
1093
1094 // LordHavoc: currently unused and not yet tested
1095 // note: this can be used for tracing a moving sphere vs a stationary sphere,
1096 // by simply adding the moving sphere's radius to the sphereradius parameter,
1097 // all the results are correct (impactpoint, impactnormal, and fraction)
1098 float Collision_ClipTrace_Line_Sphere(double *linestart, double *lineend, double *sphereorigin, double sphereradius, double *impactpoint, double *impactnormal)
1099 {
1100         double dir[3], scale, v[3], deviationdist, impactdist, linelength;
1101         // make sure the impactpoint and impactnormal are valid even if there is
1102         // no collision
1103         impactpoint[0] = lineend[0];
1104         impactpoint[1] = lineend[1];
1105         impactpoint[2] = lineend[2];
1106         impactnormal[0] = 0;
1107         impactnormal[1] = 0;
1108         impactnormal[2] = 0;
1109         // calculate line direction
1110         dir[0] = lineend[0] - linestart[0];
1111         dir[1] = lineend[1] - linestart[1];
1112         dir[2] = lineend[2] - linestart[2];
1113         // normalize direction
1114         linelength = sqrt(dir[0] * dir[0] + dir[1] * dir[1] + dir[2] * dir[2]);
1115         if (linelength)
1116         {
1117                 scale = 1.0 / linelength;
1118                 dir[0] *= scale;
1119                 dir[1] *= scale;
1120                 dir[2] *= scale;
1121         }
1122         // this dotproduct calculates the distance along the line at which the
1123         // sphere origin is (nearest point to the sphere origin on the line)
1124         impactdist = dir[0] * (sphereorigin[0] - linestart[0]) + dir[1] * (sphereorigin[1] - linestart[1]) + dir[2] * (sphereorigin[2] - linestart[2]);
1125         // calculate point on line at that distance, and subtract the
1126         // sphereorigin from it, so we have a vector to measure for the distance
1127         // of the line from the sphereorigin (deviation, how off-center it is)
1128         v[0] = linestart[0] + impactdist * dir[0] - sphereorigin[0];
1129         v[1] = linestart[1] + impactdist * dir[1] - sphereorigin[1];
1130         v[2] = linestart[2] + impactdist * dir[2] - sphereorigin[2];
1131         deviationdist = v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
1132         // if outside the radius, it's a miss for sure
1133         // (we do this comparison using squared radius to avoid a sqrt)
1134         if (deviationdist > sphereradius*sphereradius)
1135                 return 1; // miss (off to the side)
1136         // nudge back to find the correct impact distance
1137         impactdist += (sqrt(deviationdist) - sphereradius);
1138         if (impactdist >= linelength)
1139                 return 1; // miss (not close enough)
1140         if (impactdist < 0)
1141                 return 1; // miss (linestart is past or inside sphere)
1142         // calculate new impactpoint
1143         impactpoint[0] = linestart[0] + impactdist * dir[0];
1144         impactpoint[1] = linestart[1] + impactdist * dir[1];
1145         impactpoint[2] = linestart[2] + impactdist * dir[2];
1146         // calculate impactnormal (surface normal at point of impact)
1147         impactnormal[0] = impactpoint[0] - sphereorigin[0];
1148         impactnormal[1] = impactpoint[1] - sphereorigin[1];
1149         impactnormal[2] = impactpoint[2] - sphereorigin[2];
1150         // normalize impactnormal
1151         scale = impactnormal[0] * impactnormal[0] + impactnormal[1] * impactnormal[1] + impactnormal[2] * impactnormal[2];
1152         if (scale)
1153         {
1154                 scale = 1.0 / sqrt(scale);
1155                 impactnormal[0] *= scale;
1156                 impactnormal[1] *= scale;
1157                 impactnormal[2] *= scale;
1158         }
1159         // return fraction of movement distance
1160         return impactdist / linelength;
1161 }
1162
1163 typedef struct colbspnode_s
1164 {
1165         mplane_t plane;
1166         struct colbspnode_s *children[2];
1167         // the node is reallocated or split if max is reached
1168         int numcolbrushf;
1169         int maxcolbrushf;
1170         colbrushf_t **colbrushflist;
1171         //int numcolbrushd;
1172         //int maxcolbrushd;
1173         //colbrushd_t **colbrushdlist;
1174 }
1175 colbspnode_t;
1176
1177 typedef struct colbsp_s
1178 {
1179         mempool_t *mempool;
1180         colbspnode_t *nodes;
1181 }
1182 colbsp_t;
1183
1184 colbsp_t *Collision_CreateCollisionBSP(mempool_t *mempool)
1185 {
1186         colbsp_t *bsp;
1187         bsp = Mem_Alloc(mempool, sizeof(colbsp_t));
1188         bsp->mempool = mempool;
1189         bsp->nodes = Mem_Alloc(bsp->mempool, sizeof(colbspnode_t));
1190         return bsp;
1191 }
1192
1193 void Collision_FreeCollisionBSPNode(colbspnode_t *node)
1194 {
1195         if (node->children[0])
1196                 Collision_FreeCollisionBSPNode(node->children[0]);
1197         if (node->children[1])
1198                 Collision_FreeCollisionBSPNode(node->children[1]);
1199         while (--node->numcolbrushf)
1200                 Mem_Free(node->colbrushflist[node->numcolbrushf]);
1201         //while (--node->numcolbrushd)
1202         //      Mem_Free(node->colbrushdlist[node->numcolbrushd]);
1203         Mem_Free(node);
1204 }
1205
1206 void Collision_FreeCollisionBSP(colbsp_t *bsp)
1207 {
1208         Collision_FreeCollisionBSPNode(bsp->nodes);
1209         Mem_Free(bsp);
1210 }
1211
1212 void Collision_BoundingBoxOfBrushTraceSegment(const colbrushf_t *start, const colbrushf_t *end, vec3_t mins, vec3_t maxs, float startfrac, float endfrac)
1213 {
1214         int i;
1215         colpointf_t *ps, *pe;
1216         float tempstart[3], tempend[3];
1217         VectorLerp(start->points[0].v, startfrac, end->points[0].v, mins);
1218         VectorCopy(mins, maxs);
1219         for (i = 0, ps = start->points, pe = end->points;i < start->numpoints;i++, ps++, pe++)
1220         {
1221                 VectorLerp(ps->v, startfrac, pe->v, tempstart);
1222                 VectorLerp(ps->v, endfrac, pe->v, tempend);
1223                 mins[0] = min(mins[0], min(tempstart[0], tempend[0]));
1224                 mins[1] = min(mins[1], min(tempstart[1], tempend[1]));
1225                 mins[2] = min(mins[2], min(tempstart[2], tempend[2]));
1226                 maxs[0] = min(maxs[0], min(tempstart[0], tempend[0]));
1227                 maxs[1] = min(maxs[1], min(tempstart[1], tempend[1]));
1228                 maxs[2] = min(maxs[2], min(tempstart[2], tempend[2]));
1229         }
1230 }
1231