5ffed49907
This PR and commit adds a new IK system for 3D with the Skeleton3D node that adds several new IK solvers, as well as additional changes and functionality for making bone manipulation in Godot easier. This work was sponsored by GSoC 2020 and TwistedTwigleg Full list of changes: * Adds a SkeletonModification3D resource * This resource is the base where all IK code is written and executed * Adds a SkeletonModificationStack3D resource * This node oversees the execution of the modifications and acts as a bridge of sorts for the modifications to the Skeleton3D node * Adds SkeletonModification3D resources for LookAt, CCDIK, FABRIK, Jiggle, and TwoBoneIK * Each modification is in it's own file * Several changes to Skeletons, listed below: * Added local_pose_override, which acts just like global_pose_override but keeps bone-child relationships intract * So if you move a bone using local_pose_override, all of the bones that are children will also be moved. This is different than global_pose_override, which only affects the individual bone * Internally bones keep track of their children. This removes the need of a processing list, makes it possible to update just a few select bones at a time, and makes it easier to traverse down the bone chain * Additional functions added for converting from world transform to global poses, global poses to local poses, and all the same changes but backwards (local to global, global to world). This makes it much easier to work with bone transforms without needing to think too much about how to convert them. * New signal added, bone_pose_changed, that can be used to tell if a specific bone changed its transform. Needed for BoneAttachment3D * Added functions for getting the forward position of a bone * BoneAttachment3D node refactored heavily * BoneAttachment3D node is now completely standalone in its functionality. * This makes the code easier and less interconnected, as well as allowing them to function properly without being direct children of Skeleton3D nodes * BoneAttachment3D now can be set either using the index or the bone name. * BoneAttachment3D nodes can now set the bone transform instead of just following it. This is disabled by default for compatibility * BoneAttachment3D now shows a warning when not configured correctly * Added rotate_to_align function in Basis * Added class reference documentation for all changes
170 lines
6.9 KiB
C++
170 lines
6.9 KiB
C++
/*************************************************************************/
|
|
/* skeleton_modification_3d.cpp */
|
|
/*************************************************************************/
|
|
/* This file is part of: */
|
|
/* GODOT ENGINE */
|
|
/* https://godotengine.org */
|
|
/*************************************************************************/
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
|
/* */
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
/* a copy of this software and associated documentation files (the */
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
/* the following conditions: */
|
|
/* */
|
|
/* The above copyright notice and this permission notice shall be */
|
|
/* included in all copies or substantial portions of the Software. */
|
|
/* */
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
/*************************************************************************/
|
|
|
|
#include "skeleton_modification_3d.h"
|
|
#include "scene/3d/skeleton_3d.h"
|
|
|
|
void SkeletonModification3D::_execute(real_t p_delta) {
|
|
if (get_script_instance()) {
|
|
if (get_script_instance()->has_method("execute")) {
|
|
get_script_instance()->call("execute", p_delta);
|
|
}
|
|
}
|
|
|
|
if (!enabled)
|
|
return;
|
|
}
|
|
|
|
void SkeletonModification3D::_setup_modification(SkeletonModificationStack3D *p_stack) {
|
|
stack = p_stack;
|
|
if (stack) {
|
|
is_setup = true;
|
|
} else {
|
|
WARN_PRINT("Could not setup modification with name " + this->get_name());
|
|
}
|
|
|
|
if (get_script_instance()) {
|
|
if (get_script_instance()->has_method("setup_modification")) {
|
|
get_script_instance()->call("setup_modification", p_stack);
|
|
}
|
|
}
|
|
}
|
|
|
|
void SkeletonModification3D::set_enabled(bool p_enabled) {
|
|
enabled = p_enabled;
|
|
}
|
|
|
|
bool SkeletonModification3D::get_enabled() {
|
|
return enabled;
|
|
}
|
|
|
|
// Helper function. Needed for CCDIK.
|
|
real_t SkeletonModification3D::clamp_angle(real_t p_angle, real_t p_min_bound, real_t p_max_bound, bool p_invert) {
|
|
// Map to the 0 to 360 range (in radians though) instead of the -180 to 180 range.
|
|
if (p_angle < 0) {
|
|
p_angle = Math_TAU + p_angle;
|
|
}
|
|
|
|
// Make min and max in the range of 0 to 360 (in radians), and make sure they are in the right order
|
|
if (p_min_bound < 0) {
|
|
p_min_bound = Math_TAU + p_min_bound;
|
|
}
|
|
if (p_max_bound < 0) {
|
|
p_max_bound = Math_TAU + p_max_bound;
|
|
}
|
|
if (p_min_bound > p_max_bound) {
|
|
real_t tmp = p_min_bound;
|
|
p_min_bound = p_max_bound;
|
|
p_max_bound = tmp;
|
|
}
|
|
|
|
// Note: May not be the most optimal way to clamp, but it always constraints to the nearest angle.
|
|
if (p_invert == false) {
|
|
if (p_angle < p_min_bound || p_angle > p_max_bound) {
|
|
Vector2 min_bound_vec = Vector2(Math::cos(p_min_bound), Math::sin(p_min_bound));
|
|
Vector2 max_bound_vec = Vector2(Math::cos(p_max_bound), Math::sin(p_max_bound));
|
|
Vector2 angle_vec = Vector2(Math::cos(p_angle), Math::sin(p_angle));
|
|
|
|
if (angle_vec.distance_squared_to(min_bound_vec) <= angle_vec.distance_squared_to(max_bound_vec)) {
|
|
p_angle = p_min_bound;
|
|
} else {
|
|
p_angle = p_max_bound;
|
|
}
|
|
}
|
|
} else {
|
|
if (p_angle > p_min_bound && p_angle < p_max_bound) {
|
|
Vector2 min_bound_vec = Vector2(Math::cos(p_min_bound), Math::sin(p_min_bound));
|
|
Vector2 max_bound_vec = Vector2(Math::cos(p_max_bound), Math::sin(p_max_bound));
|
|
Vector2 angle_vec = Vector2(Math::cos(p_angle), Math::sin(p_angle));
|
|
|
|
if (angle_vec.distance_squared_to(min_bound_vec) <= angle_vec.distance_squared_to(max_bound_vec)) {
|
|
p_angle = p_min_bound;
|
|
} else {
|
|
p_angle = p_max_bound;
|
|
}
|
|
}
|
|
}
|
|
return p_angle;
|
|
}
|
|
|
|
bool SkeletonModification3D::_print_execution_error(bool p_condition, String p_message) {
|
|
// If the modification is not setup, don't bother printing the error
|
|
if (!is_setup) {
|
|
return p_condition;
|
|
}
|
|
|
|
if (p_condition && !execution_error_found) {
|
|
ERR_PRINT(p_message);
|
|
execution_error_found = true;
|
|
}
|
|
return p_condition;
|
|
}
|
|
|
|
Ref<SkeletonModificationStack3D> SkeletonModification3D::get_modification_stack() {
|
|
return stack;
|
|
}
|
|
|
|
void SkeletonModification3D::set_is_setup(bool p_is_setup) {
|
|
is_setup = p_is_setup;
|
|
}
|
|
|
|
bool SkeletonModification3D::get_is_setup() const {
|
|
return is_setup;
|
|
}
|
|
|
|
void SkeletonModification3D::set_execution_mode(int p_mode) {
|
|
execution_mode = p_mode;
|
|
}
|
|
|
|
int SkeletonModification3D::get_execution_mode() const {
|
|
return execution_mode;
|
|
}
|
|
|
|
void SkeletonModification3D::_bind_methods() {
|
|
BIND_VMETHOD(MethodInfo("_execute", PropertyInfo(Variant::FLOAT, "delta")));
|
|
BIND_VMETHOD(MethodInfo("_setup_modification", PropertyInfo(Variant::OBJECT, "modification_stack", PROPERTY_HINT_RESOURCE_TYPE, "SkeletonModificationStack3D")));
|
|
|
|
ClassDB::bind_method(D_METHOD("set_enabled", "enabled"), &SkeletonModification3D::set_enabled);
|
|
ClassDB::bind_method(D_METHOD("get_enabled"), &SkeletonModification3D::get_enabled);
|
|
ClassDB::bind_method(D_METHOD("get_modification_stack"), &SkeletonModification3D::get_modification_stack);
|
|
ClassDB::bind_method(D_METHOD("set_is_setup", "is_setup"), &SkeletonModification3D::set_is_setup);
|
|
ClassDB::bind_method(D_METHOD("get_is_setup"), &SkeletonModification3D::get_is_setup);
|
|
ClassDB::bind_method(D_METHOD("set_execution_mode", "execution_mode"), &SkeletonModification3D::set_execution_mode);
|
|
ClassDB::bind_method(D_METHOD("get_execution_mode"), &SkeletonModification3D::get_execution_mode);
|
|
ClassDB::bind_method(D_METHOD("clamp_angle", "angle", "min", "max", "invert"), &SkeletonModification3D::clamp_angle);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "enabled"), "set_enabled", "get_enabled");
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "execution_mode", PROPERTY_HINT_ENUM, "process, physics_process"), "set_execution_mode", "get_execution_mode");
|
|
}
|
|
|
|
SkeletonModification3D::SkeletonModification3D() {
|
|
stack = nullptr;
|
|
is_setup = false;
|
|
}
|