Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

math.h 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*!
  2. *
  3. * \file include/utils/math.h
  4. * \brief Vector and Matrix math
  5. *
  6. * \author Mongoose
  7. */
  8. #include <math.h>
  9. #ifndef _UTILS_MATH_H
  10. #define _UTILS_MATH_H
  11. #define HEL_PI ((float)M_PI) //!< pi
  12. #define HEL_2_PI (HEL_PI * 2.0f) //!< pi*2
  13. #define HEL_PI_OVER_4 (HEL_PI / 4.0f) //!< pi/4
  14. #define HEL_PI_OVER_180 (HEL_PI / 180.0f) //!< pi/180
  15. #define HEL_180_OVER_PI (180.0f / HEL_PI) //!< 180/pi
  16. #define HEL_RAD_TO_DEG(x) ((x) * HEL_180_OVER_PI) //!< Convert radians to degrees
  17. #define HEL_DEG_TO_RAD(x) ((x) * HEL_PI_OVER_180) //!< Convert degrees to radians
  18. typedef float vec_t; //!< 1D Vector, aka float
  19. typedef float vec2_t[2]; //!< 2D Vector
  20. typedef float vec3_t[3]; //!< 3D Vector
  21. typedef float vec4_t[4]; //!< 4D Vector
  22. typedef vec_t matrix_t[16]; //!< Used as _Column_major_ in every class now!
  23. /*!
  24. * \brief Compare two floats with an Epsilon.
  25. * \param a first float
  26. * \param b second float
  27. * \returns true if a and b are probably the same.
  28. */
  29. bool equalEpsilon(vec_t a, vec_t b);
  30. /*!
  31. * \brief Calculate Intersection of a line and a polygon
  32. * \param intersect Where the intersection is stored, if it exists
  33. * \param p1 First point of line segment
  34. * \param p2 Second point of line segment
  35. * \param polygon polygon vertex array (0 to 2 are used)
  36. * \returns 0 if there is no intersection
  37. */
  38. int helIntersectionLineAndPolygon(vec3_t intersect, vec3_t p1, vec3_t p2, vec3_t *polygon);
  39. /*!
  40. * \brief Calculate the distance from a sphere to a plane
  41. * \param center Center of sphere
  42. * \param radius Radius of sphere
  43. * \param plane Plane
  44. * \returns distance
  45. */
  46. vec_t helDistToSphereFromPlane3v(vec3_t center, vec_t radius, vec4_t plane);
  47. /*!
  48. * \brief Calculate the distance from a box to a plane
  49. * \param min Minimum Point of a bounding box
  50. * \param max Maximum Point of a bounding box
  51. * \param plane Plane
  52. * \returns distance
  53. */
  54. vec_t helDistToBboxFromPlane3v(vec3_t min, vec3_t max, vec4_t plane);
  55. /*!
  56. * \brief Calculate the length of a line segment / the distance between two points
  57. * \param a First point
  58. * \param b Second point
  59. * \returns distance/length
  60. */
  61. vec_t helDist3v(vec3_t a, vec3_t b);
  62. /*!
  63. * \brief Calculates the midpoint between two points / of a line segment
  64. * \param a First point
  65. * \param b Second point
  66. * \param mid Midpoint will be stored here
  67. */
  68. void helMidpoint3v(vec3_t a, vec3_t b, vec3_t mid);
  69. /*!
  70. * \brief Calculates a pseudo-random number
  71. * \param from Lower bound of resulting number
  72. * \param to Upper bound
  73. * \returns random number
  74. */
  75. vec_t helRandomNum(vec_t from, vec_t to);
  76. #endif