jolt_shape_instance_3d.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**************************************************************************/
  2. /* jolt_shape_instance_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_INSTANCE_3D_H
  31. #define JOLT_SHAPE_INSTANCE_3D_H
  32. #include "core/math/transform_3d.h"
  33. #include "Jolt/Jolt.h"
  34. #include "Jolt/Physics/Collision/Shape/Shape.h"
  35. class JoltShapedObject3D;
  36. class JoltShape3D;
  37. class JoltShapeInstance3D {
  38. // This RAII helper exists solely to avoid needing to maintain copy construction/assignment in the shape instance.
  39. // Ideally this would be move-only instead, but Godot's containers don't support that at the moment.
  40. struct ShapeReference {
  41. JoltShapedObject3D *parent = nullptr;
  42. JoltShape3D *shape = nullptr;
  43. ShapeReference() = default;
  44. ShapeReference(JoltShapedObject3D *p_parent, JoltShape3D *p_shape);
  45. ShapeReference(const ShapeReference &p_other);
  46. ShapeReference(ShapeReference &&p_other) = delete;
  47. ~ShapeReference();
  48. ShapeReference &operator=(const ShapeReference &p_other);
  49. ShapeReference &operator=(ShapeReference &&p_other) = delete;
  50. JoltShape3D *operator*() const { return shape; }
  51. JoltShape3D *operator->() const { return shape; }
  52. operator JoltShape3D *() const { return shape; }
  53. };
  54. inline static uint32_t next_id = 1;
  55. Transform3D transform;
  56. Vector3 scale;
  57. ShapeReference shape;
  58. JPH::ShapeRefC jolt_ref;
  59. uint32_t id = next_id++;
  60. bool disabled = false;
  61. public:
  62. JoltShapeInstance3D() = default;
  63. JoltShapeInstance3D(JoltShapedObject3D *p_parent, JoltShape3D *p_shape, const Transform3D &p_transform = Transform3D(), const Vector3 &p_scale = Vector3(1.0f, 1.0f, 1.0f), bool p_disabled = false);
  64. uint32_t get_id() const { return id; }
  65. JoltShape3D *get_shape() const { return shape; }
  66. const JPH::Shape *get_jolt_ref() const { return jolt_ref; }
  67. const Transform3D &get_transform_unscaled() const { return transform; }
  68. Transform3D get_transform_scaled() const { return transform.scaled_local(scale); }
  69. void set_transform(const Transform3D &p_transform) { transform = p_transform; }
  70. const Vector3 &get_scale() const { return scale; }
  71. void set_scale(const Vector3 &p_scale) { scale = p_scale; }
  72. AABB get_aabb() const;
  73. bool is_built() const { return jolt_ref != nullptr; }
  74. bool is_enabled() const { return !disabled; }
  75. bool is_disabled() const { return disabled; }
  76. void enable() { disabled = false; }
  77. void disable() { disabled = true; }
  78. bool try_build();
  79. };
  80. #endif // JOLT_SHAPE_INSTANCE_3D_H