Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/2d/physics/ray_cast_2d.cpp
9906 views
1
/**************************************************************************/
2
/* ray_cast_2d.cpp */
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
#include "ray_cast_2d.h"
32
33
#include "scene/2d/physics/collision_object_2d.h"
34
#include "scene/resources/world_2d.h"
35
36
void RayCast2D::set_target_position(const Vector2 &p_point) {
37
target_position = p_point;
38
if (is_inside_tree() && (Engine::get_singleton()->is_editor_hint() || get_tree()->is_debugging_collisions_hint())) {
39
queue_redraw();
40
}
41
}
42
43
Vector2 RayCast2D::get_target_position() const {
44
return target_position;
45
}
46
47
void RayCast2D::set_collision_mask(uint32_t p_mask) {
48
collision_mask = p_mask;
49
}
50
51
uint32_t RayCast2D::get_collision_mask() const {
52
return collision_mask;
53
}
54
55
void RayCast2D::set_collision_mask_value(int p_layer_number, bool p_value) {
56
ERR_FAIL_COND_MSG(p_layer_number < 1, "Collision layer number must be between 1 and 32 inclusive.");
57
ERR_FAIL_COND_MSG(p_layer_number > 32, "Collision layer number must be between 1 and 32 inclusive.");
58
uint32_t mask = get_collision_mask();
59
if (p_value) {
60
mask |= 1 << (p_layer_number - 1);
61
} else {
62
mask &= ~(1 << (p_layer_number - 1));
63
}
64
set_collision_mask(mask);
65
}
66
67
bool RayCast2D::get_collision_mask_value(int p_layer_number) const {
68
ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Collision layer number must be between 1 and 32 inclusive.");
69
ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Collision layer number must be between 1 and 32 inclusive.");
70
return get_collision_mask() & (1 << (p_layer_number - 1));
71
}
72
73
bool RayCast2D::is_colliding() const {
74
return collided;
75
}
76
77
Object *RayCast2D::get_collider() const {
78
if (against.is_null()) {
79
return nullptr;
80
}
81
82
return ObjectDB::get_instance(against);
83
}
84
85
RID RayCast2D::get_collider_rid() const {
86
return against_rid;
87
}
88
89
int RayCast2D::get_collider_shape() const {
90
return against_shape;
91
}
92
93
Vector2 RayCast2D::get_collision_point() const {
94
return collision_point;
95
}
96
97
Vector2 RayCast2D::get_collision_normal() const {
98
return collision_normal;
99
}
100
101
void RayCast2D::set_enabled(bool p_enabled) {
102
enabled = p_enabled;
103
queue_redraw();
104
if (is_inside_tree() && !Engine::get_singleton()->is_editor_hint()) {
105
set_physics_process_internal(p_enabled);
106
}
107
if (!p_enabled) {
108
collided = false;
109
}
110
}
111
112
bool RayCast2D::is_enabled() const {
113
return enabled;
114
}
115
116
void RayCast2D::set_exclude_parent_body(bool p_exclude_parent_body) {
117
if (exclude_parent_body == p_exclude_parent_body) {
118
return;
119
}
120
121
exclude_parent_body = p_exclude_parent_body;
122
123
if (!is_inside_tree()) {
124
return;
125
}
126
127
if (Object::cast_to<CollisionObject2D>(get_parent())) {
128
if (exclude_parent_body) {
129
exclude.insert(Object::cast_to<CollisionObject2D>(get_parent())->get_rid());
130
} else {
131
exclude.erase(Object::cast_to<CollisionObject2D>(get_parent())->get_rid());
132
}
133
}
134
}
135
136
bool RayCast2D::get_exclude_parent_body() const {
137
return exclude_parent_body;
138
}
139
140
void RayCast2D::_notification(int p_what) {
141
switch (p_what) {
142
case NOTIFICATION_ENTER_TREE: {
143
if (enabled && !Engine::get_singleton()->is_editor_hint()) {
144
set_physics_process_internal(true);
145
} else {
146
set_physics_process_internal(false);
147
}
148
149
if (Object::cast_to<CollisionObject2D>(get_parent())) {
150
if (exclude_parent_body) {
151
exclude.insert(Object::cast_to<CollisionObject2D>(get_parent())->get_rid());
152
} else {
153
exclude.erase(Object::cast_to<CollisionObject2D>(get_parent())->get_rid());
154
}
155
}
156
} break;
157
158
case NOTIFICATION_EXIT_TREE: {
159
if (enabled) {
160
set_physics_process_internal(false);
161
}
162
} break;
163
164
case NOTIFICATION_DRAW: {
165
ERR_FAIL_COND(!is_inside_tree());
166
if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) {
167
break;
168
}
169
_draw_debug_shape();
170
} break;
171
172
case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
173
if (!enabled) {
174
break;
175
}
176
_update_raycast_state();
177
} break;
178
}
179
}
180
181
void RayCast2D::_update_raycast_state() {
182
Ref<World2D> w2d = get_world_2d();
183
ERR_FAIL_COND(w2d.is_null());
184
185
PhysicsDirectSpaceState2D *dss = PhysicsServer2D::get_singleton()->space_get_direct_state(w2d->get_space());
186
ERR_FAIL_NULL(dss);
187
188
Transform2D gt = get_global_transform();
189
190
Vector2 to = target_position;
191
if (to == Vector2()) {
192
to = Vector2(0, 0.01);
193
}
194
195
PhysicsDirectSpaceState2D::RayResult rr;
196
bool prev_collision_state = collided;
197
198
PhysicsDirectSpaceState2D::RayParameters ray_params;
199
ray_params.from = gt.get_origin();
200
ray_params.to = gt.xform(to);
201
ray_params.exclude = exclude;
202
ray_params.collision_mask = collision_mask;
203
ray_params.collide_with_bodies = collide_with_bodies;
204
ray_params.collide_with_areas = collide_with_areas;
205
ray_params.hit_from_inside = hit_from_inside;
206
207
if (dss->intersect_ray(ray_params, rr)) {
208
collided = true;
209
against = rr.collider_id;
210
against_rid = rr.rid;
211
collision_point = rr.position;
212
collision_normal = rr.normal;
213
against_shape = rr.shape;
214
} else {
215
collided = false;
216
against = ObjectID();
217
against_rid = RID();
218
against_shape = 0;
219
}
220
221
if (prev_collision_state != collided) {
222
queue_redraw();
223
}
224
}
225
226
void RayCast2D::_draw_debug_shape() {
227
Color draw_col = collided ? Color(1.0, 0.01, 0) : get_tree()->get_debug_collisions_color();
228
if (!enabled) {
229
const float g = draw_col.get_v();
230
draw_col.r = g;
231
draw_col.g = g;
232
draw_col.b = g;
233
}
234
235
// Draw an arrow indicating where the RayCast is pointing to
236
const real_t max_arrow_size = 6;
237
const real_t line_width = 1.4;
238
bool no_line = target_position.length() < line_width;
239
real_t arrow_size = CLAMP(target_position.length() * 2 / 3, line_width, max_arrow_size);
240
241
if (no_line) {
242
arrow_size = target_position.length();
243
} else {
244
draw_line(Vector2(), target_position - target_position.normalized() * arrow_size, draw_col, line_width);
245
}
246
247
Transform2D xf;
248
xf.rotate(target_position.angle());
249
xf.translate_local(Vector2(no_line ? 0 : target_position.length() - arrow_size, 0));
250
251
Vector<Vector2> pts = {
252
xf.xform(Vector2(arrow_size, 0)),
253
xf.xform(Vector2(0, 0.5 * arrow_size)),
254
xf.xform(Vector2(0, -0.5 * arrow_size))
255
};
256
257
Vector<Color> cols = { draw_col, draw_col, draw_col };
258
259
draw_primitive(pts, cols, Vector<Vector2>());
260
}
261
262
void RayCast2D::force_raycast_update() {
263
_update_raycast_state();
264
}
265
266
void RayCast2D::add_exception_rid(const RID &p_rid) {
267
exclude.insert(p_rid);
268
}
269
270
void RayCast2D::add_exception(const CollisionObject2D *p_node) {
271
ERR_FAIL_NULL_MSG(p_node, "The passed Node must be an instance of CollisionObject2D.");
272
add_exception_rid(p_node->get_rid());
273
}
274
275
void RayCast2D::remove_exception_rid(const RID &p_rid) {
276
exclude.erase(p_rid);
277
}
278
279
void RayCast2D::remove_exception(const CollisionObject2D *p_node) {
280
ERR_FAIL_NULL_MSG(p_node, "The passed Node must be an instance of CollisionObject2D.");
281
remove_exception_rid(p_node->get_rid());
282
}
283
284
void RayCast2D::clear_exceptions() {
285
exclude.clear();
286
287
if (exclude_parent_body && is_inside_tree()) {
288
CollisionObject2D *parent = Object::cast_to<CollisionObject2D>(get_parent());
289
if (parent) {
290
exclude.insert(parent->get_rid());
291
}
292
}
293
}
294
295
void RayCast2D::set_collide_with_areas(bool p_enabled) {
296
collide_with_areas = p_enabled;
297
}
298
299
bool RayCast2D::is_collide_with_areas_enabled() const {
300
return collide_with_areas;
301
}
302
303
void RayCast2D::set_collide_with_bodies(bool p_enabled) {
304
collide_with_bodies = p_enabled;
305
}
306
307
bool RayCast2D::is_collide_with_bodies_enabled() const {
308
return collide_with_bodies;
309
}
310
311
void RayCast2D::set_hit_from_inside(bool p_enabled) {
312
hit_from_inside = p_enabled;
313
}
314
315
bool RayCast2D::is_hit_from_inside_enabled() const {
316
return hit_from_inside;
317
}
318
319
void RayCast2D::_bind_methods() {
320
ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &RayCast2D::set_enabled);
321
ClassDB::bind_method(D_METHOD("is_enabled"), &RayCast2D::is_enabled);
322
323
ClassDB::bind_method(D_METHOD("set_target_position", "local_point"), &RayCast2D::set_target_position);
324
ClassDB::bind_method(D_METHOD("get_target_position"), &RayCast2D::get_target_position);
325
326
ClassDB::bind_method(D_METHOD("is_colliding"), &RayCast2D::is_colliding);
327
ClassDB::bind_method(D_METHOD("force_raycast_update"), &RayCast2D::force_raycast_update);
328
329
ClassDB::bind_method(D_METHOD("get_collider"), &RayCast2D::get_collider);
330
ClassDB::bind_method(D_METHOD("get_collider_rid"), &RayCast2D::get_collider_rid);
331
ClassDB::bind_method(D_METHOD("get_collider_shape"), &RayCast2D::get_collider_shape);
332
ClassDB::bind_method(D_METHOD("get_collision_point"), &RayCast2D::get_collision_point);
333
ClassDB::bind_method(D_METHOD("get_collision_normal"), &RayCast2D::get_collision_normal);
334
335
ClassDB::bind_method(D_METHOD("add_exception_rid", "rid"), &RayCast2D::add_exception_rid);
336
ClassDB::bind_method(D_METHOD("add_exception", "node"), &RayCast2D::add_exception);
337
338
ClassDB::bind_method(D_METHOD("remove_exception_rid", "rid"), &RayCast2D::remove_exception_rid);
339
ClassDB::bind_method(D_METHOD("remove_exception", "node"), &RayCast2D::remove_exception);
340
341
ClassDB::bind_method(D_METHOD("clear_exceptions"), &RayCast2D::clear_exceptions);
342
343
ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &RayCast2D::set_collision_mask);
344
ClassDB::bind_method(D_METHOD("get_collision_mask"), &RayCast2D::get_collision_mask);
345
346
ClassDB::bind_method(D_METHOD("set_collision_mask_value", "layer_number", "value"), &RayCast2D::set_collision_mask_value);
347
ClassDB::bind_method(D_METHOD("get_collision_mask_value", "layer_number"), &RayCast2D::get_collision_mask_value);
348
349
ClassDB::bind_method(D_METHOD("set_exclude_parent_body", "mask"), &RayCast2D::set_exclude_parent_body);
350
ClassDB::bind_method(D_METHOD("get_exclude_parent_body"), &RayCast2D::get_exclude_parent_body);
351
352
ClassDB::bind_method(D_METHOD("set_collide_with_areas", "enable"), &RayCast2D::set_collide_with_areas);
353
ClassDB::bind_method(D_METHOD("is_collide_with_areas_enabled"), &RayCast2D::is_collide_with_areas_enabled);
354
355
ClassDB::bind_method(D_METHOD("set_collide_with_bodies", "enable"), &RayCast2D::set_collide_with_bodies);
356
ClassDB::bind_method(D_METHOD("is_collide_with_bodies_enabled"), &RayCast2D::is_collide_with_bodies_enabled);
357
358
ClassDB::bind_method(D_METHOD("set_hit_from_inside", "enable"), &RayCast2D::set_hit_from_inside);
359
ClassDB::bind_method(D_METHOD("is_hit_from_inside_enabled"), &RayCast2D::is_hit_from_inside_enabled);
360
361
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "is_enabled");
362
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "exclude_parent"), "set_exclude_parent_body", "get_exclude_parent_body");
363
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "target_position", PROPERTY_HINT_NONE, "suffix:px"), "set_target_position", "get_target_position");
364
ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_2D_PHYSICS), "set_collision_mask", "get_collision_mask");
365
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hit_from_inside"), "set_hit_from_inside", "is_hit_from_inside_enabled");
366
367
ADD_GROUP("Collide With", "collide_with");
368
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_areas"), "set_collide_with_areas", "is_collide_with_areas_enabled");
369
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_bodies"), "set_collide_with_bodies", "is_collide_with_bodies_enabled");
370
}
371
372
RayCast2D::RayCast2D() {
373
set_hide_clip_children(true);
374
}
375
376