space_sw.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*************************************************************************/
  2. /* space_sw.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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 SPACE_SW_H
  31. #define SPACE_SW_H
  32. #include "area_pair_sw.h"
  33. #include "area_sw.h"
  34. #include "body_pair_sw.h"
  35. #include "body_sw.h"
  36. #include "broad_phase_sw.h"
  37. #include "collision_object_sw.h"
  38. #include "core/hash_map.h"
  39. #include "core/project_settings.h"
  40. #include "core/typedefs.h"
  41. class PhysicsDirectSpaceStateSW : public PhysicsDirectSpaceState {
  42. GDCLASS(PhysicsDirectSpaceStateSW, PhysicsDirectSpaceState);
  43. public:
  44. SpaceSW *space;
  45. virtual int intersect_point(const Vector3 &p_point, ShapeResult *r_results, int p_result_max, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false);
  46. virtual bool intersect_ray(const Vector3 &p_from, const Vector3 &p_to, RayResult &r_result, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false, bool p_pick_ray = false);
  47. virtual int intersect_shape(const RID &p_shape, const Transform &p_xform, real_t p_margin, ShapeResult *r_results, int p_result_max, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false);
  48. virtual bool cast_motion(const RID &p_shape, const Transform &p_xform, const Vector3 &p_motion, real_t p_margin, real_t &p_closest_safe, real_t &p_closest_unsafe, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false, ShapeRestInfo *r_info = NULL);
  49. virtual bool collide_shape(RID p_shape, const Transform &p_shape_xform, real_t p_margin, Vector3 *r_results, int p_result_max, int &r_result_count, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false);
  50. virtual bool rest_info(RID p_shape, const Transform &p_shape_xform, real_t p_margin, ShapeRestInfo *r_info, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false);
  51. virtual Vector3 get_closest_point_to_object_volume(RID p_object, const Vector3 p_point) const;
  52. PhysicsDirectSpaceStateSW();
  53. };
  54. class SpaceSW : public RID_Data {
  55. public:
  56. enum ElapsedTime {
  57. ELAPSED_TIME_INTEGRATE_FORCES,
  58. ELAPSED_TIME_GENERATE_ISLANDS,
  59. ELAPSED_TIME_SETUP_CONSTRAINTS,
  60. ELAPSED_TIME_SOLVE_CONSTRAINTS,
  61. ELAPSED_TIME_INTEGRATE_VELOCITIES,
  62. ELAPSED_TIME_MAX
  63. };
  64. private:
  65. uint64_t elapsed_time[ELAPSED_TIME_MAX];
  66. PhysicsDirectSpaceStateSW *direct_access;
  67. RID self;
  68. BroadPhaseSW *broadphase;
  69. SelfList<BodySW>::List active_list;
  70. SelfList<BodySW>::List inertia_update_list;
  71. SelfList<BodySW>::List state_query_list;
  72. SelfList<AreaSW>::List monitor_query_list;
  73. SelfList<AreaSW>::List area_moved_list;
  74. static void *_broadphase_pair(CollisionObjectSW *A, int p_subindex_A, CollisionObjectSW *B, int p_subindex_B, void *p_self);
  75. static void _broadphase_unpair(CollisionObjectSW *A, int p_subindex_A, CollisionObjectSW *B, int p_subindex_B, void *p_data, void *p_self);
  76. Set<CollisionObjectSW *> objects;
  77. AreaSW *area;
  78. real_t contact_recycle_radius;
  79. real_t contact_max_separation;
  80. real_t contact_max_allowed_penetration;
  81. real_t constraint_bias;
  82. real_t test_motion_min_contact_depth;
  83. enum {
  84. INTERSECTION_QUERY_MAX = 2048
  85. };
  86. CollisionObjectSW *intersection_query_results[INTERSECTION_QUERY_MAX];
  87. int intersection_query_subindex_results[INTERSECTION_QUERY_MAX];
  88. real_t body_linear_velocity_sleep_threshold;
  89. real_t body_angular_velocity_sleep_threshold;
  90. real_t body_time_to_sleep;
  91. real_t body_angular_velocity_damp_ratio;
  92. bool locked;
  93. int island_count;
  94. int active_objects;
  95. int collision_pairs;
  96. RID static_global_body;
  97. Vector<Vector3> contact_debug;
  98. int contact_debug_count;
  99. friend class PhysicsDirectSpaceStateSW;
  100. int _cull_aabb_for_body(BodySW *p_body, const AABB &p_aabb);
  101. public:
  102. _FORCE_INLINE_ void set_self(const RID &p_self) { self = p_self; }
  103. _FORCE_INLINE_ RID get_self() const { return self; }
  104. void set_default_area(AreaSW *p_area) { area = p_area; }
  105. AreaSW *get_default_area() const { return area; }
  106. const SelfList<BodySW>::List &get_active_body_list() const;
  107. void body_add_to_active_list(SelfList<BodySW> *p_body);
  108. void body_remove_from_active_list(SelfList<BodySW> *p_body);
  109. void body_add_to_inertia_update_list(SelfList<BodySW> *p_body);
  110. void body_remove_from_inertia_update_list(SelfList<BodySW> *p_body);
  111. void body_add_to_state_query_list(SelfList<BodySW> *p_body);
  112. void body_remove_from_state_query_list(SelfList<BodySW> *p_body);
  113. void area_add_to_monitor_query_list(SelfList<AreaSW> *p_area);
  114. void area_remove_from_monitor_query_list(SelfList<AreaSW> *p_area);
  115. void area_add_to_moved_list(SelfList<AreaSW> *p_area);
  116. void area_remove_from_moved_list(SelfList<AreaSW> *p_area);
  117. const SelfList<AreaSW>::List &get_moved_area_list() const;
  118. BroadPhaseSW *get_broadphase();
  119. void add_object(CollisionObjectSW *p_object);
  120. void remove_object(CollisionObjectSW *p_object);
  121. const Set<CollisionObjectSW *> &get_objects() const;
  122. _FORCE_INLINE_ real_t get_contact_recycle_radius() const { return contact_recycle_radius; }
  123. _FORCE_INLINE_ real_t get_contact_max_separation() const { return contact_max_separation; }
  124. _FORCE_INLINE_ real_t get_contact_max_allowed_penetration() const { return contact_max_allowed_penetration; }
  125. _FORCE_INLINE_ real_t get_constraint_bias() const { return constraint_bias; }
  126. _FORCE_INLINE_ real_t get_body_linear_velocity_sleep_threshold() const { return body_linear_velocity_sleep_threshold; }
  127. _FORCE_INLINE_ real_t get_body_angular_velocity_sleep_threshold() const { return body_angular_velocity_sleep_threshold; }
  128. _FORCE_INLINE_ real_t get_body_time_to_sleep() const { return body_time_to_sleep; }
  129. _FORCE_INLINE_ real_t get_body_angular_velocity_damp_ratio() const { return body_angular_velocity_damp_ratio; }
  130. void update();
  131. void setup();
  132. void call_queries();
  133. bool is_locked() const;
  134. void lock();
  135. void unlock();
  136. void set_param(PhysicsServer::SpaceParameter p_param, real_t p_value);
  137. real_t get_param(PhysicsServer::SpaceParameter p_param) const;
  138. void set_island_count(int p_island_count) { island_count = p_island_count; }
  139. int get_island_count() const { return island_count; }
  140. void set_active_objects(int p_active_objects) { active_objects = p_active_objects; }
  141. int get_active_objects() const { return active_objects; }
  142. int get_collision_pairs() const { return collision_pairs; }
  143. PhysicsDirectSpaceStateSW *get_direct_state();
  144. void set_debug_contacts(int p_amount) { contact_debug.resize(p_amount); }
  145. _FORCE_INLINE_ bool is_debugging_contacts() const { return !contact_debug.empty(); }
  146. _FORCE_INLINE_ void add_debug_contact(const Vector3 &p_contact) {
  147. if (contact_debug_count < contact_debug.size()) contact_debug.write[contact_debug_count++] = p_contact;
  148. }
  149. _FORCE_INLINE_ Vector<Vector3> get_debug_contacts() { return contact_debug; }
  150. _FORCE_INLINE_ int get_debug_contact_count() { return contact_debug_count; }
  151. void set_static_global_body(RID p_body) { static_global_body = p_body; }
  152. RID get_static_global_body() { return static_global_body; }
  153. void set_elapsed_time(ElapsedTime p_time, uint64_t p_msec) { elapsed_time[p_time] = p_msec; }
  154. uint64_t get_elapsed_time(ElapsedTime p_time) const { return elapsed_time[p_time]; }
  155. int test_body_ray_separation(BodySW *p_body, const Transform &p_transform, bool p_infinite_inertia, Vector3 &r_recover_motion, PhysicsServer::SeparationResult *r_results, int p_result_max, real_t p_margin);
  156. bool test_body_motion(BodySW *p_body, const Transform &p_from, const Vector3 &p_motion, bool p_infinite_inertia, real_t p_margin, PhysicsServer::MotionResult *r_result, bool p_exclude_raycast_shapes);
  157. SpaceSW();
  158. ~SpaceSW();
  159. };
  160. #endif // SPACE__SW_H