| 1 | /**************************************************************************/ |
| 2 | /* joint_limitation_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 "joint_limitation_3d.h" |
| 32 | |
| 33 | #include "scene/3d/skeleton_modifier_3d.h" |
| 34 | |
| 35 | Quaternion JointLimitation3D::make_space(const Vector3 &p_local_forward_vector, const Vector3 &p_local_right_vector, const Quaternion &p_rotation_offset) const { |
| 36 | const double ALMOST_ONE = 1.0 - CMP_EPSILON; |
| 37 | // The default is to interpret the forward vector as the +Y axis. |
| 38 | Vector3 axis_y = p_local_forward_vector.normalized(); |
| 39 | Vector3 axis_x = p_local_right_vector.normalized(); |
| 40 | if (axis_x.is_zero_approx() || Math::abs(axis_x.dot(axis_y)) > ALMOST_ONE) { |
| 41 | return (Quaternion(Vector3(0, 1, 0), axis_y) * p_rotation_offset.normalized()).normalized(); |
| 42 | } |
| 43 | // Prior X axis. |
| 44 | Vector3 axis_z = axis_x.cross(axis_y); |
| 45 | axis_z.normalize(); |
| 46 | axis_x = axis_y.cross(axis_z); |
| 47 | axis_x.normalize(); |
| 48 | return (Basis(axis_x, axis_y, axis_z).get_rotation_quaternion() * p_rotation_offset.normalized()).normalized(); |
| 49 | } |
| 50 | |
| 51 | Vector3 JointLimitation3D::_solve(const Vector3 &p_direction) const { |
| 52 | return p_direction; |
| 53 | } |
| 54 | |
| 55 | Vector3 JointLimitation3D::solve(const Vector3 &p_local_forward_vector, const Vector3 &p_local_right_vector, const Quaternion &p_rotation_offset, const Vector3 &p_local_current_vector) const { |
| 56 | Quaternion space = make_space(p_local_forward_vector, p_local_right_vector, p_rotation_offset); |
| 57 | Vector3 dir = p_local_current_vector.normalized(); |
| 58 | return space.xform(_solve(space.xform_inv(dir))); |
| 59 | } |
| 60 | |
| 61 | #ifdef TOOLS_ENABLED |
| 62 | void JointLimitation3D::draw_shape(Ref<SurfaceTool> &p_surface_tool, const Transform3D &p_transform, float p_bone_length, const Color &p_color) const { |
| 63 | // |
| 64 | } |
| 65 | #endif // TOOLS_ENABLED |