r3d_kinematics.odin 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /* r3d_kinematics.odin -- R3D Kinematics Module.
  2. *
  3. * Copyright (c) 2025-2026 Le Juez Victor
  4. *
  5. * This software is provided 'as-is', without any express or implied warranty.
  6. * For conditions of distribution and use, see accompanying LICENSE file.
  7. */
  8. package r3d
  9. import rl "vendor:raylib"
  10. when ODIN_OS == .Windows {
  11. foreign import lib {
  12. "windows/libr3d.a",
  13. "system:raylib",
  14. "system:assimp",
  15. }
  16. } else when ODIN_OS == .Linux {
  17. foreign import lib {
  18. "linux/libr3d.a",
  19. "system:raylib",
  20. "system:assimp",
  21. }
  22. } else when ODIN_OS == .Darwin {
  23. foreign import lib {
  24. "darwin/libr3d.a",
  25. "system:raylib",
  26. "system:assimp",
  27. }
  28. }
  29. /**
  30. * @brief Capsule shape defined by two endpoints and radius
  31. */
  32. Capsule :: struct {
  33. start: rl.Vector3, ///< Start point of capsule axis
  34. end: rl.Vector3, ///< End point of capsule axis
  35. radius: f32, ///< Capsule radius
  36. }
  37. /**
  38. * @brief Penetration information from an overlap test
  39. */
  40. Penetration :: struct {
  41. collides: bool, ///< Whether shapes are overlapping
  42. depth: f32, ///< Penetration depth
  43. normal: rl.Vector3, ///< Collision normal (direction to resolve penetration)
  44. mtv: rl.Vector3, ///< Minimum Translation Vector (normal * depth)
  45. }
  46. /**
  47. * @brief Collision information from a sweep test
  48. */
  49. SweepCollision :: struct {
  50. hit: bool, ///< Whether a collision occurred
  51. time: f32, ///< Time of impact [0-1], fraction along velocity vector
  52. point: rl.Vector3, ///< World space collision point
  53. normal: rl.Vector3, ///< Surface normal at collision point
  54. }
  55. @(default_calling_convention="c", link_prefix="R3D_")
  56. foreign lib {
  57. /**
  58. * @brief Check if capsule intersects with box
  59. * @param capsule Capsule shape
  60. * @param box Bounding box
  61. * @return true if collision detected
  62. */
  63. CheckCollisionCapsuleBox :: proc(capsule: Capsule, box: rl.BoundingBox) -> bool ---
  64. /**
  65. * @brief Check if capsule intersects with sphere
  66. * @param capsule Capsule shape
  67. * @param center Sphere center
  68. * @param radius Sphere radius
  69. * @return true if collision detected
  70. */
  71. CheckCollisionCapsuleSphere :: proc(capsule: Capsule, center: rl.Vector3, radius: f32) -> bool ---
  72. /**
  73. * @brief Check if two capsules intersect
  74. * @param a First capsule
  75. * @param b Second capsule
  76. * @return true if collision detected
  77. */
  78. CheckCollisionCapsules :: proc(a: Capsule, b: Capsule) -> bool ---
  79. /**
  80. * @brief Check if capsule intersects with mesh
  81. * @param capsule Capsule shape
  82. * @param mesh Mesh data
  83. * @param transform Mesh transform
  84. * @return true if collision detected
  85. */
  86. CheckCollisionCapsuleMesh :: proc(capsule: Capsule, mesh: MeshData, transform: rl.Matrix) -> bool ---
  87. /**
  88. * @brief Check penetration between capsule and box
  89. * @param capsule Capsule shape
  90. * @param box Bounding box
  91. * @return Penetration information.
  92. */
  93. CheckPenetrationCapsuleBox :: proc(capsule: Capsule, box: rl.BoundingBox) -> Penetration ---
  94. /**
  95. * @brief Check penetration between capsule and sphere
  96. * @param capsule Capsule shape
  97. * @param center Sphere center
  98. * @param radius Sphere radius
  99. * @return Penetration information.
  100. */
  101. CheckPenetrationCapsuleSphere :: proc(capsule: Capsule, center: rl.Vector3, radius: f32) -> Penetration ---
  102. /**
  103. * @brief Check penetration between two capsules
  104. * @param a First capsule
  105. * @param b Second capsule
  106. * @return Penetration information.
  107. */
  108. CheckPenetrationCapsules :: proc(a: Capsule, b: Capsule) -> Penetration ---
  109. /**
  110. * @brief Calculate slide velocity along surface
  111. * @param velocity Original velocity
  112. * @param normal Surface normal (must be normalized)
  113. * @return Velocity sliding along surface (perpendicular component removed)
  114. */
  115. SlideVelocity :: proc(velocity: rl.Vector3, normal: rl.Vector3) -> rl.Vector3 ---
  116. /**
  117. * @brief Calculate bounce velocity after collision
  118. * @param velocity Incoming velocity
  119. * @param normal Surface normal (must be normalized)
  120. * @param bounciness Coefficient of restitution (0=no bounce, 1=perfect bounce)
  121. * @return Reflected velocity
  122. */
  123. BounceVelocity :: proc(velocity: rl.Vector3, normal: rl.Vector3, bounciness: f32) -> rl.Vector3 ---
  124. /**
  125. * @brief Slide sphere along box surface, resolving collisions
  126. * @param center Sphere center position
  127. * @param radius Sphere radius
  128. * @param velocity Desired movement vector
  129. * @param box Obstacle bounding box
  130. * @param outNormal Optional: receives collision normal if collision occurred
  131. * @return Actual movement applied (may be reduced/redirected by collision)
  132. */
  133. SlideSphereBox :: proc(center: rl.Vector3, radius: f32, velocity: rl.Vector3, box: rl.BoundingBox, outNormal: ^rl.Vector3) -> rl.Vector3 ---
  134. /**
  135. * @brief Slide sphere along mesh surface, resolving collisions
  136. * @param center Sphere center position
  137. * @param radius Sphere radius
  138. * @param velocity Desired movement vector
  139. * @param mesh Mesh data to collide against
  140. * @param transform Mesh world transform
  141. * @param outNormal Optional: receives collision normal if collision occurred
  142. * @return Actual movement applied (may be reduced/redirected by collision)
  143. */
  144. SlideSphereMesh :: proc(center: rl.Vector3, radius: f32, velocity: rl.Vector3, mesh: MeshData, transform: rl.Matrix, outNormal: ^rl.Vector3) -> rl.Vector3 ---
  145. /**
  146. * @brief Slide capsule along box surface, resolving collisions
  147. * @param capsule Capsule shape
  148. * @param velocity Desired movement vector
  149. * @param box Obstacle bounding box
  150. * @param outNormal Optional: receives collision normal if collision occurred
  151. * @return Actual movement applied (may be reduced/redirected by collision)
  152. */
  153. SlideCapsuleBox :: proc(capsule: Capsule, velocity: rl.Vector3, box: rl.BoundingBox, outNormal: ^rl.Vector3) -> rl.Vector3 ---
  154. /**
  155. * @brief Slide capsule along mesh surface, resolving collisions
  156. * @param capsule Capsule shape
  157. * @param velocity Desired movement vector
  158. * @param mesh Mesh data to collide against
  159. * @param transform Mesh world transform
  160. * @param outNormal Optional: receives collision normal if collision occurred
  161. * @return Actual movement applied (may be reduced/redirected by collision)
  162. */
  163. SlideCapsuleMesh :: proc(capsule: Capsule, velocity: rl.Vector3, mesh: MeshData, transform: rl.Matrix, outNormal: ^rl.Vector3) -> rl.Vector3 ---
  164. /**
  165. * @brief Push sphere out of box if penetrating
  166. * @param center Sphere center (modified in place if penetrating)
  167. * @param radius Sphere radius
  168. * @param box Obstacle box
  169. * @param outPenetration Optional: receives penetration depth
  170. * @return true if depenetration occurred
  171. */
  172. DepenetrateSphereBox :: proc(center: ^rl.Vector3, radius: f32, box: rl.BoundingBox, outPenetration: ^f32) -> bool ---
  173. /**
  174. * @brief Push capsule out of box if penetrating
  175. * @param capsule Capsule shape (modified in place if penetrating)
  176. * @param box Obstacle box
  177. * @param outPenetration Optional: receives penetration depth
  178. * @return true if depenetration occurred
  179. */
  180. DepenetrateCapsuleBox :: proc(capsule: ^Capsule, box: rl.BoundingBox, outPenetration: ^f32) -> bool ---
  181. /**
  182. * @brief Cast a ray against mesh geometry
  183. * @param ray rl.Ray to cast
  184. * @param mesh Mesh data to test against
  185. * @param transform Mesh world transform
  186. * @return rl.Ray collision info (hit, distance, point, normal)
  187. */
  188. RaycastMesh :: proc(ray: rl.Ray, mesh: MeshData, transform: rl.Matrix) -> rl.RayCollision ---
  189. /**
  190. * @brief Cast a ray against a model (tests all meshes)
  191. * @param ray rl.Ray to cast
  192. * @param model Model to test against (must have valid meshData)
  193. * @param transform Model world transform
  194. * @return rl.Ray collision info for closest hit (hit=false if no meshData)
  195. */
  196. RaycastModel :: proc(ray: rl.Ray, model: Model, transform: rl.Matrix) -> rl.RayCollision ---
  197. /**
  198. * @brief Sweep sphere against single point
  199. * @param center Sphere center position
  200. * @param radius Sphere radius
  201. * @param velocity Movement vector (direction and magnitude)
  202. * @param point Point to test against
  203. * @return Sweep collision info (hit, time, point, normal)
  204. */
  205. SweepSpherePoint :: proc(center: rl.Vector3, radius: f32, velocity: rl.Vector3, point: rl.Vector3) -> SweepCollision ---
  206. /**
  207. * @brief Sweep sphere against line segment
  208. * @param center Sphere center position
  209. * @param radius Sphere radius
  210. * @param velocity Movement vector (direction and magnitude)
  211. * @param a Segment start point
  212. * @param b Segment end point
  213. * @return Sweep collision info (hit, time, point, normal)
  214. */
  215. SweepSphereSegment :: proc(center: rl.Vector3, radius: f32, velocity: rl.Vector3, a: rl.Vector3, b: rl.Vector3) -> SweepCollision ---
  216. /**
  217. * @brief Sweep sphere against triangle plane (no edge/vertex clipping)
  218. * @param center Sphere center position
  219. * @param radius Sphere radius
  220. * @param velocity Movement vector (direction and magnitude)
  221. * @param a Triangle vertex A
  222. * @param b Triangle vertex B
  223. * @param c Triangle vertex C
  224. * @return Sweep collision info (hit, time, point, normal)
  225. */
  226. SweepSphereTrianglePlane :: proc(center: rl.Vector3, radius: f32, velocity: rl.Vector3, a: rl.Vector3, b: rl.Vector3, _c: rl.Vector3) -> SweepCollision ---
  227. /**
  228. * @brief Sweep sphere against triangle with edge/vertex handling
  229. * @param center Sphere center position
  230. * @param radius Sphere radius
  231. * @param velocity Movement vector (direction and magnitude)
  232. * @param a Triangle vertex A
  233. * @param b Triangle vertex B
  234. * @param c Triangle vertex C
  235. * @return Sweep collision info (hit, time, point, normal)
  236. */
  237. SweepSphereTriangle :: proc(center: rl.Vector3, radius: f32, velocity: rl.Vector3, a: rl.Vector3, b: rl.Vector3, _c: rl.Vector3) -> SweepCollision ---
  238. /**
  239. * @brief Sweep sphere along velocity vector
  240. * @param center Sphere center position
  241. * @param radius Sphere radius
  242. * @param velocity Movement vector (direction and magnitude)
  243. * @param box Obstacle bounding box
  244. * @return Sweep collision info (hit, distance, point, normal)
  245. */
  246. SweepSphereBox :: proc(center: rl.Vector3, radius: f32, velocity: rl.Vector3, box: rl.BoundingBox) -> SweepCollision ---
  247. /**
  248. * @brief Sweep sphere along velocity vector against mesh geometry
  249. * @param center Sphere center position
  250. * @param radius Sphere radius
  251. * @param velocity Movement vector (direction and magnitude)
  252. * @param mesh Mesh data to test against
  253. * @param transform Mesh world transform
  254. * @return Sweep collision info (hit, time, point, normal)
  255. */
  256. SweepSphereMesh :: proc(center: rl.Vector3, radius: f32, velocity: rl.Vector3, mesh: MeshData, transform: rl.Matrix) -> SweepCollision ---
  257. /**
  258. * @brief Sweep capsule along velocity vector
  259. * @param capsule Capsule shape to sweep
  260. * @param velocity Movement vector (direction and magnitude)
  261. * @param box Obstacle bounding box
  262. * @return Sweep collision info (hit, distance, point, normal)
  263. */
  264. SweepCapsuleBox :: proc(capsule: Capsule, velocity: rl.Vector3, box: rl.BoundingBox) -> SweepCollision ---
  265. /**
  266. * @brief Sweep capsule along velocity vector against mesh geometry
  267. * @param capsule Capsule shape to sweep
  268. * @param velocity Movement vector (direction and magnitude)
  269. * @param mesh Mesh data to test against
  270. * @param transform Mesh world transform
  271. * @return Sweep collision info (hit, time, point, normal)
  272. */
  273. SweepCapsuleMesh :: proc(capsule: Capsule, velocity: rl.Vector3, mesh: MeshData, transform: rl.Matrix) -> SweepCollision ---
  274. /**
  275. * @brief Check if sphere is grounded against a box
  276. * @param center Sphere center
  277. * @param radius Sphere radius
  278. * @param checkDistance How far below to check
  279. * @param ground Ground box to test against
  280. * @param outGround Optional: receives raycast hit info
  281. * @return true if grounded within checkDistance
  282. */
  283. IsSphereGroundedBox :: proc(center: rl.Vector3, radius: f32, checkDistance: f32, ground: rl.BoundingBox, outGround: ^rl.RayCollision) -> bool ---
  284. /**
  285. * @brief Check if sphere is grounded against mesh geometry
  286. * @param center Sphere center
  287. * @param radius Sphere radius
  288. * @param checkDistance How far below to check
  289. * @param mesh Mesh data to test against
  290. * @param transform Mesh world transform
  291. * @param outGround Optional: receives raycast hit info
  292. * @return true if grounded within checkDistance
  293. */
  294. IsSphereGroundedMesh :: proc(center: rl.Vector3, radius: f32, checkDistance: f32, mesh: MeshData, transform: rl.Matrix, outGround: ^rl.RayCollision) -> bool ---
  295. /**
  296. * @brief Check if capsule is grounded against a box
  297. * @param capsule Character capsule
  298. * @param checkDistance How far below to check (e.g., 0.1)
  299. * @param ground Ground box to test against
  300. * @param outGround Optional: receives raycast hit info
  301. * @return true if grounded within checkDistance
  302. */
  303. IsCapsuleGroundedBox :: proc(capsule: Capsule, checkDistance: f32, ground: rl.BoundingBox, outGround: ^rl.RayCollision) -> bool ---
  304. /**
  305. * @brief Check if capsule is grounded against mesh geometry
  306. * @param capsule Character capsule
  307. * @param checkDistance How far below to check
  308. * @param mesh Mesh data to test against
  309. * @param transform Mesh world transform
  310. * @param outGround Optional: receives raycast hit info
  311. * @return true if grounded within checkDistance
  312. */
  313. IsCapsuleGroundedMesh :: proc(capsule: Capsule, checkDistance: f32, mesh: MeshData, transform: rl.Matrix, outGround: ^rl.RayCollision) -> bool ---
  314. /**
  315. * @brief Find closest point on line segment to given point
  316. * @param point Query point
  317. * @param start Segment start
  318. * @param end Segment end
  319. * @return Closest point on segment [start, end]
  320. */
  321. ClosestPointOnSegment :: proc(point: rl.Vector3, start: rl.Vector3, end: rl.Vector3) -> rl.Vector3 ---
  322. /**
  323. * @brief Find closest point on triangle to given point
  324. * @param p Query point
  325. * @param a Triangle vertex A
  326. * @param b Triangle vertex B
  327. * @param c Triangle vertex C
  328. * @return Closest point on triangle surface
  329. */
  330. ClosestPointOnTriangle :: proc(p: rl.Vector3, a: rl.Vector3, b: rl.Vector3, _c: rl.Vector3) -> rl.Vector3 ---
  331. /**
  332. * @brief Find closest point on box surface to given point
  333. * @param point Query point
  334. * @param box Bounding box
  335. * @return Closest point on/in box (clamped to box bounds)
  336. */
  337. ClosestPointOnBox :: proc(point: rl.Vector3, box: rl.BoundingBox) -> rl.Vector3 ---
  338. }