tinyphysicsengine.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  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. This is a suckless library for simple 3D (and 2D) physics simulation. The
  10. physics is based on the Newtonian model but is further simplified,
  11. particularly in the area of rotation: there is no moment of inertia for
  12. objects, i.e. every object rotates as if it was a ball, and the object can be
  13. rotating around at most one axis at a time, i.e. it is not possible to
  14. simulate e.g. the Dzhanibekov effect.
  15. CONVENTIONS:
  16. - Compatibility and simple usage with small3dlib is intended, so most
  17. convention and data types copy those of small3dlib (which takes a lot of
  18. conventions of OpenGL).
  19. - No floating point is used, we instead use integers (effectively a fixed
  20. point). TPE_FRACTIONS_PER_UNIT is an equivalent to 1.0 in floating point and
  21. all numbers are normalized by this constant.
  22. - Units: for any measure only an abstract mathematical unit is used. This unit
  23. always has TPE_FRACTIONS_PER_UNIT parts. You can assign any correcpondence
  24. with real life units to these units. E.g. 1 spatial unit (which you can see
  25. as e.g. 1 meter) is equal to TPE_FRACTIONS_PER_UNIT. Same with temporatl
  26. (e.g. 1 second) and mass (e.g. 1 kilogram) units, and also any derived
  27. units, e.g. a unit of velocity (e.g. 1 m/s) is also equal to 1
  28. TPE_FRACTIONS_PER_UNIT. A full angle is also split into
  29. TPE_FRACTIONS_PER_UNIT parts (instead of 2 * PI or degrees).
  30. - Quaternions are represented as vec4 where x ~ i, y ~ j, z ~ k, w ~ real.
  31. */
  32. #include <stdint.h>
  33. typedef int32_t TPE_Unit;
  34. /** How many fractions a unit is split into. This is NOT SUPPOSED TO BE
  35. REDEFINED, so rather don't do it (otherwise things may overflow etc.). */
  36. #define TPE_FRACTIONS_PER_UNIT 512
  37. #define TPE_INFINITY 2147483647
  38. #define TPE_PI 1608 ///< pi in TPE_Units
  39. #define TPE_SHAPE_POINT 0 ///< single point in space
  40. #define TPE_SHAPE_SPHERE 1 ///< sphere, params.: radius
  41. #define TPE_SHAPE_CUBOID 2 ///< cuboid, params.: width, height, depth
  42. #define TPE_SHAPE_PLANE 3 ///< plane, params.: width, depth
  43. #define TPE_SHAPE_CYLINDER 4 ///< cylinder, params.: radius, height
  44. #define TPE_SHAPE_TRIMESH 5 /**< triangle mesh, params.:
  45. vertex count,
  46. triangle count
  47. vertices (int32_t pointer),
  48. indices (uint16_t pointer) */
  49. #define TPE_MAX_SHAPE_PARAMS 3
  50. #define TPE_MAX_SHAPE_PARAMPOINTERS 2
  51. #define TPE_BODY_FLAG_DISABLED 0x00 ///< won't take part in simul. at all
  52. #define TPE_BODY_FLAG_NONCOLLIDING 0x01 ///< simulated but won't collide
  53. TPE_Unit TPE_wrap(TPE_Unit value, TPE_Unit mod);
  54. TPE_Unit TPE_clamp(TPE_Unit v, TPE_Unit v1, TPE_Unit v2);
  55. static inline TPE_Unit TPE_nonZero(TPE_Unit x);
  56. /** Returns an integer square root of given value. */
  57. TPE_Unit TPE_sqrt(TPE_Unit value);
  58. /** Returns a sine of given arguments, both in TPE_Units (see the library
  59. conventions). */
  60. TPE_Unit TPE_sin(TPE_Unit x);
  61. TPE_Unit TPE_cos(TPE_Unit x);
  62. TPE_Unit TPE_asin(TPE_Unit x);
  63. TPE_Unit TPE_acos(TPE_Unit x);
  64. typedef struct
  65. {
  66. TPE_Unit x;
  67. TPE_Unit y;
  68. TPE_Unit z;
  69. TPE_Unit w;
  70. } TPE_Vec4;
  71. #define TPE_PRINTF_VEC4(v) printf("[%d %d %d %d] ",(v).x,(v).y,(v).z,(v).w);
  72. /** Initializes vec4 to a zero vector. */
  73. void TPE_initVec4(TPE_Vec4 *v);
  74. TPE_Vec4 TPE_vec4(TPE_Unit x, TPE_Unit y, TPE_Unit z, TPE_Unit w);
  75. void TPE_vec4Set(TPE_Vec4 *v, TPE_Unit x, TPE_Unit y, TPE_Unit z, TPE_Unit w);
  76. void TPE_vec3Add(TPE_Vec4 a, TPE_Vec4 b, TPE_Vec4 *result);
  77. void TPE_vec4Add(TPE_Vec4 a, TPE_Vec4 b, TPE_Vec4 *result);
  78. void TPE_vec3Substract(TPE_Vec4 a, TPE_Vec4 b, TPE_Vec4 *result);
  79. void TPE_vec3Average(TPE_Vec4 a, TPE_Vec4 b, TPE_Vec4 *result);
  80. void TPE_vec4Substract(TPE_Vec4 a, TPE_Vec4 b, TPE_Vec4 *result);
  81. void TPE_vec3Multiply(TPE_Vec4 v, TPE_Unit f, TPE_Vec4 *result);
  82. void TPE_vec4Multiply(TPE_Vec4 v, TPE_Unit f, TPE_Vec4 *result);
  83. TPE_Unit TPE_vec3Len(TPE_Vec4 v);
  84. TPE_Unit TPE_vec4Len(TPE_Vec4 v);
  85. TPE_Unit TPE_vec3DotProduct(TPE_Vec4 v1, TPE_Vec4 v2);
  86. void TPE_vec3CrossProduct(TPE_Vec4 a, TPE_Vec4 b, TPE_Vec4 *result);
  87. void TPE_vec3Normalize(TPE_Vec4 *v);
  88. void TPE_vec4Normalize(TPE_Vec4 *v);
  89. void TPE_vec3Project(TPE_Vec4 v, TPE_Vec4 base, TPE_Vec4 *result);
  90. TPE_Vec4 TPE_vec3Plus(TPE_Vec4 a, TPE_Vec4 b);
  91. TPE_Vec4 TPE_vec3Minus(TPE_Vec4 a, TPE_Vec4 b);
  92. TPE_Vec4 TPE_vec3Times(TPE_Vec4 a, TPE_Unit f);
  93. TPE_Vec4 TPE_vec3Cross(TPE_Vec4 a, TPE_Vec4 b);
  94. /** Converts a linear velocity of an orbiting point to the angular velocity
  95. (angle units per time units). This depends on the distance of the point from
  96. the center of rotation. */
  97. TPE_Unit TPE_linearVelocityToAngular(TPE_Unit velocity, TPE_Unit distance);
  98. TPE_Unit TPE_angularVelocityToLinear(TPE_Unit velocity, TPE_Unit distance);
  99. /** Holds a rotation state around a single axis, in a way that prevents rounding
  100. errors from distorting the rotation over time. In theory rotation of a body
  101. could be represented as
  102. [current orientation, axis of rotation,angular velocity]
  103. However applying the rotation and normalizing the orientation quaternion each
  104. simulation step leads to error cumulation and the rotation gets aligned with
  105. one principal axis after some time. Because of this we rather represent the
  106. rotation state as
  107. [original orientation, axis of rotation, angular velocity, current angle]
  108. From this we can at each simulation step compute the current orientation by
  109. applying rotation by current angle to the original rotation without error
  110. cumulation. */
  111. typedef struct
  112. {
  113. TPE_Vec4 originalOrientation; /**< quaternion holding the original
  114. orientation of the body at the time when it
  115. has taken on this rotational state */
  116. TPE_Vec4 axisVelocity; /**< axis of rotation (x,y,z) and a
  117. non-negative angular velocity around this
  118. axis (w), determined ny the right hand
  119. rule */
  120. TPE_Unit currentAngle; /**< angle the body has already rotated along
  121. the rotation axis (from the original
  122. orientation) */
  123. } TPE_RotationState;
  124. typedef struct
  125. {
  126. uint8_t shape;
  127. TPE_Unit shapeParams[TPE_MAX_SHAPE_PARAMS]; ///< parameters of the body type
  128. void *shapeParamPointers[TPE_MAX_SHAPE_PARAMPOINTERS]; ///< pointer parameters
  129. uint8_t flags;
  130. TPE_Unit mass; /**< body mass, setting this to TPE_INFINITY will
  131. make the object static (not moving at all)
  132. which may help performance */
  133. TPE_Vec4 position; ///< position of the body's center of mass
  134. TPE_Vec4 velocity; ///< linear velocity vector
  135. TPE_RotationState rotation; /**< holds the state related to rotation, i.e.
  136. the rotation axis, angular momentum and data
  137. from which current orientation can be
  138. inferred */
  139. } TPE_Body;
  140. /** Initializes a physical body, this should be called on all TPE_Bodys that
  141. are created.*/
  142. void TPE_bodyInit(TPE_Body *body);
  143. /** Computes a 4x4 transform matrix of given body. The matrix has the same
  144. format as S3L_Mat4 from small3dlib. */
  145. void TPE_bodyGetTransformMatrix(const TPE_Body *body, TPE_Unit matrix[4][4]);
  146. void TPE_bodyGetOrientation(const TPE_Body *body, TPE_Vec4 *quaternion);
  147. void TPE_bodyStep(TPE_Body *body);
  148. void TPE_bodySetRotation(TPE_Body *body, TPE_Vec4 axis, TPE_Unit velocity);
  149. void TPE_bodyAddRotation(TPE_Body *body, TPE_Vec4 axis, TPE_Unit velocity);
  150. /** Applies a velocity change to a body at a specific point (relative to the
  151. body center), which will change its linear and/or angular velocity. This is
  152. similar to an impulse but doesn't take mass into account, only velocity. */
  153. void TPE_bodyApplyVelocity(TPE_Body *body, TPE_Vec4 point, TPE_Vec4 velocity);
  154. /** Collision detection: checks if two bodies are colliding. The return value is
  155. the collision depth along the collision normal (0 if the bodies are not
  156. colliding). World-space collision point is returned via a pointer. Collision
  157. normal is also returned via a pointer and its direction is "away from body1",
  158. i.e. if you move body1 in the opposite direction of this normal by the
  159. collision depth (return value), the bodies should no longer be colliding
  160. (in some cases another collision may still occur). */
  161. TPE_Unit TPE_bodyCollides(const TPE_Body *body1, const TPE_Body *body2,
  162. TPE_Vec4 *collisionPoint, TPE_Vec4 *collisionNormal);
  163. /** Gets a velocity of a single point on a rigid body, taking into account its
  164. linear velocity and rotation. The point coordinates are relative to the body
  165. center. The point does NOT have to be on the surface, it can be inside and
  166. even outside the body too. */
  167. TPE_Vec4 TPE_bodyGetPointVelocity(const TPE_Body *body, TPE_Vec4 point);
  168. void TPE_resolveCollision(TPE_Body *body1 ,TPE_Body *body2,
  169. TPE_Vec4 collisionPoint, TPE_Vec4 collisionNormal);
  170. /** Gets a uint16_t integer type of collision depending on two shapes, the order
  171. of shapes doesn't matter. */
  172. #define TPE_COLLISION_TYPE(shape1,shape2) \
  173. ((shape1) <= (shape2) ? \
  174. (((uint16_t) (shape1)) << 8) | (shape2) : \
  175. (((uint16_t) (shape2)) << 8) | (shape1))
  176. typedef struct
  177. {
  178. uint16_t bodyCount;
  179. TPE_Body *bodies;
  180. } TPE_PhysicsWorld;
  181. /** Multiplies two quaternions which can be seen as chaining two rotations
  182. represented by them. This is not commutative (a*b != b*a)! Rotations a is
  183. performed firth, then rotation b is performed. */
  184. void TPE_quaternionMultiply(TPE_Vec4 a, TPE_Vec4 b, TPE_Vec4 *result);
  185. /** Initializes quaternion to the a rotation identity (i.e. NOT zero
  186. quaternion). */
  187. void TPE_quaternionInit(TPE_Vec4 *quaternion);
  188. /** Converts a rotation given as an axis and angle around this axis (by right
  189. hand rule) to a rotation quaternion. */
  190. void TPE_rotationToQuaternion(TPE_Vec4 axis, TPE_Unit angle,
  191. TPE_Vec4 *quaternion);
  192. void TPE_quaternionToRotation(TPE_Vec4 quaternion, TPE_Vec4 *axis,
  193. TPE_Unit *angle);
  194. /** Converts a rotation quaternion to a 4x4 rotation matrix. The matrix is
  195. indexed as [column][row] and is in the same format as S3L_Mat4 from
  196. small3dlib. */
  197. void TPE_quaternionToRotationMatrix(TPE_Vec4 quaternion, TPE_Unit matrix[4][4]);
  198. void TPE_getVelocitiesAfterCollision(
  199. TPE_Unit *v1,
  200. TPE_Unit *v2,
  201. TPE_Unit m1,
  202. TPE_Unit m2,
  203. TPE_Unit elasticity
  204. );
  205. //------------------------------------------------------------------------------
  206. void TPE_initVec4(TPE_Vec4 *v)
  207. {
  208. v->x = 0;
  209. v->y = 0;
  210. v->z = 0;
  211. v->w = 0;
  212. }
  213. TPE_Vec4 TPE_vec4(TPE_Unit x, TPE_Unit y, TPE_Unit z, TPE_Unit w)
  214. {
  215. TPE_Vec4 r;
  216. r.x = x;
  217. r.y = y;
  218. r.z = z;
  219. r.w = w;
  220. return r;
  221. }
  222. void TPE_vec4Set(TPE_Vec4 *v, TPE_Unit x, TPE_Unit y, TPE_Unit z, TPE_Unit w)
  223. {
  224. v->x = x;
  225. v->y = y;
  226. v->z = z;
  227. v->w = w;
  228. }
  229. TPE_Unit TPE_wrap(TPE_Unit value, TPE_Unit mod)
  230. {
  231. return value >= 0 ? (value % mod) : (mod + (value % mod) - 1);
  232. }
  233. TPE_Unit TPE_clamp(TPE_Unit v, TPE_Unit v1, TPE_Unit v2)
  234. {
  235. return v >= v1 ? (v <= v2 ? v : v2) : v1;
  236. }
  237. TPE_Unit TPE_nonZero(TPE_Unit x)
  238. {
  239. return x + (x == 0);
  240. }
  241. #define TPE_SIN_TABLE_LENGTH 128
  242. static const TPE_Unit TPE_sinTable[TPE_SIN_TABLE_LENGTH] =
  243. {
  244. /* 511 was chosen here as a highest number that doesn't overflow during
  245. compilation for TPE_FRACTIONS_PER_UNIT == 1024 */
  246. (0*TPE_FRACTIONS_PER_UNIT)/511, (6*TPE_FRACTIONS_PER_UNIT)/511,
  247. (12*TPE_FRACTIONS_PER_UNIT)/511, (18*TPE_FRACTIONS_PER_UNIT)/511,
  248. (25*TPE_FRACTIONS_PER_UNIT)/511, (31*TPE_FRACTIONS_PER_UNIT)/511,
  249. (37*TPE_FRACTIONS_PER_UNIT)/511, (43*TPE_FRACTIONS_PER_UNIT)/511,
  250. (50*TPE_FRACTIONS_PER_UNIT)/511, (56*TPE_FRACTIONS_PER_UNIT)/511,
  251. (62*TPE_FRACTIONS_PER_UNIT)/511, (68*TPE_FRACTIONS_PER_UNIT)/511,
  252. (74*TPE_FRACTIONS_PER_UNIT)/511, (81*TPE_FRACTIONS_PER_UNIT)/511,
  253. (87*TPE_FRACTIONS_PER_UNIT)/511, (93*TPE_FRACTIONS_PER_UNIT)/511,
  254. (99*TPE_FRACTIONS_PER_UNIT)/511, (105*TPE_FRACTIONS_PER_UNIT)/511,
  255. (111*TPE_FRACTIONS_PER_UNIT)/511, (118*TPE_FRACTIONS_PER_UNIT)/511,
  256. (124*TPE_FRACTIONS_PER_UNIT)/511, (130*TPE_FRACTIONS_PER_UNIT)/511,
  257. (136*TPE_FRACTIONS_PER_UNIT)/511, (142*TPE_FRACTIONS_PER_UNIT)/511,
  258. (148*TPE_FRACTIONS_PER_UNIT)/511, (154*TPE_FRACTIONS_PER_UNIT)/511,
  259. (160*TPE_FRACTIONS_PER_UNIT)/511, (166*TPE_FRACTIONS_PER_UNIT)/511,
  260. (172*TPE_FRACTIONS_PER_UNIT)/511, (178*TPE_FRACTIONS_PER_UNIT)/511,
  261. (183*TPE_FRACTIONS_PER_UNIT)/511, (189*TPE_FRACTIONS_PER_UNIT)/511,
  262. (195*TPE_FRACTIONS_PER_UNIT)/511, (201*TPE_FRACTIONS_PER_UNIT)/511,
  263. (207*TPE_FRACTIONS_PER_UNIT)/511, (212*TPE_FRACTIONS_PER_UNIT)/511,
  264. (218*TPE_FRACTIONS_PER_UNIT)/511, (224*TPE_FRACTIONS_PER_UNIT)/511,
  265. (229*TPE_FRACTIONS_PER_UNIT)/511, (235*TPE_FRACTIONS_PER_UNIT)/511,
  266. (240*TPE_FRACTIONS_PER_UNIT)/511, (246*TPE_FRACTIONS_PER_UNIT)/511,
  267. (251*TPE_FRACTIONS_PER_UNIT)/511, (257*TPE_FRACTIONS_PER_UNIT)/511,
  268. (262*TPE_FRACTIONS_PER_UNIT)/511, (268*TPE_FRACTIONS_PER_UNIT)/511,
  269. (273*TPE_FRACTIONS_PER_UNIT)/511, (278*TPE_FRACTIONS_PER_UNIT)/511,
  270. (283*TPE_FRACTIONS_PER_UNIT)/511, (289*TPE_FRACTIONS_PER_UNIT)/511,
  271. (294*TPE_FRACTIONS_PER_UNIT)/511, (299*TPE_FRACTIONS_PER_UNIT)/511,
  272. (304*TPE_FRACTIONS_PER_UNIT)/511, (309*TPE_FRACTIONS_PER_UNIT)/511,
  273. (314*TPE_FRACTIONS_PER_UNIT)/511, (319*TPE_FRACTIONS_PER_UNIT)/511,
  274. (324*TPE_FRACTIONS_PER_UNIT)/511, (328*TPE_FRACTIONS_PER_UNIT)/511,
  275. (333*TPE_FRACTIONS_PER_UNIT)/511, (338*TPE_FRACTIONS_PER_UNIT)/511,
  276. (343*TPE_FRACTIONS_PER_UNIT)/511, (347*TPE_FRACTIONS_PER_UNIT)/511,
  277. (352*TPE_FRACTIONS_PER_UNIT)/511, (356*TPE_FRACTIONS_PER_UNIT)/511,
  278. (361*TPE_FRACTIONS_PER_UNIT)/511, (365*TPE_FRACTIONS_PER_UNIT)/511,
  279. (370*TPE_FRACTIONS_PER_UNIT)/511, (374*TPE_FRACTIONS_PER_UNIT)/511,
  280. (378*TPE_FRACTIONS_PER_UNIT)/511, (382*TPE_FRACTIONS_PER_UNIT)/511,
  281. (386*TPE_FRACTIONS_PER_UNIT)/511, (391*TPE_FRACTIONS_PER_UNIT)/511,
  282. (395*TPE_FRACTIONS_PER_UNIT)/511, (398*TPE_FRACTIONS_PER_UNIT)/511,
  283. (402*TPE_FRACTIONS_PER_UNIT)/511, (406*TPE_FRACTIONS_PER_UNIT)/511,
  284. (410*TPE_FRACTIONS_PER_UNIT)/511, (414*TPE_FRACTIONS_PER_UNIT)/511,
  285. (417*TPE_FRACTIONS_PER_UNIT)/511, (421*TPE_FRACTIONS_PER_UNIT)/511,
  286. (424*TPE_FRACTIONS_PER_UNIT)/511, (428*TPE_FRACTIONS_PER_UNIT)/511,
  287. (431*TPE_FRACTIONS_PER_UNIT)/511, (435*TPE_FRACTIONS_PER_UNIT)/511,
  288. (438*TPE_FRACTIONS_PER_UNIT)/511, (441*TPE_FRACTIONS_PER_UNIT)/511,
  289. (444*TPE_FRACTIONS_PER_UNIT)/511, (447*TPE_FRACTIONS_PER_UNIT)/511,
  290. (450*TPE_FRACTIONS_PER_UNIT)/511, (453*TPE_FRACTIONS_PER_UNIT)/511,
  291. (456*TPE_FRACTIONS_PER_UNIT)/511, (459*TPE_FRACTIONS_PER_UNIT)/511,
  292. (461*TPE_FRACTIONS_PER_UNIT)/511, (464*TPE_FRACTIONS_PER_UNIT)/511,
  293. (467*TPE_FRACTIONS_PER_UNIT)/511, (469*TPE_FRACTIONS_PER_UNIT)/511,
  294. (472*TPE_FRACTIONS_PER_UNIT)/511, (474*TPE_FRACTIONS_PER_UNIT)/511,
  295. (476*TPE_FRACTIONS_PER_UNIT)/511, (478*TPE_FRACTIONS_PER_UNIT)/511,
  296. (481*TPE_FRACTIONS_PER_UNIT)/511, (483*TPE_FRACTIONS_PER_UNIT)/511,
  297. (485*TPE_FRACTIONS_PER_UNIT)/511, (487*TPE_FRACTIONS_PER_UNIT)/511,
  298. (488*TPE_FRACTIONS_PER_UNIT)/511, (490*TPE_FRACTIONS_PER_UNIT)/511,
  299. (492*TPE_FRACTIONS_PER_UNIT)/511, (494*TPE_FRACTIONS_PER_UNIT)/511,
  300. (495*TPE_FRACTIONS_PER_UNIT)/511, (497*TPE_FRACTIONS_PER_UNIT)/511,
  301. (498*TPE_FRACTIONS_PER_UNIT)/511, (499*TPE_FRACTIONS_PER_UNIT)/511,
  302. (501*TPE_FRACTIONS_PER_UNIT)/511, (502*TPE_FRACTIONS_PER_UNIT)/511,
  303. (503*TPE_FRACTIONS_PER_UNIT)/511, (504*TPE_FRACTIONS_PER_UNIT)/511,
  304. (505*TPE_FRACTIONS_PER_UNIT)/511, (506*TPE_FRACTIONS_PER_UNIT)/511,
  305. (507*TPE_FRACTIONS_PER_UNIT)/511, (507*TPE_FRACTIONS_PER_UNIT)/511,
  306. (508*TPE_FRACTIONS_PER_UNIT)/511, (509*TPE_FRACTIONS_PER_UNIT)/511,
  307. (509*TPE_FRACTIONS_PER_UNIT)/511, (510*TPE_FRACTIONS_PER_UNIT)/511,
  308. (510*TPE_FRACTIONS_PER_UNIT)/511, (510*TPE_FRACTIONS_PER_UNIT)/511,
  309. (510*TPE_FRACTIONS_PER_UNIT)/511, (510*TPE_FRACTIONS_PER_UNIT)/511
  310. };
  311. #define TPE_SIN_TABLE_UNIT_STEP\
  312. (TPE_FRACTIONS_PER_UNIT / (TPE_SIN_TABLE_LENGTH * 4))
  313. TPE_Unit TPE_sqrt(TPE_Unit value)
  314. {
  315. int8_t sign = 1;
  316. if (value < 0)
  317. {
  318. sign = -1;
  319. value *= -1;
  320. }
  321. uint32_t result = 0;
  322. uint32_t a = value;
  323. uint32_t b = 1u << 30;
  324. while (b > a)
  325. b >>= 2;
  326. while (b != 0)
  327. {
  328. if (a >= result + b)
  329. {
  330. a -= result + b;
  331. result = result + 2 * b;
  332. }
  333. b >>= 2;
  334. result >>= 1;
  335. }
  336. return result * sign;
  337. }
  338. TPE_Unit TPE_sin(TPE_Unit x)
  339. {
  340. x = TPE_wrap(x / TPE_SIN_TABLE_UNIT_STEP,TPE_SIN_TABLE_LENGTH * 4);
  341. int8_t positive = 1;
  342. if (x < TPE_SIN_TABLE_LENGTH)
  343. {
  344. }
  345. else if (x < TPE_SIN_TABLE_LENGTH * 2)
  346. {
  347. x = TPE_SIN_TABLE_LENGTH * 2 - x - 1;
  348. }
  349. else if (x < TPE_SIN_TABLE_LENGTH * 3)
  350. {
  351. x = x - TPE_SIN_TABLE_LENGTH * 2;
  352. positive = 0;
  353. }
  354. else
  355. {
  356. x = TPE_SIN_TABLE_LENGTH - (x - TPE_SIN_TABLE_LENGTH * 3) - 1;
  357. positive = 0;
  358. }
  359. return positive ? TPE_sinTable[x] : -1 * TPE_sinTable[x];
  360. }
  361. TPE_Unit TPE_cos(TPE_Unit x)
  362. {
  363. return TPE_sin(x + TPE_FRACTIONS_PER_UNIT / 4);
  364. }
  365. void TPE_bodyInit(TPE_Body *body)
  366. {
  367. // TODO
  368. TPE_initVec4(&(body->position));
  369. TPE_initVec4(&(body->velocity));
  370. // init orientation to identity unit quaternion (1,0,0,0):
  371. TPE_quaternionInit(&(body->rotation.originalOrientation));
  372. TPE_vec4Set(&(body->rotation.axisVelocity),TPE_FRACTIONS_PER_UNIT,0,0,0);
  373. body->rotation.currentAngle = 0;
  374. body->mass = TPE_FRACTIONS_PER_UNIT;
  375. }
  376. void TPE_bodyGetOrientation(const TPE_Body *body, TPE_Vec4 *quaternion)
  377. {
  378. TPE_Vec4 axisRotation;
  379. TPE_rotationToQuaternion(
  380. body->rotation.axisVelocity,
  381. body->rotation.currentAngle,
  382. &axisRotation);
  383. TPE_quaternionMultiply(
  384. body->rotation.originalOrientation,
  385. axisRotation,
  386. quaternion);
  387. TPE_vec4Normalize(quaternion);
  388. }
  389. void TPE_vec3CrossProduct(TPE_Vec4 a, TPE_Vec4 b, TPE_Vec4 *result)
  390. {
  391. TPE_Vec4 r;
  392. r.x = (a.y * b.z - a.z * b.y) / TPE_FRACTIONS_PER_UNIT;
  393. r.y = (a.z * b.x - a.x * b.z) / TPE_FRACTIONS_PER_UNIT;
  394. r.z = (a.x * b.y - a.y * b.x) / TPE_FRACTIONS_PER_UNIT;
  395. *result = r;
  396. }
  397. TPE_Vec4 TPE_vec3Cross(TPE_Vec4 a, TPE_Vec4 b)
  398. {
  399. TPE_vec3CrossProduct(a,b,&a);
  400. return a;
  401. }
  402. void TPE_bodyApplyVelocity(TPE_Body *body, TPE_Vec4 point, TPE_Vec4 velocity)
  403. {
  404. TPE_Vec4 angularVelocity, rotationAxis;
  405. TPE_vec3Add(body->velocity,velocity,&(body->velocity));
  406. TPE_Unit pointDistance = TPE_vec3Len(point);
  407. if (pointDistance != 0)
  408. {
  409. /* normalize the point, we don't use the function as we don't want to
  410. recompute the vector length */
  411. point.x = (point.x * TPE_FRACTIONS_PER_UNIT) / pointDistance;
  412. point.y = (point.y * TPE_FRACTIONS_PER_UNIT) / pointDistance;
  413. point.z = (point.z * TPE_FRACTIONS_PER_UNIT) / pointDistance;
  414. /* Now we take only a part of the applied velocity, the part projected
  415. to a plane perpendicular to the point vector, and this part will
  416. contribute to the body rotation. */
  417. TPE_Vec4 tmp;
  418. TPE_vec3Project(velocity,point,&tmp);
  419. TPE_vec3Substract(velocity,tmp,&angularVelocity);
  420. TPE_vec3CrossProduct(point,angularVelocity,&rotationAxis);
  421. TPE_bodyAddRotation(body,rotationAxis,
  422. TPE_linearVelocityToAngular(
  423. TPE_vec3Len(angularVelocity),-1 * pointDistance));
  424. }
  425. }
  426. void _TPE_getShapes(const TPE_Body *b1, const TPE_Body *b2, uint8_t shape1,
  427. const TPE_Body **first, const TPE_Body **second)
  428. {
  429. if (b1->shape == shape1)
  430. {
  431. *first = b1;
  432. *second = b2;
  433. }
  434. else
  435. {
  436. *first = b2;
  437. *second = b1;
  438. }
  439. }
  440. TPE_Unit TPE_bodyCollides(const TPE_Body *body1, const TPE_Body *body2,
  441. TPE_Vec4 *collisionPoint, TPE_Vec4 *collisionNormal)
  442. {
  443. // now check the actual collisions:
  444. switch (TPE_COLLISION_TYPE(body1->shape,body2->shape))
  445. {
  446. case TPE_COLLISION_TYPE(TPE_SHAPE_SPHERE,TPE_SHAPE_SPHERE):
  447. {
  448. TPE_Vec4 distanceVec;
  449. TPE_vec3Substract(body2->position,body1->position,&distanceVec);
  450. TPE_Unit distance = TPE_vec3Len(distanceVec);
  451. distance -= body1->shapeParams[0] + body2->shapeParams[0];
  452. if (distance < 0)
  453. {
  454. TPE_vec3Average(body1->position,body2->position,collisionPoint);
  455. *collisionNormal = distanceVec;
  456. TPE_vec3Normalize(collisionNormal);
  457. return -1 * distance;
  458. }
  459. break;
  460. }
  461. case TPE_COLLISION_TYPE(TPE_SHAPE_SPHERE,TPE_SHAPE_CUBOID):
  462. {
  463. const TPE_Body *sphere;
  464. const TPE_Body *cuboid;
  465. _TPE_getShapes(body1,body2,TPE_SHAPE_SPHERE,&sphere,&cuboid);
  466. break;
  467. }
  468. default:
  469. break;
  470. }
  471. return 0;
  472. }
  473. TPE_Vec4 TPE_bodyGetPointVelocity(const TPE_Body *body, TPE_Vec4 point)
  474. {
  475. TPE_Vec4 result = body->velocity;
  476. TPE_Vec4 normal = TPE_vec3Cross(
  477. point,TPE_vec3Minus(point,body->rotation.axisVelocity));
  478. TPE_Unit dist = TPE_vec3Len(normal); // point-line distance
  479. TPE_Unit velocity =
  480. TPE_angularVelocityToLinear(body->rotation.axisVelocity.w,dist);
  481. TPE_vec3Normalize(&normal);
  482. return TPE_vec3Plus(result,TPE_vec3Times(normal,velocity));
  483. }
  484. void TPE_resolveCollision(TPE_Body *body1 ,TPE_Body *body2,
  485. TPE_Vec4 collisionPoint, TPE_Vec4 collisionNormal)
  486. {
  487. TPE_Vec4 v1, v2, p1, p2;
  488. p1 = TPE_vec3Minus(collisionPoint,body1->position);
  489. p2 = TPE_vec3Minus(collisionPoint,body2->position);
  490. v1 = TPE_bodyGetPointVelocity(body1,p1);
  491. v2 = TPE_bodyGetPointVelocity(body2,p2);
  492. int8_t
  493. v1Sign = TPE_vec3DotProduct(v1,collisionNormal) >= 0,
  494. v2Sign = TPE_vec3DotProduct(v2,collisionNormal) >= 0;
  495. if (!v1Sign && v2Sign)
  496. return; // opposite going velocities => not a real collision
  497. TPE_vec3Project(v1,collisionNormal,&v1);
  498. TPE_vec3Project(v2,collisionNormal,&v2);
  499. TPE_Unit
  500. v1Scalar = TPE_vec3Len(v1) * (v1Sign ? 1 : -1),
  501. v2Scalar = TPE_vec3Len(v2) * (v2Sign ? 1 : -1);
  502. if ((v1Sign && v2Sign && (v2Scalar > v1Scalar)) ||
  503. (!v1Sign && !v2Sign && (v1Scalar > v2Scalar)))
  504. return; // not a valid collision
  505. TPE_Unit
  506. v1ScalarNew = v1Scalar,
  507. v2ScalarNew = v2Scalar;
  508. TPE_getVelocitiesAfterCollision(
  509. &v1ScalarNew,
  510. &v2ScalarNew,
  511. body1->mass,
  512. body2->mass,
  513. 512); // TODO: elasticity
  514. TPE_bodyApplyVelocity(body1,p1,
  515. TPE_vec3Times(collisionNormal,v1ScalarNew - v1Scalar));
  516. TPE_bodyApplyVelocity(body2,p2,
  517. TPE_vec3Times(collisionNormal,v2ScalarNew - v2Scalar));
  518. }
  519. TPE_Unit TPE_linearVelocityToAngular(TPE_Unit velocity, TPE_Unit distance)
  520. {
  521. TPE_Unit circumfence = (2 * TPE_PI * distance) / TPE_FRACTIONS_PER_UNIT;
  522. return (velocity * TPE_FRACTIONS_PER_UNIT) / circumfence;
  523. }
  524. TPE_Unit TPE_angularVelocityToLinear(TPE_Unit velocity, TPE_Unit distance)
  525. {
  526. TPE_Unit circumfence = (2 * TPE_PI * distance) / TPE_FRACTIONS_PER_UNIT;
  527. return (velocity * circumfence) / TPE_FRACTIONS_PER_UNIT;
  528. }
  529. void TPE_bodyStep(TPE_Body *body)
  530. {
  531. TPE_vec3Add(body->position,body->velocity,&(body->position));
  532. body->rotation.currentAngle += body->rotation.axisVelocity.w;
  533. }
  534. void TPE_bodySetRotation(TPE_Body *body, TPE_Vec4 axis, TPE_Unit velocity)
  535. {
  536. TPE_bodyGetOrientation(body,&(body->rotation.originalOrientation));
  537. if (velocity < 0)
  538. {
  539. axis.x *= -1;
  540. axis.y *= -1;
  541. axis.z *= -1;
  542. velocity *= -1;
  543. }
  544. TPE_vec3Normalize(&axis);
  545. body->rotation.axisVelocity = axis;
  546. body->rotation.axisVelocity.w = velocity;
  547. body->rotation.currentAngle = 0;
  548. }
  549. void TPE_bodyAddRotation(TPE_Body *body, TPE_Vec4 axis, TPE_Unit velocity)
  550. {
  551. /* Rotation is added like this: we convert both the original and added
  552. rotation to vectors whose direction is along the rotations axis and
  553. magnitude is the rotation speed, then we add these vectors and convert
  554. the final vector back to normalized rotation axis + scalar rotation
  555. speed.*/
  556. body->rotation.axisVelocity.x =
  557. (body->rotation.axisVelocity.x * body->rotation.axisVelocity.w)
  558. / TPE_FRACTIONS_PER_UNIT;
  559. body->rotation.axisVelocity.y =
  560. (body->rotation.axisVelocity.y * body->rotation.axisVelocity.w)
  561. / TPE_FRACTIONS_PER_UNIT;
  562. body->rotation.axisVelocity.z =
  563. (body->rotation.axisVelocity.z * body->rotation.axisVelocity.w)
  564. / TPE_FRACTIONS_PER_UNIT;
  565. TPE_vec3Normalize(&axis);
  566. axis.x = (axis.x * velocity) / TPE_FRACTIONS_PER_UNIT;
  567. axis.y = (axis.y * velocity) / TPE_FRACTIONS_PER_UNIT;
  568. axis.z = (axis.z * velocity) / TPE_FRACTIONS_PER_UNIT;
  569. TPE_vec3Add(body->rotation.axisVelocity,axis,&axis);
  570. axis.w = TPE_vec3Len(axis);
  571. TPE_bodySetRotation(body,axis,axis.w);
  572. }
  573. void TPE_quaternionMultiply(TPE_Vec4 a, TPE_Vec4 b, TPE_Vec4 *result)
  574. {
  575. TPE_Vec4 r; // in case result is identical to a or b
  576. r.x =
  577. (a.w * b.x +
  578. a.x * b.w +
  579. a.y * b.z -
  580. a.z * b.y) / TPE_FRACTIONS_PER_UNIT;
  581. r.y =
  582. (a.w * b.y -
  583. a.x * b.z +
  584. a.y * b.w +
  585. a.z * b.x) / TPE_FRACTIONS_PER_UNIT;
  586. r.z =
  587. (a.w * b.z +
  588. a.x * b.y -
  589. a.y * b.x +
  590. a.z * b.w) / TPE_FRACTIONS_PER_UNIT;
  591. r.w =
  592. (a.w * b.w -
  593. a.x * b.x -
  594. a.y * b.y -
  595. a.z * b.z) / TPE_FRACTIONS_PER_UNIT;
  596. result->x = r.x;
  597. result->y = r.y;
  598. result->z = r.z;
  599. result->w = r.w;
  600. }
  601. void TPE_rotationToQuaternion(TPE_Vec4 axis, TPE_Unit angle, TPE_Vec4 *quaternion)
  602. {
  603. TPE_vec3Normalize(&axis);
  604. angle /= 2;
  605. TPE_Unit s = TPE_sin(angle);
  606. quaternion->x = (s * axis.x) / TPE_FRACTIONS_PER_UNIT;
  607. quaternion->y = (s * axis.y) / TPE_FRACTIONS_PER_UNIT;
  608. quaternion->z = (s * axis.z) / TPE_FRACTIONS_PER_UNIT;
  609. quaternion->w = TPE_cos(angle);
  610. }
  611. TPE_Unit TPE_asin(TPE_Unit x)
  612. {
  613. x = TPE_clamp(x,-TPE_FRACTIONS_PER_UNIT,TPE_FRACTIONS_PER_UNIT);
  614. int8_t sign = 1;
  615. if (x < 0)
  616. {
  617. sign = -1;
  618. x *= -1;
  619. }
  620. int16_t low = 0;
  621. int16_t high = TPE_SIN_TABLE_LENGTH -1;
  622. int16_t middle;
  623. while (low <= high) // binary search
  624. {
  625. middle = (low + high) / 2;
  626. TPE_Unit v = TPE_sinTable[middle];
  627. if (v > x)
  628. high = middle - 1;
  629. else if (v < x)
  630. low = middle + 1;
  631. else
  632. break;
  633. }
  634. middle *= TPE_SIN_TABLE_UNIT_STEP;
  635. return sign * middle;
  636. }
  637. TPE_Unit TPE_acos(TPE_Unit x)
  638. {
  639. return TPE_asin(-1 * x) + TPE_FRACTIONS_PER_UNIT / 4;
  640. }
  641. void TPE_quaternionToRotation(TPE_Vec4 quaternion, TPE_Vec4 *axis, TPE_Unit *angle)
  642. {
  643. *angle = 2 * TPE_acos(quaternion.x);
  644. TPE_Unit tmp =
  645. TPE_nonZero(TPE_sqrt(
  646. (TPE_FRACTIONS_PER_UNIT -
  647. (quaternion.x * quaternion.x) / TPE_FRACTIONS_PER_UNIT
  648. ) * TPE_FRACTIONS_PER_UNIT));
  649. axis->x = (quaternion.x * TPE_FRACTIONS_PER_UNIT) / tmp;
  650. axis->y = (quaternion.y * TPE_FRACTIONS_PER_UNIT) / tmp;
  651. axis->z = (quaternion.z * TPE_FRACTIONS_PER_UNIT) / tmp;
  652. }
  653. void TPE_quaternionToRotationMatrix(TPE_Vec4 quaternion, TPE_Unit matrix[4][4])
  654. {
  655. TPE_Unit
  656. _2x2 = (2 * quaternion.x * quaternion.x) / TPE_FRACTIONS_PER_UNIT,
  657. _2y2 = (2 * quaternion.y * quaternion.y) / TPE_FRACTIONS_PER_UNIT,
  658. _2z2 = (2 * quaternion.z * quaternion.z) / TPE_FRACTIONS_PER_UNIT,
  659. _2xy = (2 * quaternion.x * quaternion.y) / TPE_FRACTIONS_PER_UNIT,
  660. _2xw = (2 * quaternion.x * quaternion.w) / TPE_FRACTIONS_PER_UNIT,
  661. _2zw = (2 * quaternion.z * quaternion.w) / TPE_FRACTIONS_PER_UNIT,
  662. _2xz = (2 * quaternion.x * quaternion.z) / TPE_FRACTIONS_PER_UNIT,
  663. _2yw = (2 * quaternion.y * quaternion.w) / TPE_FRACTIONS_PER_UNIT,
  664. _2yz = (2 * quaternion.y * quaternion.z) / TPE_FRACTIONS_PER_UNIT;
  665. #define ONE TPE_FRACTIONS_PER_UNIT
  666. matrix[0][0] = ONE - _2y2 - _2z2;
  667. matrix[1][0] = _2xy - _2zw;
  668. matrix[2][0] = _2xz + _2yw;
  669. matrix[3][0] = 0;
  670. matrix[0][1] = _2xy + _2zw;
  671. matrix[1][1] = ONE - _2x2 - _2z2;
  672. matrix[2][1] = _2yz - _2xw;
  673. matrix[3][1] = 0;
  674. matrix[0][2] = _2xz - _2yw;
  675. matrix[1][2] = _2yz + _2xw;
  676. matrix[2][2] = ONE - _2x2 - _2y2;
  677. matrix[3][2] = 0;
  678. matrix[0][3] = 0;
  679. matrix[1][3] = 0;
  680. matrix[2][3] = 0;
  681. matrix[3][3] = ONE;
  682. #undef ONE
  683. }
  684. void TPE_vec3Add(const TPE_Vec4 a, const TPE_Vec4 b, TPE_Vec4 *result)
  685. {
  686. result->x = a.x + b.x;
  687. result->y = a.y + b.y;
  688. result->z = a.z + b.z;
  689. }
  690. void TPE_vec4Add(const TPE_Vec4 a, const TPE_Vec4 b, TPE_Vec4 *result)
  691. {
  692. result->x = a.x + b.x;
  693. result->y = a.y + b.y;
  694. result->z = a.z + b.z;
  695. result->w = a.w + b.w;
  696. }
  697. void TPE_vec3Substract(const TPE_Vec4 a, const TPE_Vec4 b, TPE_Vec4 *result)
  698. {
  699. result->x = a.x - b.x;
  700. result->y = a.y - b.y;
  701. result->z = a.z - b.z;
  702. }
  703. TPE_Vec4 TPE_vec3Plus(TPE_Vec4 a, TPE_Vec4 b)
  704. {
  705. a.x += b.x;
  706. a.y += b.y;
  707. a.z += b.z;
  708. return a;
  709. }
  710. TPE_Vec4 TPE_vec3Minus(TPE_Vec4 a, TPE_Vec4 b)
  711. {
  712. a.x -= b.x;
  713. a.y -= b.y;
  714. a.z -= a.z;
  715. return a;
  716. }
  717. TPE_Vec4 TPE_vec3Times(TPE_Vec4 a, TPE_Unit f)
  718. {
  719. a.x = (a.x * f) / TPE_FRACTIONS_PER_UNIT;
  720. a.y = (a.y * f) / TPE_FRACTIONS_PER_UNIT;
  721. a.z = (a.z * f) / TPE_FRACTIONS_PER_UNIT;
  722. return a;
  723. }
  724. void TPE_vec3Average(TPE_Vec4 a, TPE_Vec4 b, TPE_Vec4 *result)
  725. {
  726. result->x = (a.x + b.x) / 2;
  727. result->y = (a.y + b.y) / 2;
  728. result->z = (a.z + b.z) / 2;
  729. }
  730. void TPE_vec4Substract(const TPE_Vec4 a, const TPE_Vec4 b, TPE_Vec4 *result)
  731. {
  732. result->x = a.x - b.x;
  733. result->y = a.y - b.y;
  734. result->z = a.z - b.z;
  735. result->w = a.w - b.w;
  736. }
  737. void TPE_vec3Multiply(const TPE_Vec4 v, TPE_Unit f, TPE_Vec4 *result)
  738. {
  739. result->x = (v.x * f) / TPE_FRACTIONS_PER_UNIT;
  740. result->y = (v.y * f) / TPE_FRACTIONS_PER_UNIT;
  741. result->z = (v.z * f) / TPE_FRACTIONS_PER_UNIT;
  742. }
  743. void TPE_vec4Multiply(const TPE_Vec4 v, TPE_Unit f, TPE_Vec4 *result)
  744. {
  745. result->x = (v.x * f) / TPE_FRACTIONS_PER_UNIT;
  746. result->y = (v.y * f) / TPE_FRACTIONS_PER_UNIT;
  747. result->z = (v.z * f) / TPE_FRACTIONS_PER_UNIT;
  748. result->w = (v.w * f) / TPE_FRACTIONS_PER_UNIT;
  749. }
  750. TPE_Unit TPE_vec3Len(TPE_Vec4 v)
  751. {
  752. return TPE_sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
  753. }
  754. TPE_Unit TPE_vec4Len(TPE_Vec4 v)
  755. {
  756. return TPE_sqrt(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w);
  757. }
  758. TPE_Unit TPE_vec3DotProduct(const TPE_Vec4 v1, const TPE_Vec4 v2)
  759. {
  760. return
  761. (v1.x * v2.x + v1.y * v2.y + v1.z * v2.z) / TPE_FRACTIONS_PER_UNIT;
  762. }
  763. void TPE_vec3Normalize(TPE_Vec4 *v)
  764. {
  765. TPE_Unit l = TPE_vec3Len(*v);
  766. if (l == 0)
  767. {
  768. v->x = TPE_FRACTIONS_PER_UNIT;
  769. return;
  770. }
  771. v->x = (v->x * TPE_FRACTIONS_PER_UNIT) / l;
  772. v->y = (v->y * TPE_FRACTIONS_PER_UNIT) / l;
  773. v->z = (v->z * TPE_FRACTIONS_PER_UNIT) / l;
  774. }
  775. void TPE_vec4Normalize(TPE_Vec4 *v)
  776. {
  777. TPE_Unit l = TPE_vec4Len(*v);
  778. if (l == 0)
  779. {
  780. v->x = TPE_FRACTIONS_PER_UNIT;
  781. return;
  782. }
  783. v->x = (v->x * TPE_FRACTIONS_PER_UNIT) / l;
  784. v->y = (v->y * TPE_FRACTIONS_PER_UNIT) / l;
  785. v->z = (v->z * TPE_FRACTIONS_PER_UNIT) / l;
  786. v->w = (v->w * TPE_FRACTIONS_PER_UNIT) / l;
  787. }
  788. void TPE_vec3Project(TPE_Vec4 v, TPE_Vec4 base, TPE_Vec4 *result)
  789. {
  790. TPE_Unit p = TPE_vec3DotProduct(v,base);
  791. result->x = (p * base.x) / TPE_FRACTIONS_PER_UNIT;
  792. result->y = (p * base.y) / TPE_FRACTIONS_PER_UNIT;
  793. result->z = (p * base.z) / TPE_FRACTIONS_PER_UNIT;
  794. }
  795. void TPE_getVelocitiesAfterCollision(
  796. TPE_Unit *v1,
  797. TPE_Unit *v2,
  798. TPE_Unit m1,
  799. TPE_Unit m2,
  800. TPE_Unit elasticity
  801. )
  802. {
  803. /* in the following a lot of TPE_FRACTIONS_PER_UNIT cancel out, feel free to
  804. check if confused */
  805. #define ANTI_OVERFLOW 30000
  806. #define ANTI_OVERFLOW_SCALE 128
  807. uint8_t overflowDanger = m1 > ANTI_OVERFLOW || *v1 > ANTI_OVERFLOW ||
  808. m2 > ANTI_OVERFLOW || *v2 > ANTI_OVERFLOW;
  809. if (overflowDanger)
  810. {
  811. m1 = (m1 != 0) ? TPE_nonZero(m1 / ANTI_OVERFLOW_SCALE) : 0;
  812. m2 = (m2 != 0) ? TPE_nonZero(m2 / ANTI_OVERFLOW_SCALE) : 0;
  813. *v1 = (*v1 != 0) ? TPE_nonZero(*v1 / ANTI_OVERFLOW_SCALE) : 0;
  814. *v2 = (*v2 != 0) ? TPE_nonZero(*v2 / ANTI_OVERFLOW_SCALE) : 0;
  815. }
  816. TPE_Unit m1Pm2 = TPE_nonZero(m1 + m2);
  817. TPE_Unit v2Mv1 = TPE_nonZero(*v2 - *v1);
  818. TPE_Unit m1v1Pm2v2 = ((m1 * *v1) + (m2 * *v2));
  819. *v1 = (((elasticity * m2 / TPE_FRACTIONS_PER_UNIT) * v2Mv1)
  820. + m1v1Pm2v2) / m1Pm2;
  821. *v2 = (((elasticity * m1 / TPE_FRACTIONS_PER_UNIT) * -1 * v2Mv1)
  822. + m1v1Pm2v2) / m1Pm2;
  823. if (overflowDanger)
  824. {
  825. *v1 *= ANTI_OVERFLOW_SCALE;
  826. *v2 *= ANTI_OVERFLOW_SCALE;
  827. }
  828. #undef ANTI_OVERFLOW
  829. #undef ANTI_OVERFLOW_SCALE
  830. }
  831. void TPE_resolvePointCollision(
  832. TPE_Vec4 collisionPoint,
  833. TPE_Vec4 collisionNormal,
  834. TPE_Unit elasticity,
  835. TPE_Vec4 linVelocity1,
  836. TPE_Vec4 rotVelocity1,
  837. TPE_Unit m1,
  838. TPE_Vec4 linVelocity2,
  839. TPE_Vec4 rotVelocity2,
  840. TPE_Unit m2)
  841. {
  842. TPE_Vec4 v1, v2, v1New, v2New;
  843. TPE_initVec4(&v1);
  844. TPE_initVec4(&v2);
  845. TPE_initVec4(&v1New);
  846. TPE_initVec4(&v2New);
  847. // add lin. and rot. velocities to get the overall vel. of both points:
  848. TPE_vec4Add(linVelocity1,rotVelocity1,&v1);
  849. TPE_vec4Add(linVelocity2,rotVelocity2,&v2);
  850. /* project both of these velocities to the collision normal as we'll apply
  851. the collision equation only in the direction of this normal: */
  852. TPE_vec3Project(v1,collisionNormal,&v1New);
  853. TPE_vec3Project(v2,collisionNormal,&v2New);
  854. // get the velocities of the components
  855. TPE_Unit
  856. v1NewMag = TPE_vec3Len(v1New),
  857. v2NewMag = TPE_vec3Len(v2New);
  858. /* now also substract this component from the original velocity (so that it
  859. will now be in the collision plane), we'll later add back the updated
  860. velocity to it */
  861. TPE_vec4Substract(v1,v1New,&v1);
  862. TPE_vec4Substract(v2,v2New,&v2);
  863. // apply the 1D collision equation to velocities along the normal:
  864. TPE_getVelocitiesAfterCollision(
  865. &v1NewMag,
  866. &v2NewMag,
  867. m1,
  868. m2,
  869. elasticity);
  870. // add back the updated velocities to get the new overall velocities:
  871. v1New.x += (collisionNormal.x * v1NewMag) / TPE_FRACTIONS_PER_UNIT;
  872. v1New.y += (collisionNormal.y * v1NewMag) / TPE_FRACTIONS_PER_UNIT;
  873. v1New.z += (collisionNormal.z * v1NewMag) / TPE_FRACTIONS_PER_UNIT;
  874. v2New.x += (collisionNormal.x * v2NewMag) / TPE_FRACTIONS_PER_UNIT;
  875. v2New.y += (collisionNormal.y * v2NewMag) / TPE_FRACTIONS_PER_UNIT;
  876. v2New.z += (collisionNormal.z * v2NewMag) / TPE_FRACTIONS_PER_UNIT;
  877. // TODO
  878. }
  879. void TPE_bodyGetTransformMatrix(const TPE_Body *body, TPE_Unit matrix[4][4])
  880. {
  881. TPE_Vec4 orientation;
  882. TPE_bodyGetOrientation(body,&orientation);
  883. TPE_quaternionToRotationMatrix(orientation,matrix);
  884. matrix[0][3] = body->position.x;
  885. matrix[1][3] = body->position.y;
  886. matrix[2][3] = body->position.z;
  887. }
  888. void TPE_quaternionInit(TPE_Vec4 *quaternion)
  889. {
  890. quaternion->x = 0;
  891. quaternion->y = 0;
  892. quaternion->z = 0;
  893. quaternion->w = TPE_FRACTIONS_PER_UNIT;
  894. }
  895. #endif // guard