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