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