Permalink
Browse files

Merge branch 'master' of https://github.com/Kazade/kazmath

  • Loading branch information...
2 parents 03e1b67 + 21ee048 commit eb277daee27a09d0d9302b8ba49bd3c7b3d71cad @Kazade committed Apr 13, 2012
Showing with 52 additions and 14 deletions.
  1. +34 −13 kazmath/ray2.c
  2. +1 −1 kazmath/utility.h
  3. +15 −0 kazmath/vec2.c
  4. +2 −0 kazmath/vec2.h
View
@@ -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
@@ -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
View
@@ -116,3 +116,18 @@ int kmVec2AreEqual(const kmVec2* p1, const kmVec2* p2)
(p1->y < p2->y + kmEpsilon && p1->y > p2->y - kmEpsilon)
);
}
+
+/**
+ * Assigns pIn to pOut. Returns pOut. If pIn and pOut are the same
+ * then nothing happens but pOut is still returned
+ */
+kmVec2* kmVec2Assign(kmVec2* pOut, const kmVec2* pIn) {
+ if (pOut == pIn) {
+ return pOut;
+ }
+
+ pOut->x = pIn->x;
+ pOut->y = pIn->y;
+
+ return pOut;
+}
View
@@ -44,6 +44,7 @@ typedef struct kmVec2 {
#ifdef __cplusplus
extern "C" {
#endif
+
kmVec2* kmVec2Fill(kmVec2* pOut, kmScalar x, kmScalar y);
kmScalar kmVec2Length(const kmVec2* pIn); ///< Returns the length of the vector
kmScalar kmVec2LengthSq(const kmVec2* pIn); ///< Returns the square of the length of the vector
@@ -55,6 +56,7 @@ kmVec2* kmVec2Transform(kmVec2* pOut, const kmVec2* pV1, const struct kmMat3* pM
kmVec2* kmVec2TransformCoord(kmVec2* pOut, const kmVec2* pV, const struct kmMat3* pM); ///<Transforms a 2D vector by a given matrix, projecting the result back into w = 1.
kmVec2* kmVec2Scale(kmVec2* pOut, const kmVec2* pIn, const kmScalar s); ///< Scales a vector to length s
int kmVec2AreEqual(const kmVec2* p1, const kmVec2* p2); ///< Returns 1 if both vectors are equal
+kmVec2* kmVec2Assign(kmVec2* pOut, const kmVec2* pIn);
#ifdef __cplusplus
}

0 comments on commit eb277da

Please sign in to comment.