Debian Codegraph @

scene/3d/physics/collision_polygon_3d.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* collision_polygon_3d.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_3d.h"
32
33 #include "core/math/geometry_2d.h"
34 #include "scene/3d/physics/collision_object_3d.h"
35 #include "scene/resources/3d/convex_polygon_shape_3d.h"
36
37 void CollisionPolygon3D::_build_polygon() {
38 if (!collision_object) {
39 return;
40 }
41
42 collision_object->shape_owner_clear_shapes(owner_id);
43
44 if (polygon.is_empty()) {
45 return;
46 }
47
48 Vector<Vector<Vector2>> decomp = Geometry2D::decompose_polygon_in_convex(polygon);
49 if (decomp.is_empty()) {
50 return;
51 }
52
53 //here comes the sun, lalalala
54 //decompose concave into multiple convex polygons and add them
55
56 for (int i = 0; i < decomp.size(); i++) {
57 Ref<ConvexPolygonShape3D> convex = memnew(ConvexPolygonShape3D);
58 Vector<Vector3> cp;
59 int cs = decomp[i].size();
60 cp.resize(cs * 2);
61 {
62 Vector3 *w = cp.ptrw();
63 int idx = 0;
64 for (int j = 0; j < cs; j++) {
65 Vector2 d = decomp[i][j];
66 w[idx++] = Vector3(d.x, d.y, depth * 0.5);
67 w[idx++] = Vector3(d.x, d.y, -depth * 0.5);
68 }
69 }
70
71 convex->set_points(cp);
72 convex->set_margin(margin);
73 convex->set_debug_color(debug_color);
74 convex->set_debug_fill(debug_fill);
75 collision_object->shape_owner_add_shape(owner_id, convex);
76 collision_object->shape_owner_set_disabled(owner_id, disabled);
77 }
78 }
79
80 void CollisionPolygon3D::_update_in_shape_owner(bool p_xform_only) {
81 collision_object->shape_owner_set_transform(owner_id, get_transform());
82 if (p_xform_only) {
83 return;
84 }
85 collision_object->shape_owner_set_disabled(owner_id, disabled);
86 }
87
88 void CollisionPolygon3D::_notification(int p_what) {
89 switch (p_what) {
90 case NOTIFICATION_PARENTED: {
91 collision_object = Object::cast_to<CollisionObject3D>(get_parent());
92 if (collision_object) {
93 owner_id = collision_object->create_shape_owner(this);
94 _build_polygon();
95 _update_in_shape_owner();
96 }
97 } break;
98
99 case NOTIFICATION_ENTER_TREE: {
100 if (collision_object) {
101 _update_in_shape_owner();
102 }
103 } break;
104
105 case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
106 if (collision_object) {
107 _update_in_shape_owner(true);
108 }
109 update_configuration_warnings();
110 } break;
111
112 case NOTIFICATION_UNPARENTED: {
113 if (collision_object) {
114 collision_object->remove_shape_owner(owner_id);
115 }
116 owner_id = 0;
117 collision_object = nullptr;
118 } break;
119 }
120 }
121
122 void CollisionPolygon3D::set_polygon(const Vector<Point2> &p_polygon) {
123 polygon = p_polygon;
124 if (collision_object) {
125 _build_polygon();
126 }
127 update_configuration_warnings();
128 update_gizmos();
129 }
130
131 Vector<Point2> CollisionPolygon3D::get_polygon() const {
132 return polygon;
133 }
134
135 AABB CollisionPolygon3D::get_item_rect() const {
136 return aabb;
137 }
138
139 void CollisionPolygon3D::set_depth(real_t p_depth) {
140 depth = p_depth;
141 _build_polygon();
142 update_gizmos();
143 }
144
145 real_t CollisionPolygon3D::get_depth() const {
146 return depth;
147 }
148
149 void CollisionPolygon3D::set_disabled(bool p_disabled) {
150 disabled = p_disabled;
151 update_gizmos();
152
153 if (collision_object) {
154 collision_object->shape_owner_set_disabled(owner_id, p_disabled);
155 }
156 }
157
158 bool CollisionPolygon3D::is_disabled() const {
159 return disabled;
160 }
161
162 Color CollisionPolygon3D::_get_default_debug_color() const {
163 const SceneTree *st = SceneTree::get_singleton();
164 return st ? st->get_debug_collisions_color() : Color(0.0, 0.0, 0.0, 0.0);
165 }
166
167 void CollisionPolygon3D::set_debug_color(const Color &p_color) {
168 if (debug_color == p_color) {
169 return;
170 }
171
172 debug_color = p_color;
173
174 update_gizmos();
175 }
176
177 Color CollisionPolygon3D::get_debug_color() const {
178 return debug_color;
179 }
180
181 void CollisionPolygon3D::set_debug_fill_enabled(bool p_enable) {
182 if (debug_fill == p_enable) {
183 return;
184 }
185
186 debug_fill = p_enable;
187
188 update_gizmos();
189 }
190
191 bool CollisionPolygon3D::get_debug_fill_enabled() const {
192 return debug_fill;
193 }
194
195 #ifdef DEBUG_ENABLED
196
197 bool CollisionPolygon3D::_property_can_revert(const StringName &p_name) const {
198 if (p_name == "debug_color") {
199 return true;
200 }
201 return false;
202 }
203
204 bool CollisionPolygon3D::_property_get_revert(const StringName &p_name, Variant &r_property) const {
205 if (p_name == "debug_color") {
206 r_property = _get_default_debug_color();
207 return true;
208 }
209 return false;
210 }
211
212 void CollisionPolygon3D::_validate_property(PropertyInfo &p_property) const {
213 if (p_property.name == "debug_color") {
214 if (debug_color == _get_default_debug_color()) {
215 p_property.usage = PROPERTY_USAGE_DEFAULT & ~PROPERTY_USAGE_STORAGE;
216 } else {
217 p_property.usage = PROPERTY_USAGE_DEFAULT;
218 }
219 }
220 }
221
222 #endif // DEBUG_ENABLED
223
224 real_t CollisionPolygon3D::get_margin() const {
225 return margin;
226 }
227
228 void CollisionPolygon3D::set_margin(real_t p_margin) {
229 margin = p_margin;
230 if (collision_object) {
231 _build_polygon();
232 }
233 }
234
235 PackedStringArray CollisionPolygon3D::get_configuration_warnings() const {
236 PackedStringArray warnings = Node3D::get_configuration_warnings();
237
238 if (!Object::cast_to<CollisionObject3D>(get_parent())) {
239 warnings.push_back(RTR("CollisionPolygon3D only serves to provide a collision shape to a CollisionObject3D derived node.\nPlease only use it as a child of Area3D, StaticBody3D, RigidBody3D, CharacterBody3D, etc. to give them a shape."));
240 }
241
242 if (polygon.is_empty()) {
243 warnings.push_back(RTR("An empty CollisionPolygon3D has no effect on collision."));
244 }
245
246 Vector3 scale = get_transform().get_basis().get_scale();
247 if (!(Math::is_zero_approx(scale.x - scale.y) && Math::is_zero_approx(scale.y - scale.z))) {
248 warnings.push_back(RTR("A non-uniformly scaled CollisionPolygon3D node will probably not function as expected.\nPlease make its scale uniform (i.e. the same on all axes), and change its polygon's vertices instead."));
249 }
250
251 return warnings;
252 }
253
254 bool CollisionPolygon3D::_is_editable_3d_polygon() const {
255 return true;
256 }
257
258 void CollisionPolygon3D::_bind_methods() {
259 ClassDB::bind_method(D_METHOD("set_depth", "depth"), &CollisionPolygon3D::set_depth);
260 ClassDB::bind_method(D_METHOD("get_depth"), &CollisionPolygon3D::get_depth);
261
262 ClassDB::bind_method(D_METHOD("set_polygon", "polygon"), &CollisionPolygon3D::set_polygon);
263 ClassDB::bind_method(D_METHOD("get_polygon"), &CollisionPolygon3D::get_polygon);
264
265 ClassDB::bind_method(D_METHOD("set_disabled", "disabled"), &CollisionPolygon3D::set_disabled);
266 ClassDB::bind_method(D_METHOD("is_disabled"), &CollisionPolygon3D::is_disabled);
267
268 ClassDB::bind_method(D_METHOD("set_debug_color", "color"), &CollisionPolygon3D::set_debug_color);
269 ClassDB::bind_method(D_METHOD("get_debug_color"), &CollisionPolygon3D::get_debug_color);
270
271 ClassDB::bind_method(D_METHOD("set_enable_debug_fill", "enable"), &CollisionPolygon3D::set_debug_fill_enabled);
272 ClassDB::bind_method(D_METHOD("get_enable_debug_fill"), &CollisionPolygon3D::get_debug_fill_enabled);
273
274 ClassDB::bind_method(D_METHOD("set_margin", "margin"), &CollisionPolygon3D::set_margin);
275 ClassDB::bind_method(D_METHOD("get_margin"), &CollisionPolygon3D::get_margin);
276
277 ClassDB::bind_method(D_METHOD("_is_editable_3d_polygon"), &CollisionPolygon3D::_is_editable_3d_polygon);
278
279 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "depth", PROPERTY_HINT_NONE, "suffix:m"), "set_depth", "get_depth");
280 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
281 ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "polygon"), "set_polygon", "get_polygon");
282 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "margin", PROPERTY_HINT_RANGE, "0.001,10,0.001,suffix:m"), "set_margin", "get_margin");
283
284 ADD_PROPERTY(PropertyInfo(Variant::COLOR, "debug_color"), "set_debug_color", "get_debug_color");
285 // Default value depends on a project setting, override for doc generation purposes.
286 ADD_PROPERTY_DEFAULT("debug_color", Color(0.0, 0.0, 0.0, 0.0));
287
288 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_fill"), "set_enable_debug_fill", "get_enable_debug_fill");
289 }
290
291 CollisionPolygon3D::CollisionPolygon3D() {
292 set_notify_local_transform(true);
293 debug_color = _get_default_debug_color();
294 }