skeleton_ik_3d.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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. Skeleton3D *skeleton = get_parent_skeleton();
  272. if (skeleton) {
  273. String names("--,");
  274. for (int i = 0; i < skeleton->get_bone_count(); i++) {
  275. if (i > 0) {
  276. names += ",";
  277. }
  278. names += skeleton->get_bone_name(i);
  279. }
  280. p_property.hint = PROPERTY_HINT_ENUM;
  281. p_property.hint_string = names;
  282. } else {
  283. p_property.hint = PROPERTY_HINT_NONE;
  284. p_property.hint_string = "";
  285. }
  286. }
  287. }
  288. void SkeletonIK3D::_bind_methods() {
  289. ClassDB::bind_method(D_METHOD("set_root_bone", "root_bone"), &SkeletonIK3D::set_root_bone);
  290. ClassDB::bind_method(D_METHOD("get_root_bone"), &SkeletonIK3D::get_root_bone);
  291. ClassDB::bind_method(D_METHOD("set_tip_bone", "tip_bone"), &SkeletonIK3D::set_tip_bone);
  292. ClassDB::bind_method(D_METHOD("get_tip_bone"), &SkeletonIK3D::get_tip_bone);
  293. ClassDB::bind_method(D_METHOD("set_interpolation", "interpolation"), &SkeletonIK3D::set_interpolation);
  294. ClassDB::bind_method(D_METHOD("get_interpolation"), &SkeletonIK3D::get_interpolation);
  295. ClassDB::bind_method(D_METHOD("set_target_transform", "target"), &SkeletonIK3D::set_target_transform);
  296. ClassDB::bind_method(D_METHOD("get_target_transform"), &SkeletonIK3D::get_target_transform);
  297. ClassDB::bind_method(D_METHOD("set_target_node", "node"), &SkeletonIK3D::set_target_node);
  298. ClassDB::bind_method(D_METHOD("get_target_node"), &SkeletonIK3D::get_target_node);
  299. ClassDB::bind_method(D_METHOD("set_override_tip_basis", "override"), &SkeletonIK3D::set_override_tip_basis);
  300. ClassDB::bind_method(D_METHOD("is_override_tip_basis"), &SkeletonIK3D::is_override_tip_basis);
  301. ClassDB::bind_method(D_METHOD("set_use_magnet", "use"), &SkeletonIK3D::set_use_magnet);
  302. ClassDB::bind_method(D_METHOD("is_using_magnet"), &SkeletonIK3D::is_using_magnet);
  303. ClassDB::bind_method(D_METHOD("set_magnet_position", "local_position"), &SkeletonIK3D::set_magnet_position);
  304. ClassDB::bind_method(D_METHOD("get_magnet_position"), &SkeletonIK3D::get_magnet_position);
  305. ClassDB::bind_method(D_METHOD("get_parent_skeleton"), &SkeletonIK3D::get_parent_skeleton);
  306. ClassDB::bind_method(D_METHOD("is_running"), &SkeletonIK3D::is_running);
  307. ClassDB::bind_method(D_METHOD("set_min_distance", "min_distance"), &SkeletonIK3D::set_min_distance);
  308. ClassDB::bind_method(D_METHOD("get_min_distance"), &SkeletonIK3D::get_min_distance);
  309. ClassDB::bind_method(D_METHOD("set_max_iterations", "iterations"), &SkeletonIK3D::set_max_iterations);
  310. ClassDB::bind_method(D_METHOD("get_max_iterations"), &SkeletonIK3D::get_max_iterations);
  311. ClassDB::bind_method(D_METHOD("start", "one_time"), &SkeletonIK3D::start, DEFVAL(false));
  312. ClassDB::bind_method(D_METHOD("stop"), &SkeletonIK3D::stop);
  313. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "root_bone"), "set_root_bone", "get_root_bone");
  314. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "tip_bone"), "set_tip_bone", "get_tip_bone");
  315. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "interpolation", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_interpolation", "get_interpolation");
  316. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM3D, "target", PROPERTY_HINT_NONE, "suffix:m"), "set_target_transform", "get_target_transform");
  317. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "override_tip_basis"), "set_override_tip_basis", "is_override_tip_basis");
  318. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_magnet"), "set_use_magnet", "is_using_magnet");
  319. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "magnet", PROPERTY_HINT_NONE, "suffix:m"), "set_magnet_position", "get_magnet_position");
  320. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "target_node"), "set_target_node", "get_target_node");
  321. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "min_distance", PROPERTY_HINT_NONE, "suffix:m"), "set_min_distance", "get_min_distance");
  322. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_iterations"), "set_max_iterations", "get_max_iterations");
  323. }
  324. void SkeletonIK3D::_notification(int p_what) {
  325. switch (p_what) {
  326. case NOTIFICATION_ENTER_TREE: {
  327. skeleton_ref = Object::cast_to<Skeleton3D>(get_parent());
  328. set_process_priority(1);
  329. reload_chain();
  330. } break;
  331. case NOTIFICATION_INTERNAL_PROCESS: {
  332. if (target_node_override_ref) {
  333. reload_goal();
  334. }
  335. _solve_chain();
  336. } break;
  337. case NOTIFICATION_EXIT_TREE: {
  338. stop();
  339. } break;
  340. }
  341. }
  342. SkeletonIK3D::SkeletonIK3D() {
  343. }
  344. SkeletonIK3D::~SkeletonIK3D() {
  345. FabrikInverseKinematic::free_task(task);
  346. task = nullptr;
  347. }
  348. void SkeletonIK3D::set_root_bone(const StringName &p_root_bone) {
  349. root_bone = p_root_bone;
  350. reload_chain();
  351. }
  352. StringName SkeletonIK3D::get_root_bone() const {
  353. return root_bone;
  354. }
  355. void SkeletonIK3D::set_tip_bone(const StringName &p_tip_bone) {
  356. tip_bone = p_tip_bone;
  357. reload_chain();
  358. }
  359. StringName SkeletonIK3D::get_tip_bone() const {
  360. return tip_bone;
  361. }
  362. void SkeletonIK3D::set_interpolation(real_t p_interpolation) {
  363. interpolation = p_interpolation;
  364. }
  365. real_t SkeletonIK3D::get_interpolation() const {
  366. return interpolation;
  367. }
  368. void SkeletonIK3D::set_target_transform(const Transform3D &p_target) {
  369. target = p_target;
  370. reload_goal();
  371. }
  372. const Transform3D &SkeletonIK3D::get_target_transform() const {
  373. return target;
  374. }
  375. void SkeletonIK3D::set_target_node(const NodePath &p_node) {
  376. target_node_path_override = p_node;
  377. target_node_override_ref = Variant();
  378. reload_goal();
  379. }
  380. NodePath SkeletonIK3D::get_target_node() {
  381. return target_node_path_override;
  382. }
  383. void SkeletonIK3D::set_override_tip_basis(bool p_override) {
  384. override_tip_basis = p_override;
  385. }
  386. bool SkeletonIK3D::is_override_tip_basis() const {
  387. return override_tip_basis;
  388. }
  389. void SkeletonIK3D::set_use_magnet(bool p_use) {
  390. use_magnet = p_use;
  391. }
  392. bool SkeletonIK3D::is_using_magnet() const {
  393. return use_magnet;
  394. }
  395. void SkeletonIK3D::set_magnet_position(const Vector3 &p_local_position) {
  396. magnet_position = p_local_position;
  397. }
  398. const Vector3 &SkeletonIK3D::get_magnet_position() const {
  399. return magnet_position;
  400. }
  401. void SkeletonIK3D::set_min_distance(real_t p_min_distance) {
  402. min_distance = p_min_distance;
  403. }
  404. void SkeletonIK3D::set_max_iterations(int p_iterations) {
  405. max_iterations = p_iterations;
  406. }
  407. Skeleton3D *SkeletonIK3D::get_parent_skeleton() const {
  408. return cast_to<Skeleton3D>(skeleton_ref.get_validated_object());
  409. }
  410. bool SkeletonIK3D::is_running() {
  411. return is_processing_internal();
  412. }
  413. void SkeletonIK3D::start(bool p_one_time) {
  414. if (p_one_time) {
  415. set_process_internal(false);
  416. if (target_node_override_ref) {
  417. reload_goal();
  418. }
  419. _solve_chain();
  420. } else {
  421. set_process_internal(true);
  422. }
  423. }
  424. void SkeletonIK3D::stop() {
  425. set_process_internal(false);
  426. Skeleton3D *skeleton = get_parent_skeleton();
  427. if (skeleton) {
  428. skeleton->clear_bones_global_pose_override();
  429. }
  430. }
  431. Transform3D SkeletonIK3D::_get_target_transform() {
  432. if (!target_node_override_ref && !target_node_path_override.is_empty()) {
  433. target_node_override_ref = Object::cast_to<Node3D>(get_node(target_node_path_override));
  434. }
  435. Node3D *target_node_override = cast_to<Node3D>(target_node_override_ref.get_validated_object());
  436. if (target_node_override && target_node_override->is_inside_tree()) {
  437. return target_node_override->get_global_transform();
  438. } else {
  439. return target;
  440. }
  441. }
  442. void SkeletonIK3D::reload_chain() {
  443. FabrikInverseKinematic::free_task(task);
  444. task = nullptr;
  445. Skeleton3D *skeleton = get_parent_skeleton();
  446. if (!skeleton) {
  447. return;
  448. }
  449. task = FabrikInverseKinematic::create_simple_task(skeleton, skeleton->find_bone(root_bone), skeleton->find_bone(tip_bone), _get_target_transform());
  450. if (task) {
  451. task->max_iterations = max_iterations;
  452. task->min_distance = min_distance;
  453. }
  454. }
  455. void SkeletonIK3D::reload_goal() {
  456. if (!task) {
  457. return;
  458. }
  459. FabrikInverseKinematic::set_goal(task, _get_target_transform());
  460. }
  461. void SkeletonIK3D::_solve_chain() {
  462. if (!task) {
  463. return;
  464. }
  465. FabrikInverseKinematic::solve(task, interpolation, override_tip_basis, use_magnet, magnet_position);
  466. }
  467. #endif // _3D_DISABLED