skeleton_ik_3d.cpp 20 KB

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