2
0

jolt_space_3d.h 6.7 KB

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