tinyphysicsengine.h 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125
  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. Therefore the library is mostly intended
  15. for entertainment software.
  16. CONVENTIONS:
  17. - Compatibility and simple usage with small3dlib is intended, so most
  18. convention and data types copy those of small3dlib (which takes a lot of
  19. conventions of OpenGL).
  20. - No floating point is used, we instead use integers (effectively a fixed
  21. point). TPE_FRACTIONS_PER_UNIT is an equivalent to 1.0 in floating point and
  22. all numbers are normalized by this constant.
  23. - Units: for any measure only an abstract mathematical unit is used. This unit
  24. always has TPE_FRACTIONS_PER_UNIT parts. You can assign any correcpondence
  25. with real life units to these units. E.g. 1 spatial unit (which you can see
  26. as e.g. 1 meter) is equal to TPE_FRACTIONS_PER_UNIT. Same with temporatl
  27. (e.g. 1 second) and mass (e.g. 1 kilogram) units, and also any derived
  28. units, e.g. a unit of velocity (e.g. 1 m/s) is also equal to 1
  29. TPE_FRACTIONS_PER_UNIT. A full angle is also split into
  30. TPE_FRACTIONS_PER_UNIT parts (instead of 2 * PI or degrees).
  31. - Quaternions are represented as vec4 where x ~ i, y ~ j, z ~ k, w ~ real.
  32. - There is no vec3 type, vec4 is usead for all vectors, for simplicity.
  33. */
  34. #include <stdint.h>
  35. typedef int32_t TPE_Unit;
  36. /** How many fractions a unit is split into. This is NOT SUPPOSED TO BE
  37. REDEFINED, so rather don't do it (otherwise things may overflow etc.). */
  38. #define TPE_FRACTIONS_PER_UNIT 512
  39. #define TPE_INFINITY 2147483647
  40. #define TPE_PI 1608 ///< pi in TPE_Units
  41. #define TPE_SHAPE_POINT 0 ///< single point in space
  42. #define TPE_SHAPE_SPHERE 1 ///< sphere, params.: radius
  43. #define TPE_SHAPE_CAPSULE 2 ///< capsule: radius, height
  44. #define TPE_SHAPE_CUBOID 3 ///< cuboid, params.: width, height, depth
  45. #define TPE_SHAPE_PLANE 4 ///< plane, params.: width, depth
  46. #define TPE_SHAPE_CYLINDER 5 ///< cylinder, params.: radius, height
  47. #define TPE_SHAPE_TRIMESH 6 /**< triangle mesh, params.:
  48. vertex count,
  49. triangle count
  50. vertices (int32_t pointer),
  51. indices (uint16_t pointer) */
  52. #define TPE_MAX_SHAPE_PARAMS 3
  53. #define TPE_MAX_SHAPE_PARAMPOINTERS 2
  54. #define TPE_BODY_FLAG_DISABLED 0x00 ///< won't take part in simul. at all
  55. #define TPE_BODY_FLAG_NONCOLLIDING 0x01 ///< simulated but won't collide
  56. // anti-vibration constants:
  57. #define TPE_ANTI_VIBRATION_MAX_FRAMES 100
  58. #define TPE_ANTI_VIBRATION_INCREMENT 10
  59. #define TPE_ANTI_VIBRATION_VELOCITY_BREAK 50
  60. TPE_Unit TPE_wrap(TPE_Unit value, TPE_Unit mod);
  61. TPE_Unit TPE_clamp(TPE_Unit v, TPE_Unit v1, TPE_Unit v2);
  62. static inline TPE_Unit TPE_abs(TPE_Unit x);
  63. static inline TPE_Unit TPE_nonZero(TPE_Unit x);
  64. /** Returns an integer square root of given value. */
  65. TPE_Unit TPE_sqrt(TPE_Unit value);
  66. /** Multiplies two values (with normalization) so that the result is 0 only if
  67. one or both values are zero. */
  68. TPE_Unit TPE_timesAntiZero(TPE_Unit a, TPE_Unit b);
  69. /** Returns a sine of given arguments, both in TPE_Units (see the library
  70. conventions). */
  71. TPE_Unit TPE_sin(TPE_Unit x);
  72. TPE_Unit TPE_cos(TPE_Unit x);
  73. TPE_Unit TPE_asin(TPE_Unit x);
  74. TPE_Unit TPE_acos(TPE_Unit x);
  75. int8_t TPE_sign(TPE_Unit x);
  76. typedef struct
  77. {
  78. TPE_Unit x;
  79. TPE_Unit y;
  80. TPE_Unit z;
  81. TPE_Unit w;
  82. } TPE_Vec4;
  83. #define TPE_PRINTF_VEC4(v) printf("[%d %d %d %d] ",(v).x,(v).y,(v).z,(v).w);
  84. /** Initializes vec4 to a zero vector. */
  85. void TPE_initVec4(TPE_Vec4 *v);
  86. void TPE_vec4Set(TPE_Vec4 *v, TPE_Unit x, TPE_Unit y, TPE_Unit z, TPE_Unit w);
  87. void TPE_vec3Add(TPE_Vec4 a, TPE_Vec4 b, TPE_Vec4 *result);
  88. void TPE_vec4Add(TPE_Vec4 a, TPE_Vec4 b, TPE_Vec4 *result);
  89. void TPE_vec3Substract(TPE_Vec4 a, TPE_Vec4 b, TPE_Vec4 *result);
  90. void TPE_vec3Average(TPE_Vec4 a, TPE_Vec4 b, TPE_Vec4 *result);
  91. void TPE_vec4Substract(TPE_Vec4 a, TPE_Vec4 b, TPE_Vec4 *result);
  92. void TPE_vec3Multiply(TPE_Vec4 v, TPE_Unit f, TPE_Vec4 *result);
  93. void TPE_vec3MultiplyPlain(TPE_Vec4 v, TPE_Unit f, TPE_Vec4 *result);
  94. void TPE_vec4Multiply(TPE_Vec4 v, TPE_Unit f, TPE_Vec4 *result);
  95. void TPE_vec3CrossProduct(TPE_Vec4 a, TPE_Vec4 b, TPE_Vec4 *result);
  96. void TPE_vec3Normalize(TPE_Vec4 *v);
  97. void TPE_vec4Normalize(TPE_Vec4 *v);
  98. void TPE_vec3Project(TPE_Vec4 v, TPE_Vec4 base, TPE_Vec4 *result);
  99. TPE_Unit TPE_vec3Len(TPE_Vec4 v);
  100. TPE_Unit TPE_vec3LenTaxicab(TPE_Vec4 v);
  101. TPE_Unit TPE_vec3Dist(TPE_Vec4 a, TPE_Vec4 b);
  102. TPE_Unit TPE_vec4Len(TPE_Vec4 v);
  103. TPE_Unit TPE_vec3DotProduct(TPE_Vec4 v1, TPE_Vec4 v2);
  104. TPE_Unit TPE_vec3DotProductPlain(TPE_Vec4 v1, TPE_Vec4 v2);
  105. TPE_Vec4 TPE_vec4(TPE_Unit x, TPE_Unit y, TPE_Unit z, TPE_Unit w);
  106. TPE_Vec4 TPE_vec3Plus(TPE_Vec4 a, TPE_Vec4 b);
  107. TPE_Vec4 TPE_vec3Minus(TPE_Vec4 a, TPE_Vec4 b);
  108. TPE_Vec4 TPE_vec3Times(TPE_Vec4 a, TPE_Unit f);
  109. TPE_Vec4 TPE_vec3TimesAntiZero(TPE_Vec4 a, TPE_Unit f);
  110. TPE_Vec4 TPE_vec3Cross(TPE_Vec4 a, TPE_Vec4 b);
  111. static inline TPE_Vec4 TPE_vec3Normalized(TPE_Vec4 v);
  112. static inline TPE_Vec4 TPE_vec3Projected(TPE_Vec4 v, TPE_Vec4 base);
  113. /** Returns the closest point on given line segment (a,b) to given point (p). */
  114. TPE_Vec4 TPE_lineSegmentClosestPoint(TPE_Vec4 a, TPE_Vec4 b, TPE_Vec4 p);
  115. /** Converts a linear velocity of an orbiting point to the angular velocity
  116. (angle units per time units). This depends on the distance of the point from
  117. the center of rotation. */
  118. TPE_Unit TPE_linearVelocityToAngular(TPE_Unit velocity, TPE_Unit distance);
  119. /** Performs the opposite conversion of TPE_linearVelocityToAngular. */
  120. TPE_Unit TPE_angularVelocityToLinear(TPE_Unit velocity, TPE_Unit distance);
  121. /** Holds a rotation state around a single axis, in a way that prevents rounding
  122. errors from distorting the rotation over time. In theory rotation of a body
  123. could be represented as
  124. [current orientation, axis of rotation, angular velocity]
  125. However applying the rotation and normalizing the orientation quaternion each
  126. simulation step leads to error cumulation and the rotation gets aligned with
  127. one principal axis after some time. Because of this we rather represent the
  128. rotation state as
  129. [original orientation, axis of rotation, angular velocity, current angle]
  130. From this we can at each simulation step compute the current orientation by
  131. applying rotation by current angle to the original rotation without error
  132. cumulation. */
  133. typedef struct
  134. {
  135. TPE_Vec4 originalOrientation; /**< quaternion holding the original
  136. orientation of the body at the time when it
  137. has taken on this rotational state */
  138. TPE_Vec4 axisVelocity; /**< axis of rotation (x,y,z) and a
  139. non-negative angular velocity around this
  140. axis (w), determined ny the right hand
  141. rule */
  142. TPE_Unit currentAngle; /**< angle the body has already rotated along
  143. the rotation axis (from the original
  144. orientation) */
  145. } TPE_RotationState;
  146. typedef struct
  147. {
  148. uint8_t shape;
  149. TPE_Unit shapeParams[TPE_MAX_SHAPE_PARAMS]; ///< parameters of the body type
  150. void *shapeParamPointers[TPE_MAX_SHAPE_PARAMPOINTERS]; ///< pointer parameters
  151. uint8_t flags;
  152. TPE_Unit mass; /**< body mass, setting this to TPE_INFINITY will
  153. make the object static (not moving at all)
  154. which may help performance */
  155. TPE_Vec4 position; ///< position of the body's center of mass
  156. TPE_Vec4 velocity; ///< linear velocity vector
  157. TPE_RotationState rotation; /**< holds the state related to rotation, i.e.
  158. the rotation axis, angular momentum and data
  159. from which current orientation can be
  160. inferred */
  161. TPE_Unit boundingSphereRadius;
  162. uint8_t antiVibration;
  163. } TPE_Body;
  164. /** Initializes a physical body, this should be called on all TPE_Body objects
  165. that are created.*/
  166. void TPE_bodyInit(TPE_Body *body);
  167. /** Recomputes the body bounding sphere, must be called every time the body's
  168. shape parameters change. */
  169. void TPE_bodyRecomputeBounds(TPE_Body *body);
  170. /** Computes a 4x4 transform matrix of given body. The matrix has the same
  171. format as S3L_Mat4 from small3dlib. */
  172. void TPE_bodyGetTransformMatrix(const TPE_Body *body, TPE_Unit matrix[4][4]);
  173. /** Gets the current orientation of a body as a quaternion. */
  174. TPE_Vec4 TPE_bodyGetOrientation(const TPE_Body *body);
  175. /** Multiplies the body's kinetic energy, i.e. changes its linear and angular
  176. velocity. */
  177. void TPE_bodyMultiplyKineticEnergy(TPE_Body *body, TPE_Unit f);
  178. void TPE_bodySetOrientation(TPE_Body *body, TPE_Vec4 orientation);
  179. /** Updates the body position and rotation according to its current velocity
  180. and rotation state. */
  181. void TPE_bodyStep(TPE_Body *body);
  182. /** Sets the rotation state of a body as an axis of rotation and angular
  183. velocity around this axis. */
  184. void TPE_bodySetRotation(TPE_Body *body, TPE_Vec4 axis, TPE_Unit velocity);
  185. /** Adds a rotation to the current rotation of a body. This addition is perfomed
  186. as a vector addition of the current and new rotation represented as vectors
  187. whose direction is the rotation axis and magnitude is the angular velocity
  188. around that axis. */
  189. void TPE_bodyAddRotation(TPE_Body *body, TPE_Vec4 axis, TPE_Unit velocity);
  190. /** Applies impulse (force in short time) to a body at a specified point
  191. (relative to its center), which will potentially change its linear and/or
  192. angular velocity. */
  193. void TPE_bodyApplyImpulse(TPE_Body *body, TPE_Vec4 point, TPE_Vec4 impulse);
  194. /** Computes and returns a body's bounding sphere radius, i.e. the maximum
  195. extent from its center point. */
  196. TPE_Unit TPE_bodyGetMaxExtent(const TPE_Body *body);
  197. /** Computes and returns a body's total kinetic energy (sum of linear and
  198. rotational kin. energy). In rotating bodies this may not be physically
  199. accurate as, for simplicity, we operate with the moment of inertia of sphere
  200. for all bodies (when in reality moment of inertia depends on shape). */
  201. TPE_Unit TPE_bodyGetKineticEnergy(const TPE_Body *body);
  202. /** Attempts to correct rounding errors in the total energy of a system of two
  203. bodies after collision, given the initial energy and desired restitution
  204. (fraction of energy that should remain after the collision). */
  205. void TPE_correctEnergies(TPE_Body *body1, TPE_Body *body2,
  206. TPE_Unit previousEnergy, TPE_Unit restitution);
  207. /** Collision detection: checks if two bodies are colliding. The return value is
  208. the collision depth along the collision normal (0 if the bodies are not
  209. colliding). World-space collision point is returned via a pointer. Collision
  210. normal (normalized) is also returned via a pointer and its direction is
  211. "away from body1", i.e. if you move body1 in the opposite direction of this
  212. normal by the collision depth (return value), the bodies should no longer
  213. exhibit this particular collision. This function checks the bounding spheres
  214. to quickly opt out of impossible collisions. */
  215. TPE_Unit TPE_bodyCollides(const TPE_Body *body1, const TPE_Body *body2,
  216. TPE_Vec4 *collisionPoint, TPE_Vec4 *collisionNormal);
  217. /** Gets a velocity of a single point on a rigid body, taking into account its
  218. linear velocity and rotation. The point coordinates are relative to the body
  219. center. The point does NOT have to be on the surface, it can be inside and
  220. even outside the body too. */
  221. TPE_Vec4 TPE_bodyGetPointVelocity(const TPE_Body *body, TPE_Vec4 point);
  222. void TPE_resolveCollision(TPE_Body *body1 ,TPE_Body *body2,
  223. TPE_Vec4 collisionPoint, TPE_Vec4 collisionNormal, TPE_Unit collisionDepth,
  224. TPE_Unit energyMultiplier);
  225. /** Gets a uint16_t integer type of collision depending on two shapes, the order
  226. of shapes doesn't matter. */
  227. #define TPE_COLLISION_TYPE(shape1,shape2) \
  228. ((shape1) <= (shape2) ? \
  229. (((uint16_t) (shape1)) << 8) | (shape2) : \
  230. (((uint16_t) (shape2)) << 8) | (shape1))
  231. typedef struct
  232. {
  233. uint16_t bodyCount;
  234. TPE_Body *bodies;
  235. } TPE_PhysicsWorld;
  236. /** Multiplies two quaternions which can be seen as chaining two rotations
  237. represented by them. This is not commutative (a*b != b*a)! Rotations a is
  238. performed firth, then rotation b is performed. */
  239. void TPE_quaternionMultiply(TPE_Vec4 a, TPE_Vec4 b, TPE_Vec4 *result);
  240. /** Initializes quaternion to the rotation identity (i.e. NOT zero
  241. quaternion). */
  242. void TPE_quaternionInit(TPE_Vec4 *quaternion);
  243. /** Converts a rotation given as an axis and angle around this axis (by right
  244. hand rule) to a rotation quaternion. */
  245. void TPE_rotationToQuaternion(TPE_Vec4 axis, TPE_Unit angle,
  246. TPE_Vec4 *quaternion);
  247. void TPE_quaternionToRotation(TPE_Vec4 quaternion, TPE_Vec4 *axis,
  248. TPE_Unit *angle);
  249. /** Computes the conjugate of a quaternion (analogous to matrix inversion, the
  250. quaternion will represent the opposite rotation). */
  251. TPE_Vec4 TPE_quaternionConjugate(TPE_Vec4 quaternion);
  252. /** Converts a rotation quaternion to a 4x4 rotation matrix. The matrix is
  253. indexed as [column][row] and is in the same format as S3L_Mat4 from
  254. small3dlib. */
  255. void TPE_quaternionToRotationMatrix(TPE_Vec4 quaternion, TPE_Unit matrix[4][4]);
  256. void TPE_rotatePoint(TPE_Vec4 *point, TPE_Vec4 quaternion);
  257. void TPE_getVelocitiesAfterCollision(
  258. TPE_Unit *v1,
  259. TPE_Unit *v2,
  260. TPE_Unit m1,
  261. TPE_Unit m2,
  262. TPE_Unit elasticity
  263. );
  264. //------------------------------------------------------------------------------
  265. void TPE_initVec4(TPE_Vec4 *v)
  266. {
  267. v->x = 0;
  268. v->y = 0;
  269. v->z = 0;
  270. v->w = 0;
  271. }
  272. TPE_Vec4 TPE_vec4(TPE_Unit x, TPE_Unit y, TPE_Unit z, TPE_Unit w)
  273. {
  274. TPE_Vec4 r;
  275. r.x = x;
  276. r.y = y;
  277. r.z = z;
  278. r.w = w;
  279. return r;
  280. }
  281. void TPE_vec4Set(TPE_Vec4 *v, TPE_Unit x, TPE_Unit y, TPE_Unit z, TPE_Unit w)
  282. {
  283. v->x = x;
  284. v->y = y;
  285. v->z = z;
  286. v->w = w;
  287. }
  288. TPE_Unit TPE_wrap(TPE_Unit value, TPE_Unit mod)
  289. {
  290. return value >= 0 ? (value % mod) : (mod + (value % mod) - 1);
  291. }
  292. TPE_Unit TPE_clamp(TPE_Unit v, TPE_Unit v1, TPE_Unit v2)
  293. {
  294. return v >= v1 ? (v <= v2 ? v : v2) : v1;
  295. }
  296. TPE_Unit TPE_nonZero(TPE_Unit x)
  297. {
  298. return x + (x == 0);
  299. }
  300. #define TPE_SIN_TABLE_LENGTH 128
  301. static const TPE_Unit TPE_sinTable[TPE_SIN_TABLE_LENGTH] =
  302. {
  303. /* 511 was chosen here as a highest number that doesn't overflow during
  304. compilation for TPE_FRACTIONS_PER_UNIT == 1024 */
  305. (0*TPE_FRACTIONS_PER_UNIT)/511, (6*TPE_FRACTIONS_PER_UNIT)/511,
  306. (12*TPE_FRACTIONS_PER_UNIT)/511, (18*TPE_FRACTIONS_PER_UNIT)/511,
  307. (25*TPE_FRACTIONS_PER_UNIT)/511, (31*TPE_FRACTIONS_PER_UNIT)/511,
  308. (37*TPE_FRACTIONS_PER_UNIT)/511, (43*TPE_FRACTIONS_PER_UNIT)/511,
  309. (50*TPE_FRACTIONS_PER_UNIT)/511, (56*TPE_FRACTIONS_PER_UNIT)/511,
  310. (62*TPE_FRACTIONS_PER_UNIT)/511, (68*TPE_FRACTIONS_PER_UNIT)/511,
  311. (74*TPE_FRACTIONS_PER_UNIT)/511, (81*TPE_FRACTIONS_PER_UNIT)/511,
  312. (87*TPE_FRACTIONS_PER_UNIT)/511, (93*TPE_FRACTIONS_PER_UNIT)/511,
  313. (99*TPE_FRACTIONS_PER_UNIT)/511, (105*TPE_FRACTIONS_PER_UNIT)/511,
  314. (111*TPE_FRACTIONS_PER_UNIT)/511, (118*TPE_FRACTIONS_PER_UNIT)/511,
  315. (124*TPE_FRACTIONS_PER_UNIT)/511, (130*TPE_FRACTIONS_PER_UNIT)/511,
  316. (136*TPE_FRACTIONS_PER_UNIT)/511, (142*TPE_FRACTIONS_PER_UNIT)/511,
  317. (148*TPE_FRACTIONS_PER_UNIT)/511, (154*TPE_FRACTIONS_PER_UNIT)/511,
  318. (160*TPE_FRACTIONS_PER_UNIT)/511, (166*TPE_FRACTIONS_PER_UNIT)/511,
  319. (172*TPE_FRACTIONS_PER_UNIT)/511, (178*TPE_FRACTIONS_PER_UNIT)/511,
  320. (183*TPE_FRACTIONS_PER_UNIT)/511, (189*TPE_FRACTIONS_PER_UNIT)/511,
  321. (195*TPE_FRACTIONS_PER_UNIT)/511, (201*TPE_FRACTIONS_PER_UNIT)/511,
  322. (207*TPE_FRACTIONS_PER_UNIT)/511, (212*TPE_FRACTIONS_PER_UNIT)/511,
  323. (218*TPE_FRACTIONS_PER_UNIT)/511, (224*TPE_FRACTIONS_PER_UNIT)/511,
  324. (229*TPE_FRACTIONS_PER_UNIT)/511, (235*TPE_FRACTIONS_PER_UNIT)/511,
  325. (240*TPE_FRACTIONS_PER_UNIT)/511, (246*TPE_FRACTIONS_PER_UNIT)/511,
  326. (251*TPE_FRACTIONS_PER_UNIT)/511, (257*TPE_FRACTIONS_PER_UNIT)/511,
  327. (262*TPE_FRACTIONS_PER_UNIT)/511, (268*TPE_FRACTIONS_PER_UNIT)/511,
  328. (273*TPE_FRACTIONS_PER_UNIT)/511, (278*TPE_FRACTIONS_PER_UNIT)/511,
  329. (283*TPE_FRACTIONS_PER_UNIT)/511, (289*TPE_FRACTIONS_PER_UNIT)/511,
  330. (294*TPE_FRACTIONS_PER_UNIT)/511, (299*TPE_FRACTIONS_PER_UNIT)/511,
  331. (304*TPE_FRACTIONS_PER_UNIT)/511, (309*TPE_FRACTIONS_PER_UNIT)/511,
  332. (314*TPE_FRACTIONS_PER_UNIT)/511, (319*TPE_FRACTIONS_PER_UNIT)/511,
  333. (324*TPE_FRACTIONS_PER_UNIT)/511, (328*TPE_FRACTIONS_PER_UNIT)/511,
  334. (333*TPE_FRACTIONS_PER_UNIT)/511, (338*TPE_FRACTIONS_PER_UNIT)/511,
  335. (343*TPE_FRACTIONS_PER_UNIT)/511, (347*TPE_FRACTIONS_PER_UNIT)/511,
  336. (352*TPE_FRACTIONS_PER_UNIT)/511, (356*TPE_FRACTIONS_PER_UNIT)/511,
  337. (361*TPE_FRACTIONS_PER_UNIT)/511, (365*TPE_FRACTIONS_PER_UNIT)/511,
  338. (370*TPE_FRACTIONS_PER_UNIT)/511, (374*TPE_FRACTIONS_PER_UNIT)/511,
  339. (378*TPE_FRACTIONS_PER_UNIT)/511, (382*TPE_FRACTIONS_PER_UNIT)/511,
  340. (386*TPE_FRACTIONS_PER_UNIT)/511, (391*TPE_FRACTIONS_PER_UNIT)/511,
  341. (395*TPE_FRACTIONS_PER_UNIT)/511, (398*TPE_FRACTIONS_PER_UNIT)/511,
  342. (402*TPE_FRACTIONS_PER_UNIT)/511, (406*TPE_FRACTIONS_PER_UNIT)/511,
  343. (410*TPE_FRACTIONS_PER_UNIT)/511, (414*TPE_FRACTIONS_PER_UNIT)/511,
  344. (417*TPE_FRACTIONS_PER_UNIT)/511, (421*TPE_FRACTIONS_PER_UNIT)/511,
  345. (424*TPE_FRACTIONS_PER_UNIT)/511, (428*TPE_FRACTIONS_PER_UNIT)/511,
  346. (431*TPE_FRACTIONS_PER_UNIT)/511, (435*TPE_FRACTIONS_PER_UNIT)/511,
  347. (438*TPE_FRACTIONS_PER_UNIT)/511, (441*TPE_FRACTIONS_PER_UNIT)/511,
  348. (444*TPE_FRACTIONS_PER_UNIT)/511, (447*TPE_FRACTIONS_PER_UNIT)/511,
  349. (450*TPE_FRACTIONS_PER_UNIT)/511, (453*TPE_FRACTIONS_PER_UNIT)/511,
  350. (456*TPE_FRACTIONS_PER_UNIT)/511, (459*TPE_FRACTIONS_PER_UNIT)/511,
  351. (461*TPE_FRACTIONS_PER_UNIT)/511, (464*TPE_FRACTIONS_PER_UNIT)/511,
  352. (467*TPE_FRACTIONS_PER_UNIT)/511, (469*TPE_FRACTIONS_PER_UNIT)/511,
  353. (472*TPE_FRACTIONS_PER_UNIT)/511, (474*TPE_FRACTIONS_PER_UNIT)/511,
  354. (476*TPE_FRACTIONS_PER_UNIT)/511, (478*TPE_FRACTIONS_PER_UNIT)/511,
  355. (481*TPE_FRACTIONS_PER_UNIT)/511, (483*TPE_FRACTIONS_PER_UNIT)/511,
  356. (485*TPE_FRACTIONS_PER_UNIT)/511, (487*TPE_FRACTIONS_PER_UNIT)/511,
  357. (488*TPE_FRACTIONS_PER_UNIT)/511, (490*TPE_FRACTIONS_PER_UNIT)/511,
  358. (492*TPE_FRACTIONS_PER_UNIT)/511, (494*TPE_FRACTIONS_PER_UNIT)/511,
  359. (495*TPE_FRACTIONS_PER_UNIT)/511, (497*TPE_FRACTIONS_PER_UNIT)/511,
  360. (498*TPE_FRACTIONS_PER_UNIT)/511, (499*TPE_FRACTIONS_PER_UNIT)/511,
  361. (501*TPE_FRACTIONS_PER_UNIT)/511, (502*TPE_FRACTIONS_PER_UNIT)/511,
  362. (503*TPE_FRACTIONS_PER_UNIT)/511, (504*TPE_FRACTIONS_PER_UNIT)/511,
  363. (505*TPE_FRACTIONS_PER_UNIT)/511, (506*TPE_FRACTIONS_PER_UNIT)/511,
  364. (507*TPE_FRACTIONS_PER_UNIT)/511, (507*TPE_FRACTIONS_PER_UNIT)/511,
  365. (508*TPE_FRACTIONS_PER_UNIT)/511, (509*TPE_FRACTIONS_PER_UNIT)/511,
  366. (509*TPE_FRACTIONS_PER_UNIT)/511, (510*TPE_FRACTIONS_PER_UNIT)/511,
  367. (510*TPE_FRACTIONS_PER_UNIT)/511, (510*TPE_FRACTIONS_PER_UNIT)/511,
  368. (510*TPE_FRACTIONS_PER_UNIT)/511, (510*TPE_FRACTIONS_PER_UNIT)/511
  369. };
  370. #define TPE_SIN_TABLE_UNIT_STEP\
  371. (TPE_FRACTIONS_PER_UNIT / (TPE_SIN_TABLE_LENGTH * 4))
  372. TPE_Unit TPE_sqrt(TPE_Unit value)
  373. {
  374. int8_t sign = 1;
  375. if (value < 0)
  376. {
  377. sign = -1;
  378. value *= -1;
  379. }
  380. uint32_t result = 0;
  381. uint32_t a = value;
  382. uint32_t b = 1u << 30;
  383. while (b > a)
  384. b >>= 2;
  385. while (b != 0)
  386. {
  387. if (a >= result + b)
  388. {
  389. a -= result + b;
  390. result = result + 2 * b;
  391. }
  392. b >>= 2;
  393. result >>= 1;
  394. }
  395. return result * sign;
  396. }
  397. TPE_Unit TPE_sin(TPE_Unit x)
  398. {
  399. x = TPE_wrap(x / TPE_SIN_TABLE_UNIT_STEP,TPE_SIN_TABLE_LENGTH * 4);
  400. int8_t positive = 1;
  401. if (x < TPE_SIN_TABLE_LENGTH)
  402. {
  403. }
  404. else if (x < TPE_SIN_TABLE_LENGTH * 2)
  405. {
  406. x = TPE_SIN_TABLE_LENGTH * 2 - x - 1;
  407. }
  408. else if (x < TPE_SIN_TABLE_LENGTH * 3)
  409. {
  410. x = x - TPE_SIN_TABLE_LENGTH * 2;
  411. positive = 0;
  412. }
  413. else
  414. {
  415. x = TPE_SIN_TABLE_LENGTH - (x - TPE_SIN_TABLE_LENGTH * 3) - 1;
  416. positive = 0;
  417. }
  418. return positive ? TPE_sinTable[x] : -1 * TPE_sinTable[x];
  419. }
  420. TPE_Unit TPE_cos(TPE_Unit x)
  421. {
  422. return TPE_sin(x + TPE_FRACTIONS_PER_UNIT / 4);
  423. }
  424. void TPE_bodyInit(TPE_Body *body)
  425. {
  426. // TODO
  427. TPE_initVec4(&(body->position));
  428. TPE_initVec4(&(body->velocity));
  429. // init orientation to identity unit quaternion (1,0,0,0):
  430. TPE_quaternionInit(&(body->rotation.originalOrientation));
  431. TPE_vec4Set(&(body->rotation.axisVelocity),TPE_FRACTIONS_PER_UNIT,0,0,0);
  432. body->rotation.currentAngle = 0;
  433. body->mass = TPE_FRACTIONS_PER_UNIT;
  434. body->boundingSphereRadius = 0;
  435. body->antiVibration = 0;
  436. }
  437. void TPE_bodySetOrientation(TPE_Body *body, TPE_Vec4 orientation)
  438. {
  439. body->rotation.originalOrientation = orientation;
  440. body->rotation.currentAngle = 0;
  441. }
  442. TPE_Vec4 TPE_bodyGetOrientation(const TPE_Body *body)
  443. {
  444. TPE_Vec4 axisRotation, result;
  445. TPE_rotationToQuaternion(
  446. body->rotation.axisVelocity,
  447. body->rotation.currentAngle,
  448. &axisRotation);
  449. TPE_quaternionMultiply(
  450. body->rotation.originalOrientation,
  451. axisRotation,
  452. &result);
  453. TPE_vec4Normalize(&result);
  454. return result;
  455. }
  456. void TPE_vec3CrossProduct(TPE_Vec4 a, TPE_Vec4 b, TPE_Vec4 *result)
  457. {
  458. TPE_Vec4 r;
  459. r.x = (a.y * b.z - a.z * b.y) / TPE_FRACTIONS_PER_UNIT;
  460. r.y = (a.z * b.x - a.x * b.z) / TPE_FRACTIONS_PER_UNIT;
  461. r.z = (a.x * b.y - a.y * b.x) / TPE_FRACTIONS_PER_UNIT;
  462. *result = r;
  463. }
  464. TPE_Vec4 TPE_vec3Cross(TPE_Vec4 a, TPE_Vec4 b)
  465. {
  466. TPE_vec3CrossProduct(a,b,&a);
  467. return a;
  468. }
  469. void TPE_bodyApplyImpulse(TPE_Body *body, TPE_Vec4 point, TPE_Vec4 impulse)
  470. {
  471. TPE_Unit pointDistance = TPE_vec3Len(point);
  472. if (pointDistance != 0)
  473. {
  474. impulse.x = (impulse.x * TPE_FRACTIONS_PER_UNIT) / body->mass;
  475. impulse.y = (impulse.y * TPE_FRACTIONS_PER_UNIT) / body->mass;
  476. impulse.z = (impulse.z * TPE_FRACTIONS_PER_UNIT) / body->mass;
  477. TPE_vec3Add(body->velocity,impulse,&(body->velocity));
  478. /* normalize the point, we don't use the function as we don't want to
  479. recompute the vector length */
  480. point.x = (point.x * TPE_FRACTIONS_PER_UNIT) / pointDistance;
  481. point.y = (point.y * TPE_FRACTIONS_PER_UNIT) / pointDistance;
  482. point.z = (point.z * TPE_FRACTIONS_PER_UNIT) / pointDistance;
  483. /* for simplicity we'll suppose angular momentum of a sphere: */
  484. impulse = TPE_vec3Cross(impulse,point);
  485. TPE_Unit r = TPE_bodyGetMaxExtent(body);
  486. r = TPE_nonZero((2 * r * r) / TPE_FRACTIONS_PER_UNIT);
  487. TPE_Vec4 tmp = impulse;
  488. impulse.x = (impulse.x * 5 * TPE_FRACTIONS_PER_UNIT) / r;
  489. impulse.y = (impulse.y * 5 * TPE_FRACTIONS_PER_UNIT) / r;
  490. impulse.z = (impulse.z * 5 * TPE_FRACTIONS_PER_UNIT) / r;
  491. if (impulse.x == 0 &&
  492. impulse.y == 0 &&
  493. impulse.z == 0 &&
  494. (
  495. tmp.x != 0 ||
  496. tmp.y != 0 ||
  497. tmp.z != 0
  498. ))
  499. {
  500. impulse.x = TPE_sign(tmp.x);
  501. impulse.y = TPE_sign(tmp.y);
  502. impulse.z = TPE_sign(tmp.z);
  503. }
  504. /*
  505. impulse.x = (impulse.x * 5 * TPE_FRACTIONS_PER_UNIT) / r;
  506. impulse.y = (impulse.y * 5 * TPE_FRACTIONS_PER_UNIT) / r;
  507. impulse.z = (impulse.z * 5 * TPE_FRACTIONS_PER_UNIT) / r;
  508. */
  509. TPE_bodyAddRotation(body,impulse,TPE_vec3Len(impulse));
  510. }
  511. }
  512. void _TPE_getShapes(const TPE_Body *b1, const TPE_Body *b2, uint8_t shape1,
  513. const TPE_Body **first, const TPE_Body **second)
  514. {
  515. if (b1->shape == shape1)
  516. {
  517. *first = b1;
  518. *second = b2;
  519. }
  520. else
  521. {
  522. *first = b2;
  523. *second = b1;
  524. }
  525. }
  526. void _TPE_getCapsuleCyllinderEndpoints(const TPE_Body *body,
  527. TPE_Vec4 *a, TPE_Vec4 *b)
  528. {
  529. TPE_Vec4 quat = TPE_bodyGetOrientation(body);
  530. *a = TPE_vec4(0,body->shapeParams[1] / 2,0,0);
  531. *b = TPE_vec4(0,-1 * a->y,0,0);
  532. TPE_rotatePoint(a,quat);
  533. TPE_rotatePoint(b,quat);
  534. TPE_vec3Add(*a,body->position,a);
  535. TPE_vec3Add(*b,body->position,b);
  536. }
  537. /** Helpter function for cuboid collision detection. Given a line segment
  538. as a line equation limited by parameter bounds t1 and t2, center point C and
  539. side offset from the center point O, the function further limits the parameter
  540. bounds (t1, t2) to restrict the line only to the region between two planes:
  541. both with normal O, one passing throung point C + O and the other through
  542. C - O. If t2 > t1 after this function finishes, the line segment is completely
  543. outside the region. */
  544. void _TPE_cutLineSegmentByPlanes(TPE_Vec4 center, TPE_Vec4 sideOffset,
  545. TPE_Vec4 lineStart, TPE_Vec4 lineDir, TPE_Unit *t1, TPE_Unit *t2)
  546. {
  547. // we shift the center to [0,0,0] to simplify and prevent overflows
  548. lineStart = TPE_vec3Minus(lineStart,center);
  549. //center = TPE_vec4(0,0,0,0);
  550. TPE_Unit da = TPE_vec3DotProductPlain(sideOffset,lineStart);
  551. TPE_Vec4 dc;
  552. dc.z = 0;
  553. // TODO: dot(d,dc) could be cached for all sides between calls to save recomputing
  554. dc = sideOffset;
  555. TPE_Unit denom = TPE_nonZero(TPE_vec3DotProductPlain(sideOffset,lineDir));
  556. #define tAntiOverflow(t) \
  557. TPE_Unit t = TPE_vec3DotProductPlain(sideOffset,dc) - da;\
  558. t = (TPE_abs(t) < 500000) ? (t * TPE_FRACTIONS_PER_UNIT) / denom :\
  559. (((t / 64) * TPE_FRACTIONS_PER_UNIT) / TPE_nonZero(denom / 64));
  560. tAntiOverflow(tA)
  561. TPE_vec3MultiplyPlain(sideOffset,-1,&dc);
  562. tAntiOverflow(tB)
  563. #undef tAntiOverflow
  564. if (tB < tA)
  565. {
  566. TPE_Unit tmp = tA;
  567. tA = tB;
  568. tB = tmp;
  569. }
  570. if (tA > *t1)
  571. *t1 = tA;
  572. if (tB < *t2)
  573. *t2 = tB;
  574. }
  575. TPE_Unit TPE_bodyCollides(const TPE_Body *body1, const TPE_Body *body2,
  576. TPE_Vec4 *collisionPoint, TPE_Vec4 *collisionNormal)
  577. {
  578. // handle collision of different shapes each in a specific case:
  579. uint16_t collType = TPE_COLLISION_TYPE(body1->shape,body2->shape);
  580. if (collType != TPE_COLLISION_TYPE(TPE_SHAPE_SPHERE,TPE_SHAPE_SPHERE))
  581. {
  582. /* initial bounding sphere check to quickly discard impossible collisions,
  583. plus this also prevents overflow errors in long-distance computations */
  584. // TODO: taxicab could be also considered here
  585. if (TPE_vec3Len(TPE_vec3Minus(body1->position,body2->position)) >
  586. body1->boundingSphereRadius + body2->boundingSphereRadius)
  587. return 0;
  588. }
  589. switch (TPE_COLLISION_TYPE(body1->shape,body2->shape))
  590. {
  591. case TPE_COLLISION_TYPE(TPE_SHAPE_SPHERE,TPE_SHAPE_SPHERE):
  592. {
  593. TPE_Vec4 distanceVec;
  594. TPE_vec3Substract(body2->position,body1->position,&distanceVec);
  595. TPE_Unit distance = TPE_vec3Len(distanceVec);
  596. distance -= body1->shapeParams[0] + body2->shapeParams[0];
  597. if (distance < 0)
  598. {
  599. TPE_vec3Average(body1->position,body2->position,collisionPoint);
  600. *collisionNormal = distanceVec;
  601. TPE_vec3Normalize(collisionNormal);
  602. return -1 * distance;
  603. }
  604. break;
  605. }
  606. case TPE_COLLISION_TYPE(TPE_SHAPE_SPHERE,TPE_SHAPE_CAPSULE):
  607. {
  608. const TPE_Body *sphere;
  609. const TPE_Body *capsule;
  610. _TPE_getShapes(body1,body2,TPE_SHAPE_SPHERE,&sphere,&capsule);
  611. TPE_Vec4 cA, cB;
  612. _TPE_getCapsuleCyllinderEndpoints(capsule,&cA,&cB);
  613. TPE_Body sphere2; // sphere at the capsule's closest point
  614. TPE_bodyInit(&sphere2);
  615. sphere2.shape = TPE_SHAPE_SPHERE;
  616. sphere2.shapeParams[0] = capsule->shapeParams[0];
  617. sphere2.position = TPE_lineSegmentClosestPoint(cA,cB,sphere->position);
  618. uint8_t swap = sphere == body2;
  619. return TPE_bodyCollides(swap ? &sphere2 : sphere,swap ? sphere : &sphere2,
  620. collisionPoint,collisionNormal);
  621. break;
  622. }
  623. case TPE_COLLISION_TYPE(TPE_SHAPE_CAPSULE,TPE_SHAPE_CAPSULE):
  624. {
  625. TPE_Vec4 a1, b1, a2, b2;
  626. _TPE_getCapsuleCyllinderEndpoints(body1,&a1,&b1);
  627. _TPE_getCapsuleCyllinderEndpoints(body2,&a2,&b2);
  628. TPE_Unit aa, ab, ba, bb; // squared distances between points
  629. TPE_Vec4 tmp;
  630. tmp = TPE_vec3Minus(a1,a2);
  631. aa = tmp.x * tmp.x + tmp.y * tmp.y + tmp.z * tmp.z;
  632. tmp = TPE_vec3Minus(a1,b2);
  633. ab = tmp.x * tmp.x + tmp.y * tmp.y + tmp.z * tmp.z;
  634. tmp = TPE_vec3Minus(b1,a2);
  635. ba = tmp.x * tmp.x + tmp.y * tmp.y + tmp.z * tmp.z;
  636. tmp = TPE_vec3Minus(b1,b2);
  637. bb = tmp.x * tmp.x + tmp.y * tmp.y + tmp.z * tmp.z;
  638. // let a1 hold the point figuring in the shortest distance:
  639. if (ab < aa)
  640. aa = ab; // means: aa = min(aa,ab)
  641. if (bb < ba)
  642. ba = bb; // means: ba = min(ba,bb)
  643. if (ba < aa) // means: min(ba,bb) < min(aa,ab)
  644. a1 = b1;
  645. a2 = TPE_lineSegmentClosestPoint(a2,b2,a1);
  646. a1 = TPE_lineSegmentClosestPoint(a1,b1,a2);
  647. // now a1 and a2 are the closest two points on capsule axes
  648. TPE_Body sphere1, sphere2;
  649. TPE_bodyInit(&sphere1);
  650. sphere1.shape = TPE_SHAPE_SPHERE;
  651. sphere1.shapeParams[0] = body1->shapeParams[0];
  652. sphere1.position = a1;
  653. TPE_bodyInit(&sphere2);
  654. sphere2.shape = TPE_SHAPE_SPHERE;
  655. sphere2.shapeParams[0] = body2->shapeParams[0];
  656. sphere2.position = a2;
  657. return TPE_bodyCollides(&sphere1,&sphere2,collisionPoint,collisionNormal);
  658. break;
  659. }
  660. case TPE_COLLISION_TYPE(TPE_SHAPE_SPHERE,TPE_SHAPE_CYLINDER):
  661. {
  662. // TODO: would this be better to do via sphere-capsule collision?
  663. const TPE_Body *sphere;
  664. const TPE_Body *cylinder;
  665. _TPE_getShapes(body1,body2,TPE_SHAPE_SPHERE,&sphere,&cylinder);
  666. TPE_Vec4 sphereRelativePos = // by this we shift the cylinder to [0,0,0]
  667. TPE_vec3Minus(sphere->position,cylinder->position);
  668. // vector along the cylinder height:
  669. TPE_Vec4 cylinderAxis = TPE_vec4(0,TPE_FRACTIONS_PER_UNIT,0,0);
  670. TPE_rotatePoint(&cylinderAxis,TPE_bodyGetOrientation(cylinder));
  671. TPE_Vec4 sphereAxisPos = // sphere pos projected to the cylinder axis
  672. TPE_vec3Projected(sphereRelativePos,cylinderAxis);
  673. TPE_Unit sphereAxisDistance = TPE_vec3Len(sphereAxisPos);
  674. TPE_Unit tmp = cylinder->shapeParams[1] / 2; // half of cylinder height
  675. /* now we have three possible regions the sphere can occupy:
  676. C :B: A :B: C
  677. : :_____: :
  678. : |_____| : cylinder
  679. : : : :
  680. : : : : */
  681. if (sphereAxisDistance >= tmp + sphere->shapeParams[0]) // case C: no col.
  682. break;
  683. TPE_Vec4 sphereAxisToRelative =
  684. TPE_vec3Minus(sphereRelativePos,sphereAxisPos);
  685. TPE_Unit sphereCylinderDistance = TPE_vec3Len(sphereAxisToRelative);
  686. tmp = sphereAxisDistance - tmp;
  687. if (tmp < 0) // case A: potential collision with cylinder body
  688. {
  689. TPE_Unit penetration = cylinder->shapeParams[0]
  690. - (sphereCylinderDistance - sphere->shapeParams[0]);
  691. if (penetration > 0)
  692. {
  693. TPE_vec3Normalize(&sphereAxisToRelative);
  694. *collisionPoint = TPE_vec3Plus(cylinder->position,
  695. TPE_vec3Plus(sphereAxisPos,TPE_vec3Times(
  696. sphereAxisToRelative,cylinder->shapeParams[0])));
  697. *collisionNormal = sphereAxisToRelative;
  698. if (sphere == body1)
  699. TPE_vec3MultiplyPlain(*collisionNormal,-1,collisionNormal);
  700. return penetration;
  701. }
  702. else
  703. break;
  704. }
  705. /* case B: here we have two subcases, one with the sphere center being
  706. within the cylinder radius (collision with the cylinder top/bottom),
  707. and the other case (collision with the cylinder top/bottom edge). */
  708. TPE_Vec4 cylinderPlaneMiddle = TPE_vec3Times(
  709. TPE_vec3Normalized(sphereAxisPos),
  710. cylinder->shapeParams[1] / 2);
  711. if (sphereCylinderDistance < cylinder->shapeParams[0]) // top/bottom cap
  712. {
  713. TPE_Unit penetration = cylinder->shapeParams[1] / 2 -
  714. (sphereAxisDistance - sphere->shapeParams[0]);
  715. if (penetration <= 0) // shouldn't normally happen, but rounding errors
  716. penetration = 1;
  717. *collisionNormal = TPE_vec3Normalized(sphereAxisPos);
  718. *collisionPoint =
  719. TPE_vec3Plus(
  720. cylinder->position,
  721. TPE_vec3Plus(sphereAxisToRelative,cylinderPlaneMiddle));
  722. if (body1 == sphere)
  723. TPE_vec3MultiplyPlain(*collisionNormal,-1,collisionNormal);
  724. return penetration;
  725. }
  726. else // potential edge collision
  727. {
  728. TPE_Vec4 edgePoint = TPE_vec3Plus(cylinderPlaneMiddle,
  729. TPE_vec3Times(TPE_vec3Normalized(sphereAxisToRelative),
  730. cylinder->shapeParams[0]));
  731. TPE_Unit penetration = sphere->shapeParams[0] -
  732. TPE_vec3Dist(edgePoint,sphereRelativePos);
  733. if (penetration > 0)
  734. {
  735. *collisionPoint = TPE_vec3Plus(cylinder->position,edgePoint);
  736. *collisionNormal =
  737. TPE_vec3Normalized(TPE_vec3Minus(sphereRelativePos,edgePoint));
  738. if (body1 == sphere)
  739. TPE_vec3MultiplyPlain(*collisionNormal,-1,collisionNormal);
  740. return penetration;
  741. }
  742. }
  743. break;
  744. }
  745. case TPE_COLLISION_TYPE(TPE_SHAPE_CUBOID,TPE_SHAPE_CUBOID):
  746. {
  747. TPE_Vec4 // min/max extent of the colliding area:
  748. collisionExtentMax =
  749. TPE_vec4(-TPE_INFINITY,-TPE_INFINITY,-TPE_INFINITY,0),
  750. collisionExtentMin =
  751. TPE_vec4(TPE_INFINITY,TPE_INFINITY,TPE_INFINITY,0);
  752. uint8_t collisionHappened = 0;
  753. TPE_Vec4 aX1, aY1, aZ1, // first cuboid axes
  754. aX2, aY2, aZ2; // second cuboid axes
  755. for (uint8_t i = 0; i < 2; ++i) // for each body
  756. {
  757. TPE_Vec4 q = TPE_bodyGetOrientation(body1);
  758. // construct the cuboid axes:
  759. aX1 = TPE_vec4(body1->shapeParams[0] / 2,0,0,0);
  760. TPE_rotatePoint(&aX1,q);
  761. aY1 = TPE_vec4(0,body1->shapeParams[1] / 2,0,0);
  762. TPE_rotatePoint(&aY1,q);
  763. aZ1 = TPE_vec4(0,0,body1->shapeParams[2] / 2,0);
  764. TPE_rotatePoint(&aZ1,q);
  765. q = TPE_bodyGetOrientation(body2);
  766. aX2 = TPE_vec4(body2->shapeParams[0] / 2,0,0,0);
  767. TPE_rotatePoint(&aX2,q);
  768. aY2 = TPE_vec4(0,body2->shapeParams[1] / 2,0,0);
  769. TPE_rotatePoint(&aY2,q);
  770. aZ2 = TPE_vec4(0,0,body2->shapeParams[2] / 2,0);
  771. TPE_rotatePoint(&aZ2,q);
  772. uint8_t edges[12] = // list of all cuboid edges as combinations of axes
  773. { // xyz xyz
  774. 0x3b, // +++ -++ |
  775. 0x3e, // +++ ++- | top
  776. 0x13, // -+- -++ |
  777. 0x16, // -+- ++- |
  778. 0x29, // +-+ --+ |
  779. 0x2c, // +-+ +-- | bottom
  780. 0x01, // --- --+ |
  781. 0x04, // --- +-- |
  782. 0x3d, // +++ +-+ |
  783. 0x19, // -++ --+ | sides
  784. 0x10, // -+- --- |
  785. 0x34 // ++- +-- |
  786. };
  787. for (uint8_t j = 0; j < 12; ++j) // for each edge
  788. {
  789. // we check the edge against all sides of the other cuboid
  790. TPE_Vec4 lineStart = body1->position;
  791. TPE_Vec4 lineEnd = body1->position;
  792. uint8_t edge = edges[j];
  793. #define offsetCenter(c,v,a) \
  794. v = (edge & c) ? TPE_vec3Plus(v,a) : TPE_vec3Minus(v,a);
  795. offsetCenter(0x04,lineStart,aX1)
  796. offsetCenter(0x02,lineStart,aY1)
  797. offsetCenter(0x01,lineStart,aZ1)
  798. offsetCenter(0x20,lineEnd,aX1)
  799. offsetCenter(0x10,lineEnd,aY1)
  800. offsetCenter(0x08,lineEnd,aZ1)
  801. #undef offsetCenter
  802. TPE_Unit t1 = 0, t2 = TPE_FRACTIONS_PER_UNIT;
  803. TPE_Vec4 edgeDir = TPE_vec3Minus(lineEnd,lineStart);
  804. for (uint8_t k = 0; k < 3; ++k) // for each axis (pair of sides)
  805. {
  806. TPE_Vec4 *sideOffset;
  807. if (k == 0)
  808. sideOffset = &aX2;
  809. else if (k == 1)
  810. sideOffset = &aY2;
  811. else
  812. sideOffset = &aZ2;
  813. _TPE_cutLineSegmentByPlanes(body2->position,*sideOffset,lineStart,
  814. edgeDir,&t1,&t2);
  815. if (t1 > t2)
  816. break; // no solution already, no point checking on
  817. }
  818. if (t2 > t1) // if part of edge exists between all side planes
  819. {
  820. // edge collided with the cuboid
  821. collisionHappened = 1;
  822. *collisionPoint = edgeDir;
  823. collisionPoint->x = (collisionPoint->x * t1) / TPE_FRACTIONS_PER_UNIT;
  824. collisionPoint->y = (collisionPoint->y * t1) / TPE_FRACTIONS_PER_UNIT;
  825. collisionPoint->z = (collisionPoint->z * t1) / TPE_FRACTIONS_PER_UNIT;
  826. *collisionPoint = TPE_vec3Plus(lineStart,*collisionPoint);
  827. if (collisionPoint->x > collisionExtentMax.x)
  828. collisionExtentMax.x = collisionPoint->x;
  829. if (collisionPoint->x < collisionExtentMin.x)
  830. collisionExtentMin.x = collisionPoint->x;
  831. if (collisionPoint->y > collisionExtentMax.y)
  832. collisionExtentMax.y = collisionPoint->y;
  833. if (collisionPoint->y < collisionExtentMin.y)
  834. collisionExtentMin.y = collisionPoint->y;
  835. if (collisionPoint->z > collisionExtentMax.z)
  836. collisionExtentMax.z = collisionPoint->z;
  837. if (collisionPoint->z < collisionExtentMin.z)
  838. collisionExtentMin.z = collisionPoint->z;
  839. *collisionPoint = edgeDir;
  840. collisionPoint->x = (collisionPoint->x * t2) / TPE_FRACTIONS_PER_UNIT;
  841. collisionPoint->y = (collisionPoint->y * t2) / TPE_FRACTIONS_PER_UNIT;
  842. collisionPoint->z = (collisionPoint->z * t2) / TPE_FRACTIONS_PER_UNIT;
  843. *collisionPoint = TPE_vec3Plus(lineStart,*collisionPoint);
  844. if (collisionPoint->x > collisionExtentMax.x)
  845. collisionExtentMax.x = collisionPoint->x;
  846. if (collisionPoint->x < collisionExtentMin.x)
  847. collisionExtentMin.x = collisionPoint->x;
  848. if (collisionPoint->y > collisionExtentMax.y)
  849. collisionExtentMax.y = collisionPoint->y;
  850. if (collisionPoint->y < collisionExtentMin.y)
  851. collisionExtentMin.y = collisionPoint->y;
  852. if (collisionPoint->z > collisionExtentMax.z)
  853. collisionExtentMax.z = collisionPoint->z;
  854. if (collisionPoint->z < collisionExtentMin.z)
  855. collisionExtentMin.z = collisionPoint->z;
  856. }
  857. } // for each edge
  858. if (i == 0)
  859. {
  860. // now swap the bodies and do it again:
  861. const TPE_Body *tmp = body1;
  862. body1 = body2;
  863. body2 = tmp;
  864. }
  865. } // for each body
  866. if (collisionHappened)
  867. {
  868. // average all collision points to get the center point
  869. *collisionPoint = TPE_vec3Plus(collisionExtentMin,collisionExtentMax);
  870. collisionPoint->x /= 2;
  871. collisionPoint->y /= 2;
  872. collisionPoint->z /= 2;
  873. collisionPoint->w = 0;
  874. /* We'll find the "closest" side to collision point, compute the
  875. penetration depth for both bodies (can't do just one) and return the
  876. bigger one. */
  877. TPE_Unit result = -TPE_INFINITY;
  878. for (int i = 0; i < 2; ++i) // for each body
  879. {
  880. TPE_Vec4 bestAxis = TPE_vec4(1,0,0,0);
  881. TPE_Unit bestDot = -1;
  882. TPE_Unit currentDot;
  883. collisionExtentMin = TPE_vec3Minus(*collisionPoint,
  884. i == 0 ? body1->position : body2->position); // reuse
  885. #define checkAxis(a) \
  886. currentDot = (TPE_vec3DotProduct(a,collisionExtentMin) * TPE_FRACTIONS_PER_UNIT) / \
  887. TPE_nonZero(TPE_vec3DotProduct(a,a)); \
  888. if (currentDot > bestDot) \
  889. { bestDot = currentDot; bestAxis = a; } \
  890. else { \
  891. currentDot *= -1; \
  892. if (currentDot > bestDot) { \
  893. bestDot = currentDot; bestAxis = a; \
  894. TPE_vec3MultiplyPlain(bestAxis,-1,&bestAxis); } \
  895. }
  896. checkAxis(aX1)
  897. checkAxis(aY1)
  898. checkAxis(aZ1)
  899. #undef checkAxis
  900. TPE_Unit len = TPE_nonZero(TPE_vec3Len(bestAxis));
  901. len = len - TPE_vec3DotProductPlain(bestAxis,
  902. TPE_vec3Minus(*collisionPoint,
  903. i == 0 ? body1->position : body2->position)) / len;
  904. if (len > result)
  905. {
  906. result = len;
  907. *collisionNormal = bestAxis;
  908. TPE_vec3Normalize(collisionNormal);
  909. if (i == 0)
  910. TPE_vec3MultiplyPlain(*collisionNormal,-1,collisionNormal);
  911. }
  912. aX1 = aX2; // check the second body's axes in next iteration
  913. aY1 = aY2;
  914. aZ1 = aZ2;
  915. }
  916. return result > 1 ? result : 1;
  917. }
  918. break;
  919. }
  920. default:
  921. break;
  922. }
  923. return 0;
  924. }
  925. TPE_Vec4 TPE_bodyGetPointVelocity(const TPE_Body *body, TPE_Vec4 point)
  926. {
  927. TPE_Vec4 result = body->velocity;
  928. TPE_Vec4 normal = TPE_vec3Cross(
  929. point,TPE_vec3Minus(point,body->rotation.axisVelocity));
  930. TPE_vec3MultiplyPlain(normal,-1,&normal); // TODO: think about WHY
  931. TPE_Unit dist = TPE_vec3Len(normal); // point-line distance
  932. TPE_Unit velocity =
  933. TPE_angularVelocityToLinear(body->rotation.axisVelocity.w,dist);
  934. TPE_vec3Normalize(&normal);
  935. return TPE_vec3Plus(result,TPE_vec3Times(normal,velocity));
  936. }
  937. void TPE_bodyMultiplyKineticEnergy(TPE_Body *body, TPE_Unit f)
  938. {
  939. if (body->mass == TPE_INFINITY)
  940. return;
  941. TPE_vec3Multiply(body->velocity,f,&(body->velocity));
  942. f = TPE_sqrt(f * TPE_FRACTIONS_PER_UNIT);
  943. TPE_vec3Multiply(body->velocity,f,&(body->velocity));
  944. int8_t sign =
  945. TPE_sign(body->rotation.axisVelocity.w);
  946. body->rotation.axisVelocity.w =
  947. (body->rotation.axisVelocity.w * f) / TPE_FRACTIONS_PER_UNIT;
  948. /* we try to prevent the angular welocity from falling to 0 as that causes
  949. issues with gravity (bodies balancing on corners) */
  950. if (f != 0 &&
  951. sign != 0 && body->rotation.axisVelocity.w == 0)
  952. body->rotation.axisVelocity.w = sign;
  953. }
  954. void TPE_correctEnergies(TPE_Body *body1, TPE_Body *body2,
  955. TPE_Unit previousEnergy, TPE_Unit restitution)
  956. {
  957. if (previousEnergy == 0)
  958. return;
  959. int8_t r = restitution > TPE_FRACTIONS_PER_UNIT ?
  960. 1 : (restitution < TPE_FRACTIONS_PER_UNIT ? -1 : 0);
  961. TPE_Unit newEnergy = TPE_bodyGetKineticEnergy(body1) +
  962. TPE_bodyGetKineticEnergy(body2);
  963. TPE_Unit f = (newEnergy * TPE_FRACTIONS_PER_UNIT) /
  964. previousEnergy;
  965. restitution = (f != 0) ?
  966. (restitution * TPE_FRACTIONS_PER_UNIT) / f :
  967. TPE_FRACTIONS_PER_UNIT;
  968. if (restitution > TPE_FRACTIONS_PER_UNIT + 2 || // TODO: magic const.
  969. restitution < TPE_FRACTIONS_PER_UNIT -2)
  970. {
  971. f = (previousEnergy * restitution) / TPE_FRACTIONS_PER_UNIT;
  972. if ((r < 0 && f < previousEnergy) ||
  973. (r == 0 && f == previousEnergy) ||
  974. (r > 0 && f > previousEnergy))
  975. {
  976. TPE_bodyMultiplyKineticEnergy(body1,restitution);
  977. TPE_bodyMultiplyKineticEnergy(body2,restitution);
  978. }
  979. }
  980. }
  981. uint8_t _TPE_bodyUpdateAntivibration(TPE_Body *body)
  982. {
  983. uint8_t tmp = body->antiVibration & 0x7f;
  984. if (body->antiVibration & 0x80)
  985. {
  986. tmp = (tmp < 127 - TPE_ANTI_VIBRATION_INCREMENT) ?
  987. tmp + TPE_ANTI_VIBRATION_INCREMENT : 127;
  988. body->antiVibration = (body->antiVibration & 0x80) |
  989. tmp;
  990. }
  991. return tmp <= TPE_ANTI_VIBRATION_MAX_FRAMES;
  992. }
  993. void TPE_resolveCollision(TPE_Body *body1 ,TPE_Body *body2,
  994. TPE_Vec4 collisionPoint, TPE_Vec4 collisionNormal, TPE_Unit collisionDepth,
  995. TPE_Unit energyMultiplier)
  996. {
  997. /*
  998. TODO:
  999. - false coll. detection?
  1000. - coll with static
  1001. - handle small values!!!
  1002. - handle big values
  1003. */
  1004. if (body2->mass == TPE_INFINITY) // handle static bodies
  1005. {
  1006. if (body1->mass == TPE_INFINITY)
  1007. return; // static-static collision: do nothing
  1008. // switch the bodies so that the static body is always the first one:
  1009. TPE_Body *tmp = body1;
  1010. body1 = body2;
  1011. body2 = tmp;
  1012. TPE_vec3MultiplyPlain(collisionNormal,-1,&collisionNormal);
  1013. }
  1014. TPE_Vec4 p1, p2;
  1015. p1 = TPE_vec3Minus(collisionPoint,body1->position);
  1016. p2 = TPE_vec3Minus(collisionPoint,body2->position);
  1017. // separate the bodies:
  1018. collisionPoint = collisionNormal; // reuse collisionPoint
  1019. if (body1->mass != TPE_INFINITY)
  1020. {
  1021. TPE_vec3Multiply(collisionPoint,collisionDepth / 2,&collisionPoint);
  1022. TPE_vec3Add(body2->position,collisionPoint,&body2->position);
  1023. TPE_vec3Substract(body1->position,collisionPoint,&body1->position);
  1024. }
  1025. else
  1026. {
  1027. TPE_vec3Multiply(collisionPoint,collisionDepth,&collisionPoint);
  1028. TPE_vec3Add(body2->position,collisionPoint,&body2->position);
  1029. }
  1030. {
  1031. TPE_Vec4 vel1, vel2;
  1032. vel1 = TPE_bodyGetPointVelocity(body1,p1);
  1033. vel2 = TPE_bodyGetPointVelocity(body2,p2);
  1034. if (TPE_vec3Len(TPE_vec3Minus(vel1,vel2)) >=
  1035. TPE_ANTI_VIBRATION_VELOCITY_BREAK)
  1036. {
  1037. body1->antiVibration = 0;
  1038. body2->antiVibration = 0;
  1039. }
  1040. if (TPE_vec3DotProduct(collisionNormal,vel1) <
  1041. TPE_vec3DotProduct(collisionNormal,vel2))
  1042. return; // invalid collision (bodies going away from each other)
  1043. }
  1044. /* We now want to find an impulse I such that if we apply I to body2 and -I
  1045. to body1, we conserve kinetic energy (or keep as much of it as defined by
  1046. energyMultiplier). The direction of I is always the direction of
  1047. collisionNormal, we are only looking for the size of the impulse. We don't
  1048. have to worry about conserving momentum, it is automatically conserved by us
  1049. applying the same (but opposite) impulse to both bodies. The equation is
  1050. constructed as:
  1051. e_out1 + e_out2 - energyMultiplier * (e_in1 + e_in2) = 0
  1052. Where e_in1 (e_in2) is the current kin. energy of body1 (body2) and e_out1
  1053. (e_out2) is the energy of body1 (body2) AFTER applying impulse I. The
  1054. unknown (x) in the equation is the size of the impulse. Expanding all this,
  1055. considering moment of ineartia of a sphere (for simplicity), we get a
  1056. quadratic equation with coefficients:
  1057. a = 1/(2 * m1) + 1/(2 * m2) +
  1058. q1/2 * dot(cross(normal,p1),cross(normal,p1)) +
  1059. q2/2 * dot(cross(normal,p2),cross(normal,p2))
  1060. b = dot(v2,normal) - dot(v1,normal) +
  1061. dot(r2,cross(normal,p2) -
  1062. dot(r1,cross(normal,p1)
  1063. c = m1/2 * dot(v1,v1) + w1 * dot(r1,r1) +
  1064. m2/2 * dot(v2,v2) + w2 * dot(r2,r2) - energyMultiplier * (e_in1 + e_in2)
  1065. where
  1066. qn = 5 / (2 * mn * dn)
  1067. wn = (mn * dn) / 5
  1068. dn = maximum extent of body n
  1069. The following code is solving this equation: */
  1070. TPE_Unit tmp = TPE_bodyGetMaxExtent(body1);
  1071. TPE_Unit w1 = ((((body1->mass * tmp) / TPE_FRACTIONS_PER_UNIT) * tmp) /
  1072. TPE_FRACTIONS_PER_UNIT) / 5;
  1073. TPE_Unit q1 = (TPE_FRACTIONS_PER_UNIT * TPE_FRACTIONS_PER_UNIT * 2) /
  1074. TPE_nonZero(w1);
  1075. TPE_Vec4 nxp1 = TPE_vec3Cross(collisionNormal,p1);
  1076. TPE_Vec4 rot1 =
  1077. TPE_vec3Times(body1->rotation.axisVelocity,body1->rotation.axisVelocity.w);
  1078. tmp = TPE_bodyGetMaxExtent(body2);
  1079. TPE_Unit w2 = ((((body2->mass * tmp) / TPE_FRACTIONS_PER_UNIT) * tmp) /
  1080. TPE_FRACTIONS_PER_UNIT) / 5;
  1081. TPE_Unit q2 = (TPE_FRACTIONS_PER_UNIT * TPE_FRACTIONS_PER_UNIT * 2) /
  1082. TPE_nonZero(w2);
  1083. TPE_Vec4 nxp2 = TPE_vec3Cross(collisionNormal,p2);
  1084. TPE_Vec4 rot2 =
  1085. TPE_vec3Times(body2->rotation.axisVelocity,body2->rotation.axisVelocity.w);
  1086. uint8_t dynamic = body1->mass != TPE_INFINITY;
  1087. // quadratic eq. coefficients:
  1088. TPE_Unit a =
  1089. ((dynamic * TPE_FRACTIONS_PER_UNIT * TPE_FRACTIONS_PER_UNIT) / body1->mass +
  1090. (TPE_FRACTIONS_PER_UNIT * TPE_FRACTIONS_PER_UNIT) / body2->mass) / 2 +
  1091. (dynamic * q1 * TPE_vec3DotProduct(nxp1,nxp1) + q2 * TPE_vec3DotProduct(nxp2,nxp2)) /
  1092. (2 * TPE_FRACTIONS_PER_UNIT);
  1093. TPE_Unit b =
  1094. TPE_vec3DotProduct(body2->velocity,collisionNormal) +
  1095. TPE_vec3DotProduct(rot2,nxp2) -
  1096. dynamic * (
  1097. TPE_vec3DotProduct(body1->velocity,collisionNormal) +
  1098. TPE_vec3DotProduct(rot1,nxp1));
  1099. TPE_Unit
  1100. e1 = dynamic * TPE_bodyGetKineticEnergy(body1),
  1101. e2 = TPE_bodyGetKineticEnergy(body2);
  1102. TPE_Unit c =
  1103. (
  1104. dynamic * body1->mass * TPE_vec3DotProduct(body1->velocity,body1->velocity) +
  1105. body2->mass * TPE_vec3DotProduct(body2->velocity,body2->velocity)
  1106. ) / (2 * TPE_FRACTIONS_PER_UNIT) +
  1107. (
  1108. dynamic * w1 * TPE_vec3DotProduct(rot1,rot1) +
  1109. w2 * TPE_vec3DotProduct(rot2,rot2)
  1110. ) / TPE_FRACTIONS_PER_UNIT
  1111. - (((e1 + e2) * energyMultiplier) / TPE_FRACTIONS_PER_UNIT);
  1112. c = TPE_sqrt(b * b - 4 * a * c); // discriminant
  1113. b *= -1;
  1114. a *= 2;
  1115. // solutions:
  1116. TPE_Unit x1, x2;
  1117. x1 = ((b - c) * TPE_FRACTIONS_PER_UNIT) / a;
  1118. x2 = ((b + c) * TPE_FRACTIONS_PER_UNIT) / a;
  1119. // here at least one solution (x1 or x2) should be 0 (or close)
  1120. if (TPE_abs(x1) < TPE_abs(x2))
  1121. x1 = x2; // we take the non-0 solution
  1122. collisionNormal = TPE_vec3Times(collisionNormal,x1);
  1123. if (_TPE_bodyUpdateAntivibration(body2))
  1124. TPE_bodyApplyImpulse(body2,p2,collisionNormal);
  1125. else
  1126. TPE_bodyMultiplyKineticEnergy(body2,0);
  1127. if (body1->mass != TPE_INFINITY)
  1128. {
  1129. if (_TPE_bodyUpdateAntivibration(body1))
  1130. {
  1131. TPE_vec3MultiplyPlain(collisionNormal,-1,&collisionNormal);
  1132. TPE_bodyApplyImpulse(body1,p1,collisionNormal);
  1133. }
  1134. else
  1135. TPE_bodyMultiplyKineticEnergy(body1,0);
  1136. }
  1137. // we try to correct possible numerical errors:
  1138. TPE_correctEnergies(body1,body2,e1 + e2,energyMultiplier);
  1139. }
  1140. TPE_Unit TPE_linearVelocityToAngular(TPE_Unit velocity, TPE_Unit distance)
  1141. {
  1142. TPE_Unit circumfence = (2 * TPE_PI * distance) / TPE_FRACTIONS_PER_UNIT;
  1143. return (velocity * TPE_FRACTIONS_PER_UNIT) / circumfence;
  1144. }
  1145. TPE_Unit TPE_angularVelocityToLinear(TPE_Unit velocity, TPE_Unit distance)
  1146. {
  1147. TPE_Unit circumfence = (2 * TPE_PI * distance) / TPE_FRACTIONS_PER_UNIT;
  1148. return (velocity * circumfence) / TPE_FRACTIONS_PER_UNIT;
  1149. }
  1150. void TPE_bodyStep(TPE_Body *body)
  1151. {
  1152. if (body->mass != TPE_INFINITY)
  1153. {
  1154. TPE_vec3Add(body->position,body->velocity,&(body->position));
  1155. body->rotation.currentAngle += body->rotation.axisVelocity.w;
  1156. }
  1157. if ((body->antiVibration & 0x7f) > 0)
  1158. {
  1159. body->antiVibration = (body->antiVibration & 0x80) |
  1160. ((body->antiVibration & 0x7f) - 1);
  1161. if (body->antiVibration == 0x80)
  1162. body->antiVibration = 0;
  1163. }
  1164. }
  1165. void TPE_bodySetRotation(TPE_Body *body, TPE_Vec4 axis, TPE_Unit velocity)
  1166. {
  1167. if (body->rotation.currentAngle != 0)
  1168. body->rotation.originalOrientation = TPE_bodyGetOrientation(body);
  1169. if (velocity < 0)
  1170. {
  1171. axis.x *= -1;
  1172. axis.y *= -1;
  1173. axis.z *= -1;
  1174. velocity *= -1;
  1175. }
  1176. TPE_vec3Normalize(&axis);
  1177. body->antiVibration = (body->antiVibration & 0x7f) |
  1178. ((TPE_vec3DotProductPlain(axis,body->rotation.axisVelocity) <= 0) << 7);
  1179. body->rotation.axisVelocity = axis;
  1180. body->rotation.axisVelocity.w = velocity;
  1181. body->rotation.currentAngle = 0;
  1182. }
  1183. void TPE_bodyAddRotation(TPE_Body *body, TPE_Vec4 axis, TPE_Unit velocity)
  1184. {
  1185. /* Rotation is added like this: we convert both the original and added
  1186. rotation to vectors whose direction is along the rotations axis and
  1187. magnitude is the rotation speed, then we add these vectors and convert
  1188. the final vector back to normalized rotation axis + scalar rotation
  1189. speed.*/
  1190. if (velocity == 0)
  1191. return;
  1192. body->rotation.axisVelocity.x =
  1193. (body->rotation.axisVelocity.x * body->rotation.axisVelocity.w)
  1194. / TPE_FRACTIONS_PER_UNIT;
  1195. body->rotation.axisVelocity.y =
  1196. (body->rotation.axisVelocity.y * body->rotation.axisVelocity.w)
  1197. / TPE_FRACTIONS_PER_UNIT;
  1198. body->rotation.axisVelocity.z =
  1199. (body->rotation.axisVelocity.z * body->rotation.axisVelocity.w)
  1200. / TPE_FRACTIONS_PER_UNIT;
  1201. TPE_vec3Normalize(&axis);
  1202. axis.x = (axis.x * velocity) / TPE_FRACTIONS_PER_UNIT;
  1203. axis.y = (axis.y * velocity) / TPE_FRACTIONS_PER_UNIT;
  1204. axis.z = (axis.z * velocity) / TPE_FRACTIONS_PER_UNIT;
  1205. TPE_vec3Add(body->rotation.axisVelocity,axis,&axis);
  1206. axis.w = TPE_vec3Len(axis);
  1207. TPE_bodySetRotation(body,axis,axis.w);
  1208. }
  1209. void TPE_quaternionMultiply(TPE_Vec4 a, TPE_Vec4 b, TPE_Vec4 *result)
  1210. {
  1211. TPE_Vec4 r; // in case result is identical to a or b
  1212. r.x =
  1213. (a.w * b.x +
  1214. a.x * b.w +
  1215. a.y * b.z -
  1216. a.z * b.y) / TPE_FRACTIONS_PER_UNIT;
  1217. r.y =
  1218. (a.w * b.y -
  1219. a.x * b.z +
  1220. a.y * b.w +
  1221. a.z * b.x) / TPE_FRACTIONS_PER_UNIT;
  1222. r.z =
  1223. (a.w * b.z +
  1224. a.x * b.y -
  1225. a.y * b.x +
  1226. a.z * b.w) / TPE_FRACTIONS_PER_UNIT;
  1227. r.w =
  1228. (a.w * b.w -
  1229. a.x * b.x -
  1230. a.y * b.y -
  1231. a.z * b.z) / TPE_FRACTIONS_PER_UNIT;
  1232. result->x = r.x;
  1233. result->y = r.y;
  1234. result->z = r.z;
  1235. result->w = r.w;
  1236. }
  1237. void TPE_rotationToQuaternion(TPE_Vec4 axis, TPE_Unit angle, TPE_Vec4 *quaternion)
  1238. {
  1239. TPE_vec3Normalize(&axis);
  1240. angle /= 2;
  1241. TPE_Unit s = TPE_sin(angle);
  1242. quaternion->x = (s * axis.x) / TPE_FRACTIONS_PER_UNIT;
  1243. quaternion->y = (s * axis.y) / TPE_FRACTIONS_PER_UNIT;
  1244. quaternion->z = (s * axis.z) / TPE_FRACTIONS_PER_UNIT;
  1245. quaternion->w = TPE_cos(angle);
  1246. }
  1247. TPE_Unit TPE_asin(TPE_Unit x)
  1248. {
  1249. x = TPE_clamp(x,-TPE_FRACTIONS_PER_UNIT,TPE_FRACTIONS_PER_UNIT);
  1250. int8_t sign = 1;
  1251. if (x < 0)
  1252. {
  1253. sign = -1;
  1254. x *= -1;
  1255. }
  1256. int16_t low = 0;
  1257. int16_t high = TPE_SIN_TABLE_LENGTH -1;
  1258. int16_t middle;
  1259. while (low <= high) // binary search
  1260. {
  1261. middle = (low + high) / 2;
  1262. TPE_Unit v = TPE_sinTable[middle];
  1263. if (v > x)
  1264. high = middle - 1;
  1265. else if (v < x)
  1266. low = middle + 1;
  1267. else
  1268. break;
  1269. }
  1270. middle *= TPE_SIN_TABLE_UNIT_STEP;
  1271. return sign * middle;
  1272. }
  1273. TPE_Unit TPE_acos(TPE_Unit x)
  1274. {
  1275. return TPE_asin(-1 * x) + TPE_FRACTIONS_PER_UNIT / 4;
  1276. }
  1277. void TPE_quaternionToRotation(TPE_Vec4 quaternion, TPE_Vec4 *axis, TPE_Unit *angle)
  1278. {
  1279. *angle = 2 * TPE_acos(quaternion.x);
  1280. TPE_Unit tmp =
  1281. TPE_nonZero(TPE_sqrt(
  1282. (TPE_FRACTIONS_PER_UNIT -
  1283. (quaternion.x * quaternion.x) / TPE_FRACTIONS_PER_UNIT
  1284. ) * TPE_FRACTIONS_PER_UNIT));
  1285. axis->x = (quaternion.x * TPE_FRACTIONS_PER_UNIT) / tmp;
  1286. axis->y = (quaternion.y * TPE_FRACTIONS_PER_UNIT) / tmp;
  1287. axis->z = (quaternion.z * TPE_FRACTIONS_PER_UNIT) / tmp;
  1288. }
  1289. void TPE_quaternionToRotationMatrix(TPE_Vec4 quaternion, TPE_Unit matrix[4][4])
  1290. {
  1291. TPE_Unit
  1292. _2x2 = (2 * quaternion.x * quaternion.x) / TPE_FRACTIONS_PER_UNIT,
  1293. _2y2 = (2 * quaternion.y * quaternion.y) / TPE_FRACTIONS_PER_UNIT,
  1294. _2z2 = (2 * quaternion.z * quaternion.z) / TPE_FRACTIONS_PER_UNIT,
  1295. _2xy = (2 * quaternion.x * quaternion.y) / TPE_FRACTIONS_PER_UNIT,
  1296. _2xw = (2 * quaternion.x * quaternion.w) / TPE_FRACTIONS_PER_UNIT,
  1297. _2zw = (2 * quaternion.z * quaternion.w) / TPE_FRACTIONS_PER_UNIT,
  1298. _2xz = (2 * quaternion.x * quaternion.z) / TPE_FRACTIONS_PER_UNIT,
  1299. _2yw = (2 * quaternion.y * quaternion.w) / TPE_FRACTIONS_PER_UNIT,
  1300. _2yz = (2 * quaternion.y * quaternion.z) / TPE_FRACTIONS_PER_UNIT;
  1301. #define ONE TPE_FRACTIONS_PER_UNIT
  1302. matrix[0][0] = ONE - _2y2 - _2z2;
  1303. matrix[1][0] = _2xy - _2zw;
  1304. matrix[2][0] = _2xz + _2yw;
  1305. matrix[3][0] = 0;
  1306. matrix[0][1] = _2xy + _2zw;
  1307. matrix[1][1] = ONE - _2x2 - _2z2;
  1308. matrix[2][1] = _2yz - _2xw;
  1309. matrix[3][1] = 0;
  1310. matrix[0][2] = _2xz - _2yw;
  1311. matrix[1][2] = _2yz + _2xw;
  1312. matrix[2][2] = ONE - _2x2 - _2y2;
  1313. matrix[3][2] = 0;
  1314. matrix[0][3] = 0;
  1315. matrix[1][3] = 0;
  1316. matrix[2][3] = 0;
  1317. matrix[3][3] = ONE;
  1318. #undef ONE
  1319. }
  1320. void TPE_vec3Add(const TPE_Vec4 a, const TPE_Vec4 b, TPE_Vec4 *result)
  1321. {
  1322. result->x = a.x + b.x;
  1323. result->y = a.y + b.y;
  1324. result->z = a.z + b.z;
  1325. }
  1326. void TPE_vec4Add(const TPE_Vec4 a, const TPE_Vec4 b, TPE_Vec4 *result)
  1327. {
  1328. result->x = a.x + b.x;
  1329. result->y = a.y + b.y;
  1330. result->z = a.z + b.z;
  1331. result->w = a.w + b.w;
  1332. }
  1333. void TPE_vec3Substract(const TPE_Vec4 a, const TPE_Vec4 b, TPE_Vec4 *result)
  1334. {
  1335. result->x = a.x - b.x;
  1336. result->y = a.y - b.y;
  1337. result->z = a.z - b.z;
  1338. }
  1339. TPE_Vec4 TPE_vec3Plus(TPE_Vec4 a, TPE_Vec4 b)
  1340. {
  1341. a.x += b.x;
  1342. a.y += b.y;
  1343. a.z += b.z;
  1344. return a;
  1345. }
  1346. TPE_Vec4 TPE_vec3Minus(TPE_Vec4 a, TPE_Vec4 b)
  1347. {
  1348. a.x -= b.x;
  1349. a.y -= b.y;
  1350. a.z -= b.z;
  1351. return a;
  1352. }
  1353. TPE_Vec4 TPE_vec3Times(TPE_Vec4 a, TPE_Unit f)
  1354. {
  1355. a.x = (a.x * f) / TPE_FRACTIONS_PER_UNIT;
  1356. a.y = (a.y * f) / TPE_FRACTIONS_PER_UNIT;
  1357. a.z = (a.z * f) / TPE_FRACTIONS_PER_UNIT;
  1358. return a;
  1359. }
  1360. TPE_Vec4 TPE_vec3TimesAntiZero(TPE_Vec4 a, TPE_Unit f)
  1361. {
  1362. a.x *= f;
  1363. if (a.x != 0)
  1364. a.x = a.x >= TPE_FRACTIONS_PER_UNIT ? a.x / TPE_FRACTIONS_PER_UNIT :
  1365. (a.x > 0 ? 1 : -1);
  1366. a.y *= f;
  1367. if (a.y != 0)
  1368. a.y = a.y >= TPE_FRACTIONS_PER_UNIT ? a.y / TPE_FRACTIONS_PER_UNIT :
  1369. (a.y > 0 ? 1 : -1);
  1370. a.z *= f;
  1371. if (a.z != 0)
  1372. a.z = a.z >= TPE_FRACTIONS_PER_UNIT ? a.z / TPE_FRACTIONS_PER_UNIT :
  1373. (a.z > 0 ? 1 : -1);
  1374. /*
  1375. if (a.x != 0)
  1376. a.x = a.x >= TPE_FRACTIONS_PER_UNIT ? a.x / TPE_FRACTIONS_PER_UNIT :
  1377. (a.x > 0 ? 1 : -1);
  1378. if (a.y != 0)
  1379. a.y = a.y >= TPE_FRACTIONS_PER_UNIT ? a.y / TPE_FRACTIONS_PER_UNIT :
  1380. (a.y > 0 ? 1 : -1);
  1381. if (a.z != 0)
  1382. a.z = a.z >= TPE_FRACTIONS_PER_UNIT ? a.z / TPE_FRACTIONS_PER_UNIT :
  1383. (a.z > 0 ? 1 : -1);
  1384. */
  1385. return a;
  1386. }
  1387. void TPE_vec3Average(TPE_Vec4 a, TPE_Vec4 b, TPE_Vec4 *result)
  1388. {
  1389. result->x = (a.x + b.x) / 2;
  1390. result->y = (a.y + b.y) / 2;
  1391. result->z = (a.z + b.z) / 2;
  1392. }
  1393. void TPE_vec4Substract(const TPE_Vec4 a, const TPE_Vec4 b, TPE_Vec4 *result)
  1394. {
  1395. result->x = a.x - b.x;
  1396. result->y = a.y - b.y;
  1397. result->z = a.z - b.z;
  1398. result->w = a.w - b.w;
  1399. }
  1400. void TPE_vec3Multiply(const TPE_Vec4 v, TPE_Unit f, TPE_Vec4 *result)
  1401. {
  1402. result->x = (v.x * f) / TPE_FRACTIONS_PER_UNIT;
  1403. result->y = (v.y * f) / TPE_FRACTIONS_PER_UNIT;
  1404. result->z = (v.z * f) / TPE_FRACTIONS_PER_UNIT;
  1405. }
  1406. void TPE_vec3MultiplyPlain(TPE_Vec4 v, TPE_Unit f, TPE_Vec4 *result)
  1407. {
  1408. result->x = v.x * f;
  1409. result->y = v.y * f;
  1410. result->z = v.z * f;
  1411. }
  1412. void TPE_vec4Multiply(const TPE_Vec4 v, TPE_Unit f, TPE_Vec4 *result)
  1413. {
  1414. result->x = (v.x * f) / TPE_FRACTIONS_PER_UNIT;
  1415. result->y = (v.y * f) / TPE_FRACTIONS_PER_UNIT;
  1416. result->z = (v.z * f) / TPE_FRACTIONS_PER_UNIT;
  1417. result->w = (v.w * f) / TPE_FRACTIONS_PER_UNIT;
  1418. }
  1419. TPE_Unit TPE_abs(TPE_Unit x)
  1420. {
  1421. return (x >= 0) ? x : (-1 * x);
  1422. }
  1423. TPE_Unit TPE_vec3Len(TPE_Vec4 v)
  1424. {
  1425. return TPE_sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
  1426. }
  1427. TPE_Unit TPE_vec3Dist(TPE_Vec4 a, TPE_Vec4 b)
  1428. {
  1429. return TPE_vec3Len(TPE_vec3Minus(a,b));
  1430. }
  1431. TPE_Unit TPE_vec4Len(TPE_Vec4 v)
  1432. {
  1433. return TPE_sqrt(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w);
  1434. }
  1435. TPE_Unit TPE_vec3LenTaxicab(TPE_Vec4 v)
  1436. {
  1437. return TPE_abs(v.x) + TPE_abs(v.y) + TPE_abs(v.z);
  1438. }
  1439. TPE_Unit TPE_vec3DotProduct(const TPE_Vec4 v1, const TPE_Vec4 v2)
  1440. {
  1441. return
  1442. (v1.x * v2.x + v1.y * v2.y + v1.z * v2.z) / TPE_FRACTIONS_PER_UNIT;
  1443. }
  1444. TPE_Unit TPE_vec3DotProductPlain(const TPE_Vec4 v1, const TPE_Vec4 v2)
  1445. {
  1446. return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
  1447. }
  1448. void TPE_vec3Normalize(TPE_Vec4 *v)
  1449. {
  1450. TPE_Unit l = TPE_vec3Len(*v);
  1451. if (l == 0)
  1452. {
  1453. v->x = TPE_FRACTIONS_PER_UNIT;
  1454. return;
  1455. }
  1456. v->x = (v->x * TPE_FRACTIONS_PER_UNIT) / l;
  1457. v->y = (v->y * TPE_FRACTIONS_PER_UNIT) / l;
  1458. v->z = (v->z * TPE_FRACTIONS_PER_UNIT) / l;
  1459. }
  1460. void TPE_vec4Normalize(TPE_Vec4 *v)
  1461. {
  1462. TPE_Unit l = TPE_vec4Len(*v);
  1463. if (l == 0)
  1464. {
  1465. v->x = TPE_FRACTIONS_PER_UNIT;
  1466. return;
  1467. }
  1468. v->x = (v->x * TPE_FRACTIONS_PER_UNIT) / l;
  1469. v->y = (v->y * TPE_FRACTIONS_PER_UNIT) / l;
  1470. v->z = (v->z * TPE_FRACTIONS_PER_UNIT) / l;
  1471. v->w = (v->w * TPE_FRACTIONS_PER_UNIT) / l;
  1472. }
  1473. void TPE_vec3Project(TPE_Vec4 v, TPE_Vec4 base, TPE_Vec4 *result)
  1474. {
  1475. TPE_Unit p = TPE_vec3DotProduct(v,base);
  1476. result->x = (p * base.x) / TPE_FRACTIONS_PER_UNIT;
  1477. result->y = (p * base.y) / TPE_FRACTIONS_PER_UNIT;
  1478. result->z = (p * base.z) / TPE_FRACTIONS_PER_UNIT;
  1479. }
  1480. TPE_Vec4 TPE_vec3Projected(TPE_Vec4 v, TPE_Vec4 base)
  1481. {
  1482. TPE_Vec4 r;
  1483. TPE_vec3Project(v,base,&r);
  1484. return r;
  1485. }
  1486. void TPE_getVelocitiesAfterCollision(
  1487. TPE_Unit *v1,
  1488. TPE_Unit *v2,
  1489. TPE_Unit m1,
  1490. TPE_Unit m2,
  1491. TPE_Unit elasticity
  1492. )
  1493. {
  1494. /* in the following a lot of TPE_FRACTIONS_PER_UNIT cancel out, feel free to
  1495. check if confused */
  1496. #define ANTI_OVERFLOW 30000
  1497. #define ANTI_OVERFLOW_SCALE 128
  1498. uint8_t overflowDanger = m1 > ANTI_OVERFLOW || *v1 > ANTI_OVERFLOW ||
  1499. m2 > ANTI_OVERFLOW || *v2 > ANTI_OVERFLOW;
  1500. if (overflowDanger)
  1501. {
  1502. m1 = (m1 != 0) ? TPE_nonZero(m1 / ANTI_OVERFLOW_SCALE) : 0;
  1503. m2 = (m2 != 0) ? TPE_nonZero(m2 / ANTI_OVERFLOW_SCALE) : 0;
  1504. *v1 = (*v1 != 0) ? TPE_nonZero(*v1 / ANTI_OVERFLOW_SCALE) : 0;
  1505. *v2 = (*v2 != 0) ? TPE_nonZero(*v2 / ANTI_OVERFLOW_SCALE) : 0;
  1506. }
  1507. TPE_Unit m1Pm2 = TPE_nonZero(m1 + m2);
  1508. TPE_Unit v2Mv1 = TPE_nonZero(*v2 - *v1);
  1509. TPE_Unit m1v1Pm2v2 = ((m1 * *v1) + (m2 * *v2));
  1510. *v1 = (((elasticity * m2 / TPE_FRACTIONS_PER_UNIT) * v2Mv1)
  1511. + m1v1Pm2v2) / m1Pm2;
  1512. *v2 = (((elasticity * m1 / TPE_FRACTIONS_PER_UNIT) * -1 * v2Mv1)
  1513. + m1v1Pm2v2) / m1Pm2;
  1514. if (overflowDanger)
  1515. {
  1516. *v1 *= ANTI_OVERFLOW_SCALE;
  1517. *v2 *= ANTI_OVERFLOW_SCALE;
  1518. }
  1519. #undef ANTI_OVERFLOW
  1520. #undef ANTI_OVERFLOW_SCALE
  1521. }
  1522. void TPE_bodyGetTransformMatrix(const TPE_Body *body, TPE_Unit matrix[4][4])
  1523. {
  1524. TPE_Vec4 orientation;
  1525. orientation = TPE_bodyGetOrientation(body);
  1526. TPE_quaternionToRotationMatrix(orientation,matrix);
  1527. matrix[0][3] = body->position.x;
  1528. matrix[1][3] = body->position.y;
  1529. matrix[2][3] = body->position.z;
  1530. }
  1531. void TPE_quaternionInit(TPE_Vec4 *quaternion)
  1532. {
  1533. quaternion->x = 0;
  1534. quaternion->y = 0;
  1535. quaternion->z = 0;
  1536. quaternion->w = TPE_FRACTIONS_PER_UNIT;
  1537. }
  1538. void TPE_rotatePoint(TPE_Vec4 *point, TPE_Vec4 quaternion)
  1539. {
  1540. // TODO: the first method is bugged, but maybe would be faster?
  1541. #if 0
  1542. TPE_Vec4 quaternionConjugate = TPE_quaternionConjugate(quaternion);
  1543. point->w = 0;
  1544. TPE_quaternionMultiply(quaternion,*point,point);
  1545. TPE_quaternionMultiply(*point,quaternionConjugate,point);
  1546. #else
  1547. TPE_Unit m[4][4];
  1548. TPE_quaternionToRotationMatrix(quaternion,m);
  1549. TPE_Vec4 p = *point;
  1550. point->x = (p.x * m[0][0] + p.y * m[0][1] + p.z * m[0][2]) / TPE_FRACTIONS_PER_UNIT;
  1551. point->y = (p.x * m[1][0] + p.y * m[1][1] + p.z * m[1][2]) / TPE_FRACTIONS_PER_UNIT;
  1552. point->z = (p.x * m[2][0] + p.y * m[2][1] + p.z * m[2][2]) / TPE_FRACTIONS_PER_UNIT;
  1553. #endif
  1554. }
  1555. TPE_Vec4 TPE_quaternionConjugate(TPE_Vec4 quaternion)
  1556. {
  1557. quaternion.x *= -1;
  1558. quaternion.y *= -1;
  1559. quaternion.z *= -1;
  1560. return quaternion;
  1561. }
  1562. TPE_Vec4 TPE_vec3Normalized(TPE_Vec4 v)
  1563. {
  1564. TPE_vec3Normalize(&v);
  1565. return v;
  1566. }
  1567. TPE_Vec4 TPE_lineSegmentClosestPoint(TPE_Vec4 a, TPE_Vec4 b, TPE_Vec4 p)
  1568. {
  1569. TPE_Vec4 ab = TPE_vec3Minus(b,a);
  1570. TPE_Unit t = ((TPE_vec3DotProduct(ab,TPE_vec3Minus(p,a)) *
  1571. TPE_FRACTIONS_PER_UNIT) / TPE_nonZero(TPE_vec3DotProduct(ab,ab)));
  1572. if (t < 0)
  1573. t = 0;
  1574. else if (t > TPE_FRACTIONS_PER_UNIT)
  1575. t = TPE_FRACTIONS_PER_UNIT;
  1576. TPE_vec3Multiply(ab,t,&ab);
  1577. return TPE_vec3Plus(a,ab);
  1578. }
  1579. TPE_Unit TPE_bodyGetKineticEnergy(const TPE_Body *body)
  1580. {
  1581. TPE_Unit v = TPE_vec3Len(body->velocity);
  1582. v *= v;
  1583. v = (v == 0 || v >= TPE_FRACTIONS_PER_UNIT) ?
  1584. v / TPE_FRACTIONS_PER_UNIT : 1;
  1585. v = (body->mass * v) / (2 * TPE_FRACTIONS_PER_UNIT);
  1586. // TODO: handle small values
  1587. // TODO: clean this mess :)
  1588. TPE_Unit r = TPE_bodyGetMaxExtent(body);
  1589. r =
  1590. (
  1591. TPE_timesAntiZero(
  1592. TPE_timesAntiZero(r,r),
  1593. TPE_timesAntiZero(body->rotation.axisVelocity.w,body->rotation.axisVelocity.w)
  1594. )
  1595. *
  1596. body->mass
  1597. )
  1598. / (5 * TPE_FRACTIONS_PER_UNIT);
  1599. if (r == 0 && body->rotation.axisVelocity.w != 0)
  1600. r = 1;
  1601. return v + r;
  1602. }
  1603. TPE_Unit TPE_bodyGetMaxExtent(const TPE_Body *body)
  1604. {
  1605. switch (body->shape)
  1606. {
  1607. case TPE_SHAPE_SPHERE:
  1608. return body->shapeParams[0];
  1609. break;
  1610. case TPE_SHAPE_CUBOID:
  1611. return TPE_vec3Len(TPE_vec4(
  1612. body->shapeParams[0] / 2,
  1613. body->shapeParams[1] / 2,
  1614. body->shapeParams[2] / 2,0));
  1615. break;
  1616. // TODO: other shapes
  1617. default: return 0; break;
  1618. }
  1619. }
  1620. void TPE_bodyRecomputeBounds(TPE_Body *body)
  1621. {
  1622. body->boundingSphereRadius = TPE_bodyGetMaxExtent(body);
  1623. }
  1624. TPE_Unit TPE_timesAntiZero(TPE_Unit a, TPE_Unit b)
  1625. {
  1626. TPE_Unit result = a * b;
  1627. return result >= TPE_FRACTIONS_PER_UNIT ?
  1628. result / TPE_FRACTIONS_PER_UNIT : (result != 0 ? 1 : 0);
  1629. }
  1630. int8_t TPE_sign(TPE_Unit x)
  1631. {
  1632. return x > 0 ? 1 : (x < 0 ? -1 : 0);
  1633. }
  1634. #endif // guard