Skip to content

Instantly share code, notes, and snippets.

@dwilliamson
Created January 29, 2018 22:00
Show Gist options
  • Select an option

  • Save dwilliamson/a52a6b9757c9d465b82334bd47bb1a04 to your computer and use it in GitHub Desktop.

Select an option

Save dwilliamson/a52a6b9757c9d465b82334bd47bb1a04 to your computer and use it in GitHub Desktop.
Swept ray test for character->world ledge hang intersection checks
// | = dot product, ^ = cross product. Please forgive my younger self for his sins.
void UpdateMinMax(float value, float& min, float& max)
{
if (value < min)
min = value;
else if (value > max)
max = value;
}
void ProjectTriangleOntoAxis(const cVector3 points[3], const cVector3& axis, float& min_t, float& max_t)
{
// Initial projection of first point
min_t = points[0] | axis;
max_t = min_t;
// Project remaining points and adjust projection range
UpdateMinMax(points[1] | axis, min_t, max_t);
UpdateMinMax(points[2] | axis, min_t, max_t);
}
void ProjectParallelogramOntoAxis(const cVector3& pos, const cVector3 edges[2], const cVector3& axis, float& min_t, float& max_t)
{
// Initial projection of first point
min_t = pos | axis;
max_t = min_t;
// Project remaining points and adjust projection range
UpdateMinMax((pos + edges[0]) | axis, min_t, max_t);
UpdateMinMax((pos + edges[1]) | axis, min_t, max_t);
UpdateMinMax((pos + edges[0] + edges[1]) | axis, min_t, max_t);
}
bool TestParallelogramTriangle(const cVector3& pos, const cVector3 edges[2], const cVector3 points[3], const cVector3& normal)
{
float d, min0, max0, min1, max1;
// Big list of SATs. Only need to test for Fa + Fb + Ea * Eb axes, which in this case
// is 1 + 1 + 2 * 3 = 8.
// In all cases both objects are being projected onto the same axes. Since the only required
// result of these comparisons is an overlap test we can ignore normalisation of vectors. Of course,
// this might include numerical errors for very large or small objects but we need to see how big
// or small this is before we can make any decisions as to how to efficiently solve the problem.
// Check axis of triangle normal
d = points[0] | normal;
min0 = d;
max0 = min0;
ProjectParallelogramOntoAxis(pos, edges, normal, min1, max1);
if (max1 < min0 || max0 < min1)
return (false);
// Check axis of parallelogram normal
cVector3 pn = edges[0] ^ edges[1];
min0 = pos | pn;
max0 = min0;
ProjectTriangleOntoAxis(points, pn, min1, max1);
if (max1 < min0 || max0 < min1)
return (false);
cVector3 tri_edges[] =
{
points[1] - points[0],
points[2] - points[1],
points[0] - points[2]
};
// Check permutations of edge cross products
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
cVector3 axis = edges[i] ^ tri_edges[j];
ProjectTriangleOntoAxis(points, axis, min0, max0);
ProjectParallelogramOntoAxis(pos, edges, axis, min1, max1);
if (max1 < min0 || max0 < min1)
return (false);
}
}
return (true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment