RegisterTypes.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <Jolt/Jolt.h>
  5. #include <Jolt/RegisterTypes.h>
  6. #include <Jolt/Core/Factory.h>
  7. #include <Jolt/Core/RTTI.h>
  8. #include <Jolt/Core/TickCounter.h>
  9. #include <Jolt/Physics/Collision/CollisionDispatch.h>
  10. #include <Jolt/Physics/Collision/Shape/TriangleShape.h>
  11. #include <Jolt/Physics/Collision/Shape/PlaneShape.h>
  12. #include <Jolt/Physics/Collision/Shape/SphereShape.h>
  13. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  14. #include <Jolt/Physics/Collision/Shape/CapsuleShape.h>
  15. #include <Jolt/Physics/Collision/Shape/TaperedCapsuleShape.h>
  16. #include <Jolt/Physics/Collision/Shape/CylinderShape.h>
  17. #include <Jolt/Physics/Collision/Shape/TaperedCylinderShape.h>
  18. #include <Jolt/Physics/Collision/Shape/ScaledShape.h>
  19. #include <Jolt/Physics/Collision/Shape/MeshShape.h>
  20. #include <Jolt/Physics/Collision/Shape/ConvexHullShape.h>
  21. #include <Jolt/Physics/Collision/Shape/HeightFieldShape.h>
  22. #include <Jolt/Physics/Collision/Shape/RotatedTranslatedShape.h>
  23. #include <Jolt/Physics/Collision/Shape/OffsetCenterOfMassShape.h>
  24. #include <Jolt/Physics/Collision/Shape/MutableCompoundShape.h>
  25. #include <Jolt/Physics/Collision/Shape/StaticCompoundShape.h>
  26. #include <Jolt/Physics/Collision/Shape/EmptyShape.h>
  27. #include <Jolt/Physics/Collision/PhysicsMaterialSimple.h>
  28. #include <Jolt/Physics/SoftBody/SoftBodyShape.h>
  29. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, Skeleton)
  30. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, SkeletalAnimation)
  31. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, RagdollSettings)
  32. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, PointConstraintSettings)
  33. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, SixDOFConstraintSettings)
  34. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, SliderConstraintSettings)
  35. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, SwingTwistConstraintSettings)
  36. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, DistanceConstraintSettings)
  37. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, HingeConstraintSettings)
  38. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, FixedConstraintSettings)
  39. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, ConeConstraintSettings)
  40. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, PathConstraintSettings)
  41. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, PathConstraintPath)
  42. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, PathConstraintPathHermite)
  43. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, VehicleConstraintSettings)
  44. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, WheeledVehicleControllerSettings)
  45. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, RackAndPinionConstraintSettings)
  46. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, GearConstraintSettings)
  47. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, PulleyConstraintSettings)
  48. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, MotorSettings)
  49. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, PhysicsScene)
  50. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, PhysicsMaterial)
  51. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, GroupFilter)
  52. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, GroupFilterTable)
  53. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, BodyCreationSettings)
  54. JPH_DECLARE_RTTI_WITH_NAMESPACE_FOR_FACTORY(JPH_EXPORT, JPH, SoftBodyCreationSettings)
  55. JPH_NAMESPACE_BEGIN
  56. bool VerifyJoltVersionIDInternal(uint64 inVersionID)
  57. {
  58. return inVersionID == JPH_VERSION_ID;
  59. }
  60. void RegisterTypesInternal(uint64 inVersionID)
  61. {
  62. // Version check
  63. if (!VerifyJoltVersionIDInternal(inVersionID))
  64. {
  65. Trace("Version mismatch, make sure you compile the client code with the same Jolt version and compiler definitions!");
  66. uint64 mismatch = JPH_VERSION_ID ^ inVersionID;
  67. auto check_bit = [mismatch](int inBit, const char *inLabel) { if (mismatch & (uint64(1) << (inBit + 23))) Trace("Mismatching define %s.", inLabel); };
  68. check_bit(1, "JPH_DOUBLE_PRECISION");
  69. check_bit(2, "JPH_CROSS_PLATFORM_DETERMINISTIC");
  70. check_bit(3, "JPH_FLOATING_POINT_EXCEPTIONS_ENABLED");
  71. check_bit(4, "JPH_PROFILE_ENABLED");
  72. check_bit(5, "JPH_EXTERNAL_PROFILE");
  73. check_bit(6, "JPH_DEBUG_RENDERER");
  74. check_bit(7, "JPH_DISABLE_TEMP_ALLOCATOR");
  75. check_bit(8, "JPH_DISABLE_CUSTOM_ALLOCATOR");
  76. check_bit(9, "JPH_OBJECT_LAYER_BITS");
  77. check_bit(10, "JPH_ENABLE_ASSERTS");
  78. check_bit(11, "JPH_OBJECT_STREAM");
  79. std::abort();
  80. }
  81. #ifndef JPH_DISABLE_CUSTOM_ALLOCATOR
  82. JPH_ASSERT(Allocate != nullptr && Reallocate != nullptr && Free != nullptr && AlignedAllocate != nullptr && AlignedFree != nullptr, "Need to supply an allocator first or call RegisterDefaultAllocator()");
  83. #endif // !JPH_DISABLE_CUSTOM_ALLOCATOR
  84. JPH_ASSERT(Factory::sInstance != nullptr, "Need to create a factory first!");
  85. // Initialize dispatcher
  86. CollisionDispatch::sInit();
  87. // Register base classes first so that we can specialize them later
  88. CompoundShape::sRegister();
  89. ConvexShape::sRegister();
  90. // Register compounds before others so that we can specialize them later (register them in reverse order of collision complexity)
  91. MutableCompoundShape::sRegister();
  92. StaticCompoundShape::sRegister();
  93. // Leaf classes
  94. TriangleShape::sRegister();
  95. PlaneShape::sRegister();
  96. SphereShape::sRegister();
  97. BoxShape::sRegister();
  98. CapsuleShape::sRegister();
  99. TaperedCapsuleShape::sRegister();
  100. CylinderShape::sRegister();
  101. TaperedCylinderShape::sRegister();
  102. MeshShape::sRegister();
  103. ConvexHullShape::sRegister();
  104. HeightFieldShape::sRegister();
  105. SoftBodyShape::sRegister();
  106. // Register these last because their collision functions are simple so we want to execute them first (register them in reverse order of collision complexity)
  107. RotatedTranslatedShape::sRegister();
  108. OffsetCenterOfMassShape::sRegister();
  109. ScaledShape::sRegister();
  110. EmptyShape::sRegister();
  111. // Create list of all types
  112. const RTTI *types[] = {
  113. JPH_RTTI(SkeletalAnimation),
  114. JPH_RTTI(Skeleton),
  115. JPH_RTTI(CompoundShapeSettings),
  116. JPH_RTTI(StaticCompoundShapeSettings),
  117. JPH_RTTI(MutableCompoundShapeSettings),
  118. JPH_RTTI(TriangleShapeSettings),
  119. JPH_RTTI(PlaneShapeSettings),
  120. JPH_RTTI(SphereShapeSettings),
  121. JPH_RTTI(BoxShapeSettings),
  122. JPH_RTTI(CapsuleShapeSettings),
  123. JPH_RTTI(TaperedCapsuleShapeSettings),
  124. JPH_RTTI(CylinderShapeSettings),
  125. JPH_RTTI(TaperedCylinderShapeSettings),
  126. JPH_RTTI(ScaledShapeSettings),
  127. JPH_RTTI(MeshShapeSettings),
  128. JPH_RTTI(ConvexHullShapeSettings),
  129. JPH_RTTI(HeightFieldShapeSettings),
  130. JPH_RTTI(RotatedTranslatedShapeSettings),
  131. JPH_RTTI(OffsetCenterOfMassShapeSettings),
  132. JPH_RTTI(EmptyShapeSettings),
  133. JPH_RTTI(RagdollSettings),
  134. JPH_RTTI(PointConstraintSettings),
  135. JPH_RTTI(SixDOFConstraintSettings),
  136. JPH_RTTI(SliderConstraintSettings),
  137. JPH_RTTI(SwingTwistConstraintSettings),
  138. JPH_RTTI(DistanceConstraintSettings),
  139. JPH_RTTI(HingeConstraintSettings),
  140. JPH_RTTI(FixedConstraintSettings),
  141. JPH_RTTI(ConeConstraintSettings),
  142. JPH_RTTI(PathConstraintSettings),
  143. JPH_RTTI(VehicleConstraintSettings),
  144. JPH_RTTI(WheeledVehicleControllerSettings),
  145. JPH_RTTI(PathConstraintPath),
  146. JPH_RTTI(PathConstraintPathHermite),
  147. JPH_RTTI(RackAndPinionConstraintSettings),
  148. JPH_RTTI(GearConstraintSettings),
  149. JPH_RTTI(PulleyConstraintSettings),
  150. JPH_RTTI(MotorSettings),
  151. JPH_RTTI(PhysicsScene),
  152. JPH_RTTI(PhysicsMaterial),
  153. JPH_RTTI(PhysicsMaterialSimple),
  154. JPH_RTTI(GroupFilter),
  155. JPH_RTTI(GroupFilterTable),
  156. JPH_RTTI(BodyCreationSettings),
  157. JPH_RTTI(SoftBodyCreationSettings)
  158. };
  159. // Register them all
  160. Factory::sInstance->Register(types, (uint)std::size(types));
  161. // Initialize default physics material
  162. if (PhysicsMaterial::sDefault == nullptr)
  163. PhysicsMaterial::sDefault = new PhysicsMaterialSimple("Default", Color::sGrey);
  164. }
  165. void UnregisterTypes()
  166. {
  167. // Unregister all types
  168. if (Factory::sInstance != nullptr)
  169. Factory::sInstance->Clear();
  170. // Delete default physics material
  171. PhysicsMaterial::sDefault = nullptr;
  172. }
  173. JPH_NAMESPACE_END