jolt_shaped_object_3d.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /**************************************************************************/
  2. /* jolt_shaped_object_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_shaped_object_3d.h"
  31. #include "../misc/jolt_math_funcs.h"
  32. #include "../misc/jolt_type_conversions.h"
  33. #include "../shapes/jolt_custom_double_sided_shape.h"
  34. #include "../shapes/jolt_shape_3d.h"
  35. #include "../spaces/jolt_space_3d.h"
  36. #include "Jolt/Physics/Collision/Shape/EmptyShape.h"
  37. #include "Jolt/Physics/Collision/Shape/MutableCompoundShape.h"
  38. #include "Jolt/Physics/Collision/Shape/StaticCompoundShape.h"
  39. bool JoltShapedObject3D::_is_big() const {
  40. // This number is completely arbitrary, and mostly just needs to capture `WorldBoundaryShape3D`, which needs to be kept out of the normal broadphase layers.
  41. return get_aabb().get_longest_axis_size() >= 1000.0f;
  42. }
  43. JPH::ShapeRefC JoltShapedObject3D::_try_build_shape(bool p_optimize_compound) {
  44. int built_shapes = 0;
  45. for (JoltShapeInstance3D &shape : shapes) {
  46. if (shape.is_enabled() && shape.try_build()) {
  47. built_shapes += 1;
  48. }
  49. }
  50. if (unlikely(built_shapes == 0)) {
  51. return nullptr;
  52. }
  53. JPH::ShapeRefC result = built_shapes == 1 ? _try_build_single_shape() : _try_build_compound_shape(p_optimize_compound);
  54. if (unlikely(result == nullptr)) {
  55. return nullptr;
  56. }
  57. if (has_custom_center_of_mass()) {
  58. result = JoltShape3D::with_center_of_mass(result, get_center_of_mass_custom());
  59. }
  60. if (scale != Vector3(1, 1, 1)) {
  61. Vector3 actual_scale = scale;
  62. JOLT_ENSURE_SCALE_VALID(result, actual_scale, vformat("Failed to correctly scale body '%s'.", to_string()));
  63. result = JoltShape3D::with_scale(result, actual_scale);
  64. }
  65. if (is_area()) {
  66. result = JoltShape3D::with_double_sided(result, true);
  67. }
  68. return result;
  69. }
  70. JPH::ShapeRefC JoltShapedObject3D::_try_build_single_shape() {
  71. for (int shape_index = 0; shape_index < (int)shapes.size(); ++shape_index) {
  72. const JoltShapeInstance3D &sub_shape = shapes[shape_index];
  73. if (!sub_shape.is_enabled() || !sub_shape.is_built()) {
  74. continue;
  75. }
  76. JPH::ShapeRefC jolt_sub_shape = sub_shape.get_jolt_ref();
  77. Vector3 sub_shape_scale = sub_shape.get_scale();
  78. const Transform3D sub_shape_transform = sub_shape.get_transform_unscaled();
  79. if (sub_shape_scale != Vector3(1, 1, 1)) {
  80. JOLT_ENSURE_SCALE_VALID(jolt_sub_shape, sub_shape_scale, vformat("Failed to correctly scale shape at index %d in body '%s'.", shape_index, to_string()));
  81. jolt_sub_shape = JoltShape3D::with_scale(jolt_sub_shape, sub_shape_scale);
  82. }
  83. if (sub_shape_transform != Transform3D()) {
  84. jolt_sub_shape = JoltShape3D::with_basis_origin(jolt_sub_shape, sub_shape_transform.basis, sub_shape_transform.origin);
  85. }
  86. return jolt_sub_shape;
  87. }
  88. return nullptr;
  89. }
  90. JPH::ShapeRefC JoltShapedObject3D::_try_build_compound_shape(bool p_optimize) {
  91. JPH::StaticCompoundShapeSettings static_compound_shape_settings;
  92. JPH::MutableCompoundShapeSettings mutable_compound_shape_settings;
  93. JPH::CompoundShapeSettings *compound_shape_settings = p_optimize ? static_cast<JPH::CompoundShapeSettings *>(&static_compound_shape_settings) : static_cast<JPH::CompoundShapeSettings *>(&mutable_compound_shape_settings);
  94. compound_shape_settings->mSubShapes.reserve((size_t)shapes.size());
  95. for (int shape_index = 0; shape_index < (int)shapes.size(); ++shape_index) {
  96. const JoltShapeInstance3D &sub_shape = shapes[shape_index];
  97. if (!sub_shape.is_enabled() || !sub_shape.is_built()) {
  98. continue;
  99. }
  100. JPH::ShapeRefC jolt_sub_shape = sub_shape.get_jolt_ref();
  101. Vector3 sub_shape_scale = sub_shape.get_scale();
  102. const Transform3D sub_shape_transform = sub_shape.get_transform_unscaled();
  103. if (sub_shape_scale != Vector3(1, 1, 1)) {
  104. JOLT_ENSURE_SCALE_VALID(jolt_sub_shape, sub_shape_scale, vformat("Failed to correctly scale shape at index %d for body '%s'.", shape_index, to_string()));
  105. jolt_sub_shape = JoltShape3D::with_scale(jolt_sub_shape, sub_shape_scale);
  106. }
  107. compound_shape_settings->AddShape(to_jolt(sub_shape_transform.origin), to_jolt(sub_shape_transform.basis), jolt_sub_shape);
  108. }
  109. const JPH::ShapeSettings::ShapeResult shape_result = p_optimize ? static_compound_shape_settings.Create(space->get_temp_allocator()) : mutable_compound_shape_settings.Create();
  110. ERR_FAIL_COND_V_MSG(shape_result.HasError(), nullptr, vformat("Failed to create compound shape for body '%s'. It returned the following error: '%s'.", to_string(), to_godot(shape_result.GetError())));
  111. return shape_result.Get();
  112. }
  113. void JoltShapedObject3D::_enqueue_shapes_changed() {
  114. if (space != nullptr) {
  115. space->enqueue_shapes_changed(&shapes_changed_element);
  116. }
  117. }
  118. void JoltShapedObject3D::_dequeue_shapes_changed() {
  119. if (space != nullptr) {
  120. space->dequeue_shapes_changed(&shapes_changed_element);
  121. }
  122. }
  123. void JoltShapedObject3D::_enqueue_needs_optimization() {
  124. if (space != nullptr) {
  125. space->enqueue_needs_optimization(&needs_optimization_element);
  126. }
  127. }
  128. void JoltShapedObject3D::_dequeue_needs_optimization() {
  129. if (space != nullptr) {
  130. space->dequeue_needs_optimization(&needs_optimization_element);
  131. }
  132. }
  133. void JoltShapedObject3D::_shapes_changed() {
  134. commit_shapes(false);
  135. }
  136. void JoltShapedObject3D::_shapes_committed() {
  137. _update_object_layer();
  138. }
  139. void JoltShapedObject3D::_space_changing() {
  140. JoltObject3D::_space_changing();
  141. _dequeue_shapes_changed();
  142. _dequeue_needs_optimization();
  143. previous_jolt_shape = nullptr;
  144. if (in_space()) {
  145. jolt_settings = new JPH::BodyCreationSettings(jolt_body->GetBodyCreationSettings());
  146. }
  147. }
  148. JoltShapedObject3D::JoltShapedObject3D(ObjectType p_object_type) :
  149. JoltObject3D(p_object_type),
  150. shapes_changed_element(this),
  151. needs_optimization_element(this) {
  152. jolt_settings->mAllowSleeping = true;
  153. jolt_settings->mFriction = 1.0f;
  154. jolt_settings->mRestitution = 0.0f;
  155. jolt_settings->mLinearDamping = 0.0f;
  156. jolt_settings->mAngularDamping = 0.0f;
  157. jolt_settings->mGravityFactor = 0.0f;
  158. }
  159. JoltShapedObject3D::~JoltShapedObject3D() {
  160. if (jolt_settings != nullptr) {
  161. delete jolt_settings;
  162. jolt_settings = nullptr;
  163. }
  164. }
  165. Transform3D JoltShapedObject3D::get_transform_unscaled() const {
  166. if (!in_space()) {
  167. return Transform3D(to_godot(jolt_settings->mRotation), to_godot(jolt_settings->mPosition));
  168. } else {
  169. return Transform3D(to_godot(jolt_body->GetRotation()), to_godot(jolt_body->GetPosition()));
  170. }
  171. }
  172. Transform3D JoltShapedObject3D::get_transform_scaled() const {
  173. return get_transform_unscaled().scaled_local(scale);
  174. }
  175. Basis JoltShapedObject3D::get_basis() const {
  176. if (!in_space()) {
  177. return to_godot(jolt_settings->mRotation);
  178. } else {
  179. return to_godot(jolt_body->GetRotation());
  180. }
  181. }
  182. Vector3 JoltShapedObject3D::get_position() const {
  183. if (!in_space()) {
  184. return to_godot(jolt_settings->mPosition);
  185. } else {
  186. return to_godot(jolt_body->GetPosition());
  187. }
  188. }
  189. Vector3 JoltShapedObject3D::get_center_of_mass() const {
  190. ERR_FAIL_COND_V_MSG(!in_space(), Vector3(), vformat("Failed to retrieve center-of-mass of '%s'. Doing so without a physics space is not supported when using Jolt Physics. If this relates to a node, try adding the node to a scene tree first.", to_string()));
  191. return to_godot(jolt_body->GetCenterOfMassPosition());
  192. }
  193. Vector3 JoltShapedObject3D::get_center_of_mass_relative() const {
  194. return get_center_of_mass() - get_position();
  195. }
  196. Vector3 JoltShapedObject3D::get_center_of_mass_local() const {
  197. ERR_FAIL_NULL_V_MSG(space, Vector3(), vformat("Failed to retrieve local center-of-mass of '%s'. Doing so without a physics space is not supported when using Jolt Physics. If this relates to a node, try adding the node to a scene tree first.", to_string()));
  198. return get_transform_scaled().xform_inv(get_center_of_mass());
  199. }
  200. Vector3 JoltShapedObject3D::get_linear_velocity() const {
  201. if (!in_space()) {
  202. return to_godot(jolt_settings->mLinearVelocity);
  203. } else {
  204. return to_godot(jolt_body->GetLinearVelocity());
  205. }
  206. }
  207. Vector3 JoltShapedObject3D::get_angular_velocity() const {
  208. if (!in_space()) {
  209. return to_godot(jolt_settings->mAngularVelocity);
  210. } else {
  211. return to_godot(jolt_body->GetAngularVelocity());
  212. }
  213. }
  214. AABB JoltShapedObject3D::get_aabb() const {
  215. AABB result;
  216. for (const JoltShapeInstance3D &shape : shapes) {
  217. if (shape.is_disabled()) {
  218. continue;
  219. }
  220. if (result == AABB()) {
  221. result = shape.get_aabb();
  222. } else {
  223. result.merge_with(shape.get_aabb());
  224. }
  225. }
  226. return get_transform_scaled().xform(result);
  227. }
  228. JPH::ShapeRefC JoltShapedObject3D::build_shapes(bool p_optimize_compound) {
  229. JPH::ShapeRefC new_shape = _try_build_shape(p_optimize_compound);
  230. if (new_shape == nullptr) {
  231. if (has_custom_center_of_mass()) {
  232. new_shape = JPH::EmptyShapeSettings(to_jolt(get_center_of_mass_custom())).Create().Get();
  233. } else {
  234. new_shape = new JPH::EmptyShape();
  235. }
  236. }
  237. return new_shape;
  238. }
  239. void JoltShapedObject3D::commit_shapes(bool p_optimize_compound) {
  240. if (!in_space()) {
  241. _shapes_committed();
  242. return;
  243. }
  244. JPH::ShapeRefC new_shape = build_shapes(p_optimize_compound);
  245. if (new_shape == jolt_shape) {
  246. return;
  247. }
  248. if (previous_jolt_shape == nullptr) {
  249. previous_jolt_shape = jolt_shape;
  250. }
  251. jolt_shape = new_shape;
  252. space->get_body_iface().SetShape(jolt_body->GetID(), jolt_shape, false, JPH::EActivation::DontActivate);
  253. _enqueue_shapes_changed();
  254. if (!p_optimize_compound && jolt_shape->GetType() == JPH::EShapeType::Compound) {
  255. _enqueue_needs_optimization();
  256. } else {
  257. _dequeue_needs_optimization();
  258. }
  259. _shapes_committed();
  260. }
  261. void JoltShapedObject3D::add_shape(JoltShape3D *p_shape, Transform3D p_transform, bool p_disabled) {
  262. JOLT_ENSURE_SCALE_NOT_ZERO(p_transform, vformat("An invalid transform was passed when adding shape at index %d to physics body '%s'.", shapes.size(), to_string()));
  263. Vector3 shape_scale;
  264. JoltMath::decompose(p_transform, shape_scale);
  265. shapes.push_back(JoltShapeInstance3D(this, p_shape, p_transform, shape_scale, p_disabled));
  266. _shapes_changed();
  267. }
  268. void JoltShapedObject3D::remove_shape(const JoltShape3D *p_shape) {
  269. for (int i = shapes.size() - 1; i >= 0; i--) {
  270. if (shapes[i].get_shape() == p_shape) {
  271. shapes.remove_at(i);
  272. }
  273. }
  274. _shapes_changed();
  275. }
  276. void JoltShapedObject3D::remove_shape(int p_index) {
  277. ERR_FAIL_INDEX(p_index, (int)shapes.size());
  278. shapes.remove_at(p_index);
  279. _shapes_changed();
  280. }
  281. JoltShape3D *JoltShapedObject3D::get_shape(int p_index) const {
  282. ERR_FAIL_INDEX_V(p_index, (int)shapes.size(), nullptr);
  283. return shapes[p_index].get_shape();
  284. }
  285. void JoltShapedObject3D::set_shape(int p_index, JoltShape3D *p_shape) {
  286. ERR_FAIL_INDEX(p_index, (int)shapes.size());
  287. shapes[p_index] = JoltShapeInstance3D(this, p_shape);
  288. _shapes_changed();
  289. }
  290. void JoltShapedObject3D::clear_shapes() {
  291. shapes.clear();
  292. _shapes_changed();
  293. }
  294. void JoltShapedObject3D::clear_previous_shape() {
  295. previous_jolt_shape = nullptr;
  296. }
  297. int JoltShapedObject3D::find_shape_index(uint32_t p_shape_instance_id) const {
  298. for (int i = 0; i < (int)shapes.size(); ++i) {
  299. if (shapes[i].get_id() == p_shape_instance_id) {
  300. return i;
  301. }
  302. }
  303. return -1;
  304. }
  305. int JoltShapedObject3D::find_shape_index(const JPH::SubShapeID &p_sub_shape_id) const {
  306. ERR_FAIL_NULL_V(jolt_shape, -1);
  307. return find_shape_index((uint32_t)jolt_shape->GetSubShapeUserData(p_sub_shape_id));
  308. }
  309. JoltShape3D *JoltShapedObject3D::find_shape(uint32_t p_shape_instance_id) const {
  310. const int shape_index = find_shape_index(p_shape_instance_id);
  311. return shape_index != -1 ? shapes[shape_index].get_shape() : nullptr;
  312. }
  313. JoltShape3D *JoltShapedObject3D::find_shape(const JPH::SubShapeID &p_sub_shape_id) const {
  314. const int shape_index = find_shape_index(p_sub_shape_id);
  315. return shape_index != -1 ? shapes[shape_index].get_shape() : nullptr;
  316. }
  317. Transform3D JoltShapedObject3D::get_shape_transform_unscaled(int p_index) const {
  318. ERR_FAIL_INDEX_V(p_index, (int)shapes.size(), Transform3D());
  319. return shapes[p_index].get_transform_unscaled();
  320. }
  321. Transform3D JoltShapedObject3D::get_shape_transform_scaled(int p_index) const {
  322. ERR_FAIL_INDEX_V(p_index, (int)shapes.size(), Transform3D());
  323. return shapes[p_index].get_transform_scaled();
  324. }
  325. void JoltShapedObject3D::set_shape_transform(int p_index, Transform3D p_transform) {
  326. ERR_FAIL_INDEX(p_index, (int)shapes.size());
  327. JOLT_ENSURE_SCALE_NOT_ZERO(p_transform, "Failed to correctly set transform for shape at index %d in body '%s'.");
  328. Vector3 new_scale;
  329. JoltMath::decompose(p_transform, new_scale);
  330. JoltShapeInstance3D &shape = shapes[p_index];
  331. if (shape.get_transform_unscaled() == p_transform && shape.get_scale() == new_scale) {
  332. return;
  333. }
  334. shape.set_transform(p_transform);
  335. shape.set_scale(new_scale);
  336. _shapes_changed();
  337. }
  338. Vector3 JoltShapedObject3D::get_shape_scale(int p_index) const {
  339. ERR_FAIL_INDEX_V(p_index, (int)shapes.size(), Vector3());
  340. return shapes[p_index].get_scale();
  341. }
  342. bool JoltShapedObject3D::is_shape_disabled(int p_index) const {
  343. ERR_FAIL_INDEX_V(p_index, (int)shapes.size(), false);
  344. return shapes[p_index].is_disabled();
  345. }
  346. void JoltShapedObject3D::set_shape_disabled(int p_index, bool p_disabled) {
  347. ERR_FAIL_INDEX(p_index, (int)shapes.size());
  348. JoltShapeInstance3D &shape = shapes[p_index];
  349. if (shape.is_disabled() == p_disabled) {
  350. return;
  351. }
  352. if (p_disabled) {
  353. shape.disable();
  354. } else {
  355. shape.enable();
  356. }
  357. _shapes_changed();
  358. }