1 | /**************************************************************************/ |
2 | /* godot_physics_server_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_PHYSICS_SERVER_2D_H |
32 | #define GODOT_PHYSICS_SERVER_2D_H |
33 | |
34 | #include "godot_joints_2d.h" |
35 | #include "godot_shape_2d.h" |
36 | #include "godot_space_2d.h" |
37 | #include "godot_step_2d.h" |
38 | |
39 | #include "core/templates/rid_owner.h" |
40 | #include "servers/physics_server_2d.h" |
41 | |
42 | class GodotPhysicsServer2D : public PhysicsServer2D { |
43 | GDCLASS(GodotPhysicsServer2D, PhysicsServer2D); |
44 | |
45 | friend class GodotPhysicsDirectSpaceState2D; |
46 | friend class GodotPhysicsDirectBodyState2D; |
47 | bool active = true; |
48 | bool doing_sync = false; |
49 | |
50 | int island_count = 0; |
51 | int active_objects = 0; |
52 | int collision_pairs = 0; |
53 | |
54 | bool using_threads = false; |
55 | |
56 | bool flushing_queries = false; |
57 | |
58 | GodotStep2D *stepper = nullptr; |
59 | HashSet<const GodotSpace2D *> active_spaces; |
60 | |
61 | mutable RID_PtrOwner<GodotShape2D, true> shape_owner; |
62 | mutable RID_PtrOwner<GodotSpace2D, true> space_owner; |
63 | mutable RID_PtrOwner<GodotArea2D, true> area_owner; |
64 | mutable RID_PtrOwner<GodotBody2D, true> body_owner; |
65 | mutable RID_PtrOwner<GodotJoint2D, true> joint_owner; |
66 | |
67 | static GodotPhysicsServer2D *godot_singleton; |
68 | |
69 | friend class GodotCollisionObject2D; |
70 | SelfList<GodotCollisionObject2D>::List pending_shape_update_list; |
71 | void _update_shapes(); |
72 | |
73 | RID _shape_create(ShapeType p_shape); |
74 | |
75 | public: |
76 | struct CollCbkData { |
77 | Vector2 valid_dir; |
78 | real_t valid_depth = 0.0; |
79 | int max = 0; |
80 | int amount = 0; |
81 | int passed = 0; |
82 | int invalid_by_dir = 0; |
83 | Vector2 *ptr = nullptr; |
84 | }; |
85 | |
86 | virtual RID world_boundary_shape_create() override; |
87 | virtual RID separation_ray_shape_create() override; |
88 | virtual RID segment_shape_create() override; |
89 | virtual RID circle_shape_create() override; |
90 | virtual RID rectangle_shape_create() override; |
91 | virtual RID capsule_shape_create() override; |
92 | virtual RID convex_polygon_shape_create() override; |
93 | virtual RID concave_polygon_shape_create() override; |
94 | |
95 | static void _shape_col_cbk(const Vector2 &p_point_A, const Vector2 &p_point_B, void *p_userdata); |
96 | |
97 | virtual void shape_set_data(RID p_shape, const Variant &p_data) override; |
98 | virtual void shape_set_custom_solver_bias(RID p_shape, real_t p_bias) override; |
99 | |
100 | virtual ShapeType shape_get_type(RID p_shape) const override; |
101 | virtual Variant shape_get_data(RID p_shape) const override; |
102 | virtual real_t shape_get_custom_solver_bias(RID p_shape) const override; |
103 | |
104 | virtual bool shape_collide(RID p_shape_A, const Transform2D &p_xform_A, const Vector2 &p_motion_A, RID p_shape_B, const Transform2D &p_xform_B, const Vector2 &p_motion_B, Vector2 *r_results, int p_result_max, int &r_result_count) override; |
105 | |
106 | /* SPACE API */ |
107 | |
108 | virtual RID space_create() override; |
109 | virtual void space_set_active(RID p_space, bool p_active) override; |
110 | virtual bool space_is_active(RID p_space) const override; |
111 | |
112 | virtual void space_set_param(RID p_space, SpaceParameter p_param, real_t p_value) override; |
113 | virtual real_t space_get_param(RID p_space, SpaceParameter p_param) const override; |
114 | |
115 | virtual void space_set_debug_contacts(RID p_space, int p_max_contacts) override; |
116 | virtual Vector<Vector2> space_get_contacts(RID p_space) const override; |
117 | virtual int space_get_contact_count(RID p_space) const override; |
118 | |
119 | // this function only works on physics process, errors and returns null otherwise |
120 | virtual PhysicsDirectSpaceState2D *space_get_direct_state(RID p_space) override; |
121 | |
122 | /* AREA API */ |
123 | |
124 | virtual RID area_create() override; |
125 | |
126 | virtual void area_set_space(RID p_area, RID p_space) override; |
127 | virtual RID area_get_space(RID p_area) const override; |
128 | |
129 | virtual void area_add_shape(RID p_area, RID p_shape, const Transform2D &p_transform = Transform2D(), bool p_disabled = false) override; |
130 | virtual void area_set_shape(RID p_area, int p_shape_idx, RID p_shape) override; |
131 | virtual void area_set_shape_transform(RID p_area, int p_shape_idx, const Transform2D &p_transform) override; |
132 | |
133 | virtual int area_get_shape_count(RID p_area) const override; |
134 | virtual RID area_get_shape(RID p_area, int p_shape_idx) const override; |
135 | virtual Transform2D area_get_shape_transform(RID p_area, int p_shape_idx) const override; |
136 | |
137 | virtual void area_set_shape_disabled(RID p_area, int p_shape, bool p_disabled) override; |
138 | |
139 | virtual void area_remove_shape(RID p_area, int p_shape_idx) override; |
140 | virtual void area_clear_shapes(RID p_area) override; |
141 | |
142 | virtual void area_attach_object_instance_id(RID p_area, ObjectID p_id) override; |
143 | virtual ObjectID area_get_object_instance_id(RID p_area) const override; |
144 | |
145 | virtual void area_attach_canvas_instance_id(RID p_area, ObjectID p_id) override; |
146 | virtual ObjectID area_get_canvas_instance_id(RID p_area) const override; |
147 | |
148 | virtual void area_set_param(RID p_area, AreaParameter p_param, const Variant &p_value) override; |
149 | virtual void area_set_transform(RID p_area, const Transform2D &p_transform) override; |
150 | |
151 | virtual Variant area_get_param(RID p_area, AreaParameter p_param) const override; |
152 | virtual Transform2D area_get_transform(RID p_area) const override; |
153 | virtual void area_set_monitorable(RID p_area, bool p_monitorable) override; |
154 | |
155 | virtual void area_set_collision_layer(RID p_area, uint32_t p_layer) override; |
156 | virtual uint32_t area_get_collision_layer(RID p_area) const override; |
157 | |
158 | virtual void area_set_collision_mask(RID p_area, uint32_t p_mask) override; |
159 | virtual uint32_t area_get_collision_mask(RID p_area) const override; |
160 | |
161 | virtual void area_set_monitor_callback(RID p_area, const Callable &p_callback) override; |
162 | virtual void area_set_area_monitor_callback(RID p_area, const Callable &p_callback) override; |
163 | |
164 | virtual void area_set_pickable(RID p_area, bool p_pickable) override; |
165 | |
166 | /* BODY API */ |
167 | |
168 | // create a body of a given type |
169 | virtual RID body_create() override; |
170 | |
171 | virtual void body_set_space(RID p_body, RID p_space) override; |
172 | virtual RID body_get_space(RID p_body) const override; |
173 | |
174 | virtual void body_set_mode(RID p_body, BodyMode p_mode) override; |
175 | virtual BodyMode body_get_mode(RID p_body) const override; |
176 | |
177 | virtual void body_add_shape(RID p_body, RID p_shape, const Transform2D &p_transform = Transform2D(), bool p_disabled = false) override; |
178 | virtual void body_set_shape(RID p_body, int p_shape_idx, RID p_shape) override; |
179 | virtual void body_set_shape_transform(RID p_body, int p_shape_idx, const Transform2D &p_transform) override; |
180 | |
181 | virtual int body_get_shape_count(RID p_body) const override; |
182 | virtual RID body_get_shape(RID p_body, int p_shape_idx) const override; |
183 | virtual Transform2D body_get_shape_transform(RID p_body, int p_shape_idx) const override; |
184 | |
185 | virtual void body_remove_shape(RID p_body, int p_shape_idx) override; |
186 | virtual void body_clear_shapes(RID p_body) override; |
187 | |
188 | virtual void body_set_shape_disabled(RID p_body, int p_shape_idx, bool p_disabled) override; |
189 | virtual void body_set_shape_as_one_way_collision(RID p_body, int p_shape_idx, bool p_enable, real_t p_margin) override; |
190 | |
191 | virtual void body_attach_object_instance_id(RID p_body, ObjectID p_id) override; |
192 | virtual ObjectID body_get_object_instance_id(RID p_body) const override; |
193 | |
194 | virtual void body_attach_canvas_instance_id(RID p_body, ObjectID p_id) override; |
195 | virtual ObjectID body_get_canvas_instance_id(RID p_body) const override; |
196 | |
197 | virtual void body_set_continuous_collision_detection_mode(RID p_body, CCDMode p_mode) override; |
198 | virtual CCDMode body_get_continuous_collision_detection_mode(RID p_body) const override; |
199 | |
200 | virtual void body_set_collision_layer(RID p_body, uint32_t p_layer) override; |
201 | virtual uint32_t body_get_collision_layer(RID p_body) const override; |
202 | |
203 | virtual void body_set_collision_mask(RID p_body, uint32_t p_mask) override; |
204 | virtual uint32_t body_get_collision_mask(RID p_body) const override; |
205 | |
206 | virtual void body_set_collision_priority(RID p_body, real_t p_priority) override; |
207 | virtual real_t body_get_collision_priority(RID p_body) const override; |
208 | |
209 | virtual void body_set_param(RID p_body, BodyParameter p_param, const Variant &p_value) override; |
210 | virtual Variant body_get_param(RID p_body, BodyParameter p_param) const override; |
211 | |
212 | virtual void body_reset_mass_properties(RID p_body) override; |
213 | |
214 | virtual void body_set_state(RID p_body, BodyState p_state, const Variant &p_variant) override; |
215 | virtual Variant body_get_state(RID p_body, BodyState p_state) const override; |
216 | |
217 | virtual void body_apply_central_impulse(RID p_body, const Vector2 &p_impulse) override; |
218 | virtual void body_apply_torque_impulse(RID p_body, real_t p_torque) override; |
219 | virtual void body_apply_impulse(RID p_body, const Vector2 &p_impulse, const Vector2 &p_position = Vector2()) override; |
220 | |
221 | virtual void body_apply_central_force(RID p_body, const Vector2 &p_force) override; |
222 | virtual void body_apply_force(RID p_body, const Vector2 &p_force, const Vector2 &p_position = Vector2()) override; |
223 | virtual void body_apply_torque(RID p_body, real_t p_torque) override; |
224 | |
225 | virtual void body_add_constant_central_force(RID p_body, const Vector2 &p_force) override; |
226 | virtual void body_add_constant_force(RID p_body, const Vector2 &p_force, const Vector2 &p_position = Vector2()) override; |
227 | virtual void body_add_constant_torque(RID p_body, real_t p_torque) override; |
228 | |
229 | virtual void body_set_constant_force(RID p_body, const Vector2 &p_force) override; |
230 | virtual Vector2 body_get_constant_force(RID p_body) const override; |
231 | |
232 | virtual void body_set_constant_torque(RID p_body, real_t p_torque) override; |
233 | virtual real_t body_get_constant_torque(RID p_body) const override; |
234 | |
235 | virtual void body_set_axis_velocity(RID p_body, const Vector2 &p_axis_velocity) override; |
236 | |
237 | virtual void body_add_collision_exception(RID p_body, RID p_body_b) override; |
238 | virtual void body_remove_collision_exception(RID p_body, RID p_body_b) override; |
239 | virtual void body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) override; |
240 | |
241 | virtual void body_set_contacts_reported_depth_threshold(RID p_body, real_t p_threshold) override; |
242 | virtual real_t body_get_contacts_reported_depth_threshold(RID p_body) const override; |
243 | |
244 | virtual void body_set_omit_force_integration(RID p_body, bool p_omit) override; |
245 | virtual bool body_is_omitting_force_integration(RID p_body) const override; |
246 | |
247 | virtual void body_set_max_contacts_reported(RID p_body, int p_contacts) override; |
248 | virtual int body_get_max_contacts_reported(RID p_body) const override; |
249 | |
250 | virtual void body_set_state_sync_callback(RID p_body, const Callable &p_callable) override; |
251 | virtual void body_set_force_integration_callback(RID p_body, const Callable &p_callable, const Variant &p_udata = Variant()) override; |
252 | |
253 | virtual bool body_collide_shape(RID p_body, int p_body_shape, RID p_shape, const Transform2D &p_shape_xform, const Vector2 &p_motion, Vector2 *r_results, int p_result_max, int &r_result_count) override; |
254 | |
255 | virtual void body_set_pickable(RID p_body, bool p_pickable) override; |
256 | |
257 | virtual bool body_test_motion(RID p_body, const MotionParameters &p_parameters, MotionResult *r_result = nullptr) override; |
258 | |
259 | // this function only works on physics process, errors and returns null otherwise |
260 | virtual PhysicsDirectBodyState2D *body_get_direct_state(RID p_body) override; |
261 | |
262 | /* JOINT API */ |
263 | |
264 | virtual RID joint_create() override; |
265 | |
266 | virtual void joint_clear(RID p_joint) override; |
267 | |
268 | virtual void joint_set_param(RID p_joint, JointParam p_param, real_t p_value) override; |
269 | virtual real_t joint_get_param(RID p_joint, JointParam p_param) const override; |
270 | |
271 | virtual void joint_disable_collisions_between_bodies(RID p_joint, const bool p_disabled) override; |
272 | virtual bool joint_is_disabled_collisions_between_bodies(RID p_joint) const override; |
273 | |
274 | virtual void joint_make_pin(RID p_joint, const Vector2 &p_anchor, RID p_body_a, RID p_body_b = RID()) override; |
275 | virtual void joint_make_groove(RID p_joint, const Vector2 &p_a_groove1, const Vector2 &p_a_groove2, const Vector2 &p_b_anchor, RID p_body_a, RID p_body_b) override; |
276 | virtual void joint_make_damped_spring(RID p_joint, const Vector2 &p_anchor_a, const Vector2 &p_anchor_b, RID p_body_a, RID p_body_b = RID()) override; |
277 | |
278 | virtual void pin_joint_set_param(RID p_joint, PinJointParam p_param, real_t p_value) override; |
279 | virtual real_t pin_joint_get_param(RID p_joint, PinJointParam p_param) const override; |
280 | virtual void damped_spring_joint_set_param(RID p_joint, DampedSpringParam p_param, real_t p_value) override; |
281 | virtual real_t damped_spring_joint_get_param(RID p_joint, DampedSpringParam p_param) const override; |
282 | |
283 | virtual JointType joint_get_type(RID p_joint) const override; |
284 | |
285 | /* MISC */ |
286 | |
287 | virtual void free(RID p_rid) override; |
288 | |
289 | virtual void set_active(bool p_active) override; |
290 | virtual void init() override; |
291 | virtual void step(real_t p_step) override; |
292 | virtual void sync() override; |
293 | virtual void flush_queries() override; |
294 | virtual void end_sync() override; |
295 | virtual void finish() override; |
296 | |
297 | virtual bool is_flushing_queries() const override { return flushing_queries; } |
298 | |
299 | int get_process_info(ProcessInfo p_info) override; |
300 | |
301 | GodotPhysicsServer2D(bool p_using_threads = false); |
302 | ~GodotPhysicsServer2D() {} |
303 | }; |
304 | |
305 | #endif // GODOT_PHYSICS_SERVER_2D_H |
306 | |