skeleton_ik.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. /*************************************************************************/
  2. /* skeleton_ik.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. * @author AndreaCatania
  32. */
  33. #include "skeleton_ik.h"
  34. #ifndef _3D_DISABLED
  35. FabrikInverseKinematic::ChainItem *FabrikInverseKinematic::ChainItem::find_child(const BoneId p_bone_id) {
  36. for (int i = children.size() - 1; 0 <= i; --i) {
  37. if (p_bone_id == children[i].bone) {
  38. return &children.write[i];
  39. }
  40. }
  41. return nullptr;
  42. }
  43. FabrikInverseKinematic::ChainItem *FabrikInverseKinematic::ChainItem::add_child(const BoneId p_bone_id) {
  44. const int infant_child_id = children.size();
  45. children.resize(infant_child_id + 1);
  46. children.write[infant_child_id].bone = p_bone_id;
  47. children.write[infant_child_id].parent_item = this;
  48. return &children.write[infant_child_id];
  49. }
  50. /// Build a chain that starts from the root to tip
  51. bool FabrikInverseKinematic::build_chain(Task *p_task, bool p_force_simple_chain) {
  52. ERR_FAIL_COND_V(-1 == p_task->root_bone, false);
  53. Chain &chain(p_task->chain);
  54. chain.tips.resize(p_task->end_effectors.size());
  55. chain.chain_root.bone = p_task->root_bone;
  56. chain.chain_root.initial_transform = p_task->skeleton->get_bone_global_pose(chain.chain_root.bone);
  57. chain.chain_root.current_pos = chain.chain_root.initial_transform.origin;
  58. chain.middle_chain_item = nullptr;
  59. // Holds all IDs that are composing a single chain in reverse order
  60. Vector<BoneId> chain_ids;
  61. // This is used to know the chain size
  62. int sub_chain_size;
  63. // Resize only one time in order to fit all joints for performance reason
  64. chain_ids.resize(p_task->skeleton->get_bone_count());
  65. for (int x = p_task->end_effectors.size() - 1; 0 <= x; --x) {
  66. const EndEffector *ee(&p_task->end_effectors[x]);
  67. ERR_FAIL_COND_V(p_task->root_bone >= ee->tip_bone, false);
  68. ERR_FAIL_INDEX_V(ee->tip_bone, p_task->skeleton->get_bone_count(), false);
  69. sub_chain_size = 0;
  70. // Picks all IDs that composing a single chain in reverse order (except the root)
  71. BoneId chain_sub_tip(ee->tip_bone);
  72. while (chain_sub_tip > p_task->root_bone) {
  73. chain_ids.write[sub_chain_size++] = chain_sub_tip;
  74. chain_sub_tip = p_task->skeleton->get_bone_parent(chain_sub_tip);
  75. }
  76. BoneId middle_chain_item_id = (((float)sub_chain_size) * 0.5);
  77. // Build chain by reading chain ids in reverse order
  78. // For each chain item id will be created a ChainItem if doesn't exists
  79. ChainItem *sub_chain(&chain.chain_root);
  80. for (int i = sub_chain_size - 1; 0 <= i; --i) {
  81. ChainItem *child_ci(sub_chain->find_child(chain_ids[i]));
  82. if (!child_ci) {
  83. child_ci = sub_chain->add_child(chain_ids[i]);
  84. child_ci->initial_transform = p_task->skeleton->get_bone_global_pose(child_ci->bone);
  85. child_ci->current_pos = child_ci->initial_transform.origin;
  86. if (child_ci->parent_item) {
  87. child_ci->length = (child_ci->current_pos - child_ci->parent_item->current_pos).length();
  88. }
  89. }
  90. sub_chain = child_ci;
  91. if (middle_chain_item_id == i) {
  92. chain.middle_chain_item = child_ci;
  93. }
  94. }
  95. if (!middle_chain_item_id) {
  96. chain.middle_chain_item = nullptr;
  97. }
  98. // Initialize current tip
  99. chain.tips.write[x].chain_item = sub_chain;
  100. chain.tips.write[x].end_effector = ee;
  101. if (p_force_simple_chain) {
  102. // NOTE:
  103. // This is an "hack" that force to create only one tip per chain since the solver of multi tip (end effector)
  104. // is not yet created.
  105. // Remove this code when this is done
  106. break;
  107. }
  108. }
  109. return true;
  110. }
  111. void FabrikInverseKinematic::solve_simple(Task *p_task, bool p_solve_magnet, Vector3 p_origin_pos) {
  112. real_t distance_to_goal(1e4);
  113. real_t previous_distance_to_goal(0);
  114. int can_solve(p_task->max_iterations);
  115. while (distance_to_goal > p_task->min_distance && Math::abs(previous_distance_to_goal - distance_to_goal) > 0.005 && can_solve) {
  116. previous_distance_to_goal = distance_to_goal;
  117. --can_solve;
  118. solve_simple_backwards(p_task->chain, p_solve_magnet);
  119. solve_simple_forwards(p_task->chain, p_solve_magnet, p_origin_pos);
  120. distance_to_goal = (p_task->chain.tips[0].chain_item->current_pos - p_task->chain.tips[0].end_effector->goal_transform.origin).length();
  121. }
  122. }
  123. void FabrikInverseKinematic::solve_simple_backwards(Chain &r_chain, bool p_solve_magnet) {
  124. if (p_solve_magnet && !r_chain.middle_chain_item) {
  125. return;
  126. }
  127. Vector3 goal;
  128. ChainItem *sub_chain_tip;
  129. if (p_solve_magnet) {
  130. goal = r_chain.magnet_position;
  131. sub_chain_tip = r_chain.middle_chain_item;
  132. } else {
  133. goal = r_chain.tips[0].end_effector->goal_transform.origin;
  134. sub_chain_tip = r_chain.tips[0].chain_item;
  135. }
  136. while (sub_chain_tip) {
  137. sub_chain_tip->current_pos = goal;
  138. if (sub_chain_tip->parent_item) {
  139. // Not yet in the chain root
  140. // So calculate next goal location
  141. const Vector3 look_parent((sub_chain_tip->parent_item->current_pos - sub_chain_tip->current_pos).normalized());
  142. goal = sub_chain_tip->current_pos + (look_parent * sub_chain_tip->length);
  143. // [TODO] Constraints goes here
  144. }
  145. sub_chain_tip = sub_chain_tip->parent_item;
  146. }
  147. }
  148. void FabrikInverseKinematic::solve_simple_forwards(Chain &r_chain, bool p_solve_magnet, Vector3 p_origin_pos) {
  149. if (p_solve_magnet && !r_chain.middle_chain_item) {
  150. return;
  151. }
  152. ChainItem *sub_chain_root(&r_chain.chain_root);
  153. Vector3 origin = p_origin_pos;
  154. while (sub_chain_root) { // Reach the tip
  155. sub_chain_root->current_pos = origin;
  156. if (!sub_chain_root->children.empty()) {
  157. ChainItem &child(sub_chain_root->children.write[0]);
  158. // Is not tip
  159. // So calculate next origin location
  160. // Look child
  161. sub_chain_root->current_ori = (child.current_pos - sub_chain_root->current_pos).normalized();
  162. origin = sub_chain_root->current_pos + (sub_chain_root->current_ori * child.length);
  163. // [TODO] Constraints goes here
  164. if (p_solve_magnet && sub_chain_root == r_chain.middle_chain_item) {
  165. // In case of magnet solving this is the tip
  166. sub_chain_root = nullptr;
  167. } else {
  168. sub_chain_root = &child;
  169. }
  170. } else {
  171. // Is tip
  172. sub_chain_root = nullptr;
  173. }
  174. }
  175. }
  176. FabrikInverseKinematic::Task *FabrikInverseKinematic::create_simple_task(Skeleton *p_sk, BoneId root_bone, BoneId tip_bone, const Transform &goal_transform) {
  177. FabrikInverseKinematic::EndEffector ee;
  178. ee.tip_bone = tip_bone;
  179. Task *task(memnew(Task));
  180. task->skeleton = p_sk;
  181. task->root_bone = root_bone;
  182. task->end_effectors.push_back(ee);
  183. task->goal_global_transform = goal_transform;
  184. if (!build_chain(task)) {
  185. free_task(task);
  186. return nullptr;
  187. }
  188. return task;
  189. }
  190. void FabrikInverseKinematic::free_task(Task *p_task) {
  191. if (p_task) {
  192. memdelete(p_task);
  193. }
  194. }
  195. void FabrikInverseKinematic::set_goal(Task *p_task, const Transform &p_goal) {
  196. p_task->goal_global_transform = p_goal;
  197. }
  198. void FabrikInverseKinematic::make_goal(Task *p_task, const Transform &p_inverse_transf, real_t blending_delta) {
  199. if (blending_delta >= 0.99f) {
  200. // Update the end_effector (local transform) without blending
  201. p_task->end_effectors.write[0].goal_transform = p_inverse_transf * p_task->goal_global_transform;
  202. } else {
  203. // End effector in local transform
  204. const Transform end_effector_pose(p_task->skeleton->get_bone_global_pose_no_override(p_task->end_effectors[0].tip_bone));
  205. // Update the end_effector (local transform) by blending with current pose
  206. p_task->end_effectors.write[0].goal_transform = end_effector_pose.interpolate_with(p_inverse_transf * p_task->goal_global_transform, blending_delta);
  207. }
  208. }
  209. void FabrikInverseKinematic::solve(Task *p_task, real_t blending_delta, bool override_tip_basis, bool p_use_magnet, const Vector3 &p_magnet_position) {
  210. if (blending_delta <= 0.01f) {
  211. // Before skipping, make sure we undo the global pose overrides
  212. ChainItem *ci(&p_task->chain.chain_root);
  213. while (ci) {
  214. p_task->skeleton->set_bone_global_pose_override(ci->bone, ci->initial_transform, 0.0, false);
  215. if (!ci->children.empty()) {
  216. ci = &ci->children.write[0];
  217. } else {
  218. ci = nullptr;
  219. }
  220. }
  221. return; // Skip solving
  222. }
  223. // Update the initial root transform so its synced with any animation changes
  224. _update_chain(p_task->skeleton, &p_task->chain.chain_root);
  225. p_task->skeleton->set_bone_global_pose_override(p_task->chain.chain_root.bone, Transform(), 0.0, false);
  226. Vector3 origin_pos = p_task->skeleton->get_bone_global_pose(p_task->chain.chain_root.bone).origin;
  227. make_goal(p_task, p_task->skeleton->get_global_transform().affine_inverse(), blending_delta);
  228. if (p_use_magnet && p_task->chain.middle_chain_item) {
  229. p_task->chain.magnet_position = p_task->chain.middle_chain_item->initial_transform.origin.linear_interpolate(p_magnet_position, blending_delta);
  230. solve_simple(p_task, true, origin_pos);
  231. }
  232. solve_simple(p_task, false, origin_pos);
  233. // Assign new bone position.
  234. ChainItem *ci(&p_task->chain.chain_root);
  235. while (ci) {
  236. Transform new_bone_pose(ci->initial_transform);
  237. new_bone_pose.origin = ci->current_pos;
  238. if (!ci->children.empty()) {
  239. /// Rotate basis
  240. const Vector3 initial_ori((ci->children[0].initial_transform.origin - ci->initial_transform.origin).normalized());
  241. const Vector3 rot_axis(initial_ori.cross(ci->current_ori).normalized());
  242. if (rot_axis[0] != 0 && rot_axis[1] != 0 && rot_axis[2] != 0) {
  243. const real_t rot_angle(Math::acos(CLAMP(initial_ori.dot(ci->current_ori), -1, 1)));
  244. new_bone_pose.basis.rotate(rot_axis, rot_angle);
  245. }
  246. } else {
  247. // Set target orientation to tip
  248. if (override_tip_basis)
  249. new_bone_pose.basis = p_task->chain.tips[0].end_effector->goal_transform.basis;
  250. else
  251. new_bone_pose.basis = new_bone_pose.basis * p_task->chain.tips[0].end_effector->goal_transform.basis;
  252. }
  253. // IK should not affect scale, so undo any scaling
  254. new_bone_pose.basis.orthonormalize();
  255. new_bone_pose.basis.scale(p_task->skeleton->get_bone_global_pose(ci->bone).basis.get_scale());
  256. p_task->skeleton->set_bone_global_pose_override(ci->bone, new_bone_pose, 1.0, true);
  257. if (!ci->children.empty()) {
  258. ci = &ci->children.write[0];
  259. } else {
  260. ci = nullptr;
  261. }
  262. }
  263. }
  264. void FabrikInverseKinematic::_update_chain(const Skeleton *p_sk, ChainItem *p_chain_item) {
  265. if (!p_chain_item) {
  266. return;
  267. }
  268. p_chain_item->initial_transform = p_sk->get_bone_global_pose_no_override(p_chain_item->bone);
  269. p_chain_item->current_pos = p_chain_item->initial_transform.origin;
  270. ChainItem *items = p_chain_item->children.ptrw();
  271. for (int i = 0; i < p_chain_item->children.size(); i += 1) {
  272. _update_chain(p_sk, items + i);
  273. }
  274. }
  275. void SkeletonIK::_validate_property(PropertyInfo &property) const {
  276. if (property.name == "root_bone" || property.name == "tip_bone") {
  277. if (skeleton) {
  278. String names("--,");
  279. for (int i = 0; i < skeleton->get_bone_count(); i++) {
  280. if (i > 0) {
  281. names += ",";
  282. }
  283. names += skeleton->get_bone_name(i);
  284. }
  285. property.hint = PROPERTY_HINT_ENUM;
  286. property.hint_string = names;
  287. } else {
  288. property.hint = PROPERTY_HINT_NONE;
  289. property.hint_string = "";
  290. }
  291. }
  292. }
  293. void SkeletonIK::_bind_methods() {
  294. ClassDB::bind_method(D_METHOD("set_root_bone", "root_bone"), &SkeletonIK::set_root_bone);
  295. ClassDB::bind_method(D_METHOD("get_root_bone"), &SkeletonIK::get_root_bone);
  296. ClassDB::bind_method(D_METHOD("set_tip_bone", "tip_bone"), &SkeletonIK::set_tip_bone);
  297. ClassDB::bind_method(D_METHOD("get_tip_bone"), &SkeletonIK::get_tip_bone);
  298. ClassDB::bind_method(D_METHOD("set_interpolation", "interpolation"), &SkeletonIK::set_interpolation);
  299. ClassDB::bind_method(D_METHOD("get_interpolation"), &SkeletonIK::get_interpolation);
  300. ClassDB::bind_method(D_METHOD("set_target_transform", "target"), &SkeletonIK::set_target_transform);
  301. ClassDB::bind_method(D_METHOD("get_target_transform"), &SkeletonIK::get_target_transform);
  302. ClassDB::bind_method(D_METHOD("set_target_node", "node"), &SkeletonIK::set_target_node);
  303. ClassDB::bind_method(D_METHOD("get_target_node"), &SkeletonIK::get_target_node);
  304. ClassDB::bind_method(D_METHOD("set_override_tip_basis", "override"), &SkeletonIK::set_override_tip_basis);
  305. ClassDB::bind_method(D_METHOD("is_override_tip_basis"), &SkeletonIK::is_override_tip_basis);
  306. ClassDB::bind_method(D_METHOD("set_use_magnet", "use"), &SkeletonIK::set_use_magnet);
  307. ClassDB::bind_method(D_METHOD("is_using_magnet"), &SkeletonIK::is_using_magnet);
  308. ClassDB::bind_method(D_METHOD("set_magnet_position", "local_position"), &SkeletonIK::set_magnet_position);
  309. ClassDB::bind_method(D_METHOD("get_magnet_position"), &SkeletonIK::get_magnet_position);
  310. ClassDB::bind_method(D_METHOD("get_parent_skeleton"), &SkeletonIK::get_parent_skeleton);
  311. ClassDB::bind_method(D_METHOD("is_running"), &SkeletonIK::is_running);
  312. ClassDB::bind_method(D_METHOD("set_min_distance", "min_distance"), &SkeletonIK::set_min_distance);
  313. ClassDB::bind_method(D_METHOD("get_min_distance"), &SkeletonIK::get_min_distance);
  314. ClassDB::bind_method(D_METHOD("set_max_iterations", "iterations"), &SkeletonIK::set_max_iterations);
  315. ClassDB::bind_method(D_METHOD("get_max_iterations"), &SkeletonIK::get_max_iterations);
  316. ClassDB::bind_method(D_METHOD("start", "one_time"), &SkeletonIK::start, DEFVAL(false));
  317. ClassDB::bind_method(D_METHOD("stop"), &SkeletonIK::stop);
  318. ADD_PROPERTY(PropertyInfo(Variant::STRING, "root_bone"), "set_root_bone", "get_root_bone");
  319. ADD_PROPERTY(PropertyInfo(Variant::STRING, "tip_bone"), "set_tip_bone", "get_tip_bone");
  320. ADD_PROPERTY(PropertyInfo(Variant::REAL, "interpolation", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_interpolation", "get_interpolation");
  321. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM, "target"), "set_target_transform", "get_target_transform");
  322. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "override_tip_basis"), "set_override_tip_basis", "is_override_tip_basis");
  323. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_magnet"), "set_use_magnet", "is_using_magnet");
  324. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "magnet"), "set_magnet_position", "get_magnet_position");
  325. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "target_node"), "set_target_node", "get_target_node");
  326. ADD_PROPERTY(PropertyInfo(Variant::REAL, "min_distance"), "set_min_distance", "get_min_distance");
  327. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_iterations"), "set_max_iterations", "get_max_iterations");
  328. }
  329. void SkeletonIK::_notification(int p_what) {
  330. switch (p_what) {
  331. case NOTIFICATION_ENTER_TREE: {
  332. skeleton = Object::cast_to<Skeleton>(get_parent());
  333. set_process_priority(1);
  334. reload_chain();
  335. } break;
  336. case NOTIFICATION_INTERNAL_PROCESS: {
  337. if (target_node_override) {
  338. reload_goal();
  339. }
  340. _solve_chain();
  341. } break;
  342. case NOTIFICATION_EXIT_TREE: {
  343. reload_chain();
  344. } break;
  345. }
  346. }
  347. SkeletonIK::SkeletonIK() :
  348. interpolation(1),
  349. override_tip_basis(true),
  350. use_magnet(false),
  351. min_distance(0.01),
  352. max_iterations(10),
  353. skeleton(nullptr),
  354. target_node_override(nullptr),
  355. task(nullptr) {
  356. }
  357. SkeletonIK::~SkeletonIK() {
  358. FabrikInverseKinematic::free_task(task);
  359. task = nullptr;
  360. }
  361. void SkeletonIK::set_root_bone(const StringName &p_root_bone) {
  362. root_bone = p_root_bone;
  363. reload_chain();
  364. }
  365. StringName SkeletonIK::get_root_bone() const {
  366. return root_bone;
  367. }
  368. void SkeletonIK::set_tip_bone(const StringName &p_tip_bone) {
  369. tip_bone = p_tip_bone;
  370. reload_chain();
  371. }
  372. StringName SkeletonIK::get_tip_bone() const {
  373. return tip_bone;
  374. }
  375. void SkeletonIK::set_interpolation(real_t p_interpolation) {
  376. interpolation = p_interpolation;
  377. }
  378. real_t SkeletonIK::get_interpolation() const {
  379. return interpolation;
  380. }
  381. void SkeletonIK::set_target_transform(const Transform &p_target) {
  382. target = p_target;
  383. reload_goal();
  384. }
  385. const Transform &SkeletonIK::get_target_transform() const {
  386. return target;
  387. }
  388. void SkeletonIK::set_target_node(const NodePath &p_node) {
  389. target_node_path_override = p_node;
  390. target_node_override = nullptr;
  391. reload_goal();
  392. }
  393. NodePath SkeletonIK::get_target_node() {
  394. return target_node_path_override;
  395. }
  396. void SkeletonIK::set_override_tip_basis(bool p_override) {
  397. override_tip_basis = p_override;
  398. }
  399. bool SkeletonIK::is_override_tip_basis() const {
  400. return override_tip_basis;
  401. }
  402. void SkeletonIK::set_use_magnet(bool p_use) {
  403. use_magnet = p_use;
  404. }
  405. bool SkeletonIK::is_using_magnet() const {
  406. return use_magnet;
  407. }
  408. void SkeletonIK::set_magnet_position(const Vector3 &p_local_position) {
  409. magnet_position = p_local_position;
  410. }
  411. const Vector3 &SkeletonIK::get_magnet_position() const {
  412. return magnet_position;
  413. }
  414. void SkeletonIK::set_min_distance(real_t p_min_distance) {
  415. min_distance = p_min_distance;
  416. }
  417. void SkeletonIK::set_max_iterations(int p_iterations) {
  418. max_iterations = p_iterations;
  419. }
  420. bool SkeletonIK::is_running() {
  421. return is_processing_internal();
  422. }
  423. void SkeletonIK::start(bool p_one_time) {
  424. if (p_one_time) {
  425. set_process_internal(false);
  426. if (target_node_override) {
  427. reload_goal();
  428. }
  429. _solve_chain();
  430. } else {
  431. set_process_internal(true);
  432. }
  433. }
  434. void SkeletonIK::stop() {
  435. set_process_internal(false);
  436. if (skeleton) {
  437. skeleton->clear_bones_global_pose_override();
  438. }
  439. }
  440. Transform SkeletonIK::_get_target_transform() {
  441. if (!target_node_override && !target_node_path_override.is_empty()) {
  442. target_node_override = Object::cast_to<Spatial>(get_node(target_node_path_override));
  443. }
  444. if (target_node_override && target_node_override->is_inside_tree()) {
  445. return target_node_override->get_global_transform();
  446. } else {
  447. return target;
  448. }
  449. }
  450. void SkeletonIK::reload_chain() {
  451. FabrikInverseKinematic::free_task(task);
  452. task = nullptr;
  453. if (!skeleton) {
  454. return;
  455. }
  456. task = FabrikInverseKinematic::create_simple_task(skeleton, skeleton->find_bone(root_bone), skeleton->find_bone(tip_bone), _get_target_transform());
  457. if (task) {
  458. task->max_iterations = max_iterations;
  459. task->min_distance = min_distance;
  460. }
  461. }
  462. void SkeletonIK::reload_goal() {
  463. if (!task) {
  464. return;
  465. }
  466. FabrikInverseKinematic::set_goal(task, _get_target_transform());
  467. }
  468. void SkeletonIK::_solve_chain() {
  469. if (!task) {
  470. return;
  471. }
  472. FabrikInverseKinematic::solve(task, interpolation, override_tip_basis, use_magnet, magnet_position);
  473. }
  474. #endif // _3D_DISABLED