Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/2d/physics/collision_polygon_2d.cpp
9906 views
1
/**************************************************************************/
2
/* collision_polygon_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_polygon_2d.h"
32
33
#include "core/math/geometry_2d.h"
34
#include "scene/2d/physics/area_2d.h"
35
#include "scene/2d/physics/collision_object_2d.h"
36
#include "scene/resources/2d/concave_polygon_shape_2d.h"
37
#include "scene/resources/2d/convex_polygon_shape_2d.h"
38
39
void CollisionPolygon2D::_build_polygon() {
40
collision_object->shape_owner_clear_shapes(owner_id);
41
42
bool solids = build_mode == BUILD_SOLIDS;
43
44
if (solids) {
45
if (polygon.size() < 3) {
46
return;
47
}
48
49
//here comes the sun, lalalala
50
//decompose concave into multiple convex polygons and add them
51
Vector<Vector<Vector2>> decomp = _decompose_in_convex();
52
for (int i = 0; i < decomp.size(); i++) {
53
Ref<ConvexPolygonShape2D> convex = memnew(ConvexPolygonShape2D);
54
convex->set_points(decomp[i]);
55
collision_object->shape_owner_add_shape(owner_id, convex);
56
}
57
58
} else {
59
if (polygon.size() < 2) {
60
return;
61
}
62
63
Ref<ConcavePolygonShape2D> concave = memnew(ConcavePolygonShape2D);
64
65
Vector<Vector2> segments;
66
segments.resize(polygon.size() * 2);
67
Vector2 *w = segments.ptrw();
68
69
for (int i = 0; i < polygon.size(); i++) {
70
w[(i << 1) + 0] = polygon[i];
71
w[(i << 1) + 1] = polygon[(i + 1) % polygon.size()];
72
}
73
74
concave->set_segments(segments);
75
76
collision_object->shape_owner_add_shape(owner_id, concave);
77
}
78
}
79
80
Vector<Vector<Vector2>> CollisionPolygon2D::_decompose_in_convex() {
81
Vector<Vector<Vector2>> decomp = Geometry2D::decompose_polygon_in_convex(polygon);
82
return decomp;
83
}
84
85
void CollisionPolygon2D::_update_in_shape_owner(bool p_xform_only) {
86
collision_object->shape_owner_set_transform(owner_id, get_transform());
87
if (p_xform_only) {
88
return;
89
}
90
collision_object->shape_owner_set_disabled(owner_id, disabled);
91
collision_object->shape_owner_set_one_way_collision(owner_id, one_way_collision);
92
collision_object->shape_owner_set_one_way_collision_margin(owner_id, one_way_collision_margin);
93
}
94
95
void CollisionPolygon2D::_notification(int p_what) {
96
switch (p_what) {
97
case NOTIFICATION_PARENTED: {
98
collision_object = Object::cast_to<CollisionObject2D>(get_parent());
99
if (collision_object) {
100
owner_id = collision_object->create_shape_owner(this);
101
_build_polygon();
102
_update_in_shape_owner();
103
}
104
} break;
105
106
case NOTIFICATION_ENTER_TREE: {
107
if (collision_object) {
108
_update_in_shape_owner();
109
}
110
} break;
111
112
case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
113
if (collision_object) {
114
_update_in_shape_owner(true);
115
}
116
} break;
117
118
case NOTIFICATION_UNPARENTED: {
119
if (collision_object) {
120
collision_object->remove_shape_owner(owner_id);
121
}
122
owner_id = 0;
123
collision_object = nullptr;
124
} break;
125
126
case NOTIFICATION_DRAW: {
127
ERR_FAIL_COND(!is_inside_tree());
128
if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) {
129
break;
130
}
131
132
if (polygon.size() > 2) {
133
#ifdef TOOLS_ENABLED
134
if (build_mode == BUILD_SOLIDS) {
135
Vector<Vector<Vector2>> decomp = _decompose_in_convex();
136
137
Color c(0.4, 0.9, 0.1);
138
for (int i = 0; i < decomp.size(); i++) {
139
c.set_hsv(Math::fmod(c.get_h() + 0.738, 1), c.get_s(), c.get_v(), 0.5);
140
draw_colored_polygon(decomp[i], c);
141
}
142
}
143
#endif
144
145
const Color stroke_color = get_tree()->get_debug_collisions_color();
146
draw_polyline(polygon, stroke_color);
147
// Draw the last segment.
148
draw_line(polygon[polygon.size() - 1], polygon[0], stroke_color);
149
}
150
151
if (one_way_collision) {
152
Color dcol = get_tree()->get_debug_collisions_color(); //0.9,0.2,0.2,0.4);
153
dcol.a = 1.0;
154
Vector2 line_to(0, 20);
155
draw_line(Vector2(), line_to, dcol, 3);
156
real_t tsize = 8;
157
158
Vector<Vector2> pts = {
159
line_to + Vector2(0, tsize),
160
line_to + Vector2(Math::SQRT12 * tsize, 0),
161
line_to + Vector2(-Math::SQRT12 * tsize, 0)
162
};
163
164
Vector<Color> cols{ dcol, dcol, dcol };
165
166
draw_primitive(pts, cols, Vector<Vector2>()); //small arrow
167
}
168
} break;
169
}
170
}
171
172
void CollisionPolygon2D::set_polygon(const Vector<Point2> &p_polygon) {
173
polygon = p_polygon;
174
175
{
176
for (int i = 0; i < polygon.size(); i++) {
177
if (i == 0) {
178
aabb = Rect2(polygon[i], Size2());
179
} else {
180
aabb.expand_to(polygon[i]);
181
}
182
}
183
if (aabb == Rect2()) {
184
aabb = Rect2(-10, -10, 20, 20);
185
} else {
186
aabb.position -= aabb.size * 0.3;
187
aabb.size += aabb.size * 0.6;
188
}
189
}
190
191
if (collision_object) {
192
_build_polygon();
193
_update_in_shape_owner();
194
}
195
queue_redraw();
196
update_configuration_warnings();
197
}
198
199
Vector<Point2> CollisionPolygon2D::get_polygon() const {
200
return polygon;
201
}
202
203
void CollisionPolygon2D::set_build_mode(BuildMode p_mode) {
204
ERR_FAIL_INDEX((int)p_mode, 2);
205
build_mode = p_mode;
206
if (collision_object) {
207
_build_polygon();
208
_update_in_shape_owner();
209
}
210
queue_redraw();
211
update_configuration_warnings();
212
}
213
214
CollisionPolygon2D::BuildMode CollisionPolygon2D::get_build_mode() const {
215
return build_mode;
216
}
217
218
#ifdef DEBUG_ENABLED
219
Rect2 CollisionPolygon2D::_edit_get_rect() const {
220
return aabb;
221
}
222
223
bool CollisionPolygon2D::_edit_use_rect() const {
224
return true;
225
}
226
227
bool CollisionPolygon2D::_edit_is_selected_on_click(const Point2 &p_point, double p_tolerance) const {
228
return Geometry2D::is_point_in_polygon(p_point, Variant(polygon));
229
}
230
#endif
231
232
PackedStringArray CollisionPolygon2D::get_configuration_warnings() const {
233
PackedStringArray warnings = Node2D::get_configuration_warnings();
234
235
if (!Object::cast_to<CollisionObject2D>(get_parent())) {
236
warnings.push_back(RTR("CollisionPolygon2D only serves to provide a collision shape to a CollisionObject2D derived node. Please only use it as a child of Area2D, StaticBody2D, RigidBody2D, CharacterBody2D, etc. to give them a shape."));
237
}
238
239
int polygon_count = polygon.size();
240
if (polygon_count == 0) {
241
warnings.push_back(RTR("An empty CollisionPolygon2D has no effect on collision."));
242
} else {
243
bool solids = build_mode == BUILD_SOLIDS;
244
if (solids) {
245
if (polygon_count < 3) {
246
warnings.push_back(RTR("Invalid polygon. At least 3 points are needed in 'Solids' build mode."));
247
}
248
} else if (polygon_count < 2) {
249
warnings.push_back(RTR("Invalid polygon. At least 2 points are needed in 'Segments' build mode."));
250
}
251
}
252
if (one_way_collision && Object::cast_to<Area2D>(get_parent())) {
253
warnings.push_back(RTR("The One Way Collision property will be ignored when the collision object is an Area2D."));
254
}
255
256
return warnings;
257
}
258
259
void CollisionPolygon2D::set_disabled(bool p_disabled) {
260
disabled = p_disabled;
261
queue_redraw();
262
if (collision_object) {
263
collision_object->shape_owner_set_disabled(owner_id, p_disabled);
264
}
265
}
266
267
bool CollisionPolygon2D::is_disabled() const {
268
return disabled;
269
}
270
271
void CollisionPolygon2D::set_one_way_collision(bool p_enable) {
272
one_way_collision = p_enable;
273
queue_redraw();
274
if (collision_object) {
275
collision_object->shape_owner_set_one_way_collision(owner_id, p_enable);
276
}
277
update_configuration_warnings();
278
}
279
280
bool CollisionPolygon2D::is_one_way_collision_enabled() const {
281
return one_way_collision;
282
}
283
284
void CollisionPolygon2D::set_one_way_collision_margin(real_t p_margin) {
285
one_way_collision_margin = p_margin;
286
if (collision_object) {
287
collision_object->shape_owner_set_one_way_collision_margin(owner_id, one_way_collision_margin);
288
}
289
}
290
291
real_t CollisionPolygon2D::get_one_way_collision_margin() const {
292
return one_way_collision_margin;
293
}
294
295
void CollisionPolygon2D::_bind_methods() {
296
ClassDB::bind_method(D_METHOD("set_polygon", "polygon"), &CollisionPolygon2D::set_polygon);
297
ClassDB::bind_method(D_METHOD("get_polygon"), &CollisionPolygon2D::get_polygon);
298
299
ClassDB::bind_method(D_METHOD("set_build_mode", "build_mode"), &CollisionPolygon2D::set_build_mode);
300
ClassDB::bind_method(D_METHOD("get_build_mode"), &CollisionPolygon2D::get_build_mode);
301
ClassDB::bind_method(D_METHOD("set_disabled", "disabled"), &CollisionPolygon2D::set_disabled);
302
ClassDB::bind_method(D_METHOD("is_disabled"), &CollisionPolygon2D::is_disabled);
303
ClassDB::bind_method(D_METHOD("set_one_way_collision", "enabled"), &CollisionPolygon2D::set_one_way_collision);
304
ClassDB::bind_method(D_METHOD("is_one_way_collision_enabled"), &CollisionPolygon2D::is_one_way_collision_enabled);
305
ClassDB::bind_method(D_METHOD("set_one_way_collision_margin", "margin"), &CollisionPolygon2D::set_one_way_collision_margin);
306
ClassDB::bind_method(D_METHOD("get_one_way_collision_margin"), &CollisionPolygon2D::get_one_way_collision_margin);
307
308
ADD_PROPERTY(PropertyInfo(Variant::INT, "build_mode", PROPERTY_HINT_ENUM, "Solids,Segments"), "set_build_mode", "get_build_mode");
309
ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "polygon"), "set_polygon", "get_polygon");
310
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
311
312
ADD_GROUP("One Way Collision", "one_way_collision");
313
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_way_collision", PROPERTY_HINT_GROUP_ENABLE), "set_one_way_collision", "is_one_way_collision_enabled");
314
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");
315
316
BIND_ENUM_CONSTANT(BUILD_SOLIDS);
317
BIND_ENUM_CONSTANT(BUILD_SEGMENTS);
318
}
319
320
CollisionPolygon2D::CollisionPolygon2D() {
321
set_notify_local_transform(true);
322
set_hide_clip_children(true);
323
}
324
325