tinyphysicsengine.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. #ifndef TINYPHYSICSENGINE_H
  2. #define TINYPHYSICSENGINE_H
  3. /**
  4. author: Miloslav Ciz
  5. license: CC0 1.0 (public domain)
  6. found at https://creativecommons.org/publicdomain/zero/1.0/
  7. + additional waiver of all IP
  8. version: 0.1d
  9. CONVENTIONS:
  10. - Compatibility and simple usage with small3dlib is intended, so most
  11. convention and data types copy those of small3dlib (which takes a lot of
  12. conventions of OpenGL).
  13. - No floating point is used, we instead use integers (effectively a fixed
  14. point). TPE_FRACTIONS_PER_UNIT is an equivalent to 1.0 in floating point and
  15. all numbers are normalized by this constant.
  16. - Units: for any measure only an abstract mathematical unit is used. This unit
  17. always has TPE_FRACTIONS_PER_UNIT parts. You can assign any correcpondence
  18. with real life units to these units. E.g. 1 spatial unit (which you can see
  19. as e.g. 1 meter) is equal to TPE_FRACTIONS_PER_UNIT. Same with temporatl
  20. (e.g. 1 second) and mass (e.g. 1 kilogram) units, and also any derived
  21. units, e.g. a unit of velocity (e.g. 1 m/s) is also equal to 1
  22. TPE_FRACTIONS_PER_UNIT. A full angle is also split into
  23. TPE_FRACTIONS_PER_UNIT parts (instead of 2 * PI or degrees).
  24. - Quaternions are represented as vec4 where x ~ i, y ~ j, z ~ k, w ~ real.
  25. */
  26. #include <stdint.h>
  27. typedef int32_t TPE_Unit;
  28. /** How many fractions a unit is split into. This is NOT SUPPOSED TO BE
  29. REDEFINED, so rather don't do it (otherwise things may overflow etc.). */
  30. #define TPE_FRACTIONS_PER_UNIT 512
  31. #define TPE_INFINITY 2147483647
  32. #define TPE_SHAPE_POINT 0 ///< single point in space
  33. #define TPE_SHAPE_SPHERE 1 ///< sphere, params.: radius
  34. #define TPE_SHAPE_CUBOID 2 ///< cuboid, params.: width, height, depth
  35. #define TPE_SHAPE_PLANE 3 ///< plane, params.: width, depth
  36. #define TPE_SHAPE_CYLINDER 4 ///< cylinder, params.: radius, height
  37. #define TPE_SHAPE_TRIMESH 5 /**< triangle mesh, params.:
  38. vertex count,
  39. triangle count
  40. vertices (int32_t pointer),
  41. indices (uint16_t pointer) */
  42. #define TPE_MAX_SHAPE_PARAMS 3
  43. #define TPE_MAX_SHAPE_PARAMPOINTERS 2
  44. #define TPE_BODY_FLAG_DISABLED 0x00 ///< won't take part in simul. at all
  45. #define TPE_BODY_FLAG_NONCOLLIDING 0x01 ///< simulated but won't collide
  46. TPE_Unit TPE_wrap(TPE_Unit value, TPE_Unit mod);
  47. TPE_Unit TPE_clamp(TPE_Unit v, TPE_Unit v1, TPE_Unit v2);
  48. static inline TPE_Unit TPE_nonZero(TPE_Unit x);
  49. /** Returns an integer square root of given value. */
  50. TPE_Unit TPE_sqrt(TPE_Unit value);
  51. /** Returns a sine of given arguments, both in TPE_Units (see the library
  52. conventions). */
  53. TPE_Unit TPE_sin(TPE_Unit x);
  54. TPE_Unit TPE_cos(TPE_Unit x);
  55. TPE_Unit TPE_asin(TPE_Unit x);
  56. TPE_Unit TPE_acos(TPE_Unit x);
  57. typedef struct
  58. {
  59. TPE_Unit x;
  60. TPE_Unit y;
  61. TPE_Unit z;
  62. TPE_Unit w;
  63. } TPE_Vec4;
  64. /** Initializes vec4 to a zero vector. */
  65. void TPE_initVec4(TPE_Vec4 *v);
  66. void TPE_vec4Set(TPE_Vec4 *v, TPE_Unit x, TPE_Unit y, TPE_Unit z, TPE_Unit w);
  67. void TPE_vec3Add(TPE_Vec4 a, TPE_Vec4 b, TPE_Vec4 *result);
  68. void TPE_vec4Add(TPE_Vec4 a, TPE_Vec4 b, TPE_Vec4 *result);
  69. void TPE_vec3Substract(TPE_Vec4 a, TPE_Vec4 b, TPE_Vec4 *result);
  70. void TPE_vec4Substract(TPE_Vec4 a, TPE_Vec4 b, TPE_Vec4 *result);
  71. void TPE_vec3Multiply(TPE_Vec4 v, TPE_Unit f, TPE_Vec4 *result);
  72. void TPE_vec4Multiply(TPE_Vec4 v, TPE_Unit f, TPE_Vec4 *result);
  73. TPE_Unit TPE_vec3Len(TPE_Vec4 v);
  74. TPE_Unit TPE_vec4Len(TPE_Vec4 v);
  75. TPE_Unit TPE_vec3DotProduct(TPE_Vec4 v1, TPE_Vec4 v2);
  76. void TPE_vec3CrossProduct(TPE_Vec4 a, TPE_Vec4 b, TPE_Vec4 *result);
  77. void TPE_vec3Normalize(TPE_Vec4 *v);
  78. void TPE_vec4Normalize(TPE_Vec4 *v);
  79. void TPE_vec3Project(TPE_Vec4 v, TPE_Vec4 base, TPE_Vec4 *result);
  80. /** Holds a rotation state around a single axis, in a way that prevents rounding
  81. errors from distorting the rotation over time. In theory rotation of a body
  82. could be represented as
  83. [current orientation, axis of rotation,angular velocity]
  84. However applying the rotation and normalizing the orientation quaternion each
  85. simulation step leads to error cumulation and the rotation gets aligned with
  86. one principal axis after some time. Because of this we rather represent the
  87. rotation state as
  88. [original orientation, axis of rotation, angular velocity, current angle]
  89. From this we can at each simulation step compute the current orientation by
  90. applying rotation by current angle to the original rotation without error
  91. cumulation. */
  92. typedef struct
  93. {
  94. TPE_Vec4 originalOrientation; /**< quaternion holding the original
  95. orientation of the body at the time when it
  96. has taken on this rotational state */
  97. TPE_Vec4 axisVelocity; /**< axis of rotation (x,y,z) and a
  98. non-negative angular velocity around this
  99. axis (w), determined ny the right hand
  100. rule */
  101. TPE_Unit currentAngle; /**< angle the body has already rotated along
  102. the rotation axis (from the original
  103. orientation) */
  104. } TPE_RotationState;
  105. typedef struct
  106. {
  107. uint8_t shape;
  108. TPE_Unit shapeParams[TPE_MAX_SHAPE_PARAMS]; ///< parameters of the body type
  109. void *shapeParamPointers[TPE_MAX_SHAPE_PARAMPOINTERS]; ///< pointer parameters
  110. uint8_t flags;
  111. TPE_Unit mass; /**< body mass, setting this to TPE_INFINITY will
  112. make the object static (not moving at all)
  113. which may help performance */
  114. TPE_Vec4 position; ///< position of the body's center of mass
  115. TPE_Vec4 velocity; ///< linear velocity vector
  116. TPE_RotationState rotation; /**< holds the state related to rotation, i.e.
  117. the rotation axis, angular momentum and data
  118. from which current orientation can be
  119. inferred */
  120. } TPE_Body;
  121. /** Initializes a physical body, this should be called on all TPE_Bodys that
  122. are created.*/
  123. void TPE_bodyInit(TPE_Body *body);
  124. /** Computes a 4x4 transform matrix of given body. The matrix has the same
  125. format as S3L_Mat4 from small3dlib. */
  126. void TPE_bodyGetTransformMatrix(const TPE_Body *body, TPE_Unit matrix[4][4]);
  127. void TPE_bodyGetOrientation(const TPE_Body *body, TPE_Vec4 *quaternion);
  128. void TPE_bodyStep(TPE_Body *body);
  129. void TPE_bodySetRotation(TPE_Body *body, TPE_Vec4 axis, TPE_Unit velocity);
  130. /** Applies a velocity change to a body at a specific point (relative to the
  131. body center), which will change its linear and/or angular velocity. This is
  132. similar to an impulse but doesn't take mass into account, only velocity. */
  133. void TPE_bodyApplyVelocity(TPE_Body *body, TPE_Vec4 point, TPE_Vec4 velocity);
  134. #define TPE_PRINTF_VEC4(v) printf("[%d %d %d %d]\n",(v).x,(v).y,(v).z,(v).w);
  135. typedef struct
  136. {
  137. uint16_t bodyCount;
  138. TPE_Body *bodies;
  139. } TPE_PhysicsWorld;
  140. /** Multiplies two quaternions which can be seen as chaining two rotations
  141. represented by them. This is not commutative (a*b != b*a)! Rotations a is
  142. performed firth, then rotation b is performed. */
  143. void TPE_quaternionMultiply(TPE_Vec4 a, TPE_Vec4 b, TPE_Vec4 *result);
  144. /** Initializes quaternion to the a rotation identity (i.e. NOT zero
  145. quaternion). */
  146. void TPE_quaternionInit(TPE_Vec4 *quaternion);
  147. /** Converts a rotation given as an axis and angle around this axis (by right
  148. hand rule) to a rotation quaternion. */
  149. void TPE_rotationToQuaternion(TPE_Vec4 axis, TPE_Unit angle,
  150. TPE_Vec4 *quaternion);
  151. void TPE_quaternionToRotation(TPE_Vec4 quaternion, TPE_Vec4 *axis,
  152. TPE_Unit *angle);
  153. /** Converts a rotation quaternion to a 4x4 rotation matrix. The matrix is
  154. indexed as [column][row] and is in the same format as S3L_Mat4 from
  155. small3dlib. */
  156. void TPE_quaternionToRotationMatrix(TPE_Vec4 quaternion, TPE_Unit matrix[4][4]);
  157. void TPE_getVelocitiesAfterCollision(
  158. TPE_Unit *v1,
  159. TPE_Unit *v2,
  160. TPE_Unit m1,
  161. TPE_Unit m2,
  162. TPE_Unit elasticity
  163. );
  164. //------------------------------------------------------------------------------
  165. void TPE_initVec4(TPE_Vec4 *v)
  166. {
  167. v->x = 0;
  168. v->y = 0;
  169. v->z = 0;
  170. v->w = 0;
  171. }
  172. void TPE_vec4Set(TPE_Vec4 *v, TPE_Unit x, TPE_Unit y, TPE_Unit z, TPE_Unit w)
  173. {
  174. v->x = x;
  175. v->y = y;
  176. v->z = z;
  177. v->w = w;
  178. }
  179. TPE_Unit TPE_wrap(TPE_Unit value, TPE_Unit mod)
  180. {
  181. return value >= 0 ? (value % mod) : (mod + (value % mod) - 1);
  182. }
  183. TPE_Unit TPE_clamp(TPE_Unit v, TPE_Unit v1, TPE_Unit v2)
  184. {
  185. return v >= v1 ? (v <= v2 ? v : v2) : v1;
  186. }
  187. TPE_Unit TPE_nonZero(TPE_Unit x)
  188. {
  189. return x + (x == 0);
  190. }
  191. #define TPE_SIN_TABLE_LENGTH 128
  192. static const TPE_Unit TPE_sinTable[TPE_SIN_TABLE_LENGTH] =
  193. {
  194. /* 511 was chosen here as a highest number that doesn't overflow during
  195. compilation for TPE_FRACTIONS_PER_UNIT == 1024 */
  196. (0*TPE_FRACTIONS_PER_UNIT)/511, (6*TPE_FRACTIONS_PER_UNIT)/511,
  197. (12*TPE_FRACTIONS_PER_UNIT)/511, (18*TPE_FRACTIONS_PER_UNIT)/511,
  198. (25*TPE_FRACTIONS_PER_UNIT)/511, (31*TPE_FRACTIONS_PER_UNIT)/511,
  199. (37*TPE_FRACTIONS_PER_UNIT)/511, (43*TPE_FRACTIONS_PER_UNIT)/511,
  200. (50*TPE_FRACTIONS_PER_UNIT)/511, (56*TPE_FRACTIONS_PER_UNIT)/511,
  201. (62*TPE_FRACTIONS_PER_UNIT)/511, (68*TPE_FRACTIONS_PER_UNIT)/511,
  202. (74*TPE_FRACTIONS_PER_UNIT)/511, (81*TPE_FRACTIONS_PER_UNIT)/511,
  203. (87*TPE_FRACTIONS_PER_UNIT)/511, (93*TPE_FRACTIONS_PER_UNIT)/511,
  204. (99*TPE_FRACTIONS_PER_UNIT)/511, (105*TPE_FRACTIONS_PER_UNIT)/511,
  205. (111*TPE_FRACTIONS_PER_UNIT)/511, (118*TPE_FRACTIONS_PER_UNIT)/511,
  206. (124*TPE_FRACTIONS_PER_UNIT)/511, (130*TPE_FRACTIONS_PER_UNIT)/511,
  207. (136*TPE_FRACTIONS_PER_UNIT)/511, (142*TPE_FRACTIONS_PER_UNIT)/511,
  208. (148*TPE_FRACTIONS_PER_UNIT)/511, (154*TPE_FRACTIONS_PER_UNIT)/511,
  209. (160*TPE_FRACTIONS_PER_UNIT)/511, (166*TPE_FRACTIONS_PER_UNIT)/511,
  210. (172*TPE_FRACTIONS_PER_UNIT)/511, (178*TPE_FRACTIONS_PER_UNIT)/511,
  211. (183*TPE_FRACTIONS_PER_UNIT)/511, (189*TPE_FRACTIONS_PER_UNIT)/511,
  212. (195*TPE_FRACTIONS_PER_UNIT)/511, (201*TPE_FRACTIONS_PER_UNIT)/511,
  213. (207*TPE_FRACTIONS_PER_UNIT)/511, (212*TPE_FRACTIONS_PER_UNIT)/511,
  214. (218*TPE_FRACTIONS_PER_UNIT)/511, (224*TPE_FRACTIONS_PER_UNIT)/511,
  215. (229*TPE_FRACTIONS_PER_UNIT)/511, (235*TPE_FRACTIONS_PER_UNIT)/511,
  216. (240*TPE_FRACTIONS_PER_UNIT)/511, (246*TPE_FRACTIONS_PER_UNIT)/511,
  217. (251*TPE_FRACTIONS_PER_UNIT)/511, (257*TPE_FRACTIONS_PER_UNIT)/511,
  218. (262*TPE_FRACTIONS_PER_UNIT)/511, (268*TPE_FRACTIONS_PER_UNIT)/511,
  219. (273*TPE_FRACTIONS_PER_UNIT)/511, (278*TPE_FRACTIONS_PER_UNIT)/511,
  220. (283*TPE_FRACTIONS_PER_UNIT)/511, (289*TPE_FRACTIONS_PER_UNIT)/511,
  221. (294*TPE_FRACTIONS_PER_UNIT)/511, (299*TPE_FRACTIONS_PER_UNIT)/511,
  222. (304*TPE_FRACTIONS_PER_UNIT)/511, (309*TPE_FRACTIONS_PER_UNIT)/511,
  223. (314*TPE_FRACTIONS_PER_UNIT)/511, (319*TPE_FRACTIONS_PER_UNIT)/511,
  224. (324*TPE_FRACTIONS_PER_UNIT)/511, (328*TPE_FRACTIONS_PER_UNIT)/511,
  225. (333*TPE_FRACTIONS_PER_UNIT)/511, (338*TPE_FRACTIONS_PER_UNIT)/511,
  226. (343*TPE_FRACTIONS_PER_UNIT)/511, (347*TPE_FRACTIONS_PER_UNIT)/511,
  227. (352*TPE_FRACTIONS_PER_UNIT)/511, (356*TPE_FRACTIONS_PER_UNIT)/511,
  228. (361*TPE_FRACTIONS_PER_UNIT)/511, (365*TPE_FRACTIONS_PER_UNIT)/511,
  229. (370*TPE_FRACTIONS_PER_UNIT)/511, (374*TPE_FRACTIONS_PER_UNIT)/511,
  230. (378*TPE_FRACTIONS_PER_UNIT)/511, (382*TPE_FRACTIONS_PER_UNIT)/511,
  231. (386*TPE_FRACTIONS_PER_UNIT)/511, (391*TPE_FRACTIONS_PER_UNIT)/511,
  232. (395*TPE_FRACTIONS_PER_UNIT)/511, (398*TPE_FRACTIONS_PER_UNIT)/511,
  233. (402*TPE_FRACTIONS_PER_UNIT)/511, (406*TPE_FRACTIONS_PER_UNIT)/511,
  234. (410*TPE_FRACTIONS_PER_UNIT)/511, (414*TPE_FRACTIONS_PER_UNIT)/511,
  235. (417*TPE_FRACTIONS_PER_UNIT)/511, (421*TPE_FRACTIONS_PER_UNIT)/511,
  236. (424*TPE_FRACTIONS_PER_UNIT)/511, (428*TPE_FRACTIONS_PER_UNIT)/511,
  237. (431*TPE_FRACTIONS_PER_UNIT)/511, (435*TPE_FRACTIONS_PER_UNIT)/511,
  238. (438*TPE_FRACTIONS_PER_UNIT)/511, (441*TPE_FRACTIONS_PER_UNIT)/511,
  239. (444*TPE_FRACTIONS_PER_UNIT)/511, (447*TPE_FRACTIONS_PER_UNIT)/511,
  240. (450*TPE_FRACTIONS_PER_UNIT)/511, (453*TPE_FRACTIONS_PER_UNIT)/511,
  241. (456*TPE_FRACTIONS_PER_UNIT)/511, (459*TPE_FRACTIONS_PER_UNIT)/511,
  242. (461*TPE_FRACTIONS_PER_UNIT)/511, (464*TPE_FRACTIONS_PER_UNIT)/511,
  243. (467*TPE_FRACTIONS_PER_UNIT)/511, (469*TPE_FRACTIONS_PER_UNIT)/511,
  244. (472*TPE_FRACTIONS_PER_UNIT)/511, (474*TPE_FRACTIONS_PER_UNIT)/511,
  245. (476*TPE_FRACTIONS_PER_UNIT)/511, (478*TPE_FRACTIONS_PER_UNIT)/511,
  246. (481*TPE_FRACTIONS_PER_UNIT)/511, (483*TPE_FRACTIONS_PER_UNIT)/511,
  247. (485*TPE_FRACTIONS_PER_UNIT)/511, (487*TPE_FRACTIONS_PER_UNIT)/511,
  248. (488*TPE_FRACTIONS_PER_UNIT)/511, (490*TPE_FRACTIONS_PER_UNIT)/511,
  249. (492*TPE_FRACTIONS_PER_UNIT)/511, (494*TPE_FRACTIONS_PER_UNIT)/511,
  250. (495*TPE_FRACTIONS_PER_UNIT)/511, (497*TPE_FRACTIONS_PER_UNIT)/511,
  251. (498*TPE_FRACTIONS_PER_UNIT)/511, (499*TPE_FRACTIONS_PER_UNIT)/511,
  252. (501*TPE_FRACTIONS_PER_UNIT)/511, (502*TPE_FRACTIONS_PER_UNIT)/511,
  253. (503*TPE_FRACTIONS_PER_UNIT)/511, (504*TPE_FRACTIONS_PER_UNIT)/511,
  254. (505*TPE_FRACTIONS_PER_UNIT)/511, (506*TPE_FRACTIONS_PER_UNIT)/511,
  255. (507*TPE_FRACTIONS_PER_UNIT)/511, (507*TPE_FRACTIONS_PER_UNIT)/511,
  256. (508*TPE_FRACTIONS_PER_UNIT)/511, (509*TPE_FRACTIONS_PER_UNIT)/511,
  257. (509*TPE_FRACTIONS_PER_UNIT)/511, (510*TPE_FRACTIONS_PER_UNIT)/511,
  258. (510*TPE_FRACTIONS_PER_UNIT)/511, (510*TPE_FRACTIONS_PER_UNIT)/511,
  259. (510*TPE_FRACTIONS_PER_UNIT)/511, (510*TPE_FRACTIONS_PER_UNIT)/511
  260. };
  261. #define TPE_SIN_TABLE_UNIT_STEP\
  262. (TPE_FRACTIONS_PER_UNIT / (TPE_SIN_TABLE_LENGTH * 4))
  263. TPE_Unit TPE_sqrt(TPE_Unit value)
  264. {
  265. int8_t sign = 1;
  266. if (value < 0)
  267. {
  268. sign = -1;
  269. value *= -1;
  270. }
  271. uint32_t result = 0;
  272. uint32_t a = value;
  273. uint32_t b = 1u << 30;
  274. while (b > a)
  275. b >>= 2;
  276. while (b != 0)
  277. {
  278. if (a >= result + b)
  279. {
  280. a -= result + b;
  281. result = result + 2 * b;
  282. }
  283. b >>= 2;
  284. result >>= 1;
  285. }
  286. return result * sign;
  287. }
  288. TPE_Unit TPE_sin(TPE_Unit x)
  289. {
  290. x = TPE_wrap(x / TPE_SIN_TABLE_UNIT_STEP,TPE_SIN_TABLE_LENGTH * 4);
  291. int8_t positive = 1;
  292. if (x < TPE_SIN_TABLE_LENGTH)
  293. {
  294. }
  295. else if (x < TPE_SIN_TABLE_LENGTH * 2)
  296. {
  297. x = TPE_SIN_TABLE_LENGTH * 2 - x - 1;
  298. }
  299. else if (x < TPE_SIN_TABLE_LENGTH * 3)
  300. {
  301. x = x - TPE_SIN_TABLE_LENGTH * 2;
  302. positive = 0;
  303. }
  304. else
  305. {
  306. x = TPE_SIN_TABLE_LENGTH - (x - TPE_SIN_TABLE_LENGTH * 3) - 1;
  307. positive = 0;
  308. }
  309. return positive ? TPE_sinTable[x] : -1 * TPE_sinTable[x];
  310. }
  311. TPE_Unit TPE_cos(TPE_Unit x)
  312. {
  313. return TPE_sin(x + TPE_FRACTIONS_PER_UNIT / 4);
  314. }
  315. void TPE_bodyInit(TPE_Body *body)
  316. {
  317. // TODO
  318. TPE_initVec4(&(body->position));
  319. TPE_initVec4(&(body->velocity));
  320. // init orientation to identity unit quaternion (1,0,0,0):
  321. TPE_quaternionInit(&(body->rotation.originalOrientation));
  322. TPE_vec4Set(&(body->rotation.axisVelocity),TPE_FRACTIONS_PER_UNIT,0,0,0);
  323. body->rotation.currentAngle = 0;
  324. }
  325. void TPE_bodyGetOrientation(const TPE_Body *body, TPE_Vec4 *quaternion)
  326. {
  327. TPE_Vec4 axisRotation;
  328. TPE_rotationToQuaternion(
  329. body->rotation.axisVelocity,
  330. body->rotation.currentAngle,
  331. &axisRotation);
  332. TPE_quaternionMultiply(
  333. body->rotation.originalOrientation,
  334. axisRotation,
  335. quaternion);
  336. TPE_vec4Normalize(quaternion);
  337. }
  338. void TPE_vec3CrossProduct(TPE_Vec4 a, TPE_Vec4 b, TPE_Vec4 *result)
  339. {
  340. TPE_Vec4 r;
  341. r.x = a.y * b.z - a.z * b.y;
  342. r.y = a.z * b.x - a.x * b.z;
  343. r.z = a.x * b.y - a.y * b.x;
  344. *result = r;
  345. }
  346. void TPE_bodyApplyVelocity(TPE_Body *body, TPE_Vec4 point, TPE_Vec4 velocity)
  347. {
  348. TPE_Vec4 linearVelocity, angularVelocity, rotationAxis;
  349. TPE_vec3Normalize(&point);
  350. TPE_vec3Project(velocity,point,&linearVelocity);
  351. TPE_vec3Substract(velocity,linearVelocity,&angularVelocity);
  352. TPE_vec3Add(body->velocity,linearVelocity,&(body->velocity));
  353. TPE_vec3CrossProduct(point,angularVelocity,&rotationAxis);
  354. // TODO: rotation
  355. }
  356. void TPE_bodyStep(TPE_Body *body)
  357. {
  358. TPE_vec3Add(body->position,body->velocity,&(body->position));
  359. body->rotation.currentAngle += body->rotation.axisVelocity.w;
  360. }
  361. void TPE_bodySetRotation(TPE_Body *body, TPE_Vec4 axis, TPE_Unit velocity)
  362. {
  363. TPE_bodyGetOrientation(body,&(body->rotation.originalOrientation));
  364. if (velocity < 0)
  365. {
  366. axis.x *= -1;
  367. axis.y *= -1;
  368. axis.z *= -1;
  369. velocity *= -1;
  370. }
  371. body->rotation.axisVelocity = axis;
  372. body->rotation.axisVelocity.w = velocity;
  373. body->rotation.currentAngle = 0;
  374. }
  375. void TPE_quaternionMultiply(TPE_Vec4 a, TPE_Vec4 b, TPE_Vec4 *result)
  376. {
  377. TPE_Vec4 r; // in case result is identical to a or b
  378. r.x =
  379. (a.w * b.x +
  380. a.x * b.w +
  381. a.y * b.z -
  382. a.z * b.y) / TPE_FRACTIONS_PER_UNIT;
  383. r.y =
  384. (a.w * b.y -
  385. a.x * b.z +
  386. a.y * b.w +
  387. a.z * b.x) / TPE_FRACTIONS_PER_UNIT;
  388. r.z =
  389. (a.w * b.z +
  390. a.x * b.y -
  391. a.y * b.x +
  392. a.z * b.w) / TPE_FRACTIONS_PER_UNIT;
  393. r.w =
  394. (a.w * b.w -
  395. a.x * b.x -
  396. a.y * b.y -
  397. a.z * b.z) / TPE_FRACTIONS_PER_UNIT;
  398. result->x = r.x;
  399. result->y = r.y;
  400. result->z = r.z;
  401. result->w = r.w;
  402. }
  403. void TPE_rotationToQuaternion(TPE_Vec4 axis, TPE_Unit angle, TPE_Vec4 *quaternion)
  404. {
  405. TPE_vec3Normalize(&axis);
  406. angle /= 2;
  407. TPE_Unit s = TPE_sin(angle);
  408. quaternion->x = (s * axis.x) / TPE_FRACTIONS_PER_UNIT;
  409. quaternion->y = (s * axis.y) / TPE_FRACTIONS_PER_UNIT;
  410. quaternion->z = (s * axis.z) / TPE_FRACTIONS_PER_UNIT;
  411. quaternion->w = TPE_cos(angle);
  412. }
  413. TPE_Unit TPE_asin(TPE_Unit x)
  414. {
  415. x = TPE_clamp(x,-TPE_FRACTIONS_PER_UNIT,TPE_FRACTIONS_PER_UNIT);
  416. int8_t sign = 1;
  417. if (x < 0)
  418. {
  419. sign = -1;
  420. x *= -1;
  421. }
  422. int16_t low = 0;
  423. int16_t high = TPE_SIN_TABLE_LENGTH -1;
  424. int16_t middle;
  425. while (low <= high) // binary search
  426. {
  427. middle = (low + high) / 2;
  428. TPE_Unit v = TPE_sinTable[middle];
  429. if (v > x)
  430. high = middle - 1;
  431. else if (v < x)
  432. low = middle + 1;
  433. else
  434. break;
  435. }
  436. middle *= TPE_SIN_TABLE_UNIT_STEP;
  437. return sign * middle;
  438. }
  439. TPE_Unit TPE_acos(TPE_Unit x)
  440. {
  441. return TPE_asin(-1 * x) + TPE_FRACTIONS_PER_UNIT / 4;
  442. }
  443. void TPE_quaternionToRotation(TPE_Vec4 quaternion, TPE_Vec4 *axis, TPE_Unit *angle)
  444. {
  445. *angle = 2 * TPE_acos(quaternion.x);
  446. TPE_Unit tmp =
  447. TPE_nonZero(TPE_sqrt(
  448. (TPE_FRACTIONS_PER_UNIT -
  449. (quaternion.x * quaternion.x) / TPE_FRACTIONS_PER_UNIT
  450. ) * TPE_FRACTIONS_PER_UNIT));
  451. axis->x = (quaternion.x * TPE_FRACTIONS_PER_UNIT) / tmp;
  452. axis->y = (quaternion.y * TPE_FRACTIONS_PER_UNIT) / tmp;
  453. axis->z = (quaternion.z * TPE_FRACTIONS_PER_UNIT) / tmp;
  454. }
  455. void TPE_quaternionToRotationMatrix(TPE_Vec4 quaternion, TPE_Unit matrix[4][4])
  456. {
  457. TPE_Unit
  458. _2x2 = (2 * quaternion.x * quaternion.x) / TPE_FRACTIONS_PER_UNIT,
  459. _2y2 = (2 * quaternion.y * quaternion.y) / TPE_FRACTIONS_PER_UNIT,
  460. _2z2 = (2 * quaternion.z * quaternion.z) / TPE_FRACTIONS_PER_UNIT,
  461. _2xy = (2 * quaternion.x * quaternion.y) / TPE_FRACTIONS_PER_UNIT,
  462. _2xw = (2 * quaternion.x * quaternion.w) / TPE_FRACTIONS_PER_UNIT,
  463. _2zw = (2 * quaternion.z * quaternion.w) / TPE_FRACTIONS_PER_UNIT,
  464. _2xz = (2 * quaternion.x * quaternion.z) / TPE_FRACTIONS_PER_UNIT,
  465. _2yw = (2 * quaternion.y * quaternion.w) / TPE_FRACTIONS_PER_UNIT,
  466. _2yz = (2 * quaternion.y * quaternion.z) / TPE_FRACTIONS_PER_UNIT;
  467. #define ONE TPE_FRACTIONS_PER_UNIT
  468. matrix[0][0] = ONE - _2y2 - _2z2;
  469. matrix[1][0] = _2xy - _2zw;
  470. matrix[2][0] = _2xz + _2yw;
  471. matrix[3][0] = 0;
  472. matrix[0][1] = _2xy + _2zw;
  473. matrix[1][1] = ONE - _2x2 - _2z2;
  474. matrix[2][1] = _2yz - _2xw;
  475. matrix[3][1] = 0;
  476. matrix[0][2] = _2xz - _2yw;
  477. matrix[1][2] = _2yz + _2xw;
  478. matrix[2][2] = ONE - _2x2 - _2y2;
  479. matrix[3][2] = 0;
  480. matrix[0][3] = 0;
  481. matrix[1][3] = 0;
  482. matrix[2][3] = 0;
  483. matrix[3][3] = ONE;
  484. #undef ONE
  485. }
  486. void TPE_vec3Add(const TPE_Vec4 a, const TPE_Vec4 b, TPE_Vec4 *result)
  487. {
  488. result->x = a.x + b.x;
  489. result->y = a.y + b.y;
  490. result->z = a.z + b.z;
  491. }
  492. void TPE_vec4Add(const TPE_Vec4 a, const TPE_Vec4 b, TPE_Vec4 *result)
  493. {
  494. result->x = a.x + b.x;
  495. result->y = a.y + b.y;
  496. result->z = a.z + b.z;
  497. result->w = a.w + b.w;
  498. }
  499. void TPE_vec3Substract(const TPE_Vec4 a, const TPE_Vec4 b, TPE_Vec4 *result)
  500. {
  501. result->x = a.x - b.x;
  502. result->y = a.y - b.y;
  503. result->z = a.z - b.z;
  504. }
  505. void TPE_vec4Substract(const TPE_Vec4 a, const TPE_Vec4 b, TPE_Vec4 *result)
  506. {
  507. result->x = a.x - b.x;
  508. result->y = a.y - b.y;
  509. result->z = a.z - b.z;
  510. result->w = a.w - b.w;
  511. }
  512. void TPE_vec3Multiply(const TPE_Vec4 v, TPE_Unit f, TPE_Vec4 *result)
  513. {
  514. result->x = (v.x * f) / TPE_FRACTIONS_PER_UNIT;
  515. result->y = (v.y * f) / TPE_FRACTIONS_PER_UNIT;
  516. result->z = (v.z * f) / TPE_FRACTIONS_PER_UNIT;
  517. }
  518. void TPE_vec4Multiply(const TPE_Vec4 v, TPE_Unit f, TPE_Vec4 *result)
  519. {
  520. result->x = (v.x * f) / TPE_FRACTIONS_PER_UNIT;
  521. result->y = (v.y * f) / TPE_FRACTIONS_PER_UNIT;
  522. result->z = (v.z * f) / TPE_FRACTIONS_PER_UNIT;
  523. result->w = (v.w * f) / TPE_FRACTIONS_PER_UNIT;
  524. }
  525. TPE_Unit TPE_vec3Len(TPE_Vec4 v)
  526. {
  527. return TPE_sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
  528. }
  529. TPE_Unit TPE_vec4Len(TPE_Vec4 v)
  530. {
  531. return TPE_sqrt(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w);
  532. }
  533. TPE_Unit TPE_vec3DotProduct(const TPE_Vec4 v1, const TPE_Vec4 v2)
  534. {
  535. return
  536. (v1.x * v2.x + v1.y * v2.y + v1.z * v2.z) / TPE_FRACTIONS_PER_UNIT;
  537. }
  538. void TPE_vec3Normalize(TPE_Vec4 *v)
  539. {
  540. TPE_Unit l = TPE_vec3Len(*v);
  541. if (l == 0)
  542. {
  543. v->x = TPE_FRACTIONS_PER_UNIT;
  544. return;
  545. }
  546. v->x = (v->x * TPE_FRACTIONS_PER_UNIT) / l;
  547. v->y = (v->y * TPE_FRACTIONS_PER_UNIT) / l;
  548. v->z = (v->z * TPE_FRACTIONS_PER_UNIT) / l;
  549. }
  550. void TPE_vec4Normalize(TPE_Vec4 *v)
  551. {
  552. TPE_Unit l = TPE_vec4Len(*v);
  553. if (l == 0)
  554. {
  555. v->x = TPE_FRACTIONS_PER_UNIT;
  556. return;
  557. }
  558. v->x = (v->x * TPE_FRACTIONS_PER_UNIT) / l;
  559. v->y = (v->y * TPE_FRACTIONS_PER_UNIT) / l;
  560. v->z = (v->z * TPE_FRACTIONS_PER_UNIT) / l;
  561. v->w = (v->w * TPE_FRACTIONS_PER_UNIT) / l;
  562. }
  563. void TPE_vec3Project(TPE_Vec4 v, TPE_Vec4 base, TPE_Vec4 *result)
  564. {
  565. TPE_Unit p = TPE_vec3DotProduct(v,base);
  566. result->x = (p * base.x) / TPE_FRACTIONS_PER_UNIT;
  567. result->y = (p * base.y) / TPE_FRACTIONS_PER_UNIT;
  568. result->z = (p * base.z) / TPE_FRACTIONS_PER_UNIT;
  569. }
  570. void TPE_getVelocitiesAfterCollision(
  571. TPE_Unit *v1,
  572. TPE_Unit *v2,
  573. TPE_Unit m1,
  574. TPE_Unit m2,
  575. TPE_Unit elasticity
  576. )
  577. {
  578. /* in the following a lot of TPE_FRACTIONS_PER_UNIT cancel out, feel free to
  579. check if confused */
  580. #define ANTI_OVERFLOW 30000
  581. #define ANTI_OVERFLOW_SCALE 128
  582. uint8_t overflowDanger = m1 > ANTI_OVERFLOW || *v1 > ANTI_OVERFLOW ||
  583. m2 > ANTI_OVERFLOW || *v2 > ANTI_OVERFLOW;
  584. if (overflowDanger)
  585. {
  586. m1 = (m1 != 0) ? TPE_nonZero(m1 / ANTI_OVERFLOW_SCALE) : 0;
  587. m2 = (m2 != 0) ? TPE_nonZero(m2 / ANTI_OVERFLOW_SCALE) : 0;
  588. *v1 = (*v1 != 0) ? TPE_nonZero(*v1 / ANTI_OVERFLOW_SCALE) : 0;
  589. *v2 = (*v2 != 0) ? TPE_nonZero(*v2 / ANTI_OVERFLOW_SCALE) : 0;
  590. }
  591. TPE_Unit m1Pm2 = m1 + m2;
  592. TPE_Unit v2Mv1 = *v2 - *v1;
  593. TPE_Unit m1v1Pm2v2 = ((m1 * *v1) + (m2 * *v2));
  594. *v1 = (((elasticity * m2 / TPE_FRACTIONS_PER_UNIT) * v2Mv1)
  595. + m1v1Pm2v2) / m1Pm2;
  596. *v2 = (((elasticity * m1 / TPE_FRACTIONS_PER_UNIT) * -1 * v2Mv1)
  597. + m1v1Pm2v2) / m1Pm2;
  598. if (overflowDanger)
  599. {
  600. *v1 *= ANTI_OVERFLOW_SCALE;
  601. *v2 *= ANTI_OVERFLOW_SCALE;
  602. }
  603. #undef ANTI_OVERFLOW
  604. #undef ANTI_OVERFLOW_SCALE
  605. }
  606. void TPE_resolvePointCollision(
  607. TPE_Vec4 collisionPoint,
  608. TPE_Vec4 collisionNormal,
  609. TPE_Unit elasticity,
  610. TPE_Vec4 linVelocity1,
  611. TPE_Vec4 rotVelocity1,
  612. TPE_Unit m1,
  613. TPE_Vec4 linVelocity2,
  614. TPE_Vec4 rotVelocity2,
  615. TPE_Unit m2)
  616. {
  617. TPE_Vec4 v1, v2, v1New, v2New;
  618. TPE_initVec4(&v1);
  619. TPE_initVec4(&v2);
  620. TPE_initVec4(&v1New);
  621. TPE_initVec4(&v2New);
  622. // add lin. and rot. velocities to get the overall vel. of both points:
  623. TPE_vec4Add(linVelocity1,rotVelocity1,&v1);
  624. TPE_vec4Add(linVelocity2,rotVelocity2,&v2);
  625. /* project both of these velocities to the collision normal as we'll apply
  626. the collision equation only in the direction of this normal: */
  627. TPE_vec3Project(v1,collisionNormal,&v1New);
  628. TPE_vec3Project(v2,collisionNormal,&v2New);
  629. // get the velocities of the components
  630. TPE_Unit
  631. v1NewMag = TPE_vec3Len(v1New),
  632. v2NewMag = TPE_vec3Len(v2New);
  633. /* now also substract this component from the original velocity (so that it
  634. will now be in the collision plane), we'll later add back the updated
  635. velocity to it */
  636. TPE_vec4Substract(v1,v1New,&v1);
  637. TPE_vec4Substract(v2,v2New,&v2);
  638. // apply the 1D collision equation to velocities along the normal:
  639. TPE_getVelocitiesAfterCollision(
  640. &v1NewMag,
  641. &v2NewMag,
  642. m1,
  643. m2,
  644. elasticity);
  645. // add back the updated velocities to get the new overall velocities:
  646. v1New.x += (collisionNormal.x * v1NewMag) / TPE_FRACTIONS_PER_UNIT;
  647. v1New.y += (collisionNormal.y * v1NewMag) / TPE_FRACTIONS_PER_UNIT;
  648. v1New.z += (collisionNormal.z * v1NewMag) / TPE_FRACTIONS_PER_UNIT;
  649. v2New.x += (collisionNormal.x * v2NewMag) / TPE_FRACTIONS_PER_UNIT;
  650. v2New.y += (collisionNormal.y * v2NewMag) / TPE_FRACTIONS_PER_UNIT;
  651. v2New.z += (collisionNormal.z * v2NewMag) / TPE_FRACTIONS_PER_UNIT;
  652. // TODO
  653. }
  654. void TPE_bodyGetTransformMatrix(const TPE_Body *body, TPE_Unit matrix[4][4])
  655. {
  656. TPE_Vec4 orientation;
  657. TPE_bodyGetOrientation(body,&orientation);
  658. TPE_quaternionToRotationMatrix(orientation,matrix);
  659. matrix[0][3] = body->position.x;
  660. matrix[1][3] = body->position.y;
  661. matrix[2][3] = body->position.z;
  662. }
  663. void TPE_quaternionInit(TPE_Vec4 *quaternion)
  664. {
  665. quaternion->x = 0;
  666. quaternion->y = 0;
  667. quaternion->z = 0;
  668. quaternion->w = TPE_FRACTIONS_PER_UNIT;
  669. }
  670. #endif // guard