jolt_space_3d.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**************************************************************************/
  2. /* jolt_space_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. #pragma once
  31. #include "servers/physics_server_3d.h"
  32. #include "Jolt/Jolt.h"
  33. #include "Jolt/Core/JobSystem.h"
  34. #include "Jolt/Core/TempAllocator.h"
  35. #include "Jolt/Physics/Body/BodyInterface.h"
  36. #include "Jolt/Physics/Collision/BroadPhase/BroadPhaseQuery.h"
  37. #include "Jolt/Physics/Collision/NarrowPhaseQuery.h"
  38. #include "Jolt/Physics/Constraints/Constraint.h"
  39. #include "Jolt/Physics/PhysicsSystem.h"
  40. class JoltArea3D;
  41. class JoltBody3D;
  42. class JoltBodyActivationListener3D;
  43. class JoltContactListener3D;
  44. class JoltJoint3D;
  45. class JoltLayers;
  46. class JoltObject3D;
  47. class JoltPhysicsDirectSpaceState3D;
  48. class JoltShapedObject3D;
  49. class JoltSoftBody3D;
  50. class JoltSpace3D {
  51. Mutex pending_objects_mutex;
  52. Mutex body_call_queries_mutex;
  53. SelfList<JoltBody3D>::List body_call_queries_list;
  54. SelfList<JoltArea3D>::List area_call_queries_list;
  55. SelfList<JoltShapedObject3D>::List shapes_changed_list;
  56. SelfList<JoltShapedObject3D>::List needs_optimization_list;
  57. LocalVector<JPH::BodyID> pending_objects_sleeping;
  58. LocalVector<JPH::BodyID> pending_objects_awake;
  59. RID rid;
  60. JPH::JobSystem *job_system = nullptr;
  61. JPH::TempAllocator *temp_allocator = nullptr;
  62. JoltLayers *layers = nullptr;
  63. JoltContactListener3D *contact_listener = nullptr;
  64. JoltBodyActivationListener3D *body_activation_listener = nullptr;
  65. JPH::PhysicsSystem *physics_system = nullptr;
  66. JoltPhysicsDirectSpaceState3D *direct_state = nullptr;
  67. JoltArea3D *default_area = nullptr;
  68. float last_step = 0.0f;
  69. bool active = false;
  70. bool stepping = false;
  71. void _pre_step(float p_step);
  72. void _post_step(float p_step);
  73. public:
  74. explicit JoltSpace3D(JPH::JobSystem *p_job_system);
  75. ~JoltSpace3D();
  76. void step(float p_step);
  77. void call_queries();
  78. RID get_rid() const { return rid; }
  79. void set_rid(const RID &p_rid) { rid = p_rid; }
  80. bool is_active() const { return active; }
  81. void set_active(bool p_active) { active = p_active; }
  82. bool is_stepping() const { return stepping; }
  83. double get_param(PhysicsServer3D::SpaceParameter p_param) const;
  84. void set_param(PhysicsServer3D::SpaceParameter p_param, double p_value);
  85. JPH::PhysicsSystem &get_physics_system() const { return *physics_system; }
  86. JPH::TempAllocator &get_temp_allocator() const { return *temp_allocator; }
  87. JPH::BodyInterface &get_body_iface();
  88. const JPH::BodyInterface &get_body_iface() const;
  89. const JPH::BodyLockInterface &get_lock_iface() const;
  90. const JPH::BroadPhaseQuery &get_broad_phase_query() const;
  91. const JPH::NarrowPhaseQuery &get_narrow_phase_query() const;
  92. JPH::ObjectLayer map_to_object_layer(JPH::BroadPhaseLayer p_broad_phase_layer, uint32_t p_collision_layer, uint32_t p_collision_mask);
  93. void map_from_object_layer(JPH::ObjectLayer p_object_layer, JPH::BroadPhaseLayer &r_broad_phase_layer, uint32_t &r_collision_layer, uint32_t &r_collision_mask) const;
  94. JPH::Body *try_get_jolt_body(const JPH::BodyID &p_body_id) const;
  95. JoltObject3D *try_get_object(const JPH::BodyID &p_body_id) const;
  96. JoltShapedObject3D *try_get_shaped(const JPH::BodyID &p_body_id) const;
  97. JoltBody3D *try_get_body(const JPH::BodyID &p_body_id) const;
  98. JoltArea3D *try_get_area(const JPH::BodyID &p_body_id) const;
  99. JoltSoftBody3D *try_get_soft_body(const JPH::BodyID &p_body_id) const;
  100. JoltPhysicsDirectSpaceState3D *get_direct_state();
  101. JoltArea3D *get_default_area() const { return default_area; }
  102. void set_default_area(JoltArea3D *p_area);
  103. float get_last_step() const { return last_step; }
  104. JPH::Body *add_object(const JoltObject3D &p_object, const JPH::BodyCreationSettings &p_settings, bool p_sleeping = false);
  105. JPH::Body *add_object(const JoltObject3D &p_object, const JPH::SoftBodyCreationSettings &p_settings, bool p_sleeping = false);
  106. void remove_object(const JPH::BodyID &p_jolt_id);
  107. void flush_pending_objects();
  108. void set_is_object_sleeping(const JPH::BodyID &p_jolt_id, bool p_enable);
  109. void enqueue_call_queries(SelfList<JoltBody3D> *p_body);
  110. void enqueue_call_queries(SelfList<JoltArea3D> *p_area);
  111. void dequeue_call_queries(SelfList<JoltBody3D> *p_body);
  112. void dequeue_call_queries(SelfList<JoltArea3D> *p_area);
  113. void enqueue_shapes_changed(SelfList<JoltShapedObject3D> *p_object);
  114. void dequeue_shapes_changed(SelfList<JoltShapedObject3D> *p_object);
  115. void enqueue_needs_optimization(SelfList<JoltShapedObject3D> *p_object);
  116. void dequeue_needs_optimization(SelfList<JoltShapedObject3D> *p_object);
  117. void add_joint(JPH::Constraint *p_jolt_ref);
  118. void add_joint(JoltJoint3D *p_joint);
  119. void remove_joint(JPH::Constraint *p_jolt_ref);
  120. void remove_joint(JoltJoint3D *p_joint);
  121. #ifdef DEBUG_ENABLED
  122. void dump_debug_snapshot(const String &p_dir);
  123. const PackedVector3Array &get_debug_contacts() const;
  124. int get_debug_contact_count() const;
  125. int get_max_debug_contacts() const;
  126. void set_max_debug_contacts(int p_count);
  127. #endif
  128. };