| 1 | /**************************************************************************/ |
| 2 | /* jolt_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 "jolt_box_shape_3d.h" |
| 32 | |
| 33 | #include "../jolt_project_settings.h" |
| 34 | #include "../misc/jolt_type_conversions.h" |
| 35 | |
| 36 | #include "Jolt/Physics/Collision/Shape/BoxShape.h" |
| 37 | |
| 38 | JPH::ShapeRefC JoltBoxShape3D::_build() const { |
| 39 | const float min_half_extent = (float)half_extents[half_extents.min_axis_index()]; |
| 40 | const float actual_margin = MIN(margin, min_half_extent * JoltProjectSettings::collision_margin_fraction); |
| 41 | |
| 42 | const JPH::BoxShapeSettings shape_settings(to_jolt(half_extents), actual_margin); |
| 43 | const JPH::ShapeSettings::ShapeResult shape_result = shape_settings.Create(); |
| 44 | |
| 45 | ERR_FAIL_COND_V_MSG(shape_result.HasError(), nullptr, vformat("Failed to build Jolt Physics box shape with %s. It returned the following error: '%s'. This shape belongs to %s.", to_string(), to_godot(shape_result.GetError()), _owners_to_string())); |
| 46 | |
| 47 | return shape_result.Get(); |
| 48 | } |
| 49 | |
| 50 | Variant JoltBoxShape3D::get_data() const { |
| 51 | return half_extents; |
| 52 | } |
| 53 | |
| 54 | void JoltBoxShape3D::set_data(const Variant &p_data) { |
| 55 | ERR_FAIL_COND(p_data.get_type() != Variant::VECTOR3); |
| 56 | |
| 57 | const Vector3 new_half_extents = p_data; |
| 58 | if (unlikely(new_half_extents == half_extents)) { |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | half_extents = new_half_extents; |
| 63 | |
| 64 | destroy(); |
| 65 | } |
| 66 | |
| 67 | void JoltBoxShape3D::set_margin(float p_margin) { |
| 68 | if (unlikely(margin == p_margin)) { |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | margin = p_margin; |
| 73 | |
| 74 | destroy(); |
| 75 | } |
| 76 | |
| 77 | String JoltBoxShape3D::to_string() const { |
| 78 | return vformat("{half_extents=%v margin=%f}", half_extents, margin); |
| 79 | } |
| 80 | |
| 81 | AABB JoltBoxShape3D::get_aabb() const { |
| 82 | return AABB(-half_extents, half_extents * 2.0f); |
| 83 | } |