Browse files

Add kmVec3RotationToDirection function

  • Loading branch information...
1 parent 4e2ae1e commit 03abcfef5c83b31bd0162e63fcdb10c8557e2991 @Cloudef Cloudef committed Apr 15, 2012
Showing with 41 additions and 0 deletions.
  1. +40 −0 kazmath/vec3.c
  2. +1 −0 kazmath/vec3.h
View
40 kazmath/vec3.c
@@ -336,3 +336,43 @@ kmVec3* kmVec3GetHorizontalAngle(kmVec3* pOut, const kmVec3 *pIn) {
return pOut;
}
+
+/**
+ * Builds a direction vector from input vector.
+ * Input vector is assumed to be rotation vector composed from 3 Euler angle rotations, in degrees.
+ * The forwards vector will be rotated by the input vector
+ *
+ * Code ported from Irrlicht: http://irrlicht.sourceforge.net/
+ */
+kmVec3* kmVec3RotationToDirection(kmVec3* pOut, const kmVec3* pIn, const kmVec3* forwards)
+{
+ const kmScalar xr = kmDegreesToRadians(pIn->x);
+ const kmScalar yr = kmDegreesToRadians(pIn->y);
+ const kmScalar zr = kmDegreesToRadians(pIn->z);
+ const kmScalar cr = cos(xr), sr = sin(xr);
+ const kmScalar cp = cos(yr), sp = cos(yr);
+ const kmScalar cy = cos(zr), sy = cos(zr);
+
+ const kmScalar srsp = sr*sp;
+ const kmScalar crsp = cr*sp;
+
+ const kmScalar pseudoMatrix[] = {
+ (cp*cy), (cp*sy), (-sp),
+ (srsp*cy-cr*sy), (srsp*sy+cr*cy), (sr*cp),
+ (crsp*cy+sr*sy), (crsp*sy-sr*cy), (cr*cp)
+ };
+
+ pOut->x = forwards->x * pseudoMatrix[0] +
+ forwards->y * pseudoMatrix[3] +
+ forwards->z * pseudoMatrix[6];
+
+ pOut->y = forwards->x * pseudoMatrix[1] +
+ forwards->y * pseudoMatrix[4] +
+ forwards->z * pseudoMatrix[7];
+
+ pOut->z = forwards->x * pseudoMatrix[2] +
+ forwards->y * pseudoMatrix[5] +
+ forwards->z * pseudoMatrix[8];
+
+ return pOut;
+}
View
1 kazmath/vec3.h
@@ -62,6 +62,7 @@ kmVec3* kmVec3InverseTransformNormal(kmVec3* pOut, const kmVec3* pVect, const st
kmVec3* kmVec3Assign(kmVec3* pOut, const kmVec3* pIn);
kmVec3* kmVec3Zero(kmVec3* pOut);
kmVec3* kmVec3GetHorizontalAngle(kmVec3* pOut, const kmVec3 *pIn); /** Get the rotations that would make a (0,0,1) direction vector point in the same direction as this direction vector. */
+kmVec3* kmVec3RotationToDirection(kmVec3* pOut, const kmVec3* pIn, const kmVec3* forwards); /** Builds a direction vector from input vector. */
#ifdef __cplusplus
}

0 comments on commit 03abcfe

Please sign in to comment.