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