jolt_space_3d.h 6.6 KB

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