space_2d_sw.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*************************************************************************/
  2. /* space_2d_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_2D_SW_H
  31. #define SPACE_2D_SW_H
  32. #include "area_2d_sw.h"
  33. #include "area_pair_2d_sw.h"
  34. #include "body_2d_sw.h"
  35. #include "body_pair_2d_sw.h"
  36. #include "broad_phase_2d_sw.h"
  37. #include "collision_object_2d_sw.h"
  38. #include "core/hash_map.h"
  39. #include "core/project_settings.h"
  40. #include "core/typedefs.h"
  41. class Physics2DDirectSpaceStateSW : public Physics2DDirectSpaceState {
  42. GDCLASS(Physics2DDirectSpaceStateSW, Physics2DDirectSpaceState);
  43. int _intersect_point_impl(const Vector2 &p_point, ShapeResult *r_results, int p_result_max, const Set<RID> &p_exclude, uint32_t p_collision_mask, bool p_collide_with_bodies, bool p_collide_with_areas, bool p_pick_point, bool p_filter_by_canvas = false, ObjectID p_canvas_instance_id = 0);
  44. public:
  45. Space2DSW *space;
  46. virtual int intersect_point(const Vector2 &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, bool p_pick_point = false);
  47. virtual int intersect_point_on_canvas(const Vector2 &p_point, ObjectID p_canvas_instance_id, 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, bool p_pick_point = false);
  48. virtual bool intersect_ray(const Vector2 &p_from, const Vector2 &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);
  49. virtual int intersect_shape(const RID &p_shape, const Transform2D &p_xform, const Vector2 &p_motion, 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);
  50. virtual bool cast_motion(const RID &p_shape, const Transform2D &p_xform, const Vector2 &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);
  51. virtual bool collide_shape(RID p_shape, const Transform2D &p_shape_xform, const Vector2 &p_motion, real_t p_margin, Vector2 *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);
  52. virtual bool rest_info(RID p_shape, const Transform2D &p_shape_xform, const Vector2 &p_motion, 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);
  53. Physics2DDirectSpaceStateSW();
  54. };
  55. class Space2DSW : public RID_Data {
  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. struct ExcludedShapeSW {
  67. Shape2DSW *local_shape;
  68. const CollisionObject2DSW *against_object;
  69. int against_shape_index;
  70. };
  71. uint64_t elapsed_time[ELAPSED_TIME_MAX];
  72. Physics2DDirectSpaceStateSW *direct_access;
  73. RID self;
  74. BroadPhase2DSW *broadphase;
  75. SelfList<Body2DSW>::List active_list;
  76. SelfList<Body2DSW>::List inertia_update_list;
  77. SelfList<Body2DSW>::List state_query_list;
  78. SelfList<Area2DSW>::List monitor_query_list;
  79. SelfList<Area2DSW>::List area_moved_list;
  80. static void *_broadphase_pair(CollisionObject2DSW *A, int p_subindex_A, CollisionObject2DSW *B, int p_subindex_B, void *p_self);
  81. static void _broadphase_unpair(CollisionObject2DSW *A, int p_subindex_A, CollisionObject2DSW *B, int p_subindex_B, void *p_data, void *p_self);
  82. Set<CollisionObject2DSW *> objects;
  83. Area2DSW *area;
  84. real_t contact_recycle_radius;
  85. real_t contact_max_separation;
  86. real_t contact_max_allowed_penetration;
  87. real_t constraint_bias;
  88. enum {
  89. INTERSECTION_QUERY_MAX = 2048
  90. };
  91. CollisionObject2DSW *intersection_query_results[INTERSECTION_QUERY_MAX];
  92. int intersection_query_subindex_results[INTERSECTION_QUERY_MAX];
  93. real_t body_linear_velocity_sleep_threshold;
  94. real_t body_angular_velocity_sleep_threshold;
  95. real_t body_time_to_sleep;
  96. bool locked;
  97. int island_count;
  98. int active_objects;
  99. int collision_pairs;
  100. int _cull_aabb_for_body(Body2DSW *p_body, const Rect2 &p_aabb);
  101. Vector<Vector2> contact_debug;
  102. int contact_debug_count;
  103. friend class Physics2DDirectSpaceStateSW;
  104. public:
  105. _FORCE_INLINE_ void set_self(const RID &p_self) { self = p_self; }
  106. _FORCE_INLINE_ RID get_self() const { return self; }
  107. void set_default_area(Area2DSW *p_area) { area = p_area; }
  108. Area2DSW *get_default_area() const { return area; }
  109. const SelfList<Body2DSW>::List &get_active_body_list() const;
  110. void body_add_to_active_list(SelfList<Body2DSW> *p_body);
  111. void body_remove_from_active_list(SelfList<Body2DSW> *p_body);
  112. void body_add_to_inertia_update_list(SelfList<Body2DSW> *p_body);
  113. void body_remove_from_inertia_update_list(SelfList<Body2DSW> *p_body);
  114. void area_add_to_moved_list(SelfList<Area2DSW> *p_area);
  115. void area_remove_from_moved_list(SelfList<Area2DSW> *p_area);
  116. const SelfList<Area2DSW>::List &get_moved_area_list() const;
  117. void body_add_to_state_query_list(SelfList<Body2DSW> *p_body);
  118. void body_remove_from_state_query_list(SelfList<Body2DSW> *p_body);
  119. void area_add_to_monitor_query_list(SelfList<Area2DSW> *p_area);
  120. void area_remove_from_monitor_query_list(SelfList<Area2DSW> *p_area);
  121. BroadPhase2DSW *get_broadphase();
  122. void add_object(CollisionObject2DSW *p_object);
  123. void remove_object(CollisionObject2DSW *p_object);
  124. const Set<CollisionObject2DSW *> &get_objects() const;
  125. _FORCE_INLINE_ real_t get_contact_recycle_radius() const { return contact_recycle_radius; }
  126. _FORCE_INLINE_ real_t get_contact_max_separation() const { return contact_max_separation; }
  127. _FORCE_INLINE_ real_t get_contact_max_allowed_penetration() const { return contact_max_allowed_penetration; }
  128. _FORCE_INLINE_ real_t get_constraint_bias() const { return constraint_bias; }
  129. _FORCE_INLINE_ real_t get_body_linear_velocity_sleep_threshold() const { return body_linear_velocity_sleep_threshold; }
  130. _FORCE_INLINE_ real_t get_body_angular_velocity_sleep_threshold() const { return body_angular_velocity_sleep_threshold; }
  131. _FORCE_INLINE_ real_t get_body_time_to_sleep() const { return body_time_to_sleep; }
  132. void update();
  133. void setup();
  134. void call_queries();
  135. bool is_locked() const;
  136. void lock();
  137. void unlock();
  138. void set_param(Physics2DServer::SpaceParameter p_param, real_t p_value);
  139. real_t get_param(Physics2DServer::SpaceParameter p_param) const;
  140. void set_island_count(int p_island_count) { island_count = p_island_count; }
  141. int get_island_count() const { return island_count; }
  142. void set_active_objects(int p_active_objects) { active_objects = p_active_objects; }
  143. int get_active_objects() const { return active_objects; }
  144. int get_collision_pairs() const { return collision_pairs; }
  145. bool test_body_motion(Body2DSW *p_body, const Transform2D &p_from, const Vector2 &p_motion, bool p_infinite_inertia, real_t p_margin, Physics2DServer::MotionResult *r_result, bool p_exclude_raycast_shapes = true);
  146. int test_body_ray_separation(Body2DSW *p_body, const Transform2D &p_transform, bool p_infinite_inertia, Vector2 &r_recover_motion, Physics2DServer::SeparationResult *r_results, int p_result_max, real_t p_margin);
  147. void set_debug_contacts(int p_amount) { contact_debug.resize(p_amount); }
  148. _FORCE_INLINE_ bool is_debugging_contacts() const { return !contact_debug.empty(); }
  149. _FORCE_INLINE_ void add_debug_contact(const Vector2 &p_contact) {
  150. if (contact_debug_count < contact_debug.size()) contact_debug.write[contact_debug_count++] = p_contact;
  151. }
  152. _FORCE_INLINE_ Vector<Vector2> get_debug_contacts() { return contact_debug; }
  153. _FORCE_INLINE_ int get_debug_contact_count() { return contact_debug_count; }
  154. Physics2DDirectSpaceStateSW *get_direct_state();
  155. void set_elapsed_time(ElapsedTime p_time, uint64_t p_msec) { elapsed_time[p_time] = p_msec; }
  156. uint64_t get_elapsed_time(ElapsedTime p_time) const { return elapsed_time[p_time]; }
  157. Space2DSW();
  158. ~Space2DSW();
  159. };
  160. #endif // SPACE_2D_SW_H