| 1 | /**************************************************************************/ |
| 2 | /* spring_bone_collision_capsule_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_capsule_3d.h" |
| 32 | |
| 33 | #include "scene/3d/spring_bone_collision_sphere_3d.h" |
| 34 | |
| 35 | void SpringBoneCollisionCapsule3D::set_radius(float p_radius) { |
| 36 | radius = p_radius; |
| 37 | if (radius > height * 0.5) { |
| 38 | height = radius * 2.0; |
| 39 | } |
| 40 | #ifdef TOOLS_ENABLED |
| 41 | update_gizmos(); |
| 42 | #endif // TOOLS_ENABLED |
| 43 | } |
| 44 | |
| 45 | float SpringBoneCollisionCapsule3D::get_radius() const { |
| 46 | return radius; |
| 47 | } |
| 48 | |
| 49 | void SpringBoneCollisionCapsule3D::set_height(float p_height) { |
| 50 | height = p_height; |
| 51 | if (radius > height * 0.5) { |
| 52 | radius = height * 0.5; |
| 53 | } |
| 54 | #ifdef TOOLS_ENABLED |
| 55 | update_gizmos(); |
| 56 | #endif // TOOLS_ENABLED |
| 57 | } |
| 58 | |
| 59 | float SpringBoneCollisionCapsule3D::get_height() const { |
| 60 | return height; |
| 61 | } |
| 62 | |
| 63 | void SpringBoneCollisionCapsule3D::set_mid_height(real_t p_mid_height) { |
| 64 | ERR_FAIL_COND_MSG(p_mid_height < 0.0f, "SpringBoneCollisionCapsule3D mid-height cannot be negative."); |
| 65 | height = p_mid_height + radius * 2.0f; |
| 66 | #ifdef TOOLS_ENABLED |
| 67 | update_gizmos(); |
| 68 | #endif // TOOLS_ENABLED |
| 69 | } |
| 70 | |
| 71 | real_t SpringBoneCollisionCapsule3D::get_mid_height() const { |
| 72 | return height - radius * 2.0f; |
| 73 | } |
| 74 | |
| 75 | void SpringBoneCollisionCapsule3D::set_inside(bool p_enabled) { |
| 76 | inside = p_enabled; |
| 77 | #ifdef TOOLS_ENABLED |
| 78 | update_gizmos(); |
| 79 | #endif // TOOLS_ENABLED |
| 80 | } |
| 81 | |
| 82 | bool SpringBoneCollisionCapsule3D::is_inside() const { |
| 83 | return inside; |
| 84 | } |
| 85 | |
| 86 | Pair<Vector3, Vector3> SpringBoneCollisionCapsule3D::get_head_and_tail(const Transform3D &p_center) const { |
| 87 | Transform3D tr = get_transform_from_skeleton(p_center); |
| 88 | return Pair<Vector3, Vector3>(tr.origin + tr.basis.xform(Vector3::UP * (height * 0.5 - radius)), tr.origin + tr.basis.xform(Vector3::DOWN * (height * 0.5 - radius))); |
| 89 | } |
| 90 | |
| 91 | void SpringBoneCollisionCapsule3D::_bind_methods() { |
| 92 | ClassDB::bind_method(D_METHOD("set_radius", "radius"), &SpringBoneCollisionCapsule3D::set_radius); |
| 93 | ClassDB::bind_method(D_METHOD("get_radius"), &SpringBoneCollisionCapsule3D::get_radius); |
| 94 | ClassDB::bind_method(D_METHOD("set_height", "height"), &SpringBoneCollisionCapsule3D::set_height); |
| 95 | ClassDB::bind_method(D_METHOD("get_height"), &SpringBoneCollisionCapsule3D::get_height); |
| 96 | ClassDB::bind_method(D_METHOD("set_mid_height", "mid_height"), &SpringBoneCollisionCapsule3D::set_mid_height); |
| 97 | ClassDB::bind_method(D_METHOD("get_mid_height"), &SpringBoneCollisionCapsule3D::get_mid_height); |
| 98 | ClassDB::bind_method(D_METHOD("set_inside", "enabled"), &SpringBoneCollisionCapsule3D::set_inside); |
| 99 | ClassDB::bind_method(D_METHOD("is_inside"), &SpringBoneCollisionCapsule3D::is_inside); |
| 100 | |
| 101 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0,1,0.001,or_greater,suffix:m"), "set_radius", "get_radius"); |
| 102 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height", PROPERTY_HINT_RANGE, "0,1,0.001,or_greater,suffix:m"), "set_height", "get_height"); |
| 103 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "mid_height", PROPERTY_HINT_RANGE, "0,1,0.001,or_greater,suffix:m", PROPERTY_USAGE_NONE), "set_mid_height", "get_mid_height"); |
| 104 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "inside"), "set_inside", "is_inside"); |
| 105 | } |
| 106 | |
| 107 | Vector3 SpringBoneCollisionCapsule3D::_collide(const Transform3D &p_center, float p_bone_radius, float p_bone_length, const Vector3 &p_current) const { |
| 108 | Pair<Vector3, Vector3> head_tail = get_head_and_tail(p_center); |
| 109 | Vector3 head = head_tail.first; |
| 110 | Vector3 tail = head_tail.second; |
| 111 | Vector3 p = tail - head; |
| 112 | Vector3 q = p_current - head; |
| 113 | float dot = p.dot(q); |
| 114 | if (dot <= 0) { |
| 115 | return SpringBoneCollisionSphere3D::_collide_sphere(head, radius, inside, p_bone_radius, p_bone_length, p_current); |
| 116 | } |
| 117 | float pls = p.length_squared(); |
| 118 | if (Math::is_zero_approx(pls)) { |
| 119 | return p_current; |
| 120 | } |
| 121 | if (pls <= dot) { |
| 122 | return SpringBoneCollisionSphere3D::_collide_sphere(head + p, radius, inside, p_bone_radius, p_bone_length, p_current); |
| 123 | } |
| 124 | return SpringBoneCollisionSphere3D::_collide_sphere(head + p * (dot / pls), radius, inside, p_bone_radius, p_bone_length, p_current); |
| 125 | } |