| 1 | /**************************************************************************/ |
| 2 | /* jolt_capsule_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_capsule_shape_3d.h" |
| 32 | |
| 33 | #include "../misc/jolt_type_conversions.h" |
| 34 | |
| 35 | #include "Jolt/Physics/Collision/Shape/CapsuleShape.h" |
| 36 | |
| 37 | JPH::ShapeRefC JoltCapsuleShape3D::_build() const { |
| 38 | ERR_FAIL_COND_V_MSG(radius <= 0.0f, nullptr, vformat("Failed to build Jolt Physics capsule shape with %s. Its radius must be greater than 0. This shape belongs to %s.", to_string(), _owners_to_string())); |
| 39 | ERR_FAIL_COND_V_MSG(height <= 0.0f, nullptr, vformat("Failed to build Jolt Physics capsule shape with %s. Its height must be greater than 0. This shape belongs to %s.", to_string(), _owners_to_string())); |
| 40 | ERR_FAIL_COND_V_MSG(height < radius * 2.0f, nullptr, vformat("Failed to build Jolt Physics capsule shape with %s. Its height must be at least double that of its radius. This shape belongs to %s.", to_string(), _owners_to_string())); |
| 41 | |
| 42 | const float half_height = height / 2.0f; |
| 43 | const float cylinder_height = half_height - radius; |
| 44 | |
| 45 | const JPH::CapsuleShapeSettings shape_settings(cylinder_height, radius); |
| 46 | const JPH::ShapeSettings::ShapeResult shape_result = shape_settings.Create(); |
| 47 | ERR_FAIL_COND_V_MSG(shape_result.HasError(), nullptr, vformat("Failed to build Jolt Physics capsule shape with %s. It returned the following error: '%s'. This shape belongs to %s.", to_string(), to_godot(shape_result.GetError()), _owners_to_string())); |
| 48 | |
| 49 | return shape_result.Get(); |
| 50 | } |
| 51 | |
| 52 | Variant JoltCapsuleShape3D::get_data() const { |
| 53 | Dictionary data; |
| 54 | data["height"] = height; |
| 55 | data["radius"] = radius; |
| 56 | return data; |
| 57 | } |
| 58 | |
| 59 | void JoltCapsuleShape3D::set_data(const Variant &p_data) { |
| 60 | ERR_FAIL_COND(p_data.get_type() != Variant::DICTIONARY); |
| 61 | |
| 62 | const Dictionary data = p_data; |
| 63 | |
| 64 | const Variant maybe_height = data.get("height", Variant()); |
| 65 | ERR_FAIL_COND(maybe_height.get_type() != Variant::FLOAT); |
| 66 | |
| 67 | const Variant maybe_radius = data.get("radius", Variant()); |
| 68 | ERR_FAIL_COND(maybe_radius.get_type() != Variant::FLOAT); |
| 69 | |
| 70 | const float new_height = maybe_height; |
| 71 | const float new_radius = maybe_radius; |
| 72 | |
| 73 | if (unlikely(new_height == height && new_radius == radius)) { |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | height = new_height; |
| 78 | radius = new_radius; |
| 79 | |
| 80 | destroy(); |
| 81 | } |
| 82 | |
| 83 | AABB JoltCapsuleShape3D::get_aabb() const { |
| 84 | const Vector3 half_extents(radius, height / 2.0f, radius); |
| 85 | return AABB(-half_extents, half_extents * 2.0f); |
| 86 | } |
| 87 | |
| 88 | String JoltCapsuleShape3D::to_string() const { |
| 89 | return vformat("{height=%f radius=%f}", height, radius); |
| 90 | } |