Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Reduce the epsilon value (need something better really). Make ray int…

…ersections always return the correct collision normal
  • Loading branch information...
commit 21ee048d2c7db1cf67990d2a3151efb6614cbb1f 1 parent af9f66b
@Kazade authored
Showing with 35 additions and 14 deletions.
  1. +34 −13 kazmath/ray2.c
  2. +1 −1  kazmath/utility.h
View
47 kazmath/ray2.c
@@ -106,15 +106,36 @@ kmBool kmRay2IntersectLineSegment(const kmRay2* ray, const kmVec2* p1, const kmV
return KM_TRUE;*/
}
-void calculate_line_normal(kmVec2 p1, kmVec2 p2, kmVec2* normal_out) {
- kmVec2 tmp;
- kmVec2Subtract(&tmp, &p2, &p1); //Get direction vector
-
- normal_out->x = -tmp.y;
- normal_out->y = tmp.x;
- kmVec2Normalize(normal_out, normal_out);
-
- //TODO: should check that the normal is pointing out of the triangle
+void calculate_line_normal(kmVec2 p1, kmVec2 p2, kmVec2 other_point, kmVec2* normal_out) {
+ /*
+ A = (3,4)
+ B = (2,1)
+ C = (1,3)
+
+ AB = (2,1) - (3,4) = (-1,-3)
+ AC = (1,3) - (3,4) = (-2,-1)
+ N = n(AB) = (-3,1)
+ D = dot(N,AC) = 6 + -1 = 5
+
+ since D > 0:
+ N = -N = (3,-1)
+ */
+
+ kmVec2 edge, other_edge;
+ kmVec2Subtract(&edge, &p2, &p1);
+ kmVec2Subtract(&other_edge, &other_point, &p1);
+
+ kmVec2 n;
+ n.x = edge.y;
+ n.y = -edge.x;
+
+ float d = kmVec2Dot(&n, &other_edge);
+ if(d > 0.0f) {
+ n.x = -n.x;
+ n.y = -n.y;
+ }
+ normal_out->x = n.x;
+ normal_out->y = n.y;
}
kmBool kmRay2IntersectTriangle(const kmRay2* ray, const kmVec2* p1, const kmVec2* p2, const kmVec2* p3, kmVec2* intersection, kmVec2* normal_out) {
@@ -134,7 +155,7 @@ kmBool kmRay2IntersectTriangle(const kmRay2* ray, const kmVec2* p1, const kmVec2
final_intersect.y = intersect.y;
distance = this_distance;
- calculate_line_normal(*p1, *p2, &normal);
+ calculate_line_normal(*p1, *p2, *p3, &normal);
}
}
@@ -148,7 +169,7 @@ kmBool kmRay2IntersectTriangle(const kmRay2* ray, const kmVec2* p1, const kmVec2
final_intersect.y = intersect.y;
distance = this_distance;
- calculate_line_normal(*p2, *p3, &normal);
+ calculate_line_normal(*p2, *p3, *p1, &normal);
}
}
@@ -161,8 +182,8 @@ kmBool kmRay2IntersectTriangle(const kmRay2* ray, const kmVec2* p1, const kmVec2
final_intersect.x = intersect.x;
final_intersect.y = intersect.y;
distance = this_distance;
-
- calculate_line_normal(*p3, *p1, &normal);
+
+ calculate_line_normal(*p3, *p1, *p2, &normal);
}
}
View
2  kazmath/utility.h
@@ -51,7 +51,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define kmPI 3.141592f
#define kmPIOver180 0.017453f // PI / 180
#define kmPIUnder180 57.295779f // 180 / PI
-#define kmEpsilon 1.0 / 64.0
+#define kmEpsilon 0.00001
Please sign in to comment.
Something went wrong with that request. Please try again.