space_sw.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. enum {
  83. INTERSECTION_QUERY_MAX = 2048
  84. };
  85. CollisionObjectSW *intersection_query_results[INTERSECTION_QUERY_MAX];
  86. int intersection_query_subindex_results[INTERSECTION_QUERY_MAX];
  87. real_t body_linear_velocity_sleep_threshold;
  88. real_t body_angular_velocity_sleep_threshold;
  89. real_t body_time_to_sleep;
  90. real_t body_angular_velocity_damp_ratio;
  91. bool locked;
  92. int island_count;
  93. int active_objects;
  94. int collision_pairs;
  95. RID static_global_body;
  96. Vector<Vector3> contact_debug;
  97. int contact_debug_count;
  98. friend class PhysicsDirectSpaceStateSW;
  99. int _cull_aabb_for_body(BodySW *p_body, const AABB &p_aabb);
  100. public:
  101. _FORCE_INLINE_ void set_self(const RID &p_self) { self = p_self; }
  102. _FORCE_INLINE_ RID get_self() const { return self; }
  103. void set_default_area(AreaSW *p_area) { area = p_area; }
  104. AreaSW *get_default_area() const { return area; }
  105. const SelfList<BodySW>::List &get_active_body_list() const;
  106. void body_add_to_active_list(SelfList<BodySW> *p_body);
  107. void body_remove_from_active_list(SelfList<BodySW> *p_body);
  108. void body_add_to_inertia_update_list(SelfList<BodySW> *p_body);
  109. void body_remove_from_inertia_update_list(SelfList<BodySW> *p_body);
  110. void body_add_to_state_query_list(SelfList<BodySW> *p_body);
  111. void body_remove_from_state_query_list(SelfList<BodySW> *p_body);
  112. void area_add_to_monitor_query_list(SelfList<AreaSW> *p_area);
  113. void area_remove_from_monitor_query_list(SelfList<AreaSW> *p_area);
  114. void area_add_to_moved_list(SelfList<AreaSW> *p_area);
  115. void area_remove_from_moved_list(SelfList<AreaSW> *p_area);
  116. const SelfList<AreaSW>::List &get_moved_area_list() const;
  117. BroadPhaseSW *get_broadphase();
  118. void add_object(CollisionObjectSW *p_object);
  119. void remove_object(CollisionObjectSW *p_object);
  120. const Set<CollisionObjectSW *> &get_objects() const;
  121. _FORCE_INLINE_ real_t get_contact_recycle_radius() const { return contact_recycle_radius; }
  122. _FORCE_INLINE_ real_t get_contact_max_separation() const { return contact_max_separation; }
  123. _FORCE_INLINE_ real_t get_contact_max_allowed_penetration() const { return contact_max_allowed_penetration; }
  124. _FORCE_INLINE_ real_t get_constraint_bias() const { return constraint_bias; }
  125. _FORCE_INLINE_ real_t get_body_linear_velocity_sleep_threshold() const { return body_linear_velocity_sleep_threshold; }
  126. _FORCE_INLINE_ real_t get_body_angular_velocity_sleep_threshold() const { return body_angular_velocity_sleep_threshold; }
  127. _FORCE_INLINE_ real_t get_body_time_to_sleep() const { return body_time_to_sleep; }
  128. _FORCE_INLINE_ real_t get_body_angular_velocity_damp_ratio() const { return body_angular_velocity_damp_ratio; }
  129. void update();
  130. void setup();
  131. void call_queries();
  132. bool is_locked() const;
  133. void lock();
  134. void unlock();
  135. void set_param(PhysicsServer::SpaceParameter p_param, real_t p_value);
  136. real_t get_param(PhysicsServer::SpaceParameter p_param) const;
  137. void set_island_count(int p_island_count) { island_count = p_island_count; }
  138. int get_island_count() const { return island_count; }
  139. void set_active_objects(int p_active_objects) { active_objects = p_active_objects; }
  140. int get_active_objects() const { return active_objects; }
  141. int get_collision_pairs() const { return collision_pairs; }
  142. PhysicsDirectSpaceStateSW *get_direct_state();
  143. void set_debug_contacts(int p_amount) { contact_debug.resize(p_amount); }
  144. _FORCE_INLINE_ bool is_debugging_contacts() const { return !contact_debug.empty(); }
  145. _FORCE_INLINE_ void add_debug_contact(const Vector3 &p_contact) {
  146. if (contact_debug_count < contact_debug.size()) contact_debug.write[contact_debug_count++] = p_contact;
  147. }
  148. _FORCE_INLINE_ Vector<Vector3> get_debug_contacts() { return contact_debug; }
  149. _FORCE_INLINE_ int get_debug_contact_count() { return contact_debug_count; }
  150. void set_static_global_body(RID p_body) { static_global_body = p_body; }
  151. RID get_static_global_body() { return static_global_body; }
  152. void set_elapsed_time(ElapsedTime p_time, uint64_t p_msec) { elapsed_time[p_time] = p_msec; }
  153. uint64_t get_elapsed_time(ElapsedTime p_time) const { return elapsed_time[p_time]; }
  154. 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);
  155. 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);
  156. SpaceSW();
  157. ~SpaceSW();
  158. };
  159. #endif // SPACE__SW_H