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)"};
19 void Collision_Init (void)
21 Cvar_RegisterVariable(&collision_impactnudge);
22 Cvar_RegisterVariable(&collision_startnudge);
23 Cvar_RegisterVariable(&collision_endnudge);
24 Cvar_RegisterVariable(&collision_enternudge);
25 Cvar_RegisterVariable(&collision_leavenudge);
26 Cvar_RegisterVariable(&collision_prefernudgedfraction);
42 void Collision_PrintBrushAsQHull(colbrushf_t *brush, const char *name)
45 Con_Printf("3 %s\n%i\n", name, brush->numpoints);
46 for (i = 0;i < brush->numpoints;i++)
47 Con_Printf("%f %f %f\n", brush->points[i].v[0], brush->points[i].v[1], brush->points[i].v[2]);
49 Con_Printf("4\n%i\n", brush->numplanes);
50 for (i = 0;i < brush->numplanes;i++)
51 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);
54 void Collision_ValidateBrush(colbrushf_t *brush)
56 int j, k, pointsoffplanes, pointonplanes, pointswithinsufficientplanes, printbrush;
59 if (!brush->numpoints)
61 Con_Print("Collision_ValidateBrush: brush with no points!\n");
65 // it's ok for a brush to have one point and no planes...
66 if (brush->numplanes == 0 && brush->numpoints != 1)
68 Con_Print("Collision_ValidateBrush: brush with no planes and more than one point!\n");
75 pointswithinsufficientplanes = 0;
76 for (k = 0;k < brush->numplanes;k++)
77 if (DotProduct(brush->planes[k].normal, brush->planes[k].normal) < 0.0001f)
78 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);
79 for (j = 0;j < brush->numpoints;j++)
82 for (k = 0;k < brush->numplanes;k++)
84 d = DotProduct(brush->points[j].v, brush->planes[k].normal) - brush->planes[k].dist;
85 if (d > COLLISION_PLANE_DIST_EPSILON)
87 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);
90 if (fabs(d) > COLLISION_PLANE_DIST_EPSILON)
95 if (pointonplanes < 3)
96 pointswithinsufficientplanes++;
98 if (pointswithinsufficientplanes)
100 Con_Print("Collision_ValidateBrush: some points have insufficient planes, every point must be on at least 3 planes to form a corner.\n");
103 if (pointsoffplanes == 0) // all points are on all planes
105 Con_Print("Collision_ValidateBrush: all points lie on all planes (degenerate, no brush volume!)\n");
110 Collision_PrintBrushAsQHull(brush, "unnamed");
113 float nearestplanedist_float(const float *normal, const colpointf_t *points, int numpoints)
115 float dist, bestdist;
118 bestdist = DotProduct(points->v, normal);
122 dist = DotProduct(points->v, normal);
123 bestdist = min(bestdist, dist);
129 float furthestplanedist_float(const float *normal, const colpointf_t *points, int numpoints)
131 float dist, bestdist;
134 bestdist = DotProduct(points->v, normal);
138 dist = DotProduct(points->v, normal);
139 bestdist = max(bestdist, dist);
145 void Collision_CalcEdgeDirsForPolygonBrushFloat(colbrushf_t *brush)
148 for (i = 0, j = brush->numpoints - 1;i < brush->numpoints;j = i, i++)
149 VectorSubtract(brush->points[i].v, brush->points[j].v, brush->edgedirs[j].v);
152 colbrushf_t *Collision_NewBrushFromPlanes(mempool_t *mempool, int numoriginalplanes, const colplanef_t *originalplanes, int supercontents, int q3surfaceflags, texture_t *texture, int hasaabbplanes)
154 // TODO: planesbuf could be replaced by a remapping table
155 int j, k, l, m, w, xyzflags;
156 int numpointsbuf = 0, maxpointsbuf = 256, numedgedirsbuf = 0, maxedgedirsbuf = 256, numplanesbuf = 0, maxplanesbuf = 256, numelementsbuf = 0, maxelementsbuf = 256;
160 colpointf_t pointsbuf[256];
161 colpointf_t edgedirsbuf[256];
162 colplanef_t planesbuf[256];
163 int elementsbuf[1024];
164 int polypointbuf[256];
169 // enable these if debugging to avoid seeing garbage in unused data-
170 memset(pointsbuf, 0, sizeof(pointsbuf));
171 memset(edgedirsbuf, 0, sizeof(edgedirsbuf));
172 memset(planesbuf, 0, sizeof(planesbuf));
173 memset(elementsbuf, 0, sizeof(elementsbuf));
174 memset(polypointbuf, 0, sizeof(polypointbuf));
175 memset(p, 0, sizeof(p));
178 // check if there are too many planes and skip the brush
179 if (numoriginalplanes >= maxplanesbuf)
181 Con_DPrint("Collision_NewBrushFromPlanes: failed to build collision brush: too many planes for buffer\n");
185 // figure out how large a bounding box we need to properly compute this brush
187 for (j = 0;j < numoriginalplanes;j++)
188 maxdist = max(maxdist, fabs(originalplanes[j].dist));
189 // now make it large enough to enclose the entire brush, and round it off to a reasonable multiple of 1024
190 maxdist = floor(maxdist * (4.0 / 1024.0) + 2) * 1024.0;
191 // construct a collision brush (points, planes, and renderable mesh) from
192 // a set of planes, this also optimizes out any unnecessary planes (ones
193 // whose polygon is clipped away by the other planes)
194 for (j = 0;j < numoriginalplanes;j++)
197 VectorCopy(originalplanes[j].normal, planesbuf[numplanesbuf].normal);
198 planesbuf[numplanesbuf].dist = originalplanes[j].dist;
199 planesbuf[numplanesbuf].q3surfaceflags = originalplanes[j].q3surfaceflags;
200 planesbuf[numplanesbuf].texture = originalplanes[j].texture;
203 // create a large polygon from the plane
205 PolygonD_QuadForPlane(p[w], originalplanes[j].normal[0], originalplanes[j].normal[1], originalplanes[j].normal[2], originalplanes[j].dist, maxdist);
207 // clip it by all other planes
208 for (k = 0;k < numoriginalplanes && pnumpoints >= 3 && pnumpoints <= pmaxpoints;k++)
210 // skip the plane this polygon
211 // (nothing happens if it is processed, this is just an optimization)
214 // we want to keep the inside of the brush plane so we flip
216 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);
221 // if nothing is left, skip it
224 //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);
228 for (k = 0;k < pnumpoints;k++)
232 for (l = 0;l < numoriginalplanes;l++)
233 if (fabs(DotProduct(&p[w][k*3], originalplanes[l].normal) - originalplanes[l].dist) < COLLISION_PLANE_DIST_EPSILON)
240 Con_DPrintf("Collision_NewBrushFromPlanes: warning: polygon point does not lie on at least 3 planes\n");
244 // check if there are too many polygon vertices for buffer
245 if (pnumpoints > pmaxpoints)
247 Con_DPrint("Collision_NewBrushFromPlanes: failed to build collision brush: too many points for buffer\n");
251 // check if there are too many triangle elements for buffer
252 if (numelementsbuf + (pnumpoints - 2) * 3 > maxelementsbuf)
254 Con_DPrint("Collision_NewBrushFromPlanes: failed to build collision brush: too many triangle elements for buffer\n");
258 // add the unique points for this polygon
259 for (k = 0;k < pnumpoints;k++)
262 // downgrade to float precision before comparing
263 VectorCopy(&p[w][k*3], v);
265 // check if there is already a matching point (no duplicates)
266 for (m = 0;m < numpointsbuf;m++)
267 if (VectorDistance2(v, pointsbuf[m].v) < COLLISION_SNAP2)
270 // if there is no match, add a new one
271 if (m == numpointsbuf)
273 // check if there are too many and skip the brush
274 if (numpointsbuf >= maxpointsbuf)
276 Con_DPrint("Collision_NewBrushFromPlanes: failed to build collision brush: too many points for buffer\n");
280 VectorCopy(&p[w][k*3], pointsbuf[numpointsbuf].v);
284 // store the index into a buffer
288 // add the triangles for the polygon
289 // (this particular code makes a triangle fan)
290 for (k = 0;k < pnumpoints - 2;k++)
292 elementsbuf[numelementsbuf++] = polypointbuf[0];
293 elementsbuf[numelementsbuf++] = polypointbuf[k + 1];
294 elementsbuf[numelementsbuf++] = polypointbuf[k + 2];
297 // add the unique edgedirs for this polygon
298 for (k = 0, l = pnumpoints-1;k < pnumpoints;l = k, k++)
301 // downgrade to float precision before comparing
302 VectorSubtract(&p[w][k*3], &p[w][l*3], dir);
303 VectorNormalize(dir);
305 // check if there is already a matching edgedir (no duplicates)
306 for (m = 0;m < numedgedirsbuf;m++)
307 if (DotProduct(dir, edgedirsbuf[m].v) >= COLLISION_EDGEDIR_DOT_EPSILON)
309 // skip this if there is
310 if (m < numedgedirsbuf)
313 // try again with negated edgedir
314 VectorNegate(dir, dir);
315 // check if there is already a matching edgedir (no duplicates)
316 for (m = 0;m < numedgedirsbuf;m++)
317 if (DotProduct(dir, edgedirsbuf[m].v) >= COLLISION_EDGEDIR_DOT_EPSILON)
319 // if there is no match, add a new one
320 if (m == numedgedirsbuf)
322 // check if there are too many and skip the brush
323 if (numedgedirsbuf >= maxedgedirsbuf)
325 Con_DPrint("Collision_NewBrushFromPlanes: failed to build collision brush: too many edgedirs for buffer\n");
329 VectorCopy(dir, edgedirsbuf[numedgedirsbuf].v);
334 // if any normal is not purely axial, it's not an axis-aligned box
335 if (isaabb && (originalplanes[j].normal[0] == 0) + (originalplanes[j].normal[1] == 0) + (originalplanes[j].normal[2] == 0) < 2)
339 // if nothing is left, there's nothing to allocate
340 if (numplanesbuf < 4)
342 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);
346 // if no triangles or points could be constructed, then this routine failed but the brush is not discarded
347 if (numelementsbuf < 12 || numpointsbuf < 4)
348 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);
350 // validate plane distances
351 for (j = 0;j < numplanesbuf;j++)
353 float d = furthestplanedist_float(planesbuf[j].normal, pointsbuf, numpointsbuf);
354 if (fabs(planesbuf[j].dist - d) > COLLISION_PLANE_DIST_EPSILON)
355 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);
358 // allocate the brush and copy to it
359 brush = (colbrushf_t *)Mem_Alloc(mempool, sizeof(colbrushf_t) + sizeof(colpointf_t) * numpointsbuf + sizeof(colpointf_t) * numedgedirsbuf + sizeof(colplanef_t) * numplanesbuf + sizeof(int) * numelementsbuf);
360 brush->isaabb = isaabb;
361 brush->hasaabbplanes = hasaabbplanes;
362 brush->supercontents = supercontents;
363 brush->numplanes = numplanesbuf;
364 brush->numedgedirs = numedgedirsbuf;
365 brush->numpoints = numpointsbuf;
366 brush->numtriangles = numelementsbuf / 3;
367 brush->planes = (colplanef_t *)(brush + 1);
368 brush->points = (colpointf_t *)(brush->planes + brush->numplanes);
369 brush->edgedirs = (colpointf_t *)(brush->points + brush->numpoints);
370 brush->elements = (int *)(brush->points + brush->numpoints);
371 brush->q3surfaceflags = q3surfaceflags;
372 brush->texture = texture;
373 for (j = 0;j < brush->numpoints;j++)
375 brush->points[j].v[0] = pointsbuf[j].v[0];
376 brush->points[j].v[1] = pointsbuf[j].v[1];
377 brush->points[j].v[2] = pointsbuf[j].v[2];
379 for (j = 0;j < brush->numedgedirs;j++)
381 brush->edgedirs[j].v[0] = edgedirsbuf[j].v[0];
382 brush->edgedirs[j].v[1] = edgedirsbuf[j].v[1];
383 brush->edgedirs[j].v[2] = edgedirsbuf[j].v[2];
385 for (j = 0;j < brush->numplanes;j++)
387 brush->planes[j].normal[0] = planesbuf[j].normal[0];
388 brush->planes[j].normal[1] = planesbuf[j].normal[1];
389 brush->planes[j].normal[2] = planesbuf[j].normal[2];
390 brush->planes[j].dist = planesbuf[j].dist;
391 brush->planes[j].q3surfaceflags = planesbuf[j].q3surfaceflags;
392 brush->planes[j].texture = planesbuf[j].texture;
394 for (j = 0;j < brush->numtriangles * 3;j++)
395 brush->elements[j] = elementsbuf[j];
398 VectorClear(brush->mins);
399 VectorClear(brush->maxs);
400 for (j = 0;j < min(6, numoriginalplanes);j++)
402 if (originalplanes[j].normal[0] == 1) {xyzflags |= 1;brush->maxs[0] = originalplanes[j].dist;}
403 else if (originalplanes[j].normal[0] == -1) {xyzflags |= 2;brush->mins[0] = -originalplanes[j].dist;}
404 else if (originalplanes[j].normal[1] == 1) {xyzflags |= 4;brush->maxs[1] = originalplanes[j].dist;}
405 else if (originalplanes[j].normal[1] == -1) {xyzflags |= 8;brush->mins[1] = -originalplanes[j].dist;}
406 else if (originalplanes[j].normal[2] == 1) {xyzflags |= 16;brush->maxs[2] = originalplanes[j].dist;}
407 else if (originalplanes[j].normal[2] == -1) {xyzflags |= 32;brush->mins[2] = -originalplanes[j].dist;}
409 // if not all xyzflags were set, then this is not a brush from q3map/q3map2, and needs reconstruction of the bounding box
410 // (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)
413 VectorCopy(brush->points[0].v, brush->mins);
414 VectorCopy(brush->points[0].v, brush->maxs);
415 for (j = 1;j < brush->numpoints;j++)
417 brush->mins[0] = min(brush->mins[0], brush->points[j].v[0]);
418 brush->mins[1] = min(brush->mins[1], brush->points[j].v[1]);
419 brush->mins[2] = min(brush->mins[2], brush->points[j].v[2]);
420 brush->maxs[0] = max(brush->maxs[0], brush->points[j].v[0]);
421 brush->maxs[1] = max(brush->maxs[1], brush->points[j].v[1]);
422 brush->maxs[2] = max(brush->maxs[2], brush->points[j].v[2]);
431 Collision_ValidateBrush(brush);
437 void Collision_CalcPlanesForPolygonBrushFloat(colbrushf_t *brush)
440 float edge0[3], edge1[3], edge2[3], normal[3], dist, bestdist;
443 // FIXME: these probably don't actually need to be normalized if the collision code does not care
444 if (brush->numpoints == 3)
446 // optimized triangle case
447 TriangleNormal(brush->points[0].v, brush->points[1].v, brush->points[2].v, brush->planes[0].normal);
448 if (DotProduct(brush->planes[0].normal, brush->planes[0].normal) < 0.0001f)
450 // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
451 brush->numplanes = 0;
456 brush->numplanes = 5;
457 brush->numedgedirs = 3;
458 VectorNormalize(brush->planes[0].normal);
459 brush->planes[0].dist = DotProduct(brush->points->v, brush->planes[0].normal);
460 VectorNegate(brush->planes[0].normal, brush->planes[1].normal);
461 brush->planes[1].dist = -brush->planes[0].dist;
462 VectorSubtract(brush->points[2].v, brush->points[0].v, edge0);
463 VectorSubtract(brush->points[0].v, brush->points[1].v, edge1);
464 VectorSubtract(brush->points[1].v, brush->points[2].v, edge2);
465 VectorCopy(edge0, brush->edgedirs[0].v);
466 VectorCopy(edge1, brush->edgedirs[1].v);
467 VectorCopy(edge2, brush->edgedirs[2].v);
470 float projectionnormal[3], projectionedge0[3], projectionedge1[3], projectionedge2[3];
472 float dist, bestdist;
473 bestdist = fabs(brush->planes[0].normal[0]);
475 for (i = 1;i < 3;i++)
477 dist = fabs(brush->planes[0].normal[i]);
484 VectorClear(projectionnormal);
485 if (brush->planes[0].normal[best] < 0)
486 projectionnormal[best] = -1;
488 projectionnormal[best] = 1;
489 VectorCopy(edge0, projectionedge0);
490 VectorCopy(edge1, projectionedge1);
491 VectorCopy(edge2, projectionedge2);
492 projectionedge0[best] = 0;
493 projectionedge1[best] = 0;
494 projectionedge2[best] = 0;
495 CrossProduct(projectionedge0, projectionnormal, brush->planes[2].normal);
496 CrossProduct(projectionedge1, projectionnormal, brush->planes[3].normal);
497 CrossProduct(projectionedge2, projectionnormal, brush->planes[4].normal);
500 CrossProduct(edge0, brush->planes->normal, brush->planes[2].normal);
501 CrossProduct(edge1, brush->planes->normal, brush->planes[3].normal);
502 CrossProduct(edge2, brush->planes->normal, brush->planes[4].normal);
504 VectorNormalize(brush->planes[2].normal);
505 VectorNormalize(brush->planes[3].normal);
506 VectorNormalize(brush->planes[4].normal);
507 brush->planes[2].dist = DotProduct(brush->points[2].v, brush->planes[2].normal);
508 brush->planes[3].dist = DotProduct(brush->points[0].v, brush->planes[3].normal);
509 brush->planes[4].dist = DotProduct(brush->points[1].v, brush->planes[4].normal);
511 if (developer.integer >= 100)
517 VectorSubtract(brush->points[0].v, brush->points[1].v, edge0);
518 VectorSubtract(brush->points[2].v, brush->points[1].v, edge1);
519 CrossProduct(edge0, edge1, normal);
520 VectorNormalize(normal);
521 VectorSubtract(normal, brush->planes[0].normal, temp);
522 if (VectorLength(temp) > 0.01f)
523 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]);
524 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)
525 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);
527 if (fabs(DotProduct(brush->planes[2].normal, brush->planes[0].normal)) > 0.01f)
528 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);
529 if (fabs(DotProduct(brush->planes[3].normal, brush->planes[0].normal)) > 0.01f)
530 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);
531 if (fabs(DotProduct(brush->planes[4].normal, brush->planes[0].normal)) > 0.01f)
532 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);
533 if (fabs(DotProduct(brush->planes[2].normal, edge0)) > 0.01f)
534 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]);
535 if (fabs(DotProduct(brush->planes[3].normal, edge1)) > 0.01f)
536 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]);
537 if (fabs(DotProduct(brush->planes[4].normal, edge2)) > 0.01f)
538 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]);
541 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)
542 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);
543 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)
544 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);
545 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)
546 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);
547 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)
548 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);
549 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)
550 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);
556 // choose best surface normal for polygon's plane
558 for (i = 0, p = brush->points + 1;i < brush->numpoints - 2;i++, p++)
560 VectorSubtract(p[-1].v, p[0].v, edge0);
561 VectorSubtract(p[1].v, p[0].v, edge1);
562 CrossProduct(edge0, edge1, normal);
563 //TriangleNormal(p[-1].v, p[0].v, p[1].v, normal);
564 dist = DotProduct(normal, normal);
565 if (i == 0 || bestdist < dist)
568 VectorCopy(normal, brush->planes->normal);
571 if (bestdist < 0.0001f)
573 // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
574 brush->numplanes = 0;
579 brush->numplanes = brush->numpoints + 2;
580 VectorNormalize(brush->planes->normal);
581 brush->planes->dist = DotProduct(brush->points->v, brush->planes->normal);
583 // negate plane to create other side
584 VectorNegate(brush->planes[0].normal, brush->planes[1].normal);
585 brush->planes[1].dist = -brush->planes[0].dist;
586 for (i = 0, p = brush->points + (brush->numpoints - 1), p2 = brush->points;i < brush->numpoints;i++, p = p2, p2++)
588 VectorSubtract(p->v, p2->v, edge0);
589 CrossProduct(edge0, brush->planes->normal, brush->planes[i + 2].normal);
590 VectorNormalize(brush->planes[i + 2].normal);
591 brush->planes[i + 2].dist = DotProduct(p->v, brush->planes[i + 2].normal);
596 if (developer.integer >= 100)
598 // validity check - will be disabled later
599 Collision_ValidateBrush(brush);
600 for (i = 0;i < brush->numplanes;i++)
603 for (j = 0, p = brush->points;j < brush->numpoints;j++, p++)
604 if (DotProduct(p->v, brush->planes[i].normal) > brush->planes[i].dist + COLLISION_PLANE_DIST_EPSILON)
605 Con_Printf("Error in brush plane generation, plane %i\n", i);
610 colbrushf_t *Collision_AllocBrushFromPermanentPolygonFloat(mempool_t *mempool, int numpoints, float *points, int supercontents, int q3surfaceflags, texture_t *texture)
613 brush = (colbrushf_t *)Mem_Alloc(mempool, sizeof(colbrushf_t) + sizeof(colplanef_t) * (numpoints + 2) + sizeof(colpointf_t) * numpoints);
614 brush->isaabb = false;
615 brush->hasaabbplanes = false;
616 brush->supercontents = supercontents;
617 brush->numpoints = numpoints;
618 brush->numedgedirs = numpoints;
619 brush->numplanes = numpoints + 2;
620 brush->planes = (colplanef_t *)(brush + 1);
621 brush->points = (colpointf_t *)points;
622 brush->edgedirs = (colpointf_t *)(brush->planes + brush->numplanes);
623 brush->q3surfaceflags = q3surfaceflags;
624 brush->texture = texture;
625 Sys_Error("Collision_AllocBrushFromPermanentPolygonFloat: FIXME: this code needs to be updated to generate a mesh...");
629 // NOTE: start and end of each brush pair must have same numplanes/numpoints
630 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)
632 int nplane, nplane2, nedge1, nedge2, hitq3surfaceflags = 0;
633 int tracenumedgedirs = trace_start->numedgedirs;
634 //int othernumedgedirs = other_start->numedgedirs;
635 int tracenumpoints = trace_start->numpoints;
636 int othernumpoints = other_start->numpoints;
637 int numplanes1 = other_start->numplanes;
638 int numplanes2 = numplanes1 + trace_start->numplanes;
639 int numplanes3 = numplanes2 + trace_start->numedgedirs * other_start->numedgedirs * 2;
640 vec_t enterfrac = -1, leavefrac = 1, startdist, enddist, ie, f, imove, enterfrac2 = -1;
643 vec4_t newimpactplane;
644 texture_t *hittexture = NULL;
645 vec_t startdepth = 1;
646 vec3_t startdepthnormal;
648 VectorClear(startdepthnormal);
649 Vector4Clear(newimpactplane);
651 // fast case for AABB vs compiled brushes (which begin with AABB planes and also have precomputed bevels for AABB collisions)
652 if (trace_start->isaabb && other_start->hasaabbplanes)
653 numplanes3 = numplanes2 = numplanes1;
655 // Separating Axis Theorem:
656 // if a supporting vector (plane normal) can be found that separates two
657 // objects, they are not colliding.
660 // reduce the size of one object to a point while enlarging the other to
661 // represent the space that point can not occupy.
663 // try every plane we can construct between the two brushes and measure
664 // the distance between them.
665 for (nplane = 0;nplane < numplanes3;nplane++)
667 if (nplane < numplanes1)
670 VectorCopy(other_start->planes[nplane2].normal, startplane);
671 VectorCopy(other_end->planes[nplane2].normal, endplane);
673 else if (nplane < numplanes2)
675 nplane2 = nplane - numplanes1;
676 VectorCopy(trace_start->planes[nplane2].normal, startplane);
677 VectorCopy(trace_end->planes[nplane2].normal, endplane);
681 // pick an edgedir from each brush and cross them
682 nplane2 = nplane - numplanes2;
683 nedge1 = nplane2 >> 1;
684 nedge2 = nedge1 / tracenumedgedirs;
685 nedge1 -= nedge2 * tracenumedgedirs;
688 CrossProduct(trace_start->edgedirs[nedge1].v, other_start->edgedirs[nedge2].v, startplane);
689 if (VectorLength2(startplane) < COLLISION_EDGECROSS_MINLENGTH2)
690 continue; // degenerate crossproduct
691 CrossProduct(trace_end->edgedirs[nedge1].v, other_end->edgedirs[nedge2].v, endplane);
692 if (VectorLength2(endplane) < COLLISION_EDGECROSS_MINLENGTH2)
693 continue; // degenerate crossproduct
697 CrossProduct(other_start->edgedirs[nedge2].v, trace_start->edgedirs[nedge1].v, startplane);
698 if (VectorLength2(startplane) < COLLISION_EDGECROSS_MINLENGTH2)
699 continue; // degenerate crossproduct
700 CrossProduct(other_end->edgedirs[nedge2].v, trace_end->edgedirs[nedge1].v, endplane);
701 if (VectorLength2(endplane) < COLLISION_EDGECROSS_MINLENGTH2)
702 continue; // degenerate crossproduct
704 VectorNormalize(startplane);
705 VectorNormalize(endplane);
707 startplane[3] = furthestplanedist_float(startplane, other_start->points, othernumpoints);
708 endplane[3] = furthestplanedist_float(startplane, other_end->points, othernumpoints);
709 startdist = nearestplanedist_float(startplane, trace_start->points, tracenumpoints) - startplane[3] - collision_startnudge.value;
710 enddist = nearestplanedist_float(endplane, trace_end->points, tracenumpoints) - endplane[3] - collision_endnudge.value;
711 //Con_Printf("%c%i: startdist = %f, enddist = %f, startdist / (startdist - enddist) = %f\n", nplane2 != nplane ? 'b' : 'a', nplane2, startdist, enddist, startdist / (startdist - enddist));
713 // aside from collisions, this is also used for error correction
714 if (startdist < 0 && (startdepth < startdist || startdepth == 1))
716 startdepth = startdist;
717 VectorCopy(startplane, startdepthnormal);
720 if (startdist > enddist)
723 if (enddist >= collision_enternudge.value)
728 imove = 1 / (startdist - enddist);
729 f = (startdist - collision_enternudge.value) * imove;
732 // check if this will reduce the collision time range
735 // reduced collision time range
737 // if the collision time range is now empty, no collision
738 if (enterfrac > leavefrac)
740 // if the collision would be further away than the trace's
741 // existing collision data, we don't care about this
743 if (enterfrac > trace->realfraction)
745 // calculate the nudged fraction and impact normal we'll
746 // need if we accept this collision later
747 enterfrac2 = (startdist - collision_impactnudge.value) * imove;
748 ie = 1.0f - enterfrac;
749 newimpactplane[0] = startplane[0] * ie + endplane[0] * enterfrac;
750 newimpactplane[1] = startplane[1] * ie + endplane[1] * enterfrac;
751 newimpactplane[2] = startplane[2] * ie + endplane[2] * enterfrac;
752 newimpactplane[3] = startplane[3] * ie + endplane[3] * enterfrac;
753 if (nplane < numplanes1)
755 // use the plane from other
757 hitq3surfaceflags = other_start->planes[nplane2].q3surfaceflags;
758 hittexture = other_start->planes[nplane2].texture;
760 else if (nplane < numplanes2)
762 // use the plane from trace
763 nplane2 = nplane - numplanes1;
764 hitq3surfaceflags = trace_start->planes[nplane2].q3surfaceflags;
765 hittexture = trace_start->planes[nplane2].texture;
769 hitq3surfaceflags = other_start->q3surfaceflags;
770 hittexture = other_start->texture;
777 // moving out of brush
783 f = (startdist + collision_leavenudge.value) / (startdist - enddist);
786 // check if this will reduce the collision time range
789 // reduced collision time range
791 // if the collision time range is now empty, no collision
792 if (enterfrac > leavefrac)
799 // at this point we know the trace overlaps the brush because it was not
800 // rejected at any point in the loop above
802 // see if the trace started outside the brush or not
805 // started outside, and overlaps, therefore there is a collision here
806 // store out the impact information
807 if (trace->hitsupercontentsmask & other_start->supercontents)
809 trace->hitsupercontents = other_start->supercontents;
810 trace->hitq3surfaceflags = hitq3surfaceflags;
811 trace->hittexture = hittexture;
812 trace->realfraction = bound(0, enterfrac, 1);
813 trace->fraction = bound(0, enterfrac2, 1);
814 if (collision_prefernudgedfraction.integer)
815 trace->realfraction = trace->fraction;
816 VectorCopy(newimpactplane, trace->plane.normal);
817 trace->plane.dist = newimpactplane[3];
822 // started inside, update startsolid and friends
823 trace->startsupercontents |= other_start->supercontents;
824 if (trace->hitsupercontentsmask & other_start->supercontents)
826 trace->startsolid = true;
828 trace->allsolid = true;
829 VectorCopy(newimpactplane, trace->plane.normal);
830 trace->plane.dist = newimpactplane[3];
831 if (trace->startdepth > startdepth)
833 trace->startdepth = startdepth;
834 VectorCopy(startdepthnormal, trace->startdepthnormal);
840 // NOTE: start and end of each brush pair must have same numplanes/numpoints
841 void Collision_TraceLineBrushFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, const colbrushf_t *other_start, const colbrushf_t *other_end)
843 int nplane, hitq3surfaceflags = 0;
844 int numplanes = other_start->numplanes;
845 vec_t enterfrac = -1, leavefrac = 1, startdist, enddist, ie, f, imove, enterfrac2 = -1;
848 vec4_t newimpactplane;
849 texture_t *hittexture = NULL;
850 vec_t startdepth = 1;
851 vec3_t startdepthnormal;
853 VectorClear(startdepthnormal);
854 Vector4Clear(newimpactplane);
856 // Separating Axis Theorem:
857 // if a supporting vector (plane normal) can be found that separates two
858 // objects, they are not colliding.
861 // reduce the size of one object to a point while enlarging the other to
862 // represent the space that point can not occupy.
864 // try every plane we can construct between the two brushes and measure
865 // the distance between them.
866 for (nplane = 0;nplane < numplanes;nplane++)
868 VectorCopy(other_start->planes[nplane].normal, startplane);
869 startplane[3] = other_start->planes[nplane].dist;
870 VectorCopy(other_end->planes[nplane].normal, endplane);
871 endplane[3] = other_end->planes[nplane].dist;
872 startdist = DotProduct(linestart, startplane) - startplane[3] - collision_startnudge.value;
873 enddist = DotProduct(lineend, endplane) - endplane[3] - collision_endnudge.value;
874 //Con_Printf("%c%i: startdist = %f, enddist = %f, startdist / (startdist - enddist) = %f\n", nplane2 != nplane ? 'b' : 'a', nplane2, startdist, enddist, startdist / (startdist - enddist));
876 // aside from collisions, this is also used for error correction
877 if (startdist < 0 && (startdepth < startdist || startdepth == 1))
879 startdepth = startdist;
880 VectorCopy(startplane, startdepthnormal);
883 if (startdist > enddist)
886 if (enddist >= collision_enternudge.value)
891 imove = 1 / (startdist - enddist);
892 f = (startdist - collision_enternudge.value) * imove;
895 // check if this will reduce the collision time range
898 // reduced collision time range
900 // if the collision time range is now empty, no collision
901 if (enterfrac > leavefrac)
903 // if the collision would be further away than the trace's
904 // existing collision data, we don't care about this
906 if (enterfrac > trace->realfraction)
908 // calculate the nudged fraction and impact normal we'll
909 // need if we accept this collision later
910 enterfrac2 = (startdist - collision_impactnudge.value) * imove;
911 ie = 1.0f - enterfrac;
912 newimpactplane[0] = startplane[0] * ie + endplane[0] * enterfrac;
913 newimpactplane[1] = startplane[1] * ie + endplane[1] * enterfrac;
914 newimpactplane[2] = startplane[2] * ie + endplane[2] * enterfrac;
915 newimpactplane[3] = startplane[3] * ie + endplane[3] * enterfrac;
916 hitq3surfaceflags = other_start->planes[nplane].q3surfaceflags;
917 hittexture = other_start->planes[nplane].texture;
923 // moving out of brush
929 f = (startdist + collision_leavenudge.value) / (startdist - enddist);
932 // check if this will reduce the collision time range
935 // reduced collision time range
937 // if the collision time range is now empty, no collision
938 if (enterfrac > leavefrac)
945 // at this point we know the trace overlaps the brush because it was not
946 // rejected at any point in the loop above
948 // see if the trace started outside the brush or not
951 // started outside, and overlaps, therefore there is a collision here
952 // store out the impact information
953 if (trace->hitsupercontentsmask & other_start->supercontents)
955 trace->hitsupercontents = other_start->supercontents;
956 trace->hitq3surfaceflags = hitq3surfaceflags;
957 trace->hittexture = hittexture;
958 trace->realfraction = bound(0, enterfrac, 1);
959 trace->fraction = bound(0, enterfrac2, 1);
960 if (collision_prefernudgedfraction.integer)
961 trace->realfraction = trace->fraction;
962 VectorCopy(newimpactplane, trace->plane.normal);
963 trace->plane.dist = newimpactplane[3];
968 // started inside, update startsolid and friends
969 trace->startsupercontents |= other_start->supercontents;
970 if (trace->hitsupercontentsmask & other_start->supercontents)
972 trace->startsolid = true;
974 trace->allsolid = true;
975 VectorCopy(newimpactplane, trace->plane.normal);
976 trace->plane.dist = newimpactplane[3];
977 if (trace->startdepth > startdepth)
979 trace->startdepth = startdepth;
980 VectorCopy(startdepthnormal, trace->startdepthnormal);
986 qboolean Collision_PointInsideBrushFloat(const vec3_t point, const colbrushf_t *brush)
989 const colplanef_t *plane;
991 if (!BoxesOverlap(point, point, brush->mins, brush->maxs))
993 for (nplane = 0, plane = brush->planes;nplane < brush->numplanes;nplane++, plane++)
994 if (DotProduct(plane->normal, point) > plane->dist)
999 void Collision_TracePointBrushFloat(trace_t *trace, const vec3_t point, const colbrushf_t *thatbrush)
1001 if (!Collision_PointInsideBrushFloat(point, thatbrush))
1004 trace->startsupercontents |= thatbrush->supercontents;
1005 if (trace->hitsupercontentsmask & thatbrush->supercontents)
1007 trace->startsolid = true;
1008 trace->allsolid = true;
1012 void Collision_SnapCopyPoints(int numpoints, const colpointf_t *in, colpointf_t *out, float fractionprecision, float invfractionprecision)
1015 for (i = 0;i < numpoints;i++)
1017 out[i].v[0] = floor(in[i].v[0] * fractionprecision + 0.5f) * invfractionprecision;
1018 out[i].v[1] = floor(in[i].v[1] * fractionprecision + 0.5f) * invfractionprecision;
1019 out[i].v[2] = floor(in[i].v[2] * fractionprecision + 0.5f) * invfractionprecision;
1023 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, texture_t *texture, const vec3_t segmentmins, const vec3_t segmentmaxs)
1026 colpointf_t points[3];
1027 colpointf_t edgedirs[3];
1028 colplanef_t planes[5];
1030 memset(&brush, 0, sizeof(brush));
1031 brush.isaabb = false;
1032 brush.hasaabbplanes = false;
1033 brush.numpoints = 3;
1034 brush.numedgedirs = 3;
1035 brush.numplanes = 5;
1036 brush.points = points;
1037 brush.edgedirs = edgedirs;
1038 brush.planes = planes;
1039 brush.supercontents = supercontents;
1040 brush.q3surfaceflags = q3surfaceflags;
1041 brush.texture = texture;
1042 for (i = 0;i < brush.numplanes;i++)
1044 brush.planes[i].q3surfaceflags = q3surfaceflags;
1045 brush.planes[i].texture = texture;
1050 cnt = (numtriangles + stride - 1) / stride;
1051 for(i = 0; i < cnt; ++i)
1053 if(BoxesOverlap(bbox6f + i * 6, bbox6f + i * 6 + 3, segmentmins, segmentmaxs))
1055 for(k = 0; k < stride; ++k)
1057 tri = i * stride + k;
1058 if(tri >= numtriangles)
1060 VectorCopy(vertex3f + element3i[tri * 3 + 0] * 3, points[0].v);
1061 VectorCopy(vertex3f + element3i[tri * 3 + 1] * 3, points[1].v);
1062 VectorCopy(vertex3f + element3i[tri * 3 + 2] * 3, points[2].v);
1063 Collision_SnapCopyPoints(brush.numpoints, points, points, COLLISION_SNAPSCALE, COLLISION_SNAP);
1064 Collision_CalcEdgeDirsForPolygonBrushFloat(&brush);
1065 Collision_CalcPlanesForPolygonBrushFloat(&brush);
1066 //Collision_PrintBrushAsQHull(&brush, "brush");
1067 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &brush, &brush);
1074 for (i = 0;i < numtriangles;i++, element3i += 3)
1076 if (TriangleOverlapsBox(vertex3f + element3i[0]*3, vertex3f + element3i[1]*3, vertex3f + element3i[2]*3, segmentmins, segmentmaxs))
1078 VectorCopy(vertex3f + element3i[0] * 3, points[0].v);
1079 VectorCopy(vertex3f + element3i[1] * 3, points[1].v);
1080 VectorCopy(vertex3f + element3i[2] * 3, points[2].v);
1081 Collision_SnapCopyPoints(brush.numpoints, points, points, COLLISION_SNAPSCALE, COLLISION_SNAP);
1082 Collision_CalcEdgeDirsForPolygonBrushFloat(&brush);
1083 Collision_CalcPlanesForPolygonBrushFloat(&brush);
1084 //Collision_PrintBrushAsQHull(&brush, "brush");
1085 Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &brush, &brush);
1091 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, texture_t *texture, const vec3_t segmentmins, const vec3_t segmentmaxs)
1095 // FIXME: snap vertices?
1099 cnt = (numtriangles + stride - 1) / stride;
1100 for(i = 0; i < cnt; ++i)
1102 if(BoxesOverlap(bbox6f + i * 6, bbox6f + i * 6 + 3, segmentmins, segmentmaxs))
1104 for(k = 0; k < stride; ++k)
1106 tri = i * stride + k;
1107 if(tri >= numtriangles)
1109 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);
1116 for (i = 0;i < numtriangles;i++, element3i += 3)
1117 Collision_TraceLineTriangleFloat(trace, linestart, lineend, vertex3f + element3i[0] * 3, vertex3f + element3i[1] * 3, vertex3f + element3i[2] * 3, supercontents, q3surfaceflags, texture);
1120 colpointf_t points[3];
1121 colpointf_t edgedirs[3];
1122 colplanef_t planes[5];
1124 memset(&brush, 0, sizeof(brush));
1125 brush.isaabb = false;
1126 brush.hasaabbplanes = false;
1127 brush.numpoints = 3;
1128 brush.numedgedirs = 3;
1129 brush.numplanes = 5;
1130 brush.points = points;
1131 brush.edgedirs = edgedirs;
1132 brush.planes = planes;
1133 brush.supercontents = supercontents;
1134 brush.q3surfaceflags = q3surfaceflags;
1135 brush.texture = texture;
1136 for (i = 0;i < brush.numplanes;i++)
1138 brush.planes[i].q3surfaceflags = q3surfaceflags;
1139 brush.planes[i].texture = texture;
1141 for (i = 0;i < numtriangles;i++, element3i += 3)
1143 if (TriangleOverlapsBox(vertex3f + element3i[0]*3, vertex3 + [element3i[1]*3, vertex3f + element3i[2]*3, segmentmins, segmentmaxs))
1145 VectorCopy(vertex3f + element3i[0] * 3, points[0].v);
1146 VectorCopy(vertex3f + element3i[1] * 3, points[1].v);
1147 VectorCopy(vertex3f + element3i[2] * 3, points[2].v);
1148 Collision_SnapCopyPoints(brush.numpoints, points, points, COLLISION_SNAPSCALE, COLLISION_SNAP);
1149 Collision_CalcEdgeDirsForPolygonBrushFloat(&brush);
1150 Collision_CalcPlanesForPolygonBrushFloat(&brush);
1151 //Collision_PrintBrushAsQHull(&brush, "brush");
1152 Collision_TraceLineBrushFloat(trace, linestart, lineend, &brush, &brush);
1158 void Collision_BrushForBox(colboxbrushf_t *boxbrush, const vec3_t mins, const vec3_t maxs, int supercontents, int q3surfaceflags, texture_t *texture)
1161 memset(boxbrush, 0, sizeof(*boxbrush));
1162 boxbrush->brush.points = boxbrush->points;
1163 boxbrush->brush.edgedirs = boxbrush->edgedirs;
1164 boxbrush->brush.planes = boxbrush->planes;
1165 boxbrush->brush.supercontents = supercontents;
1166 boxbrush->brush.q3surfaceflags = q3surfaceflags;
1167 boxbrush->brush.texture = texture;
1168 if (VectorCompare(mins, maxs))
1171 boxbrush->brush.numpoints = 1;
1172 boxbrush->brush.numedgedirs = 0;
1173 boxbrush->brush.numplanes = 0;
1174 VectorCopy(mins, boxbrush->brush.points[0].v);
1178 boxbrush->brush.numpoints = 8;
1179 boxbrush->brush.numedgedirs = 3;
1180 boxbrush->brush.numplanes = 6;
1181 // there are 8 points on a box
1182 // there are 3 edgedirs on a box (both signs are tested in collision)
1183 // there are 6 planes on a box
1184 VectorSet(boxbrush->brush.points[0].v, mins[0], mins[1], mins[2]);
1185 VectorSet(boxbrush->brush.points[1].v, maxs[0], mins[1], mins[2]);
1186 VectorSet(boxbrush->brush.points[2].v, mins[0], maxs[1], mins[2]);
1187 VectorSet(boxbrush->brush.points[3].v, maxs[0], maxs[1], mins[2]);
1188 VectorSet(boxbrush->brush.points[4].v, mins[0], mins[1], maxs[2]);
1189 VectorSet(boxbrush->brush.points[5].v, maxs[0], mins[1], maxs[2]);
1190 VectorSet(boxbrush->brush.points[6].v, mins[0], maxs[1], maxs[2]);
1191 VectorSet(boxbrush->brush.points[7].v, maxs[0], maxs[1], maxs[2]);
1192 VectorSet(boxbrush->brush.edgedirs[0].v, 1, 0, 0);
1193 VectorSet(boxbrush->brush.edgedirs[1].v, 0, 1, 0);
1194 VectorSet(boxbrush->brush.edgedirs[2].v, 0, 0, 1);
1195 VectorSet(boxbrush->brush.planes[0].normal, -1, 0, 0);boxbrush->brush.planes[0].dist = -mins[0];
1196 VectorSet(boxbrush->brush.planes[1].normal, 1, 0, 0);boxbrush->brush.planes[1].dist = maxs[0];
1197 VectorSet(boxbrush->brush.planes[2].normal, 0, -1, 0);boxbrush->brush.planes[2].dist = -mins[1];
1198 VectorSet(boxbrush->brush.planes[3].normal, 0, 1, 0);boxbrush->brush.planes[3].dist = maxs[1];
1199 VectorSet(boxbrush->brush.planes[4].normal, 0, 0, -1);boxbrush->brush.planes[4].dist = -mins[2];
1200 VectorSet(boxbrush->brush.planes[5].normal, 0, 0, 1);boxbrush->brush.planes[5].dist = maxs[2];
1201 for (i = 0;i < 6;i++)
1203 boxbrush->brush.planes[i].q3surfaceflags = q3surfaceflags;
1204 boxbrush->brush.planes[i].texture = texture;
1207 boxbrush->brush.supercontents = supercontents;
1208 boxbrush->brush.q3surfaceflags = q3surfaceflags;
1209 boxbrush->brush.texture = texture;
1210 VectorSet(boxbrush->brush.mins, mins[0] - 1, mins[1] - 1, mins[2] - 1);
1211 VectorSet(boxbrush->brush.maxs, maxs[0] + 1, maxs[1] + 1, maxs[2] + 1);
1212 Collision_ValidateBrush(&boxbrush->brush);
1215 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)
1217 colboxbrushf_t boxbrush, thisbrush_start, thisbrush_end;
1218 vec3_t startmins, startmaxs, endmins, endmaxs;
1220 // create brushes for the collision
1221 VectorAdd(start, mins, startmins);
1222 VectorAdd(start, maxs, startmaxs);
1223 VectorAdd(end, mins, endmins);
1224 VectorAdd(end, maxs, endmaxs);
1225 Collision_BrushForBox(&boxbrush, cmins, cmaxs, supercontents, q3surfaceflags, texture);
1226 Collision_BrushForBox(&thisbrush_start, startmins, startmaxs, 0, 0, NULL);
1227 Collision_BrushForBox(&thisbrush_end, endmins, endmaxs, 0, 0, NULL);
1229 memset(trace, 0, sizeof(trace_t));
1230 trace->hitsupercontentsmask = hitsupercontentsmask;
1231 trace->fraction = 1;
1232 trace->realfraction = 1;
1233 trace->allsolid = true;
1234 Collision_TraceBrushBrushFloat(trace, &thisbrush_start.brush, &thisbrush_end.brush, &boxbrush.brush, &boxbrush.brush);
1237 //pseudocode for detecting line/sphere overlap without calculating an impact point
1238 //linesphereorigin = sphereorigin - linestart;linediff = lineend - linestart;linespherefrac = DotProduct(linesphereorigin, linediff) / DotProduct(linediff, linediff);return VectorLength2(linesphereorigin - bound(0, linespherefrac, 1) * linediff) >= sphereradius*sphereradius;
1240 // LordHavoc: currently unused, but tested
1241 // note: this can be used for tracing a moving sphere vs a stationary sphere,
1242 // by simply adding the moving sphere's radius to the sphereradius parameter,
1243 // all the results are correct (impactpoint, impactnormal, and fraction)
1244 float Collision_ClipTrace_Line_Sphere(double *linestart, double *lineend, double *sphereorigin, double sphereradius, double *impactpoint, double *impactnormal)
1246 double dir[3], scale, v[3], deviationdist, impactdist, linelength;
1247 // make sure the impactpoint and impactnormal are valid even if there is
1249 VectorCopy(lineend, impactpoint);
1250 VectorClear(impactnormal);
1251 // calculate line direction
1252 VectorSubtract(lineend, linestart, dir);
1253 // normalize direction
1254 linelength = VectorLength(dir);
1257 scale = 1.0 / linelength;
1258 VectorScale(dir, scale, dir);
1260 // this dotproduct calculates the distance along the line at which the
1261 // sphere origin is (nearest point to the sphere origin on the line)
1262 impactdist = DotProduct(sphereorigin, dir) - DotProduct(linestart, dir);
1263 // calculate point on line at that distance, and subtract the
1264 // sphereorigin from it, so we have a vector to measure for the distance
1265 // of the line from the sphereorigin (deviation, how off-center it is)
1266 VectorMA(linestart, impactdist, dir, v);
1267 VectorSubtract(v, sphereorigin, v);
1268 deviationdist = VectorLength2(v);
1269 // if outside the radius, it's a miss for sure
1270 // (we do this comparison using squared radius to avoid a sqrt)
1271 if (deviationdist > sphereradius*sphereradius)
1272 return 1; // miss (off to the side)
1273 // nudge back to find the correct impact distance
1274 impactdist -= sphereradius - deviationdist/sphereradius;
1275 if (impactdist >= linelength)
1276 return 1; // miss (not close enough)
1278 return 1; // miss (linestart is past or inside sphere)
1279 // calculate new impactpoint
1280 VectorMA(linestart, impactdist, dir, impactpoint);
1281 // calculate impactnormal (surface normal at point of impact)
1282 VectorSubtract(impactpoint, sphereorigin, impactnormal);
1283 // normalize impactnormal
1284 VectorNormalize(impactnormal);
1285 // return fraction of movement distance
1286 return impactdist / linelength;
1289 void Collision_TraceLineTriangleFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, const float *point0, const float *point1, const float *point2, int supercontents, int q3surfaceflags, texture_t *texture)
1293 float d1, d2, d, f, impact[3], edgenormal[3], faceplanenormal[3], faceplanedist, faceplanenormallength2, edge01[3], edge21[3], edge02[3];
1295 // this function executes:
1296 // 32 ops when line starts behind triangle
1297 // 38 ops when line ends infront of triangle
1298 // 43 ops when line fraction is already closer than this triangle
1299 // 72 ops when line is outside edge 01
1300 // 92 ops when line is outside edge 21
1301 // 115 ops when line is outside edge 02
1302 // 123 ops when line impacts triangle and updates trace results
1304 // this code is designed for clockwise triangles, conversion to
1305 // counterclockwise would require swapping some things around...
1306 // it is easier to simply swap the point0 and point2 parameters to this
1307 // function when calling it than it is to rewire the internals.
1309 // calculate the faceplanenormal of the triangle, this represents the front side
1311 VectorSubtract(point0, point1, edge01);
1312 VectorSubtract(point2, point1, edge21);
1313 CrossProduct(edge01, edge21, faceplanenormal);
1314 // there's no point in processing a degenerate triangle (GIGO - Garbage In, Garbage Out)
1316 faceplanenormallength2 = DotProduct(faceplanenormal, faceplanenormal);
1317 if (faceplanenormallength2 < 0.0001f)
1319 // calculate the distance
1321 faceplanedist = DotProduct(point0, faceplanenormal);
1323 // if start point is on the back side there is no collision
1324 // (we don't care about traces going through the triangle the wrong way)
1326 // calculate the start distance
1328 d1 = DotProduct(faceplanenormal, linestart);
1329 if (d1 <= faceplanedist)
1332 // calculate the end distance
1334 d2 = DotProduct(faceplanenormal, lineend);
1335 // if both are in front, there is no collision
1336 if (d2 >= faceplanedist)
1339 // from here on we know d1 is >= 0 and d2 is < 0
1340 // this means the line starts infront and ends behind, passing through it
1342 // calculate the recipricol of the distance delta,
1343 // so we can use it multiple times cheaply (instead of division)
1345 d = 1.0f / (d1 - d2);
1346 // calculate the impact fraction by taking the start distance (> 0)
1347 // and subtracting the face plane distance (this is the distance of the
1348 // triangle along that same normal)
1349 // then multiply by the recipricol distance delta
1351 f = (d1 - faceplanedist) * d;
1352 // skip out if this impact is further away than previous ones
1354 if (f > trace->realfraction)
1356 // calculate the perfect impact point for classification of insidedness
1358 impact[0] = linestart[0] + f * (lineend[0] - linestart[0]);
1359 impact[1] = linestart[1] + f * (lineend[1] - linestart[1]);
1360 impact[2] = linestart[2] + f * (lineend[2] - linestart[2]);
1362 // calculate the edge normal and reject if impact is outside triangle
1363 // (an edge normal faces away from the triangle, to get the desired normal
1364 // a crossproduct with the faceplanenormal is used, and because of the way
1365 // the insidedness comparison is written it does not need to be normalized)
1367 // first use the two edges from the triangle plane math
1368 // the other edge only gets calculated if the point survives that long
1371 CrossProduct(edge01, faceplanenormal, edgenormal);
1372 if (DotProduct(impact, edgenormal) > DotProduct(point1, edgenormal))
1376 CrossProduct(faceplanenormal, edge21, edgenormal);
1377 if (DotProduct(impact, edgenormal) > DotProduct(point2, edgenormal))
1381 VectorSubtract(point0, point2, edge02);
1382 CrossProduct(faceplanenormal, edge02, edgenormal);
1383 if (DotProduct(impact, edgenormal) > DotProduct(point0, edgenormal))
1388 // store the new trace fraction
1389 trace->realfraction = f;
1391 // calculate a nudged fraction to keep it out of the surface
1392 // (the main fraction remains perfect)
1393 trace->fraction = f - collision_impactnudge.value * d;
1395 if (collision_prefernudgedfraction.integer)
1396 trace->realfraction = trace->fraction;
1398 // store the new trace plane (because collisions only happen from
1399 // the front this is always simply the triangle normal, never flipped)
1400 d = 1.0 / sqrt(faceplanenormallength2);
1401 VectorScale(faceplanenormal, d, trace->plane.normal);
1402 trace->plane.dist = faceplanedist * d;
1404 trace->hitsupercontents = supercontents;
1405 trace->hitq3surfaceflags = q3surfaceflags;
1406 trace->hittexture = texture;
1408 float d1, d2, d, f, fnudged, impact[3], edgenormal[3], faceplanenormal[3], faceplanedist, edge[3];
1410 // this code is designed for clockwise triangles, conversion to
1411 // counterclockwise would require swapping some things around...
1412 // it is easier to simply swap the point0 and point2 parameters to this
1413 // function when calling it than it is to rewire the internals.
1415 // calculate the unnormalized faceplanenormal of the triangle,
1416 // this represents the front side
1417 TriangleNormal(point0, point1, point2, faceplanenormal);
1418 // there's no point in processing a degenerate triangle
1419 // (GIGO - Garbage In, Garbage Out)
1420 if (DotProduct(faceplanenormal, faceplanenormal) < 0.0001f)
1422 // calculate the unnormalized distance
1423 faceplanedist = DotProduct(point0, faceplanenormal);
1425 // calculate the unnormalized start distance
1426 d1 = DotProduct(faceplanenormal, linestart) - faceplanedist;
1427 // if start point is on the back side there is no collision
1428 // (we don't care about traces going through the triangle the wrong way)
1432 // calculate the unnormalized end distance
1433 d2 = DotProduct(faceplanenormal, lineend) - faceplanedist;
1434 // if both are in front, there is no collision
1438 // from here on we know d1 is >= 0 and d2 is < 0
1439 // this means the line starts infront and ends behind, passing through it
1441 // calculate the recipricol of the distance delta,
1442 // so we can use it multiple times cheaply (instead of division)
1443 d = 1.0f / (d1 - d2);
1444 // calculate the impact fraction by taking the start distance (> 0)
1445 // and subtracting the face plane distance (this is the distance of the
1446 // triangle along that same normal)
1447 // then multiply by the recipricol distance delta
1449 // skip out if this impact is further away than previous ones
1450 if (f > trace->realfraction)
1452 // calculate the perfect impact point for classification of insidedness
1453 impact[0] = linestart[0] + f * (lineend[0] - linestart[0]);
1454 impact[1] = linestart[1] + f * (lineend[1] - linestart[1]);
1455 impact[2] = linestart[2] + f * (lineend[2] - linestart[2]);
1457 // calculate the edge normal and reject if impact is outside triangle
1458 // (an edge normal faces away from the triangle, to get the desired normal
1459 // a crossproduct with the faceplanenormal is used, and because of the way
1460 // the insidedness comparison is written it does not need to be normalized)
1462 VectorSubtract(point2, point0, edge);
1463 CrossProduct(edge, faceplanenormal, edgenormal);
1464 if (DotProduct(impact, edgenormal) > DotProduct(point0, edgenormal))
1467 VectorSubtract(point0, point1, edge);
1468 CrossProduct(edge, faceplanenormal, edgenormal);
1469 if (DotProduct(impact, edgenormal) > DotProduct(point1, edgenormal))
1472 VectorSubtract(point1, point2, edge);
1473 CrossProduct(edge, faceplanenormal, edgenormal);
1474 if (DotProduct(impact, edgenormal) > DotProduct(point2, edgenormal))
1477 // store the new trace fraction
1478 trace->realfraction = bound(0, f, 1);
1480 // store the new trace plane (because collisions only happen from
1481 // the front this is always simply the triangle normal, never flipped)
1482 VectorNormalize(faceplanenormal);
1483 VectorCopy(faceplanenormal, trace->plane.normal);
1484 trace->plane.dist = DotProduct(point0, faceplanenormal);
1486 // calculate the normalized start and end distances
1487 d1 = DotProduct(trace->plane.normal, linestart) - trace->plane.dist;
1488 d2 = DotProduct(trace->plane.normal, lineend) - trace->plane.dist;
1490 // calculate a nudged fraction to keep it out of the surface
1491 // (the main fraction remains perfect)
1492 fnudged = (d1 - collision_impactnudge.value) / (d1 - d2);
1493 trace->fraction = bound(0, fnudged, 1);
1495 // store the new trace endpos
1496 // not needed, it's calculated later when the trace is finished
1497 //trace->endpos[0] = linestart[0] + fnudged * (lineend[0] - linestart[0]);
1498 //trace->endpos[1] = linestart[1] + fnudged * (lineend[1] - linestart[1]);
1499 //trace->endpos[2] = linestart[2] + fnudged * (lineend[2] - linestart[2]);
1500 trace->hitsupercontents = supercontents;
1501 trace->hitq3surfaceflags = q3surfaceflags;
1502 trace->hittexture = texture;
1506 typedef struct colbspnode_s
1509 struct colbspnode_s *children[2];
1510 // the node is reallocated or split if max is reached
1513 colbrushf_t **colbrushflist;
1516 //colbrushd_t **colbrushdlist;
1520 typedef struct colbsp_s
1523 colbspnode_t *nodes;
1527 colbsp_t *Collision_CreateCollisionBSP(mempool_t *mempool)
1530 bsp = (colbsp_t *)Mem_Alloc(mempool, sizeof(colbsp_t));
1531 bsp->mempool = mempool;
1532 bsp->nodes = (colbspnode_t *)Mem_Alloc(bsp->mempool, sizeof(colbspnode_t));
1536 void Collision_FreeCollisionBSPNode(colbspnode_t *node)
1538 if (node->children[0])
1539 Collision_FreeCollisionBSPNode(node->children[0]);
1540 if (node->children[1])
1541 Collision_FreeCollisionBSPNode(node->children[1]);
1542 while (--node->numcolbrushf)
1543 Mem_Free(node->colbrushflist[node->numcolbrushf]);
1544 //while (--node->numcolbrushd)
1545 // Mem_Free(node->colbrushdlist[node->numcolbrushd]);
1549 void Collision_FreeCollisionBSP(colbsp_t *bsp)
1551 Collision_FreeCollisionBSPNode(bsp->nodes);
1555 void Collision_BoundingBoxOfBrushTraceSegment(const colbrushf_t *start, const colbrushf_t *end, vec3_t mins, vec3_t maxs, float startfrac, float endfrac)
1558 colpointf_t *ps, *pe;
1559 float tempstart[3], tempend[3];
1560 VectorLerp(start->points[0].v, startfrac, end->points[0].v, mins);
1561 VectorCopy(mins, maxs);
1562 for (i = 0, ps = start->points, pe = end->points;i < start->numpoints;i++, ps++, pe++)
1564 VectorLerp(ps->v, startfrac, pe->v, tempstart);
1565 VectorLerp(ps->v, endfrac, pe->v, tempend);
1566 mins[0] = min(mins[0], min(tempstart[0], tempend[0]));
1567 mins[1] = min(mins[1], min(tempstart[1], tempend[1]));
1568 mins[2] = min(mins[2], min(tempstart[2], tempend[2]));
1569 maxs[0] = min(maxs[0], min(tempstart[0], tempend[0]));
1570 maxs[1] = min(maxs[1], min(tempstart[1], tempend[1]));
1571 maxs[2] = min(maxs[2], min(tempstart[2], tempend[2]));
1581 //===========================================
1583 void Collision_ClipToGenericEntity(trace_t *trace, dp_model_t *model, int frame, 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)
1585 float starttransformed[3], endtransformed[3];
1587 memset(trace, 0, sizeof(*trace));
1588 trace->fraction = trace->realfraction = 1;
1590 Matrix4x4_Transform(inversematrix, start, starttransformed);
1591 Matrix4x4_Transform(inversematrix, end, endtransformed);
1592 #if COLLISIONPARANOID >= 3
1593 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]);
1596 if (model && model->TraceBox)
1597 model->TraceBox(model, bound(0, frame, (model->numframes - 1)), trace, starttransformed, mins, maxs, endtransformed, hitsupercontentsmask);
1599 Collision_ClipTrace_Box(trace, bodymins, bodymaxs, starttransformed, mins, maxs, endtransformed, hitsupercontentsmask, bodysupercontents, 0, NULL);
1600 trace->fraction = bound(0, trace->fraction, 1);
1601 trace->realfraction = bound(0, trace->realfraction, 1);
1603 VectorLerp(start, trace->fraction, end, trace->endpos);
1605 // NOTE: this relies on plane.dist being directly after plane.normal
1606 Matrix4x4_TransformPositivePlane(matrix, trace->plane.normal[0], trace->plane.normal[1], trace->plane.normal[2], trace->plane.dist, trace->plane.normal);
1609 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)
1611 memset(trace, 0, sizeof(*trace));
1612 trace->fraction = trace->realfraction = 1;
1613 if (model && model->TraceBox)
1614 model->TraceBox(model, 0, trace, start, mins, maxs, end, hitsupercontents);
1615 trace->fraction = bound(0, trace->fraction, 1);
1616 trace->realfraction = bound(0, trace->realfraction, 1);
1617 VectorLerp(start, trace->fraction, end, trace->endpos);
1620 void Collision_ClipLineToGenericEntity(trace_t *trace, dp_model_t *model, int frame, 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)
1622 float starttransformed[3], endtransformed[3];
1624 memset(trace, 0, sizeof(*trace));
1625 trace->fraction = trace->realfraction = 1;
1627 Matrix4x4_Transform(inversematrix, start, starttransformed);
1628 Matrix4x4_Transform(inversematrix, end, endtransformed);
1629 #if COLLISIONPARANOID >= 3
1630 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]);
1633 if (model && model->TraceLine)
1634 model->TraceLine(model, bound(0, frame, (model->numframes - 1)), trace, starttransformed, endtransformed, hitsupercontentsmask);
1636 Collision_ClipTrace_Box(trace, bodymins, bodymaxs, starttransformed, vec3_origin, vec3_origin, endtransformed, hitsupercontentsmask, bodysupercontents, 0, NULL);
1637 trace->fraction = bound(0, trace->fraction, 1);
1638 trace->realfraction = bound(0, trace->realfraction, 1);
1640 VectorLerp(start, trace->fraction, end, trace->endpos);
1642 // NOTE: this relies on plane.dist being directly after plane.normal
1643 Matrix4x4_TransformPositivePlane(matrix, trace->plane.normal[0], trace->plane.normal[1], trace->plane.normal[2], trace->plane.dist, trace->plane.normal);
1646 void Collision_ClipLineToWorld(trace_t *trace, dp_model_t *model, const vec3_t start, const vec3_t end, int hitsupercontents)
1648 memset(trace, 0, sizeof(*trace));
1649 trace->fraction = trace->realfraction = 1;
1650 if (model && model->TraceLine)
1651 model->TraceLine(model, 0, trace, start, end, hitsupercontents);
1652 trace->fraction = bound(0, trace->fraction, 1);
1653 trace->realfraction = bound(0, trace->realfraction, 1);
1654 VectorLerp(start, trace->fraction, end, trace->endpos);
1657 void Collision_ClipPointToGenericEntity(trace_t *trace, dp_model_t *model, int frame, const vec3_t bodymins, const vec3_t bodymaxs, int bodysupercontents, matrix4x4_t *matrix, matrix4x4_t *inversematrix, const vec3_t start, int hitsupercontentsmask)
1659 float starttransformed[3];
1661 memset(trace, 0, sizeof(*trace));
1662 trace->fraction = trace->realfraction = 1;
1664 Matrix4x4_Transform(inversematrix, start, starttransformed);
1665 #if COLLISIONPARANOID >= 3
1666 Con_Printf("trans(%f %f %f -> %f %f %f)", start[0], start[1], start[2], starttransformed[0], starttransformed[1], starttransformed[2]);
1669 if (model && model->TracePoint)
1670 model->TracePoint(model, bound(0, frame, (model->numframes - 1)), trace, starttransformed, hitsupercontentsmask);
1672 Collision_ClipTrace_Point(trace, bodymins, bodymaxs, starttransformed, hitsupercontentsmask, bodysupercontents, 0, NULL);
1674 VectorCopy(start, trace->endpos);
1676 // NOTE: this relies on plane.dist being directly after plane.normal
1677 Matrix4x4_TransformPositivePlane(matrix, trace->plane.normal[0], trace->plane.normal[1], trace->plane.normal[2], trace->plane.dist, trace->plane.normal);
1680 void Collision_ClipPointToWorld(trace_t *trace, dp_model_t *model, const vec3_t start, int hitsupercontents)
1682 memset(trace, 0, sizeof(*trace));
1683 trace->fraction = trace->realfraction = 1;
1684 if (model && model->TracePoint)
1685 model->TracePoint(model, 0, trace, start, hitsupercontents);
1686 VectorCopy(start, trace->endpos);
1689 void Collision_CombineTraces(trace_t *cliptrace, const trace_t *trace, void *touch, qboolean isbmodel)
1691 // take the 'best' answers from the new trace and combine with existing data
1692 if (trace->allsolid)
1693 cliptrace->allsolid = true;
1694 if (trace->startsolid)
1697 cliptrace->bmodelstartsolid = true;
1698 cliptrace->startsolid = true;
1699 if (cliptrace->realfraction == 1)
1700 cliptrace->ent = touch;
1701 if (cliptrace->startdepth > trace->startdepth)
1703 cliptrace->startdepth = trace->startdepth;
1704 VectorCopy(trace->startdepthnormal, cliptrace->startdepthnormal);
1707 // don't set this except on the world, because it can easily confuse
1708 // monsters underwater if there's a bmodel involved in the trace
1709 // (inopen && inwater is how they check water visibility)
1710 //if (trace->inopen)
1711 // cliptrace->inopen = true;
1713 cliptrace->inwater = true;
1714 if ((trace->realfraction <= cliptrace->realfraction) && (VectorLength2(trace->plane.normal) > 0))
1716 cliptrace->fraction = trace->fraction;
1717 cliptrace->realfraction = trace->realfraction;
1718 VectorCopy(trace->endpos, cliptrace->endpos);
1719 cliptrace->plane = trace->plane;
1720 cliptrace->ent = touch;
1721 cliptrace->hitsupercontents = trace->hitsupercontents;
1722 cliptrace->hitq3surfaceflags = trace->hitq3surfaceflags;
1723 cliptrace->hittexture = trace->hittexture;
1725 cliptrace->startsupercontents |= trace->startsupercontents;
1728 void Collision_ShortenTrace(trace_t *trace, float shorten_factor, const vec3_t end)
1730 // now undo our moving end 1 qu farther...
1731 trace->fraction = bound(trace->fraction, trace->fraction / shorten_factor - 1e-6, 1); // we subtract 1e-6 to guard for roundoff errors
1732 trace->realfraction = bound(trace->realfraction, trace->realfraction / shorten_factor - 1e-6, 1); // we subtract 1e-6 to guard for roundoff errors
1733 if(trace->fraction >= 1) // trace would NOT hit if not expanded!
1735 trace->fraction = 1;
1736 trace->realfraction = 1;
1737 VectorCopy(end, trace->endpos);
1738 memset(&trace->plane, 0, sizeof(trace->plane));
1740 trace->hitsupercontentsmask = 0;
1741 trace->hitsupercontents = 0;
1742 trace->hitq3surfaceflags = 0;
1743 trace->hittexture = NULL;