1/**************************************************************************/
2/* godot_space_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_SPACE_3D_H
32#define GODOT_SPACE_3D_H
33
34#include "godot_area_3d.h"
35#include "godot_area_pair_3d.h"
36#include "godot_body_3d.h"
37#include "godot_body_pair_3d.h"
38#include "godot_broad_phase_3d.h"
39#include "godot_collision_object_3d.h"
40#include "godot_soft_body_3d.h"
41
42#include "core/config/project_settings.h"
43#include "core/templates/hash_map.h"
44#include "core/typedefs.h"
45
46class GodotPhysicsDirectSpaceState3D : public PhysicsDirectSpaceState3D {
47 GDCLASS(GodotPhysicsDirectSpaceState3D, PhysicsDirectSpaceState3D);
48
49public:
50 GodotSpace3D *space = nullptr;
51
52 virtual int intersect_point(const PointParameters &p_parameters, ShapeResult *r_results, int p_result_max) override;
53 virtual bool intersect_ray(const RayParameters &p_parameters, RayResult &r_result) override;
54 virtual int intersect_shape(const ShapeParameters &p_parameters, ShapeResult *r_results, int p_result_max) override;
55 virtual bool cast_motion(const ShapeParameters &p_parameters, real_t &p_closest_safe, real_t &p_closest_unsafe, ShapeRestInfo *r_info = nullptr) override;
56 virtual bool collide_shape(const ShapeParameters &p_parameters, Vector3 *r_results, int p_result_max, int &r_result_count) override;
57 virtual bool rest_info(const ShapeParameters &p_parameters, ShapeRestInfo *r_info) override;
58 virtual Vector3 get_closest_point_to_object_volume(RID p_object, const Vector3 p_point) const override;
59
60 GodotPhysicsDirectSpaceState3D();
61};
62
63class GodotSpace3D {
64public:
65 enum ElapsedTime {
66 ELAPSED_TIME_INTEGRATE_FORCES,
67 ELAPSED_TIME_GENERATE_ISLANDS,
68 ELAPSED_TIME_SETUP_CONSTRAINTS,
69 ELAPSED_TIME_SOLVE_CONSTRAINTS,
70 ELAPSED_TIME_INTEGRATE_VELOCITIES,
71 ELAPSED_TIME_MAX
72
73 };
74
75private:
76 uint64_t elapsed_time[ELAPSED_TIME_MAX] = {};
77
78 GodotPhysicsDirectSpaceState3D *direct_access = nullptr;
79 RID self;
80
81 GodotBroadPhase3D *broadphase = nullptr;
82 SelfList<GodotBody3D>::List active_list;
83 SelfList<GodotBody3D>::List mass_properties_update_list;
84 SelfList<GodotBody3D>::List state_query_list;
85 SelfList<GodotArea3D>::List monitor_query_list;
86 SelfList<GodotArea3D>::List area_moved_list;
87 SelfList<GodotSoftBody3D>::List active_soft_body_list;
88
89 static void *_broadphase_pair(GodotCollisionObject3D *A, int p_subindex_A, GodotCollisionObject3D *B, int p_subindex_B, void *p_self);
90 static void _broadphase_unpair(GodotCollisionObject3D *A, int p_subindex_A, GodotCollisionObject3D *B, int p_subindex_B, void *p_data, void *p_self);
91
92 HashSet<GodotCollisionObject3D *> objects;
93
94 GodotArea3D *area = nullptr;
95
96 int solver_iterations = 0;
97
98 real_t contact_recycle_radius = 0.0;
99 real_t contact_max_separation = 0.0;
100 real_t contact_max_allowed_penetration = 0.0;
101 real_t contact_bias = 0.0;
102
103 enum {
104 INTERSECTION_QUERY_MAX = 2048
105 };
106
107 GodotCollisionObject3D *intersection_query_results[INTERSECTION_QUERY_MAX];
108 int intersection_query_subindex_results[INTERSECTION_QUERY_MAX];
109
110 real_t body_linear_velocity_sleep_threshold = 0.0;
111 real_t body_angular_velocity_sleep_threshold = 0.0;
112 real_t body_time_to_sleep = 0.0;
113
114 bool locked = false;
115
116 real_t last_step = 0.001;
117
118 int island_count = 0;
119 int active_objects = 0;
120 int collision_pairs = 0;
121
122 RID static_global_body;
123
124 Vector<Vector3> contact_debug;
125 int contact_debug_count = 0;
126
127 friend class GodotPhysicsDirectSpaceState3D;
128
129 int _cull_aabb_for_body(GodotBody3D *p_body, const AABB &p_aabb);
130
131public:
132 _FORCE_INLINE_ void set_self(const RID &p_self) { self = p_self; }
133 _FORCE_INLINE_ RID get_self() const { return self; }
134
135 void set_default_area(GodotArea3D *p_area) { area = p_area; }
136 GodotArea3D *get_default_area() const { return area; }
137
138 const SelfList<GodotBody3D>::List &get_active_body_list() const;
139 void body_add_to_active_list(SelfList<GodotBody3D> *p_body);
140 void body_remove_from_active_list(SelfList<GodotBody3D> *p_body);
141 void body_add_to_mass_properties_update_list(SelfList<GodotBody3D> *p_body);
142 void body_remove_from_mass_properties_update_list(SelfList<GodotBody3D> *p_body);
143
144 void body_add_to_state_query_list(SelfList<GodotBody3D> *p_body);
145 void body_remove_from_state_query_list(SelfList<GodotBody3D> *p_body);
146
147 void area_add_to_monitor_query_list(SelfList<GodotArea3D> *p_area);
148 void area_remove_from_monitor_query_list(SelfList<GodotArea3D> *p_area);
149 void area_add_to_moved_list(SelfList<GodotArea3D> *p_area);
150 void area_remove_from_moved_list(SelfList<GodotArea3D> *p_area);
151 const SelfList<GodotArea3D>::List &get_moved_area_list() const;
152
153 const SelfList<GodotSoftBody3D>::List &get_active_soft_body_list() const;
154 void soft_body_add_to_active_list(SelfList<GodotSoftBody3D> *p_soft_body);
155 void soft_body_remove_from_active_list(SelfList<GodotSoftBody3D> *p_soft_body);
156
157 GodotBroadPhase3D *get_broadphase();
158
159 void add_object(GodotCollisionObject3D *p_object);
160 void remove_object(GodotCollisionObject3D *p_object);
161 const HashSet<GodotCollisionObject3D *> &get_objects() const;
162
163 _FORCE_INLINE_ int get_solver_iterations() const { return solver_iterations; }
164 _FORCE_INLINE_ real_t get_contact_recycle_radius() const { return contact_recycle_radius; }
165 _FORCE_INLINE_ real_t get_contact_max_separation() const { return contact_max_separation; }
166 _FORCE_INLINE_ real_t get_contact_max_allowed_penetration() const { return contact_max_allowed_penetration; }
167 _FORCE_INLINE_ real_t get_contact_bias() const { return contact_bias; }
168 _FORCE_INLINE_ real_t get_body_linear_velocity_sleep_threshold() const { return body_linear_velocity_sleep_threshold; }
169 _FORCE_INLINE_ real_t get_body_angular_velocity_sleep_threshold() const { return body_angular_velocity_sleep_threshold; }
170 _FORCE_INLINE_ real_t get_body_time_to_sleep() const { return body_time_to_sleep; }
171
172 void update();
173 void setup();
174 void call_queries();
175
176 bool is_locked() const;
177 void lock();
178 void unlock();
179
180 real_t get_last_step() const { return last_step; }
181 void set_last_step(real_t p_step) { last_step = p_step; }
182
183 void set_param(PhysicsServer3D::SpaceParameter p_param, real_t p_value);
184 real_t get_param(PhysicsServer3D::SpaceParameter p_param) const;
185
186 void set_island_count(int p_island_count) { island_count = p_island_count; }
187 int get_island_count() const { return island_count; }
188
189 void set_active_objects(int p_active_objects) { active_objects = p_active_objects; }
190 int get_active_objects() const { return active_objects; }
191
192 int get_collision_pairs() const { return collision_pairs; }
193
194 GodotPhysicsDirectSpaceState3D *get_direct_state();
195
196 void set_debug_contacts(int p_amount) { contact_debug.resize(p_amount); }
197 _FORCE_INLINE_ bool is_debugging_contacts() const { return !contact_debug.is_empty(); }
198 _FORCE_INLINE_ void add_debug_contact(const Vector3 &p_contact) {
199 if (contact_debug_count < contact_debug.size()) {
200 contact_debug.write[contact_debug_count++] = p_contact;
201 }
202 }
203 _FORCE_INLINE_ Vector<Vector3> get_debug_contacts() { return contact_debug; }
204 _FORCE_INLINE_ int get_debug_contact_count() { return contact_debug_count; }
205
206 void set_static_global_body(RID p_body) { static_global_body = p_body; }
207 RID get_static_global_body() { return static_global_body; }
208
209 void set_elapsed_time(ElapsedTime p_time, uint64_t p_msec) { elapsed_time[p_time] = p_msec; }
210 uint64_t get_elapsed_time(ElapsedTime p_time) const { return elapsed_time[p_time]; }
211
212 bool test_body_motion(GodotBody3D *p_body, const PhysicsServer3D::MotionParameters &p_parameters, PhysicsServer3D::MotionResult *r_result);
213
214 GodotSpace3D();
215 ~GodotSpace3D();
216};
217
218#endif // GODOT_SPACE_3D_H
219