| 1 | /**************************************************************************/ |
| 2 | /* spring_bone_collision_sphere_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 "spring_bone_collision_sphere_3d.h" |
| 32 | |
| 33 | void SpringBoneCollisionSphere3D::set_radius(float p_radius) { |
| 34 | radius = p_radius; |
| 35 | #ifdef TOOLS_ENABLED |
| 36 | update_gizmos(); |
| 37 | #endif // TOOLS_ENABLED |
| 38 | } |
| 39 | |
| 40 | float SpringBoneCollisionSphere3D::get_radius() const { |
| 41 | return radius; |
| 42 | } |
| 43 | |
| 44 | void SpringBoneCollisionSphere3D::set_inside(bool p_enabled) { |
| 45 | inside = p_enabled; |
| 46 | #ifdef TOOLS_ENABLED |
| 47 | update_gizmos(); |
| 48 | #endif // TOOLS_ENABLED |
| 49 | } |
| 50 | |
| 51 | bool SpringBoneCollisionSphere3D::is_inside() const { |
| 52 | return inside; |
| 53 | } |
| 54 | |
| 55 | void SpringBoneCollisionSphere3D::_bind_methods() { |
| 56 | ClassDB::bind_method(D_METHOD("set_radius", "radius"), &SpringBoneCollisionSphere3D::set_radius); |
| 57 | ClassDB::bind_method(D_METHOD("get_radius"), &SpringBoneCollisionSphere3D::get_radius); |
| 58 | ClassDB::bind_method(D_METHOD("set_inside", "enabled"), &SpringBoneCollisionSphere3D::set_inside); |
| 59 | ClassDB::bind_method(D_METHOD("is_inside"), &SpringBoneCollisionSphere3D::is_inside); |
| 60 | |
| 61 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0,1,0.001,or_greater,suffix:m"), "set_radius", "get_radius"); |
| 62 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "inside"), "set_inside", "is_inside"); |
| 63 | } |
| 64 | |
| 65 | Vector3 SpringBoneCollisionSphere3D::_collide_sphere(const Vector3 &p_origin, float p_radius, bool p_inside, float p_bone_radius, float p_bone_length, const Vector3 &p_current) { |
| 66 | Vector3 diff = p_current - p_origin; |
| 67 | float length = diff.length(); |
| 68 | float r = p_inside ? p_radius - p_bone_radius : p_bone_radius + p_radius; |
| 69 | float distance = p_inside ? r - length : length - r; |
| 70 | if (distance > 0) { |
| 71 | return p_current; |
| 72 | } |
| 73 | return p_origin + diff.normalized() * r; |
| 74 | } |
| 75 | |
| 76 | Vector3 SpringBoneCollisionSphere3D::_collide(const Transform3D &p_center, float p_bone_radius, float p_bone_length, const Vector3 &p_current) const { |
| 77 | return _collide_sphere(get_transform_from_skeleton(p_center).origin, radius, inside, p_bone_radius, p_bone_length, p_current); |
| 78 | } |