2016-03-29 21:52:31 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* ik.cpp */
|
2016-06-18 14:46:12 +02:00
|
|
|
/* Copyright (c) 2016 Sergey Lapin <slapinid@gmail.com> */
|
2016-03-29 21:52:31 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* 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 "ik.h"
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
bool InverseKinematics::_get(const StringName &p_name, Variant &r_ret) const {
|
2016-03-29 21:52:31 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (String(p_name) == "ik_bone") {
|
2016-03-29 21:52:31 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
r_ret = get_bone_name();
|
2016-03-29 21:52:31 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
bool InverseKinematics::_set(const StringName &p_name, const Variant &p_value) {
|
2016-03-29 21:52:31 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (String(p_name) == "ik_bone") {
|
2016-03-29 21:52:31 +02:00
|
|
|
|
|
|
|
set_bone_name(p_value);
|
2016-03-30 15:55:54 +02:00
|
|
|
changed = true;
|
2016-03-29 21:52:31 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void InverseKinematics::_get_property_list(List<PropertyInfo> *p_list) const {
|
2016-03-29 21:52:31 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Skeleton *parent = NULL;
|
|
|
|
if (get_parent())
|
|
|
|
parent = get_parent()->cast_to<Skeleton>();
|
2016-03-29 21:52:31 +02:00
|
|
|
|
|
|
|
if (parent) {
|
|
|
|
|
|
|
|
String names;
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int i = 0; i < parent->get_bone_count(); i++) {
|
|
|
|
if (i > 0)
|
|
|
|
names += ",";
|
|
|
|
names += parent->get_bone_name(i);
|
2016-03-29 21:52:31 +02:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
p_list->push_back(PropertyInfo(Variant::STRING, "ik_bone", PROPERTY_HINT_ENUM, names));
|
2016-03-29 21:52:31 +02:00
|
|
|
} else {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
p_list->push_back(PropertyInfo(Variant::STRING, "ik_bone"));
|
2016-03-29 21:52:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void InverseKinematics::_check_bind() {
|
2016-03-29 21:52:31 +02:00
|
|
|
|
|
|
|
if (get_parent() && get_parent()->cast_to<Skeleton>()) {
|
|
|
|
Skeleton *sk = get_parent()->cast_to<Skeleton>();
|
|
|
|
int idx = sk->find_bone(ik_bone);
|
2017-03-19 00:36:26 +01:00
|
|
|
if (idx != -1) {
|
2016-03-29 21:52:31 +02:00
|
|
|
ik_bone_no = idx;
|
2017-03-19 00:36:26 +01:00
|
|
|
bound = true;
|
2016-03-29 21:52:31 +02:00
|
|
|
}
|
2016-03-30 15:55:54 +02:00
|
|
|
skel = sk;
|
2016-03-29 21:52:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void InverseKinematics::_check_unbind() {
|
2016-03-29 21:52:31 +02:00
|
|
|
|
|
|
|
if (bound) {
|
|
|
|
|
|
|
|
if (get_parent() && get_parent()->cast_to<Skeleton>()) {
|
|
|
|
Skeleton *sk = get_parent()->cast_to<Skeleton>();
|
|
|
|
int idx = sk->find_bone(ik_bone);
|
2017-03-19 00:36:26 +01:00
|
|
|
if (idx != -1)
|
2016-03-29 21:52:31 +02:00
|
|
|
ik_bone_no = idx;
|
2016-03-30 15:55:54 +02:00
|
|
|
else
|
|
|
|
ik_bone_no = 0;
|
|
|
|
skel = sk;
|
2016-03-29 21:52:31 +02:00
|
|
|
}
|
2017-03-19 00:36:26 +01:00
|
|
|
bound = false;
|
2016-03-29 21:52:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void InverseKinematics::set_bone_name(const String &p_name) {
|
2016-03-29 21:52:31 +02:00
|
|
|
|
|
|
|
if (is_inside_tree())
|
|
|
|
_check_unbind();
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ik_bone = p_name;
|
2016-03-29 21:52:31 +02:00
|
|
|
|
|
|
|
if (is_inside_tree())
|
|
|
|
_check_bind();
|
2016-03-30 15:55:54 +02:00
|
|
|
changed = true;
|
2016-03-29 21:52:31 +02:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
String InverseKinematics::get_bone_name() const {
|
2016-03-29 21:52:31 +02:00
|
|
|
|
|
|
|
return ik_bone;
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void InverseKinematics::set_iterations(int itn) {
|
2016-03-29 21:52:31 +02:00
|
|
|
|
|
|
|
if (is_inside_tree())
|
|
|
|
_check_unbind();
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
iterations = itn;
|
2016-03-29 21:52:31 +02:00
|
|
|
|
|
|
|
if (is_inside_tree())
|
|
|
|
_check_bind();
|
2016-03-30 15:55:54 +02:00
|
|
|
changed = true;
|
2016-03-29 21:52:31 +02:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
int InverseKinematics::get_iterations() const {
|
2016-03-29 21:52:31 +02:00
|
|
|
|
|
|
|
return iterations;
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void InverseKinematics::set_chain_size(int cs) {
|
2016-03-29 21:52:31 +02:00
|
|
|
if (is_inside_tree())
|
|
|
|
_check_unbind();
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
chain_size = cs;
|
2016-03-29 21:52:31 +02:00
|
|
|
chain.clear();
|
2016-03-30 15:55:54 +02:00
|
|
|
if (bound)
|
|
|
|
update_parameters();
|
2016-03-29 21:52:31 +02:00
|
|
|
|
|
|
|
if (is_inside_tree())
|
|
|
|
_check_bind();
|
2016-03-30 15:55:54 +02:00
|
|
|
changed = true;
|
2016-03-29 21:52:31 +02:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
int InverseKinematics::get_chain_size() const {
|
2016-03-29 21:52:31 +02:00
|
|
|
|
|
|
|
return chain_size;
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void InverseKinematics::set_precision(float p) {
|
2016-03-29 21:52:31 +02:00
|
|
|
|
|
|
|
if (is_inside_tree())
|
|
|
|
_check_unbind();
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
precision = p;
|
2016-03-29 21:52:31 +02:00
|
|
|
|
|
|
|
if (is_inside_tree())
|
|
|
|
_check_bind();
|
2016-03-30 15:55:54 +02:00
|
|
|
changed = true;
|
2016-03-29 21:52:31 +02:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
float InverseKinematics::get_precision() const {
|
2016-03-29 21:52:31 +02:00
|
|
|
|
|
|
|
return precision;
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void InverseKinematics::set_speed(float p) {
|
2016-03-30 14:54:11 +02:00
|
|
|
|
|
|
|
if (is_inside_tree())
|
|
|
|
_check_unbind();
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
speed = p;
|
2016-03-30 14:54:11 +02:00
|
|
|
|
|
|
|
if (is_inside_tree())
|
|
|
|
_check_bind();
|
2016-03-30 15:55:54 +02:00
|
|
|
changed = true;
|
2016-03-30 14:54:11 +02:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
float InverseKinematics::get_speed() const {
|
2016-03-30 14:54:11 +02:00
|
|
|
|
|
|
|
return speed;
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void InverseKinematics::update_parameters() {
|
2016-03-30 15:55:54 +02:00
|
|
|
tail_bone = -1;
|
|
|
|
for (int i = 0; i < skel->get_bone_count(); i++)
|
|
|
|
if (skel->get_bone_parent(i) == ik_bone_no)
|
|
|
|
tail_bone = i;
|
|
|
|
int cur_bone = ik_bone_no;
|
|
|
|
int its = chain_size;
|
|
|
|
while (its > 0 && cur_bone >= 0) {
|
|
|
|
chain.push_back(cur_bone);
|
|
|
|
cur_bone = skel->get_bone_parent(cur_bone);
|
|
|
|
its--;
|
|
|
|
}
|
|
|
|
}
|
2016-03-29 21:52:31 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void InverseKinematics::_notification(int p_what) {
|
2016-03-29 21:52:31 +02:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
switch (p_what) {
|
2016-03-29 21:52:31 +02:00
|
|
|
|
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
|
|
|
|
|
|
|
_check_bind();
|
|
|
|
if (bound) {
|
2016-03-30 15:55:54 +02:00
|
|
|
update_parameters();
|
|
|
|
changed = false;
|
2016-03-29 21:52:31 +02:00
|
|
|
set_process(true);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case NOTIFICATION_PROCESS: {
|
2016-07-03 23:19:22 +02:00
|
|
|
|
2016-03-29 21:52:31 +02:00
|
|
|
Spatial *sksp = skel->cast_to<Spatial>();
|
|
|
|
if (!bound)
|
|
|
|
break;
|
|
|
|
if (!sksp)
|
|
|
|
break;
|
2016-03-30 15:55:54 +02:00
|
|
|
if (changed) {
|
|
|
|
update_parameters();
|
|
|
|
changed = false;
|
|
|
|
}
|
2016-03-29 21:52:31 +02:00
|
|
|
Vector3 to = get_translation();
|
|
|
|
for (int hump = 0; hump < iterations; hump++) {
|
|
|
|
int depth = 0;
|
|
|
|
float olderr = 1000.0;
|
|
|
|
float psign = 1.0;
|
|
|
|
bool reached = false;
|
|
|
|
|
|
|
|
for (List<int>::Element *b = chain.front(); b; b = b->next()) {
|
|
|
|
int cur_bone = b->get();
|
|
|
|
Vector3 d = skel->get_bone_global_pose(tail_bone).origin;
|
|
|
|
Vector3 rg = to;
|
|
|
|
float err = d.distance_squared_to(rg);
|
|
|
|
if (err < precision) {
|
|
|
|
if (!reached && err < precision)
|
|
|
|
reached = true;
|
|
|
|
break;
|
2017-03-19 00:36:26 +01:00
|
|
|
} else if (reached)
|
|
|
|
reached = false;
|
2016-03-29 21:52:31 +02:00
|
|
|
if (err > olderr)
|
|
|
|
psign = -psign;
|
|
|
|
Transform mod = skel->get_bone_global_pose(cur_bone);
|
|
|
|
Quat q1 = Quat(mod.basis).normalized();
|
|
|
|
Transform mod2 = mod.looking_at(to, Vector3(0.0, 1.0, 0.0));
|
|
|
|
Quat q2 = Quat(mod2.basis).normalized();
|
|
|
|
if (psign < 0.0)
|
|
|
|
q2 = q2.inverse();
|
2016-03-30 14:54:11 +02:00
|
|
|
Quat q = q1.slerp(q2, speed / (1.0 + 500.0 * depth)).normalized();
|
2016-03-29 21:52:31 +02:00
|
|
|
Transform fin = Transform(q);
|
|
|
|
fin.origin = mod.origin;
|
|
|
|
skel->set_bone_global_pose(cur_bone, fin);
|
|
|
|
depth++;
|
|
|
|
}
|
|
|
|
if (reached)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
} break;
|
2016-03-29 21:52:31 +02:00
|
|
|
case NOTIFICATION_EXIT_TREE: {
|
|
|
|
set_process(false);
|
|
|
|
|
|
|
|
_check_unbind();
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void InverseKinematics::_bind_methods() {
|
2017-03-19 00:36:26 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("set_bone_name", "ik_bone"), &InverseKinematics::set_bone_name);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_bone_name"), &InverseKinematics::get_bone_name);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_iterations", "iterations"), &InverseKinematics::set_iterations);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_iterations"), &InverseKinematics::get_iterations);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_chain_size", "chain_size"), &InverseKinematics::set_chain_size);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_chain_size"), &InverseKinematics::get_chain_size);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_precision", "precision"), &InverseKinematics::set_precision);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_precision"), &InverseKinematics::get_precision);
|
|
|
|
ObjectTypeDB::bind_method(_MD("set_speed", "speed"), &InverseKinematics::set_speed);
|
|
|
|
ObjectTypeDB::bind_method(_MD("get_speed"), &InverseKinematics::get_speed);
|
2016-03-29 21:52:31 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "iterations"), _SCS("set_iterations"), _SCS("get_iterations"));
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "chain_size"), _SCS("set_chain_size"), _SCS("get_chain_size"));
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::REAL, "precision"), _SCS("set_precision"), _SCS("get_precision"));
|
2016-03-30 14:54:11 +02:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::REAL, "speed"), _SCS("set_speed"), _SCS("get_speed"));
|
2016-03-29 21:52:31 +02:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
InverseKinematics::InverseKinematics() {
|
|
|
|
bound = false;
|
2016-03-29 21:52:31 +02:00
|
|
|
chain_size = 2;
|
|
|
|
iterations = 100;
|
|
|
|
precision = 0.001;
|
2016-03-30 14:54:11 +02:00
|
|
|
speed = 0.2;
|
2016-03-29 21:52:31 +02:00
|
|
|
}
|