| 1 | /**************************************************************************/ |
| 2 | /* box_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 "box_shape_3d.h" |
| 32 | #include "servers/physics_server_3d.h" |
| 33 | |
| 34 | Vector<Vector3> BoxShape3D::get_debug_mesh_lines() const { |
| 35 | Vector<Vector3> lines; |
| 36 | AABB aabb; |
| 37 | aabb.position = -size / 2; |
| 38 | aabb.size = size; |
| 39 | |
| 40 | for (int i = 0; i < 12; i++) { |
| 41 | Vector3 a, b; |
| 42 | aabb.get_edge(i, a, b); |
| 43 | lines.push_back(a); |
| 44 | lines.push_back(b); |
| 45 | } |
| 46 | |
| 47 | return lines; |
| 48 | } |
| 49 | |
| 50 | real_t BoxShape3D::get_enclosing_radius() const { |
| 51 | return size.length() / 2; |
| 52 | } |
| 53 | |
| 54 | void BoxShape3D::_update_shape() { |
| 55 | PhysicsServer3D::get_singleton()->shape_set_data(get_shape(), size / 2); |
| 56 | Shape3D::_update_shape(); |
| 57 | } |
| 58 | |
| 59 | #ifndef DISABLE_DEPRECATED |
| 60 | bool BoxShape3D::_set(const StringName &p_name, const Variant &p_value) { |
| 61 | if (p_name == "extents" ) { // Compatibility with Godot 3.x. |
| 62 | // Convert to `size`, twice as big. |
| 63 | set_size((Vector3)p_value * 2); |
| 64 | return true; |
| 65 | } |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | bool BoxShape3D::_get(const StringName &p_name, Variant &r_property) const { |
| 70 | if (p_name == "extents" ) { // Compatibility with Godot 3.x. |
| 71 | // Convert to `extents`, half as big. |
| 72 | r_property = size / 2; |
| 73 | return true; |
| 74 | } |
| 75 | return false; |
| 76 | } |
| 77 | #endif // DISABLE_DEPRECATED |
| 78 | |
| 79 | void BoxShape3D::set_size(const Vector3 &p_size) { |
| 80 | ERR_FAIL_COND_MSG(p_size.x < 0 || p_size.y < 0 || p_size.z < 0, "BoxShape3D size cannot be negative." ); |
| 81 | size = p_size; |
| 82 | _update_shape(); |
| 83 | emit_changed(); |
| 84 | } |
| 85 | |
| 86 | Vector3 BoxShape3D::get_size() const { |
| 87 | return size; |
| 88 | } |
| 89 | |
| 90 | void BoxShape3D::_bind_methods() { |
| 91 | ClassDB::bind_method(D_METHOD("set_size" , "size" ), &BoxShape3D::set_size); |
| 92 | ClassDB::bind_method(D_METHOD("get_size" ), &BoxShape3D::get_size); |
| 93 | |
| 94 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "size" , PROPERTY_HINT_NONE, "suffix:m" ), "set_size" , "get_size" ); |
| 95 | } |
| 96 | |
| 97 | BoxShape3D::BoxShape3D() : |
| 98 | Shape3D(PhysicsServer3D::get_singleton()->shape_create(PhysicsServer3D::SHAPE_BOX)) { |
| 99 | set_size(Vector3(1, 1, 1)); |
| 100 | } |
| 101 | |