godot_area_2d.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /**************************************************************************/
  2. /* godot_area_2d.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_AREA_2D_H
  31. #define GODOT_AREA_2D_H
  32. #include "godot_collision_object_2d.h"
  33. #include "core/templates/self_list.h"
  34. #include "servers/physics_server_2d.h"
  35. class GodotSpace2D;
  36. class GodotBody2D;
  37. class GodotConstraint2D;
  38. class GodotArea2D : public GodotCollisionObject2D {
  39. PhysicsServer2D::AreaSpaceOverrideMode gravity_override_mode = PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED;
  40. PhysicsServer2D::AreaSpaceOverrideMode linear_damping_override_mode = PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED;
  41. PhysicsServer2D::AreaSpaceOverrideMode angular_damping_override_mode = PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED;
  42. real_t gravity = 9.80665;
  43. Vector2 gravity_vector = Vector2(0, -1);
  44. bool gravity_is_point = false;
  45. real_t gravity_point_unit_distance = 0.0;
  46. real_t linear_damp = 0.1;
  47. real_t angular_damp = 1.0;
  48. int priority = 0;
  49. bool monitorable = false;
  50. Callable monitor_callback;
  51. Callable area_monitor_callback;
  52. SelfList<GodotArea2D> monitor_query_list;
  53. SelfList<GodotArea2D> moved_list;
  54. struct BodyKey {
  55. RID rid;
  56. ObjectID instance_id;
  57. uint32_t body_shape = 0;
  58. uint32_t area_shape = 0;
  59. static uint32_t hash(const BodyKey &p_key) {
  60. uint32_t h = hash_one_uint64(p_key.rid.get_id());
  61. h = hash_murmur3_one_64(p_key.instance_id, h);
  62. h = hash_murmur3_one_32(p_key.area_shape, h);
  63. return hash_fmix32(hash_murmur3_one_32(p_key.body_shape, h));
  64. }
  65. _FORCE_INLINE_ bool operator==(const BodyKey &p_key) const {
  66. return rid == p_key.rid && instance_id == p_key.instance_id && body_shape == p_key.body_shape && area_shape == p_key.area_shape;
  67. }
  68. _FORCE_INLINE_ BodyKey() {}
  69. BodyKey(GodotBody2D *p_body, uint32_t p_body_shape, uint32_t p_area_shape);
  70. BodyKey(GodotArea2D *p_body, uint32_t p_body_shape, uint32_t p_area_shape);
  71. };
  72. struct BodyState {
  73. int state = 0;
  74. _FORCE_INLINE_ void inc() { state++; }
  75. _FORCE_INLINE_ void dec() { state--; }
  76. };
  77. HashMap<BodyKey, BodyState, BodyKey> monitored_bodies;
  78. HashMap<BodyKey, BodyState, BodyKey> monitored_areas;
  79. HashSet<GodotConstraint2D *> constraints;
  80. virtual void _shapes_changed() override;
  81. void _queue_monitor_update();
  82. void _set_space_override_mode(PhysicsServer2D::AreaSpaceOverrideMode &r_mode, PhysicsServer2D::AreaSpaceOverrideMode p_new_mode);
  83. public:
  84. void set_monitor_callback(const Callable &p_callback);
  85. _FORCE_INLINE_ bool has_monitor_callback() const { return !monitor_callback.is_null(); }
  86. void set_area_monitor_callback(const Callable &p_callback);
  87. _FORCE_INLINE_ bool has_area_monitor_callback() const { return !area_monitor_callback.is_null(); }
  88. _FORCE_INLINE_ void add_body_to_query(GodotBody2D *p_body, uint32_t p_body_shape, uint32_t p_area_shape);
  89. _FORCE_INLINE_ void remove_body_from_query(GodotBody2D *p_body, uint32_t p_body_shape, uint32_t p_area_shape);
  90. _FORCE_INLINE_ void add_area_to_query(GodotArea2D *p_area, uint32_t p_area_shape, uint32_t p_self_shape);
  91. _FORCE_INLINE_ void remove_area_from_query(GodotArea2D *p_area, uint32_t p_area_shape, uint32_t p_self_shape);
  92. void set_param(PhysicsServer2D::AreaParameter p_param, const Variant &p_value);
  93. Variant get_param(PhysicsServer2D::AreaParameter p_param) const;
  94. _FORCE_INLINE_ void set_gravity(real_t p_gravity) { gravity = p_gravity; }
  95. _FORCE_INLINE_ real_t get_gravity() const { return gravity; }
  96. _FORCE_INLINE_ void set_gravity_vector(const Vector2 &p_gravity) { gravity_vector = p_gravity; }
  97. _FORCE_INLINE_ Vector2 get_gravity_vector() const { return gravity_vector; }
  98. _FORCE_INLINE_ void set_gravity_as_point(bool p_enable) { gravity_is_point = p_enable; }
  99. _FORCE_INLINE_ bool is_gravity_point() const { return gravity_is_point; }
  100. _FORCE_INLINE_ void set_gravity_point_unit_distance(real_t scale) { gravity_point_unit_distance = scale; }
  101. _FORCE_INLINE_ real_t get_gravity_point_unit_distance() const { return gravity_point_unit_distance; }
  102. _FORCE_INLINE_ void set_linear_damp(real_t p_linear_damp) { linear_damp = p_linear_damp; }
  103. _FORCE_INLINE_ real_t get_linear_damp() const { return linear_damp; }
  104. _FORCE_INLINE_ void set_angular_damp(real_t p_angular_damp) { angular_damp = p_angular_damp; }
  105. _FORCE_INLINE_ real_t get_angular_damp() const { return angular_damp; }
  106. _FORCE_INLINE_ void set_priority(int p_priority) { priority = p_priority; }
  107. _FORCE_INLINE_ int get_priority() const { return priority; }
  108. _FORCE_INLINE_ void add_constraint(GodotConstraint2D *p_constraint) { constraints.insert(p_constraint); }
  109. _FORCE_INLINE_ void remove_constraint(GodotConstraint2D *p_constraint) { constraints.erase(p_constraint); }
  110. _FORCE_INLINE_ const HashSet<GodotConstraint2D *> &get_constraints() const { return constraints; }
  111. _FORCE_INLINE_ void clear_constraints() { constraints.clear(); }
  112. void set_monitorable(bool p_monitorable);
  113. _FORCE_INLINE_ bool is_monitorable() const { return monitorable; }
  114. void set_transform(const Transform2D &p_transform);
  115. void set_space(GodotSpace2D *p_space) override;
  116. void call_queries();
  117. void compute_gravity(const Vector2 &p_position, Vector2 &r_gravity) const;
  118. GodotArea2D();
  119. ~GodotArea2D();
  120. };
  121. void GodotArea2D::add_body_to_query(GodotBody2D *p_body, uint32_t p_body_shape, uint32_t p_area_shape) {
  122. BodyKey bk(p_body, p_body_shape, p_area_shape);
  123. monitored_bodies[bk].inc();
  124. if (!monitor_query_list.in_list()) {
  125. _queue_monitor_update();
  126. }
  127. }
  128. void GodotArea2D::remove_body_from_query(GodotBody2D *p_body, uint32_t p_body_shape, uint32_t p_area_shape) {
  129. BodyKey bk(p_body, p_body_shape, p_area_shape);
  130. monitored_bodies[bk].dec();
  131. if (!monitor_query_list.in_list()) {
  132. _queue_monitor_update();
  133. }
  134. }
  135. void GodotArea2D::add_area_to_query(GodotArea2D *p_area, uint32_t p_area_shape, uint32_t p_self_shape) {
  136. BodyKey bk(p_area, p_area_shape, p_self_shape);
  137. monitored_areas[bk].inc();
  138. if (!monitor_query_list.in_list()) {
  139. _queue_monitor_update();
  140. }
  141. }
  142. void GodotArea2D::remove_area_from_query(GodotArea2D *p_area, uint32_t p_area_shape, uint32_t p_self_shape) {
  143. BodyKey bk(p_area, p_area_shape, p_self_shape);
  144. monitored_areas[bk].dec();
  145. if (!monitor_query_list.in_list()) {
  146. _queue_monitor_update();
  147. }
  148. }
  149. #endif // GODOT_AREA_2D_H