godot_space_3d.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /**************************************************************************/
  2. /* godot_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 GODOT_SPACE_3D_H
  31. #define GODOT_SPACE_3D_H
  32. #include "godot_area_3d.h"
  33. #include "godot_area_pair_3d.h"
  34. #include "godot_body_3d.h"
  35. #include "godot_body_pair_3d.h"
  36. #include "godot_broad_phase_3d.h"
  37. #include "godot_collision_object_3d.h"
  38. #include "godot_soft_body_3d.h"
  39. #include "core/config/project_settings.h"
  40. #include "core/templates/hash_map.h"
  41. #include "core/typedefs.h"
  42. class GodotPhysicsDirectSpaceState3D : public PhysicsDirectSpaceState3D {
  43. GDCLASS(GodotPhysicsDirectSpaceState3D, PhysicsDirectSpaceState3D);
  44. public:
  45. GodotSpace3D *space = nullptr;
  46. virtual int intersect_point(const PointParameters &p_parameters, ShapeResult *r_results, int p_result_max) override;
  47. virtual bool intersect_ray(const RayParameters &p_parameters, RayResult &r_result) override;
  48. virtual int intersect_shape(const ShapeParameters &p_parameters, ShapeResult *r_results, int p_result_max) override;
  49. virtual bool cast_motion(const ShapeParameters &p_parameters, real_t &p_closest_safe, real_t &p_closest_unsafe, ShapeRestInfo *r_info = nullptr) override;
  50. virtual bool collide_shape(const ShapeParameters &p_parameters, Vector3 *r_results, int p_result_max, int &r_result_count) override;
  51. virtual bool rest_info(const ShapeParameters &p_parameters, ShapeRestInfo *r_info) override;
  52. virtual Vector3 get_closest_point_to_object_volume(RID p_object, const Vector3 p_point) const override;
  53. GodotPhysicsDirectSpaceState3D();
  54. };
  55. class GodotSpace3D {
  56. public:
  57. enum ElapsedTime {
  58. ELAPSED_TIME_INTEGRATE_FORCES,
  59. ELAPSED_TIME_GENERATE_ISLANDS,
  60. ELAPSED_TIME_SETUP_CONSTRAINTS,
  61. ELAPSED_TIME_SOLVE_CONSTRAINTS,
  62. ELAPSED_TIME_INTEGRATE_VELOCITIES,
  63. ELAPSED_TIME_MAX
  64. };
  65. private:
  66. uint64_t elapsed_time[ELAPSED_TIME_MAX] = {};
  67. GodotPhysicsDirectSpaceState3D *direct_access = nullptr;
  68. RID self;
  69. GodotBroadPhase3D *broadphase = nullptr;
  70. SelfList<GodotBody3D>::List active_list;
  71. SelfList<GodotBody3D>::List mass_properties_update_list;
  72. SelfList<GodotBody3D>::List state_query_list;
  73. SelfList<GodotArea3D>::List monitor_query_list;
  74. SelfList<GodotArea3D>::List area_moved_list;
  75. SelfList<GodotSoftBody3D>::List active_soft_body_list;
  76. static void *_broadphase_pair(GodotCollisionObject3D *A, int p_subindex_A, GodotCollisionObject3D *B, int p_subindex_B, void *p_self);
  77. static void _broadphase_unpair(GodotCollisionObject3D *A, int p_subindex_A, GodotCollisionObject3D *B, int p_subindex_B, void *p_data, void *p_self);
  78. HashSet<GodotCollisionObject3D *> objects;
  79. GodotArea3D *area = nullptr;
  80. int solver_iterations = 0;
  81. real_t contact_recycle_radius = 0.0;
  82. real_t contact_max_separation = 0.0;
  83. real_t contact_max_allowed_penetration = 0.0;
  84. real_t contact_bias = 0.0;
  85. enum {
  86. INTERSECTION_QUERY_MAX = 2048
  87. };
  88. GodotCollisionObject3D *intersection_query_results[INTERSECTION_QUERY_MAX];
  89. int intersection_query_subindex_results[INTERSECTION_QUERY_MAX];
  90. real_t body_linear_velocity_sleep_threshold = 0.0;
  91. real_t body_angular_velocity_sleep_threshold = 0.0;
  92. real_t body_time_to_sleep = 0.0;
  93. bool locked = false;
  94. real_t last_step = 0.001;
  95. int island_count = 0;
  96. int active_objects = 0;
  97. int collision_pairs = 0;
  98. RID static_global_body;
  99. Vector<Vector3> contact_debug;
  100. int contact_debug_count = 0;
  101. friend class GodotPhysicsDirectSpaceState3D;
  102. int _cull_aabb_for_body(GodotBody3D *p_body, const AABB &p_aabb);
  103. public:
  104. _FORCE_INLINE_ void set_self(const RID &p_self) { self = p_self; }
  105. _FORCE_INLINE_ RID get_self() const { return self; }
  106. void set_default_area(GodotArea3D *p_area) { area = p_area; }
  107. GodotArea3D *get_default_area() const { return area; }
  108. const SelfList<GodotBody3D>::List &get_active_body_list() const;
  109. void body_add_to_active_list(SelfList<GodotBody3D> *p_body);
  110. void body_remove_from_active_list(SelfList<GodotBody3D> *p_body);
  111. void body_add_to_mass_properties_update_list(SelfList<GodotBody3D> *p_body);
  112. void body_remove_from_mass_properties_update_list(SelfList<GodotBody3D> *p_body);
  113. void body_add_to_state_query_list(SelfList<GodotBody3D> *p_body);
  114. void body_remove_from_state_query_list(SelfList<GodotBody3D> *p_body);
  115. void area_add_to_monitor_query_list(SelfList<GodotArea3D> *p_area);
  116. void area_remove_from_monitor_query_list(SelfList<GodotArea3D> *p_area);
  117. void area_add_to_moved_list(SelfList<GodotArea3D> *p_area);
  118. void area_remove_from_moved_list(SelfList<GodotArea3D> *p_area);
  119. const SelfList<GodotArea3D>::List &get_moved_area_list() const;
  120. const SelfList<GodotSoftBody3D>::List &get_active_soft_body_list() const;
  121. void soft_body_add_to_active_list(SelfList<GodotSoftBody3D> *p_soft_body);
  122. void soft_body_remove_from_active_list(SelfList<GodotSoftBody3D> *p_soft_body);
  123. GodotBroadPhase3D *get_broadphase();
  124. void add_object(GodotCollisionObject3D *p_object);
  125. void remove_object(GodotCollisionObject3D *p_object);
  126. const HashSet<GodotCollisionObject3D *> &get_objects() const;
  127. _FORCE_INLINE_ int get_solver_iterations() const { return solver_iterations; }
  128. _FORCE_INLINE_ real_t get_contact_recycle_radius() const { return contact_recycle_radius; }
  129. _FORCE_INLINE_ real_t get_contact_max_separation() const { return contact_max_separation; }
  130. _FORCE_INLINE_ real_t get_contact_max_allowed_penetration() const { return contact_max_allowed_penetration; }
  131. _FORCE_INLINE_ real_t get_contact_bias() const { return contact_bias; }
  132. _FORCE_INLINE_ real_t get_body_linear_velocity_sleep_threshold() const { return body_linear_velocity_sleep_threshold; }
  133. _FORCE_INLINE_ real_t get_body_angular_velocity_sleep_threshold() const { return body_angular_velocity_sleep_threshold; }
  134. _FORCE_INLINE_ real_t get_body_time_to_sleep() const { return body_time_to_sleep; }
  135. void update();
  136. void setup();
  137. void call_queries();
  138. bool is_locked() const;
  139. void lock();
  140. void unlock();
  141. real_t get_last_step() const { return last_step; }
  142. void set_last_step(real_t p_step) { last_step = p_step; }
  143. void set_param(PhysicsServer3D::SpaceParameter p_param, real_t p_value);
  144. real_t get_param(PhysicsServer3D::SpaceParameter p_param) const;
  145. void set_island_count(int p_island_count) { island_count = p_island_count; }
  146. int get_island_count() const { return island_count; }
  147. void set_active_objects(int p_active_objects) { active_objects = p_active_objects; }
  148. int get_active_objects() const { return active_objects; }
  149. int get_collision_pairs() const { return collision_pairs; }
  150. GodotPhysicsDirectSpaceState3D *get_direct_state();
  151. void set_debug_contacts(int p_amount) { contact_debug.resize(p_amount); }
  152. _FORCE_INLINE_ bool is_debugging_contacts() const { return !contact_debug.is_empty(); }
  153. _FORCE_INLINE_ void add_debug_contact(const Vector3 &p_contact) {
  154. if (contact_debug_count < contact_debug.size()) {
  155. contact_debug.write[contact_debug_count++] = p_contact;
  156. }
  157. }
  158. _FORCE_INLINE_ Vector<Vector3> get_debug_contacts() { return contact_debug; }
  159. _FORCE_INLINE_ int get_debug_contact_count() { return contact_debug_count; }
  160. void set_static_global_body(RID p_body) { static_global_body = p_body; }
  161. RID get_static_global_body() { return static_global_body; }
  162. void set_elapsed_time(ElapsedTime p_time, uint64_t p_msec) { elapsed_time[p_time] = p_msec; }
  163. uint64_t get_elapsed_time(ElapsedTime p_time) const { return elapsed_time[p_time]; }
  164. bool test_body_motion(GodotBody3D *p_body, const PhysicsServer3D::MotionParameters &p_parameters, PhysicsServer3D::MotionResult *r_result);
  165. GodotSpace3D();
  166. ~GodotSpace3D();
  167. };
  168. #endif // GODOT_SPACE_3D_H