jolt_shaped_object_3d.cpp 16 KB

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