jolt_shape_3d.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**************************************************************************/
  2. /* jolt_shape_3d.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef JOLT_SHAPE_3D_H
  31. #define JOLT_SHAPE_3D_H
  32. #include "servers/physics_server_3d.h"
  33. #include "Jolt/Jolt.h"
  34. #include "Jolt/Physics/Collision/Shape/Shape.h"
  35. class JoltShapedObject3D;
  36. class JoltShape3D {
  37. protected:
  38. HashMap<JoltShapedObject3D *, int> ref_counts_by_owner;
  39. Mutex jolt_ref_mutex;
  40. RID rid;
  41. JPH::ShapeRefC jolt_ref;
  42. virtual JPH::ShapeRefC _build() const = 0;
  43. String _owners_to_string() const;
  44. public:
  45. typedef PhysicsServer3D::ShapeType ShapeType;
  46. virtual ~JoltShape3D() = 0;
  47. RID get_rid() const { return rid; }
  48. void set_rid(const RID &p_rid) { rid = p_rid; }
  49. void add_owner(JoltShapedObject3D *p_owner);
  50. void remove_owner(JoltShapedObject3D *p_owner);
  51. void remove_self();
  52. virtual ShapeType get_type() const = 0;
  53. virtual bool is_convex() const = 0;
  54. virtual Variant get_data() const = 0;
  55. virtual void set_data(const Variant &p_data) = 0;
  56. virtual float get_margin() const = 0;
  57. virtual void set_margin(float p_margin) = 0;
  58. virtual AABB get_aabb() const = 0;
  59. float get_solver_bias() const;
  60. void set_solver_bias(float p_bias);
  61. JPH::ShapeRefC try_build();
  62. void destroy();
  63. const JPH::Shape *get_jolt_ref() const { return jolt_ref; }
  64. static JPH::ShapeRefC with_scale(const JPH::Shape *p_shape, const Vector3 &p_scale);
  65. static JPH::ShapeRefC with_basis_origin(const JPH::Shape *p_shape, const Basis &p_basis, const Vector3 &p_origin);
  66. static JPH::ShapeRefC with_center_of_mass_offset(const JPH::Shape *p_shape, const Vector3 &p_offset);
  67. static JPH::ShapeRefC with_center_of_mass(const JPH::Shape *p_shape, const Vector3 &p_center_of_mass);
  68. static JPH::ShapeRefC with_user_data(const JPH::Shape *p_shape, uint64_t p_user_data);
  69. static JPH::ShapeRefC with_double_sided(const JPH::Shape *p_shape, bool p_back_face_collision);
  70. static JPH::ShapeRefC without_custom_shapes(const JPH::Shape *p_shape);
  71. static Vector3 make_scale_valid(const JPH::Shape *p_shape, const Vector3 &p_scale);
  72. static bool is_scale_valid(const Vector3 &p_scale, const Vector3 &p_valid_scale, real_t p_tolerance = 0.01f);
  73. };
  74. #ifdef DEBUG_ENABLED
  75. #define JOLT_ENSURE_SCALE_NOT_ZERO(m_transform, m_msg) \
  76. if (unlikely((m_transform).basis.determinant() == 0.0f)) { \
  77. WARN_PRINT(vformat("%s " \
  78. "The basis of the transform was singular, which is not supported by Jolt Physics. " \
  79. "This is likely caused by one or more axes having a scale of zero. " \
  80. "The basis (and thus its scale) will be treated as identity.", \
  81. m_msg)); \
  82. \
  83. (m_transform).basis = Basis(); \
  84. } else \
  85. ((void)0)
  86. #define ERR_PRINT_INVALID_SCALE_MSG(m_scale, m_valid_scale, m_msg) \
  87. if (unlikely(!JoltShape3D::is_scale_valid(m_scale, valid_scale))) { \
  88. ERR_PRINT(vformat("%s " \
  89. "A scale of %v is not supported by Jolt Physics for this shape/body. " \
  90. "The scale will instead be treated as %v.", \
  91. m_msg, m_scale, valid_scale)); \
  92. } else \
  93. ((void)0)
  94. #else
  95. #define JOLT_ENSURE_SCALE_NOT_ZERO(m_transform, m_msg)
  96. #define ERR_PRINT_INVALID_SCALE_MSG(m_scale, m_valid_scale, m_msg)
  97. #endif
  98. #define JOLT_ENSURE_SCALE_VALID(m_shape, m_scale, m_msg) \
  99. if (true) { \
  100. const Vector3 valid_scale = JoltShape3D::make_scale_valid(m_shape, m_scale); \
  101. ERR_PRINT_INVALID_SCALE_MSG(m_scale, valid_scale, m_msg); \
  102. (m_scale) = valid_scale; \
  103. } else \
  104. ((void)0)
  105. #endif // JOLT_SHAPE_3D_H