| 1 | /**************************************************************************/ |
| 2 | /* convex_polygon_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 "convex_polygon_shape_3d.h" |
| 32 | #include "core/math/convex_hull.h" |
| 33 | #include "servers/physics_server_3d.h" |
| 34 | |
| 35 | Vector<Vector3> ConvexPolygonShape3D::get_debug_mesh_lines() const { |
| 36 | Vector<Vector3> poly_points = get_points(); |
| 37 | |
| 38 | if (poly_points.size() > 3) { |
| 39 | Vector<Vector3> varr = Variant(poly_points); |
| 40 | Geometry3D::MeshData md; |
| 41 | Error err = ConvexHullComputer::convex_hull(varr, md); |
| 42 | if (err == OK) { |
| 43 | Vector<Vector3> lines; |
| 44 | lines.resize(md.edges.size() * 2); |
| 45 | for (uint32_t i = 0; i < md.edges.size(); i++) { |
| 46 | lines.write[i * 2 + 0] = md.vertices[md.edges[i].vertex_a]; |
| 47 | lines.write[i * 2 + 1] = md.vertices[md.edges[i].vertex_b]; |
| 48 | } |
| 49 | return lines; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | return Vector<Vector3>(); |
| 54 | } |
| 55 | |
| 56 | real_t ConvexPolygonShape3D::get_enclosing_radius() const { |
| 57 | Vector<Vector3> data = get_points(); |
| 58 | const Vector3 *read = data.ptr(); |
| 59 | real_t r = 0.0; |
| 60 | for (int i(0); i < data.size(); i++) { |
| 61 | r = MAX(read[i].length_squared(), r); |
| 62 | } |
| 63 | return Math::sqrt(r); |
| 64 | } |
| 65 | |
| 66 | void ConvexPolygonShape3D::_update_shape() { |
| 67 | PhysicsServer3D::get_singleton()->shape_set_data(get_shape(), points); |
| 68 | Shape3D::_update_shape(); |
| 69 | } |
| 70 | |
| 71 | void ConvexPolygonShape3D::set_points(const Vector<Vector3> &p_points) { |
| 72 | points = p_points; |
| 73 | _update_shape(); |
| 74 | emit_changed(); |
| 75 | } |
| 76 | |
| 77 | Vector<Vector3> ConvexPolygonShape3D::get_points() const { |
| 78 | return points; |
| 79 | } |
| 80 | |
| 81 | void ConvexPolygonShape3D::_bind_methods() { |
| 82 | ClassDB::bind_method(D_METHOD("set_points" , "points" ), &ConvexPolygonShape3D::set_points); |
| 83 | ClassDB::bind_method(D_METHOD("get_points" ), &ConvexPolygonShape3D::get_points); |
| 84 | |
| 85 | ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "points" ), "set_points" , "get_points" ); |
| 86 | } |
| 87 | |
| 88 | ConvexPolygonShape3D::ConvexPolygonShape3D() : |
| 89 | Shape3D(PhysicsServer3D::get_singleton()->shape_create(PhysicsServer3D::SHAPE_CONVEX_POLYGON)) { |
| 90 | } |
| 91 | |