jolt_shape_instance_3d.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**************************************************************************/
  2. /* jolt_shape_instance_3d.cpp */
  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. #include "jolt_shape_instance_3d.h"
  31. #include "jolt_shape_3d.h"
  32. #include "Jolt/Physics/Collision/Shape/DecoratedShape.h"
  33. JoltShapeInstance3D::ShapeReference::ShapeReference(JoltShapedObject3D *p_parent, JoltShape3D *p_shape) :
  34. parent(p_parent),
  35. shape(p_shape) {
  36. if (shape != nullptr) {
  37. shape->add_owner(parent);
  38. }
  39. }
  40. JoltShapeInstance3D::ShapeReference::ShapeReference(ShapeReference &&p_other) :
  41. parent(p_other.parent),
  42. shape(p_other.shape) {
  43. p_other.parent = nullptr;
  44. p_other.shape = nullptr;
  45. }
  46. JoltShapeInstance3D::ShapeReference::~ShapeReference() {
  47. if (shape != nullptr) {
  48. shape->remove_owner(parent);
  49. }
  50. }
  51. JoltShapeInstance3D::ShapeReference &JoltShapeInstance3D::ShapeReference::operator=(ShapeReference &&p_other) {
  52. if (this != &p_other) {
  53. SWAP(parent, p_other.parent);
  54. SWAP(shape, p_other.shape);
  55. }
  56. return *this;
  57. }
  58. JoltShapeInstance3D::JoltShapeInstance3D(JoltShapedObject3D *p_parent, JoltShape3D *p_shape, const Transform3D &p_transform, const Vector3 &p_scale, bool p_disabled) :
  59. transform(p_transform),
  60. scale(p_scale),
  61. shape(p_parent, p_shape),
  62. disabled(p_disabled) {
  63. }
  64. AABB JoltShapeInstance3D::get_aabb() const {
  65. return get_transform_scaled().xform(shape->get_aabb());
  66. }
  67. bool JoltShapeInstance3D::try_build() {
  68. ERR_FAIL_COND_V(is_disabled(), false);
  69. const JPH::ShapeRefC maybe_new_shape = shape->try_build();
  70. if (maybe_new_shape == nullptr) {
  71. jolt_ref = nullptr;
  72. return false;
  73. }
  74. if (jolt_ref != nullptr) {
  75. const JPH::DecoratedShape *outer_shape = static_cast<const JPH::DecoratedShape *>(jolt_ref.GetPtr());
  76. if (outer_shape->GetInnerShape() == maybe_new_shape) {
  77. return true;
  78. }
  79. }
  80. jolt_ref = JoltShape3D::with_user_data(maybe_new_shape, (uint64_t)id);
  81. return true;
  82. }