godot_area_3d.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /**************************************************************************/
  2. /* godot_area_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_AREA_3D_H
  31. #define GODOT_AREA_3D_H
  32. #include "godot_collision_object_3d.h"
  33. #include "core/templates/self_list.h"
  34. #include "servers/physics_server_3d.h"
  35. class GodotSpace3D;
  36. class GodotBody3D;
  37. class GodotSoftBody3D;
  38. class GodotConstraint3D;
  39. class GodotArea3D : public GodotCollisionObject3D {
  40. PhysicsServer3D::AreaSpaceOverrideMode gravity_override_mode = PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED;
  41. PhysicsServer3D::AreaSpaceOverrideMode linear_damping_override_mode = PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED;
  42. PhysicsServer3D::AreaSpaceOverrideMode angular_damping_override_mode = PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED;
  43. real_t gravity = 9.80665;
  44. Vector3 gravity_vector = Vector3(0, -1, 0);
  45. bool gravity_is_point = false;
  46. real_t gravity_point_unit_distance = 0.0;
  47. real_t linear_damp = 0.1;
  48. real_t angular_damp = 0.1;
  49. real_t wind_force_magnitude = 0.0;
  50. real_t wind_attenuation_factor = 0.0;
  51. Vector3 wind_source;
  52. Vector3 wind_direction;
  53. int priority = 0;
  54. bool monitorable = false;
  55. Callable monitor_callback;
  56. Callable area_monitor_callback;
  57. SelfList<GodotArea3D> monitor_query_list;
  58. SelfList<GodotArea3D> moved_list;
  59. struct BodyKey {
  60. RID rid;
  61. ObjectID instance_id;
  62. uint32_t body_shape = 0;
  63. uint32_t area_shape = 0;
  64. static uint32_t hash(const BodyKey &p_key) {
  65. uint32_t h = hash_one_uint64(p_key.rid.get_id());
  66. h = hash_murmur3_one_64(p_key.instance_id, h);
  67. h = hash_murmur3_one_32(p_key.area_shape, h);
  68. return hash_fmix32(hash_murmur3_one_32(p_key.body_shape, h));
  69. }
  70. _FORCE_INLINE_ bool operator==(const BodyKey &p_key) const {
  71. return rid == p_key.rid && instance_id == p_key.instance_id && body_shape == p_key.body_shape && area_shape == p_key.area_shape;
  72. }
  73. _FORCE_INLINE_ BodyKey() {}
  74. BodyKey(GodotSoftBody3D *p_body, uint32_t p_body_shape, uint32_t p_area_shape);
  75. BodyKey(GodotBody3D *p_body, uint32_t p_body_shape, uint32_t p_area_shape);
  76. BodyKey(GodotArea3D *p_body, uint32_t p_body_shape, uint32_t p_area_shape);
  77. };
  78. struct BodyState {
  79. int state = 0;
  80. _FORCE_INLINE_ void inc() { state++; }
  81. _FORCE_INLINE_ void dec() { state--; }
  82. };
  83. HashMap<BodyKey, BodyState, BodyKey> monitored_soft_bodies;
  84. HashMap<BodyKey, BodyState, BodyKey> monitored_bodies;
  85. HashMap<BodyKey, BodyState, BodyKey> monitored_areas;
  86. HashSet<GodotConstraint3D *> constraints;
  87. virtual void _shapes_changed() override;
  88. void _queue_monitor_update();
  89. void _set_space_override_mode(PhysicsServer3D::AreaSpaceOverrideMode &r_mode, PhysicsServer3D::AreaSpaceOverrideMode p_new_mode);
  90. public:
  91. void set_monitor_callback(const Callable &p_callback);
  92. _FORCE_INLINE_ bool has_monitor_callback() const { return monitor_callback.is_valid(); }
  93. void set_area_monitor_callback(const Callable &p_callback);
  94. _FORCE_INLINE_ bool has_area_monitor_callback() const { return area_monitor_callback.is_valid(); }
  95. _FORCE_INLINE_ void add_body_to_query(GodotBody3D *p_body, uint32_t p_body_shape, uint32_t p_area_shape);
  96. _FORCE_INLINE_ void remove_body_from_query(GodotBody3D *p_body, uint32_t p_body_shape, uint32_t p_area_shape);
  97. _FORCE_INLINE_ void add_soft_body_to_query(GodotSoftBody3D *p_soft_body, uint32_t p_soft_body_shape, uint32_t p_area_shape);
  98. _FORCE_INLINE_ void remove_soft_body_from_query(GodotSoftBody3D *p_soft_body, uint32_t p_soft_body_shape, uint32_t p_area_shape);
  99. _FORCE_INLINE_ void add_area_to_query(GodotArea3D *p_area, uint32_t p_area_shape, uint32_t p_self_shape);
  100. _FORCE_INLINE_ void remove_area_from_query(GodotArea3D *p_area, uint32_t p_area_shape, uint32_t p_self_shape);
  101. void set_param(PhysicsServer3D::AreaParameter p_param, const Variant &p_value);
  102. Variant get_param(PhysicsServer3D::AreaParameter p_param) const;
  103. _FORCE_INLINE_ void set_gravity(real_t p_gravity) { gravity = p_gravity; }
  104. _FORCE_INLINE_ real_t get_gravity() const { return gravity; }
  105. _FORCE_INLINE_ void set_gravity_vector(const Vector3 &p_gravity) { gravity_vector = p_gravity; }
  106. _FORCE_INLINE_ Vector3 get_gravity_vector() const { return gravity_vector; }
  107. _FORCE_INLINE_ void set_gravity_as_point(bool p_enable) { gravity_is_point = p_enable; }
  108. _FORCE_INLINE_ bool is_gravity_point() const { return gravity_is_point; }
  109. _FORCE_INLINE_ void set_gravity_point_unit_distance(real_t scale) { gravity_point_unit_distance = scale; }
  110. _FORCE_INLINE_ real_t get_gravity_point_unit_distance() const { return gravity_point_unit_distance; }
  111. _FORCE_INLINE_ void set_linear_damp(real_t p_linear_damp) { linear_damp = p_linear_damp; }
  112. _FORCE_INLINE_ real_t get_linear_damp() const { return linear_damp; }
  113. _FORCE_INLINE_ void set_angular_damp(real_t p_angular_damp) { angular_damp = p_angular_damp; }
  114. _FORCE_INLINE_ real_t get_angular_damp() const { return angular_damp; }
  115. _FORCE_INLINE_ void set_priority(int p_priority) { priority = p_priority; }
  116. _FORCE_INLINE_ int get_priority() const { return priority; }
  117. _FORCE_INLINE_ void set_wind_force_magnitude(real_t p_wind_force_magnitude) { wind_force_magnitude = p_wind_force_magnitude; }
  118. _FORCE_INLINE_ real_t get_wind_force_magnitude() const { return wind_force_magnitude; }
  119. _FORCE_INLINE_ void set_wind_attenuation_factor(real_t p_wind_attenuation_factor) { wind_attenuation_factor = p_wind_attenuation_factor; }
  120. _FORCE_INLINE_ real_t get_wind_attenuation_factor() const { return wind_attenuation_factor; }
  121. _FORCE_INLINE_ void set_wind_source(const Vector3 &p_wind_source) { wind_source = p_wind_source; }
  122. _FORCE_INLINE_ const Vector3 &get_wind_source() const { return wind_source; }
  123. _FORCE_INLINE_ void set_wind_direction(const Vector3 &p_wind_direction) { wind_direction = p_wind_direction; }
  124. _FORCE_INLINE_ const Vector3 &get_wind_direction() const { return wind_direction; }
  125. _FORCE_INLINE_ void add_constraint(GodotConstraint3D *p_constraint) { constraints.insert(p_constraint); }
  126. _FORCE_INLINE_ void remove_constraint(GodotConstraint3D *p_constraint) { constraints.erase(p_constraint); }
  127. _FORCE_INLINE_ const HashSet<GodotConstraint3D *> &get_constraints() const { return constraints; }
  128. _FORCE_INLINE_ void clear_constraints() { constraints.clear(); }
  129. void set_monitorable(bool p_monitorable);
  130. _FORCE_INLINE_ bool is_monitorable() const { return monitorable; }
  131. void set_transform(const Transform3D &p_transform);
  132. void set_space(GodotSpace3D *p_space) override;
  133. void call_queries();
  134. void compute_gravity(const Vector3 &p_position, Vector3 &r_gravity) const;
  135. GodotArea3D();
  136. ~GodotArea3D();
  137. };
  138. void GodotArea3D::add_soft_body_to_query(GodotSoftBody3D *p_soft_body, uint32_t p_soft_body_shape, uint32_t p_area_shape) {
  139. BodyKey bk(p_soft_body, p_soft_body_shape, p_area_shape);
  140. monitored_soft_bodies[bk].inc();
  141. if (!monitor_query_list.in_list()) {
  142. _queue_monitor_update();
  143. }
  144. }
  145. void GodotArea3D::remove_soft_body_from_query(GodotSoftBody3D *p_soft_body, uint32_t p_soft_body_shape, uint32_t p_area_shape) {
  146. BodyKey bk(p_soft_body, p_soft_body_shape, p_area_shape);
  147. monitored_soft_bodies[bk].dec();
  148. if (get_space() && !monitor_query_list.in_list()) {
  149. _queue_monitor_update();
  150. }
  151. }
  152. void GodotArea3D::add_body_to_query(GodotBody3D *p_body, uint32_t p_body_shape, uint32_t p_area_shape) {
  153. BodyKey bk(p_body, p_body_shape, p_area_shape);
  154. monitored_bodies[bk].inc();
  155. if (!monitor_query_list.in_list()) {
  156. _queue_monitor_update();
  157. }
  158. }
  159. void GodotArea3D::remove_body_from_query(GodotBody3D *p_body, uint32_t p_body_shape, uint32_t p_area_shape) {
  160. BodyKey bk(p_body, p_body_shape, p_area_shape);
  161. monitored_bodies[bk].dec();
  162. if (get_space() && !monitor_query_list.in_list()) {
  163. _queue_monitor_update();
  164. }
  165. }
  166. void GodotArea3D::add_area_to_query(GodotArea3D *p_area, uint32_t p_area_shape, uint32_t p_self_shape) {
  167. BodyKey bk(p_area, p_area_shape, p_self_shape);
  168. monitored_areas[bk].inc();
  169. if (!monitor_query_list.in_list()) {
  170. _queue_monitor_update();
  171. }
  172. }
  173. void GodotArea3D::remove_area_from_query(GodotArea3D *p_area, uint32_t p_area_shape, uint32_t p_self_shape) {
  174. BodyKey bk(p_area, p_area_shape, p_self_shape);
  175. monitored_areas[bk].dec();
  176. if (get_space() && !monitor_query_list.in_list()) {
  177. _queue_monitor_update();
  178. }
  179. }
  180. struct AreaCMP {
  181. GodotArea3D *area = nullptr;
  182. int refCount = 0;
  183. _FORCE_INLINE_ bool operator==(const AreaCMP &p_cmp) const { return area->get_self() == p_cmp.area->get_self(); }
  184. _FORCE_INLINE_ bool operator<(const AreaCMP &p_cmp) const { return area->get_priority() < p_cmp.area->get_priority(); }
  185. _FORCE_INLINE_ AreaCMP() {}
  186. _FORCE_INLINE_ AreaCMP(GodotArea3D *p_area) {
  187. area = p_area;
  188. refCount = 1;
  189. }
  190. };
  191. #endif // GODOT_AREA_3D_H