Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/2d/physics/collision_shape_2d.cpp
22517 views
1
/**************************************************************************/
2
/* collision_shape_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 "collision_shape_2d.h"
32
33
#include "scene/2d/physics/area_2d.h"
34
#include "scene/2d/physics/collision_object_2d.h"
35
#include "scene/resources/2d/concave_polygon_shape_2d.h"
36
#include "scene/resources/2d/convex_polygon_shape_2d.h"
37
38
void CollisionShape2D::_shape_changed() {
39
queue_redraw();
40
}
41
42
void CollisionShape2D::_update_in_shape_owner(bool p_xform_only) {
43
collision_object->shape_owner_set_transform(owner_id, get_transform());
44
if (p_xform_only) {
45
return;
46
}
47
collision_object->shape_owner_set_disabled(owner_id, disabled);
48
collision_object->shape_owner_set_one_way_collision(owner_id, one_way_collision);
49
collision_object->shape_owner_set_one_way_collision_margin(owner_id, one_way_collision_margin);
50
collision_object->shape_owner_set_one_way_collision_direction(owner_id, one_way_collision_direction);
51
}
52
53
void CollisionShape2D::_notification(int p_what) {
54
switch (p_what) {
55
case NOTIFICATION_PARENTED: {
56
collision_object = Object::cast_to<CollisionObject2D>(get_parent());
57
if (collision_object) {
58
owner_id = collision_object->create_shape_owner(this);
59
if (shape.is_valid()) {
60
collision_object->shape_owner_add_shape(owner_id, shape);
61
}
62
_update_in_shape_owner();
63
}
64
} break;
65
66
case NOTIFICATION_ENTER_TREE: {
67
if (collision_object) {
68
_update_in_shape_owner();
69
}
70
} break;
71
72
case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
73
if (collision_object) {
74
_update_in_shape_owner(true);
75
}
76
} break;
77
78
case NOTIFICATION_UNPARENTED: {
79
if (collision_object) {
80
collision_object->remove_shape_owner(owner_id);
81
}
82
owner_id = 0;
83
collision_object = nullptr;
84
} break;
85
86
case NOTIFICATION_DRAW: {
87
ERR_FAIL_COND(!is_inside_tree());
88
89
if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) {
90
break;
91
}
92
93
if (shape.is_null()) {
94
break;
95
}
96
97
rect = Rect2();
98
99
Color draw_col = debug_color;
100
if (disabled) {
101
float g = draw_col.get_v();
102
draw_col.r = g;
103
draw_col.g = g;
104
draw_col.b = g;
105
draw_col.a *= 0.5;
106
}
107
shape->draw(get_canvas_item(), draw_col);
108
109
rect = shape->get_rect();
110
rect = rect.grow(3);
111
112
if (one_way_collision) {
113
// Draw an arrow indicating the one-way collision direction
114
draw_col = debug_color.inverted();
115
if (disabled) {
116
draw_col = draw_col.darkened(0.25);
117
}
118
119
Vector2 line_to = 20.0 * one_way_collision_direction;
120
draw_line(Vector2(), line_to, draw_col, 2);
121
real_t tsize = 8;
122
123
Vector<Vector2> pts{
124
line_to + tsize * one_way_collision_direction,
125
line_to + Math::SQRT12 * tsize * one_way_collision_direction.orthogonal(),
126
line_to - Math::SQRT12 * tsize * one_way_collision_direction.orthogonal(),
127
};
128
129
Vector<Color> cols{ draw_col, draw_col, draw_col };
130
131
draw_primitive(pts, cols, Vector<Vector2>());
132
}
133
} break;
134
}
135
}
136
137
void CollisionShape2D::set_shape(const Ref<Shape2D> &p_shape) {
138
if (p_shape == shape) {
139
return;
140
}
141
if (shape.is_valid()) {
142
shape->disconnect_changed(callable_mp(this, &CollisionShape2D::_shape_changed));
143
}
144
shape = p_shape;
145
queue_redraw();
146
if (collision_object) {
147
collision_object->shape_owner_clear_shapes(owner_id);
148
if (shape.is_valid()) {
149
collision_object->shape_owner_add_shape(owner_id, shape);
150
}
151
_update_in_shape_owner();
152
}
153
154
if (shape.is_valid()) {
155
shape->connect_changed(callable_mp(this, &CollisionShape2D::_shape_changed));
156
}
157
158
update_configuration_warnings();
159
}
160
161
Ref<Shape2D> CollisionShape2D::get_shape() const {
162
return shape;
163
}
164
165
bool CollisionShape2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
166
if (shape.is_null()) {
167
return false;
168
}
169
170
return shape->_edit_is_selected_on_click(p_point, p_tolerance);
171
}
172
173
PackedStringArray CollisionShape2D::get_configuration_warnings() const {
174
PackedStringArray warnings = Node2D::get_configuration_warnings();
175
176
CollisionObject2D *col_object = Object::cast_to<CollisionObject2D>(get_parent());
177
if (col_object == nullptr) {
178
warnings.push_back(RTR("CollisionShape2D only serves to provide a collision shape to a CollisionObject2D derived node.\nPlease only use it as a child of Area2D, StaticBody2D, RigidBody2D, CharacterBody2D, etc. to give them a shape."));
179
}
180
if (shape.is_null()) {
181
warnings.push_back(RTR("A shape must be provided for CollisionShape2D to function. Please create a shape resource for it!"));
182
}
183
if (one_way_collision && Object::cast_to<Area2D>(col_object)) {
184
warnings.push_back(RTR("The One Way Collision property will be ignored when the collision object is an Area2D."));
185
}
186
187
Ref<ConvexPolygonShape2D> convex = shape;
188
Ref<ConcavePolygonShape2D> concave = shape;
189
if (convex.is_valid() || concave.is_valid()) {
190
warnings.push_back(RTR("The CollisionShape2D node has limited editing options for polygon-based shapes. Consider using a CollisionPolygon2D node instead."));
191
}
192
193
return warnings;
194
}
195
196
void CollisionShape2D::set_disabled(bool p_disabled) {
197
disabled = p_disabled;
198
queue_redraw();
199
if (collision_object) {
200
collision_object->shape_owner_set_disabled(owner_id, p_disabled);
201
}
202
}
203
204
bool CollisionShape2D::is_disabled() const {
205
return disabled;
206
}
207
208
void CollisionShape2D::set_one_way_collision(bool p_enable) {
209
one_way_collision = p_enable;
210
queue_redraw();
211
if (collision_object) {
212
collision_object->shape_owner_set_one_way_collision(owner_id, p_enable);
213
}
214
update_configuration_warnings();
215
}
216
217
bool CollisionShape2D::is_one_way_collision_enabled() const {
218
return one_way_collision;
219
}
220
221
void CollisionShape2D::set_one_way_collision_margin(real_t p_margin) {
222
one_way_collision_margin = p_margin;
223
if (collision_object) {
224
collision_object->shape_owner_set_one_way_collision_margin(owner_id, one_way_collision_margin);
225
}
226
}
227
228
real_t CollisionShape2D::get_one_way_collision_margin() const {
229
return one_way_collision_margin;
230
}
231
232
void CollisionShape2D::set_one_way_collision_direction(const Vector2 &p_direction) {
233
if (p_direction == one_way_collision_direction) {
234
return;
235
}
236
237
one_way_collision_direction = p_direction.normalized();
238
if (collision_object) {
239
collision_object->shape_owner_set_one_way_collision_direction(owner_id, p_direction.normalized());
240
}
241
queue_redraw();
242
}
243
244
Vector2 CollisionShape2D::get_one_way_collision_direction() const {
245
return one_way_collision_direction;
246
}
247
248
Color CollisionShape2D::_get_default_debug_color() const {
249
const SceneTree *st = SceneTree::get_singleton();
250
return st ? st->get_debug_collisions_color() : Color(0.0, 0.0, 0.0, 0.0);
251
}
252
253
void CollisionShape2D::set_debug_color(const Color &p_color) {
254
if (debug_color == p_color) {
255
return;
256
}
257
258
debug_color = p_color;
259
queue_redraw();
260
}
261
262
Color CollisionShape2D::get_debug_color() const {
263
return debug_color;
264
}
265
266
#ifdef DEBUG_ENABLED
267
268
bool CollisionShape2D::_property_can_revert(const StringName &p_name) const {
269
if (p_name == "debug_color") {
270
return true;
271
}
272
return false;
273
}
274
275
bool CollisionShape2D::_property_get_revert(const StringName &p_name, Variant &r_property) const {
276
if (p_name == "debug_color") {
277
r_property = _get_default_debug_color();
278
return true;
279
}
280
return false;
281
}
282
283
void CollisionShape2D::_validate_property(PropertyInfo &p_property) const {
284
if (p_property.name == "debug_color") {
285
if (debug_color == _get_default_debug_color()) {
286
p_property.usage = PROPERTY_USAGE_DEFAULT & ~PROPERTY_USAGE_STORAGE;
287
} else {
288
p_property.usage = PROPERTY_USAGE_DEFAULT;
289
}
290
}
291
}
292
293
#endif // DEBUG_ENABLED
294
295
void CollisionShape2D::_bind_methods() {
296
ClassDB::bind_method(D_METHOD("set_shape", "shape"), &CollisionShape2D::set_shape);
297
ClassDB::bind_method(D_METHOD("get_shape"), &CollisionShape2D::get_shape);
298
ClassDB::bind_method(D_METHOD("set_disabled", "disabled"), &CollisionShape2D::set_disabled);
299
ClassDB::bind_method(D_METHOD("is_disabled"), &CollisionShape2D::is_disabled);
300
ClassDB::bind_method(D_METHOD("set_one_way_collision", "enabled"), &CollisionShape2D::set_one_way_collision);
301
ClassDB::bind_method(D_METHOD("is_one_way_collision_enabled"), &CollisionShape2D::is_one_way_collision_enabled);
302
ClassDB::bind_method(D_METHOD("set_one_way_collision_margin", "margin"), &CollisionShape2D::set_one_way_collision_margin);
303
ClassDB::bind_method(D_METHOD("get_one_way_collision_margin"), &CollisionShape2D::get_one_way_collision_margin);
304
ClassDB::bind_method(D_METHOD("set_one_way_collision_direction", "p_direction"), &CollisionShape2D::set_one_way_collision_direction);
305
ClassDB::bind_method(D_METHOD("get_one_way_collision_direction"), &CollisionShape2D::get_one_way_collision_direction);
306
307
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, Shape2D::get_class_static()), "set_shape", "get_shape");
308
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
309
ADD_GROUP("One Way Collision", "one_way_collision");
310
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_way_collision", PROPERTY_HINT_GROUP_ENABLE), "set_one_way_collision", "is_one_way_collision_enabled");
311
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "one_way_collision_margin", PROPERTY_HINT_RANGE, "0,128,0.1,suffix:px"), "set_one_way_collision_margin", "get_one_way_collision_margin");
312
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "one_way_collision_direction", PROPERTY_HINT_NONE, "suffix:px"), "set_one_way_collision_direction", "get_one_way_collision_direction");
313
314
ClassDB::bind_method(D_METHOD("set_debug_color", "color"), &CollisionShape2D::set_debug_color);
315
ClassDB::bind_method(D_METHOD("get_debug_color"), &CollisionShape2D::get_debug_color);
316
317
ADD_PROPERTY(PropertyInfo(Variant::COLOR, "debug_color"), "set_debug_color", "get_debug_color");
318
// Default value depends on a project setting, override for doc generation purposes.
319
ADD_PROPERTY_DEFAULT("debug_color", Color(0.0, 0.0, 0.0, 0.0));
320
}
321
322
CollisionShape2D::CollisionShape2D() {
323
set_notify_local_transform(true);
324
set_hide_clip_children(true);
325
debug_color = _get_default_debug_color();
326
}
327
328