void Collision_TraceBrushBrushFloat(trace_t *trace, const colbrushf_t *thisbrush_start, const colbrushf_t *thisbrush_end, const colbrushf_t *thatbrush_start, const colbrushf_t *thatbrush_end)
{
int nplane, nplane2, hitq3surfaceflags = 0;
- float enterfrac = -1, leavefrac = 1, d1, d2, f, imove, newimpactnormal[3], enterfrac2 = -1;
+ float enterfrac = -1, leavefrac = 1, d1, d2, s, e, ie, f, imove, enterfrac2 = -1;
const colplanef_t *startplane, *endplane;
+ plane_t newimpactplane;
texture_t *hittexture = NULL;
- VectorClear(newimpactnormal);
+ VectorClear(newimpactplane.normal);
+ newimpactplane.dist = 0;
for (nplane = 0;nplane < thatbrush_start->numplanes + thisbrush_start->numplanes;nplane++)
{
if (fabs(f - startplane->dist) > COLLISION_PLANE_DIST_EPSILON)
Con_Printf("startplane->dist %f != calculated %f (thisbrush_start)\n", startplane->dist, f);
}
- d1 = nearestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints) - furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints) - collision_startnudge.value;
- d2 = nearestplanedist_float(endplane->normal, thisbrush_end->points, thisbrush_end->numpoints) - furthestplanedist_float(endplane->normal, thatbrush_end->points, thatbrush_end->numpoints) - collision_endnudge.value;
+ s = furthestplanedist_float(startplane->normal, thatbrush_start->points, thatbrush_start->numpoints);
+ e = furthestplanedist_float(endplane->normal, thatbrush_end->points, thatbrush_end->numpoints);
+ d1 = nearestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints) - s - collision_startnudge.value;
+ d2 = nearestplanedist_float(endplane->normal, thisbrush_end->points, thisbrush_end->numpoints) - e - collision_endnudge.value;
}
else
{
if (fabs(f - startplane->dist) > COLLISION_PLANE_DIST_EPSILON)
Con_Printf("startplane->dist %f != calculated %f (thatbrush_start)\n", startplane->dist, f);
}
- d1 = nearestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints) - startplane->dist - collision_startnudge.value;
- d2 = nearestplanedist_float(endplane->normal, thisbrush_end->points, thisbrush_end->numpoints) - endplane->dist - collision_endnudge.value;
+ s = startplane->dist;
+ e = endplane->dist;
+ d1 = nearestplanedist_float(startplane->normal, thisbrush_start->points, thisbrush_start->numpoints) - s - collision_startnudge.value;
+ d2 = nearestplanedist_float(endplane->normal, thisbrush_end->points, thisbrush_end->numpoints) - e - collision_endnudge.value;
}
//Con_Printf("%c%i: d1 = %f, d2 = %f, d1 / (d1 - d2) = %f\n", nplane2 != nplane ? 'b' : 'a', nplane2, d1, d2, d1 / (d1 - d2));
// calculate the nudged fraction and impact normal we'll
// need if we accept this collision later
enterfrac2 = (d1 - collision_impactnudge.value) * imove;
- VectorLerp(startplane->normal, enterfrac, endplane->normal, newimpactnormal);
+ ie = 1.0f - enterfrac;
+ newimpactplane.normal[0] = startplane->normal[0] * ie + endplane->normal[0] * enterfrac;
+ newimpactplane.normal[1] = startplane->normal[1] * ie + endplane->normal[1] * enterfrac;
+ newimpactplane.normal[2] = startplane->normal[2] * ie + endplane->normal[2] * enterfrac;
+ newimpactplane.dist = s * ie + e * enterfrac;
hitq3surfaceflags = startplane->q3surfaceflags;
hittexture = startplane->texture;
}
trace->fraction = bound(0, enterfrac2, 1);
if (collision_prefernudgedfraction.integer)
trace->realfraction = trace->fraction;
- VectorCopy(newimpactnormal, trace->plane.normal);
+ trace->plane = newimpactplane;
}
}
else
trace->startsolid = true;
if (leavefrac < 1)
trace->allsolid = true;
+ trace->plane = newimpactplane;
}
}
}
void Collision_TraceLineBrushFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, const colbrushf_t *thatbrush_start, const colbrushf_t *thatbrush_end)
{
int nplane, hitq3surfaceflags = 0;
- float enterfrac = -1, leavefrac = 1, d1, d2, f, imove, newimpactnormal[3], enterfrac2 = -1;
+ float enterfrac = -1, leavefrac = 1, d1, d2, ie, f, imove, enterfrac2 = -1;
const colplanef_t *startplane, *endplane;
+ plane_t newimpactplane;
texture_t *hittexture = NULL;
- VectorClear(newimpactnormal);
+ VectorClear(newimpactplane.normal);
+ newimpactplane.dist = 0;
for (nplane = 0;nplane < thatbrush_start->numplanes;nplane++)
{
// calculate the nudged fraction and impact normal we'll
// need if we accept this collision later
enterfrac2 = (d1 - collision_impactnudge.value) * imove;
- VectorLerp(startplane->normal, enterfrac, endplane->normal, newimpactnormal);
+ ie = 1.0f - enterfrac;
+ newimpactplane.normal[0] = startplane->normal[0] * ie + endplane->normal[0] * enterfrac;
+ newimpactplane.normal[1] = startplane->normal[1] * ie + endplane->normal[1] * enterfrac;
+ newimpactplane.normal[2] = startplane->normal[2] * ie + endplane->normal[2] * enterfrac;
+ newimpactplane.dist = startplane->dist * ie + endplane->dist * enterfrac;
hitq3surfaceflags = startplane->q3surfaceflags;
hittexture = startplane->texture;
}
trace->fraction = bound(0, enterfrac2, 1);
if (collision_prefernudgedfraction.integer)
trace->realfraction = trace->fraction;
- VectorCopy(newimpactnormal, trace->plane.normal);
+ trace->plane = newimpactplane;
}
}
else
trace->startsolid = true;
if (leavefrac < 1)
trace->allsolid = true;
+ trace->plane = newimpactplane;
}
}
}
-void Collision_TracePointBrushFloat(trace_t *trace, const vec3_t point, const colbrushf_t *thatbrush)
+qboolean Collision_PointInsideBrushFloat(const vec3_t point, const colbrushf_t *brush)
{
int nplane;
const colplanef_t *plane;
- for (nplane = 0, plane = thatbrush->planes;nplane < thatbrush->numplanes;nplane++, plane++)
+ if (!BoxesOverlap(point, point, brush->mins, brush->maxs))
+ return false;
+ for (nplane = 0, plane = brush->planes;nplane < brush->numplanes;nplane++, plane++)
if (DotProduct(plane->normal, point) > plane->dist)
- return;
+ return false;
+ return true;
+}
+
+void Collision_TracePointBrushFloat(trace_t *trace, const vec3_t point, const colbrushf_t *thatbrush)
+{
+ if (!Collision_PointInsideBrushFloat(point, thatbrush))
+ return;
trace->startsupercontents |= thatbrush->supercontents;
if (trace->hitsupercontentsmask & thatbrush->supercontents)
Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brush, &polyf_brush);
}
-void Collision_TraceBrushTriangleMeshFloat(trace_t *trace, const colbrushf_t *thisbrush_start, const colbrushf_t *thisbrush_end, int numtriangles, const int *element3i, const float *vertex3f, int supercontents, int q3surfaceflags, texture_t *texture, const vec3_t segmentmins, const vec3_t segmentmaxs)
+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)
{
int i;
polyf_brush.numpoints = 3;
polyf_brush.planes[i].q3surfaceflags = q3surfaceflags;
polyf_brush.planes[i].texture = texture;
}
- for (i = 0;i < numtriangles;i++, element3i += 3)
+ if(stride)
{
- if (TriangleOverlapsBox(vertex3f + element3i[0]*3, vertex3f + element3i[1]*3, vertex3f + element3i[2]*3, segmentmins, segmentmaxs))
+ int k, cnt, tri;
+ cnt = (numtriangles + stride - 1) / stride;
+ for(i = 0; i < cnt; ++i)
{
- VectorCopy(vertex3f + element3i[0] * 3, polyf_points[0].v);
- VectorCopy(vertex3f + element3i[1] * 3, polyf_points[1].v);
- VectorCopy(vertex3f + element3i[2] * 3, polyf_points[2].v);
- Collision_SnapCopyPoints(polyf_brush.numpoints, polyf_points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
- Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
- //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
- Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brush, &polyf_brush);
+ if(BoxesOverlap(bbox6f + i * 6, bbox6f + i * 6 + 3, segmentmins, segmentmaxs))
+ {
+ for(k = 0; k < stride; ++k)
+ {
+ tri = i * stride + k;
+ if(tri >= numtriangles)
+ break;
+ VectorCopy(vertex3f + element3i[tri * 3 + 0] * 3, polyf_points[0].v);
+ VectorCopy(vertex3f + element3i[tri * 3 + 1] * 3, polyf_points[1].v);
+ VectorCopy(vertex3f + element3i[tri * 3 + 2] * 3, polyf_points[2].v);
+ Collision_SnapCopyPoints(polyf_brush.numpoints, polyf_points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
+ Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
+ //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
+ Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brush, &polyf_brush);
+ }
+ }
+ }
+ }
+ else
+ {
+ for (i = 0;i < numtriangles;i++, element3i += 3)
+ {
+ if (TriangleOverlapsBox(vertex3f + element3i[0]*3, vertex3f + element3i[1]*3, vertex3f + element3i[2]*3, segmentmins, segmentmaxs))
+ {
+ VectorCopy(vertex3f + element3i[0] * 3, polyf_points[0].v);
+ VectorCopy(vertex3f + element3i[1] * 3, polyf_points[1].v);
+ VectorCopy(vertex3f + element3i[2] * 3, polyf_points[2].v);
+ Collision_SnapCopyPoints(polyf_brush.numpoints, polyf_points, polyf_points, COLLISION_SNAPSCALE, COLLISION_SNAP);
+ Collision_CalcPlanesForPolygonBrushFloat(&polyf_brush);
+ //Collision_PrintBrushAsQHull(&polyf_brush, "polyf_brush");
+ Collision_TraceBrushBrushFloat(trace, thisbrush_start, thisbrush_end, &polyf_brush, &polyf_brush);
+ }
}
}
}
Collision_TraceLineBrushFloat(trace, linestart, lineend, &polyf_brush, &polyf_brush);
}
-void Collision_TraceLineTriangleMeshFloat(trace_t *trace, const vec3_t linestart, const vec3_t lineend, int numtriangles, const int *element3i, const float *vertex3f, int supercontents, int q3surfaceflags, texture_t *texture, const vec3_t segmentmins, const vec3_t segmentmaxs)
+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)
{
int i;
#if 1
// FIXME: snap vertices?
- for (i = 0;i < numtriangles;i++, element3i += 3)
- Collision_TraceLineTriangleFloat(trace, linestart, lineend, vertex3f + element3i[0] * 3, vertex3f + element3i[1] * 3, vertex3f + element3i[2] * 3, supercontents, q3surfaceflags, texture);
+ if(stride)
+ {
+ int k, cnt, tri;
+ cnt = (numtriangles + stride - 1) / stride;
+ for(i = 0; i < cnt; ++i)
+ {
+ if(BoxesOverlap(bbox6f + i * 6, bbox6f + i * 6 + 3, segmentmins, segmentmaxs))
+ {
+ for(k = 0; k < stride; ++k)
+ {
+ tri = i * stride + k;
+ if(tri >= numtriangles)
+ break;
+ 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);
+ }
+ }
+ }
+ }
+ else
+ {
+ for (i = 0;i < numtriangles;i++, element3i += 3)
+ Collision_TraceLineTriangleFloat(trace, linestart, lineend, vertex3f + element3i[0] * 3, vertex3f + element3i[1] * 3, vertex3f + element3i[2] * 3, supercontents, q3surfaceflags, texture);
+ }
#else
polyf_brush.numpoints = 3;
polyf_brush.numplanes = 5;
#define MAX_BRUSHFORBOX 16
-static int brushforbox_index = 0;
+static unsigned int brushforbox_index = 0;
+// note: this relies on integer overflow to be consistent with modulo
+// MAX_BRUSHFORBOX, or in other words, MAX_BRUSHFORBOX must be a power of two!
static colpointf_t brushforbox_point[MAX_BRUSHFORBOX*8];
static colplanef_t brushforbox_plane[MAX_BRUSHFORBOX*6];
static colbrushf_t brushforbox_brush[MAX_BRUSHFORBOX];
//===========================================
-void Collision_ClipToGenericEntity(trace_t *trace, 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)
+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)
{
- float tempnormal[3], starttransformed[3], endtransformed[3];
+ float starttransformed[3], endtransformed[3];
memset(trace, 0, sizeof(*trace));
trace->fraction = trace->realfraction = 1;
- VectorCopy(end, trace->endpos);
Matrix4x4_Transform(inversematrix, start, starttransformed);
Matrix4x4_Transform(inversematrix, end, endtransformed);
trace->fraction = bound(0, trace->fraction, 1);
trace->realfraction = bound(0, trace->realfraction, 1);
- if (trace->fraction < 1)
- {
- VectorLerp(start, trace->fraction, end, trace->endpos);
- VectorCopy(trace->plane.normal, tempnormal);
- Matrix4x4_Transform3x3(matrix, tempnormal, trace->plane.normal);
- // FIXME: should recalc trace->plane.dist
- }
+ VectorLerp(start, trace->fraction, end, trace->endpos);
+ // transform plane
+ // NOTE: this relies on plane.dist being directly after plane.normal
+ Matrix4x4_TransformPositivePlane(matrix, trace->plane.normal[0], trace->plane.normal[1], trace->plane.normal[2], trace->plane.dist, trace->plane.normal);
}
-void Collision_ClipToWorld(trace_t *trace, model_t *model, const vec3_t start, const vec3_t mins, const vec3_t maxs, const vec3_t end, int hitsupercontents)
+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)
{
memset(trace, 0, sizeof(*trace));
trace->fraction = trace->realfraction = 1;
VectorLerp(start, trace->fraction, end, trace->endpos);
}
+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)
+{
+ float starttransformed[3], endtransformed[3];
+
+ memset(trace, 0, sizeof(*trace));
+ trace->fraction = trace->realfraction = 1;
+
+ Matrix4x4_Transform(inversematrix, start, starttransformed);
+ Matrix4x4_Transform(inversematrix, end, endtransformed);
+#if COLLISIONPARANOID >= 3
+ 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]);
+#endif
+
+ if (model && model->TraceLine)
+ model->TraceLine(model, bound(0, frame, (model->numframes - 1)), trace, starttransformed, endtransformed, hitsupercontentsmask);
+ else
+ Collision_ClipTrace_Box(trace, bodymins, bodymaxs, starttransformed, vec3_origin, vec3_origin, endtransformed, hitsupercontentsmask, bodysupercontents, 0, NULL);
+ trace->fraction = bound(0, trace->fraction, 1);
+ trace->realfraction = bound(0, trace->realfraction, 1);
+
+ VectorLerp(start, trace->fraction, end, trace->endpos);
+ // transform plane
+ // NOTE: this relies on plane.dist being directly after plane.normal
+ Matrix4x4_TransformPositivePlane(matrix, trace->plane.normal[0], trace->plane.normal[1], trace->plane.normal[2], trace->plane.dist, trace->plane.normal);
+}
+
+void Collision_ClipLineToWorld(trace_t *trace, dp_model_t *model, const vec3_t start, const vec3_t end, int hitsupercontents)
+{
+ memset(trace, 0, sizeof(*trace));
+ trace->fraction = trace->realfraction = 1;
+ if (model && model->TraceLine)
+ model->TraceLine(model, 0, trace, start, end, hitsupercontents);
+ trace->fraction = bound(0, trace->fraction, 1);
+ trace->realfraction = bound(0, trace->realfraction, 1);
+ VectorLerp(start, trace->fraction, end, trace->endpos);
+}
+
+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)
+{
+ float starttransformed[3];
+
+ memset(trace, 0, sizeof(*trace));
+ trace->fraction = trace->realfraction = 1;
+
+ Matrix4x4_Transform(inversematrix, start, starttransformed);
+#if COLLISIONPARANOID >= 3
+ Con_Printf("trans(%f %f %f -> %f %f %f)", start[0], start[1], start[2], starttransformed[0], starttransformed[1], starttransformed[2]);
+#endif
+
+ if (model && model->TracePoint)
+ model->TracePoint(model, bound(0, frame, (model->numframes - 1)), trace, starttransformed, hitsupercontentsmask);
+ else
+ Collision_ClipTrace_Point(trace, bodymins, bodymaxs, starttransformed, hitsupercontentsmask, bodysupercontents, 0, NULL);
+
+ VectorCopy(start, trace->endpos);
+ // transform plane
+ // NOTE: this relies on plane.dist being directly after plane.normal
+ Matrix4x4_TransformPositivePlane(matrix, trace->plane.normal[0], trace->plane.normal[1], trace->plane.normal[2], trace->plane.dist, trace->plane.normal);
+}
+
+void Collision_ClipPointToWorld(trace_t *trace, dp_model_t *model, const vec3_t start, int hitsupercontents)
+{
+ memset(trace, 0, sizeof(*trace));
+ trace->fraction = trace->realfraction = 1;
+ if (model && model->TracePoint)
+ model->TracePoint(model, 0, trace, start, hitsupercontents);
+ VectorCopy(start, trace->endpos);
+}
+
void Collision_CombineTraces(trace_t *cliptrace, const trace_t *trace, void *touch, qboolean isbmodel)
{
// take the 'best' answers from the new trace and combine with existing data
// cliptrace->inopen = true;
if (trace->inwater)
cliptrace->inwater = true;
- if (trace->realfraction < cliptrace->realfraction)
+ if ((trace->realfraction <= cliptrace->realfraction) && (VectorLength2(trace->plane.normal) > 0))
{
cliptrace->fraction = trace->fraction;
cliptrace->realfraction = trace->realfraction;
}
cliptrace->startsupercontents |= trace->startsupercontents;
}
+
+void Collision_ShortenTrace(trace_t *trace, float shorten_factor, const vec3_t end)
+{
+ // now undo our moving end 1 qu farther...
+ trace->fraction = bound(trace->fraction, trace->fraction / shorten_factor - 1e-6, 1); // we subtract 1e-6 to guard for roundoff errors
+ trace->realfraction = bound(trace->realfraction, trace->realfraction / shorten_factor - 1e-6, 1); // we subtract 1e-6 to guard for roundoff errors
+ if(trace->fraction >= 1) // trace would NOT hit if not expanded!
+ {
+ trace->fraction = 1;
+ trace->realfraction = 1;
+ VectorCopy(end, trace->endpos);
+ memset(&trace->plane, 0, sizeof(trace->plane));
+ trace->ent = NULL;
+ trace->hitsupercontentsmask = 0;
+ trace->hitsupercontents = 0;
+ trace->hitq3surfaceflags = 0;
+ trace->hittexture = NULL;
+ }
+}