1/**************************************************************************/
2/* collision_shape_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_shape_3d.h"
32
33#include "mesh_instance_3d.h"
34#include "physics_body_3d.h"
35#include "scene/resources/concave_polygon_shape_3d.h"
36#include "scene/resources/convex_polygon_shape_3d.h"
37#include "scene/resources/world_boundary_shape_3d.h"
38
39void CollisionShape3D::make_convex_from_siblings() {
40 Node *p = get_parent();
41 if (!p) {
42 return;
43 }
44
45 Vector<Vector3> vertices;
46
47 for (int i = 0; i < p->get_child_count(); i++) {
48 Node *n = p->get_child(i);
49 MeshInstance3D *mi = Object::cast_to<MeshInstance3D>(n);
50 if (mi) {
51 Ref<Mesh> m = mi->get_mesh();
52 if (m.is_valid()) {
53 for (int j = 0; j < m->get_surface_count(); j++) {
54 Array a = m->surface_get_arrays(j);
55 if (!a.is_empty()) {
56 Vector<Vector3> v = a[RenderingServer::ARRAY_VERTEX];
57 for (int k = 0; k < v.size(); k++) {
58 vertices.append(mi->get_transform().xform(v[k]));
59 }
60 }
61 }
62 }
63 }
64 }
65
66 Ref<ConvexPolygonShape3D> shape_new = memnew(ConvexPolygonShape3D);
67 shape_new->set_points(vertices);
68 set_shape(shape_new);
69}
70
71void CollisionShape3D::_update_in_shape_owner(bool p_xform_only) {
72 collision_object->shape_owner_set_transform(owner_id, get_transform());
73 if (p_xform_only) {
74 return;
75 }
76 collision_object->shape_owner_set_disabled(owner_id, disabled);
77}
78
79void CollisionShape3D::_notification(int p_what) {
80 switch (p_what) {
81 case NOTIFICATION_PARENTED: {
82 collision_object = Object::cast_to<CollisionObject3D>(get_parent());
83 if (collision_object) {
84 owner_id = collision_object->create_shape_owner(this);
85 if (shape.is_valid()) {
86 collision_object->shape_owner_add_shape(owner_id, shape);
87 }
88 _update_in_shape_owner();
89 }
90 } break;
91
92 case NOTIFICATION_ENTER_TREE: {
93 if (collision_object) {
94 _update_in_shape_owner();
95 }
96 } break;
97
98 case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
99 if (collision_object) {
100 _update_in_shape_owner(true);
101 }
102 update_configuration_warnings();
103 } break;
104
105 case NOTIFICATION_UNPARENTED: {
106 if (collision_object) {
107 collision_object->remove_shape_owner(owner_id);
108 }
109 owner_id = 0;
110 collision_object = nullptr;
111 } break;
112 }
113}
114
115#ifndef DISABLE_DEPRECATED
116void CollisionShape3D::resource_changed(Ref<Resource> res) {
117}
118#endif
119
120PackedStringArray CollisionShape3D::get_configuration_warnings() const {
121 PackedStringArray warnings = Node::get_configuration_warnings();
122
123 CollisionObject3D *col_object = Object::cast_to<CollisionObject3D>(get_parent());
124 if (col_object == nullptr) {
125 warnings.push_back(RTR("CollisionShape3D 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."));
126 }
127
128 if (!shape.is_valid()) {
129 warnings.push_back(RTR("A shape must be provided for CollisionShape3D to function. Please create a shape resource for it."));
130 }
131
132 if (shape.is_valid() && Object::cast_to<RigidBody3D>(col_object)) {
133 if (Object::cast_to<ConcavePolygonShape3D>(*shape)) {
134 warnings.push_back(RTR("ConcavePolygonShape3D doesn't support RigidBody3D in another mode than static."));
135 } else if (Object::cast_to<WorldBoundaryShape3D>(*shape)) {
136 warnings.push_back(RTR("WorldBoundaryShape3D doesn't support RigidBody3D in another mode than static."));
137 }
138 }
139
140 Vector3 scale = get_transform().get_basis().get_scale();
141 if (!(Math::is_zero_approx(scale.x - scale.y) && Math::is_zero_approx(scale.y - scale.z))) {
142 warnings.push_back(RTR("A non-uniformly scaled CollisionShape3D node will probably not function as expected.\nPlease make its scale uniform (i.e. the same on all axes), and change the size of its shape resource instead."));
143 }
144
145 return warnings;
146}
147
148void CollisionShape3D::_bind_methods() {
149#ifndef DISABLE_DEPRECATED
150 ClassDB::bind_method(D_METHOD("resource_changed", "resource"), &CollisionShape3D::resource_changed);
151#endif
152 ClassDB::bind_method(D_METHOD("set_shape", "shape"), &CollisionShape3D::set_shape);
153 ClassDB::bind_method(D_METHOD("get_shape"), &CollisionShape3D::get_shape);
154 ClassDB::bind_method(D_METHOD("set_disabled", "enable"), &CollisionShape3D::set_disabled);
155 ClassDB::bind_method(D_METHOD("is_disabled"), &CollisionShape3D::is_disabled);
156 ClassDB::bind_method(D_METHOD("make_convex_from_siblings"), &CollisionShape3D::make_convex_from_siblings);
157 ClassDB::set_method_flags("CollisionShape3D", "make_convex_from_siblings", METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR);
158
159 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape3D"), "set_shape", "get_shape");
160 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
161}
162
163void CollisionShape3D::set_shape(const Ref<Shape3D> &p_shape) {
164 if (p_shape == shape) {
165 return;
166 }
167 if (shape.is_valid()) {
168 shape->disconnect_changed(callable_mp((Node3D *)this, &Node3D::update_gizmos));
169 }
170 shape = p_shape;
171 if (shape.is_valid()) {
172 shape->connect_changed(callable_mp((Node3D *)this, &Node3D::update_gizmos));
173 }
174 update_gizmos();
175 if (collision_object) {
176 collision_object->shape_owner_clear_shapes(owner_id);
177 if (shape.is_valid()) {
178 collision_object->shape_owner_add_shape(owner_id, shape);
179 }
180 }
181
182 if (is_inside_tree() && collision_object) {
183 // If this is a heightfield shape our center may have changed
184 _update_in_shape_owner(true);
185 }
186 update_configuration_warnings();
187}
188
189Ref<Shape3D> CollisionShape3D::get_shape() const {
190 return shape;
191}
192
193void CollisionShape3D::set_disabled(bool p_disabled) {
194 disabled = p_disabled;
195 update_gizmos();
196 if (collision_object) {
197 collision_object->shape_owner_set_disabled(owner_id, p_disabled);
198 }
199}
200
201bool CollisionShape3D::is_disabled() const {
202 return disabled;
203}
204
205CollisionShape3D::CollisionShape3D() {
206 //indicator = RenderingServer::get_singleton()->mesh_create();
207 set_notify_local_transform(true);
208}
209
210CollisionShape3D::~CollisionShape3D() {
211 //RenderingServer::get_singleton()->free(indicator);
212}
213