New and improved IK system for Skeleton2D
This PR and commit adds a new IK system for 2D with the Skeleton2D node
that adds several new IK solvers, a way to control bones in a Skeleton2D
node similar to that in Skeleton3D. It also adds additional changes
and functionality.
This work was sponsored by GSoC 2020 and TwistedTwigleg.
Full list of changes:
* Adds a SkeletonModifier2D resource
* This resource is the base where all IK code is written and executed
* Has a function for clamping angles, since it is so commonly used
* Modifiers are unique when duplicated so it works with instancing
* Adds a SkeletonModifierStack2D resource
* This resource manages a series of SkeletonModification2Ds
* This is what the Skeleton2D directly interfaces with to make IK possible
* Adds SkeletonModifier2D resources for LookAt, CCDIK, FABRIK, Jiggle, and TwoBoneIK
* Each modification is in its own file
* There is also a SkeletonModifier2D resource that acts as a stack for using multiple stacks together
* Adds a PhysicalBone2D node
* Works similar to the PhysicalBone3D node, but uses a RigidBody2D node
* Changes to Skeleton2D listed below:
* Skeleton2D now holds a single SkeletonModificationStack2D for IK
* Skeleton2D now has a local_pose_override, which overrides the Bone2D position similar to how the overrides work in Skeleton3D
* Changes to Bone2D listed below:
* The default_length property has been changed to length. Length is the length of the bone to its child bone node
* New bone_angle property, which is the angle the bone has to its first child bone node
* Bone2D caches its transform when not modified by IK for IK interpolation purposes
* Bone2D draws its own editor gizmo, though this is stated to change in the future
* Changes to CanvasItemEditor listed below:
* Bone2D gizmo drawing code removed
* The 2D IK code is removed. Now Bone2D is the only bone system for 2D
* Transform2D now has a looking_at function for rotating to face a position
* Two new node notifications: NOTIFICATION_EDITOR_PRE_SAVE and NOTIFICATION_EDITOR_POST_SAVE
* These notifications only are called in the editor right before and after saving a scene
* Needed for not saving the IK position when executing IK in the editor
* Documentation for all the changes listed above.
2020-08-03 18:02:24 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* skeleton_modification_2d_lookat.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2022-01-03 20:27:34 +00:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
New and improved IK system for Skeleton2D
This PR and commit adds a new IK system for 2D with the Skeleton2D node
that adds several new IK solvers, a way to control bones in a Skeleton2D
node similar to that in Skeleton3D. It also adds additional changes
and functionality.
This work was sponsored by GSoC 2020 and TwistedTwigleg.
Full list of changes:
* Adds a SkeletonModifier2D resource
* This resource is the base where all IK code is written and executed
* Has a function for clamping angles, since it is so commonly used
* Modifiers are unique when duplicated so it works with instancing
* Adds a SkeletonModifierStack2D resource
* This resource manages a series of SkeletonModification2Ds
* This is what the Skeleton2D directly interfaces with to make IK possible
* Adds SkeletonModifier2D resources for LookAt, CCDIK, FABRIK, Jiggle, and TwoBoneIK
* Each modification is in its own file
* There is also a SkeletonModifier2D resource that acts as a stack for using multiple stacks together
* Adds a PhysicalBone2D node
* Works similar to the PhysicalBone3D node, but uses a RigidBody2D node
* Changes to Skeleton2D listed below:
* Skeleton2D now holds a single SkeletonModificationStack2D for IK
* Skeleton2D now has a local_pose_override, which overrides the Bone2D position similar to how the overrides work in Skeleton3D
* Changes to Bone2D listed below:
* The default_length property has been changed to length. Length is the length of the bone to its child bone node
* New bone_angle property, which is the angle the bone has to its first child bone node
* Bone2D caches its transform when not modified by IK for IK interpolation purposes
* Bone2D draws its own editor gizmo, though this is stated to change in the future
* Changes to CanvasItemEditor listed below:
* Bone2D gizmo drawing code removed
* The 2D IK code is removed. Now Bone2D is the only bone system for 2D
* Transform2D now has a looking_at function for rotating to face a position
* Two new node notifications: NOTIFICATION_EDITOR_PRE_SAVE and NOTIFICATION_EDITOR_POST_SAVE
* These notifications only are called in the editor right before and after saving a scene
* Needed for not saving the IK position when executing IK in the editor
* Documentation for all the changes listed above.
2020-08-03 18:02:24 +00:00
|
|
|
/* */
|
|
|
|
/* 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_2d_lookat.h"
|
|
|
|
#include "scene/2d/skeleton_2d.h"
|
|
|
|
|
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
#include "editor/editor_settings.h"
|
|
|
|
#endif // TOOLS_ENABLED
|
|
|
|
|
|
|
|
bool SkeletonModification2DLookAt::_set(const StringName &p_path, const Variant &p_value) {
|
|
|
|
String path = p_path;
|
|
|
|
|
|
|
|
if (path.begins_with("enable_constraint")) {
|
|
|
|
set_enable_constraint(p_value);
|
|
|
|
} else if (path.begins_with("constraint_angle_min")) {
|
2022-08-13 15:45:42 +00:00
|
|
|
set_constraint_angle_min(Math::deg_to_rad(float(p_value)));
|
New and improved IK system for Skeleton2D
This PR and commit adds a new IK system for 2D with the Skeleton2D node
that adds several new IK solvers, a way to control bones in a Skeleton2D
node similar to that in Skeleton3D. It also adds additional changes
and functionality.
This work was sponsored by GSoC 2020 and TwistedTwigleg.
Full list of changes:
* Adds a SkeletonModifier2D resource
* This resource is the base where all IK code is written and executed
* Has a function for clamping angles, since it is so commonly used
* Modifiers are unique when duplicated so it works with instancing
* Adds a SkeletonModifierStack2D resource
* This resource manages a series of SkeletonModification2Ds
* This is what the Skeleton2D directly interfaces with to make IK possible
* Adds SkeletonModifier2D resources for LookAt, CCDIK, FABRIK, Jiggle, and TwoBoneIK
* Each modification is in its own file
* There is also a SkeletonModifier2D resource that acts as a stack for using multiple stacks together
* Adds a PhysicalBone2D node
* Works similar to the PhysicalBone3D node, but uses a RigidBody2D node
* Changes to Skeleton2D listed below:
* Skeleton2D now holds a single SkeletonModificationStack2D for IK
* Skeleton2D now has a local_pose_override, which overrides the Bone2D position similar to how the overrides work in Skeleton3D
* Changes to Bone2D listed below:
* The default_length property has been changed to length. Length is the length of the bone to its child bone node
* New bone_angle property, which is the angle the bone has to its first child bone node
* Bone2D caches its transform when not modified by IK for IK interpolation purposes
* Bone2D draws its own editor gizmo, though this is stated to change in the future
* Changes to CanvasItemEditor listed below:
* Bone2D gizmo drawing code removed
* The 2D IK code is removed. Now Bone2D is the only bone system for 2D
* Transform2D now has a looking_at function for rotating to face a position
* Two new node notifications: NOTIFICATION_EDITOR_PRE_SAVE and NOTIFICATION_EDITOR_POST_SAVE
* These notifications only are called in the editor right before and after saving a scene
* Needed for not saving the IK position when executing IK in the editor
* Documentation for all the changes listed above.
2020-08-03 18:02:24 +00:00
|
|
|
} else if (path.begins_with("constraint_angle_max")) {
|
2022-08-13 15:45:42 +00:00
|
|
|
set_constraint_angle_max(Math::deg_to_rad(float(p_value)));
|
New and improved IK system for Skeleton2D
This PR and commit adds a new IK system for 2D with the Skeleton2D node
that adds several new IK solvers, a way to control bones in a Skeleton2D
node similar to that in Skeleton3D. It also adds additional changes
and functionality.
This work was sponsored by GSoC 2020 and TwistedTwigleg.
Full list of changes:
* Adds a SkeletonModifier2D resource
* This resource is the base where all IK code is written and executed
* Has a function for clamping angles, since it is so commonly used
* Modifiers are unique when duplicated so it works with instancing
* Adds a SkeletonModifierStack2D resource
* This resource manages a series of SkeletonModification2Ds
* This is what the Skeleton2D directly interfaces with to make IK possible
* Adds SkeletonModifier2D resources for LookAt, CCDIK, FABRIK, Jiggle, and TwoBoneIK
* Each modification is in its own file
* There is also a SkeletonModifier2D resource that acts as a stack for using multiple stacks together
* Adds a PhysicalBone2D node
* Works similar to the PhysicalBone3D node, but uses a RigidBody2D node
* Changes to Skeleton2D listed below:
* Skeleton2D now holds a single SkeletonModificationStack2D for IK
* Skeleton2D now has a local_pose_override, which overrides the Bone2D position similar to how the overrides work in Skeleton3D
* Changes to Bone2D listed below:
* The default_length property has been changed to length. Length is the length of the bone to its child bone node
* New bone_angle property, which is the angle the bone has to its first child bone node
* Bone2D caches its transform when not modified by IK for IK interpolation purposes
* Bone2D draws its own editor gizmo, though this is stated to change in the future
* Changes to CanvasItemEditor listed below:
* Bone2D gizmo drawing code removed
* The 2D IK code is removed. Now Bone2D is the only bone system for 2D
* Transform2D now has a looking_at function for rotating to face a position
* Two new node notifications: NOTIFICATION_EDITOR_PRE_SAVE and NOTIFICATION_EDITOR_POST_SAVE
* These notifications only are called in the editor right before and after saving a scene
* Needed for not saving the IK position when executing IK in the editor
* Documentation for all the changes listed above.
2020-08-03 18:02:24 +00:00
|
|
|
} else if (path.begins_with("constraint_angle_invert")) {
|
|
|
|
set_constraint_angle_invert(p_value);
|
|
|
|
} else if (path.begins_with("constraint_in_localspace")) {
|
|
|
|
set_constraint_in_localspace(p_value);
|
|
|
|
} else if (path.begins_with("additional_rotation")) {
|
2022-08-13 15:45:42 +00:00
|
|
|
set_additional_rotation(Math::deg_to_rad(float(p_value)));
|
New and improved IK system for Skeleton2D
This PR and commit adds a new IK system for 2D with the Skeleton2D node
that adds several new IK solvers, a way to control bones in a Skeleton2D
node similar to that in Skeleton3D. It also adds additional changes
and functionality.
This work was sponsored by GSoC 2020 and TwistedTwigleg.
Full list of changes:
* Adds a SkeletonModifier2D resource
* This resource is the base where all IK code is written and executed
* Has a function for clamping angles, since it is so commonly used
* Modifiers are unique when duplicated so it works with instancing
* Adds a SkeletonModifierStack2D resource
* This resource manages a series of SkeletonModification2Ds
* This is what the Skeleton2D directly interfaces with to make IK possible
* Adds SkeletonModifier2D resources for LookAt, CCDIK, FABRIK, Jiggle, and TwoBoneIK
* Each modification is in its own file
* There is also a SkeletonModifier2D resource that acts as a stack for using multiple stacks together
* Adds a PhysicalBone2D node
* Works similar to the PhysicalBone3D node, but uses a RigidBody2D node
* Changes to Skeleton2D listed below:
* Skeleton2D now holds a single SkeletonModificationStack2D for IK
* Skeleton2D now has a local_pose_override, which overrides the Bone2D position similar to how the overrides work in Skeleton3D
* Changes to Bone2D listed below:
* The default_length property has been changed to length. Length is the length of the bone to its child bone node
* New bone_angle property, which is the angle the bone has to its first child bone node
* Bone2D caches its transform when not modified by IK for IK interpolation purposes
* Bone2D draws its own editor gizmo, though this is stated to change in the future
* Changes to CanvasItemEditor listed below:
* Bone2D gizmo drawing code removed
* The 2D IK code is removed. Now Bone2D is the only bone system for 2D
* Transform2D now has a looking_at function for rotating to face a position
* Two new node notifications: NOTIFICATION_EDITOR_PRE_SAVE and NOTIFICATION_EDITOR_POST_SAVE
* These notifications only are called in the editor right before and after saving a scene
* Needed for not saving the IK position when executing IK in the editor
* Documentation for all the changes listed above.
2020-08-03 18:02:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
if (path.begins_with("editor/draw_gizmo")) {
|
|
|
|
set_editor_draw_gizmo(p_value);
|
|
|
|
}
|
|
|
|
#endif // TOOLS_ENABLED
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SkeletonModification2DLookAt::_get(const StringName &p_path, Variant &r_ret) const {
|
|
|
|
String path = p_path;
|
|
|
|
|
|
|
|
if (path.begins_with("enable_constraint")) {
|
|
|
|
r_ret = get_enable_constraint();
|
|
|
|
} else if (path.begins_with("constraint_angle_min")) {
|
2022-08-13 15:45:42 +00:00
|
|
|
r_ret = Math::rad_to_deg(get_constraint_angle_min());
|
New and improved IK system for Skeleton2D
This PR and commit adds a new IK system for 2D with the Skeleton2D node
that adds several new IK solvers, a way to control bones in a Skeleton2D
node similar to that in Skeleton3D. It also adds additional changes
and functionality.
This work was sponsored by GSoC 2020 and TwistedTwigleg.
Full list of changes:
* Adds a SkeletonModifier2D resource
* This resource is the base where all IK code is written and executed
* Has a function for clamping angles, since it is so commonly used
* Modifiers are unique when duplicated so it works with instancing
* Adds a SkeletonModifierStack2D resource
* This resource manages a series of SkeletonModification2Ds
* This is what the Skeleton2D directly interfaces with to make IK possible
* Adds SkeletonModifier2D resources for LookAt, CCDIK, FABRIK, Jiggle, and TwoBoneIK
* Each modification is in its own file
* There is also a SkeletonModifier2D resource that acts as a stack for using multiple stacks together
* Adds a PhysicalBone2D node
* Works similar to the PhysicalBone3D node, but uses a RigidBody2D node
* Changes to Skeleton2D listed below:
* Skeleton2D now holds a single SkeletonModificationStack2D for IK
* Skeleton2D now has a local_pose_override, which overrides the Bone2D position similar to how the overrides work in Skeleton3D
* Changes to Bone2D listed below:
* The default_length property has been changed to length. Length is the length of the bone to its child bone node
* New bone_angle property, which is the angle the bone has to its first child bone node
* Bone2D caches its transform when not modified by IK for IK interpolation purposes
* Bone2D draws its own editor gizmo, though this is stated to change in the future
* Changes to CanvasItemEditor listed below:
* Bone2D gizmo drawing code removed
* The 2D IK code is removed. Now Bone2D is the only bone system for 2D
* Transform2D now has a looking_at function for rotating to face a position
* Two new node notifications: NOTIFICATION_EDITOR_PRE_SAVE and NOTIFICATION_EDITOR_POST_SAVE
* These notifications only are called in the editor right before and after saving a scene
* Needed for not saving the IK position when executing IK in the editor
* Documentation for all the changes listed above.
2020-08-03 18:02:24 +00:00
|
|
|
} else if (path.begins_with("constraint_angle_max")) {
|
2022-08-13 15:45:42 +00:00
|
|
|
r_ret = Math::rad_to_deg(get_constraint_angle_max());
|
New and improved IK system for Skeleton2D
This PR and commit adds a new IK system for 2D with the Skeleton2D node
that adds several new IK solvers, a way to control bones in a Skeleton2D
node similar to that in Skeleton3D. It also adds additional changes
and functionality.
This work was sponsored by GSoC 2020 and TwistedTwigleg.
Full list of changes:
* Adds a SkeletonModifier2D resource
* This resource is the base where all IK code is written and executed
* Has a function for clamping angles, since it is so commonly used
* Modifiers are unique when duplicated so it works with instancing
* Adds a SkeletonModifierStack2D resource
* This resource manages a series of SkeletonModification2Ds
* This is what the Skeleton2D directly interfaces with to make IK possible
* Adds SkeletonModifier2D resources for LookAt, CCDIK, FABRIK, Jiggle, and TwoBoneIK
* Each modification is in its own file
* There is also a SkeletonModifier2D resource that acts as a stack for using multiple stacks together
* Adds a PhysicalBone2D node
* Works similar to the PhysicalBone3D node, but uses a RigidBody2D node
* Changes to Skeleton2D listed below:
* Skeleton2D now holds a single SkeletonModificationStack2D for IK
* Skeleton2D now has a local_pose_override, which overrides the Bone2D position similar to how the overrides work in Skeleton3D
* Changes to Bone2D listed below:
* The default_length property has been changed to length. Length is the length of the bone to its child bone node
* New bone_angle property, which is the angle the bone has to its first child bone node
* Bone2D caches its transform when not modified by IK for IK interpolation purposes
* Bone2D draws its own editor gizmo, though this is stated to change in the future
* Changes to CanvasItemEditor listed below:
* Bone2D gizmo drawing code removed
* The 2D IK code is removed. Now Bone2D is the only bone system for 2D
* Transform2D now has a looking_at function for rotating to face a position
* Two new node notifications: NOTIFICATION_EDITOR_PRE_SAVE and NOTIFICATION_EDITOR_POST_SAVE
* These notifications only are called in the editor right before and after saving a scene
* Needed for not saving the IK position when executing IK in the editor
* Documentation for all the changes listed above.
2020-08-03 18:02:24 +00:00
|
|
|
} else if (path.begins_with("constraint_angle_invert")) {
|
|
|
|
r_ret = get_constraint_angle_invert();
|
|
|
|
} else if (path.begins_with("constraint_in_localspace")) {
|
|
|
|
r_ret = get_constraint_in_localspace();
|
|
|
|
} else if (path.begins_with("additional_rotation")) {
|
2022-08-13 15:45:42 +00:00
|
|
|
r_ret = Math::rad_to_deg(get_additional_rotation());
|
New and improved IK system for Skeleton2D
This PR and commit adds a new IK system for 2D with the Skeleton2D node
that adds several new IK solvers, a way to control bones in a Skeleton2D
node similar to that in Skeleton3D. It also adds additional changes
and functionality.
This work was sponsored by GSoC 2020 and TwistedTwigleg.
Full list of changes:
* Adds a SkeletonModifier2D resource
* This resource is the base where all IK code is written and executed
* Has a function for clamping angles, since it is so commonly used
* Modifiers are unique when duplicated so it works with instancing
* Adds a SkeletonModifierStack2D resource
* This resource manages a series of SkeletonModification2Ds
* This is what the Skeleton2D directly interfaces with to make IK possible
* Adds SkeletonModifier2D resources for LookAt, CCDIK, FABRIK, Jiggle, and TwoBoneIK
* Each modification is in its own file
* There is also a SkeletonModifier2D resource that acts as a stack for using multiple stacks together
* Adds a PhysicalBone2D node
* Works similar to the PhysicalBone3D node, but uses a RigidBody2D node
* Changes to Skeleton2D listed below:
* Skeleton2D now holds a single SkeletonModificationStack2D for IK
* Skeleton2D now has a local_pose_override, which overrides the Bone2D position similar to how the overrides work in Skeleton3D
* Changes to Bone2D listed below:
* The default_length property has been changed to length. Length is the length of the bone to its child bone node
* New bone_angle property, which is the angle the bone has to its first child bone node
* Bone2D caches its transform when not modified by IK for IK interpolation purposes
* Bone2D draws its own editor gizmo, though this is stated to change in the future
* Changes to CanvasItemEditor listed below:
* Bone2D gizmo drawing code removed
* The 2D IK code is removed. Now Bone2D is the only bone system for 2D
* Transform2D now has a looking_at function for rotating to face a position
* Two new node notifications: NOTIFICATION_EDITOR_PRE_SAVE and NOTIFICATION_EDITOR_POST_SAVE
* These notifications only are called in the editor right before and after saving a scene
* Needed for not saving the IK position when executing IK in the editor
* Documentation for all the changes listed above.
2020-08-03 18:02:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
if (path.begins_with("editor/draw_gizmo")) {
|
|
|
|
r_ret = get_editor_draw_gizmo();
|
|
|
|
}
|
|
|
|
#endif // TOOLS_ENABLED
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkeletonModification2DLookAt::_get_property_list(List<PropertyInfo> *p_list) const {
|
|
|
|
p_list->push_back(PropertyInfo(Variant::BOOL, "enable_constraint", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
|
|
|
|
if (enable_constraint) {
|
|
|
|
p_list->push_back(PropertyInfo(Variant::FLOAT, "constraint_angle_min", PROPERTY_HINT_RANGE, "-360, 360, 0.01", PROPERTY_USAGE_DEFAULT));
|
|
|
|
p_list->push_back(PropertyInfo(Variant::FLOAT, "constraint_angle_max", PROPERTY_HINT_RANGE, "-360, 360, 0.01", PROPERTY_USAGE_DEFAULT));
|
|
|
|
p_list->push_back(PropertyInfo(Variant::BOOL, "constraint_angle_invert", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
|
|
|
|
p_list->push_back(PropertyInfo(Variant::BOOL, "constraint_in_localspace", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
|
|
|
|
}
|
|
|
|
p_list->push_back(PropertyInfo(Variant::FLOAT, "additional_rotation", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
|
|
|
|
|
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
if (Engine::get_singleton()->is_editor_hint()) {
|
|
|
|
p_list->push_back(PropertyInfo(Variant::BOOL, "editor/draw_gizmo", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
|
|
|
|
}
|
|
|
|
#endif // TOOLS_ENABLED
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkeletonModification2DLookAt::_execute(float p_delta) {
|
|
|
|
ERR_FAIL_COND_MSG(!stack || !is_setup || stack->skeleton == nullptr,
|
|
|
|
"Modification is not setup and therefore cannot execute!");
|
|
|
|
if (!enabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (target_node_cache.is_null()) {
|
|
|
|
WARN_PRINT_ONCE("Target cache is out of date. Attempting to update...");
|
|
|
|
update_target_cache();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bone2d_node_cache.is_null() && !bone2d_node.is_empty()) {
|
|
|
|
update_bone2d_cache();
|
|
|
|
WARN_PRINT_ONCE("Bone2D node cache is out of date. Attempting to update...");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (target_node_reference == nullptr) {
|
|
|
|
target_node_reference = Object::cast_to<Node2D>(ObjectDB::get_instance(target_node_cache));
|
|
|
|
}
|
|
|
|
if (!target_node_reference || !target_node_reference->is_inside_tree()) {
|
|
|
|
ERR_PRINT_ONCE("Target node is not in the scene tree. Cannot execute modification!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (bone_idx <= -1) {
|
|
|
|
ERR_PRINT_ONCE("Bone index is invalid. Cannot execute modification!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Bone2D *operation_bone = stack->skeleton->get_bone(bone_idx);
|
|
|
|
if (operation_bone == nullptr) {
|
|
|
|
ERR_PRINT_ONCE("bone_idx for modification does not point to a valid bone! Cannot execute modification");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Transform2D operation_transform = operation_bone->get_global_transform();
|
|
|
|
Transform2D target_trans = target_node_reference->get_global_transform();
|
|
|
|
|
|
|
|
// Look at the target!
|
|
|
|
operation_transform = operation_transform.looking_at(target_trans.get_origin());
|
|
|
|
// Apply whatever scale it had prior to looking_at
|
2021-08-14 08:16:57 +00:00
|
|
|
operation_transform.set_scale(operation_bone->get_global_scale());
|
New and improved IK system for Skeleton2D
This PR and commit adds a new IK system for 2D with the Skeleton2D node
that adds several new IK solvers, a way to control bones in a Skeleton2D
node similar to that in Skeleton3D. It also adds additional changes
and functionality.
This work was sponsored by GSoC 2020 and TwistedTwigleg.
Full list of changes:
* Adds a SkeletonModifier2D resource
* This resource is the base where all IK code is written and executed
* Has a function for clamping angles, since it is so commonly used
* Modifiers are unique when duplicated so it works with instancing
* Adds a SkeletonModifierStack2D resource
* This resource manages a series of SkeletonModification2Ds
* This is what the Skeleton2D directly interfaces with to make IK possible
* Adds SkeletonModifier2D resources for LookAt, CCDIK, FABRIK, Jiggle, and TwoBoneIK
* Each modification is in its own file
* There is also a SkeletonModifier2D resource that acts as a stack for using multiple stacks together
* Adds a PhysicalBone2D node
* Works similar to the PhysicalBone3D node, but uses a RigidBody2D node
* Changes to Skeleton2D listed below:
* Skeleton2D now holds a single SkeletonModificationStack2D for IK
* Skeleton2D now has a local_pose_override, which overrides the Bone2D position similar to how the overrides work in Skeleton3D
* Changes to Bone2D listed below:
* The default_length property has been changed to length. Length is the length of the bone to its child bone node
* New bone_angle property, which is the angle the bone has to its first child bone node
* Bone2D caches its transform when not modified by IK for IK interpolation purposes
* Bone2D draws its own editor gizmo, though this is stated to change in the future
* Changes to CanvasItemEditor listed below:
* Bone2D gizmo drawing code removed
* The 2D IK code is removed. Now Bone2D is the only bone system for 2D
* Transform2D now has a looking_at function for rotating to face a position
* Two new node notifications: NOTIFICATION_EDITOR_PRE_SAVE and NOTIFICATION_EDITOR_POST_SAVE
* These notifications only are called in the editor right before and after saving a scene
* Needed for not saving the IK position when executing IK in the editor
* Documentation for all the changes listed above.
2020-08-03 18:02:24 +00:00
|
|
|
|
|
|
|
// Account for the direction the bone faces in:
|
|
|
|
operation_transform.set_rotation(operation_transform.get_rotation() - operation_bone->get_bone_angle());
|
|
|
|
|
|
|
|
// Apply additional rotation
|
|
|
|
operation_transform.set_rotation(operation_transform.get_rotation() + additional_rotation);
|
|
|
|
|
|
|
|
// Apply constraints in globalspace:
|
|
|
|
if (enable_constraint && !constraint_in_localspace) {
|
|
|
|
operation_transform.set_rotation(clamp_angle(operation_transform.get_rotation(), constraint_angle_min, constraint_angle_max, constraint_angle_invert));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert from a global transform to a local transform via the Bone2D node
|
|
|
|
operation_bone->set_global_transform(operation_transform);
|
|
|
|
operation_transform = operation_bone->get_transform();
|
|
|
|
|
|
|
|
// Apply constraints in localspace:
|
|
|
|
if (enable_constraint && constraint_in_localspace) {
|
|
|
|
operation_transform.set_rotation(clamp_angle(operation_transform.get_rotation(), constraint_angle_min, constraint_angle_max, constraint_angle_invert));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the local pose override, and to make sure child bones are also updated, set the transform of the bone.
|
|
|
|
stack->skeleton->set_bone_local_pose_override(bone_idx, operation_transform, stack->strength, true);
|
|
|
|
operation_bone->set_transform(operation_transform);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkeletonModification2DLookAt::_setup_modification(SkeletonModificationStack2D *p_stack) {
|
|
|
|
stack = p_stack;
|
|
|
|
|
|
|
|
if (stack != nullptr) {
|
|
|
|
is_setup = true;
|
|
|
|
update_target_cache();
|
|
|
|
update_bone2d_cache();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkeletonModification2DLookAt::_draw_editor_gizmo() {
|
|
|
|
if (!enabled || !is_setup) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Bone2D *operation_bone = stack->skeleton->get_bone(bone_idx);
|
|
|
|
editor_draw_angle_constraints(operation_bone, constraint_angle_min, constraint_angle_max,
|
|
|
|
enable_constraint, constraint_in_localspace, constraint_angle_invert);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkeletonModification2DLookAt::update_bone2d_cache() {
|
|
|
|
if (!is_setup || !stack) {
|
|
|
|
ERR_PRINT_ONCE("Cannot update Bone2D cache: modification is not properly setup!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bone2d_node_cache = ObjectID();
|
|
|
|
if (stack->skeleton) {
|
|
|
|
if (stack->skeleton->is_inside_tree()) {
|
|
|
|
if (stack->skeleton->has_node(bone2d_node)) {
|
|
|
|
Node *node = stack->skeleton->get_node(bone2d_node);
|
|
|
|
ERR_FAIL_COND_MSG(!node || stack->skeleton == node,
|
|
|
|
"Cannot update Bone2D cache: node is this modification's skeleton or cannot be found!");
|
|
|
|
ERR_FAIL_COND_MSG(!node->is_inside_tree(),
|
|
|
|
"Cannot update Bone2D cache: node is not in the scene tree!");
|
|
|
|
bone2d_node_cache = node->get_instance_id();
|
|
|
|
|
|
|
|
Bone2D *bone = Object::cast_to<Bone2D>(node);
|
|
|
|
if (bone) {
|
|
|
|
bone_idx = bone->get_index_in_skeleton();
|
|
|
|
} else {
|
|
|
|
ERR_FAIL_MSG("Error Bone2D cache: Nodepath to Bone2D is not a Bone2D node!");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set this to null so we update it
|
|
|
|
target_node_reference = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkeletonModification2DLookAt::set_bone2d_node(const NodePath &p_target_node) {
|
|
|
|
bone2d_node = p_target_node;
|
|
|
|
update_bone2d_cache();
|
|
|
|
}
|
|
|
|
|
|
|
|
NodePath SkeletonModification2DLookAt::get_bone2d_node() const {
|
|
|
|
return bone2d_node;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SkeletonModification2DLookAt::get_bone_index() const {
|
|
|
|
return bone_idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkeletonModification2DLookAt::set_bone_index(int p_bone_idx) {
|
|
|
|
ERR_FAIL_COND_MSG(p_bone_idx < 0, "Bone index is out of range: The index is too low!");
|
|
|
|
|
2021-10-27 23:57:41 +00:00
|
|
|
if (is_setup && stack) {
|
New and improved IK system for Skeleton2D
This PR and commit adds a new IK system for 2D with the Skeleton2D node
that adds several new IK solvers, a way to control bones in a Skeleton2D
node similar to that in Skeleton3D. It also adds additional changes
and functionality.
This work was sponsored by GSoC 2020 and TwistedTwigleg.
Full list of changes:
* Adds a SkeletonModifier2D resource
* This resource is the base where all IK code is written and executed
* Has a function for clamping angles, since it is so commonly used
* Modifiers are unique when duplicated so it works with instancing
* Adds a SkeletonModifierStack2D resource
* This resource manages a series of SkeletonModification2Ds
* This is what the Skeleton2D directly interfaces with to make IK possible
* Adds SkeletonModifier2D resources for LookAt, CCDIK, FABRIK, Jiggle, and TwoBoneIK
* Each modification is in its own file
* There is also a SkeletonModifier2D resource that acts as a stack for using multiple stacks together
* Adds a PhysicalBone2D node
* Works similar to the PhysicalBone3D node, but uses a RigidBody2D node
* Changes to Skeleton2D listed below:
* Skeleton2D now holds a single SkeletonModificationStack2D for IK
* Skeleton2D now has a local_pose_override, which overrides the Bone2D position similar to how the overrides work in Skeleton3D
* Changes to Bone2D listed below:
* The default_length property has been changed to length. Length is the length of the bone to its child bone node
* New bone_angle property, which is the angle the bone has to its first child bone node
* Bone2D caches its transform when not modified by IK for IK interpolation purposes
* Bone2D draws its own editor gizmo, though this is stated to change in the future
* Changes to CanvasItemEditor listed below:
* Bone2D gizmo drawing code removed
* The 2D IK code is removed. Now Bone2D is the only bone system for 2D
* Transform2D now has a looking_at function for rotating to face a position
* Two new node notifications: NOTIFICATION_EDITOR_PRE_SAVE and NOTIFICATION_EDITOR_POST_SAVE
* These notifications only are called in the editor right before and after saving a scene
* Needed for not saving the IK position when executing IK in the editor
* Documentation for all the changes listed above.
2020-08-03 18:02:24 +00:00
|
|
|
if (stack->skeleton) {
|
|
|
|
ERR_FAIL_INDEX_MSG(p_bone_idx, stack->skeleton->get_bone_count(), "Passed-in Bone index is out of range!");
|
|
|
|
bone_idx = p_bone_idx;
|
|
|
|
bone2d_node_cache = stack->skeleton->get_bone(p_bone_idx)->get_instance_id();
|
|
|
|
bone2d_node = stack->skeleton->get_path_to(stack->skeleton->get_bone(p_bone_idx));
|
|
|
|
} else {
|
|
|
|
WARN_PRINT("Cannot verify the bone index for this modification...");
|
|
|
|
bone_idx = p_bone_idx;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
WARN_PRINT("Cannot verify the bone index for this modification...");
|
|
|
|
bone_idx = p_bone_idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
notify_property_list_changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkeletonModification2DLookAt::update_target_cache() {
|
|
|
|
if (!is_setup || !stack) {
|
|
|
|
ERR_PRINT_ONCE("Cannot update target cache: modification is not properly setup!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
target_node_cache = ObjectID();
|
|
|
|
if (stack->skeleton) {
|
|
|
|
if (stack->skeleton->is_inside_tree()) {
|
|
|
|
if (stack->skeleton->has_node(target_node)) {
|
|
|
|
Node *node = stack->skeleton->get_node(target_node);
|
|
|
|
ERR_FAIL_COND_MSG(!node || stack->skeleton == node,
|
|
|
|
"Cannot update target cache: node is this modification's skeleton or cannot be found!");
|
|
|
|
ERR_FAIL_COND_MSG(!node->is_inside_tree(),
|
|
|
|
"Cannot update target cache: node is not in the scene tree!");
|
|
|
|
target_node_cache = node->get_instance_id();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkeletonModification2DLookAt::set_target_node(const NodePath &p_target_node) {
|
|
|
|
target_node = p_target_node;
|
|
|
|
update_target_cache();
|
|
|
|
}
|
|
|
|
|
|
|
|
NodePath SkeletonModification2DLookAt::get_target_node() const {
|
|
|
|
return target_node;
|
|
|
|
}
|
|
|
|
|
|
|
|
float SkeletonModification2DLookAt::get_additional_rotation() const {
|
|
|
|
return additional_rotation;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkeletonModification2DLookAt::set_additional_rotation(float p_rotation) {
|
|
|
|
additional_rotation = p_rotation;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkeletonModification2DLookAt::set_enable_constraint(bool p_constraint) {
|
|
|
|
enable_constraint = p_constraint;
|
|
|
|
notify_property_list_changed();
|
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
if (stack && is_setup) {
|
|
|
|
stack->set_editor_gizmos_dirty(true);
|
|
|
|
}
|
|
|
|
#endif // TOOLS_ENABLED
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SkeletonModification2DLookAt::get_enable_constraint() const {
|
|
|
|
return enable_constraint;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkeletonModification2DLookAt::set_constraint_angle_min(float p_angle_min) {
|
|
|
|
constraint_angle_min = p_angle_min;
|
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
if (stack && is_setup) {
|
|
|
|
stack->set_editor_gizmos_dirty(true);
|
|
|
|
}
|
|
|
|
#endif // TOOLS_ENABLED
|
|
|
|
}
|
|
|
|
|
|
|
|
float SkeletonModification2DLookAt::get_constraint_angle_min() const {
|
|
|
|
return constraint_angle_min;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkeletonModification2DLookAt::set_constraint_angle_max(float p_angle_max) {
|
|
|
|
constraint_angle_max = p_angle_max;
|
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
if (stack && is_setup) {
|
|
|
|
stack->set_editor_gizmos_dirty(true);
|
|
|
|
}
|
|
|
|
#endif // TOOLS_ENABLED
|
|
|
|
}
|
|
|
|
|
|
|
|
float SkeletonModification2DLookAt::get_constraint_angle_max() const {
|
|
|
|
return constraint_angle_max;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkeletonModification2DLookAt::set_constraint_angle_invert(bool p_invert) {
|
|
|
|
constraint_angle_invert = p_invert;
|
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
if (stack && is_setup) {
|
|
|
|
stack->set_editor_gizmos_dirty(true);
|
|
|
|
}
|
|
|
|
#endif // TOOLS_ENABLED
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SkeletonModification2DLookAt::get_constraint_angle_invert() const {
|
|
|
|
return constraint_angle_invert;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkeletonModification2DLookAt::set_constraint_in_localspace(bool p_constraint_in_localspace) {
|
|
|
|
constraint_in_localspace = p_constraint_in_localspace;
|
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
if (stack && is_setup) {
|
|
|
|
stack->set_editor_gizmos_dirty(true);
|
|
|
|
}
|
|
|
|
#endif // TOOLS_ENABLED
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SkeletonModification2DLookAt::get_constraint_in_localspace() const {
|
|
|
|
return constraint_in_localspace;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SkeletonModification2DLookAt::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("set_bone2d_node", "bone2d_nodepath"), &SkeletonModification2DLookAt::set_bone2d_node);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_bone2d_node"), &SkeletonModification2DLookAt::get_bone2d_node);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_bone_index", "bone_idx"), &SkeletonModification2DLookAt::set_bone_index);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_bone_index"), &SkeletonModification2DLookAt::get_bone_index);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_target_node", "target_nodepath"), &SkeletonModification2DLookAt::set_target_node);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_target_node"), &SkeletonModification2DLookAt::get_target_node);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_additional_rotation", "rotation"), &SkeletonModification2DLookAt::set_additional_rotation);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_additional_rotation"), &SkeletonModification2DLookAt::get_additional_rotation);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_enable_constraint", "enable_constraint"), &SkeletonModification2DLookAt::set_enable_constraint);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_enable_constraint"), &SkeletonModification2DLookAt::get_enable_constraint);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_constraint_angle_min", "angle_min"), &SkeletonModification2DLookAt::set_constraint_angle_min);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_constraint_angle_min"), &SkeletonModification2DLookAt::get_constraint_angle_min);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_constraint_angle_max", "angle_max"), &SkeletonModification2DLookAt::set_constraint_angle_max);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_constraint_angle_max"), &SkeletonModification2DLookAt::get_constraint_angle_max);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_constraint_angle_invert", "invert"), &SkeletonModification2DLookAt::set_constraint_angle_invert);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_constraint_angle_invert"), &SkeletonModification2DLookAt::get_constraint_angle_invert);
|
|
|
|
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "bone_index"), "set_bone_index", "get_bone_index");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "bone2d_node", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Bone2D"), "set_bone2d_node", "get_bone2d_node");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "target_nodepath", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Node2D"), "set_target_node", "get_target_node");
|
|
|
|
}
|
|
|
|
|
|
|
|
SkeletonModification2DLookAt::SkeletonModification2DLookAt() {
|
|
|
|
stack = nullptr;
|
|
|
|
is_setup = false;
|
|
|
|
bone_idx = -1;
|
|
|
|
additional_rotation = 0;
|
|
|
|
enable_constraint = false;
|
|
|
|
constraint_angle_min = 0;
|
|
|
|
constraint_angle_max = Math_PI * 2;
|
|
|
|
constraint_angle_invert = false;
|
|
|
|
enabled = true;
|
|
|
|
|
|
|
|
editor_draw_gizmo = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkeletonModification2DLookAt::~SkeletonModification2DLookAt() {
|
|
|
|
}
|