5 #define COLLISION_EDGEDIR_DOT_EPSILON (0.999f)
6 #define COLLISION_EDGECROSS_MINLENGTH2 (1.0f / 4194304.0f)
7 #define COLLISION_SNAPSCALE (32.0f)
8 #define COLLISION_SNAP (1.0f / COLLISION_SNAPSCALE)
9 #define COLLISION_SNAP2 (2.0f / COLLISION_SNAPSCALE)
10 #define COLLISION_PLANE_DIST_EPSILON (2.0f / COLLISION_SNAPSCALE)
12 cvar_t collision_impactnudge = {0, "collision_impactnudge", "0.03125", "how much to back off from the impact"};
13 cvar_t collision_startnudge = {0, "collision_startnudge", "0", "how much to bias collision trace start"};
14 cvar_t collision_endnudge = {0, "collision_endnudge", "0", "how much to bias collision trace end"};
15 cvar_t collision_enternudge = {0, "collision_enternudge", "0", "how much to bias collision entry fraction"};
16 cvar_t collision_leavenudge = {0, "collision_leavenudge", "0", "how much to bias collision exit fraction"};
17 cvar_t collision_prefernudgedfraction = {0, "collision_prefernudgedfraction", "1", "whether to sort collision events by nudged fraction (1) or real fraction (0)"};
18 #ifdef COLLISION_STUPID_TRACE_ENDPOS_IN_SOLID_WORKAROUND
19 cvar_t collision_endposnudge = {0, "collision_endposnudge", "0", "workaround to fix trace_endpos sometimes being returned where it would be inside solid by making that collision hit (recommended: values like 1)"};
22 void Collision_Init (void)
24 Cvar_RegisterVariable(&collision_impactnudge);
25 Cvar_RegisterVariable(&collision_startnudge);
26 Cvar_RegisterVariable(&collision_endnudge);
27 Cvar_RegisterVariable(&collision_enternudge);
28 Cvar_RegisterVariable(&collision_leavenudge);
29 Cvar_RegisterVariable(&collision_prefernudgedfraction);
30 #ifdef COLLISION_STUPID_TRACE_ENDPOS_IN_SOLID_WORKAROUND
31 Cvar_RegisterVariable(&collision_endposnudge);
48 void Collision_PrintBrushAsQHull(colbrushf_t *brush, const char *name)
51 Con_Printf("3 %s\n%i\n", name, brush->numpoints);
52 for (i = 0;i < brush->numpoints;i++)
53 Con_Printf("%f %f %f\n", brush->points[i].v[0], brush->points[i].v[1], brush->points[i].v[2]);
55 Con_Printf("4\n%i\n", brush->numplanes);
56 for (i = 0;i < brush->numplanes;i++)
57 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);
60 void Collision_ValidateBrush(colbrushf_t *brush)
62 int j, k, pointsoffplanes, pointonplanes, pointswithinsufficientplanes, printbrush;
65 if (!brush->numpoints)
67 Con_Print("Collision_ValidateBrush: brush with no points!\n");
71 // it's ok for a brush to have one point and no planes...
72 if (brush->numplanes == 0 && brush->numpoints != 1)
74 Con_Print("Collision_ValidateBrush: brush with no planes and more than one point!\n");
81 pointswithinsufficientplanes = 0;
82 for (k = 0;k < brush->numplanes;k++)
83 if (DotProduct(brush->planes[k].normal, brush->planes[k].normal) < 0.0001f)
84 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);
85 for (j = 0;j < brush->numpoints;j++)
88 for (k = 0;k < brush->numplanes;k++)
90 d = DotProduct(brush->points[j].v, brush->planes[k].normal) - brush->planes[k].dist;
91 if (d > COLLISION_PLANE_DIST_EPSILON)
93 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);
96 if (fabs(d) > COLLISION_PLANE_DIST_EPSILON)
101 if (pointonplanes < 3)
102 pointswithinsufficientplanes++;
104 if (pointswithinsufficientplanes)
106 Con_Print("Collision_ValidateBrush: some points have insufficient planes, every point must be on at least 3 planes to form a corner.\n");
109 if (pointsoffplanes == 0) // all points are on all planes
111 Con_Print("Collision_ValidateBrush: all points lie on all planes (degenerate, no brush volume!)\n");
116 Collision_PrintBrushAsQHull(brush, "unnamed");
119 float nearestplanedist_float(const float *normal, const colpointf_t *points, int numpoints)
121 float dist, bestdist;
124 bestdist = DotProduct(points->v, normal);
128 dist = DotProduct(points->v, normal);
129 bestdist = min(bestdist, dist);
135 float furthestplanedist_float(const float *normal, const colpointf_t *points, int numpoints)
137 float dist, bestdist;
140 bestdist = DotProduct(points->v, normal);
144 dist = DotProduct(points->v, normal);
145 bestdist = max(bestdist, dist);
151 void Collision_CalcEdgeDirsForPolygonBrushFloat(colbrushf_t *brush)
154 for (i = 0, j = brush->numpoints - 1;i < brush->numpoints;j = i, i++)
155 VectorSubtract(brush->points[i].v, brush->points[j].v, brush->edgedirs[j].v);
158 colbrushf_t *Collision_NewBrushFromPlanes(mempool_t *mempool, int numoriginalplanes, const colplanef_t *originalplanes, int supercontents, int q3surfaceflags, const texture_t *texture, int hasaabbplanes)
160 // TODO: planesbuf could be replaced by a remapping table
161 int j, k, l, m, w, xyzflags;
162 int numpointsbuf = 0, maxpointsbuf = 256, numedgedirsbuf = 0, maxedgedirsbuf = 256, numplanesbuf = 0, maxplanesbuf = 256, numelementsbuf = 0, maxelementsbuf = 256;
166 colpointf_t pointsbuf[256];
167 colpointf_t edgedirsbuf[256];
168 colplanef_t planesbuf[256];
169 int elementsbuf[1024];
170 int polypointbuf[256];
175 // enable these if debugging to avoid seeing garbage in unused data-
176 memset(pointsbuf, 0, sizeof(pointsbuf));
177 memset(edgedirsbuf, 0, sizeof(edgedirsbuf));
178 memset(planesbuf, 0, sizeof(planesbuf));
179 memset(elementsbuf, 0, sizeof(elementsbuf));
180 memset(polypointbuf, 0, sizeof(polypointbuf));
181 memset(p, 0, sizeof(p));
184 // check if there are too many planes and skip the brush
185 if (numoriginalplanes >= maxplanesbuf)
187 Con_DPrint("Collision_NewBrushFromPlanes: failed to build collision brush: too many planes for buffer\n");
191 // figure out how large a bounding box we need to properly compute this brush
193 for (j = 0;j < numoriginalplanes;j++)
194 maxdist = max(maxdist, fabs(originalplanes[j].dist));
195 // now make it large enough to enclose the entire brush, and round it off to a reasonable multiple of 1024
196 maxdist = floor(maxdist * (4.0 / 1024.0) + 2) * 1024.0;
197 // construct a collision brush (points, planes, and renderable mesh) from
198 // a set of planes, this also optimizes out any unnecessary planes (ones
199 // whose polygon is clipped away by the other planes)
200 for (j = 0;j < numoriginalplanes;j++)
203 VectorCopy(originalplanes[j].normal, planesbuf[numplanesbuf].normal);
204 planesbuf[numplanesbuf].dist = originalplanes[j].dist;
205 planesbuf[numplanesbuf].q3surfaceflags = originalplanes[j].q3surfaceflags;
206 planesbuf[numplanesbuf].texture = originalplanes[j].texture;
209 // create a large polygon from the plane
211 PolygonD_QuadForPlane(p[w], originalplanes[j].normal[0], originalplanes[j].normal[1], originalplanes[j].normal[2], originalplanes[j].dist, maxdist);
213 // clip it by all other planes
214 for (k = 0;k < numoriginalplanes && pnumpoints >= 3 && pnumpoints <= pmaxpoints;k++)
216 // skip the plane this polygon
217 // (nothing happens if it is processed, this is just an optimization)
220 // we want to keep the inside of the brush plane so we flip
222 PolygonD_Divide(pnumpoints, p[w], -originalplanes[k].normal[0], -originalplanes[k].normal[1], -originalplanes[k].normal[2], -originalplanes[k].dist, COLLISION_PLANE_DIST_EPSILON, pmaxpoints, p[!w], &pnumpoints, 0, NULL, NULL, NULL);
227 // if nothing is left, skip it
230 //Con_DPrintf("Collision_NewBrushFromPlanes: warning: polygon for plane %f %f %f %f clipped away\n", originalplanes[j].normal[0], originalplanes[j].normal[1], originalplanes[j].normal[2], originalplanes[j].dist);
234 for (k = 0;k < pnumpoints;k++)
238 for (l = 0;l < numoriginalplanes;l++)
239 if (fabs(DotProduct(&p[w][k*3], originalplanes[l].normal) - originalplanes[l].dist) < COLLISION_PLANE_DIST_EPSILON)
246 Con_DPrintf("Collision_NewBrushFromPlanes: warning: polygon point does not lie on at least 3 planes\n");
250 // check if there are too many polygon vertices for buffer
251 if (pnumpoints > pmaxpoints)
253 Con_DPrint("Collision_NewBrushFromPlanes: failed to build collision brush: too many points for buffer\n");
257 // check if there are too many triangle elements for buffer
258 if (numelementsbuf + (pnumpoints - 2) * 3 > maxelementsbuf)
260 Con_DPrint("Collision_NewBrushFromPlanes: failed to build collision brush: too many triangle elements for buffer\n");
264 // add the unique points for this polygon
265 for (k = 0;k < pnumpoints;k++)
268 // downgrade to float precision before comparing
269 VectorCopy(&p[w][k*3], v);
271 // check if there is already a matching point (no duplicates)
272 for (m = 0;m < numpointsbuf;m++)
273 if (VectorDistance2(v, pointsbuf[m].v) < COLLISION_SNAP2)
276 // if there is no match, add a new one
277 if (m == numpointsbuf)
279 // check if there are too many and skip the brush
280 if (numpointsbuf >= maxpointsbuf)
282 Con_DPrint("Collision_NewBrushFromPlanes: failed to build collision brush: too many points for buffer\n");
286 VectorCopy(&p[w][k*3], pointsbuf[numpointsbuf].v);
290 // store the index into a buffer
294 // add the triangles for the polygon
295 // (this particular code makes a triangle fan)
296 for (k = 0;k < pnumpoints - 2;k++)
298 elementsbuf[numelementsbuf++] = polypointbuf[0];
299 elementsbuf[numelementsbuf++] = polypointbuf[k + 1];
300 elementsbuf[numelementsbuf++] = polypointbuf[k + 2];
303 // add the unique edgedirs for this polygon
304 for (k = 0, l = pnumpoints-1;k < pnumpoints;l = k, k++)
307 // downgrade to float precision before comparing
308 VectorSubtract(&p[w][k*3], &p[w][l*3], dir);
309 VectorNormalize(dir);
311 // check if there is already a matching edgedir (no duplicates)
312 for (m = 0;m < numedgedirsbuf;m++)
313 if (DotProduct(dir, edgedirsbuf[m].v) >= COLLISION_EDGEDIR_DOT_EPSILON)
315 // skip this if there is
316 if (m < numedgedirsbuf)
319 // try again with negated edgedir
320 VectorNegate(dir, dir);
321 // check if there is already a matching edgedir (no duplicates)
322 for (m = 0;m < numedgedirsbuf;m++)
323 if (DotProduct(dir, edgedirsbuf[m].v) >= COLLISION_EDGEDIR_DOT_EPSILON)
325 // if there is no match, add a new one
326 if (m == numedgedirsbuf)
328 // check if there are too many and skip the brush
329 if (numedgedirsbuf >= maxedgedirsbuf)
331 Con_DPrint("Collision_NewBrushFromPlanes: failed to build collision brush: too many edgedirs for buffer\n");
335 VectorCopy(dir, edgedirsbuf[numedgedirsbuf].v);
340 // if any normal is not purely axial, it's not an axis-aligned box
341 if (isaabb && (originalplanes[j].normal[0] == 0) + (originalplanes[j].normal[1] == 0) + (originalplanes[j].normal[2] == 0) < 2)
345 // if nothing is left, there's nothing to allocate
346 if (numplanesbuf < 4)
348 Con_DPrintf("Collision_NewBrushFromPlanes: failed to build collision brush: %i triangles, %i planes (input was %i planes), %i vertices\n", numelementsbuf / 3, numplanesbuf, numoriginalplanes, numpointsbuf);
352 // if no triangles or points could be constructed, then this routine failed but the brush is not discarded
353 if (numelementsbuf < 12 || numpointsbuf < 4)
354 Con_DPrintf("Collision_NewBrushFromPlanes: unable to rebuild triangles/points for collision brush: %i triangles, %i planes (input was %i planes), %i vertices\n", numelementsbuf / 3, numplanesbuf, numoriginalplanes, numpointsbuf);
356 // validate plane distances
357 for (j = 0;j < numplanesbuf;j++)
359 float d = furthestplanedist_float(planesbuf[j].normal, pointsbuf, numpointsbuf);
360 if (fabs(planesbuf[j].dist - d) > COLLISION_PLANE_DIST_EPSILON)
361 Con_DPrintf("plane %f %f %f %f mismatches dist %f\n", planesbuf[j].normal[0], planesbuf[j].normal[1], planesbuf[j].normal[2], planesbuf[j].dist, d);
364 // allocate the brush and copy to it
365 brush = (colbrushf_t *)Mem_Alloc(mempool, sizeof(colbrushf_t) + sizeof(colpointf_t) * numpointsbuf + sizeof(colpointf_t) * numedgedirsbuf + sizeof(colplanef_t) * numplanesbuf + sizeof(int) * numelementsbuf);
366 brush->isaabb = isaabb;
367 brush->hasaabbplanes = hasaabbplanes;
368 brush->supercontents = supercontents;
369 brush->numplanes = numplanesbuf;
370 brush->numedgedirs = numedgedirsbuf;
371 brush->numpoints = numpointsbuf;
372 brush->numtriangles = numelementsbuf / 3;
373 brush->planes = (colplanef_t *)(brush + 1);
374 brush->points = (colpointf_t *)(brush->planes + brush->numplanes);
375 brush->edgedirs = (colpointf_t *)(brush->points + brush->numpoints);
376 brush->elements = (int *)(brush->points + brush->numpoints);
377 brush->q3surfaceflags = q3surfaceflags;
378 brush->texture = texture;
379 for (j = 0;j < brush->numpoints;j++)
381 brush->points[j].v[0] = pointsbuf[j].v[0];
382 brush->points[j].v[1] = pointsbuf[j].v[1];
383 brush->points[j].v[2] = pointsbuf[j].v[2];
385 for (j = 0;j < brush->numedgedirs;j++)
387 brush->edgedirs[j].v[0] = edgedirsbuf[j].v[0];
388 brush->edgedirs[j].v[1] = edgedirsbuf[j].v[1];
389 brush->edgedirs[j].v[2] = edgedirsbuf[j].v[2];
391 for (j = 0;j < brush->numplanes;j++)
393 brush->planes[j].normal[0] = planesbuf[j].normal[0];
394 brush->planes[j].normal[1] = planesbuf[j].normal[1];
395 brush->planes[j].normal[2] = planesbuf[j].normal[2];
396 brush->planes[j].dist = planesbuf[j].dist;
397 brush->planes[j].q3surfaceflags = planesbuf[j].q3surfaceflags;
398 brush->planes[j].texture = planesbuf[j].texture;
400 for (j = 0;j < brush->numtriangles * 3;j++)
401 brush->elements[j] = elementsbuf[j];
404 VectorClear(brush->mins);
405 VectorClear(brush->maxs);
406 for (j = 0;j < min(6, numoriginalplanes);j++)
408 if (originalplanes[j].normal[0] == 1) {xyzflags |= 1;brush->maxs[0] = originalplanes[j].dist;}
409 else if (originalplanes[j].normal[0] == -1) {xyzflags |= 2;brush->mins[0] = -originalplanes[j].dist;}
410 else if (originalplanes[j].normal[1] == 1) {xyzflags |= 4;brush->maxs[1] = originalplanes[j].dist;}
411 else if (originalplanes[j].normal[1] == -1) {xyzflags |= 8;brush->mins[1] = -originalplanes[j].dist;}
412 else if (originalplanes[j].normal[2] == 1) {xyzflags |= 16;brush->maxs[2] = originalplanes[j].dist;}
413 else if (originalplanes[j].normal[2] == -1) {xyzflags |= 32;brush->mins[2] = -originalplanes[j].dist;}
415 // if not all xyzflags were set, then this is not a brush from q3map/q3map2, and needs reconstruction of the bounding box
416 // (this case works for any brush with valid points, but sometimes brushes are not reconstructed properly and hence the points are not valid, so this is reserved as a fallback case)
419 VectorCopy(brush->points[0].v, brush->mins);
420 VectorCopy(brush->points[0].v, brush->maxs);
421 for (j = 1;j < brush->numpoints;j++)
423 brush->mins[0] = min(brush->mins[0], brush->points[j].v[0]);
424 brush->mins[1] = min(brush->mins[1], brush->points[j].v[1]);
425 brush->mins[2] = min(brush->mins[2], brush->points[j].v[2]);
426 brush->maxs[0] = max(brush->maxs[0], brush->points[j].v[0]);
427 brush->maxs[1] = max(brush->maxs[1], brush->points[j].v[1]);
428 brush->maxs[2] = max(brush->maxs[2], brush->points[j].v[2]);
437 Collision_ValidateBrush(brush);
443 void Collision_CalcPlanesForPolygonBrushFloat(colbrushf_t *brush)
446 float edge0[3], edge1[3], edge2[3], normal[3], dist, bestdist;
449 // FIXME: these probably don't actually need to be normalized if the collision code does not care
450 if (brush->numpoints == 3)
452 // optimized triangle case
453 TriangleNormal(brush->points[0].v, brush->points[1].v, brush->points[2].v, brush->planes[0].normal);
454 if (DotProduct(brush->planes[0].normal, brush->planes[0].normal) < 0.0001f)
456 // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
457 brush->numplanes = 0;
462 brush->numplanes = 5;
463 brush->numedgedirs = 3;
464 VectorNormalize(brush->planes[0].normal);
465 brush->planes[0].dist = DotProduct(brush->points->v, brush->planes[0].normal);
466 VectorNegate(brush->planes[0].normal, brush->planes[1].normal);
467 brush->planes[1].dist = -brush->planes[0].dist;
468 VectorSubtract(brush->points[2].v, brush->points[0].v, edge0);
469 VectorSubtract(brush->points[0].v, brush->points[1].v, edge1);
470 VectorSubtract(brush->points[1].v, brush->points[2].v, edge2);
471 VectorCopy(edge0, brush->edgedirs[0].v);
472 VectorCopy(edge1, brush->edgedirs[1].v);
473 VectorCopy(edge2, brush->edgedirs[2].v);
476 float projectionnormal[3], projectionedge0[3], projectionedge1[3], projectionedge2[3];
478 float dist, bestdist;
479 bestdist = fabs(brush->planes[0].normal[0]);
481 for (i = 1;i < 3;i++)
483 dist = fabs(brush->planes[0].normal[i]);
490 VectorClear(projectionnormal);
491 if (brush->planes[0].normal[best] < 0)
492 projectionnormal[best] = -1;
494 projectionnormal[best] = 1;
495 VectorCopy(edge0, projectionedge0);
496 VectorCopy(edge1, projectionedge1);
497 VectorCopy(edge2, projectionedge2);
498 projectionedge0[best] = 0;
499 projectionedge1[best] = 0;
500 projectionedge2[best] = 0;
501 CrossProduct(projectionedge0, projectionnormal, brush->planes[2].normal);
502 CrossProduct(projectionedge1, projectionnormal, brush->planes[3].normal);
503 CrossProduct(projectionedge2, projectionnormal, brush->planes[4].normal);
506 CrossProduct(edge0, brush->planes->normal, brush->planes[2].normal);
507 CrossProduct(edge1, brush->planes->normal, brush->planes[3].normal);
508 CrossProduct(edge2, brush->planes->normal, brush->planes[4].normal);
510 VectorNormalize(brush->planes[2].normal);
511 VectorNormalize(brush->planes[3].normal);
512 VectorNormalize(brush->planes[4].normal);
513 brush->planes[2].dist = DotProduct(brush->points[2].v, brush->planes[2].normal);
514 brush->planes[3].dist = DotProduct(brush->points[0].v, brush->planes[3].normal);
515 brush->planes[4].dist = DotProduct(brush->points[1].v, brush->planes[4].normal);
517 if (developer_extra.integer)
523 VectorSubtract(brush->points[0].v, brush->points[1].v, edge0);
524 VectorSubtract(brush->points[2].v, brush->points[1].v, edge1);
525 CrossProduct(edge0, edge1, normal);
526 VectorNormalize(normal);
527 VectorSubtract(normal, brush->planes[0].normal, temp);
528 if (VectorLength(temp) > 0.01f)
529 Con_DPrintf("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]);
530 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)
531 Con_DPrintf("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);
533 if (fabs(DotProduct(brush->planes[2].normal, brush->planes[0].normal)) > 0.01f)
534 Con_DPrintf("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);
535 if (fabs(DotProduct(brush->planes[3].normal, brush->planes[0].normal)) > 0.01f)
536 Con_DPrintf("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);
537 if (fabs(DotProduct(brush->planes[4].normal, brush->planes[0].normal)) > 0.01f)
538 Con_DPrintf("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);
539 if (fabs(DotProduct(brush->planes[2].normal, edge0)) > 0.01f)
540 Con_DPrintf("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]);
541 if (fabs(DotProduct(brush->planes[3].normal, edge1)) > 0.01f)
542 Con_DPrintf("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]);
543 if (fabs(DotProduct(brush->planes[4].normal, edge2)) > 0.01f)
544 Con_DPrintf("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]);
547 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)
548 Con_DPrintf("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);
549 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)
550 Con_DPrintf("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);
551 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)
552 Con_DPrintf("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);
553 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)
554 Con_DPrintf("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);
555 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)
556 Con_DPrintf("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);
562 // choose best surface normal for polygon's plane
564 for (i = 0, p = brush->points + 1;i < brush->numpoints - 2;i++, p++)
566 VectorSubtract(p[-1].v, p[0].v, edge0);
567 VectorSubtract(p[1].v, p[0].v, edge1);
568 CrossProduct(edge0, edge1, normal);
569 //TriangleNormal(p[-1].v, p[0].v, p[1].v, normal);
570 dist = DotProduct(normal, normal);
571 if (i == 0 || bestdist < dist)
574 VectorCopy(normal, brush->planes->normal);
577 if (bestdist < 0.0001f)
579 // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
580 brush->numplanes = 0;
585 brush->numplanes = brush->numpoints + 2;
586 VectorNormalize(brush->planes->normal);
587 brush->planes->dist = DotProduct(brush->points->v, brush->planes->normal);
589 // negate plane to create other side
590 VectorNegate(brush->planes[0].normal, brush->planes[1].normal);
591 brush->planes[1].dist = -brush->planes[0].dist;
592 for (i = 0, p = brush->points + (brush->numpoints - 1), p2 = brush->points;i < brush->numpoints;i++, p = p2, p2++)
594 VectorSubtract(p->v, p2->v, edge0);
595 CrossProduct(edge0, brush->planes->normal, brush->planes[i + 2].normal);
596 VectorNormalize(brush->planes[i + 2].normal);
597 brush->planes[i + 2].dist = DotProduct(p->v, brush->planes[i + 2].normal);
602 if (developer_extra.integer)
604 // validity check - will be disabled later
605 Collision_ValidateBrush(brush);
606 for (i = 0;i < brush->numplanes;i++)
609 for (j = 0, p = brush->points;j < brush->numpoints;j++, p++)
610 if (DotProduct(p->v, brush->planes[i].normal) > brush->planes[i].dist + COLLISION_PLANE_DIST_EPSILON)
611 Con_DPrintf("Error in brush plane generation, plane %i\n", i);
616 colbrushf_t *Collision_AllocBrushFromPermanentPolygonFloat(mempool_t *mempool, int numpoints, float *points, int supercontents, int q3surfaceflags, const texture_t *texture)
619 brush = (colbrushf_t *)Mem_Alloc(mempool, sizeof(colbrushf_t) + sizeof(colplanef_t) * (numpoints + 2) + sizeof(colpointf_t) * numpoints);
620 brush->isaabb = false;
621 brush->hasaabbplanes = false;
622 brush->supercontents = supercontents;
623 brush->numpoints = numpoints;
624 brush->numedgedirs = numpoints;
625 brush->numplanes = numpoints + 2;
626 brush->planes = (colplanef_t *)(brush + 1);
627 brush->points = (colpointf_t *)points;
628 brush->edgedirs = (colpointf_t *)(brush->planes + brush->numplanes);
629 brush->q3surfaceflags = q3surfaceflags;
630 brush->texture = texture;
631 Sys_Error("Collision_AllocBrushFromPermanentPolygonFloat: FIXME: this code needs to be updated to generate a mesh...");
635 // NOTE: start and end of each brush pair must have same numplanes/numpoints
636 void Collision_TraceBrushBrushFloat(trace_t *trace, const colbrushf_t *trace_start, const colbrushf_t *trace_end, const colbrushf_t *other_start, const colbrushf_t *other_end)
638 int nplane, nplane2, nedge1, nedge2, hitq3surfaceflags = 0;
639 int tracenumedgedirs = trace_start->numedgedirs;
640 //int othernumedgedirs = other_start->numedgedirs;
641 int tracenumpoints = trace_start->numpoints;
642 int othernumpoints = other_start->numpoints;
643 int numplanes1 = other_start->numplanes;
644 int numplanes2 = numplanes1 + trace_start->numplanes;
645 int numplanes3 = numplanes2 + trace_start->numedgedirs * other_start->numedgedirs * 2;
646 vec_t enterfrac = -1, leavefrac = 1, startdist, enddist, ie, f, imove, enterfrac2 = -1;
649 vec4_t newimpactplane;
650 const texture_t *hittexture = NULL;
651 vec_t startdepth = 1;
652 vec3_t startdepthnormal;
654 VectorClear(startdepthnormal);
655 Vector4Clear(newimpactplane);
657 // fast case for AABB vs compiled brushes (which begin with AABB planes and also have precomputed bevels for AABB collisions)
658 if (trace_start->isaabb && other_start->hasaabbplanes)
659 numplanes3 = numplanes2 = numplanes1;
661 // Separating Axis Theorem:
662 // if a supporting vector (plane normal) can be found that separates two
663 // objects, they are not colliding.
666 // reduce the size of one object to a point while enlarging the other to
667 // represent the space that point can not occupy.
669 // try every plane we can construct between the two brushes and measure
670 // the distance between them.
671 for (nplane = 0;nplane < numplanes3;nplane++)
673 if (nplane < numplanes1)
676 VectorCopy(other_start->planes[nplane2].normal, startplane);
677 VectorCopy(other_end->planes[nplane2].normal, endplane);
679 else if (nplane < numplanes2)
681 nplane2 = nplane - numplanes1;
682 VectorCopy(trace_start->planes[nplane2].normal, startplane);
683 VectorCopy(trace_end->planes[nplane2].normal, endplane);
687 // pick an edgedir from each brush and cross them
688 nplane2 = nplane - numplanes2;
689 nedge1 = nplane2 >> 1;
690 nedge2 = nedge1 / tracenumedgedirs;
691 nedge1 -= nedge2 * tracenumedgedirs;
694 CrossProduct(trace_start->edgedirs[nedge1].v, other_start->edgedirs[nedge2].v, startplane);
695 if (VectorLength2(startplane) < COLLISION_EDGECROSS_MINLENGTH2)
696 continue; // degenerate crossproduct
697 CrossProduct(trace_end->edgedirs[nedge1].v, other_end->edgedirs[nedge2].v, endplane);
698 if (VectorLength2(endplane) < COLLISION_EDGECROSS_MINLENGTH2)
699 continue; // degenerate crossproduct
703 CrossProduct(other_start->edgedirs[nedge2].v, trace_start->edgedirs[nedge1].v, startplane);
704 if (VectorLength2(startplane) < COLLISION_EDGECROSS_MINLENGTH2)
705 continue; // degenerate crossproduct
706 CrossProduct(other_end->edgedirs[nedge2].v, trace_end->edgedirs[nedge1].v, endplane);
707 if (VectorLength2(endplane) < COLLISION_EDGECROSS_MINLENGTH2)
708 continue; // degenerate crossproduct
710 VectorNormalize(startplane);
711 VectorNormalize(endplane);
713 startplane[3] = furthestplanedist_float(startplane, other_start->points, othernumpoints);
714 endplane[3] = furthestplanedist_float(startplane, other_end->points, othernumpoints);
715 startdist = nearestplanedist_float(startplane, trace_start->points, tracenumpoints) - startplane[3] - collision_startnudge.value;
716 enddist = nearestplanedist_float(endplane, trace_end->points, tracenumpoints) - endplane[3] - collision_endnudge.value;
717 //Con_Printf("%c%i: startdist = %f, enddist = %f, startdist / (startdist - enddist) = %f\n", nplane2 != nplane ? 'b' : 'a', nplane2, startdist, enddist, startdist / (startdist - enddist));
719 // aside from collisions, this is also used for error correction
720 if (startdist < collision_impactnudge.value && nplane < numplanes1 && (startdepth < startdist || startdepth == 1))
722 startdepth = startdist;
723 VectorCopy(startplane, startdepthnormal);
726 if (startdist >= -collision_impactnudge.value && enddist >= startdist)
728 if (startdist <= 0 && enddist <= 0)
730 if (startdist > enddist)
733 if (enddist >= collision_enternudge.value)
738 imove = 1 / (startdist - enddist);
739 f = (startdist - collision_enternudge.value) * imove;
742 // check if this will reduce the collision time range
745 // reduced collision time range
747 // if the collision time range is now empty, no collision
748 if (enterfrac > leavefrac)
750 // if the collision would be further away than the trace's
751 // existing collision data, we don't care about this
753 if (enterfrac > trace->realfraction)
755 // calculate the nudged fraction and impact normal we'll
756 // need if we accept this collision later
757 enterfrac2 = (startdist - collision_impactnudge.value) * imove;
758 ie = 1.0f - enterfrac;
759 newimpactplane[0] = startplane[0] * ie + endplane[0] * enterfrac;
760 newimpactplane[1] = startplane[1] * ie + endplane[1] * enterfrac;
761 newimpactplane[2] = startplane[2] * ie + endplane[2] * enterfrac;
762 newimpactplane[3] = startplane[3] * ie + endplane[3] * enterfrac;
763 if (nplane < numplanes1)
765 // use the plane from other
767 hitq3surfaceflags = other_start->planes[nplane2].q3surfaceflags;
768 hittexture = other_start->planes[nplane2].texture;
770 else if (nplane < numplanes2)
772 // use the plane from trace
773 nplane2 = nplane - numplanes1;
774 hitq3surfaceflags = trace_start->planes[nplane2].q3surfaceflags;
775 hittexture = trace_start->planes[nplane2].texture;
779 hitq3surfaceflags = other_start->q3surfaceflags;
780 hittexture = other_start->texture;
787 // moving out of brush
793 f = (startdist + collision_leavenudge.value) / (startdist - enddist);
796 // check if this will reduce the collision time range
799 // reduced collision time range
801 // if the collision time range is now empty, no collision
802 if (enterfrac > leavefrac)
809 // at this point we know the trace overlaps the brush because it was not
810 // rejected at any point in the loop above
812 // see if the trace started outside the brush or not
815 // started outside, and overlaps, therefore there is a collision here
816 // store out the impact information
817 if (trace->hitsupercontentsmask & other_start->supercontents)
819 trace->hitsupercontents = other_start->supercontents;
820 trace->hitq3surfaceflags = hitq3surfaceflags;
821 trace->hittexture = hittexture;
822 trace->realfraction = bound(0, enterfrac, 1);
823 trace->fraction = bound(0, enterfrac2, 1);
824 if (collision_prefernudgedfraction.integer)
825 trace->realfraction = trace->fraction;
826 VectorCopy(newimpactplane, trace->plane.normal);
827 trace->plane.dist = newimpactplane[3];
832 // started inside, update startsolid and friends
833 trace->startsupercontents |= other_start->supercontents;
834 if (trace->hitsupercontentsmask & other_start->supercontents)
836 trace->startsolid = true;
838 trace->allsolid = true;
839 VectorCopy(newimpactplane, trace->plane.normal);
840 trace->plane.dist = newimpactplane[3];
841 if (trace->startdepth > startdepth)
843 trace->startdepth = startdepth;
844 VectorCopy(startdepthnormal, trace->startdepthnormal);
850 // NOTE: start and end of each brush pair must have same numplanes/numpoints
851 void Collision_TraceLineBrushFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, const colbrushf_t *other_start, const colbrushf_t *other_end)
853 int nplane, hitq3surfaceflags = 0;
854 int numplanes = other_start->numplanes;
855 vec_t enterfrac = -1, leavefrac = 1, startdist, enddist, ie, f, imove, enterfrac2 = -1;
858 vec4_t newimpactplane;
859 const texture_t *hittexture = NULL;
860 vec_t startdepth = 1;
861 vec3_t startdepthnormal;
863 VectorClear(startdepthnormal);
864 Vector4Clear(newimpactplane);
866 // Separating Axis Theorem:
867 // if a supporting vector (plane normal) can be found that separates two
868 // objects, they are not colliding.
871 // reduce the size of one object to a point while enlarging the other to
872 // represent the space that point can not occupy.
874 // try every plane we can construct between the two brushes and measure
875 // the distance between them.
876 for (nplane = 0;nplane < numplanes;nplane++)
878 VectorCopy(other_start->planes[nplane].normal, startplane);
879 startplane[3] = other_start->planes[nplane].dist;
880 VectorCopy(other_end->planes[nplane].normal, endplane);
881 endplane[3] = other_end->planes[nplane].dist;
882 startdist = DotProduct(linestart, startplane) - startplane[3] - collision_startnudge.value;
883 enddist = DotProduct(lineend, endplane) - endplane[3] - collision_endnudge.value;
884 //Con_Printf("%c%i: startdist = %f, enddist = %f, startdist / (startdist - enddist) = %f\n", nplane2 != nplane ? 'b' : 'a', nplane2, startdist, enddist, startdist / (startdist - enddist));
886 // aside from collisions, this is also used for error correction
887 if (startdist < collision_impactnudge.value && (startdepth < startdist || startdepth == 1))
889 startdepth = startdist;
890 VectorCopy(startplane, startdepthnormal);
893 if (startdist >= -collision_impactnudge.value && enddist >= startdist)
895 if (startdist <= 0 && enddist <= 0)
897 if (startdist > enddist)
900 if (enddist >= collision_enternudge.value)
905 imove = 1 / (startdist - enddist);
906 f = (startdist - collision_enternudge.value) * imove;
909 // check if this will reduce the collision time range
912 // reduced collision time range
914 // if the collision time range is now empty, no collision
915 if (enterfrac > leavefrac)
917 // if the collision would be further away than the trace's
918 // existing collision data, we don't care about this
920 if (enterfrac > trace->realfraction)
922 // calculate the nudged fraction and impact normal we'll
923 // need if we accept this collision later
924 enterfrac2 = (startdist - collision_impactnudge.value) * imove;
925 ie = 1.0f - enterfrac;
926 newimpactplane[0] = startplane[0] * ie + endplane[0] * enterfrac;
927 newimpactplane[1] = startplane[1] * ie + endplane[1] * enterfrac;
928 newimpactplane[2] = startplane[2] * ie + endplane[2] * enterfrac;
929 newimpactplane[3] = startplane[3] * ie + endplane[3] * enterfrac;
930 hitq3surfaceflags = other_start->planes[nplane].q3surfaceflags;
931 hittexture = other_start->planes[nplane].texture;
937 // moving out of brush
943 f = (startdist + collision_leavenudge.value) / (startdist - enddist);
946 // check if this will reduce the collision time range
949 // reduced collision time range
951 // if the collision time range is now empty, no collision
952 if (enterfrac > leavefrac)
959 // at this point we know the trace overlaps the brush because it was not
960 // rejected at any point in the loop above
962 // see if the trace started outside the brush or not
965 // started outside, and overlaps, therefore there is a collision here
966 // store out the impact information
967 if (trace->hitsupercontentsmask & other_start->supercontents)
969 trace->hitsupercontents = other_start->supercontents;
970 trace->hitq3surfaceflags = hitq3surfaceflags;
971 trace->hittexture = hittexture;
972 trace->realfraction = bound(0, enterfrac, 1);
973 trace->fraction = bound(0, enterfrac2, 1);
974 if (collision_prefernudgedfraction.integer)
975 trace->realfraction = trace->fraction;
976 VectorCopy(newimpactplane, trace->plane.normal);
977 trace->plane.dist = newimpactplane[3];
982 // started inside, update startsolid and friends
983 trace->startsupercontents |= other_start->supercontents;
984 if (trace->hitsupercontentsmask & other_start->supercontents)
986 trace->startsolid = true;
988 trace->allsolid = true;
989 VectorCopy(newimpactplane, trace->plane.normal);
990 trace->plane.dist = newimpactplane[3];
991 if (trace->startdepth > startdepth)
993 trace->startdepth = startdepth;
994 VectorCopy(startdepthnormal, trace->startdepthnormal);
1000 qboolean Collision_PointInsideBrushFloat(const vec3_t point, const colbrushf_t *brush)
1003 const colplanef_t *plane;
1005 if (!BoxesOverlap(point, point, brush->mins, brush->maxs))
1007 for (nplane = 0, plane = brush->planes;nplane < brush->numplanes;nplane++, plane++)
1008 if (DotProduct(plane->normal, point) > plane->dist)
1013 void Collision_TracePointBrushFloat(trace_t *trace, const vec3_t point, const colbrushf_t *thatbrush)
1015 if (!Collision_PointInsideBrushFloat(point, thatbrush))
1018 trace->startsupercontents |= thatbrush->supercontents;
1019 if (trace->hitsupercontentsmask & thatbrush->supercontents)
1021 trace->startsolid = true;
1022 trace->allsolid = true;
1026 void Collision_SnapCopyPoints(int numpoints, const colpointf_t *in, colpointf_t *out, float fractionprecision, float invfractionprecision)
1029 for (i = 0;i < numpoints;i++)
1031 out[i].v[0] = floor(in[i].v[0] * fractionprecision + 0.5f) * invfractionprecision;
1032 out[i].v[1] = floor(in[i].v[1] * fractionprecision + 0.5f) * invfractionprecision;
1033 out[i].v[2] = floor(in[i].v[2] * fractionprecision + 0.5f) * invfractionprecision;
1037 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 stride, float *bbox6f, int supercontents, int q3surfaceflags, const texture_t *texture, const vec3_t segmentmins, const vec3_t segmentmaxs)
1040 colpointf_t points[3];
1041 colpointf_t edgedirs[3];
1042 colplanef_t planes[5];
1044 memset(&brush, 0, sizeof(brush));
1045 brush.isaabb = false;
1046 brush.hasaabbplanes = false;
1047 brush.numpoints = 3;
1048 brush.numedgedirs = 3;
1049 brush.numplanes = 5;
1050 brush.points = points;
1051 brush.edgedirs = edgedirs;
1052 brush.planes = planes;
1053 brush.supercontents = supercontents;
1054 brush.q3surfaceflags = q3surfaceflags;
1055 brush.texture = texture;
1056 for (i = 0;i < brush.numplanes;i++)
1058 brush.planes[i].q3surfaceflags = q3surfaceflags;
1059 brush.planes[i].texture = texture;
1064 cnt = (numtriangles + stride - 1) / stride;
1065 for(i = 0; i < cnt; ++i)
1067 if(BoxesOverlap(bbox6f + i * 6, bbox6f + i * 6 + 3, segmentmins, segmentmaxs))
1069 for(k = 0; k < stride; ++k)
1071 tri = i * stride + k;
1072 if(tri >= numtriangles)
1074 VectorCopy(vertex3f + element3i[tri * 3 + 0] * 3, points[0].v);
1075 VectorCopy(vertex3f + element3i[tri * 3 + 1] * 3, points[1].v);
1076 VectorCopy(vertex3f + element3i[tri * 3 + 2] * 3, points[2].v);
1077 Collision_SnapCopyPoints(brush.numpoints, points, points, COLLISION_SNAPSCALE, COLLISION_SNAP);
1078 Collision_CalcEdgeDirsForPolygonBrushFloat(&brush);
1079 Collision_CalcPlanesForPolygonBrushFloat(&brush);
1080 //Collision_PrintBrushAsQHull(&brush, "brush");
1081 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &brush, &brush);
1086 else if(stride == 0)
1088 for (i = 0;i < numtriangles;i++, element3i += 3)
1090 if (TriangleOverlapsBox(vertex3f + element3i[0]*3, vertex3f + element3i[1]*3, vertex3f + element3i[2]*3, segmentmins, segmentmaxs))
1092 VectorCopy(vertex3f + element3i[0] * 3, points[0].v);
1093 VectorCopy(vertex3f + element3i[1] * 3, points[1].v);
1094 VectorCopy(vertex3f + element3i[2] * 3, points[2].v);
1095 Collision_SnapCopyPoints(brush.numpoints, points, points, COLLISION_SNAPSCALE, COLLISION_SNAP);
1096 Collision_CalcEdgeDirsForPolygonBrushFloat(&brush);
1097 Collision_CalcPlanesForPolygonBrushFloat(&brush);
1098 //Collision_PrintBrushAsQHull(&brush, "brush");
1099 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &brush, &brush);
1105 for (i = 0;i < numtriangles;i++, element3i += 3)
1107 VectorCopy(vertex3f + element3i[0] * 3, points[0].v);
1108 VectorCopy(vertex3f + element3i[1] * 3, points[1].v);
1109 VectorCopy(vertex3f + element3i[2] * 3, points[2].v);
1110 Collision_SnapCopyPoints(brush.numpoints, points, points, COLLISION_SNAPSCALE, COLLISION_SNAP);
1111 Collision_CalcEdgeDirsForPolygonBrushFloat(&brush);
1112 Collision_CalcPlanesForPolygonBrushFloat(&brush);
1113 //Collision_PrintBrushAsQHull(&brush, "brush");
1114 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &brush, &brush);
1119 void Collision_TraceLineTriangleMeshFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, int numtriangles, const int *element3i, const float *vertex3f, int stride, float *bbox6f, int supercontents, int q3surfaceflags, const texture_t *texture, const vec3_t segmentmins, const vec3_t segmentmaxs)
1122 // FIXME: snap vertices?
1126 cnt = (numtriangles + stride - 1) / stride;
1127 for(i = 0; i < cnt; ++i)
1129 if(BoxesOverlap(bbox6f + i * 6, bbox6f + i * 6 + 3, segmentmins, segmentmaxs))
1131 for(k = 0; k < stride; ++k)
1133 tri = i * stride + k;
1134 if(tri >= numtriangles)
1136 Collision_TraceLineTriangleFloat(trace, linestart, lineend, vertex3f + element3i[tri * 3 + 0] * 3, vertex3f + element3i[tri * 3 + 1] * 3, vertex3f + element3i[tri * 3 + 2] * 3, supercontents, q3surfaceflags, texture);
1143 for (i = 0;i < numtriangles;i++, element3i += 3)
1144 Collision_TraceLineTriangleFloat(trace, linestart, lineend, vertex3f + element3i[0] * 3, vertex3f + element3i[1] * 3, vertex3f + element3i[2] * 3, supercontents, q3surfaceflags, texture);
1148 void Collision_TraceBrushTriangleFloat(trace_t *trace, const colbrushf_t *thisbrush_start, const colbrushf_t *thisbrush_end, const float *v0, const float *v1, const float *v2, int supercontents, int q3surfaceflags, const texture_t *texture)
1151 colpointf_t points[3];
1152 colpointf_t edgedirs[3];
1153 colplanef_t planes[5];
1155 memset(&brush, 0, sizeof(brush));
1156 brush.isaabb = false;
1157 brush.hasaabbplanes = false;
1158 brush.numpoints = 3;
1159 brush.numedgedirs = 3;
1160 brush.numplanes = 5;
1161 brush.points = points;
1162 brush.edgedirs = edgedirs;
1163 brush.planes = planes;
1164 brush.supercontents = supercontents;
1165 brush.q3surfaceflags = q3surfaceflags;
1166 brush.texture = texture;
1167 for (i = 0;i < brush.numplanes;i++)
1169 brush.planes[i].q3surfaceflags = q3surfaceflags;
1170 brush.planes[i].texture = texture;
1172 VectorCopy(v0, points[0].v);
1173 VectorCopy(v1, points[1].v);
1174 VectorCopy(v2, points[2].v);
1175 Collision_SnapCopyPoints(brush.numpoints, points, points, COLLISION_SNAPSCALE, COLLISION_SNAP);
1176 Collision_CalcEdgeDirsForPolygonBrushFloat(&brush);
1177 Collision_CalcPlanesForPolygonBrushFloat(&brush);
1178 //Collision_PrintBrushAsQHull(&brush, "brush");
1179 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &brush, &brush);
1182 void Collision_BrushForBox(colboxbrushf_t *boxbrush, const vec3_t mins, const vec3_t maxs, int supercontents, int q3surfaceflags, const texture_t *texture)
1185 memset(boxbrush, 0, sizeof(*boxbrush));
1186 boxbrush->brush.isaabb = true;
1187 boxbrush->brush.hasaabbplanes = true;
1188 boxbrush->brush.points = boxbrush->points;
1189 boxbrush->brush.edgedirs = boxbrush->edgedirs;
1190 boxbrush->brush.planes = boxbrush->planes;
1191 boxbrush->brush.supercontents = supercontents;
1192 boxbrush->brush.q3surfaceflags = q3surfaceflags;
1193 boxbrush->brush.texture = texture;
1194 if (VectorCompare(mins, maxs))
1197 boxbrush->brush.numpoints = 1;
1198 boxbrush->brush.numedgedirs = 0;
1199 boxbrush->brush.numplanes = 0;
1200 VectorCopy(mins, boxbrush->brush.points[0].v);
1204 boxbrush->brush.numpoints = 8;
1205 boxbrush->brush.numedgedirs = 3;
1206 boxbrush->brush.numplanes = 6;
1207 // there are 8 points on a box
1208 // there are 3 edgedirs on a box (both signs are tested in collision)
1209 // there are 6 planes on a box
1210 VectorSet(boxbrush->brush.points[0].v, mins[0], mins[1], mins[2]);
1211 VectorSet(boxbrush->brush.points[1].v, maxs[0], mins[1], mins[2]);
1212 VectorSet(boxbrush->brush.points[2].v, mins[0], maxs[1], mins[2]);
1213 VectorSet(boxbrush->brush.points[3].v, maxs[0], maxs[1], mins[2]);
1214 VectorSet(boxbrush->brush.points[4].v, mins[0], mins[1], maxs[2]);
1215 VectorSet(boxbrush->brush.points[5].v, maxs[0], mins[1], maxs[2]);
1216 VectorSet(boxbrush->brush.points[6].v, mins[0], maxs[1], maxs[2]);
1217 VectorSet(boxbrush->brush.points[7].v, maxs[0], maxs[1], maxs[2]);
1218 VectorSet(boxbrush->brush.edgedirs[0].v, 1, 0, 0);
1219 VectorSet(boxbrush->brush.edgedirs[1].v, 0, 1, 0);
1220 VectorSet(boxbrush->brush.edgedirs[2].v, 0, 0, 1);
1221 VectorSet(boxbrush->brush.planes[0].normal, -1, 0, 0);boxbrush->brush.planes[0].dist = -mins[0];
1222 VectorSet(boxbrush->brush.planes[1].normal, 1, 0, 0);boxbrush->brush.planes[1].dist = maxs[0];
1223 VectorSet(boxbrush->brush.planes[2].normal, 0, -1, 0);boxbrush->brush.planes[2].dist = -mins[1];
1224 VectorSet(boxbrush->brush.planes[3].normal, 0, 1, 0);boxbrush->brush.planes[3].dist = maxs[1];
1225 VectorSet(boxbrush->brush.planes[4].normal, 0, 0, -1);boxbrush->brush.planes[4].dist = -mins[2];
1226 VectorSet(boxbrush->brush.planes[5].normal, 0, 0, 1);boxbrush->brush.planes[5].dist = maxs[2];
1227 for (i = 0;i < 6;i++)
1229 boxbrush->brush.planes[i].q3surfaceflags = q3surfaceflags;
1230 boxbrush->brush.planes[i].texture = texture;
1233 boxbrush->brush.supercontents = supercontents;
1234 boxbrush->brush.q3surfaceflags = q3surfaceflags;
1235 boxbrush->brush.texture = texture;
1236 VectorSet(boxbrush->brush.mins, mins[0] - 1, mins[1] - 1, mins[2] - 1);
1237 VectorSet(boxbrush->brush.maxs, maxs[0] + 1, maxs[1] + 1, maxs[2] + 1);
1238 Collision_ValidateBrush(&boxbrush->brush);
1241 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)
1243 colboxbrushf_t boxbrush, thisbrush_start, thisbrush_end;
1244 vec3_t startmins, startmaxs, endmins, endmaxs;
1246 // create brushes for the collision
1247 VectorAdd(start, mins, startmins);
1248 VectorAdd(start, maxs, startmaxs);
1249 VectorAdd(end, mins, endmins);
1250 VectorAdd(end, maxs, endmaxs);
1251 Collision_BrushForBox(&boxbrush, cmins, cmaxs, supercontents, q3surfaceflags, texture);
1252 Collision_BrushForBox(&thisbrush_start, startmins, startmaxs, 0, 0, NULL);
1253 Collision_BrushForBox(&thisbrush_end, endmins, endmaxs, 0, 0, NULL);
1255 memset(trace, 0, sizeof(trace_t));
1256 trace->hitsupercontentsmask = hitsupercontentsmask;
1257 trace->fraction = 1;
1258 trace->realfraction = 1;
1259 trace->allsolid = true;
1260 Collision_TraceBrushBrushFloat(trace, &thisbrush_start.brush, &thisbrush_end.brush, &boxbrush.brush, &boxbrush.brush);
1263 //pseudocode for detecting line/sphere overlap without calculating an impact point
1264 //linesphereorigin = sphereorigin - linestart;linediff = lineend - linestart;linespherefrac = DotProduct(linesphereorigin, linediff) / DotProduct(linediff, linediff);return VectorLength2(linesphereorigin - bound(0, linespherefrac, 1) * linediff) >= sphereradius*sphereradius;
1266 // LordHavoc: currently unused, but tested
1267 // note: this can be used for tracing a moving sphere vs a stationary sphere,
1268 // by simply adding the moving sphere's radius to the sphereradius parameter,
1269 // all the results are correct (impactpoint, impactnormal, and fraction)
1270 float Collision_ClipTrace_Line_Sphere(double *linestart, double *lineend, double *sphereorigin, double sphereradius, double *impactpoint, double *impactnormal)
1272 double dir[3], scale, v[3], deviationdist, impactdist, linelength;
1273 // make sure the impactpoint and impactnormal are valid even if there is
1275 VectorCopy(lineend, impactpoint);
1276 VectorClear(impactnormal);
1277 // calculate line direction
1278 VectorSubtract(lineend, linestart, dir);
1279 // normalize direction
1280 linelength = VectorLength(dir);
1283 scale = 1.0 / linelength;
1284 VectorScale(dir, scale, dir);
1286 // this dotproduct calculates the distance along the line at which the
1287 // sphere origin is (nearest point to the sphere origin on the line)
1288 impactdist = DotProduct(sphereorigin, dir) - DotProduct(linestart, dir);
1289 // calculate point on line at that distance, and subtract the
1290 // sphereorigin from it, so we have a vector to measure for the distance
1291 // of the line from the sphereorigin (deviation, how off-center it is)
1292 VectorMA(linestart, impactdist, dir, v);
1293 VectorSubtract(v, sphereorigin, v);
1294 deviationdist = VectorLength2(v);
1295 // if outside the radius, it's a miss for sure
1296 // (we do this comparison using squared radius to avoid a sqrt)
1297 if (deviationdist > sphereradius*sphereradius)
1298 return 1; // miss (off to the side)
1299 // nudge back to find the correct impact distance
1300 impactdist -= sphereradius - deviationdist/sphereradius;
1301 if (impactdist >= linelength)
1302 return 1; // miss (not close enough)
1304 return 1; // miss (linestart is past or inside sphere)
1305 // calculate new impactpoint
1306 VectorMA(linestart, impactdist, dir, impactpoint);
1307 // calculate impactnormal (surface normal at point of impact)
1308 VectorSubtract(impactpoint, sphereorigin, impactnormal);
1309 // normalize impactnormal
1310 VectorNormalize(impactnormal);
1311 // return fraction of movement distance
1312 return impactdist / linelength;
1315 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, const texture_t *texture)
1319 float d1, d2, d, f, impact[3], edgenormal[3], faceplanenormal[3], faceplanedist, faceplanenormallength2, edge01[3], edge21[3], edge02[3];
1321 // this function executes:
1322 // 32 ops when line starts behind triangle
1323 // 38 ops when line ends infront of triangle
1324 // 43 ops when line fraction is already closer than this triangle
1325 // 72 ops when line is outside edge 01
1326 // 92 ops when line is outside edge 21
1327 // 115 ops when line is outside edge 02
1328 // 123 ops when line impacts triangle and updates trace results
1330 // this code is designed for clockwise triangles, conversion to
1331 // counterclockwise would require swapping some things around...
1332 // it is easier to simply swap the point0 and point2 parameters to this
1333 // function when calling it than it is to rewire the internals.
1335 // calculate the faceplanenormal of the triangle, this represents the front side
1337 VectorSubtract(point0, point1, edge01);
1338 VectorSubtract(point2, point1, edge21);
1339 CrossProduct(edge01, edge21, faceplanenormal);
1340 // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
1342 faceplanenormallength2 = DotProduct(faceplanenormal, faceplanenormal);
1343 if (faceplanenormallength2 < 0.0001f)
1345 // calculate the distance
1347 faceplanedist = DotProduct(point0, faceplanenormal);
1349 // if start point is on the back side there is no collision
1350 // (we don't care about traces going through the triangle the wrong way)
1352 // calculate the start distance
1354 d1 = DotProduct(faceplanenormal, linestart);
1355 if (d1 <= faceplanedist)
1358 // calculate the end distance
1360 d2 = DotProduct(faceplanenormal, lineend);
1361 // if both are in front, there is no collision
1362 if (d2 >= faceplanedist)
1365 // from here on we know d1 is >= 0 and d2 is < 0
1366 // this means the line starts infront and ends behind, passing through it
1368 // calculate the recipricol of the distance delta,
1369 // so we can use it multiple times cheaply (instead of division)
1371 d = 1.0f / (d1 - d2);
1372 // calculate the impact fraction by taking the start distance (> 0)
1373 // and subtracting the face plane distance (this is the distance of the
1374 // triangle along that same normal)
1375 // then multiply by the recipricol distance delta
1377 f = (d1 - faceplanedist) * d;
1378 // skip out if this impact is further away than previous ones
1380 if (f > trace->realfraction)
1382 // calculate the perfect impact point for classification of insidedness
1384 impact[0] = linestart[0] + f * (lineend[0] - linestart[0]);
1385 impact[1] = linestart[1] + f * (lineend[1] - linestart[1]);
1386 impact[2] = linestart[2] + f * (lineend[2] - linestart[2]);
1388 // calculate the edge normal and reject if impact is outside triangle
1389 // (an edge normal faces away from the triangle, to get the desired normal
1390 // a crossproduct with the faceplanenormal is used, and because of the way
1391 // the insidedness comparison is written it does not need to be normalized)
1393 // first use the two edges from the triangle plane math
1394 // the other edge only gets calculated if the point survives that long
1397 CrossProduct(edge01, faceplanenormal, edgenormal);
1398 if (DotProduct(impact, edgenormal) > DotProduct(point1, edgenormal))
1402 CrossProduct(faceplanenormal, edge21, edgenormal);
1403 if (DotProduct(impact, edgenormal) > DotProduct(point2, edgenormal))
1407 VectorSubtract(point0, point2, edge02);
1408 CrossProduct(faceplanenormal, edge02, edgenormal);
1409 if (DotProduct(impact, edgenormal) > DotProduct(point0, edgenormal))
1414 // store the new trace fraction
1415 trace->realfraction = f;
1417 // calculate a nudged fraction to keep it out of the surface
1418 // (the main fraction remains perfect)
1419 trace->fraction = f - collision_impactnudge.value * d;
1421 if (collision_prefernudgedfraction.integer)
1422 trace->realfraction = trace->fraction;
1424 // store the new trace plane (because collisions only happen from
1425 // the front this is always simply the triangle normal, never flipped)
1426 d = 1.0 / sqrt(faceplanenormallength2);
1427 VectorScale(faceplanenormal, d, trace->plane.normal);
1428 trace->plane.dist = faceplanedist * d;
1430 trace->hitsupercontents = supercontents;
1431 trace->hitq3surfaceflags = q3surfaceflags;
1432 trace->hittexture = texture;
1434 float d1, d2, d, f, fnudged, impact[3], edgenormal[3], faceplanenormal[3], faceplanedist, edge[3];
1436 // this code is designed for clockwise triangles, conversion to
1437 // counterclockwise would require swapping some things around...
1438 // it is easier to simply swap the point0 and point2 parameters to this
1439 // function when calling it than it is to rewire the internals.
1441 // calculate the unnormalized faceplanenormal of the triangle,
1442 // this represents the front side
1443 TriangleNormal(point0, point1, point2, faceplanenormal);
1444 // there's no point in processing a degenerate triangle
1445 // (GIGO - Garbage In, Garbage Out)
1446 if (DotProduct(faceplanenormal, faceplanenormal) < 0.0001f)
1448 // calculate the unnormalized distance
1449 faceplanedist = DotProduct(point0, faceplanenormal);
1451 // calculate the unnormalized start distance
1452 d1 = DotProduct(faceplanenormal, linestart) - faceplanedist;
1453 // if start point is on the back side there is no collision
1454 // (we don't care about traces going through the triangle the wrong way)
1458 // calculate the unnormalized end distance
1459 d2 = DotProduct(faceplanenormal, lineend) - faceplanedist;
1460 // if both are in front, there is no collision
1464 // from here on we know d1 is >= 0 and d2 is < 0
1465 // this means the line starts infront and ends behind, passing through it
1467 // calculate the recipricol of the distance delta,
1468 // so we can use it multiple times cheaply (instead of division)
1469 d = 1.0f / (d1 - d2);
1470 // calculate the impact fraction by taking the start distance (> 0)
1471 // and subtracting the face plane distance (this is the distance of the
1472 // triangle along that same normal)
1473 // then multiply by the recipricol distance delta
1475 // skip out if this impact is further away than previous ones
1476 if (f > trace->realfraction)
1478 // calculate the perfect impact point for classification of insidedness
1479 impact[0] = linestart[0] + f * (lineend[0] - linestart[0]);
1480 impact[1] = linestart[1] + f * (lineend[1] - linestart[1]);
1481 impact[2] = linestart[2] + f * (lineend[2] - linestart[2]);
1483 // calculate the edge normal and reject if impact is outside triangle
1484 // (an edge normal faces away from the triangle, to get the desired normal
1485 // a crossproduct with the faceplanenormal is used, and because of the way
1486 // the insidedness comparison is written it does not need to be normalized)
1488 VectorSubtract(point2, point0, edge);
1489 CrossProduct(edge, faceplanenormal, edgenormal);
1490 if (DotProduct(impact, edgenormal) > DotProduct(point0, edgenormal))
1493 VectorSubtract(point0, point1, edge);
1494 CrossProduct(edge, faceplanenormal, edgenormal);
1495 if (DotProduct(impact, edgenormal) > DotProduct(point1, edgenormal))
1498 VectorSubtract(point1, point2, edge);
1499 CrossProduct(edge, faceplanenormal, edgenormal);
1500 if (DotProduct(impact, edgenormal) > DotProduct(point2, edgenormal))
1503 // store the new trace fraction
1504 trace->realfraction = bound(0, f, 1);
1506 // store the new trace plane (because collisions only happen from
1507 // the front this is always simply the triangle normal, never flipped)
1508 VectorNormalize(faceplanenormal);
1509 VectorCopy(faceplanenormal, trace->plane.normal);
1510 trace->plane.dist = DotProduct(point0, faceplanenormal);
1512 // calculate the normalized start and end distances
1513 d1 = DotProduct(trace->plane.normal, linestart) - trace->plane.dist;
1514 d2 = DotProduct(trace->plane.normal, lineend) - trace->plane.dist;
1516 // calculate a nudged fraction to keep it out of the surface
1517 // (the main fraction remains perfect)
1518 fnudged = (d1 - collision_impactnudge.value) / (d1 - d2);
1519 trace->fraction = bound(0, fnudged, 1);
1521 // store the new trace endpos
1522 // not needed, it's calculated later when the trace is finished
1523 //trace->endpos[0] = linestart[0] + fnudged * (lineend[0] - linestart[0]);
1524 //trace->endpos[1] = linestart[1] + fnudged * (lineend[1] - linestart[1]);
1525 //trace->endpos[2] = linestart[2] + fnudged * (lineend[2] - linestart[2]);
1526 trace->hitsupercontents = supercontents;
1527 trace->hitq3surfaceflags = q3surfaceflags;
1528 trace->hittexture = texture;
1532 typedef struct colbspnode_s
1535 struct colbspnode_s *children[2];
1536 // the node is reallocated or split if max is reached
1539 colbrushf_t **colbrushflist;
1542 //colbrushd_t **colbrushdlist;
1546 typedef struct colbsp_s
1549 colbspnode_t *nodes;
1553 colbsp_t *Collision_CreateCollisionBSP(mempool_t *mempool)
1556 bsp = (colbsp_t *)Mem_Alloc(mempool, sizeof(colbsp_t));
1557 bsp->mempool = mempool;
1558 bsp->nodes = (colbspnode_t *)Mem_Alloc(bsp->mempool, sizeof(colbspnode_t));
1562 void Collision_FreeCollisionBSPNode(colbspnode_t *node)
1564 if (node->children[0])
1565 Collision_FreeCollisionBSPNode(node->children[0]);
1566 if (node->children[1])
1567 Collision_FreeCollisionBSPNode(node->children[1]);
1568 while (--node->numcolbrushf)
1569 Mem_Free(node->colbrushflist[node->numcolbrushf]);
1570 //while (--node->numcolbrushd)
1571 // Mem_Free(node->colbrushdlist[node->numcolbrushd]);
1575 void Collision_FreeCollisionBSP(colbsp_t *bsp)
1577 Collision_FreeCollisionBSPNode(bsp->nodes);
1581 void Collision_BoundingBoxOfBrushTraceSegment(const colbrushf_t *start, const colbrushf_t *end, vec3_t mins, vec3_t maxs, float startfrac, float endfrac)
1584 colpointf_t *ps, *pe;
1585 float tempstart[3], tempend[3];
1586 VectorLerp(start->points[0].v, startfrac, end->points[0].v, mins);
1587 VectorCopy(mins, maxs);
1588 for (i = 0, ps = start->points, pe = end->points;i < start->numpoints;i++, ps++, pe++)
1590 VectorLerp(ps->v, startfrac, pe->v, tempstart);
1591 VectorLerp(ps->v, endfrac, pe->v, tempend);
1592 mins[0] = min(mins[0], min(tempstart[0], tempend[0]));
1593 mins[1] = min(mins[1], min(tempstart[1], tempend[1]));
1594 mins[2] = min(mins[2], min(tempstart[2], tempend[2]));
1595 maxs[0] = min(maxs[0], min(tempstart[0], tempend[0]));
1596 maxs[1] = min(maxs[1], min(tempstart[1], tempend[1]));
1597 maxs[2] = min(maxs[2], min(tempstart[2], tempend[2]));
1607 //===========================================
1609 void Collision_ClipToGenericEntity(trace_t *trace, dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, const vec3_t bodymins, const vec3_t bodymaxs, int bodysupercontents, matrix4x4_t *matrix, matrix4x4_t *inversematrix, const vec3_t start, const vec3_t mins, const vec3_t maxs, const vec3_t end, int hitsupercontentsmask)
1611 float starttransformed[3], endtransformed[3];
1613 memset(trace, 0, sizeof(*trace));
1614 trace->fraction = trace->realfraction = 1;
1616 Matrix4x4_Transform(inversematrix, start, starttransformed);
1617 Matrix4x4_Transform(inversematrix, end, endtransformed);
1618 #if COLLISIONPARANOID >= 3
1619 Con_Printf("trans(%f %f %f -> %f %f %f, %f %f %f -> %f %f %f)", start[0], start[1], start[2], starttransformed[0], starttransformed[1], starttransformed[2], end[0], end[1], end[2], endtransformed[0], endtransformed[1], endtransformed[2]);
1622 if (model && model->TraceBox)
1623 model->TraceBox(model, frameblend, skeleton, trace, starttransformed, mins, maxs, endtransformed, hitsupercontentsmask);
1625 Collision_ClipTrace_Box(trace, bodymins, bodymaxs, starttransformed, mins, maxs, endtransformed, hitsupercontentsmask, bodysupercontents, 0, NULL);
1626 trace->fraction = bound(0, trace->fraction, 1);
1627 trace->realfraction = bound(0, trace->realfraction, 1);
1629 VectorLerp(start, trace->fraction, end, trace->endpos);
1631 // NOTE: this relies on plane.dist being directly after plane.normal
1632 Matrix4x4_TransformPositivePlane(matrix, trace->plane.normal[0], trace->plane.normal[1], trace->plane.normal[2], trace->plane.dist, trace->plane.normal);
1635 void Collision_ClipToWorld(trace_t *trace, dp_model_t *model, const vec3_t start, const vec3_t mins, const vec3_t maxs, const vec3_t end, int hitsupercontents)
1637 memset(trace, 0, sizeof(*trace));
1638 trace->fraction = trace->realfraction = 1;
1639 if (model && model->TraceBox)
1640 model->TraceBox(model, NULL, NULL, trace, start, mins, maxs, end, hitsupercontents);
1641 trace->fraction = bound(0, trace->fraction, 1);
1642 trace->realfraction = bound(0, trace->realfraction, 1);
1643 VectorLerp(start, trace->fraction, end, trace->endpos);
1646 void Collision_ClipLineToGenericEntity(trace_t *trace, dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, const vec3_t bodymins, const vec3_t bodymaxs, int bodysupercontents, matrix4x4_t *matrix, matrix4x4_t *inversematrix, const vec3_t start, const vec3_t end, int hitsupercontentsmask)
1648 float starttransformed[3], endtransformed[3];
1650 memset(trace, 0, sizeof(*trace));
1651 trace->fraction = trace->realfraction = 1;
1653 Matrix4x4_Transform(inversematrix, start, starttransformed);
1654 Matrix4x4_Transform(inversematrix, end, endtransformed);
1655 #if COLLISIONPARANOID >= 3
1656 Con_Printf("trans(%f %f %f -> %f %f %f, %f %f %f -> %f %f %f)", start[0], start[1], start[2], starttransformed[0], starttransformed[1], starttransformed[2], end[0], end[1], end[2], endtransformed[0], endtransformed[1], endtransformed[2]);
1659 if (model && model->TraceLine)
1660 model->TraceLine(model, frameblend, skeleton, trace, starttransformed, endtransformed, hitsupercontentsmask);
1662 Collision_ClipTrace_Box(trace, bodymins, bodymaxs, starttransformed, vec3_origin, vec3_origin, endtransformed, hitsupercontentsmask, bodysupercontents, 0, NULL);
1663 trace->fraction = bound(0, trace->fraction, 1);
1664 trace->realfraction = bound(0, trace->realfraction, 1);
1666 VectorLerp(start, trace->fraction, end, trace->endpos);
1668 // NOTE: this relies on plane.dist being directly after plane.normal
1669 Matrix4x4_TransformPositivePlane(matrix, trace->plane.normal[0], trace->plane.normal[1], trace->plane.normal[2], trace->plane.dist, trace->plane.normal);
1672 void Collision_ClipLineToWorld(trace_t *trace, dp_model_t *model, const vec3_t start, const vec3_t end, int hitsupercontents)
1674 memset(trace, 0, sizeof(*trace));
1675 trace->fraction = trace->realfraction = 1;
1676 if (model && model->TraceLine)
1677 model->TraceLine(model, NULL, NULL, trace, start, end, hitsupercontents);
1678 trace->fraction = bound(0, trace->fraction, 1);
1679 trace->realfraction = bound(0, trace->realfraction, 1);
1680 VectorLerp(start, trace->fraction, end, trace->endpos);
1683 void Collision_ClipPointToGenericEntity(trace_t *trace, dp_model_t *model, const frameblend_t *frameblend, const skeleton_t *skeleton, const vec3_t bodymins, const vec3_t bodymaxs, int bodysupercontents, matrix4x4_t *matrix, matrix4x4_t *inversematrix, const vec3_t start, int hitsupercontentsmask)
1685 float starttransformed[3];
1687 memset(trace, 0, sizeof(*trace));
1688 trace->fraction = trace->realfraction = 1;
1690 Matrix4x4_Transform(inversematrix, start, starttransformed);
1691 #if COLLISIONPARANOID >= 3
1692 Con_Printf("trans(%f %f %f -> %f %f %f)", start[0], start[1], start[2], starttransformed[0], starttransformed[1], starttransformed[2]);
1695 if (model && model->TracePoint)
1696 model->TracePoint(model, NULL, NULL, trace, starttransformed, hitsupercontentsmask);
1698 Collision_ClipTrace_Point(trace, bodymins, bodymaxs, starttransformed, hitsupercontentsmask, bodysupercontents, 0, NULL);
1700 VectorCopy(start, trace->endpos);
1702 // NOTE: this relies on plane.dist being directly after plane.normal
1703 Matrix4x4_TransformPositivePlane(matrix, trace->plane.normal[0], trace->plane.normal[1], trace->plane.normal[2], trace->plane.dist, trace->plane.normal);
1706 void Collision_ClipPointToWorld(trace_t *trace, dp_model_t *model, const vec3_t start, int hitsupercontents)
1708 memset(trace, 0, sizeof(*trace));
1709 trace->fraction = trace->realfraction = 1;
1710 if (model && model->TracePoint)
1711 model->TracePoint(model, NULL, NULL, trace, start, hitsupercontents);
1712 VectorCopy(start, trace->endpos);
1715 void Collision_CombineTraces(trace_t *cliptrace, const trace_t *trace, void *touch, qboolean isbmodel)
1717 // take the 'best' answers from the new trace and combine with existing data
1718 if (trace->allsolid)
1719 cliptrace->allsolid = true;
1720 if (trace->startsolid)
1723 cliptrace->bmodelstartsolid = true;
1724 cliptrace->startsolid = true;
1725 if (cliptrace->realfraction == 1)
1726 cliptrace->ent = touch;
1727 if (cliptrace->startdepth > trace->startdepth)
1729 cliptrace->startdepth = trace->startdepth;
1730 VectorCopy(trace->startdepthnormal, cliptrace->startdepthnormal);
1733 // don't set this except on the world, because it can easily confuse
1734 // monsters underwater if there's a bmodel involved in the trace
1735 // (inopen && inwater is how they check water visibility)
1736 //if (trace->inopen)
1737 // cliptrace->inopen = true;
1739 cliptrace->inwater = true;
1740 if ((trace->realfraction <= cliptrace->realfraction) && (VectorLength2(trace->plane.normal) > 0))
1742 cliptrace->fraction = trace->fraction;
1743 cliptrace->realfraction = trace->realfraction;
1744 VectorCopy(trace->endpos, cliptrace->endpos);
1745 cliptrace->plane = trace->plane;
1746 cliptrace->ent = touch;
1747 cliptrace->hitsupercontents = trace->hitsupercontents;
1748 cliptrace->hitq3surfaceflags = trace->hitq3surfaceflags;
1749 cliptrace->hittexture = trace->hittexture;
1751 cliptrace->startsupercontents |= trace->startsupercontents;
1754 void Collision_ShortenTrace(trace_t *trace, float shorten_factor, const vec3_t end)
1756 // now undo our moving end 1 qu farther...
1757 trace->fraction = bound(trace->fraction, trace->fraction / shorten_factor - 1e-6, 1); // we subtract 1e-6 to guard for roundoff errors
1758 trace->realfraction = bound(trace->realfraction, trace->realfraction / shorten_factor - 1e-6, 1); // we subtract 1e-6 to guard for roundoff errors
1759 if(trace->fraction >= 1) // trace would NOT hit if not expanded!
1761 trace->fraction = 1;
1762 trace->realfraction = 1;
1763 VectorCopy(end, trace->endpos);
1764 memset(&trace->plane, 0, sizeof(trace->plane));
1766 trace->hitsupercontentsmask = 0;
1767 trace->hitsupercontents = 0;
1768 trace->hitq3surfaceflags = 0;
1769 trace->hittexture = NULL;