| 1 | /**************************************************************************/ |
| 2 | /* jolt_separation_ray_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_separation_ray_shape_3d.h" |
| 32 | |
| 33 | #include "../misc/jolt_type_conversions.h" |
| 34 | #include "jolt_custom_ray_shape.h" |
| 35 | |
| 36 | JPH::ShapeRefC JoltSeparationRayShape3D::_build() const { |
| 37 | ERR_FAIL_COND_V_MSG(length <= 0.0f, nullptr, vformat("Failed to build Jolt Physics separation ray shape with %s. Its length must be greater than 0. This shape belongs to %s.", to_string(), _owners_to_string())); |
| 38 | |
| 39 | const JoltCustomRayShapeSettings shape_settings(length, slide_on_slope); |
| 40 | const JPH::ShapeSettings::ShapeResult shape_result = shape_settings.Create(); |
| 41 | ERR_FAIL_COND_V_MSG(shape_result.HasError(), nullptr, vformat("Failed to build Jolt Physics separation ray shape with %s. It returned the following error: '%s'. This shape belongs to %s.", to_string(), to_godot(shape_result.GetError()), _owners_to_string())); |
| 42 | |
| 43 | return shape_result.Get(); |
| 44 | } |
| 45 | |
| 46 | Variant JoltSeparationRayShape3D::get_data() const { |
| 47 | Dictionary data; |
| 48 | data["length"] = length; |
| 49 | data["slide_on_slope"] = slide_on_slope; |
| 50 | return data; |
| 51 | } |
| 52 | |
| 53 | void JoltSeparationRayShape3D::set_data(const Variant &p_data) { |
| 54 | ERR_FAIL_COND(p_data.get_type() != Variant::DICTIONARY); |
| 55 | |
| 56 | const Dictionary data = p_data; |
| 57 | |
| 58 | const Variant maybe_length = data.get("length", Variant()); |
| 59 | ERR_FAIL_COND(maybe_length.get_type() != Variant::FLOAT); |
| 60 | |
| 61 | const Variant maybe_slide_on_slope = data.get("slide_on_slope", Variant()); |
| 62 | ERR_FAIL_COND(maybe_slide_on_slope.get_type() != Variant::BOOL); |
| 63 | |
| 64 | const float new_length = maybe_length; |
| 65 | const bool new_slide_on_slope = maybe_slide_on_slope; |
| 66 | |
| 67 | if (unlikely(new_length == length && new_slide_on_slope == slide_on_slope)) { |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | length = new_length; |
| 72 | slide_on_slope = new_slide_on_slope; |
| 73 | |
| 74 | destroy(); |
| 75 | } |
| 76 | |
| 77 | AABB JoltSeparationRayShape3D::get_aabb() const { |
| 78 | constexpr float size_xy = 0.1f; |
| 79 | constexpr float half_size_xy = size_xy / 2.0f; |
| 80 | return AABB(Vector3(-half_size_xy, -half_size_xy, 0.0f), Vector3(size_xy, size_xy, length)); |
| 81 | } |
| 82 | |
| 83 | String JoltSeparationRayShape3D::to_string() const { |
| 84 | return vformat("{length=%f slide_on_slope=%s}", length, slide_on_slope); |
| 85 | } |