two_bone_ik_3d.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. /**************************************************************************/
  2. /* two_bone_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 "two_bone_ik_3d.h"
  31. bool TwoBoneIK3D::_set(const StringName &p_path, const Variant &p_value) {
  32. String path = p_path;
  33. if (path.begins_with("settings/")) {
  34. int which = path.get_slicec('/', 1).to_int();
  35. String what = path.get_slicec('/', 2);
  36. ERR_FAIL_INDEX_V(which, (int)settings.size(), false);
  37. if (what == "target_node") {
  38. set_target_node(which, p_value);
  39. } else if (what == "pole_node") {
  40. set_pole_node(which, p_value);
  41. } else if (what == "root_bone_name") {
  42. set_root_bone_name(which, p_value);
  43. } else if (what == "root_bone") {
  44. set_root_bone(which, p_value);
  45. } else if (what == "middle_bone_name") {
  46. set_middle_bone_name(which, p_value);
  47. } else if (what == "middle_bone") {
  48. set_middle_bone(which, p_value);
  49. } else if (what == "pole_direction") {
  50. set_pole_direction(which, static_cast<SecondaryDirection>((int)p_value));
  51. } else if (what == "pole_direction_vector") {
  52. set_pole_direction_vector(which, p_value);
  53. } else if (what == "end_bone_name") {
  54. set_end_bone_name(which, p_value);
  55. } else if (what == "end_bone") {
  56. String opt = path.get_slicec('/', 3);
  57. if (opt.is_empty()) {
  58. set_end_bone(which, p_value);
  59. } else if (opt == "direction") {
  60. set_end_bone_direction(which, static_cast<BoneDirection>((int)p_value));
  61. } else if (opt == "length") {
  62. set_end_bone_length(which, p_value);
  63. } else {
  64. return false;
  65. }
  66. } else if (what == "use_virtual_end") {
  67. set_use_virtual_end(which, p_value);
  68. } else if (what == "extend_end_bone") {
  69. set_extend_end_bone(which, p_value);
  70. } else {
  71. return false;
  72. }
  73. }
  74. return true;
  75. }
  76. bool TwoBoneIK3D::_get(const StringName &p_path, Variant &r_ret) const {
  77. String path = p_path;
  78. if (path.begins_with("settings/")) {
  79. int which = path.get_slicec('/', 1).to_int();
  80. String what = path.get_slicec('/', 2);
  81. ERR_FAIL_INDEX_V(which, (int)settings.size(), false);
  82. if (what == "target_node") {
  83. r_ret = get_target_node(which);
  84. } else if (what == "pole_node") {
  85. r_ret = get_pole_node(which);
  86. } else if (what == "root_bone_name") {
  87. r_ret = get_root_bone_name(which);
  88. } else if (what == "root_bone") {
  89. r_ret = get_root_bone(which);
  90. } else if (what == "middle_bone_name") {
  91. r_ret = get_middle_bone_name(which);
  92. } else if (what == "middle_bone") {
  93. r_ret = get_middle_bone(which);
  94. } else if (what == "pole_direction") {
  95. r_ret = (int)get_pole_direction(which);
  96. } else if (what == "pole_direction_vector") {
  97. r_ret = get_pole_direction_vector(which);
  98. } else if (what == "end_bone_name") {
  99. r_ret = get_end_bone_name(which);
  100. } else if (what == "end_bone") {
  101. String opt = path.get_slicec('/', 3);
  102. if (opt.is_empty()) {
  103. r_ret = get_end_bone(which);
  104. } else if (opt == "direction") {
  105. r_ret = (int)get_end_bone_direction(which);
  106. } else if (opt == "length") {
  107. r_ret = get_end_bone_length(which);
  108. } else {
  109. return false;
  110. }
  111. } else if (what == "use_virtual_end") {
  112. r_ret = is_using_virtual_end(which);
  113. } else if (what == "extend_end_bone") {
  114. r_ret = is_end_bone_extended(which);
  115. } else {
  116. return false;
  117. }
  118. }
  119. return true;
  120. }
  121. void TwoBoneIK3D::_get_property_list(List<PropertyInfo> *p_list) const {
  122. String enum_hint;
  123. Skeleton3D *skeleton = get_skeleton();
  124. if (skeleton) {
  125. enum_hint = skeleton->get_concatenated_bone_names();
  126. }
  127. LocalVector<PropertyInfo> props;
  128. for (uint32_t i = 0; i < settings.size(); i++) {
  129. String path = "settings/" + itos(i) + "/";
  130. props.push_back(PropertyInfo(Variant::NODE_PATH, path + "target_node"));
  131. props.push_back(PropertyInfo(Variant::NODE_PATH, path + "pole_node"));
  132. props.push_back(PropertyInfo(Variant::STRING, path + "root_bone_name", PROPERTY_HINT_ENUM_SUGGESTION, enum_hint));
  133. props.push_back(PropertyInfo(Variant::INT, path + "root_bone", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  134. props.push_back(PropertyInfo(Variant::STRING, path + "middle_bone_name", PROPERTY_HINT_ENUM_SUGGESTION, enum_hint));
  135. props.push_back(PropertyInfo(Variant::INT, path + "middle_bone", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  136. props.push_back(PropertyInfo(Variant::INT, path + "pole_direction", PROPERTY_HINT_ENUM, SkeletonModifier3D::get_hint_secondary_direction()));
  137. props.push_back(PropertyInfo(Variant::VECTOR3, path + "pole_direction_vector"));
  138. props.push_back(PropertyInfo(Variant::STRING, path + "end_bone_name", PROPERTY_HINT_ENUM_SUGGESTION, enum_hint));
  139. props.push_back(PropertyInfo(Variant::INT, path + "end_bone", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  140. props.push_back(PropertyInfo(Variant::BOOL, path + "use_virtual_end"));
  141. props.push_back(PropertyInfo(Variant::BOOL, path + "extend_end_bone"));
  142. props.push_back(PropertyInfo(Variant::INT, path + "end_bone/direction", PROPERTY_HINT_ENUM, SkeletonModifier3D::get_hint_bone_direction()));
  143. props.push_back(PropertyInfo(Variant::FLOAT, path + "end_bone/length", PROPERTY_HINT_RANGE, "0,1,0.001,or_greater,suffix:m"));
  144. }
  145. for (PropertyInfo &p : props) {
  146. _validate_dynamic_prop(p);
  147. p_list->push_back(p);
  148. }
  149. }
  150. void TwoBoneIK3D::_validate_dynamic_prop(PropertyInfo &p_property) const {
  151. PackedStringArray split = p_property.name.split("/");
  152. if (split.size() > 2 && split[0] == "settings") {
  153. int which = split[1].to_int();
  154. bool force_hide = false;
  155. if ((split[2] == "end_bone" || split[2] == "end_bone_name") && split.size() == 3 && is_using_virtual_end(which)) {
  156. p_property.usage = PROPERTY_USAGE_NONE;
  157. }
  158. if (split[2] == "use_virtual_end" && get_middle_bone(which) == -1) {
  159. p_property.usage = PROPERTY_USAGE_NONE;
  160. }
  161. if (split[2] == "extend_end_bone") {
  162. if (is_using_virtual_end(which)) {
  163. p_property.usage = PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_READ_ONLY;
  164. } else if (get_end_bone(which) == -1) {
  165. p_property.usage = PROPERTY_USAGE_NONE;
  166. force_hide = true;
  167. }
  168. }
  169. if (force_hide || (split[2] == "end_bone" && !is_end_bone_extended(which) && split.size() > 3)) {
  170. p_property.usage = PROPERTY_USAGE_NONE;
  171. }
  172. if (split[2] == "pole_direction_vector" && get_pole_direction(which) != SECONDARY_DIRECTION_CUSTOM) {
  173. p_property.usage = PROPERTY_USAGE_NONE;
  174. }
  175. }
  176. }
  177. PackedStringArray TwoBoneIK3D::get_configuration_warnings() const {
  178. PackedStringArray warnings = SkeletonModifier3D::get_configuration_warnings();
  179. for (uint32_t i = 0; i < tb_settings.size(); i++) {
  180. if (tb_settings[i]->target_node.is_empty()) {
  181. warnings.push_back(RTR("Detecting settings with no target set! TwoBoneIK3D must have a target to work."));
  182. break;
  183. }
  184. }
  185. for (uint32_t i = 0; i < tb_settings.size(); i++) {
  186. if (tb_settings[i]->target_node.is_empty()) {
  187. warnings.push_back(RTR("Detecting settings with no pole target set! TwoBoneIK3D must have a pole target to work."));
  188. break;
  189. }
  190. }
  191. return warnings;
  192. }
  193. // Setting.
  194. void TwoBoneIK3D::set_root_bone_name(int p_index, const String &p_bone_name) {
  195. ERR_FAIL_INDEX(p_index, (int)settings.size());
  196. tb_settings[p_index]->root_bone.name = p_bone_name;
  197. Skeleton3D *sk = get_skeleton();
  198. if (sk) {
  199. set_root_bone(p_index, sk->find_bone(tb_settings[p_index]->root_bone.name));
  200. }
  201. }
  202. String TwoBoneIK3D::get_root_bone_name(int p_index) const {
  203. ERR_FAIL_INDEX_V(p_index, (int)settings.size(), String());
  204. return tb_settings[p_index]->root_bone.name;
  205. }
  206. void TwoBoneIK3D::set_root_bone(int p_index, int p_bone) {
  207. ERR_FAIL_INDEX(p_index, (int)settings.size());
  208. bool changed = tb_settings[p_index]->root_bone.bone != p_bone;
  209. tb_settings[p_index]->root_bone.bone = p_bone;
  210. Skeleton3D *sk = get_skeleton();
  211. if (sk) {
  212. if (tb_settings[p_index]->root_bone.bone <= -1 || tb_settings[p_index]->root_bone.bone >= sk->get_bone_count()) {
  213. WARN_PRINT("Root bone index out of range!");
  214. tb_settings[p_index]->root_bone.bone = -1;
  215. } else {
  216. tb_settings[p_index]->root_bone.name = sk->get_bone_name(tb_settings[p_index]->root_bone.bone);
  217. }
  218. }
  219. if (changed) {
  220. _update_joints(p_index);
  221. }
  222. }
  223. int TwoBoneIK3D::get_root_bone(int p_index) const {
  224. ERR_FAIL_INDEX_V(p_index, (int)settings.size(), -1);
  225. return tb_settings[p_index]->root_bone.bone;
  226. }
  227. void TwoBoneIK3D::set_middle_bone_name(int p_index, const String &p_bone_name) {
  228. ERR_FAIL_INDEX(p_index, (int)settings.size());
  229. tb_settings[p_index]->middle_bone.name = p_bone_name;
  230. Skeleton3D *sk = get_skeleton();
  231. if (sk) {
  232. set_middle_bone(p_index, sk->find_bone(tb_settings[p_index]->middle_bone.name));
  233. }
  234. }
  235. String TwoBoneIK3D::get_middle_bone_name(int p_index) const {
  236. ERR_FAIL_INDEX_V(p_index, (int)settings.size(), String());
  237. return tb_settings[p_index]->middle_bone.name;
  238. }
  239. void TwoBoneIK3D::set_middle_bone(int p_index, int p_bone) {
  240. ERR_FAIL_INDEX(p_index, (int)settings.size());
  241. bool changed = tb_settings[p_index]->middle_bone.bone != p_bone;
  242. tb_settings[p_index]->middle_bone.bone = p_bone;
  243. Skeleton3D *sk = get_skeleton();
  244. if (sk) {
  245. if (tb_settings[p_index]->middle_bone.bone <= -1 || tb_settings[p_index]->middle_bone.bone >= sk->get_bone_count()) {
  246. WARN_PRINT("Middle bone index out of range!");
  247. tb_settings[p_index]->middle_bone.bone = -1;
  248. tb_settings[p_index]->use_virtual_end = false; // To sync inspector.
  249. } else {
  250. tb_settings[p_index]->middle_bone.name = sk->get_bone_name(tb_settings[p_index]->middle_bone.bone);
  251. }
  252. }
  253. if (changed) {
  254. _update_joints(p_index);
  255. }
  256. notify_property_list_changed();
  257. }
  258. int TwoBoneIK3D::get_middle_bone(int p_index) const {
  259. ERR_FAIL_INDEX_V(p_index, (int)settings.size(), -1);
  260. return tb_settings[p_index]->middle_bone.bone;
  261. }
  262. void TwoBoneIK3D::set_end_bone_name(int p_index, const String &p_bone_name) {
  263. ERR_FAIL_INDEX(p_index, (int)settings.size());
  264. tb_settings[p_index]->end_bone.name = p_bone_name;
  265. Skeleton3D *sk = get_skeleton();
  266. if (sk) {
  267. set_end_bone(p_index, sk->find_bone(tb_settings[p_index]->end_bone.name));
  268. }
  269. }
  270. String TwoBoneIK3D::get_end_bone_name(int p_index) const {
  271. ERR_FAIL_INDEX_V(p_index, (int)settings.size(), String());
  272. return tb_settings[p_index]->end_bone.name;
  273. }
  274. void TwoBoneIK3D::set_end_bone(int p_index, int p_bone) {
  275. ERR_FAIL_INDEX(p_index, (int)settings.size());
  276. bool changed = tb_settings[p_index]->end_bone.bone != p_bone;
  277. tb_settings[p_index]->end_bone.bone = p_bone;
  278. Skeleton3D *sk = get_skeleton();
  279. if (sk) {
  280. if (tb_settings[p_index]->end_bone.bone <= -1 || tb_settings[p_index]->end_bone.bone >= sk->get_bone_count()) {
  281. WARN_PRINT("End bone index out of range!");
  282. tb_settings[p_index]->end_bone.bone = -1;
  283. } else {
  284. tb_settings[p_index]->end_bone.name = sk->get_bone_name(tb_settings[p_index]->end_bone.bone);
  285. }
  286. }
  287. if (changed) {
  288. _update_joints(p_index);
  289. }
  290. notify_property_list_changed();
  291. }
  292. int TwoBoneIK3D::get_end_bone(int p_index) const {
  293. ERR_FAIL_INDEX_V(p_index, (int)settings.size(), -1);
  294. return tb_settings[p_index]->get_end_bone();
  295. }
  296. void TwoBoneIK3D::set_use_virtual_end(int p_index, bool p_enabled) {
  297. ERR_FAIL_INDEX(p_index, (int)settings.size());
  298. bool changed = tb_settings[p_index]->use_virtual_end != p_enabled;
  299. tb_settings[p_index]->use_virtual_end = p_enabled;
  300. if (p_enabled) {
  301. // To sync inspector.
  302. tb_settings[p_index]->extend_end_bone = true;
  303. }
  304. tb_settings[p_index]->simulation_dirty = true;
  305. if (changed) {
  306. _update_joints(p_index);
  307. }
  308. notify_property_list_changed();
  309. }
  310. bool TwoBoneIK3D::is_using_virtual_end(int p_index) const {
  311. ERR_FAIL_INDEX_V(p_index, (int)settings.size(), false);
  312. return tb_settings[p_index]->use_virtual_end;
  313. }
  314. void TwoBoneIK3D::set_extend_end_bone(int p_index, bool p_enabled) {
  315. ERR_FAIL_INDEX(p_index, (int)settings.size());
  316. tb_settings[p_index]->extend_end_bone = p_enabled;
  317. tb_settings[p_index]->simulation_dirty = true;
  318. Skeleton3D *sk = get_skeleton();
  319. if (sk) {
  320. _validate_pole_direction(sk, p_index);
  321. }
  322. notify_property_list_changed();
  323. #ifdef TOOLS_ENABLED
  324. _make_gizmo_dirty();
  325. #endif // TOOLS_ENABLED
  326. }
  327. bool TwoBoneIK3D::is_end_bone_extended(int p_index) const {
  328. ERR_FAIL_INDEX_V(p_index, (int)settings.size(), false);
  329. return tb_settings[p_index]->extend_end_bone;
  330. }
  331. void TwoBoneIK3D::set_end_bone_direction(int p_index, BoneDirection p_bone_direction) {
  332. ERR_FAIL_INDEX(p_index, (int)settings.size());
  333. tb_settings[p_index]->end_bone_direction = p_bone_direction;
  334. Skeleton3D *sk = get_skeleton();
  335. if (sk) {
  336. _validate_pole_direction(sk, p_index);
  337. }
  338. #ifdef TOOLS_ENABLED
  339. _make_gizmo_dirty();
  340. #endif // TOOLS_ENABLED
  341. if (mutable_bone_axes) {
  342. return; // Chain dir will be recaluclated in _update_bone_axis().
  343. }
  344. tb_settings[p_index]->simulation_dirty = true;
  345. }
  346. SkeletonModifier3D::BoneDirection TwoBoneIK3D::get_end_bone_direction(int p_index) const {
  347. ERR_FAIL_INDEX_V(p_index, (int)settings.size(), BONE_DIRECTION_FROM_PARENT);
  348. return tb_settings[p_index]->end_bone_direction;
  349. }
  350. void TwoBoneIK3D::set_end_bone_length(int p_index, float p_length) {
  351. ERR_FAIL_INDEX(p_index, (int)settings.size());
  352. float old = tb_settings[p_index]->end_bone_length;
  353. tb_settings[p_index]->end_bone_length = p_length;
  354. #ifdef TOOLS_ENABLED
  355. _make_gizmo_dirty();
  356. #endif // TOOLS_ENABLED
  357. if (mutable_bone_axes && Math::is_zero_approx(old) == Math::is_zero_approx(p_length)) {
  358. return; // If chain size is not changed, length will be recaluclated in _update_bone_axis().
  359. }
  360. tb_settings[p_index]->simulation_dirty = true;
  361. }
  362. float TwoBoneIK3D::get_end_bone_length(int p_index) const {
  363. ERR_FAIL_INDEX_V(p_index, (int)settings.size(), 0);
  364. return tb_settings[p_index]->end_bone_length;
  365. }
  366. void TwoBoneIK3D::set_target_node(int p_index, const NodePath &p_node_path) {
  367. ERR_FAIL_INDEX(p_index, (int)settings.size());
  368. tb_settings[p_index]->target_node = p_node_path;
  369. update_configuration_warnings();
  370. }
  371. NodePath TwoBoneIK3D::get_target_node(int p_index) const {
  372. ERR_FAIL_INDEX_V(p_index, (int)settings.size(), NodePath());
  373. return tb_settings[p_index]->target_node;
  374. }
  375. void TwoBoneIK3D::set_pole_node(int p_index, const NodePath &p_node_path) {
  376. ERR_FAIL_INDEX(p_index, (int)settings.size());
  377. tb_settings[p_index]->pole_node = p_node_path;
  378. update_configuration_warnings();
  379. }
  380. NodePath TwoBoneIK3D::get_pole_node(int p_index) const {
  381. ERR_FAIL_INDEX_V(p_index, (int)settings.size(), NodePath());
  382. return tb_settings[p_index]->pole_node;
  383. }
  384. void TwoBoneIK3D::set_pole_direction(int p_index, SecondaryDirection p_direction) {
  385. ERR_FAIL_INDEX(p_index, (int)settings.size());
  386. tb_settings[p_index]->pole_direction = p_direction;
  387. tb_settings[p_index]->simulation_dirty = true;
  388. Skeleton3D *sk = get_skeleton();
  389. if (sk) {
  390. _validate_pole_direction(sk, p_index);
  391. }
  392. notify_property_list_changed();
  393. #ifdef TOOLS_ENABLED
  394. _make_gizmo_dirty();
  395. #endif // TOOLS_ENABLED
  396. }
  397. SkeletonModifier3D::SecondaryDirection TwoBoneIK3D::get_pole_direction(int p_index) const {
  398. ERR_FAIL_INDEX_V(p_index, (int)settings.size(), SECONDARY_DIRECTION_NONE);
  399. return tb_settings[p_index]->pole_direction;
  400. }
  401. void TwoBoneIK3D::set_pole_direction_vector(int p_index, const Vector3 &p_vector) {
  402. ERR_FAIL_INDEX(p_index, (int)settings.size());
  403. if (tb_settings[p_index]->pole_direction != SECONDARY_DIRECTION_CUSTOM) {
  404. return;
  405. }
  406. tb_settings[p_index]->pole_direction_vector = p_vector;
  407. tb_settings[p_index]->simulation_dirty = true;
  408. Skeleton3D *sk = get_skeleton();
  409. if (sk) {
  410. _validate_pole_direction(sk, p_index);
  411. }
  412. #ifdef TOOLS_ENABLED
  413. _make_gizmo_dirty();
  414. #endif // TOOLS_ENABLED
  415. }
  416. Vector3 TwoBoneIK3D::get_pole_direction_vector(int p_index) const {
  417. ERR_FAIL_INDEX_V(p_index, (int)settings.size(), Vector3());
  418. return tb_settings[p_index]->get_pole_direction_vector();
  419. }
  420. bool TwoBoneIK3D::is_valid(int p_index) const {
  421. ERR_FAIL_INDEX_V(p_index, (int)settings.size(), false);
  422. return tb_settings[p_index]->root_bone.bone != -1 && tb_settings[p_index]->middle_bone.bone != -1 && tb_settings[p_index]->is_end_valid();
  423. }
  424. void TwoBoneIK3D::_bind_methods() {
  425. // Setting.
  426. ClassDB::bind_method(D_METHOD("set_target_node", "index", "target_node"), &TwoBoneIK3D::set_target_node);
  427. ClassDB::bind_method(D_METHOD("get_target_node", "index"), &TwoBoneIK3D::get_target_node);
  428. ClassDB::bind_method(D_METHOD("set_pole_node", "index", "pole_node"), &TwoBoneIK3D::set_pole_node);
  429. ClassDB::bind_method(D_METHOD("get_pole_node", "index"), &TwoBoneIK3D::get_pole_node);
  430. ClassDB::bind_method(D_METHOD("set_root_bone_name", "index", "bone_name"), &TwoBoneIK3D::set_root_bone_name);
  431. ClassDB::bind_method(D_METHOD("get_root_bone_name", "index"), &TwoBoneIK3D::get_root_bone_name);
  432. ClassDB::bind_method(D_METHOD("set_root_bone", "index", "bone"), &TwoBoneIK3D::set_root_bone);
  433. ClassDB::bind_method(D_METHOD("get_root_bone", "index"), &TwoBoneIK3D::get_root_bone);
  434. ClassDB::bind_method(D_METHOD("set_middle_bone_name", "index", "bone_name"), &TwoBoneIK3D::set_middle_bone_name);
  435. ClassDB::bind_method(D_METHOD("get_middle_bone_name", "index"), &TwoBoneIK3D::get_middle_bone_name);
  436. ClassDB::bind_method(D_METHOD("set_middle_bone", "index", "bone"), &TwoBoneIK3D::set_middle_bone);
  437. ClassDB::bind_method(D_METHOD("get_middle_bone", "index"), &TwoBoneIK3D::get_middle_bone);
  438. ClassDB::bind_method(D_METHOD("set_pole_direction", "index", "direction"), &TwoBoneIK3D::set_pole_direction);
  439. ClassDB::bind_method(D_METHOD("get_pole_direction", "index"), &TwoBoneIK3D::get_pole_direction);
  440. ClassDB::bind_method(D_METHOD("set_pole_direction_vector", "index", "vector"), &TwoBoneIK3D::set_pole_direction_vector);
  441. ClassDB::bind_method(D_METHOD("get_pole_direction_vector", "index"), &TwoBoneIK3D::get_pole_direction_vector);
  442. ClassDB::bind_method(D_METHOD("set_end_bone_name", "index", "bone_name"), &TwoBoneIK3D::set_end_bone_name);
  443. ClassDB::bind_method(D_METHOD("get_end_bone_name", "index"), &TwoBoneIK3D::get_end_bone_name);
  444. ClassDB::bind_method(D_METHOD("set_end_bone", "index", "bone"), &TwoBoneIK3D::set_end_bone);
  445. ClassDB::bind_method(D_METHOD("get_end_bone", "index"), &TwoBoneIK3D::get_end_bone);
  446. ClassDB::bind_method(D_METHOD("set_use_virtual_end", "index", "enabled"), &TwoBoneIK3D::set_use_virtual_end);
  447. ClassDB::bind_method(D_METHOD("is_using_virtual_end", "index"), &TwoBoneIK3D::is_using_virtual_end);
  448. ClassDB::bind_method(D_METHOD("set_extend_end_bone", "index", "enabled"), &TwoBoneIK3D::set_extend_end_bone);
  449. ClassDB::bind_method(D_METHOD("is_end_bone_extended", "index"), &TwoBoneIK3D::is_end_bone_extended);
  450. ClassDB::bind_method(D_METHOD("set_end_bone_direction", "index", "bone_direction"), &TwoBoneIK3D::set_end_bone_direction);
  451. ClassDB::bind_method(D_METHOD("get_end_bone_direction", "index"), &TwoBoneIK3D::get_end_bone_direction);
  452. ClassDB::bind_method(D_METHOD("set_end_bone_length", "index", "length"), &TwoBoneIK3D::set_end_bone_length);
  453. ClassDB::bind_method(D_METHOD("get_end_bone_length", "index"), &TwoBoneIK3D::get_end_bone_length);
  454. ADD_ARRAY_COUNT("Settings", "setting_count", "set_setting_count", "get_setting_count", "settings/");
  455. }
  456. void TwoBoneIK3D::_validate_bone_names() {
  457. for (uint32_t i = 0; i < settings.size(); i++) {
  458. // Prior bone name.
  459. if (!tb_settings[i]->root_bone.name.is_empty()) {
  460. set_root_bone_name(i, tb_settings[i]->root_bone.name);
  461. } else if (tb_settings[i]->root_bone.bone != -1) {
  462. set_root_bone(i, tb_settings[i]->root_bone.bone);
  463. }
  464. // Prior bone name.
  465. if (!tb_settings[i]->middle_bone.name.is_empty()) {
  466. set_middle_bone_name(i, tb_settings[i]->middle_bone.name);
  467. } else if (tb_settings[i]->middle_bone.bone != -1) {
  468. set_middle_bone(i, tb_settings[i]->middle_bone.bone);
  469. }
  470. // Prior bone name.
  471. if (!tb_settings[i]->end_bone.name.is_empty()) {
  472. set_end_bone_name(i, tb_settings[i]->end_bone.name);
  473. } else if (tb_settings[i]->end_bone.bone != -1) {
  474. set_end_bone(i, tb_settings[i]->end_bone.bone);
  475. }
  476. }
  477. }
  478. void TwoBoneIK3D::_validate_pole_directions(Skeleton3D *p_skeleton) const {
  479. for (uint32_t i = 0; i < settings.size(); i++) {
  480. _validate_pole_direction(p_skeleton, i);
  481. }
  482. }
  483. void TwoBoneIK3D::_validate_pole_direction(Skeleton3D *p_skeleton, int p_index) const {
  484. TwoBoneIK3DSetting *setting = tb_settings[p_index];
  485. SecondaryDirection dir = setting->pole_direction;
  486. if (!is_valid(p_index) || dir == SECONDARY_DIRECTION_NONE) {
  487. return;
  488. }
  489. Vector3 kv = get_pole_direction_vector(p_index).normalized();
  490. Vector3 fwd;
  491. // End bone.
  492. int valid_end_bone = setting->get_end_bone();
  493. Vector3 axis = IKModifier3D::get_bone_axis(p_skeleton, valid_end_bone, setting->end_bone_direction, mutable_bone_axes);
  494. Vector3 global_rest_origin;
  495. if (setting->extend_end_bone && setting->end_bone_length > 0 && !axis.is_zero_approx()) {
  496. global_rest_origin = p_skeleton->get_bone_global_rest(valid_end_bone).xform(axis * setting->end_bone_length);
  497. } else {
  498. // Shouldn't be using virtual end.
  499. global_rest_origin = p_skeleton->get_bone_global_rest(valid_end_bone).origin;
  500. }
  501. // Middle bone.
  502. axis = global_rest_origin - p_skeleton->get_bone_global_rest(setting->middle_bone.bone).origin;
  503. if (!axis.is_zero_approx()) {
  504. fwd = p_skeleton->get_bone_global_rest(setting->middle_bone.bone).basis.get_rotation_quaternion().xform_inv(axis).normalized();
  505. } else {
  506. return;
  507. }
  508. if (Math::is_equal_approx(Math::abs(kv.dot(fwd)), 1)) {
  509. WARN_PRINT_ED("Setting: " + itos(p_index) + ": Pole direction and forward vector are colinear. This is not advised as it may cause unwanted rotation.");
  510. }
  511. }
  512. void TwoBoneIK3D::_make_all_joints_dirty() {
  513. for (uint32_t i = 0; i < settings.size(); i++) {
  514. _update_joints(i);
  515. }
  516. }
  517. void TwoBoneIK3D::_init_joints(Skeleton3D *p_skeleton, int p_index) {
  518. TwoBoneIK3DSetting *setting = tb_settings[p_index];
  519. cached_space = p_skeleton->get_global_transform_interpolated();
  520. if (!setting->simulation_dirty) {
  521. if (mutable_bone_axes) {
  522. _update_bone_axis(p_skeleton, p_index);
  523. }
  524. return;
  525. }
  526. _clear_joints(p_index);
  527. if (setting->root_bone.bone == -1 || setting->middle_bone.bone == -1 || !setting->is_end_valid()) {
  528. return;
  529. }
  530. _update_bone_axis(p_skeleton, p_index);
  531. setting->simulation_dirty = false;
  532. }
  533. void TwoBoneIK3D::_clear_joints(int p_index) {
  534. TwoBoneIK3DSetting *setting = tb_settings[p_index];
  535. if (!setting) {
  536. return;
  537. }
  538. setting->root_pos = Vector3();
  539. setting->mid_pos = Vector3();
  540. setting->end_pos = Vector3();
  541. if (setting->root_joint_solver_info) {
  542. memdelete(setting->root_joint_solver_info);
  543. setting->root_joint_solver_info = nullptr;
  544. }
  545. if (setting->mid_joint_solver_info) {
  546. memdelete(setting->mid_joint_solver_info);
  547. setting->mid_joint_solver_info = nullptr;
  548. }
  549. }
  550. void TwoBoneIK3D::_update_bone_axis(Skeleton3D *p_skeleton, int p_index) {
  551. TwoBoneIK3DSetting *setting = tb_settings[p_index];
  552. if (!setting) {
  553. return;
  554. }
  555. if (!setting->mid_joint_solver_info) {
  556. setting->mid_joint_solver_info = memnew(IKModifier3DSolverInfo);
  557. }
  558. if (!setting->root_joint_solver_info) {
  559. setting->root_joint_solver_info = memnew(IKModifier3DSolverInfo);
  560. }
  561. #ifdef TOOLS_ENABLED
  562. Vector3 old_mv = setting->mid_joint_solver_info->forward_vector;
  563. float old_ml = setting->mid_joint_solver_info->length;
  564. Vector3 old_rv = setting->root_joint_solver_info->forward_vector;
  565. float old_rl = setting->root_joint_solver_info->length;
  566. #endif // TOOLS_ENABLED
  567. // End bone.
  568. int valid_end_bone = setting->get_end_bone();
  569. Vector3 axis = IKModifier3D::get_bone_axis(p_skeleton, valid_end_bone, setting->end_bone_direction, mutable_bone_axes);
  570. Vector3 global_rest_origin;
  571. if (setting->extend_end_bone && setting->end_bone_length > 0 && !axis.is_zero_approx()) {
  572. setting->end_pos = p_skeleton->get_bone_global_pose(valid_end_bone).xform(axis * setting->end_bone_length);
  573. global_rest_origin = _get_bone_global_rest(p_skeleton, valid_end_bone, setting->root_bone.bone).xform(axis * setting->end_bone_length);
  574. } else {
  575. // Shouldn't be using virtual end.
  576. setting->end_pos = p_skeleton->get_bone_global_pose(valid_end_bone).origin;
  577. global_rest_origin = _get_bone_global_rest(p_skeleton, valid_end_bone, setting->root_bone.bone).origin;
  578. }
  579. // Middle bone.
  580. Vector3 orig = _get_bone_global_rest(p_skeleton, setting->middle_bone.bone, setting->root_bone.bone).origin;
  581. axis = global_rest_origin - orig;
  582. global_rest_origin = orig;
  583. if (!axis.is_zero_approx()) {
  584. setting->mid_pos = p_skeleton->get_bone_global_pose(setting->middle_bone.bone).origin;
  585. setting->mid_joint_solver_info->forward_vector = p_skeleton->get_bone_global_rest(setting->middle_bone.bone).basis.get_rotation_quaternion().xform_inv(axis).normalized();
  586. setting->mid_joint_solver_info->length = axis.length();
  587. } else {
  588. _clear_joints(p_index);
  589. return;
  590. }
  591. // Root bone.
  592. orig = _get_bone_global_rest(p_skeleton, setting->root_bone.bone, setting->root_bone.bone).origin;
  593. axis = global_rest_origin - orig;
  594. if (!axis.is_zero_approx()) {
  595. setting->root_pos = p_skeleton->get_bone_global_pose(setting->root_bone.bone).origin;
  596. setting->root_joint_solver_info->forward_vector = p_skeleton->get_bone_global_rest(setting->root_bone.bone).basis.get_rotation_quaternion().xform_inv(axis).normalized();
  597. setting->root_joint_solver_info->length = axis.length();
  598. } else {
  599. _clear_joints(p_index);
  600. return;
  601. }
  602. setting->init_current_joint_rotations(p_skeleton);
  603. double total_length = setting->root_joint_solver_info->length + setting->mid_joint_solver_info->length;
  604. setting->cached_length_sq = total_length * total_length;
  605. #ifdef TOOLS_ENABLED
  606. if (!Math::is_equal_approx(old_ml, setting->mid_joint_solver_info->length) || !Math::is_equal_approx(old_rl, setting->root_joint_solver_info->length) || !old_mv.is_equal_approx(setting->mid_joint_solver_info->forward_vector) || !old_rv.is_equal_approx(setting->root_joint_solver_info->forward_vector)) {
  607. _make_gizmo_dirty();
  608. }
  609. #endif // TOOLS_ENABLED
  610. }
  611. void TwoBoneIK3D::_update_joints(int p_index) {
  612. tb_settings[p_index]->simulation_dirty = true;
  613. #ifdef TOOLS_ENABLED
  614. _make_gizmo_dirty(); // To clear invalid setting.
  615. #endif // TOOLS_ENABLED
  616. Skeleton3D *sk = get_skeleton();
  617. if (!sk || tb_settings[p_index]->root_bone.bone == -1 || tb_settings[p_index]->middle_bone.bone == -1 || !tb_settings[p_index]->is_end_valid()) {
  618. return;
  619. }
  620. // Validation for middle bone.
  621. int parent_bone = tb_settings[p_index]->root_bone.bone;
  622. int current_bone = tb_settings[p_index]->middle_bone.bone;
  623. bool valid = false;
  624. while (current_bone >= 0) {
  625. if (current_bone == parent_bone) {
  626. valid = true;
  627. break;
  628. }
  629. current_bone = sk->get_bone_parent(current_bone);
  630. }
  631. ERR_FAIL_COND_EDMSG(!valid, "The middle bone must be a child of the root bone.");
  632. // Validation for end bone.
  633. if (!tb_settings[p_index]->use_virtual_end) {
  634. parent_bone = tb_settings[p_index]->middle_bone.bone;
  635. current_bone = tb_settings[p_index]->end_bone.bone;
  636. valid = false;
  637. while (current_bone >= 0) {
  638. if (current_bone == parent_bone) {
  639. valid = true;
  640. break;
  641. }
  642. current_bone = sk->get_bone_parent(current_bone);
  643. }
  644. ERR_FAIL_COND_EDMSG(!valid, "The end bone must be a child of the middle bone.");
  645. }
  646. if (sk) {
  647. _validate_pole_directions(sk);
  648. }
  649. if (mutable_bone_axes) {
  650. _update_bone_axis(sk, p_index);
  651. #ifdef TOOLS_ENABLED
  652. } else {
  653. _make_gizmo_dirty();
  654. #endif // TOOLS_ENABLED
  655. }
  656. }
  657. Transform3D TwoBoneIK3D::_get_bone_global_rest(Skeleton3D *p_skeleton, int p_bone, int p_root) const {
  658. if (!mutable_bone_axes) {
  659. return p_skeleton->get_bone_global_rest(p_bone);
  660. }
  661. Transform3D tr = p_skeleton->get_bone_global_rest(p_root);
  662. tr.origin = tr.origin - p_skeleton->get_bone_rest(p_root).origin + p_skeleton->get_bone_pose(p_root).origin;
  663. LocalVector<int> path;
  664. for (int bone = p_bone; bone != p_root && bone != -1; bone = p_skeleton->get_bone_parent(bone)) {
  665. path.push_back(bone);
  666. }
  667. path.reverse();
  668. for (int bone : path) {
  669. tr = tr * Transform3D(p_skeleton->get_bone_rest(bone).basis, p_skeleton->get_bone_pose(bone).origin);
  670. }
  671. return tr;
  672. }
  673. void TwoBoneIK3D::_make_simulation_dirty(int p_index) {
  674. TwoBoneIK3DSetting *setting = tb_settings[p_index];
  675. if (!setting) {
  676. return;
  677. }
  678. setting->simulation_dirty = true;
  679. }
  680. void TwoBoneIK3D::_process_ik(Skeleton3D *p_skeleton, double p_delta) {
  681. for (uint32_t i = 0; i < settings.size(); i++) {
  682. _init_joints(p_skeleton, i);
  683. Node3D *target = Object::cast_to<Node3D>(get_node_or_null(tb_settings[i]->target_node));
  684. Node3D *pole = Object::cast_to<Node3D>(get_node_or_null(tb_settings[i]->pole_node));
  685. if (!target || !pole || !tb_settings[i]->is_valid()) {
  686. continue; // Abort.
  687. }
  688. Vector3 destination = cached_space.affine_inverse().xform(target->get_global_transform_interpolated().origin);
  689. Vector3 pole_destination = cached_space.affine_inverse().xform(pole->get_global_transform_interpolated().origin);
  690. tb_settings[i]->cache_current_joint_rotations(p_skeleton, pole_destination); // Iterate over first to detect parent (outside of the chain) bone pose changes.
  691. _process_joints(p_delta, p_skeleton, tb_settings[i], destination, pole_destination);
  692. }
  693. }
  694. void TwoBoneIK3D::_process_joints(double p_delta, Skeleton3D *p_skeleton, TwoBoneIK3DSetting *p_setting, const Vector3 &p_destination, const Vector3 &p_pole_destination) {
  695. Vector3 destination = p_destination;
  696. // Make vector from root to destination.
  697. p_setting->root_pos = p_skeleton->get_bone_global_pose(p_setting->root_bone.bone).origin; // New root position.
  698. Vector3 root_to_destination = destination - p_setting->root_pos;
  699. if (root_to_destination.is_zero_approx()) {
  700. return; // Abort.
  701. }
  702. double rd_len_sq = root_to_destination.length_squared();
  703. // Compare the distance to the target with the length of the bones.
  704. if (rd_len_sq >= p_setting->cached_length_sq) {
  705. // Result is straight.
  706. Vector3 rd_nrm = root_to_destination.normalized();
  707. p_setting->mid_pos = p_setting->root_pos + rd_nrm * p_setting->root_joint_solver_info->length;
  708. p_setting->end_pos = p_setting->mid_pos + rd_nrm * p_setting->mid_joint_solver_info->length;
  709. } else {
  710. // Check if the target can be reached by subtracting the lengths of the bones.
  711. // If not, push out target to normal of the root bone sphere.
  712. double sub = p_setting->root_joint_solver_info->length - p_setting->mid_joint_solver_info->length;
  713. if (rd_len_sq < sub * sub) {
  714. Vector3 push_nrm = (destination - p_setting->root_pos).normalized();
  715. destination = p_setting->root_pos + push_nrm * Math::abs(sub);
  716. root_to_destination = destination - p_setting->root_pos;
  717. }
  718. // End is snapped to the target.
  719. p_setting->end_pos = destination;
  720. // Result is bent, determine the mid position to respect the pole target.
  721. // Mid-position should be a point of intersection of two circles.
  722. double l_chain = root_to_destination.length();
  723. Vector3 u = root_to_destination.normalized();
  724. Vector3 pole_vec = get_projected_normal(p_setting->root_pos, p_setting->end_pos, p_pole_destination);
  725. // Circle1: center is the root, radius is the length of the root bone.
  726. double r_root = p_setting->root_joint_solver_info->length;
  727. // Circle2: center is the target, radius is the length of the middle bone.
  728. double r_mid = p_setting->mid_joint_solver_info->length;
  729. double a = (l_chain * l_chain + r_root * r_root - r_mid * r_mid) / (2.0 * l_chain);
  730. double h2 = r_root * r_root - a * a;
  731. if (h2 < 0) {
  732. h2 = 0;
  733. }
  734. double h = Math::sqrt(h2);
  735. Vector3 det_plus = (p_setting->root_pos + u * a) + pole_vec * h;
  736. Vector3 det_minus = (p_setting->root_pos + u * a) - pole_vec * h;
  737. // Pick the intersection that is closest to the pole target.
  738. p_setting->mid_pos = p_pole_destination.distance_squared_to(det_plus) < p_pole_destination.distance_squared_to(det_minus) ? det_plus : det_minus;
  739. }
  740. // Update virtual bone rest/poses.
  741. p_setting->cache_current_vectors(p_skeleton);
  742. p_setting->cache_current_joint_rotations(p_skeleton, p_pole_destination);
  743. // Apply the virtual bone rest/poses to the actual bones.
  744. p_skeleton->set_bone_pose_rotation(p_setting->root_bone.bone, p_setting->root_joint_solver_info->current_lpose);
  745. // Mid joint pose is relative to the root joint pose for the case root-mid or mid-end have more than 1 joints.
  746. p_skeleton->set_bone_pose_rotation(p_setting->middle_bone.bone, get_local_pose_rotation(p_skeleton, p_setting->middle_bone.bone, p_setting->mid_joint_solver_info->current_gpose));
  747. }
  748. #ifdef TOOLS_ENABLED
  749. Vector3 TwoBoneIK3D::get_root_bone_vector(int p_index) const {
  750. Skeleton3D *skeleton = get_skeleton();
  751. if (!skeleton) {
  752. return Vector3();
  753. }
  754. ERR_FAIL_INDEX_V(p_index, (int)settings.size(), Vector3());
  755. TwoBoneIK3DSetting *setting = tb_settings[p_index];
  756. if (!setting) {
  757. return Vector3();
  758. }
  759. if (!setting->root_joint_solver_info) {
  760. return _get_bone_global_rest(skeleton, setting->middle_bone.bone, setting->root_bone.bone).origin - _get_bone_global_rest(skeleton, setting->root_bone.bone, setting->root_bone.bone).origin;
  761. }
  762. return setting->root_joint_solver_info->forward_vector * setting->root_joint_solver_info->length;
  763. }
  764. Vector3 TwoBoneIK3D::get_middle_bone_vector(int p_index) const {
  765. Skeleton3D *skeleton = get_skeleton();
  766. if (!skeleton) {
  767. return Vector3();
  768. }
  769. ERR_FAIL_INDEX_V(p_index, (int)settings.size(), Vector3());
  770. TwoBoneIK3DSetting *setting = tb_settings[p_index];
  771. if (!setting) {
  772. return Vector3();
  773. }
  774. if (!setting->mid_joint_solver_info) {
  775. int valid_end_bone = setting->get_end_bone();
  776. Vector3 axis = IKModifier3D::get_bone_axis(skeleton, valid_end_bone, setting->end_bone_direction, mutable_bone_axes);
  777. Vector3 global_rest_origin;
  778. if (setting->extend_end_bone && setting->end_bone_length > 0 && !axis.is_zero_approx()) {
  779. global_rest_origin = _get_bone_global_rest(skeleton, valid_end_bone, setting->root_bone.bone).xform(axis * setting->end_bone_length);
  780. } else {
  781. // Shouldn't be using virtual end.
  782. global_rest_origin = _get_bone_global_rest(skeleton, valid_end_bone, setting->root_bone.bone).origin;
  783. }
  784. return global_rest_origin - _get_bone_global_rest(skeleton, setting->middle_bone.bone, setting->root_bone.bone).origin;
  785. }
  786. return setting->mid_joint_solver_info->forward_vector * setting->mid_joint_solver_info->length;
  787. }
  788. #endif // TOOLS_ENABLED
  789. TwoBoneIK3D::~TwoBoneIK3D() {
  790. clear_settings();
  791. }