bone_constraint_3d.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /**************************************************************************/
  2. /* bone_constraint_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 "bone_constraint_3d.h"
  31. bool BoneConstraint3D::_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, settings.size(), false);
  37. if (what == "amount") {
  38. set_amount(which, p_value);
  39. } else if (what == "apply_bone_name") {
  40. set_apply_bone_name(which, p_value);
  41. } else if (what == "reference_bone_name") {
  42. set_reference_bone_name(which, p_value);
  43. } else if (what == "apply_bone") {
  44. set_apply_bone(which, p_value);
  45. } else if (what == "reference_bone") {
  46. set_reference_bone(which, p_value);
  47. } else {
  48. return false;
  49. }
  50. }
  51. return true;
  52. }
  53. bool BoneConstraint3D::_get(const StringName &p_path, Variant &r_ret) const {
  54. String path = p_path;
  55. if (path.begins_with("settings/")) {
  56. int which = path.get_slicec('/', 1).to_int();
  57. String what = path.get_slicec('/', 2);
  58. ERR_FAIL_INDEX_V(which, settings.size(), false);
  59. if (what == "amount") {
  60. r_ret = get_amount(which);
  61. } else if (what == "apply_bone_name") {
  62. r_ret = get_apply_bone_name(which);
  63. } else if (what == "reference_bone_name") {
  64. r_ret = get_reference_bone_name(which);
  65. } else if (what == "apply_bone") {
  66. r_ret = get_apply_bone(which);
  67. } else if (what == "reference_bone") {
  68. r_ret = get_reference_bone(which);
  69. } else {
  70. return false;
  71. }
  72. }
  73. return true;
  74. }
  75. void BoneConstraint3D::get_property_list(List<PropertyInfo> *p_list) const {
  76. String enum_hint;
  77. Skeleton3D *skeleton = get_skeleton();
  78. if (skeleton) {
  79. enum_hint = skeleton->get_concatenated_bone_names();
  80. }
  81. for (int i = 0; i < settings.size(); i++) {
  82. String path = "settings/" + itos(i) + "/";
  83. p_list->push_back(PropertyInfo(Variant::FLOAT, path + "amount", PROPERTY_HINT_RANGE, "0,1,0.001"));
  84. p_list->push_back(PropertyInfo(Variant::STRING, path + "apply_bone_name", PROPERTY_HINT_ENUM_SUGGESTION, enum_hint));
  85. p_list->push_back(PropertyInfo(Variant::INT, path + "apply_bone", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  86. p_list->push_back(PropertyInfo(Variant::STRING, path + "reference_bone_name", PROPERTY_HINT_ENUM_SUGGESTION, enum_hint));
  87. p_list->push_back(PropertyInfo(Variant::INT, path + "reference_bone", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));
  88. }
  89. }
  90. void BoneConstraint3D::set_setting_count(int p_count) {
  91. ERR_FAIL_COND(p_count < 0);
  92. int delta = p_count - settings.size();
  93. if (delta < 0) {
  94. for (int i = delta; i < 0; i++) {
  95. memdelete(settings[settings.size() + i]);
  96. }
  97. }
  98. settings.resize(p_count);
  99. delta++;
  100. if (delta > 1) {
  101. for (int i = 1; i < delta; i++) {
  102. _validate_setting(p_count - i);
  103. }
  104. }
  105. notify_property_list_changed();
  106. }
  107. int BoneConstraint3D::get_setting_count() const {
  108. return settings.size();
  109. }
  110. void BoneConstraint3D::_validate_setting(int p_index) {
  111. settings.write[p_index] = memnew(BoneConstraint3DSetting);
  112. }
  113. void BoneConstraint3D::clear_settings() {
  114. set_setting_count(0);
  115. }
  116. void BoneConstraint3D::set_amount(int p_index, float p_amount) {
  117. ERR_FAIL_INDEX(p_index, settings.size());
  118. settings[p_index]->amount = p_amount;
  119. }
  120. float BoneConstraint3D::get_amount(int p_index) const {
  121. ERR_FAIL_INDEX_V(p_index, settings.size(), 0.0);
  122. return settings[p_index]->amount;
  123. }
  124. void BoneConstraint3D::set_apply_bone_name(int p_index, const String &p_bone_name) {
  125. ERR_FAIL_INDEX(p_index, settings.size());
  126. settings[p_index]->apply_bone_name = p_bone_name;
  127. Skeleton3D *sk = get_skeleton();
  128. if (sk) {
  129. set_apply_bone(p_index, sk->find_bone(settings[p_index]->apply_bone_name));
  130. }
  131. }
  132. String BoneConstraint3D::get_apply_bone_name(int p_index) const {
  133. ERR_FAIL_INDEX_V(p_index, settings.size(), String());
  134. return settings[p_index]->apply_bone_name;
  135. }
  136. void BoneConstraint3D::set_apply_bone(int p_index, int p_bone) {
  137. ERR_FAIL_INDEX(p_index, settings.size());
  138. settings[p_index]->apply_bone = p_bone;
  139. Skeleton3D *sk = get_skeleton();
  140. if (sk) {
  141. if (settings[p_index]->apply_bone <= -1 || settings[p_index]->apply_bone >= sk->get_bone_count()) {
  142. WARN_PRINT("apply bone index out of range!");
  143. settings[p_index]->apply_bone = -1;
  144. } else {
  145. settings[p_index]->apply_bone_name = sk->get_bone_name(settings[p_index]->apply_bone);
  146. }
  147. }
  148. }
  149. int BoneConstraint3D::get_apply_bone(int p_index) const {
  150. ERR_FAIL_INDEX_V(p_index, settings.size(), -1);
  151. return settings[p_index]->apply_bone;
  152. }
  153. void BoneConstraint3D::set_reference_bone_name(int p_index, const String &p_bone_name) {
  154. ERR_FAIL_INDEX(p_index, settings.size());
  155. settings[p_index]->reference_bone_name = p_bone_name;
  156. Skeleton3D *sk = get_skeleton();
  157. if (sk) {
  158. set_reference_bone(p_index, sk->find_bone(settings[p_index]->reference_bone_name));
  159. }
  160. }
  161. String BoneConstraint3D::get_reference_bone_name(int p_index) const {
  162. ERR_FAIL_INDEX_V(p_index, settings.size(), String());
  163. return settings[p_index]->reference_bone_name;
  164. }
  165. void BoneConstraint3D::set_reference_bone(int p_index, int p_bone) {
  166. ERR_FAIL_INDEX(p_index, settings.size());
  167. settings[p_index]->reference_bone = p_bone;
  168. Skeleton3D *sk = get_skeleton();
  169. if (sk) {
  170. if (settings[p_index]->reference_bone <= -1 || settings[p_index]->reference_bone >= sk->get_bone_count()) {
  171. WARN_PRINT("reference bone index out of range!");
  172. settings[p_index]->reference_bone = -1;
  173. } else {
  174. settings[p_index]->reference_bone_name = sk->get_bone_name(settings[p_index]->reference_bone);
  175. }
  176. }
  177. }
  178. int BoneConstraint3D::get_reference_bone(int p_index) const {
  179. ERR_FAIL_INDEX_V(p_index, settings.size(), -1);
  180. return settings[p_index]->reference_bone;
  181. }
  182. void BoneConstraint3D::_bind_methods() {
  183. ClassDB::bind_method(D_METHOD("set_amount", "index", "amount"), &BoneConstraint3D::set_amount);
  184. ClassDB::bind_method(D_METHOD("get_amount", "index"), &BoneConstraint3D::get_amount);
  185. ClassDB::bind_method(D_METHOD("set_apply_bone_name", "index", "bone_name"), &BoneConstraint3D::set_apply_bone_name);
  186. ClassDB::bind_method(D_METHOD("get_apply_bone_name", "index"), &BoneConstraint3D::get_apply_bone_name);
  187. ClassDB::bind_method(D_METHOD("set_apply_bone", "index", "bone"), &BoneConstraint3D::set_apply_bone);
  188. ClassDB::bind_method(D_METHOD("get_apply_bone", "index"), &BoneConstraint3D::get_apply_bone);
  189. ClassDB::bind_method(D_METHOD("set_reference_bone_name", "index", "bone_name"), &BoneConstraint3D::set_reference_bone_name);
  190. ClassDB::bind_method(D_METHOD("get_reference_bone_name", "index"), &BoneConstraint3D::get_reference_bone_name);
  191. ClassDB::bind_method(D_METHOD("set_reference_bone", "index", "bone"), &BoneConstraint3D::set_reference_bone);
  192. ClassDB::bind_method(D_METHOD("get_reference_bone", "index"), &BoneConstraint3D::get_reference_bone);
  193. ClassDB::bind_method(D_METHOD("set_setting_count", "count"), &BoneConstraint3D::set_setting_count);
  194. ClassDB::bind_method(D_METHOD("get_setting_count"), &BoneConstraint3D::get_setting_count);
  195. ClassDB::bind_method(D_METHOD("clear_setting"), &BoneConstraint3D::clear_settings);
  196. }
  197. void BoneConstraint3D::_validate_bone_names() {
  198. for (int i = 0; i < settings.size(); i++) {
  199. // Prior bone name.
  200. if (!settings[i]->apply_bone_name.is_empty()) {
  201. set_apply_bone_name(i, settings[i]->apply_bone_name);
  202. } else if (settings[i]->apply_bone != -1) {
  203. set_apply_bone(i, settings[i]->apply_bone);
  204. }
  205. // Prior bone name.
  206. if (!settings[i]->reference_bone_name.is_empty()) {
  207. set_reference_bone_name(i, settings[i]->reference_bone_name);
  208. } else if (settings[i]->reference_bone != -1) {
  209. set_reference_bone(i, settings[i]->reference_bone);
  210. }
  211. }
  212. }
  213. void BoneConstraint3D::_process_modification(double p_delta) {
  214. Skeleton3D *skeleton = get_skeleton();
  215. if (!skeleton) {
  216. return;
  217. }
  218. for (int i = 0; i < settings.size(); i++) {
  219. int apply_bone = settings[i]->apply_bone;
  220. if (apply_bone < 0) {
  221. continue;
  222. }
  223. int reference_bone = settings[i]->reference_bone;
  224. if (reference_bone < 0) {
  225. continue;
  226. }
  227. float amount = settings[i]->amount;
  228. if (amount <= 0) {
  229. continue;
  230. }
  231. _process_constraint(i, skeleton, apply_bone, reference_bone, amount);
  232. }
  233. }
  234. void BoneConstraint3D::_process_constraint(int p_index, Skeleton3D *p_skeleton, int p_apply_bone, int p_reference_bone, float p_amount) {
  235. //
  236. }
  237. double BoneConstraint3D::symmetrize_angle(double p_angle) {
  238. double angle = Math::fposmod(p_angle, Math::TAU);
  239. return angle > Math::PI ? angle - Math::TAU : angle;
  240. }
  241. double BoneConstraint3D::get_roll_angle(const Quaternion &p_rotation, const Vector3 &p_roll_axis) {
  242. // Ensure roll axis is normalized.
  243. Vector3 roll_axis = p_roll_axis.normalized();
  244. // Project the quaternion rotation onto the roll axis.
  245. // This gives us the component of rotation around that axis.
  246. double dot = p_rotation.x * roll_axis.x +
  247. p_rotation.y * roll_axis.y +
  248. p_rotation.z * roll_axis.z;
  249. // Create a quaternion representing just the roll component.
  250. Quaternion roll_component;
  251. roll_component.x = roll_axis.x * dot;
  252. roll_component.y = roll_axis.y * dot;
  253. roll_component.z = roll_axis.z * dot;
  254. roll_component.w = p_rotation.w;
  255. // Normalize this component.
  256. double length = roll_component.length();
  257. if (length > CMP_EPSILON) {
  258. roll_component = roll_component / length;
  259. } else {
  260. return 0.0;
  261. }
  262. // Extract the angle.
  263. double angle = 2.0 * Math::acos(CLAMP(roll_component.w, -1.0, 1.0));
  264. // Determine the sign.
  265. double direction = (roll_component.x * roll_axis.x + roll_component.y * roll_axis.y + roll_component.z * roll_axis.z > 0) ? 1.0 : -1.0;
  266. return symmetrize_angle(angle * direction);
  267. }
  268. BoneConstraint3D::~BoneConstraint3D() {
  269. clear_settings();
  270. }