JoltC.cpp 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059
  1. #include <Jolt/Jolt.h>
  2. #include <Jolt/Core/Factory.h>
  3. #include <Jolt/Core/JobSystemThreadPool.h>
  4. #include <Jolt/Core/TempAllocator.h>
  5. #include <Jolt/Physics/Body/BodyActivationListener.h>
  6. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  7. #include <Jolt/Physics/Body/BodyLockMulti.h>
  8. #include <Jolt/Physics/Collision/CastResult.h>
  9. #include <Jolt/Physics/Collision/CollideShape.h>
  10. #include <Jolt/Physics/Collision/CollisionCollectorImpl.h>
  11. #include <Jolt/Physics/Collision/ContactListener.h>
  12. #include <Jolt/Physics/Collision/RayCast.h>
  13. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  14. #include <Jolt/Physics/Collision/Shape/CapsuleShape.h>
  15. #include <Jolt/Physics/Collision/Shape/CompoundShape.h>
  16. #include <Jolt/Physics/Collision/Shape/ConvexHullShape.h>
  17. #include <Jolt/Physics/Collision/Shape/CylinderShape.h>
  18. #include <Jolt/Physics/Collision/Shape/MutableCompoundShape.h>
  19. #include <Jolt/Physics/Collision/Shape/SphereShape.h>
  20. #include <Jolt/Physics/Collision/Shape/StaticCompoundShape.h>
  21. #include <Jolt/Physics/Collision/Shape/TriangleShape.h>
  22. #include <Jolt/Physics/Collision/ShapeCast.h>
  23. #include <Jolt/Physics/Collision/SimShapeFilter.h>
  24. #include <Jolt/Physics/Constraints/FixedConstraint.h>
  25. #include <Jolt/Physics/PhysicsSettings.h>
  26. #include <Jolt/Physics/PhysicsSystem.h>
  27. #include <Jolt/RegisterTypes.h>
  28. #include <Jolt/Renderer/DebugRendererSimple.h>
  29. #include <JoltC/JoltC.h>
  30. #define JPC_IMPL static
  31. #define OPAQUE_WRAPPER(c_type, cpp_type) \
  32. static c_type* to_jpc(cpp_type *in) { return reinterpret_cast<c_type*>(in); } \
  33. static const c_type* to_jpc(const cpp_type *in) { return reinterpret_cast<const c_type*>(in); } \
  34. static cpp_type* to_jph(c_type *in) { return reinterpret_cast<cpp_type*>(in); } \
  35. static const cpp_type* to_jph(const c_type *in) { return reinterpret_cast<const cpp_type*>(in); } \
  36. static cpp_type** to_jph(c_type **in) { return reinterpret_cast<cpp_type**>(in); }
  37. #define DESTRUCTOR(c_type) \
  38. JPC_API void c_type##_delete(c_type* object) { \
  39. delete to_jph(object); \
  40. }
  41. #define ENUM_CONVERSION(c_type, cpp_type) \
  42. static c_type to_jpc(cpp_type in) { return static_cast<c_type>(in); } \
  43. static cpp_type to_jph(c_type in) { return static_cast<cpp_type>(in); }
  44. #define LAYOUT_COMPATIBLE(c_type, cpp_type) \
  45. static c_type to_jpc(cpp_type in) { \
  46. c_type out; \
  47. memcpy(&out, &in, sizeof(c_type)); \
  48. return out; \
  49. } \
  50. static cpp_type to_jph(c_type in) { \
  51. cpp_type out; \
  52. memcpy(&out, &in, sizeof(cpp_type)); \
  53. return out; \
  54. } \
  55. static c_type* to_jpc(cpp_type* in) { \
  56. return reinterpret_cast<c_type*>(in); \
  57. } \
  58. static cpp_type* to_jph(c_type* in) { \
  59. return reinterpret_cast<cpp_type*>(in); \
  60. } \
  61. static const c_type* to_jpc(const cpp_type* in) { \
  62. return reinterpret_cast<const c_type*>(in); \
  63. } \
  64. static const cpp_type* to_jph(const c_type* in) { \
  65. return reinterpret_cast<const cpp_type*>(in); \
  66. } \
  67. static_assert(sizeof(c_type) == sizeof(cpp_type), "size of " #c_type " did not match size of " #cpp_type); \
  68. static_assert(alignof(c_type) == alignof(cpp_type), "align of " #c_type " did not match align of " #cpp_type); \
  69. static_assert(!std::is_polymorphic_v<cpp_type>, #cpp_type " is polymorphic and cannot be made layout compatible");
  70. template<typename E>
  71. constexpr auto to_integral(E e) -> typename std::underlying_type<E>::type
  72. {
  73. return static_cast<typename std::underlying_type<E>::type>(e);
  74. }
  75. ENUM_CONVERSION(JPC_AllowedDOFs, JPH::EAllowedDOFs)
  76. ENUM_CONVERSION(JPC_Activation, JPH::EActivation)
  77. ENUM_CONVERSION(JPC_BodyType, JPH::EBodyType)
  78. ENUM_CONVERSION(JPC_MotionQuality, JPH::EMotionQuality)
  79. ENUM_CONVERSION(JPC_OverrideMassProperties, JPH::EOverrideMassProperties)
  80. OPAQUE_WRAPPER(JPC_PhysicsSystem, JPH::PhysicsSystem)
  81. DESTRUCTOR(JPC_PhysicsSystem)
  82. OPAQUE_WRAPPER(JPC_BodyInterface, JPH::BodyInterface)
  83. OPAQUE_WRAPPER(JPC_BodyLockInterface, JPH::BodyLockInterface)
  84. OPAQUE_WRAPPER(JPC_BodyLockRead, JPH::BodyLockRead)
  85. OPAQUE_WRAPPER(JPC_BodyLockWrite, JPH::BodyLockWrite)
  86. OPAQUE_WRAPPER(JPC_BodyLockMultiRead, JPH::BodyLockMultiRead)
  87. OPAQUE_WRAPPER(JPC_BodyLockMultiWrite, JPH::BodyLockMultiWrite)
  88. OPAQUE_WRAPPER(JPC_NarrowPhaseQuery, JPH::NarrowPhaseQuery)
  89. OPAQUE_WRAPPER(JPC_TempAllocatorImpl, JPH::TempAllocatorImpl)
  90. DESTRUCTOR(JPC_TempAllocatorImpl)
  91. OPAQUE_WRAPPER(JPC_JobSystemThreadPool, JPH::JobSystemThreadPool)
  92. DESTRUCTOR(JPC_JobSystemThreadPool)
  93. OPAQUE_WRAPPER(JPC_Shape, JPH::Shape)
  94. OPAQUE_WRAPPER(JPC_CompoundShape, JPH::CompoundShape)
  95. OPAQUE_WRAPPER(JPC_Body, JPH::Body)
  96. OPAQUE_WRAPPER(JPC_VertexList, JPH::VertexList)
  97. DESTRUCTOR(JPC_VertexList)
  98. OPAQUE_WRAPPER(JPC_IndexedTriangleList, JPH::IndexedTriangleList)
  99. DESTRUCTOR(JPC_IndexedTriangleList)
  100. OPAQUE_WRAPPER(JPC_String, JPH::String)
  101. DESTRUCTOR(JPC_String)
  102. LAYOUT_COMPATIBLE(JPC_BodyManager_DrawSettings, JPH::BodyManager::DrawSettings)
  103. LAYOUT_COMPATIBLE(JPC_BodyID, JPH::BodyID)
  104. static auto to_jpc(JPH::BroadPhaseLayer in) { return in.GetValue(); }
  105. static auto to_jph(JPC_BroadPhaseLayer in) { return JPH::BroadPhaseLayer(in); }
  106. static JPC_Vec3 to_jpc(JPH::Vec3 in) {
  107. return JPC_Vec3{in.GetX(), in.GetY(), in.GetZ(), in.GetZ()};
  108. }
  109. static JPH::Vec3 to_jph(JPC_Vec3 in) {
  110. return JPH::Vec3(in.x, in.y, in.z);
  111. }
  112. static JPC_Vec4 to_jpc(JPH::Vec4 in) {
  113. return JPC_Vec4{in.GetX(), in.GetY(), in.GetZ(), in.GetW()};
  114. }
  115. static JPH::Vec4 to_jph(JPC_Vec4 in) {
  116. return JPH::Vec4(in.x, in.y, in.z, in.w);
  117. }
  118. static JPH::Array<JPH::Vec3> to_jph(const JPC_Vec3* src, size_t n) {
  119. JPH::Array<JPH::Vec3> vec;
  120. vec.resize(n);
  121. if (src != nullptr) {
  122. memcpy(vec.data(), src, n * sizeof(*src));
  123. }
  124. return vec;
  125. }
  126. static JPC_DVec3 to_jpc(JPH::DVec3 in) {
  127. return JPC_DVec3{in.GetX(), in.GetY(), in.GetZ(), in.GetZ()};
  128. }
  129. static JPH::DVec3 to_jph(JPC_DVec3 in) {
  130. return JPH::DVec3(in.x, in.y, in.z);
  131. }
  132. static JPC_Quat to_jpc(JPH::Quat in) {
  133. return JPC_Quat{in.GetX(), in.GetY(), in.GetZ(), in.GetW()};
  134. }
  135. static JPH::Quat to_jph(JPC_Quat in) {
  136. return JPH::Quat(in.x, in.y, in.z, in.w);
  137. }
  138. static JPC_Mat44 to_jpc(JPH::Mat44 in) {
  139. JPC_Mat44 out;
  140. in.StoreFloat4x4(reinterpret_cast<JPH::Float4*>(&out));
  141. return out;
  142. }
  143. static JPH::Mat44 to_jph(JPC_Mat44 in) {
  144. return JPH::Mat44::sLoadFloat4x4Aligned(reinterpret_cast<const JPH::Float4*>(&in));
  145. }
  146. static JPC_DMat44 to_jpc(JPH::DMat44 in) {
  147. JPC_DMat44 out;
  148. out.col[0] = to_jpc(in.GetColumn4(0));
  149. out.col[1] = to_jpc(in.GetColumn4(1));
  150. out.col[2] = to_jpc(in.GetColumn4(2));
  151. out.col3 = to_jpc(in.GetTranslation());
  152. return out;
  153. }
  154. static JPH::DMat44 to_jph(JPC_DMat44 in) {
  155. JPH::DVec3 col3 = to_jph(in.col3);
  156. JPH::DMat44 out(
  157. to_jph(in.col[0]),
  158. to_jph(in.col[1]),
  159. to_jph(in.col[2]),
  160. col3);
  161. return out;
  162. }
  163. static JPC_Color to_jpc(JPH::Color in) {
  164. return JPC_Color{in.r, in.g, in.b, in.a};
  165. }
  166. static JPH::Color to_jph(JPC_Color in) {
  167. return JPH::Color(in.r, in.g, in.b, in.a);
  168. }
  169. static JPH::RayCast to_jph(JPC_RayCast in) {
  170. return JPH::RayCast(to_jph(in.Origin), to_jph(in.Direction));
  171. }
  172. static JPH::RRayCast to_jph(JPC_RRayCast in) {
  173. return JPH::RRayCast(to_jph(in.Origin), to_jph(in.Direction));
  174. }
  175. static JPH::RShapeCast to_jph(JPC_RShapeCast in) {
  176. return JPH::RShapeCast(
  177. to_jph(in.Shape),
  178. to_jph(in.Scale),
  179. to_jph(in.CenterOfMassStart),
  180. to_jph(in.Direction));
  181. }
  182. static JPH::SubShapeID to_jph_subshapeid(JPC_SubShapeID in) {
  183. JPH::SubShapeID out;
  184. out.SetValue(in);
  185. return out;
  186. }
  187. static JPC_SubShapeID to_jpc(JPH::SubShapeID in) {
  188. return in.GetValue();
  189. }
  190. static JPC_RayCastResult to_jpc(JPH::RayCastResult in) {
  191. JPC_RayCastResult out{0};
  192. out.BodyID = to_jpc(in.mBodyID);
  193. out.Fraction = in.mFraction;
  194. out.SubShapeID2 = to_jpc(in.mSubShapeID2);
  195. return out;
  196. }
  197. JPC_IMPL JPC_ShapeCastResult JPC_ShapeCastResult_to_jpc(JPH::ShapeCastResult in) {
  198. JPC_ShapeCastResult out{};
  199. // CollideShapeResult
  200. out.ContactPointOn1 = to_jpc(in.mContactPointOn1);
  201. out.ContactPointOn2 = to_jpc(in.mContactPointOn2);
  202. out.PenetrationAxis = to_jpc(in.mPenetrationAxis);
  203. out.PenetrationDepth = in.mPenetrationDepth;
  204. out.SubShapeID1 = to_jpc(in.mSubShapeID1);
  205. out.SubShapeID2 = to_jpc(in.mSubShapeID2);
  206. out.BodyID2 = to_jpc(in.mBodyID2);
  207. // Face Shape1Face;
  208. // Face Shape2Face;
  209. // ShapeCastResult
  210. out.Fraction = in.mFraction;
  211. out.IsBackFaceHit = in.mIsBackFaceHit;
  212. return out;
  213. }
  214. JPC_IMPL JPH::ShapeCastSettings JPC_ShapeCastSettings_to_jph(JPC_ShapeCastSettings in) {
  215. JPH::ShapeCastSettings out{};
  216. // JPH::CollideSettingsBase
  217. // EActiveEdgeMode ActiveEdgeMode;
  218. // ECollectFacesMode CollectFacesMode;
  219. out.mCollisionTolerance = in.CollisionTolerance;
  220. out.mPenetrationTolerance = in.PenetrationTolerance;
  221. out.mActiveEdgeMovementDirection = to_jph(in.ActiveEdgeMovementDirection);
  222. // JPH::ShapeCastSettings
  223. out.mBackFaceModeTriangles = static_cast<JPH::EBackFaceMode>(in.BackFaceModeTriangles);
  224. out.mBackFaceModeConvex = static_cast<JPH::EBackFaceMode>(in.BackFaceModeConvex);
  225. out.mUseShrunkenShapeAndConvexRadius = in.UseShrunkenShapeAndConvexRadius;
  226. out.mReturnDeepestPoint = in.ReturnDeepestPoint;
  227. return out;
  228. }
  229. #include <JoltCImpl/Generated.h>
  230. JPC_API void JPC_RegisterDefaultAllocator() {
  231. JPH::RegisterDefaultAllocator();
  232. }
  233. JPC_API void JPC_FactoryInit() {
  234. JPH::Factory::sInstance = new JPH::Factory();
  235. }
  236. JPC_API void JPC_FactoryDelete() {
  237. delete JPH::Factory::sInstance;
  238. JPH::Factory::sInstance = nullptr;
  239. }
  240. JPC_API void JPC_RegisterTypes() {
  241. JPH::RegisterTypes();
  242. }
  243. JPC_API void JPC_UnregisterTypes() {
  244. JPH::UnregisterTypes();
  245. }
  246. ////////////////////////////////////////////////////////////////////////////////
  247. // VertexList == Array<Float3> == std::vector<Float3>
  248. JPC_API JPC_VertexList* JPC_VertexList_new(const JPC_Float3* storage, size_t len) {
  249. const JPH::Float3* new_storage = (const JPH::Float3*)storage;
  250. return to_jpc(new JPH::VertexList(new_storage, new_storage + len));
  251. }
  252. ////////////////////////////////////////////////////////////////////////////////
  253. // IndexedTriangleList == Array<IndexedTriangle> == std::vector<IndexedTriangle>
  254. JPC_API JPC_IndexedTriangleList* JPC_IndexedTriangleList_new(const JPC_IndexedTriangle* storage, size_t len) {
  255. const JPH::IndexedTriangle* new_storage = (const JPH::IndexedTriangle*)storage;
  256. return to_jpc(new JPH::IndexedTriangleList(new_storage, new_storage + len));
  257. }
  258. ////////////////////////////////////////////////////////////////////////////////
  259. // TempAllocatorImpl
  260. JPC_API JPC_TempAllocatorImpl* JPC_TempAllocatorImpl_new(uint size) {
  261. return to_jpc(new JPH::TempAllocatorImpl(size));
  262. }
  263. ////////////////////////////////////////////////////////////////////////////////
  264. // JobSystemThreadPool
  265. JPC_API JPC_JobSystemThreadPool* JPC_JobSystemThreadPool_new2(
  266. uint inMaxJobs,
  267. uint inMaxBarriers)
  268. {
  269. return to_jpc(new JPH::JobSystemThreadPool(inMaxJobs, inMaxBarriers));
  270. }
  271. JPC_API JPC_JobSystemThreadPool* JPC_JobSystemThreadPool_new3(
  272. uint inMaxJobs,
  273. uint inMaxBarriers,
  274. int inNumThreads)
  275. {
  276. return to_jpc(new JPH::JobSystemThreadPool(inMaxJobs, inMaxBarriers, inNumThreads));
  277. }
  278. ////////////////////////////////////////////////////////////////////////////////
  279. // BroadPhaseLayerInterface
  280. class JPC_BroadPhaseLayerInterfaceBridge final : public JPH::BroadPhaseLayerInterface {
  281. public:
  282. explicit JPC_BroadPhaseLayerInterfaceBridge(const void *self, JPC_BroadPhaseLayerInterfaceFns fns) : self(self), fns(fns) {}
  283. virtual uint GetNumBroadPhaseLayers() const override {
  284. return fns.GetNumBroadPhaseLayers(self);
  285. }
  286. virtual JPH::BroadPhaseLayer GetBroadPhaseLayer(JPH::ObjectLayer inLayer) const override {
  287. return to_jph(fns.GetBroadPhaseLayer(self, inLayer));
  288. }
  289. #if defined(JPH_EXTERNAL_PROFILE) || defined(JPH_PROFILE_ENABLED)
  290. virtual const char * GetBroadPhaseLayerName([[maybe_unused]] JPH::BroadPhaseLayer inLayer) const override {
  291. return "FIXME";
  292. }
  293. #endif
  294. private:
  295. const void* self;
  296. JPC_BroadPhaseLayerInterfaceFns fns;
  297. };
  298. OPAQUE_WRAPPER(JPC_BroadPhaseLayerInterface, JPC_BroadPhaseLayerInterfaceBridge)
  299. DESTRUCTOR(JPC_BroadPhaseLayerInterface)
  300. JPC_API JPC_BroadPhaseLayerInterface* JPC_BroadPhaseLayerInterface_new(
  301. const void *self,
  302. JPC_BroadPhaseLayerInterfaceFns fns)
  303. {
  304. return to_jpc(new JPC_BroadPhaseLayerInterfaceBridge(self, fns));
  305. }
  306. ////////////////////////////////////////////////////////////////////////////////
  307. // ObjectVsBroadPhaseLayerFilter
  308. class JPC_ObjectVsBroadPhaseLayerFilterBridge final : public JPH::ObjectVsBroadPhaseLayerFilter {
  309. public:
  310. explicit JPC_ObjectVsBroadPhaseLayerFilterBridge(const void *self, JPC_ObjectVsBroadPhaseLayerFilterFns fns) : self(self), fns(fns) {}
  311. virtual bool ShouldCollide(JPH::ObjectLayer inLayer1, JPH::BroadPhaseLayer inLayer2) const override {
  312. return fns.ShouldCollide(self, inLayer1, to_jpc(inLayer2));
  313. }
  314. private:
  315. const void* self;
  316. JPC_ObjectVsBroadPhaseLayerFilterFns fns;
  317. };
  318. OPAQUE_WRAPPER(JPC_ObjectVsBroadPhaseLayerFilter, JPC_ObjectVsBroadPhaseLayerFilterBridge)
  319. DESTRUCTOR(JPC_ObjectVsBroadPhaseLayerFilter)
  320. JPC_API JPC_ObjectVsBroadPhaseLayerFilter* JPC_ObjectVsBroadPhaseLayerFilter_new(
  321. const void *self,
  322. JPC_ObjectVsBroadPhaseLayerFilterFns fns)
  323. {
  324. return to_jpc(new JPC_ObjectVsBroadPhaseLayerFilterBridge(self, fns));
  325. }
  326. ////////////////////////////////////////////////////////////////////////////////
  327. // BroadPhaseLayerFilter
  328. class JPC_BroadPhaseLayerFilterBridge final : public JPH::BroadPhaseLayerFilter {
  329. public:
  330. explicit JPC_BroadPhaseLayerFilterBridge(const void *self, JPC_BroadPhaseLayerFilterFns fns) : self(self), fns(fns) {}
  331. virtual bool ShouldCollide(JPH::BroadPhaseLayer inLayer) const override {
  332. return fns.ShouldCollide(self, to_jpc(inLayer));
  333. }
  334. private:
  335. const void* self;
  336. JPC_BroadPhaseLayerFilterFns fns;
  337. };
  338. OPAQUE_WRAPPER(JPC_BroadPhaseLayerFilter, JPC_BroadPhaseLayerFilterBridge)
  339. DESTRUCTOR(JPC_BroadPhaseLayerFilter)
  340. JPC_API JPC_BroadPhaseLayerFilter* JPC_BroadPhaseLayerFilter_new(
  341. const void *self,
  342. JPC_BroadPhaseLayerFilterFns fns)
  343. {
  344. return to_jpc(new JPC_BroadPhaseLayerFilterBridge(self, fns));
  345. }
  346. ////////////////////////////////////////////////////////////////////////////////
  347. // ObjectLayerFilter
  348. class JPC_ObjectLayerFilterBridge final : public JPH::ObjectLayerFilter {
  349. public:
  350. explicit JPC_ObjectLayerFilterBridge(const void *self, JPC_ObjectLayerFilterFns fns) : self(self), fns(fns) {}
  351. virtual bool ShouldCollide(JPH::ObjectLayer inLayer) const override {
  352. return fns.ShouldCollide(self, inLayer);
  353. }
  354. private:
  355. const void* self;
  356. JPC_ObjectLayerFilterFns fns;
  357. };
  358. OPAQUE_WRAPPER(JPC_ObjectLayerFilter, JPC_ObjectLayerFilterBridge)
  359. DESTRUCTOR(JPC_ObjectLayerFilter)
  360. JPC_API JPC_ObjectLayerFilter* JPC_ObjectLayerFilter_new(
  361. const void *self,
  362. JPC_ObjectLayerFilterFns fns)
  363. {
  364. return to_jpc(new JPC_ObjectLayerFilterBridge(self, fns));
  365. }
  366. ////////////////////////////////////////////////////////////////////////////////
  367. // BodyFilter
  368. class JPC_BodyFilterBridge final : public JPH::BodyFilter {
  369. public:
  370. explicit JPC_BodyFilterBridge(const void *self, JPC_BodyFilterFns fns) : self(self), fns(fns) {}
  371. virtual bool ShouldCollide(const JPH::BodyID &inBodyID) const override {
  372. return fns.ShouldCollide(self, to_jpc(inBodyID));
  373. }
  374. virtual bool ShouldCollideLocked(const JPH::Body &inBody) const override {
  375. return fns.ShouldCollideLocked(self, to_jpc(&inBody));
  376. }
  377. private:
  378. const void* self;
  379. JPC_BodyFilterFns fns;
  380. };
  381. OPAQUE_WRAPPER(JPC_BodyFilter, JPC_BodyFilterBridge)
  382. DESTRUCTOR(JPC_BodyFilter)
  383. JPC_API JPC_BodyFilter* JPC_BodyFilter_new(
  384. const void *self,
  385. JPC_BodyFilterFns fns)
  386. {
  387. return to_jpc(new JPC_BodyFilterBridge(self, fns));
  388. }
  389. ////////////////////////////////////////////////////////////////////////////////
  390. // ShapeFilter
  391. class JPC_ShapeFilterBridge final : public JPH::ShapeFilter {
  392. public:
  393. explicit JPC_ShapeFilterBridge(const void *self, JPC_ShapeFilterFns fns) : self(self), fns(fns) {}
  394. virtual bool ShouldCollide(const JPH::Shape *inShape2, const JPH::SubShapeID &inSubShapeIDOfShape2) const override {
  395. if (fns.ShouldCollide == nullptr) {
  396. return true;
  397. }
  398. return fns.ShouldCollide(self, to_jpc(inShape2), to_jpc(inSubShapeIDOfShape2));
  399. }
  400. private:
  401. const void* self;
  402. JPC_ShapeFilterFns fns;
  403. };
  404. OPAQUE_WRAPPER(JPC_ShapeFilter, JPC_ShapeFilterBridge)
  405. DESTRUCTOR(JPC_ShapeFilter)
  406. JPC_API JPC_ShapeFilter* JPC_ShapeFilter_new(
  407. const void *self,
  408. JPC_ShapeFilterFns fns)
  409. {
  410. return to_jpc(new JPC_ShapeFilterBridge(self, fns));
  411. }
  412. ////////////////////////////////////////////////////////////////////////////////
  413. // SimShapeFilter
  414. class JPC_SimShapeFilterBridge final : public JPH::SimShapeFilter {
  415. public:
  416. explicit JPC_SimShapeFilterBridge(const void *self, JPC_SimShapeFilterFns fns) : self(self), fns(fns) {}
  417. virtual bool ShouldCollide(
  418. const JPH::Body &inBody1, const JPH::Shape *inShape1, const JPH::SubShapeID &inSubShapeIDOfShape1,
  419. const JPH::Body &inBody2, const JPH::Shape *inShape2, const JPH::SubShapeID &inSubShapeIDOfShape2) const override
  420. {
  421. if (fns.ShouldCollide == nullptr) {
  422. return true;
  423. }
  424. return fns.ShouldCollide(self,
  425. to_jpc(&inBody1), to_jpc(inShape1), to_jpc(inSubShapeIDOfShape1),
  426. to_jpc(&inBody2), to_jpc(inShape2), to_jpc(inSubShapeIDOfShape2));
  427. }
  428. private:
  429. const void* self;
  430. JPC_SimShapeFilterFns fns;
  431. };
  432. OPAQUE_WRAPPER(JPC_SimShapeFilter, JPC_SimShapeFilterBridge)
  433. DESTRUCTOR(JPC_SimShapeFilter)
  434. JPC_API JPC_SimShapeFilter* JPC_SimShapeFilter_new(
  435. const void *self,
  436. JPC_SimShapeFilterFns fns)
  437. {
  438. return to_jpc(new JPC_SimShapeFilterBridge(self, fns));
  439. }
  440. ////////////////////////////////////////////////////////////////////////////////
  441. // JPC_ObjectLayerPairFilter
  442. class JPC_ObjectLayerPairFilterBridge final : public JPH::ObjectLayerPairFilter {
  443. public:
  444. explicit JPC_ObjectLayerPairFilterBridge(const void *self, JPC_ObjectLayerPairFilterFns fns) : self(self), fns(fns) {}
  445. virtual bool ShouldCollide(JPH::ObjectLayer inLayer1, JPH::ObjectLayer inLayer2) const override {
  446. return fns.ShouldCollide(self, inLayer1, inLayer2);
  447. }
  448. private:
  449. const void* self;
  450. JPC_ObjectLayerPairFilterFns fns;
  451. };
  452. OPAQUE_WRAPPER(JPC_ObjectLayerPairFilter, JPC_ObjectLayerPairFilterBridge)
  453. DESTRUCTOR(JPC_ObjectLayerPairFilter)
  454. JPC_API JPC_ObjectLayerPairFilter* JPC_ObjectLayerPairFilter_new(
  455. const void *self,
  456. JPC_ObjectLayerPairFilterFns fns)
  457. {
  458. return to_jpc(new JPC_ObjectLayerPairFilterBridge(self, fns));
  459. }
  460. ////////////////////////////////////////////////////////////////////////////////
  461. // JPC_ContactListener
  462. class JPC_ContactListenerBridge final : public JPH::ContactListener {
  463. public:
  464. explicit JPC_ContactListenerBridge(void *self, JPC_ContactListenerFns fns) : self(self), fns(fns) {}
  465. JPH::ValidateResult OnContactValidate(
  466. const JPH::Body &inBody1,
  467. const JPH::Body &inBody2,
  468. JPH::RVec3Arg inBaseOffset,
  469. const JPH::CollideShapeResult &inCollisionResult) override
  470. {
  471. // if (fns.OnContactValidate != nullptr) {
  472. // return fns.OnContactValidate(self, to_jpc(inBody1), to_jpc(inBody2), to_jpc(inBaseOffset), to_jpc(inCollisionResult));
  473. // }
  474. return ContactListener::OnContactValidate(inBody1, inBody2, inBaseOffset, inCollisionResult);
  475. }
  476. void OnContactAdded(
  477. const JPH::Body &inBody1,
  478. const JPH::Body &inBody2,
  479. const JPH::ContactManifold &inManifold,
  480. JPH::ContactSettings &ioSettings) override
  481. {
  482. if (fns.OnContactAdded != nullptr) {
  483. const auto* cManifold = reinterpret_cast<const JPC_ContactManifold*>(&inManifold);
  484. auto* cSettings = reinterpret_cast<JPC_ContactSettings*>(&ioSettings);
  485. fns.OnContactAdded(self, to_jpc(&inBody1), to_jpc(&inBody2), cManifold, cSettings);
  486. }
  487. }
  488. void OnContactPersisted(
  489. const JPH::Body &inBody1,
  490. const JPH::Body &inBody2,
  491. const JPH::ContactManifold &inManifold,
  492. JPH::ContactSettings &ioSettings) override
  493. {
  494. if (fns.OnContactPersisted != nullptr) {
  495. const auto* cManifold = reinterpret_cast<const JPC_ContactManifold*>(&inManifold);
  496. auto* cSettings = reinterpret_cast<JPC_ContactSettings*>(&ioSettings);
  497. fns.OnContactPersisted(self, to_jpc(&inBody1), to_jpc(&inBody2), cManifold, cSettings);
  498. }
  499. }
  500. void OnContactRemoved(const JPH::SubShapeIDPair &inSubShapePair) override {
  501. if (fns.OnContactRemoved != nullptr) {
  502. const auto* cSubShapePair = reinterpret_cast<const JPC_SubShapeIDPair*>(&inSubShapePair);
  503. fns.OnContactRemoved(self, cSubShapePair);
  504. }
  505. }
  506. private:
  507. void* self;
  508. JPC_ContactListenerFns fns;
  509. };
  510. OPAQUE_WRAPPER(JPC_ContactListener, JPC_ContactListenerBridge)
  511. DESTRUCTOR(JPC_ContactListener)
  512. JPC_API JPC_ContactListener* JPC_ContactListener_new(
  513. void *self,
  514. JPC_ContactListenerFns fns)
  515. {
  516. return to_jpc(new JPC_ContactListenerBridge(self, fns));
  517. }
  518. ////////////////////////////////////////////////////////////////////////////////
  519. // JPC_CastShapeCollector
  520. class JPC_CastShapeCollectorBridge;
  521. OPAQUE_WRAPPER(JPC_CastShapeCollector, JPC_CastShapeCollectorBridge)
  522. class JPC_CastShapeCollectorBridge final : public JPH::CastShapeCollector {
  523. using ResultType = JPH::ShapeCastResult;
  524. public:
  525. explicit JPC_CastShapeCollectorBridge(void *self, JPC_CastShapeCollectorFns fns) : self(self), fns(fns) {}
  526. void Reset() override {
  527. JPH::CastShapeCollector::Reset();
  528. if (fns.Reset != nullptr) {
  529. fns.Reset(self);
  530. }
  531. }
  532. void AddHit(const ResultType &inResult) override {
  533. JPC_ShapeCastResult result = JPC_ShapeCastResult_to_jpc(inResult);
  534. JPC_CastShapeCollector *base = to_jpc(this);
  535. fns.AddHit(self, base, &result);
  536. }
  537. private:
  538. void* self;
  539. JPC_CastShapeCollectorFns fns;
  540. };
  541. DESTRUCTOR(JPC_CastShapeCollector)
  542. JPC_API JPC_CastShapeCollector* JPC_CastShapeCollector_new(
  543. void *self,
  544. JPC_CastShapeCollectorFns fns)
  545. {
  546. return to_jpc(new JPC_CastShapeCollectorBridge(self, fns));
  547. }
  548. JPC_API void JPC_CastShapeCollector_UpdateEarlyOutFraction(JPC_CastShapeCollector* self, float inFraction) {
  549. to_jph(self)->UpdateEarlyOutFraction(inFraction);
  550. }
  551. ////////////////////////////////////////////////////////////////////////////////
  552. // BodyManager::DrawSettings
  553. JPC_API void JPC_BodyManager_DrawSettings_default(JPC_BodyManager_DrawSettings* object) {
  554. *object = to_jpc(JPH::BodyManager::DrawSettings());
  555. }
  556. ////////////////////////////////////////////////////////////////////////////////
  557. // DebugRendererSimple
  558. class JPC_DebugRendererSimpleBridge final : public JPH::DebugRendererSimple {
  559. public:
  560. explicit JPC_DebugRendererSimpleBridge(const void *self, JPC_DebugRendererSimpleFns fns) : self(self), fns(fns) {}
  561. virtual void DrawLine(JPH::RVec3Arg inFrom, JPH::RVec3Arg inTo, JPH::ColorArg inColor) override {
  562. fns.DrawLine(self, to_jpc(inFrom), to_jpc(inTo), to_jpc(inColor));
  563. }
  564. virtual void DrawText3D(
  565. [[maybe_unused]] JPH::RVec3Arg inPosition,
  566. [[maybe_unused]] const std::string_view &inString,
  567. [[maybe_unused]] JPH::ColorArg inColor = JPH::Color::sWhite,
  568. [[maybe_unused]] float inHeight = 0.5f) override
  569. {
  570. // TODO
  571. }
  572. private:
  573. const void* self;
  574. JPC_DebugRendererSimpleFns fns;
  575. };
  576. OPAQUE_WRAPPER(JPC_DebugRendererSimple, JPC_DebugRendererSimpleBridge)
  577. DESTRUCTOR(JPC_DebugRendererSimple)
  578. JPC_API JPC_DebugRendererSimple* JPC_DebugRendererSimple_new(
  579. const void *self,
  580. JPC_DebugRendererSimpleFns fns)
  581. {
  582. return to_jpc(new JPC_DebugRendererSimpleBridge(self, fns));
  583. }
  584. ////////////////////////////////////////////////////////////////////////////////
  585. // String
  586. JPC_API const char* JPC_String_c_str(JPC_String* self) {
  587. return to_jph(self)->c_str();
  588. }
  589. ////////////////////////////////////////////////////////////////////////////////
  590. // Constraint
  591. OPAQUE_WRAPPER(JPC_Constraint, JPH::Constraint);
  592. JPC_API void JPC_Constraint_delete(JPC_Constraint* self) {
  593. delete to_jph(self);
  594. }
  595. ////////////////////////////////////////////////////////////////////////////////
  596. // FixedConstraintSettings
  597. JPC_API JPC_Constraint* JPC_FixedConstraintSettings_Create(
  598. const JPC_FixedConstraintSettings* self,
  599. JPC_Body* inBody1,
  600. JPC_Body* inBody2)
  601. {
  602. JPH::FixedConstraintSettings jphSettings;
  603. JPC_FixedConstraintSettings_to_jph(self, &jphSettings);
  604. return to_jpc(jphSettings.Create(*to_jph(inBody1), *to_jph(inBody2)));
  605. }
  606. ////////////////////////////////////////////////////////////////////////////////
  607. // Shape
  608. JPC_API uint32_t JPC_Shape_GetRefCount(const JPC_Shape* self) {
  609. return to_jph(self)->GetRefCount();
  610. }
  611. JPC_API void JPC_Shape_AddRef(const JPC_Shape* self) {
  612. to_jph(self)->AddRef();
  613. }
  614. JPC_API void JPC_Shape_Release(const JPC_Shape* self) {
  615. to_jph(self)->Release();
  616. }
  617. JPC_API uint64_t JPC_Shape_GetUserData(const JPC_Shape* self) {
  618. return to_jph(self)->GetUserData();
  619. }
  620. JPC_API void JPC_Shape_SetUserData(JPC_Shape* self, uint64_t userData) {
  621. to_jph(self)->SetUserData(userData);
  622. }
  623. JPC_API JPC_ShapeType JPC_Shape_GetType(const JPC_Shape* self) {
  624. return static_cast<JPC_ShapeType>(to_jph(self)->GetType());
  625. }
  626. JPC_API JPC_ShapeSubType JPC_Shape_GetSubType(const JPC_Shape* self) {
  627. return static_cast<JPC_ShapeSubType>(to_jph(self)->GetSubType());
  628. }
  629. JPC_API JPC_Vec3 JPC_Shape_GetCenterOfMass(const JPC_Shape* self) {
  630. return to_jpc(to_jph(self)->GetCenterOfMass());
  631. }
  632. ////////////////////////////////////////////////////////////////////////////////
  633. // CompoundShape
  634. JPC_API uint32_t JPC_CompoundShape_GetSubShapeIndexFromID(
  635. const JPC_CompoundShape* self,
  636. JPC_SubShapeID inSubShapeID,
  637. JPC_SubShapeID* outRemainder)
  638. {
  639. JPH::SubShapeID jphRemainder;
  640. uint32_t res = to_jph(self)->GetSubShapeIndexFromID(to_jph_subshapeid(inSubShapeID), jphRemainder);
  641. *outRemainder = to_jpc(jphRemainder);
  642. return res;
  643. }
  644. ////////////////////////////////////////////////////////////////////////////////
  645. // ShapeSettings
  646. // Unpack a ShapeResult into a bool and two pointers to be friendlier to C.
  647. static bool HandleShapeResult(JPH::ShapeSettings::ShapeResult res, JPC_Shape** outShape, JPC_String** outError) {
  648. if (res.HasError()) {
  649. if (outError != nullptr) {
  650. JPH::String* created = new JPH::String(std::move(res.GetError()));
  651. *outError = to_jpc(created);
  652. }
  653. return false;
  654. } else {
  655. JPH::Ref<JPH::Shape> shape = res.Get();
  656. shape->AddRef();
  657. *outShape = to_jpc((JPH::Shape*)shape);
  658. return true;
  659. }
  660. }
  661. ////////////////////////////////////////////////////////////////////////////////
  662. // TriangleShapeSettings
  663. static void to_jph(const JPC_TriangleShapeSettings* input, JPH::TriangleShapeSettings* output) {
  664. output->mUserData = input->UserData;
  665. // TODO: Material
  666. output->mDensity = input->Density;
  667. output->mV1 = to_jph(input->V1);
  668. output->mV2 = to_jph(input->V2);
  669. output->mV3 = to_jph(input->V3);
  670. output->mConvexRadius = input->ConvexRadius;
  671. }
  672. JPC_API void JPC_TriangleShapeSettings_default(JPC_TriangleShapeSettings* object) {
  673. object->UserData = 0;
  674. // TODO: Material
  675. object->Density = 1000.0;
  676. object->V1 = {0};
  677. object->V2 = {0};
  678. object->V3 = {0};
  679. object->ConvexRadius = 0.0;
  680. }
  681. JPC_API bool JPC_TriangleShapeSettings_Create(const JPC_TriangleShapeSettings* self, JPC_Shape** outShape, JPC_String** outError) {
  682. JPH::TriangleShapeSettings settings;
  683. to_jph(self, &settings);
  684. return HandleShapeResult(settings.Create(), outShape, outError);
  685. }
  686. ////////////////////////////////////////////////////////////////////////////////
  687. // BoxShapeSettings
  688. static void to_jph(const JPC_BoxShapeSettings* input, JPH::BoxShapeSettings* output) {
  689. output->mUserData = input->UserData;
  690. // TODO: Material
  691. output->mDensity = input->Density;
  692. output->mHalfExtent = to_jph(input->HalfExtent);
  693. output->mConvexRadius = input->ConvexRadius;
  694. }
  695. JPC_API void JPC_BoxShapeSettings_default(JPC_BoxShapeSettings* object) {
  696. object->UserData = 0;
  697. // TODO: Material
  698. object->Density = 1000.0;
  699. object->HalfExtent = JPC_Vec3{0};
  700. object->ConvexRadius = 0.0;
  701. }
  702. JPC_API bool JPC_BoxShapeSettings_Create(const JPC_BoxShapeSettings* self, JPC_Shape** outShape, JPC_String** outError) {
  703. JPH::BoxShapeSettings settings;
  704. to_jph(self, &settings);
  705. return HandleShapeResult(settings.Create(), outShape, outError);
  706. }
  707. ////////////////////////////////////////////////////////////////////////////////
  708. // SphereShapeSettings
  709. static void to_jph(const JPC_SphereShapeSettings* input, JPH::SphereShapeSettings* output) {
  710. output->mUserData = input->UserData;
  711. // TODO: Material
  712. output->mDensity = input->Density;
  713. output->mRadius = input->Radius;
  714. }
  715. JPC_API void JPC_SphereShapeSettings_default(JPC_SphereShapeSettings* object) {
  716. object->UserData = 0;
  717. // TODO: Material
  718. object->Density = 1000.0;
  719. object->Radius = 0.0;
  720. }
  721. JPC_API bool JPC_SphereShapeSettings_Create(const JPC_SphereShapeSettings* self, JPC_Shape** outShape, JPC_String** outError) {
  722. JPH::SphereShapeSettings settings;
  723. to_jph(self, &settings);
  724. return HandleShapeResult(settings.Create(), outShape, outError);
  725. }
  726. ////////////////////////////////////////////////////////////////////////////////
  727. // CapsuleShapeSettings
  728. static void to_jph(const JPC_CapsuleShapeSettings* input, JPH::CapsuleShapeSettings* output) {
  729. output->mUserData = input->UserData;
  730. // TODO: Material
  731. output->mDensity = input->Density;
  732. output->mRadius = input->Radius;
  733. output->mHalfHeightOfCylinder = input->HalfHeightOfCylinder;
  734. }
  735. JPC_API void JPC_CapsuleShapeSettings_default(JPC_CapsuleShapeSettings* object) {
  736. object->UserData = 0;
  737. // TODO: Material
  738. object->Density = 1000.0;
  739. object->Radius = 0.0;
  740. object->HalfHeightOfCylinder = 0.0;
  741. }
  742. JPC_API bool JPC_CapsuleShapeSettings_Create(const JPC_CapsuleShapeSettings* self, JPC_Shape** outShape, JPC_String** outError) {
  743. JPH::CapsuleShapeSettings settings;
  744. to_jph(self, &settings);
  745. return HandleShapeResult(settings.Create(), outShape, outError);
  746. }
  747. ////////////////////////////////////////////////////////////////////////////////
  748. // CylinderShapeSettings
  749. static void to_jph(const JPC_CylinderShapeSettings* input, JPH::CylinderShapeSettings* output) {
  750. output->mUserData = input->UserData;
  751. // TODO: Material
  752. output->mDensity = input->Density;
  753. output->mHalfHeight = input->HalfHeight;
  754. output->mRadius = input->Radius;
  755. output->mConvexRadius = input->ConvexRadius;
  756. }
  757. JPC_API void JPC_CylinderShapeSettings_default(JPC_CylinderShapeSettings* object) {
  758. object->UserData = 0;
  759. // TODO: Material
  760. object->Density = 1000.0;
  761. object->HalfHeight = 0.0;
  762. object->Radius = 0.0;
  763. object->ConvexRadius = 0.0;
  764. }
  765. JPC_API bool JPC_CylinderShapeSettings_Create(const JPC_CylinderShapeSettings* self, JPC_Shape** outShape, JPC_String** outError) {
  766. JPH::CylinderShapeSettings settings;
  767. to_jph(self, &settings);
  768. return HandleShapeResult(settings.Create(), outShape, outError);
  769. }
  770. ////////////////////////////////////////////////////////////////////////////////
  771. // ConvexHullShapeSettings
  772. static void to_jph(const JPC_ConvexHullShapeSettings* input, JPH::ConvexHullShapeSettings* output) {
  773. output->mUserData = input->UserData;
  774. // TODO: Material
  775. output->mDensity = input->Density;
  776. output->mPoints = to_jph(input->Points, input->PointsLen);
  777. output->mMaxConvexRadius = input->MaxConvexRadius;
  778. output->mMaxErrorConvexRadius = input->MaxErrorConvexRadius;
  779. output->mHullTolerance = input->HullTolerance;
  780. }
  781. JPC_API void JPC_ConvexHullShapeSettings_default(JPC_ConvexHullShapeSettings* object) {
  782. object->UserData = 0;
  783. // TODO: Material
  784. object->Density = 1000.0;
  785. object->Points = nullptr;
  786. object->PointsLen = 0;
  787. object->MaxConvexRadius = 0.0;
  788. object->MaxErrorConvexRadius = 0.05f;
  789. object->HullTolerance = 1.0e-3f;
  790. }
  791. JPC_API bool JPC_ConvexHullShapeSettings_Create(const JPC_ConvexHullShapeSettings* self, JPC_Shape** outShape, JPC_String** outError) {
  792. JPH::ConvexHullShapeSettings settings;
  793. to_jph(self, &settings);
  794. return HandleShapeResult(settings.Create(), outShape, outError);
  795. }
  796. ////////////////////////////////////////////////////////////////////////////////
  797. // CompoundShape::SubShapeSettings
  798. static JPH::CompoundShapeSettings::SubShapeSettings to_jph(const JPC_SubShapeSettings* input) {
  799. const JPH::Shape* shape = to_jph(input->Shape);
  800. JPH::CompoundShapeSettings::SubShapeSettings output;
  801. output.mShape = nullptr;
  802. output.mShapePtr = shape;
  803. output.mPosition = to_jph(input->Position);
  804. output.mRotation = to_jph(input->Rotation);
  805. output.mUserData = input->UserData;
  806. return output;
  807. }
  808. static JPH::Array<JPH::CompoundShapeSettings::SubShapeSettings> to_jph(const JPC_SubShapeSettings* src, size_t n) {
  809. JPH::Array<JPH::CompoundShapeSettings::SubShapeSettings> vec;
  810. vec.reserve(n);
  811. for (size_t i = 0; i < n; i++) {
  812. vec.push_back(to_jph(&src[i]));
  813. }
  814. return vec;
  815. }
  816. JPC_API void JPC_SubShapeSettings_default(JPC_SubShapeSettings* object) {
  817. object->Shape = nullptr;
  818. object->Position = JPC_Vec3{0};
  819. object->Rotation = JPC_Quat{0, 0, 0, 1};
  820. object->UserData = 0;
  821. }
  822. ////////////////////////////////////////////////////////////////////////////////
  823. // StaticCompoundShapeSettings -> CompoundShapeSettings -> ShapeSettings
  824. static void to_jph(const JPC_StaticCompoundShapeSettings* input, JPH::StaticCompoundShapeSettings* output) {
  825. output->mUserData = input->UserData;
  826. output->mSubShapes = to_jph(input->SubShapes, input->SubShapesLen);
  827. }
  828. JPC_API void JPC_StaticCompoundShapeSettings_default(JPC_StaticCompoundShapeSettings* object) {
  829. object->UserData = 0;
  830. object->SubShapes = nullptr;
  831. object->SubShapesLen = 0;
  832. }
  833. JPC_API bool JPC_StaticCompoundShapeSettings_Create(const JPC_StaticCompoundShapeSettings* self, JPC_Shape** outShape, JPC_String** outError) {
  834. JPH::StaticCompoundShapeSettings settings;
  835. to_jph(self, &settings);
  836. return HandleShapeResult(settings.Create(), outShape, outError);
  837. }
  838. ////////////////////////////////////////////////////////////////////////////////
  839. // MutableCompoundShapeSettings -> CompoundShapeSettings -> ShapeSettings
  840. static void to_jph(const JPC_MutableCompoundShapeSettings* input, JPH::MutableCompoundShapeSettings* output) {
  841. output->mUserData = input->UserData;
  842. output->mSubShapes = to_jph(input->SubShapes, input->SubShapesLen);
  843. }
  844. JPC_API void JPC_MutableCompoundShapeSettings_default(JPC_MutableCompoundShapeSettings* object) {
  845. object->UserData = 0;
  846. object->SubShapes = nullptr;
  847. object->SubShapesLen = 0;
  848. }
  849. JPC_API bool JPC_MutableCompoundShapeSettings_Create(const JPC_MutableCompoundShapeSettings* self, JPC_Shape** outShape, JPC_String** outError) {
  850. JPH::MutableCompoundShapeSettings settings;
  851. to_jph(self, &settings);
  852. return HandleShapeResult(settings.Create(), outShape, outError);
  853. }
  854. ////////////////////////////////////////////////////////////////////////////////
  855. // BodyCreationSettings
  856. static JPH::BodyCreationSettings to_jph(const JPC_BodyCreationSettings* settings) {
  857. JPH::BodyCreationSettings output{};
  858. output.mPosition = to_jph(settings->Position);
  859. output.mRotation = to_jph(settings->Rotation);
  860. output.mLinearVelocity = to_jph(settings->LinearVelocity);
  861. output.mAngularVelocity = to_jph(settings->AngularVelocity);
  862. output.mUserData = settings->UserData;
  863. output.mObjectLayer = settings->ObjectLayer;
  864. // CollisionGroup
  865. output.mMotionType = static_cast<JPH::EMotionType>(settings->MotionType);
  866. output.mAllowedDOFs = to_jph(settings->AllowedDOFs);
  867. output.mAllowDynamicOrKinematic = settings->AllowDynamicOrKinematic;
  868. output.mIsSensor = settings->IsSensor;
  869. output.mCollideKinematicVsNonDynamic = settings->CollideKinematicVsNonDynamic;
  870. output.mUseManifoldReduction = settings->UseManifoldReduction;
  871. output.mApplyGyroscopicForce = settings->ApplyGyroscopicForce;
  872. output.mMotionQuality = to_jph(settings->MotionQuality);
  873. output.mEnhancedInternalEdgeRemoval = settings->EnhancedInternalEdgeRemoval;
  874. output.mAllowSleeping = settings->AllowSleeping;
  875. output.mFriction = settings->Friction;
  876. output.mRestitution = settings->Restitution;
  877. output.mLinearDamping = settings->LinearDamping;
  878. output.mAngularDamping = settings->AngularDamping;
  879. output.mMaxLinearVelocity = settings->MaxLinearVelocity;
  880. output.mMaxAngularVelocity = settings->MaxAngularVelocity;
  881. output.mGravityFactor = settings->GravityFactor;
  882. output.mNumVelocityStepsOverride = settings->NumVelocityStepsOverride;
  883. output.mNumPositionStepsOverride = settings->NumPositionStepsOverride;
  884. output.mOverrideMassProperties = to_jph(settings->OverrideMassProperties);
  885. output.mInertiaMultiplier = settings->InertiaMultiplier;
  886. // output.mMassPropertiesOverride = settings->MassPropertiesOverride;
  887. output.SetShape(to_jph(settings->Shape));
  888. return output;
  889. }
  890. JPC_API void JPC_BodyCreationSettings_default(JPC_BodyCreationSettings* settings) {
  891. JPH::BodyCreationSettings defaultSettings{};
  892. settings->Position = to_jpc(defaultSettings.mPosition);
  893. settings->Rotation = to_jpc(defaultSettings.mRotation);
  894. settings->LinearVelocity = to_jpc(defaultSettings.mLinearVelocity);
  895. settings->AngularVelocity = to_jpc(defaultSettings.mAngularVelocity);
  896. settings->UserData = defaultSettings.mUserData;
  897. settings->ObjectLayer = defaultSettings.mObjectLayer;
  898. // CollisionGroup
  899. settings->MotionType = static_cast<JPC_MotionType>(defaultSettings.mMotionType);
  900. settings->AllowedDOFs = to_jpc(defaultSettings.mAllowedDOFs);
  901. settings->AllowDynamicOrKinematic = defaultSettings.mAllowDynamicOrKinematic;
  902. settings->IsSensor = defaultSettings.mIsSensor;
  903. settings->CollideKinematicVsNonDynamic = defaultSettings.mCollideKinematicVsNonDynamic;
  904. settings->UseManifoldReduction = defaultSettings.mUseManifoldReduction;
  905. settings->ApplyGyroscopicForce = defaultSettings.mApplyGyroscopicForce;
  906. settings->MotionQuality = to_jpc(defaultSettings.mMotionQuality);
  907. settings->EnhancedInternalEdgeRemoval = defaultSettings.mEnhancedInternalEdgeRemoval;
  908. settings->AllowSleeping = defaultSettings.mAllowSleeping;
  909. settings->Friction = defaultSettings.mFriction;
  910. settings->Restitution = defaultSettings.mRestitution;
  911. settings->LinearDamping = defaultSettings.mLinearDamping;
  912. settings->AngularDamping = defaultSettings.mAngularDamping;
  913. settings->MaxLinearVelocity = defaultSettings.mMaxLinearVelocity;
  914. settings->MaxAngularVelocity = defaultSettings.mMaxAngularVelocity;
  915. settings->GravityFactor = defaultSettings.mGravityFactor;
  916. settings->NumVelocityStepsOverride = defaultSettings.mNumVelocityStepsOverride;
  917. settings->NumPositionStepsOverride = defaultSettings.mNumPositionStepsOverride;
  918. settings->OverrideMassProperties = to_jpc(defaultSettings.mOverrideMassProperties);
  919. settings->InertiaMultiplier = defaultSettings.mInertiaMultiplier;
  920. // MassPropertiesOverride
  921. }
  922. ////////////////////////////////////////////////////////////////////////////////
  923. // Body
  924. JPC_API JPC_BodyID JPC_Body_GetID(const JPC_Body* self) {
  925. return to_jpc(to_jph(self)->GetID());
  926. }
  927. JPC_API JPC_BodyType JPC_Body_GetBodyType(const JPC_Body* self) {
  928. return to_jpc(to_jph(self)->GetBodyType());
  929. }
  930. JPC_API bool JPC_Body_IsRigidBody(const JPC_Body* self) {
  931. return to_jph(self)->IsRigidBody();
  932. }
  933. JPC_API bool JPC_Body_IsSoftBody(const JPC_Body* self) {
  934. return to_jph(self)->IsSoftBody();
  935. }
  936. JPC_API bool JPC_Body_IsActive(const JPC_Body* self) {
  937. return to_jph(self)->IsActive();
  938. }
  939. JPC_API bool JPC_Body_IsStatic(const JPC_Body* self) {
  940. return to_jph(self)->IsStatic();
  941. }
  942. JPC_API bool JPC_Body_IsKinematic(const JPC_Body* self) {
  943. return to_jph(self)->IsKinematic();
  944. }
  945. JPC_API bool JPC_Body_IsDynamic(const JPC_Body* self) {
  946. return to_jph(self)->IsDynamic();
  947. }
  948. JPC_API bool JPC_Body_CanBeKinematicOrDynamic(const JPC_Body* self) {
  949. return to_jph(self)->CanBeKinematicOrDynamic();
  950. }
  951. JPC_API void JPC_Body_SetIsSensor(JPC_Body* self, bool inIsSensor) {
  952. to_jph(self)->SetIsSensor(inIsSensor);
  953. }
  954. JPC_API bool JPC_Body_IsSensor(const JPC_Body* self) {
  955. return to_jph(self)->IsSensor();
  956. }
  957. JPC_API void JPC_Body_SetCollideKinematicVsNonDynamic(JPC_Body* self, bool inCollide) {
  958. to_jph(self)->SetCollideKinematicVsNonDynamic(inCollide);
  959. }
  960. JPC_API bool JPC_Body_GetCollideKinematicVsNonDynamic(const JPC_Body* self) {
  961. return to_jph(self)->GetCollideKinematicVsNonDynamic();
  962. }
  963. JPC_API void JPC_Body_SetUseManifoldReduction(JPC_Body* self, bool inUseReduction) {
  964. to_jph(self)->SetUseManifoldReduction(inUseReduction);
  965. }
  966. JPC_API bool JPC_Body_GetUseManifoldReduction(const JPC_Body* self) {
  967. return to_jph(self)->GetUseManifoldReduction();
  968. }
  969. JPC_API bool JPC_Body_GetUseManifoldReductionWithBody(const JPC_Body* self, const JPC_Body* inBody2) {
  970. return to_jph(self)->GetUseManifoldReductionWithBody(*to_jph(inBody2));
  971. }
  972. JPC_API void JPC_Body_SetApplyGyroscopicForce(JPC_Body* self, bool inApply) {
  973. to_jph(self)->SetApplyGyroscopicForce(inApply);
  974. }
  975. JPC_API bool JPC_Body_GetApplyGyroscopicForce(const JPC_Body* self) {
  976. return to_jph(self)->GetApplyGyroscopicForce();
  977. }
  978. JPC_API void JPC_Body_SetEnhancedInternalEdgeRemoval(JPC_Body* self, bool inApply) {
  979. to_jph(self)->SetEnhancedInternalEdgeRemoval(inApply);
  980. }
  981. JPC_API bool JPC_Body_GetEnhancedInternalEdgeRemoval(const JPC_Body* self) {
  982. return to_jph(self)->GetEnhancedInternalEdgeRemoval();
  983. }
  984. JPC_API bool JPC_Body_GetEnhancedInternalEdgeRemovalWithBody(const JPC_Body* self, const JPC_Body* inBody2) {
  985. return to_jph(self)->GetEnhancedInternalEdgeRemovalWithBody(*to_jph(inBody2));
  986. }
  987. JPC_API JPC_MotionType JPC_Body_GetMotionType(const JPC_Body* self) {
  988. return static_cast<JPC_MotionType>(to_jph(self)->GetMotionType());
  989. }
  990. JPC_API void JPC_Body_SetMotionType(JPC_Body* self, JPC_MotionType inMotionType) {
  991. to_jph(self)->SetMotionType(static_cast<JPH::EMotionType>(inMotionType));
  992. }
  993. JPC_API JPC_BroadPhaseLayer JPC_Body_GetBroadPhaseLayer(const JPC_Body* self) {
  994. return to_jpc(to_jph(self)->GetBroadPhaseLayer());
  995. }
  996. JPC_API JPC_ObjectLayer JPC_Body_GetObjectLayer(const JPC_Body* self) {
  997. return to_jph(self)->GetObjectLayer();
  998. }
  999. // JPC_API const CollisionGroup & JPC_Body_GetCollisionGroup(const JPC_Body* self);
  1000. // JPC_API CollisionGroup & JPC_Body_GetCollisionGroup(JPC_Body* self);
  1001. // JPC_API void JPC_Body_SetCollisionGroup(JPC_Body* self, const CollisionGroup &inGroup);
  1002. JPC_API bool JPC_Body_GetAllowSleeping(const JPC_Body* self) {
  1003. return to_jph(self)->GetAllowSleeping();
  1004. }
  1005. JPC_API void JPC_Body_SetAllowSleeping(JPC_Body* self, bool inAllow) {
  1006. to_jph(self)->SetAllowSleeping(inAllow);
  1007. }
  1008. JPC_API void JPC_Body_ResetSleepTimer(JPC_Body* self) {
  1009. to_jph(self)->ResetSleepTimer();
  1010. }
  1011. JPC_API float JPC_Body_GetFriction(const JPC_Body* self) {
  1012. return to_jph(self)->GetFriction();
  1013. }
  1014. JPC_API void JPC_Body_SetFriction(JPC_Body* self, float inFriction) {
  1015. to_jph(self)->SetFriction(inFriction);
  1016. }
  1017. JPC_API float JPC_Body_GetRestitution(const JPC_Body* self) {
  1018. return to_jph(self)->GetRestitution();
  1019. }
  1020. JPC_API void JPC_Body_SetRestitution(JPC_Body* self, float inRestitution) {
  1021. to_jph(self)->SetRestitution(inRestitution);
  1022. }
  1023. JPC_API JPC_Vec3 JPC_Body_GetLinearVelocity(const JPC_Body* self) {
  1024. return to_jpc(to_jph(self)->GetLinearVelocity());
  1025. }
  1026. JPC_API void JPC_Body_SetLinearVelocity(JPC_Body* self, JPC_Vec3 inLinearVelocity) {
  1027. to_jph(self)->SetLinearVelocity(to_jph(inLinearVelocity));
  1028. }
  1029. JPC_API void JPC_Body_SetLinearVelocityClamped(JPC_Body* self, JPC_Vec3 inLinearVelocity) {
  1030. to_jph(self)->SetLinearVelocityClamped(to_jph(inLinearVelocity));
  1031. }
  1032. JPC_API JPC_Vec3 JPC_Body_GetAngularVelocity(const JPC_Body* self) {
  1033. return to_jpc(to_jph(self)->GetAngularVelocity());
  1034. }
  1035. JPC_API void JPC_Body_SetAngularVelocity(JPC_Body* self, JPC_Vec3 inAngularVelocity) {
  1036. to_jph(self)->SetAngularVelocity(to_jph(inAngularVelocity));
  1037. }
  1038. JPC_API void JPC_Body_SetAngularVelocityClamped(JPC_Body* self, JPC_Vec3 inAngularVelocity) {
  1039. to_jph(self)->SetAngularVelocityClamped(to_jph(inAngularVelocity));
  1040. }
  1041. JPC_API JPC_Vec3 JPC_Body_GetPointVelocityCOM(const JPC_Body* self, JPC_Vec3 inPointRelativeToCOM) {
  1042. return to_jpc(to_jph(self)->GetPointVelocityCOM(to_jph(inPointRelativeToCOM)));
  1043. }
  1044. JPC_API JPC_Vec3 JPC_Body_GetPointVelocity(const JPC_Body* self, JPC_RVec3 inPoint) {
  1045. return to_jpc(to_jph(self)->GetPointVelocity(to_jph(inPoint)));
  1046. }
  1047. JPC_API void JPC_Body_AddForce(JPC_Body* self, JPC_Vec3 inForce) {
  1048. to_jph(self)->AddForce(to_jph(inForce));
  1049. }
  1050. // overload of Body::AddForce
  1051. JPC_API void JPC_Body_AddForceAtPoint(JPC_Body* self, JPC_Vec3 inForce, JPC_RVec3 inPosition) {
  1052. to_jph(self)->AddForce(to_jph(inForce), to_jph(inPosition));
  1053. }
  1054. JPC_API void JPC_Body_AddTorque(JPC_Body* self, JPC_Vec3 inTorque) {
  1055. to_jph(self)->AddTorque(to_jph(inTorque));
  1056. }
  1057. JPC_API JPC_Vec3 JPC_Body_GetAccumulatedForce(const JPC_Body* self) {
  1058. return to_jpc(to_jph(self)->GetAccumulatedForce());
  1059. }
  1060. JPC_API JPC_Vec3 JPC_Body_GetAccumulatedTorque(const JPC_Body* self) {
  1061. return to_jpc(to_jph(self)->GetAccumulatedTorque());
  1062. }
  1063. JPC_API void JPC_Body_ResetForce(JPC_Body* self) {
  1064. to_jph(self)->ResetForce();
  1065. }
  1066. JPC_API void JPC_Body_ResetTorque(JPC_Body* self) {
  1067. to_jph(self)->ResetTorque();
  1068. }
  1069. JPC_API void JPC_Body_ResetMotion(JPC_Body* self) {
  1070. to_jph(self)->ResetMotion();
  1071. }
  1072. JPC_API void JPC_Body_GetInverseInertia(const JPC_Body* self, JPC_Mat44* outMatrix) {
  1073. to_jph(self)->GetInverseInertia().StoreFloat4x4(reinterpret_cast<JPH::Float4*>(outMatrix));
  1074. }
  1075. JPC_API void JPC_Body_AddImpulse(JPC_Body* self, JPC_Vec3 inImpulse) {
  1076. to_jph(self)->AddImpulse(to_jph(inImpulse));
  1077. }
  1078. JPC_API void JPC_Body_AddImpulse2(JPC_Body* self, JPC_Vec3 inImpulse, JPC_RVec3 inPosition) {
  1079. to_jph(self)->AddImpulse(to_jph(inImpulse), to_jph(inPosition));
  1080. }
  1081. JPC_API void JPC_Body_AddAngularImpulse(JPC_Body* self, JPC_Vec3 inAngularImpulse) {
  1082. to_jph(self)->AddAngularImpulse(to_jph(inAngularImpulse));
  1083. }
  1084. JPC_API void JPC_Body_MoveKinematic(JPC_Body* self, JPC_RVec3 inTargetPosition, JPC_Quat inTargetRotation, float inDeltaTime) {
  1085. to_jph(self)->MoveKinematic(to_jph(inTargetPosition), to_jph(inTargetRotation), inDeltaTime);
  1086. }
  1087. JPC_API bool JPC_Body_ApplyBuoyancyImpulse(JPC_Body* self, JPC_RVec3 inSurfacePosition, JPC_Vec3 inSurfaceNormal, float inBuoyancy, float inLinearDrag, float inAngularDrag, JPC_Vec3 inFluidVelocity, JPC_Vec3 inGravity, float inDeltaTime) {
  1088. return to_jph(self)->ApplyBuoyancyImpulse(to_jph(inSurfacePosition), to_jph(inSurfaceNormal), inBuoyancy, inLinearDrag, inAngularDrag, to_jph(inFluidVelocity), to_jph(inGravity), inDeltaTime);
  1089. }
  1090. JPC_API bool JPC_Body_IsInBroadPhase(const JPC_Body* self) {
  1091. return to_jph(self)->IsInBroadPhase();
  1092. }
  1093. JPC_API bool JPC_Body_IsCollisionCacheInvalid(const JPC_Body* self) {
  1094. return to_jph(self)->IsCollisionCacheInvalid();
  1095. }
  1096. JPC_API const JPC_Shape* JPC_Body_GetShape(const JPC_Body* self) {
  1097. return to_jpc(to_jph(self)->GetShape());
  1098. }
  1099. JPC_API JPC_RVec3 JPC_Body_GetPosition(const JPC_Body* self) {
  1100. return to_jpc(to_jph(self)->GetPosition());
  1101. }
  1102. JPC_API JPC_Quat JPC_Body_GetRotation(const JPC_Body* self) {
  1103. return to_jpc(to_jph(self)->GetRotation());
  1104. }
  1105. JPC_API JPC_RMat44 JPC_Body_GetWorldTransform(const JPC_Body* self) {
  1106. return to_jpc(to_jph(self)->GetWorldTransform());
  1107. }
  1108. JPC_API JPC_RVec3 JPC_Body_GetCenterOfMassPosition(const JPC_Body* self) {
  1109. return to_jpc(to_jph(self)->GetCenterOfMassPosition());
  1110. }
  1111. JPC_API JPC_RMat44 JPC_Body_GetCenterOfMassTransform(const JPC_Body* self) {
  1112. return to_jpc(to_jph(self)->GetCenterOfMassTransform());
  1113. }
  1114. JPC_API JPC_RMat44 JPC_Body_GetInverseCenterOfMassTransform(const JPC_Body* self) {
  1115. return to_jpc(to_jph(self)->GetInverseCenterOfMassTransform());
  1116. }
  1117. // JPC_API const AABox & JPC_Body_GetWorldSpaceBounds(const JPC_Body* self);
  1118. // JPC_API const MotionProperties *JPC_Body_GetMotionProperties(const JPC_Body* self)
  1119. // JPC_API MotionProperties * JPC_Body_GetMotionProperties(JPC_Body* self);
  1120. // JPC_API const MotionProperties *JPC_Body_GetMotionPropertiesUnchecked(const JPC_Body* self)
  1121. // JPC_API MotionProperties * JPC_Body_GetMotionPropertiesUnchecked(JPC_Body* self);
  1122. JPC_API uint64_t JPC_Body_GetUserData(const JPC_Body* self) {
  1123. return to_jph(self)->GetUserData();
  1124. }
  1125. JPC_API void JPC_Body_SetUserData(JPC_Body* self, uint64_t inUserData) {
  1126. to_jph(self)->SetUserData(inUserData);
  1127. }
  1128. // JPC_API JPC_Vec3 JPC_Body_GetWorldSpaceSurfaceNormal(const JPC_Body* self, const SubShapeID &inSubShapeID, JPC_RVec3 inPosition);
  1129. // JPC_API TransformedShape JPC_Body_GetTransformedShape(const JPC_Body* self);
  1130. // JPC_API BodyCreationSettings JPC_Body_GetBodyCreationSettings(const JPC_Body* self);
  1131. // JPC_API SoftBodyCreationSettings JPC_Body_GetSoftBodyCreationSettings(const JPC_Body* self);
  1132. ////////////////////////////////////////////////////////////////////////////////
  1133. // BodyLockRead
  1134. JPC_API JPC_BodyLockRead* JPC_BodyLockRead_new(const JPC_BodyLockInterface* interface, JPC_BodyID bodyID) {
  1135. JPH::BodyLockRead* lockRead = new JPH::BodyLockRead(*to_jph(interface), to_jph(bodyID));
  1136. return to_jpc(lockRead);
  1137. }
  1138. JPC_API void JPC_BodyLockRead_delete(JPC_BodyLockRead* self) {
  1139. delete to_jph(self);
  1140. }
  1141. JPC_API bool JPC_BodyLockRead_Succeeded(JPC_BodyLockRead* self) {
  1142. return to_jph(self)->Succeeded();
  1143. }
  1144. JPC_API const JPC_Body* JPC_BodyLockRead_GetBody(JPC_BodyLockRead* self) {
  1145. return to_jpc(&to_jph(self)->GetBody());
  1146. }
  1147. ////////////////////////////////////////////////////////////////////////////////
  1148. // BodyLockWrite
  1149. JPC_API JPC_BodyLockWrite* JPC_BodyLockWrite_new(const JPC_BodyLockInterface* interface, JPC_BodyID bodyID) {
  1150. JPH::BodyLockWrite* lockWrite = new JPH::BodyLockWrite(*to_jph(interface), to_jph(bodyID));
  1151. return to_jpc(lockWrite);
  1152. }
  1153. JPC_API void JPC_BodyLockWrite_delete(JPC_BodyLockWrite* self) {
  1154. delete to_jph(self);
  1155. }
  1156. JPC_API bool JPC_BodyLockWrite_Succeeded(JPC_BodyLockWrite* self) {
  1157. return to_jph(self)->Succeeded();
  1158. }
  1159. JPC_API JPC_Body* JPC_BodyLockWrite_GetBody(JPC_BodyLockWrite* self) {
  1160. return to_jpc(&to_jph(self)->GetBody());
  1161. }
  1162. ////////////////////////////////////////////////////////////////////////////////
  1163. // BodyLockMultiRead
  1164. typedef struct JPC_BodyLockMultiRead JPC_BodyLockMultiRead;
  1165. JPC_API JPC_BodyLockMultiRead* JPC_BodyLockMultiRead_new(
  1166. const JPC_BodyLockInterface* interface,
  1167. const JPC_BodyID *inBodyIDs,
  1168. int inNumber)
  1169. {
  1170. JPH::BodyLockMultiRead* lockRead = new JPH::BodyLockMultiRead(*to_jph(interface), to_jph(inBodyIDs), inNumber);
  1171. return to_jpc(lockRead);
  1172. }
  1173. JPC_API void JPC_BodyLockMultiRead_delete(JPC_BodyLockMultiRead* self) {
  1174. delete to_jph(self);
  1175. }
  1176. JPC_API const JPC_Body* JPC_BodyLockMultiRead_GetBody(JPC_BodyLockMultiRead* self, int inBodyIndex) {
  1177. return to_jpc(to_jph(self)->GetBody(inBodyIndex));
  1178. }
  1179. ////////////////////////////////////////////////////////////////////////////////
  1180. // BodyLockMultiWrite
  1181. typedef struct JPC_BodyLockMultiWrite JPC_BodyLockMultiWrite;
  1182. JPC_API JPC_BodyLockMultiWrite* JPC_BodyLockMultiWrite_new(
  1183. const JPC_BodyLockInterface* interface,
  1184. const JPC_BodyID *inBodyIDs,
  1185. int inNumber)
  1186. {
  1187. JPH::BodyLockMultiWrite* lockWrite = new JPH::BodyLockMultiWrite(*to_jph(interface), to_jph(inBodyIDs), inNumber);
  1188. return to_jpc(lockWrite);
  1189. }
  1190. JPC_API void JPC_BodyLockMultiWrite_delete(JPC_BodyLockMultiWrite* self) {
  1191. delete to_jph(self);
  1192. }
  1193. JPC_API JPC_Body* JPC_BodyLockMultiWrite_GetBody(JPC_BodyLockMultiWrite* self, int inBodyIndex) {
  1194. return to_jpc(to_jph(self)->GetBody(inBodyIndex));
  1195. }
  1196. ////////////////////////////////////////////////////////////////////////////////
  1197. // BodyInterface
  1198. JPC_API JPC_Body* JPC_BodyInterface_CreateBody(JPC_BodyInterface* self, const JPC_BodyCreationSettings* inSettings) {
  1199. return to_jpc(to_jph(self)->CreateBody(to_jph(inSettings)));
  1200. }
  1201. // JPC_API JPC_Body* JPC_BodyInterface_CreateSoftBody(JPC_BodyInterface *self, const SoftBodyCreationSettings &inSettings);
  1202. JPC_API JPC_Body* JPC_BodyInterface_CreateBodyWithID(JPC_BodyInterface *self, JPC_BodyID inBodyID, const JPC_BodyCreationSettings* inSettings) {
  1203. return to_jpc(to_jph(self)->CreateBodyWithID(to_jph(inBodyID), to_jph(inSettings)));
  1204. }
  1205. // JPC_API JPC_Body* JPC_BodyInterface_CreateSoftBodyWithID(JPC_BodyInterface *self, JPC_BodyID inBodyID, const SoftBodyCreationSettings* inSettings);
  1206. JPC_API JPC_Body* JPC_BodyInterface_CreateBodyWithoutID(const JPC_BodyInterface *self, const JPC_BodyCreationSettings* inSettings) {
  1207. return to_jpc(to_jph(self)->CreateBodyWithoutID(to_jph(inSettings)));
  1208. }
  1209. // JPC_API JPC_Body* JPC_BodyInterface_CreateSoftBodyWithoutID(const JPC_BodyInterface *self, const SoftBodyCreationSettings* inSettings);
  1210. JPC_API void JPC_BodyInterface_DestroyBodyWithoutID(const JPC_BodyInterface *self, JPC_Body *inBody) {
  1211. to_jph(self)->DestroyBodyWithoutID(to_jph(inBody));
  1212. }
  1213. JPC_API bool JPC_BodyInterface_AssignBodyID(JPC_BodyInterface *self, JPC_Body *ioBody) {
  1214. return to_jph(self)->AssignBodyID(to_jph(ioBody));
  1215. }
  1216. // JPC_API bool JPC_BodyInterface_AssignBodyID(JPC_BodyInterface *self, JPC_Body *ioBody, JPC_BodyID inBodyID);
  1217. JPC_API JPC_Body* JPC_BodyInterface_UnassignBodyID(JPC_BodyInterface *self, JPC_BodyID inBodyID) {
  1218. return to_jpc(to_jph(self)->UnassignBodyID(to_jph(inBodyID)));
  1219. }
  1220. // JPC_API void JPC_BodyInterface_UnassignBodyIDs(JPC_BodyInterface *self, const JPC_BodyID *inBodyIDs, int inNumber, JPC_Body **outBodies) {
  1221. // return to_jph(self)->UnassignBodyIDs(to_jph(inBodyIDs), inNumber, to_jph(outBodies));
  1222. // }
  1223. JPC_API void JPC_BodyInterface_DestroyBody(JPC_BodyInterface *self, JPC_BodyID inBodyID) {
  1224. to_jph(self)->DestroyBody(to_jph(inBodyID));
  1225. }
  1226. // JPC_API void JPC_BodyInterface_DestroyBodies(JPC_BodyInterface *self, const JPC_BodyID *inBodyIDs, int inNumber) {
  1227. // return to_jph(self)->DestroyBodies(to_jph(inBodyIDs), int inNumber);
  1228. // }
  1229. JPC_API void JPC_BodyInterface_AddBody(JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_Activation inActivationMode) {
  1230. to_jph(self)->AddBody(to_jph(inBodyID), to_jph(inActivationMode));
  1231. }
  1232. JPC_API void JPC_BodyInterface_RemoveBody(JPC_BodyInterface *self, JPC_BodyID inBodyID) {
  1233. to_jph(self)->RemoveBody(to_jph(inBodyID));
  1234. }
  1235. JPC_API bool JPC_BodyInterface_IsAdded(const JPC_BodyInterface *self, JPC_BodyID inBodyID) {
  1236. return to_jph(self)->IsAdded(to_jph(inBodyID));
  1237. }
  1238. JPC_API JPC_BodyID JPC_BodyInterface_CreateAndAddBody(JPC_BodyInterface *self, const JPC_BodyCreationSettings* inSettings, JPC_Activation inActivationMode) {
  1239. return to_jpc(to_jph(self)->CreateAndAddBody(to_jph(inSettings), to_jph(inActivationMode)));
  1240. }
  1241. // JPC_API JPC_BodyID JPC_BodyInterface_CreateAndAddSoftBody(JPC_BodyInterface *self, const SoftBodyCreationSettings &inSettings, JPC_Activation inActivationMode);
  1242. JPC_API void* JPC_BodyInterface_AddBodiesPrepare(JPC_BodyInterface *self, JPC_BodyID *ioBodies, int inNumber) {
  1243. return to_jph(self)->AddBodiesPrepare(to_jph(ioBodies), inNumber);
  1244. }
  1245. JPC_API void JPC_BodyInterface_AddBodiesFinalize(JPC_BodyInterface *self, JPC_BodyID *ioBodies, int inNumber, void* inAddState, JPC_Activation inActivationMode) {
  1246. to_jph(self)->AddBodiesFinalize(to_jph(ioBodies), inNumber, inAddState, to_jph(inActivationMode));
  1247. }
  1248. JPC_API void JPC_BodyInterface_AddBodiesAbort(JPC_BodyInterface *self, JPC_BodyID *ioBodies, int inNumber, void* inAddState) {
  1249. to_jph(self)->AddBodiesAbort(to_jph(ioBodies), inNumber, inAddState);
  1250. }
  1251. JPC_API void JPC_BodyInterface_RemoveBodies(JPC_BodyInterface *self, JPC_BodyID *ioBodies, int inNumber) {
  1252. to_jph(self)->RemoveBodies(to_jph(ioBodies), inNumber);
  1253. }
  1254. JPC_API void JPC_BodyInterface_ActivateBody(JPC_BodyInterface *self, JPC_BodyID inBodyID) {
  1255. to_jph(self)->ActivateBody(to_jph(inBodyID));
  1256. }
  1257. JPC_API void JPC_BodyInterface_ActivateBodies(JPC_BodyInterface *self, JPC_BodyID *inBodyIDs, int inNumber) {
  1258. to_jph(self)->ActivateBodies(to_jph(inBodyIDs), inNumber);
  1259. }
  1260. // JPC_API void JPC_BodyInterface_ActivateBodiesInAABox(JPC_BodyInterface *self, const AABox &inBox, const BroadPhaseLayerFilter &inBroadPhaseLayerFilter, const ObjectLayerFilter &inObjectLayerFilter);
  1261. JPC_API void JPC_BodyInterface_DeactivateBody(JPC_BodyInterface *self, JPC_BodyID inBodyID) {
  1262. to_jph(self)->DeactivateBody(to_jph(inBodyID));
  1263. }
  1264. JPC_API void JPC_BodyInterface_DeactivateBodies(JPC_BodyInterface *self, JPC_BodyID *inBodyIDs, int inNumber) {
  1265. to_jph(self)->DeactivateBodies(to_jph(inBodyIDs), inNumber);
  1266. }
  1267. JPC_API bool JPC_BodyInterface_IsActive(const JPC_BodyInterface *self, JPC_BodyID inBodyID) {
  1268. return to_jph(self)->IsActive(to_jph(inBodyID));
  1269. }
  1270. // TwoBodyConstraint * JPC_BodyInterface_CreateConstraint(JPC_BodyInterface *self, const TwoBodyConstraintSettings *inSettings, JPC_BodyID inBodyID1, JPC_BodyID inBodyID2);
  1271. // JPC_API void JPC_BodyInterface_ActivateConstraint(JPC_BodyInterface *self, const TwoBodyConstraint *inConstraint);
  1272. JPC_API const JPC_Shape* JPC_BodyInterface_GetShape(const JPC_BodyInterface *self, JPC_BodyID inBodyID) {
  1273. // NOTE: This pointer will only be alive as long as BodyInterface holds onto it!
  1274. return to_jpc(to_jph(self)->GetShape(to_jph(inBodyID)).GetPtr());
  1275. }
  1276. JPC_API void JPC_BodyInterface_SetShape(const JPC_BodyInterface *self, JPC_BodyID inBodyID, const JPC_Shape *inShape, bool inUpdateMassProperties, JPC_Activation inActivationMode) {
  1277. to_jph(self)->SetShape(to_jph(inBodyID), to_jph(inShape), inUpdateMassProperties, to_jph(inActivationMode));
  1278. }
  1279. JPC_API void JPC_BodyInterface_NotifyShapeChanged(const JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_Vec3 inPreviousCenterOfMass, bool inUpdateMassProperties, JPC_Activation inActivationMode) {
  1280. to_jph(self)->NotifyShapeChanged(to_jph(inBodyID), to_jph(inPreviousCenterOfMass), inUpdateMassProperties, to_jph(inActivationMode));
  1281. }
  1282. JPC_API void JPC_BodyInterface_SetObjectLayer(JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_ObjectLayer inLayer) {
  1283. to_jph(self)->SetObjectLayer(to_jph(inBodyID), inLayer);
  1284. }
  1285. JPC_API JPC_ObjectLayer JPC_BodyInterface_GetObjectLayer(const JPC_BodyInterface *self, JPC_BodyID inBodyID) {
  1286. return to_jph(self)->GetObjectLayer(to_jph(inBodyID));
  1287. }
  1288. JPC_API void JPC_BodyInterface_SetPositionAndRotation(JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_RVec3 inPosition, JPC_Quat inRotation, JPC_Activation inActivationMode) {
  1289. to_jph(self)->SetPositionAndRotation(to_jph(inBodyID), to_jph(inPosition), to_jph(inRotation), to_jph(inActivationMode));
  1290. }
  1291. JPC_API void JPC_BodyInterface_SetPositionAndRotationWhenChanged(JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_RVec3 inPosition, JPC_Quat inRotation, JPC_Activation inActivationMode) {
  1292. to_jph(self)->SetPositionAndRotationWhenChanged(to_jph(inBodyID), to_jph(inPosition), to_jph(inRotation), to_jph(inActivationMode));
  1293. }
  1294. JPC_API void JPC_BodyInterface_GetPositionAndRotation(const JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_RVec3 *outPosition, JPC_Quat *outRotation) {
  1295. JPH::RVec3 outPos{};
  1296. JPH::Quat outRot{};
  1297. to_jph(self)->GetPositionAndRotation(to_jph(inBodyID), outPos, outRot);
  1298. *outPosition = to_jpc(outPos);
  1299. *outRotation = to_jpc(outRot);
  1300. }
  1301. JPC_API void JPC_BodyInterface_SetPosition(JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_RVec3 inPosition, JPC_Activation inActivationMode) {
  1302. to_jph(self)->SetPosition(to_jph(inBodyID), to_jph(inPosition), to_jph(inActivationMode));
  1303. }
  1304. JPC_API JPC_RVec3 JPC_BodyInterface_GetPosition(const JPC_BodyInterface *self, JPC_BodyID inBodyID) {
  1305. return to_jpc(to_jph(self)->GetPosition(to_jph(inBodyID)));
  1306. }
  1307. JPC_API JPC_RVec3 JPC_BodyInterface_GetCenterOfMassPosition(const JPC_BodyInterface *self, JPC_BodyID inBodyID) {
  1308. return to_jpc(to_jph(self)->GetCenterOfMassPosition(to_jph(inBodyID)));
  1309. }
  1310. JPC_API void JPC_BodyInterface_SetRotation(JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_Quat inRotation, JPC_Activation inActivationMode) {
  1311. to_jph(self)->SetRotation(to_jph(inBodyID), to_jph(inRotation), to_jph(inActivationMode));
  1312. }
  1313. JPC_API JPC_Quat JPC_BodyInterface_GetRotation(const JPC_BodyInterface *self, JPC_BodyID inBodyID) {
  1314. return to_jpc(to_jph(self)->GetRotation(to_jph(inBodyID)));
  1315. }
  1316. JPC_API JPC_RMat44 JPC_BodyInterface_GetWorldTransform(const JPC_BodyInterface *self, JPC_BodyID inBodyID) {
  1317. return to_jpc(to_jph(self)->GetWorldTransform(to_jph(inBodyID)));
  1318. }
  1319. JPC_API JPC_RMat44 JPC_BodyInterface_GetCenterOfMassTransform(const JPC_BodyInterface *self, JPC_BodyID inBodyID) {
  1320. return to_jpc(to_jph(self)->GetCenterOfMassTransform(to_jph(inBodyID)));
  1321. }
  1322. JPC_API void JPC_BodyInterface_MoveKinematic(JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_RVec3 inTargetPosition, JPC_Quat inTargetRotation, float inDeltaTime) {
  1323. to_jph(self)->MoveKinematic(to_jph(inBodyID), to_jph(inTargetPosition), to_jph(inTargetRotation), inDeltaTime);
  1324. }
  1325. JPC_API void JPC_BodyInterface_SetLinearAndAngularVelocity(JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_Vec3 inLinearVelocity, JPC_Vec3 inAngularVelocity) {
  1326. to_jph(self)->SetLinearAndAngularVelocity(to_jph(inBodyID), to_jph(inLinearVelocity), to_jph(inAngularVelocity));
  1327. }
  1328. JPC_API void JPC_BodyInterface_GetLinearAndAngularVelocity(const JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_Vec3 *outLinearVelocity, JPC_Vec3 *outAngularVelocity) {
  1329. JPH::Vec3 outLinVel;
  1330. JPH::Vec3 outAngVel;
  1331. to_jph(self)->GetLinearAndAngularVelocity(to_jph(inBodyID), outLinVel, outAngVel);
  1332. *outLinearVelocity = to_jpc(outLinVel);
  1333. *outAngularVelocity = to_jpc(outAngVel);
  1334. }
  1335. JPC_API void JPC_BodyInterface_SetLinearVelocity(JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_Vec3 inLinearVelocity) {
  1336. to_jph(self)->SetLinearVelocity(to_jph(inBodyID), to_jph(inLinearVelocity));
  1337. }
  1338. JPC_API JPC_Vec3 JPC_BodyInterface_GetLinearVelocity(const JPC_BodyInterface *self, JPC_BodyID inBodyID) {
  1339. return to_jpc(to_jph(self)->GetLinearVelocity(to_jph(inBodyID)));
  1340. }
  1341. JPC_API void JPC_BodyInterface_AddLinearVelocity(JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_Vec3 inLinearVelocity) {
  1342. to_jph(self)->AddLinearVelocity(to_jph(inBodyID), to_jph(inLinearVelocity));
  1343. }
  1344. JPC_API void JPC_BodyInterface_AddLinearAndAngularVelocity(JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_Vec3 inLinearVelocity, JPC_Vec3 inAngularVelocity) {
  1345. to_jph(self)->AddLinearAndAngularVelocity(to_jph(inBodyID), to_jph(inLinearVelocity), to_jph(inAngularVelocity));
  1346. }
  1347. JPC_API void JPC_BodyInterface_SetAngularVelocity(JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_Vec3 inAngularVelocity) {
  1348. to_jph(self)->SetAngularVelocity(to_jph(inBodyID), to_jph(inAngularVelocity));
  1349. }
  1350. JPC_API JPC_Vec3 JPC_BodyInterface_GetAngularVelocity(const JPC_BodyInterface *self, JPC_BodyID inBodyID) {
  1351. return to_jpc(to_jph(self)->GetAngularVelocity(to_jph(inBodyID)));
  1352. }
  1353. JPC_API JPC_Vec3 JPC_BodyInterface_GetPointVelocity(const JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_RVec3 inPoint) {
  1354. return to_jpc(to_jph(self)->GetPointVelocity(to_jph(inBodyID), to_jph(inPoint)));
  1355. }
  1356. JPC_API void JPC_BodyInterface_SetPositionRotationAndVelocity(JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_RVec3 inPosition, JPC_Quat inRotation, JPC_Vec3 inLinearVelocity, JPC_Vec3 inAngularVelocity) {
  1357. to_jph(self)->SetPositionRotationAndVelocity(to_jph(inBodyID), to_jph(inPosition), to_jph(inRotation), to_jph(inLinearVelocity), to_jph(inAngularVelocity));
  1358. }
  1359. JPC_API void JPC_BodyInterface_AddForce(JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_Vec3 inForce) {
  1360. to_jph(self)->AddForce(to_jph(inBodyID), to_jph(inForce));
  1361. }
  1362. // overload of BodyInterface::AddForce
  1363. JPC_API void JPC_BodyInterface_AddForceAtPoint(JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_Vec3 inForce, JPC_RVec3 inPoint) {
  1364. to_jph(self)->AddForce(to_jph(inBodyID), to_jph(inForce), to_jph(inPoint));
  1365. }
  1366. JPC_API void JPC_BodyInterface_AddTorque(JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_Vec3 inTorque) {
  1367. to_jph(self)->AddTorque(to_jph(inBodyID), to_jph(inTorque));
  1368. }
  1369. JPC_API void JPC_BodyInterface_AddForceAndTorque(JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_Vec3 inForce, JPC_Vec3 inTorque) {
  1370. to_jph(self)->AddForceAndTorque(to_jph(inBodyID), to_jph(inForce), to_jph(inTorque));
  1371. }
  1372. JPC_API void JPC_BodyInterface_AddImpulse(JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_Vec3 inImpulse) {
  1373. to_jph(self)->AddImpulse(to_jph(inBodyID), to_jph(inImpulse));
  1374. }
  1375. JPC_API void JPC_BodyInterface_AddImpulse3(JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_Vec3 inImpulse, JPC_RVec3 inPoint) {
  1376. to_jph(self)->AddImpulse(to_jph(inBodyID), to_jph(inImpulse), to_jph(inPoint));
  1377. }
  1378. JPC_API void JPC_BodyInterface_AddAngularImpulse(JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_Vec3 inAngularImpulse) {
  1379. to_jph(self)->AddAngularImpulse(to_jph(inBodyID), to_jph(inAngularImpulse));
  1380. }
  1381. JPC_API JPC_BodyType JPC_BodyInterface_GetBodyType(const JPC_BodyInterface *self, JPC_BodyID inBodyID) {
  1382. return to_jpc(to_jph(self)->GetBodyType(to_jph(inBodyID)));
  1383. }
  1384. JPC_API void JPC_BodyInterface_SetMotionType(JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_MotionType inMotionType, JPC_Activation inActivationMode) {
  1385. to_jph(self)->SetMotionType(to_jph(inBodyID), static_cast<JPH::EMotionType>(inMotionType), to_jph(inActivationMode));
  1386. }
  1387. JPC_API JPC_MotionType JPC_BodyInterface_GetMotionType(const JPC_BodyInterface *self, JPC_BodyID inBodyID) {
  1388. return static_cast<JPC_MotionType>(to_jph(self)->GetMotionType(to_jph(inBodyID)));
  1389. }
  1390. JPC_API void JPC_BodyInterface_SetMotionQuality(JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_MotionQuality inMotionQuality) {
  1391. to_jph(self)->SetMotionQuality(to_jph(inBodyID), to_jph(inMotionQuality));
  1392. }
  1393. JPC_API JPC_MotionQuality JPC_BodyInterface_GetMotionQuality(const JPC_BodyInterface *self, JPC_BodyID inBodyID) {
  1394. return to_jpc(to_jph(self)->GetMotionQuality(to_jph(inBodyID)));
  1395. }
  1396. JPC_API void JPC_BodyInterface_GetInverseInertia(const JPC_BodyInterface *self, JPC_BodyID inBodyID, JPC_Mat44 *outMatrix) {
  1397. to_jph(self)->GetInverseInertia(to_jph(inBodyID)).StoreFloat4x4(reinterpret_cast<JPH::Float4*>(outMatrix));
  1398. }
  1399. JPC_API void JPC_BodyInterface_SetRestitution(JPC_BodyInterface *self, JPC_BodyID inBodyID, float inRestitution) {
  1400. to_jph(self)->SetRestitution(to_jph(inBodyID), inRestitution);
  1401. }
  1402. JPC_API float JPC_BodyInterface_GetRestitution(const JPC_BodyInterface *self, JPC_BodyID inBodyID) {
  1403. return to_jph(self)->GetRestitution(to_jph(inBodyID));
  1404. }
  1405. JPC_API void JPC_BodyInterface_SetFriction(JPC_BodyInterface *self, JPC_BodyID inBodyID, float inFriction) {
  1406. to_jph(self)->SetFriction(to_jph(inBodyID), inFriction);
  1407. }
  1408. JPC_API float JPC_BodyInterface_GetFriction(const JPC_BodyInterface *self, JPC_BodyID inBodyID) {
  1409. return to_jph(self)->GetFriction(to_jph(inBodyID));
  1410. }
  1411. JPC_API void JPC_BodyInterface_SetGravityFactor(JPC_BodyInterface *self, JPC_BodyID inBodyID, float inGravityFactor) {
  1412. to_jph(self)->SetGravityFactor(to_jph(inBodyID), inGravityFactor);
  1413. }
  1414. JPC_API float JPC_BodyInterface_GetGravityFactor(const JPC_BodyInterface *self, JPC_BodyID inBodyID) {
  1415. return to_jph(self)->GetGravityFactor(to_jph(inBodyID));
  1416. }
  1417. JPC_API void JPC_BodyInterface_SetUseManifoldReduction(JPC_BodyInterface *self, JPC_BodyID inBodyID, bool inUseReduction) {
  1418. to_jph(self)->SetUseManifoldReduction(to_jph(inBodyID), inUseReduction);
  1419. }
  1420. JPC_API bool JPC_BodyInterface_GetUseManifoldReduction(const JPC_BodyInterface *self, JPC_BodyID inBodyID) {
  1421. return to_jph(self)->GetUseManifoldReduction(to_jph(inBodyID));
  1422. }
  1423. // TransformedShape JPC_BodyInterface_GetTransformedShape(const JPC_BodyInterface *self, JPC_BodyID inBodyID);
  1424. JPC_API uint64_t JPC_BodyInterface_GetUserData(const JPC_BodyInterface *self, JPC_BodyID inBodyID) {
  1425. return to_jph(self)->GetUserData(to_jph(inBodyID));
  1426. }
  1427. JPC_API void JPC_BodyInterface_SetUserData(const JPC_BodyInterface *self, JPC_BodyID inBodyID, uint64_t inUserData) {
  1428. to_jph(self)->SetUserData(to_jph(inBodyID), inUserData);
  1429. }
  1430. // const PhysicsMaterial* JPC_BodyInterface_GetMaterial(const JPC_BodyInterface *self, JPC_BodyID inBodyID, const SubShapeID &inSubShapeID);
  1431. JPC_API void JPC_BodyInterface_InvalidateContactCache(JPC_BodyInterface *self, JPC_BodyID inBodyID) {
  1432. to_jph(self)->InvalidateContactCache(to_jph(inBodyID));
  1433. }
  1434. ////////////////////////////////////////////////////////////////////////////////
  1435. // NarrowPhaseQuery
  1436. JPC_API bool JPC_NarrowPhaseQuery_CastRay(const JPC_NarrowPhaseQuery* self, JPC_NarrowPhaseQuery_CastRayArgs* args) {
  1437. JPH::RayCastResult result;
  1438. JPH::RayCastSettings settings;
  1439. JPH::BroadPhaseLayerFilter defaultBplFilter{};
  1440. const JPH::BroadPhaseLayerFilter* bplFilter = &defaultBplFilter;
  1441. if (args->BroadPhaseLayerFilter != nullptr) {
  1442. bplFilter = to_jph(args->BroadPhaseLayerFilter);
  1443. }
  1444. JPH::ObjectLayerFilter defaultOlFilter{};
  1445. const JPH::ObjectLayerFilter* olFilter = &defaultOlFilter;
  1446. if (args->ObjectLayerFilter != nullptr) {
  1447. olFilter = to_jph(args->ObjectLayerFilter);
  1448. }
  1449. JPH::BodyFilter defaultBodyFilter{};
  1450. const JPH::BodyFilter* bodyFilter = &defaultBodyFilter;
  1451. if (args->BodyFilter != nullptr) {
  1452. bodyFilter = to_jph(args->BodyFilter);
  1453. }
  1454. JPH::ShapeFilter defaultShapeFilter{};
  1455. const JPH::ShapeFilter* shapeFilter = &defaultShapeFilter;
  1456. if (args->ShapeFilter != nullptr) {
  1457. shapeFilter = to_jph(args->ShapeFilter);
  1458. }
  1459. JPH::ClosestHitCollisionCollector<JPH::CastRayCollector> collector;
  1460. to_jph(self)->CastRay(
  1461. to_jph(args->Ray),
  1462. settings,
  1463. collector,
  1464. *bplFilter,
  1465. *olFilter,
  1466. *bodyFilter,
  1467. *shapeFilter);
  1468. bool hit = collector.HadHit();
  1469. if (hit) {
  1470. args->Result = to_jpc(collector.mHit);
  1471. }
  1472. return hit;
  1473. }
  1474. JPC_API void JPC_ShapeCastSettings_default(JPC_ShapeCastSettings* settings) {
  1475. JPH::ShapeCastSettings defaultSettings{};
  1476. // JPH::CollideSettingsBase
  1477. // EActiveEdgeMode ActiveEdgeMode;
  1478. // ECollectFacesMode CollectFacesMode;
  1479. settings->CollisionTolerance = defaultSettings.mCollisionTolerance;
  1480. settings->PenetrationTolerance = defaultSettings.mPenetrationTolerance;
  1481. settings->ActiveEdgeMovementDirection = to_jpc(defaultSettings.mActiveEdgeMovementDirection);
  1482. // JPH::ShapeCastSettings
  1483. settings->BackFaceModeTriangles = static_cast<JPC_BackFaceMode>(defaultSettings.mBackFaceModeTriangles);
  1484. settings->BackFaceModeConvex = static_cast<JPC_BackFaceMode>(defaultSettings.mBackFaceModeConvex);
  1485. settings->UseShrunkenShapeAndConvexRadius = defaultSettings.mUseShrunkenShapeAndConvexRadius;
  1486. settings->ReturnDeepestPoint = defaultSettings.mReturnDeepestPoint;
  1487. }
  1488. JPC_API void JPC_NarrowPhaseQuery_CastShape(const JPC_NarrowPhaseQuery* self, JPC_NarrowPhaseQuery_CastShapeArgs* args) {
  1489. JPH::ShapeCastSettings settings = JPC_ShapeCastSettings_to_jph(args->Settings);
  1490. JPH::ClosestHitCollisionCollector<JPH::CastShapeCollector> defaultCollector{};
  1491. JPH::CastShapeCollector* collector = &defaultCollector;
  1492. if (args->Collector != nullptr) {
  1493. collector = to_jph(args->Collector);
  1494. }
  1495. JPH::BroadPhaseLayerFilter defaultBplFilter{};
  1496. const JPH::BroadPhaseLayerFilter* bplFilter = &defaultBplFilter;
  1497. if (args->BroadPhaseLayerFilter != nullptr) {
  1498. bplFilter = to_jph(args->BroadPhaseLayerFilter);
  1499. }
  1500. JPH::ObjectLayerFilter defaultOlFilter{};
  1501. const JPH::ObjectLayerFilter* olFilter = &defaultOlFilter;
  1502. if (args->ObjectLayerFilter != nullptr) {
  1503. olFilter = to_jph(args->ObjectLayerFilter);
  1504. }
  1505. JPH::BodyFilter defaultBodyFilter{};
  1506. const JPH::BodyFilter* bodyFilter = &defaultBodyFilter;
  1507. if (args->BodyFilter != nullptr) {
  1508. bodyFilter = to_jph(args->BodyFilter);
  1509. }
  1510. JPH::ShapeFilter defaultShapeFilter{};
  1511. const JPH::ShapeFilter* shapeFilter = &defaultShapeFilter;
  1512. // if (args->ShapeFilter != nullptr) {
  1513. // shapeFilter = to_jph(args->ShapeFilter);
  1514. // }
  1515. to_jph(self)->CastShape(
  1516. to_jph(args->ShapeCast),
  1517. settings,
  1518. to_jph(args->BaseOffset),
  1519. *collector,
  1520. *bplFilter,
  1521. *olFilter,
  1522. *bodyFilter,
  1523. *shapeFilter);
  1524. }
  1525. ////////////////////////////////////////////////////////////////////////////////
  1526. // PhysicsSystem
  1527. JPC_API JPC_PhysicsSystem* JPC_PhysicsSystem_new() {
  1528. return to_jpc(new JPH::PhysicsSystem());
  1529. }
  1530. JPC_API void JPC_PhysicsSystem_Init(
  1531. JPC_PhysicsSystem* self,
  1532. uint inMaxBodies,
  1533. uint inNumBodyMutexes,
  1534. uint inMaxBodyPairs,
  1535. uint inMaxContactConstraints,
  1536. JPC_BroadPhaseLayerInterface* inBroadPhaseLayerInterface,
  1537. JPC_ObjectVsBroadPhaseLayerFilter* inObjectVsBroadPhaseLayerFilter,
  1538. JPC_ObjectLayerPairFilter* inObjectLayerPairFilter)
  1539. {
  1540. JPC_BroadPhaseLayerInterfaceBridge* impl_inBroadPhaseLayerInterface = to_jph(inBroadPhaseLayerInterface);
  1541. JPC_ObjectVsBroadPhaseLayerFilterBridge* impl_inObjectVsBroadPhaseLayerFilter = to_jph(inObjectVsBroadPhaseLayerFilter);
  1542. JPC_ObjectLayerPairFilterBridge* impl_inObjectLayerPairFilter = to_jph(inObjectLayerPairFilter);
  1543. to_jph(self)->Init(
  1544. inMaxBodies,
  1545. inNumBodyMutexes,
  1546. inMaxBodyPairs,
  1547. inMaxContactConstraints,
  1548. *impl_inBroadPhaseLayerInterface,
  1549. *impl_inObjectVsBroadPhaseLayerFilter,
  1550. *impl_inObjectLayerPairFilter);
  1551. }
  1552. JPC_API void JPC_PhysicsSystem_OptimizeBroadPhase(JPC_PhysicsSystem* self) {
  1553. to_jph(self)->OptimizeBroadPhase();
  1554. }
  1555. JPC_API void JPC_PhysicsSystem_AddConstraint(JPC_PhysicsSystem* self, JPC_Constraint* constraint) {
  1556. to_jph(self)->AddConstraint(to_jph(constraint));
  1557. }
  1558. JPC_API void JPC_PhysicsSystem_RemoveConstraint(JPC_PhysicsSystem* self, JPC_Constraint* constraint) {
  1559. to_jph(self)->RemoveConstraint(to_jph(constraint));
  1560. }
  1561. JPC_API JPC_BodyInterface* JPC_PhysicsSystem_GetBodyInterface(JPC_PhysicsSystem* self) {
  1562. return to_jpc(&to_jph(self)->GetBodyInterface());
  1563. }
  1564. JPC_API const JPC_BodyLockInterface* JPC_PhysicsSystem_GetBodyLockInterface(JPC_PhysicsSystem* self) {
  1565. return to_jpc(&to_jph(self)->GetBodyLockInterface());
  1566. }
  1567. JPC_API const JPC_NarrowPhaseQuery* JPC_PhysicsSystem_GetNarrowPhaseQuery(const JPC_PhysicsSystem* self) {
  1568. return to_jpc(&to_jph(self)->GetNarrowPhaseQuery());
  1569. }
  1570. JPC_API JPC_PhysicsUpdateError JPC_PhysicsSystem_Update(
  1571. JPC_PhysicsSystem* self,
  1572. float inDeltaTime,
  1573. int inCollisionSteps,
  1574. JPC_TempAllocatorImpl *inTempAllocator,
  1575. JPC_JobSystemThreadPool *inJobSystem)
  1576. {
  1577. auto res = to_jph(self)->Update(
  1578. inDeltaTime,
  1579. inCollisionSteps,
  1580. to_jph(inTempAllocator),
  1581. to_jph(inJobSystem));
  1582. return to_integral(res);
  1583. }
  1584. JPC_API void JPC_PhysicsSystem_DrawBodies(
  1585. JPC_PhysicsSystem* self,
  1586. JPC_BodyManager_DrawSettings* inSettings,
  1587. JPC_DebugRendererSimple* inRenderer,
  1588. [[maybe_unused]] const void* inBodyFilter)
  1589. {
  1590. to_jph(self)->DrawBodies(to_jph(*inSettings), to_jph(inRenderer), nullptr);
  1591. }
  1592. JPC_API void JPC_PhysicsSystem_SetSimShapeFilter(
  1593. JPC_PhysicsSystem* self,
  1594. const JPC_SimShapeFilter* inShapeFilter)
  1595. {
  1596. to_jph(self)->SetSimShapeFilter(to_jph(inShapeFilter));
  1597. }