limit_angular_velocity_modifier_3d.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /**************************************************************************/
  2. /* limit_angular_velocity_modifier_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 "limit_angular_velocity_modifier_3d.h"
  31. bool LimitAngularVelocityModifier3D::_set(const StringName &p_path, const Variant &p_value) {
  32. String path = p_path;
  33. if (path.begins_with("chains/")) {
  34. int which = path.get_slicec('/', 1).to_int();
  35. String what = path.get_slicec('/', 2);
  36. ERR_FAIL_INDEX_V(which, (int)chains.size(), false);
  37. if (what == "root_bone_name") {
  38. set_root_bone_name(which, p_value);
  39. } else if (what == "root_bone") {
  40. set_root_bone(which, p_value);
  41. } else if (what == "end_bone_name") {
  42. set_end_bone_name(which, p_value);
  43. } else if (what == "end_bone") {
  44. set_end_bone(which, p_value);
  45. } else {
  46. return false;
  47. }
  48. }
  49. return true;
  50. }
  51. bool LimitAngularVelocityModifier3D::_get(const StringName &p_path, Variant &r_ret) const {
  52. String path = p_path;
  53. if (path.begins_with("chains/")) {
  54. int which = path.get_slicec('/', 1).to_int();
  55. ERR_FAIL_INDEX_V(which, (int)chains.size(), false);
  56. String what = path.get_slicec('/', 2);
  57. if (what == "root_bone_name") {
  58. r_ret = get_root_bone_name(which);
  59. } else if (what == "root_bone") {
  60. r_ret = get_root_bone(which);
  61. } else if (what == "end_bone_name") {
  62. r_ret = get_end_bone_name(which);
  63. } else if (what == "end_bone") {
  64. r_ret = get_end_bone(which);
  65. } else {
  66. return false;
  67. }
  68. }
  69. if (path.begins_with("joints/")) {
  70. int which = path.get_slicec('/', 1).to_int();
  71. ERR_FAIL_INDEX_V(which, (int)joints.size(), false);
  72. String what = path.get_slicec('/', 2);
  73. if (what == "bone_name") {
  74. r_ret = get_joint_bone_name(which);
  75. } else if (what == "bone") {
  76. r_ret = get_joint_bone(which);
  77. } else {
  78. return false;
  79. }
  80. }
  81. return true;
  82. }
  83. void LimitAngularVelocityModifier3D::_get_property_list(List<PropertyInfo> *p_list) const {
  84. String enum_hint;
  85. Skeleton3D *skeleton = get_skeleton();
  86. if (skeleton) {
  87. enum_hint = skeleton->get_concatenated_bone_names();
  88. }
  89. for (uint32_t i = 0; i < chains.size(); i++) {
  90. String path = "chains/" + itos(i) + "/";
  91. p_list->push_back(PropertyInfo(Variant::STRING, path + "root_bone_name", PROPERTY_HINT_ENUM_SUGGESTION, enum_hint));
  92. p_list->push_back(PropertyInfo(Variant::INT, path + "root_bone", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  93. p_list->push_back(PropertyInfo(Variant::STRING, path + "end_bone_name", PROPERTY_HINT_ENUM_SUGGESTION, enum_hint));
  94. p_list->push_back(PropertyInfo(Variant::INT, path + "end_bone", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  95. }
  96. for (uint32_t i = 0; i < joints.size(); i++) {
  97. String path = "joints/" + itos(i) + "/";
  98. p_list->push_back(PropertyInfo(Variant::STRING, path + "bone_name", PROPERTY_HINT_ENUM_SUGGESTION, enum_hint, PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_READ_ONLY));
  99. p_list->push_back(PropertyInfo(Variant::INT, path + "bone", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_READ_ONLY));
  100. }
  101. }
  102. void LimitAngularVelocityModifier3D::_validate_property(PropertyInfo &p_property) const {
  103. if (p_property.name == "joint_count") {
  104. p_property.usage = PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_ARRAY | PROPERTY_USAGE_READ_ONLY;
  105. p_property.class_name = "Joints,joints/,static,const";
  106. }
  107. }
  108. void LimitAngularVelocityModifier3D::_notification(int p_what) {
  109. switch (p_what) {
  110. case NOTIFICATION_ENTER_TREE: {
  111. _make_joints_dirty();
  112. } break;
  113. }
  114. }
  115. // Setting.
  116. void LimitAngularVelocityModifier3D::set_root_bone_name(int p_index, const String &p_bone_name) {
  117. ERR_FAIL_INDEX(p_index, (int)chains.size());
  118. chains[p_index].root_bone.name = p_bone_name;
  119. Skeleton3D *sk = get_skeleton();
  120. if (sk) {
  121. set_root_bone(p_index, sk->find_bone(chains[p_index].root_bone.name));
  122. }
  123. }
  124. String LimitAngularVelocityModifier3D::get_root_bone_name(int p_index) const {
  125. ERR_FAIL_INDEX_V(p_index, (int)chains.size(), String());
  126. return chains[p_index].root_bone.name;
  127. }
  128. void LimitAngularVelocityModifier3D::set_root_bone(int p_index, int p_bone) {
  129. ERR_FAIL_INDEX(p_index, (int)chains.size());
  130. bool changed = chains[p_index].root_bone.bone != p_bone;
  131. chains[p_index].root_bone.bone = p_bone;
  132. Skeleton3D *sk = get_skeleton();
  133. if (sk) {
  134. if (chains[p_index].root_bone.bone <= -1 || chains[p_index].root_bone.bone >= sk->get_bone_count()) {
  135. WARN_PRINT("Root bone index out of range!");
  136. chains[p_index].root_bone.bone = -1;
  137. } else {
  138. chains[p_index].root_bone.name = sk->get_bone_name(chains[p_index].root_bone.bone);
  139. }
  140. }
  141. if (changed) {
  142. _make_joints_dirty();
  143. }
  144. }
  145. int LimitAngularVelocityModifier3D::get_root_bone(int p_index) const {
  146. ERR_FAIL_INDEX_V(p_index, (int)chains.size(), -1);
  147. return chains[p_index].root_bone.bone;
  148. }
  149. void LimitAngularVelocityModifier3D::set_end_bone_name(int p_index, const String &p_bone_name) {
  150. ERR_FAIL_INDEX(p_index, (int)chains.size());
  151. chains[p_index].end_bone.name = p_bone_name;
  152. Skeleton3D *sk = get_skeleton();
  153. if (sk) {
  154. set_end_bone(p_index, sk->find_bone(chains[p_index].end_bone.name));
  155. }
  156. }
  157. String LimitAngularVelocityModifier3D::get_end_bone_name(int p_index) const {
  158. ERR_FAIL_INDEX_V(p_index, (int)chains.size(), String());
  159. return chains[p_index].end_bone.name;
  160. }
  161. void LimitAngularVelocityModifier3D::set_end_bone(int p_index, int p_bone) {
  162. ERR_FAIL_INDEX(p_index, (int)chains.size());
  163. bool changed = chains[p_index].end_bone.bone != p_bone;
  164. chains[p_index].end_bone.bone = p_bone;
  165. Skeleton3D *sk = get_skeleton();
  166. if (sk) {
  167. if (chains[p_index].end_bone.bone <= -1 || chains[p_index].end_bone.bone >= sk->get_bone_count()) {
  168. WARN_PRINT("End bone index out of range!");
  169. chains[p_index].end_bone.bone = -1;
  170. } else {
  171. chains[p_index].end_bone.name = sk->get_bone_name(chains[p_index].end_bone.bone);
  172. }
  173. }
  174. if (changed) {
  175. _make_joints_dirty();
  176. }
  177. }
  178. int LimitAngularVelocityModifier3D::get_end_bone(int p_index) const {
  179. ERR_FAIL_INDEX_V(p_index, (int)chains.size(), -1);
  180. return chains[p_index].end_bone.bone;
  181. }
  182. void LimitAngularVelocityModifier3D::set_chain_count(int p_count) {
  183. ERR_FAIL_COND(p_count < 0);
  184. chains.resize(p_count);
  185. _make_joints_dirty();
  186. }
  187. int LimitAngularVelocityModifier3D::get_chain_count() const {
  188. return chains.size();
  189. }
  190. void LimitAngularVelocityModifier3D::clear_chains() {
  191. set_chain_count(0);
  192. }
  193. String LimitAngularVelocityModifier3D::get_joint_bone_name(int p_index) const {
  194. ERR_FAIL_INDEX_V(p_index, (int)joints.size(), String());
  195. return joints[p_index].name;
  196. }
  197. int LimitAngularVelocityModifier3D::get_joint_bone(int p_index) const {
  198. ERR_FAIL_INDEX_V(p_index, (int)joints.size(), -1);
  199. return joints[p_index].bone;
  200. }
  201. int LimitAngularVelocityModifier3D::_get_joint_count() const {
  202. return joints.size();
  203. }
  204. void LimitAngularVelocityModifier3D::set_max_angular_velocity(double p_angular_velocity) {
  205. max_angular_velocity = p_angular_velocity;
  206. }
  207. double LimitAngularVelocityModifier3D::get_max_angular_velocity() const {
  208. return max_angular_velocity;
  209. }
  210. void LimitAngularVelocityModifier3D::set_exclude(bool p_exclude) {
  211. exclude = p_exclude;
  212. }
  213. bool LimitAngularVelocityModifier3D::is_exclude() const {
  214. return exclude;
  215. }
  216. void LimitAngularVelocityModifier3D::_bind_methods() {
  217. // Setting.
  218. ClassDB::bind_method(D_METHOD("set_root_bone_name", "index", "bone_name"), &LimitAngularVelocityModifier3D::set_root_bone_name);
  219. ClassDB::bind_method(D_METHOD("get_root_bone_name", "index"), &LimitAngularVelocityModifier3D::get_root_bone_name);
  220. ClassDB::bind_method(D_METHOD("set_root_bone", "index", "bone"), &LimitAngularVelocityModifier3D::set_root_bone);
  221. ClassDB::bind_method(D_METHOD("get_root_bone", "index"), &LimitAngularVelocityModifier3D::get_root_bone);
  222. ClassDB::bind_method(D_METHOD("set_end_bone_name", "index", "bone_name"), &LimitAngularVelocityModifier3D::set_end_bone_name);
  223. ClassDB::bind_method(D_METHOD("get_end_bone_name", "index"), &LimitAngularVelocityModifier3D::get_end_bone_name);
  224. ClassDB::bind_method(D_METHOD("set_end_bone", "index", "bone"), &LimitAngularVelocityModifier3D::set_end_bone);
  225. ClassDB::bind_method(D_METHOD("get_end_bone", "index"), &LimitAngularVelocityModifier3D::get_end_bone);
  226. ClassDB::bind_method(D_METHOD("set_chain_count", "count"), &LimitAngularVelocityModifier3D::set_chain_count);
  227. ClassDB::bind_method(D_METHOD("get_chain_count"), &LimitAngularVelocityModifier3D::get_chain_count);
  228. ClassDB::bind_method(D_METHOD("clear_chains"), &LimitAngularVelocityModifier3D::clear_chains);
  229. ClassDB::bind_method(D_METHOD("set_max_angular_velocity", "angular_velocity"), &LimitAngularVelocityModifier3D::set_max_angular_velocity);
  230. ClassDB::bind_method(D_METHOD("get_max_angular_velocity"), &LimitAngularVelocityModifier3D::get_max_angular_velocity);
  231. ClassDB::bind_method(D_METHOD("set_exclude", "exclude"), &LimitAngularVelocityModifier3D::set_exclude);
  232. ClassDB::bind_method(D_METHOD("is_exclude"), &LimitAngularVelocityModifier3D::is_exclude);
  233. ClassDB::bind_method(D_METHOD("reset"), &LimitAngularVelocityModifier3D::reset);
  234. ClassDB::bind_method(D_METHOD("_get_joint_count"), &LimitAngularVelocityModifier3D::_get_joint_count);
  235. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "max_angular_velocity", PROPERTY_HINT_RANGE, "0,720,or_greater,radians_as_degrees,suffix:" + String(U"°") + "/s"), "set_max_angular_velocity", "get_max_angular_velocity");
  236. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "exclude"), "set_exclude", "is_exclude");
  237. ADD_ARRAY_COUNT("Chains", "chain_count", "set_chain_count", "get_chain_count", "chains/");
  238. ADD_ARRAY_COUNT("Joints", "joint_count", "", "_get_joint_count", "joints/");
  239. }
  240. void LimitAngularVelocityModifier3D::_set_active(bool p_active) {
  241. if (p_active) {
  242. reset();
  243. }
  244. }
  245. void LimitAngularVelocityModifier3D::_skeleton_changed(Skeleton3D *p_old, Skeleton3D *p_new) {
  246. _make_joints_dirty();
  247. }
  248. void LimitAngularVelocityModifier3D::_validate_bone_names() {
  249. for (uint32_t i = 0; i < chains.size(); i++) {
  250. // Prior bone name.
  251. if (!chains[i].root_bone.name.is_empty()) {
  252. set_root_bone_name(i, chains[i].root_bone.name);
  253. } else if (chains[i].root_bone.bone != -1) {
  254. set_root_bone(i, chains[i].root_bone.bone);
  255. }
  256. // Prior bone name.
  257. if (!chains[i].end_bone.name.is_empty()) {
  258. set_end_bone_name(i, chains[i].end_bone.name);
  259. } else if (chains[i].end_bone.bone != -1) {
  260. set_end_bone(i, chains[i].end_bone.bone);
  261. }
  262. }
  263. }
  264. void LimitAngularVelocityModifier3D::_make_joints_dirty() {
  265. if (joints_dirty) {
  266. return;
  267. }
  268. joints_dirty = true;
  269. callable_mp(this, &LimitAngularVelocityModifier3D::_update_joints).call_deferred();
  270. }
  271. bool LimitAngularVelocityModifier3D::_is_joint_contained(int p_bone) {
  272. bool ret = false;
  273. for (const BoneJoint &joint : joints) {
  274. if (joint.bone == p_bone) {
  275. ret = true;
  276. break;
  277. }
  278. }
  279. return ret;
  280. }
  281. void LimitAngularVelocityModifier3D::_update_joints() {
  282. joints.clear();
  283. bones.clear();
  284. Skeleton3D *sk = get_skeleton();
  285. if (!sk) {
  286. joints_dirty = false;
  287. notify_property_list_changed();
  288. return;
  289. }
  290. LocalVector<int> tmp_joints;
  291. for (uint32_t i = 0; i < chains.size(); i++) {
  292. tmp_joints.clear();
  293. Chain cn = chains[i];
  294. int current_bone = cn.end_bone.bone;
  295. int root_bone = cn.root_bone.bone;
  296. if (current_bone < 0 || root_bone < 0) {
  297. continue;
  298. }
  299. // Validation.
  300. bool valid = false;
  301. while (current_bone >= 0) {
  302. if (current_bone == root_bone) {
  303. valid = true;
  304. break;
  305. }
  306. current_bone = sk->get_bone_parent(current_bone);
  307. }
  308. if (!valid) {
  309. ERR_PRINT_ED("Chains[" + itos(i) + "]: End bone must be the same as or a child of root bone.");
  310. continue;
  311. }
  312. current_bone = cn.end_bone.bone;
  313. while (current_bone != root_bone) {
  314. tmp_joints.push_back(current_bone);
  315. current_bone = sk->get_bone_parent(current_bone);
  316. }
  317. tmp_joints.push_back(current_bone);
  318. tmp_joints.reverse();
  319. for (uint32_t j = 0; j < tmp_joints.size(); j++) {
  320. int bn = tmp_joints[j];
  321. if (!_is_joint_contained(bn)) {
  322. BoneJoint bj;
  323. bj.bone = bn;
  324. bj.name = sk->get_bone_name(bn);
  325. joints.push_back(bj);
  326. }
  327. }
  328. }
  329. if (exclude) {
  330. for (int b = 0; b < sk->get_bone_count(); b++) {
  331. if (_is_joint_contained(b)) {
  332. continue;
  333. }
  334. BoneRot br;
  335. br.first = b;
  336. br.second = sk->get_bone_pose_rotation(b);
  337. bones.push_back(br);
  338. }
  339. } else {
  340. for (const BoneJoint &E : joints) {
  341. BoneRot br;
  342. br.first = E.bone;
  343. br.second = sk->get_bone_pose_rotation(E.bone);
  344. bones.push_back(br);
  345. }
  346. }
  347. joints_dirty = false;
  348. reset();
  349. notify_property_list_changed();
  350. }
  351. void LimitAngularVelocityModifier3D::_process_modification(double p_delta) {
  352. Skeleton3D *skeleton = get_skeleton();
  353. if (!skeleton) {
  354. return;
  355. }
  356. if (init_needed) {
  357. // Note:
  358. // The pose retrieval within `_update_joints()` is done outside the skeleton's update process,
  359. // so it ignores the pose resulting from the previous modifier's modification.
  360. // This causes unintended initialization when `active` is set to true, so it must be initialized here.
  361. for (uint32_t i = 0; i < bones.size(); i++) {
  362. bones[i].second = skeleton->get_bone_pose_rotation(bones[i].first);
  363. }
  364. init_needed = false;
  365. }
  366. double limit_in_frame = max_angular_velocity * p_delta;
  367. for (uint32_t i = 0; i < bones.size(); i++) {
  368. int bn = bones[i].first;
  369. Quaternion dest = skeleton->get_bone_pose_rotation(bn);
  370. double diff = bones[i].second.angle_to(dest);
  371. if (!Math::is_zero_approx(diff)) {
  372. bones[i].second = bones[i].second.slerp(dest, MIN(1.0, limit_in_frame / diff));
  373. }
  374. skeleton->set_bone_pose_rotation(bn, bones[i].second);
  375. }
  376. }
  377. void LimitAngularVelocityModifier3D::reset() {
  378. init_needed = true;
  379. }
  380. LimitAngularVelocityModifier3D::~LimitAngularVelocityModifier3D() {
  381. clear_chains();
  382. }