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