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 "collision_object_3d.h"
34#include "core/math/geometry_2d.h"
35#include "scene/resources/convex_polygon_shape_3d.h"
36
37void CollisionPolygon3D::_build_polygon() {
38 if (!collision_object) {
39 return;
40 }
41
42 collision_object->shape_owner_clear_shapes(owner_id);
43
44 if (polygon.size() == 0) {
45 return;
46 }
47
48 Vector<Vector<Vector2>> decomp = Geometry2D::decompose_polygon_in_convex(polygon);
49 if (decomp.size() == 0) {
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 collision_object->shape_owner_add_shape(owner_id, convex);
74 collision_object->shape_owner_set_disabled(owner_id, disabled);
75 }
76}
77
78void CollisionPolygon3D::_update_in_shape_owner(bool p_xform_only) {
79 collision_object->shape_owner_set_transform(owner_id, get_transform());
80 if (p_xform_only) {
81 return;
82 }
83 collision_object->shape_owner_set_disabled(owner_id, disabled);
84}
85
86void CollisionPolygon3D::_notification(int p_what) {
87 switch (p_what) {
88 case NOTIFICATION_PARENTED: {
89 collision_object = Object::cast_to<CollisionObject3D>(get_parent());
90 if (collision_object) {
91 owner_id = collision_object->create_shape_owner(this);
92 _build_polygon();
93 _update_in_shape_owner();
94 }
95 } break;
96
97 case NOTIFICATION_ENTER_TREE: {
98 if (collision_object) {
99 _update_in_shape_owner();
100 }
101 } break;
102
103 case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
104 if (collision_object) {
105 _update_in_shape_owner(true);
106 }
107 update_configuration_warnings();
108 } break;
109
110 case NOTIFICATION_UNPARENTED: {
111 if (collision_object) {
112 collision_object->remove_shape_owner(owner_id);
113 }
114 owner_id = 0;
115 collision_object = nullptr;
116 } break;
117 }
118}
119
120void CollisionPolygon3D::set_polygon(const Vector<Point2> &p_polygon) {
121 polygon = p_polygon;
122 if (collision_object) {
123 _build_polygon();
124 }
125 update_configuration_warnings();
126 update_gizmos();
127}
128
129Vector<Point2> CollisionPolygon3D::get_polygon() const {
130 return polygon;
131}
132
133AABB CollisionPolygon3D::get_item_rect() const {
134 return aabb;
135}
136
137void CollisionPolygon3D::set_depth(real_t p_depth) {
138 depth = p_depth;
139 _build_polygon();
140 update_gizmos();
141}
142
143real_t CollisionPolygon3D::get_depth() const {
144 return depth;
145}
146
147void CollisionPolygon3D::set_disabled(bool p_disabled) {
148 disabled = p_disabled;
149 update_gizmos();
150
151 if (collision_object) {
152 collision_object->shape_owner_set_disabled(owner_id, p_disabled);
153 }
154}
155
156bool CollisionPolygon3D::is_disabled() const {
157 return disabled;
158}
159
160real_t CollisionPolygon3D::get_margin() const {
161 return margin;
162}
163
164void CollisionPolygon3D::set_margin(real_t p_margin) {
165 margin = p_margin;
166 if (collision_object) {
167 _build_polygon();
168 }
169}
170
171PackedStringArray CollisionPolygon3D::get_configuration_warnings() const {
172 PackedStringArray warnings = Node::get_configuration_warnings();
173
174 if (!Object::cast_to<CollisionObject3D>(get_parent())) {
175 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."));
176 }
177
178 if (polygon.is_empty()) {
179 warnings.push_back(RTR("An empty CollisionPolygon3D has no effect on collision."));
180 }
181
182 Vector3 scale = get_transform().get_basis().get_scale();
183 if (!(Math::is_zero_approx(scale.x - scale.y) && Math::is_zero_approx(scale.y - scale.z))) {
184 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."));
185 }
186
187 return warnings;
188}
189
190bool CollisionPolygon3D::_is_editable_3d_polygon() const {
191 return true;
192}
193
194void CollisionPolygon3D::_bind_methods() {
195 ClassDB::bind_method(D_METHOD("set_depth", "depth"), &CollisionPolygon3D::set_depth);
196 ClassDB::bind_method(D_METHOD("get_depth"), &CollisionPolygon3D::get_depth);
197
198 ClassDB::bind_method(D_METHOD("set_polygon", "polygon"), &CollisionPolygon3D::set_polygon);
199 ClassDB::bind_method(D_METHOD("get_polygon"), &CollisionPolygon3D::get_polygon);
200
201 ClassDB::bind_method(D_METHOD("set_disabled", "disabled"), &CollisionPolygon3D::set_disabled);
202 ClassDB::bind_method(D_METHOD("is_disabled"), &CollisionPolygon3D::is_disabled);
203
204 ClassDB::bind_method(D_METHOD("set_margin", "margin"), &CollisionPolygon3D::set_margin);
205 ClassDB::bind_method(D_METHOD("get_margin"), &CollisionPolygon3D::get_margin);
206
207 ClassDB::bind_method(D_METHOD("_is_editable_3d_polygon"), &CollisionPolygon3D::_is_editable_3d_polygon);
208
209 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "depth", PROPERTY_HINT_NONE, "suffix:m"), "set_depth", "get_depth");
210 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
211 ADD_PROPERTY(PropertyInfo(Variant::PACKED_VECTOR2_ARRAY, "polygon"), "set_polygon", "get_polygon");
212 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "margin", PROPERTY_HINT_RANGE, "0.001,10,0.001,suffix:m"), "set_margin", "get_margin");
213}
214
215CollisionPolygon3D::CollisionPolygon3D() {
216 set_notify_local_transform(true);
217}
218