2
0

jolt_shape_instance_3d.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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(const ShapeReference &p_other) :
  41. parent(p_other.parent),
  42. shape(p_other.shape) {
  43. if (shape != nullptr) {
  44. shape->add_owner(parent);
  45. }
  46. }
  47. JoltShapeInstance3D::ShapeReference::~ShapeReference() {
  48. if (shape != nullptr) {
  49. shape->remove_owner(parent);
  50. }
  51. }
  52. JoltShapeInstance3D::ShapeReference &JoltShapeInstance3D::ShapeReference::operator=(const ShapeReference &p_other) {
  53. if (shape != nullptr) {
  54. shape->remove_owner(parent);
  55. }
  56. parent = p_other.parent;
  57. shape = p_other.shape;
  58. if (shape != nullptr) {
  59. shape->add_owner(parent);
  60. }
  61. return *this;
  62. }
  63. JoltShapeInstance3D::JoltShapeInstance3D(JoltShapedObject3D *p_parent, JoltShape3D *p_shape, const Transform3D &p_transform, const Vector3 &p_scale, bool p_disabled) :
  64. transform(p_transform),
  65. scale(p_scale),
  66. shape(p_parent, p_shape),
  67. disabled(p_disabled) {
  68. }
  69. AABB JoltShapeInstance3D::get_aabb() const {
  70. return get_transform_scaled().xform(shape->get_aabb());
  71. }
  72. bool JoltShapeInstance3D::try_build() {
  73. ERR_FAIL_COND_V(is_disabled(), false);
  74. const JPH::ShapeRefC maybe_new_shape = shape->try_build();
  75. if (maybe_new_shape == nullptr) {
  76. jolt_ref = nullptr;
  77. return false;
  78. }
  79. if (jolt_ref != nullptr) {
  80. const JPH::DecoratedShape *outer_shape = static_cast<const JPH::DecoratedShape *>(jolt_ref.GetPtr());
  81. if (outer_shape->GetInnerShape() == maybe_new_shape) {
  82. return true;
  83. }
  84. }
  85. jolt_ref = JoltShape3D::with_user_data(maybe_new_shape, (uint64_t)id);
  86. return true;
  87. }