Fix triangle intersections. Add a kmAlmostEqual utility function

1 parent bf50186 commit 51dd7b473e082bcbbbbb1bcb85bee88cfbc611d3 committed Jan 28, 2011
Showing with 39 additions and 12 deletions.
1. +1 −0 include/kazmath/utility.h
2. +34 −12 src/ray2.c
3. +4 −0 src/utility.c
1 include/kazmath/utility.h
 @@ -65,6 +65,7 @@ extern kmScalar kmRadiansToDegrees(kmScalar radians); extern kmScalar min(kmScalar lhs, kmScalar rhs); extern kmScalar max(kmScalar lhs, kmScalar rhs); +extern kmBool kmAlmostEqual(kmScalar lhs, kmScalar rhs); #ifdef __cplusplus }
46 src/ray2.c
 @@ -119,34 +119,56 @@ void calculate_line_normal(kmVec2 p1, kmVec2 p2, kmVec2* normal_out) { kmBool kmRay2IntersectTriangle(const kmRay2* ray, const kmVec2* p1, const kmVec2* p2, const kmVec2* p3, kmVec2* intersection, kmVec2* normal_out) { kmVec2 intersect; + kmVec2 final_intersect; kmVec2 normal; - + kmScalar distance = 10000.0f; kmBool intersected = KM_FALSE; if(kmRay2IntersectLineSegment(ray, p1, p2, &intersect)) { - intersection->x = intersect.x; - intersection->y = intersect.y; intersected = KM_TRUE; - calculate_line_normal(*p1, *p2, &normal); + + kmVec2 tmp; + kmScalar this_distance = kmVec2Length(kmVec2Subtract(&tmp, &intersect, &ray->start)); + if(this_distance < distance) { + final_intersect.x = intersect.x; + final_intersect.y = intersect.y; + distance = this_distance; + + calculate_line_normal(*p1, *p2, &normal); + } } if(kmRay2IntersectLineSegment(ray, p2, p3, &intersect)) { - intersection->x = intersect.x; - intersection->y = intersect.y; intersected = KM_TRUE; - calculate_line_normal(*p2, *p3, &normal); + + kmVec2 tmp; + kmScalar this_distance = kmVec2Length(kmVec2Subtract(&tmp, &intersect, &ray->start)); + if(this_distance < distance) { + final_intersect.x = intersect.x; + final_intersect.y = intersect.y; + distance = this_distance; + + calculate_line_normal(*p2, *p3, &normal); + } } if(kmRay2IntersectLineSegment(ray, p3, p1, &intersect)) { - intersection->x = intersect.x; - intersection->y = intersect.y; intersected = KM_TRUE; - calculate_line_normal(*p3, *p1, &normal); + + kmVec2 tmp; + kmScalar this_distance = kmVec2Length(kmVec2Subtract(&tmp, &intersect, &ray->start)); + if(this_distance < distance) { + final_intersect.x = intersect.x; + final_intersect.y = intersect.y; + distance = this_distance; + + calculate_line_normal(*p3, *p1, &normal); + } } if(intersected) { - intersection->x = intersect.x; - intersection->y = intersect.y; + intersection->x = final_intersect.x; + intersection->y = final_intersect.y; if(normal_out) { normal_out->x = normal.x; normal_out->y = normal.y;
4 src/utility.c
 @@ -53,3 +53,7 @@ kmScalar min(kmScalar lhs, kmScalar rhs) { kmScalar max(kmScalar lhs, kmScalar rhs) { return (lhs > rhs)? lhs : rhs; } + +kmBool kmAlmostEqual(kmScalar lhs, kmScalar rhs) { + return (lhs + kmEpsilon > rhs && lhs - kmEpsilon < rhs); +}