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