skeleton_modification_2d_jiggle.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. /*************************************************************************/
  2. /* skeleton_modification_2d_jiggle.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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_modification_2d_jiggle.h"
  31. #include "scene/2d/skeleton_2d.h"
  32. #include "scene/resources/world_2d.h"
  33. bool SkeletonModification2DJiggle::_set(const StringName &p_path, const Variant &p_value) {
  34. String path = p_path;
  35. if (path.begins_with("joint_data/")) {
  36. int which = path.get_slicec('/', 1).to_int();
  37. String what = path.get_slicec('/', 2);
  38. ERR_FAIL_INDEX_V(which, jiggle_data_chain.size(), false);
  39. if (what == "bone2d_node") {
  40. set_jiggle_joint_bone2d_node(which, p_value);
  41. } else if (what == "bone_index") {
  42. set_jiggle_joint_bone_index(which, p_value);
  43. } else if (what == "override_defaults") {
  44. set_jiggle_joint_override(which, p_value);
  45. } else if (what == "stiffness") {
  46. set_jiggle_joint_stiffness(which, p_value);
  47. } else if (what == "mass") {
  48. set_jiggle_joint_mass(which, p_value);
  49. } else if (what == "damping") {
  50. set_jiggle_joint_damping(which, p_value);
  51. } else if (what == "use_gravity") {
  52. set_jiggle_joint_use_gravity(which, p_value);
  53. } else if (what == "gravity") {
  54. set_jiggle_joint_gravity(which, p_value);
  55. }
  56. return true;
  57. } else {
  58. if (path == "use_colliders") {
  59. set_use_colliders(p_value);
  60. } else if (path == "collision_mask") {
  61. set_collision_mask(p_value);
  62. }
  63. }
  64. return true;
  65. }
  66. bool SkeletonModification2DJiggle::_get(const StringName &p_path, Variant &r_ret) const {
  67. String path = p_path;
  68. if (path.begins_with("joint_data/")) {
  69. int which = path.get_slicec('/', 1).to_int();
  70. String what = path.get_slicec('/', 2);
  71. ERR_FAIL_INDEX_V(which, jiggle_data_chain.size(), false);
  72. if (what == "bone2d_node") {
  73. r_ret = get_jiggle_joint_bone2d_node(which);
  74. } else if (what == "bone_index") {
  75. r_ret = get_jiggle_joint_bone_index(which);
  76. } else if (what == "override_defaults") {
  77. r_ret = get_jiggle_joint_override(which);
  78. } else if (what == "stiffness") {
  79. r_ret = get_jiggle_joint_stiffness(which);
  80. } else if (what == "mass") {
  81. r_ret = get_jiggle_joint_mass(which);
  82. } else if (what == "damping") {
  83. r_ret = get_jiggle_joint_damping(which);
  84. } else if (what == "use_gravity") {
  85. r_ret = get_jiggle_joint_use_gravity(which);
  86. } else if (what == "gravity") {
  87. r_ret = get_jiggle_joint_gravity(which);
  88. }
  89. return true;
  90. } else {
  91. if (path == "use_colliders") {
  92. r_ret = get_use_colliders();
  93. } else if (path == "collision_mask") {
  94. r_ret = get_collision_mask();
  95. }
  96. }
  97. return true;
  98. }
  99. void SkeletonModification2DJiggle::_get_property_list(List<PropertyInfo> *p_list) const {
  100. p_list->push_back(PropertyInfo(Variant::BOOL, "use_colliders", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
  101. if (use_colliders) {
  102. p_list->push_back(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_2D_PHYSICS, "", PROPERTY_USAGE_DEFAULT));
  103. }
  104. for (int i = 0; i < jiggle_data_chain.size(); i++) {
  105. String base_string = "joint_data/" + itos(i) + "/";
  106. p_list->push_back(PropertyInfo(Variant::INT, base_string + "bone_index", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
  107. p_list->push_back(PropertyInfo(Variant::NODE_PATH, base_string + "bone2d_node", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Bone2D", PROPERTY_USAGE_DEFAULT));
  108. p_list->push_back(PropertyInfo(Variant::BOOL, base_string + "override_defaults", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
  109. if (jiggle_data_chain[i].override_defaults) {
  110. p_list->push_back(PropertyInfo(Variant::FLOAT, base_string + "stiffness", PROPERTY_HINT_RANGE, "0, 1000, 0.01", PROPERTY_USAGE_DEFAULT));
  111. p_list->push_back(PropertyInfo(Variant::FLOAT, base_string + "mass", PROPERTY_HINT_RANGE, "0, 1000, 0.01", PROPERTY_USAGE_DEFAULT));
  112. p_list->push_back(PropertyInfo(Variant::FLOAT, base_string + "damping", PROPERTY_HINT_RANGE, "0, 1, 0.01", PROPERTY_USAGE_DEFAULT));
  113. p_list->push_back(PropertyInfo(Variant::BOOL, base_string + "use_gravity", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
  114. if (jiggle_data_chain[i].use_gravity) {
  115. p_list->push_back(PropertyInfo(Variant::VECTOR2, base_string + "gravity", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT));
  116. }
  117. }
  118. }
  119. }
  120. void SkeletonModification2DJiggle::_execute(float p_delta) {
  121. ERR_FAIL_COND_MSG(!stack || !is_setup || stack->skeleton == nullptr,
  122. "Modification is not setup and therefore cannot execute!");
  123. if (!enabled) {
  124. return;
  125. }
  126. if (target_node_cache.is_null()) {
  127. WARN_PRINT_ONCE("Target cache is out of date. Attempting to update...");
  128. update_target_cache();
  129. return;
  130. }
  131. Node2D *target = Object::cast_to<Node2D>(ObjectDB::get_instance(target_node_cache));
  132. if (!target || !target->is_inside_tree()) {
  133. ERR_PRINT_ONCE("Target node is not in the scene tree. Cannot execute modification!");
  134. return;
  135. }
  136. for (int i = 0; i < jiggle_data_chain.size(); i++) {
  137. _execute_jiggle_joint(i, target, p_delta);
  138. }
  139. }
  140. void SkeletonModification2DJiggle::_execute_jiggle_joint(int p_joint_idx, Node2D *p_target, float p_delta) {
  141. // Adopted from: https://wiki.unity3d.com/index.php/JiggleBone
  142. // With modifications by TwistedTwigleg.
  143. if (jiggle_data_chain[p_joint_idx].bone_idx <= -1 || jiggle_data_chain[p_joint_idx].bone_idx > stack->skeleton->get_bone_count()) {
  144. ERR_PRINT_ONCE("Jiggle joint " + itos(p_joint_idx) + " bone index is invalid. Cannot execute modification on joint...");
  145. return;
  146. }
  147. if (jiggle_data_chain[p_joint_idx].bone2d_node_cache.is_null() && !jiggle_data_chain[p_joint_idx].bone2d_node.is_empty()) {
  148. WARN_PRINT_ONCE("Bone2D cache for joint " + itos(p_joint_idx) + " is out of date. Updating...");
  149. jiggle_joint_update_bone2d_cache(p_joint_idx);
  150. }
  151. Bone2D *operation_bone = stack->skeleton->get_bone(jiggle_data_chain[p_joint_idx].bone_idx);
  152. if (!operation_bone) {
  153. ERR_PRINT_ONCE("Jiggle joint " + itos(p_joint_idx) + " does not have a Bone2D node or it cannot be found!");
  154. return;
  155. }
  156. Transform2D operation_bone_trans = operation_bone->get_global_transform();
  157. Vector2 target_position = p_target->get_global_position();
  158. jiggle_data_chain.write[p_joint_idx].force = (target_position - jiggle_data_chain[p_joint_idx].dynamic_position) * jiggle_data_chain[p_joint_idx].stiffness * p_delta;
  159. if (jiggle_data_chain[p_joint_idx].use_gravity) {
  160. jiggle_data_chain.write[p_joint_idx].force += jiggle_data_chain[p_joint_idx].gravity * p_delta;
  161. }
  162. jiggle_data_chain.write[p_joint_idx].acceleration = jiggle_data_chain[p_joint_idx].force / jiggle_data_chain[p_joint_idx].mass;
  163. jiggle_data_chain.write[p_joint_idx].velocity += jiggle_data_chain[p_joint_idx].acceleration * (1 - jiggle_data_chain[p_joint_idx].damping);
  164. jiggle_data_chain.write[p_joint_idx].dynamic_position += jiggle_data_chain[p_joint_idx].velocity + jiggle_data_chain[p_joint_idx].force;
  165. jiggle_data_chain.write[p_joint_idx].dynamic_position += operation_bone_trans.get_origin() - jiggle_data_chain[p_joint_idx].last_position;
  166. jiggle_data_chain.write[p_joint_idx].last_position = operation_bone_trans.get_origin();
  167. // Collision detection/response
  168. if (use_colliders) {
  169. if (execution_mode == SkeletonModificationStack2D::EXECUTION_MODE::execution_mode_physics_process) {
  170. Ref<World2D> world_2d = stack->skeleton->get_world_2d();
  171. ERR_FAIL_COND(world_2d.is_null());
  172. PhysicsDirectSpaceState2D *space_state = PhysicsServer2D::get_singleton()->space_get_direct_state(world_2d->get_space());
  173. PhysicsDirectSpaceState2D::RayResult ray_result;
  174. PhysicsDirectSpaceState2D::RayParameters ray_params;
  175. ray_params.from = operation_bone_trans.get_origin();
  176. ray_params.to = jiggle_data_chain[p_joint_idx].dynamic_position;
  177. ray_params.collision_mask = collision_mask;
  178. // Add exception support?
  179. bool ray_hit = space_state->intersect_ray(ray_params, ray_result);
  180. if (ray_hit) {
  181. jiggle_data_chain.write[p_joint_idx].dynamic_position = jiggle_data_chain[p_joint_idx].last_noncollision_position;
  182. jiggle_data_chain.write[p_joint_idx].acceleration = Vector2(0, 0);
  183. jiggle_data_chain.write[p_joint_idx].velocity = Vector2(0, 0);
  184. } else {
  185. jiggle_data_chain.write[p_joint_idx].last_noncollision_position = jiggle_data_chain[p_joint_idx].dynamic_position;
  186. }
  187. } else {
  188. WARN_PRINT_ONCE("Jiggle 2D modifier: You cannot detect colliders without the stack mode being set to _physics_process!");
  189. }
  190. }
  191. // Rotate the bone using the dynamic position!
  192. operation_bone_trans = operation_bone_trans.looking_at(jiggle_data_chain[p_joint_idx].dynamic_position);
  193. operation_bone_trans.set_rotation(operation_bone_trans.get_rotation() - operation_bone->get_bone_angle());
  194. // Reset scale
  195. operation_bone_trans.set_scale(operation_bone->get_global_scale());
  196. operation_bone->set_global_transform(operation_bone_trans);
  197. stack->skeleton->set_bone_local_pose_override(jiggle_data_chain[p_joint_idx].bone_idx, operation_bone->get_transform(), stack->strength, true);
  198. }
  199. void SkeletonModification2DJiggle::_update_jiggle_joint_data() {
  200. for (int i = 0; i < jiggle_data_chain.size(); i++) {
  201. if (!jiggle_data_chain[i].override_defaults) {
  202. set_jiggle_joint_stiffness(i, stiffness);
  203. set_jiggle_joint_mass(i, mass);
  204. set_jiggle_joint_damping(i, damping);
  205. set_jiggle_joint_use_gravity(i, use_gravity);
  206. set_jiggle_joint_gravity(i, gravity);
  207. }
  208. }
  209. }
  210. void SkeletonModification2DJiggle::_setup_modification(SkeletonModificationStack2D *p_stack) {
  211. stack = p_stack;
  212. if (stack) {
  213. is_setup = true;
  214. if (stack->skeleton) {
  215. for (int i = 0; i < jiggle_data_chain.size(); i++) {
  216. int bone_idx = jiggle_data_chain[i].bone_idx;
  217. if (bone_idx > 0 && bone_idx < stack->skeleton->get_bone_count()) {
  218. Bone2D *bone2d_node = stack->skeleton->get_bone(bone_idx);
  219. jiggle_data_chain.write[i].dynamic_position = bone2d_node->get_global_position();
  220. }
  221. }
  222. }
  223. update_target_cache();
  224. }
  225. }
  226. void SkeletonModification2DJiggle::update_target_cache() {
  227. if (!is_setup || !stack) {
  228. ERR_PRINT_ONCE("Cannot update target cache: modification is not properly setup!");
  229. return;
  230. }
  231. target_node_cache = ObjectID();
  232. if (stack->skeleton) {
  233. if (stack->skeleton->is_inside_tree()) {
  234. if (stack->skeleton->has_node(target_node)) {
  235. Node *node = stack->skeleton->get_node(target_node);
  236. ERR_FAIL_COND_MSG(!node || stack->skeleton == node,
  237. "Cannot update target cache: node is this modification's skeleton or cannot be found!");
  238. ERR_FAIL_COND_MSG(!node->is_inside_tree(),
  239. "Cannot update target cache: node is not in scene tree!");
  240. target_node_cache = node->get_instance_id();
  241. }
  242. }
  243. }
  244. }
  245. void SkeletonModification2DJiggle::jiggle_joint_update_bone2d_cache(int p_joint_idx) {
  246. ERR_FAIL_INDEX_MSG(p_joint_idx, jiggle_data_chain.size(), "Cannot update bone2d cache: joint index out of range!");
  247. if (!is_setup || !stack) {
  248. ERR_PRINT_ONCE("Cannot update Jiggle " + itos(p_joint_idx) + " Bone2D cache: modification is not properly setup!");
  249. return;
  250. }
  251. jiggle_data_chain.write[p_joint_idx].bone2d_node_cache = ObjectID();
  252. if (stack->skeleton) {
  253. if (stack->skeleton->is_inside_tree()) {
  254. if (stack->skeleton->has_node(jiggle_data_chain[p_joint_idx].bone2d_node)) {
  255. Node *node = stack->skeleton->get_node(jiggle_data_chain[p_joint_idx].bone2d_node);
  256. ERR_FAIL_COND_MSG(!node || stack->skeleton == node,
  257. "Cannot update Jiggle joint " + itos(p_joint_idx) + " Bone2D cache: node is this modification's skeleton or cannot be found!");
  258. ERR_FAIL_COND_MSG(!node->is_inside_tree(),
  259. "Cannot update Jiggle joint " + itos(p_joint_idx) + " Bone2D cache: node is not in scene tree!");
  260. jiggle_data_chain.write[p_joint_idx].bone2d_node_cache = node->get_instance_id();
  261. Bone2D *bone = Object::cast_to<Bone2D>(node);
  262. if (bone) {
  263. jiggle_data_chain.write[p_joint_idx].bone_idx = bone->get_index_in_skeleton();
  264. } else {
  265. ERR_FAIL_MSG("Jiggle joint " + itos(p_joint_idx) + " Bone2D cache: Nodepath to Bone2D is not a Bone2D node!");
  266. }
  267. }
  268. }
  269. }
  270. }
  271. void SkeletonModification2DJiggle::set_target_node(const NodePath &p_target_node) {
  272. target_node = p_target_node;
  273. update_target_cache();
  274. }
  275. NodePath SkeletonModification2DJiggle::get_target_node() const {
  276. return target_node;
  277. }
  278. void SkeletonModification2DJiggle::set_stiffness(float p_stiffness) {
  279. ERR_FAIL_COND_MSG(p_stiffness < 0, "Stiffness cannot be set to a negative value!");
  280. stiffness = p_stiffness;
  281. _update_jiggle_joint_data();
  282. }
  283. float SkeletonModification2DJiggle::get_stiffness() const {
  284. return stiffness;
  285. }
  286. void SkeletonModification2DJiggle::set_mass(float p_mass) {
  287. ERR_FAIL_COND_MSG(p_mass < 0, "Mass cannot be set to a negative value!");
  288. mass = p_mass;
  289. _update_jiggle_joint_data();
  290. }
  291. float SkeletonModification2DJiggle::get_mass() const {
  292. return mass;
  293. }
  294. void SkeletonModification2DJiggle::set_damping(float p_damping) {
  295. ERR_FAIL_COND_MSG(p_damping < 0, "Damping cannot be set to a negative value!");
  296. ERR_FAIL_COND_MSG(p_damping > 1, "Damping cannot be more than one!");
  297. damping = p_damping;
  298. _update_jiggle_joint_data();
  299. }
  300. float SkeletonModification2DJiggle::get_damping() const {
  301. return damping;
  302. }
  303. void SkeletonModification2DJiggle::set_use_gravity(bool p_use_gravity) {
  304. use_gravity = p_use_gravity;
  305. _update_jiggle_joint_data();
  306. }
  307. bool SkeletonModification2DJiggle::get_use_gravity() const {
  308. return use_gravity;
  309. }
  310. void SkeletonModification2DJiggle::set_gravity(Vector2 p_gravity) {
  311. gravity = p_gravity;
  312. _update_jiggle_joint_data();
  313. }
  314. Vector2 SkeletonModification2DJiggle::get_gravity() const {
  315. return gravity;
  316. }
  317. void SkeletonModification2DJiggle::set_use_colliders(bool p_use_colliders) {
  318. use_colliders = p_use_colliders;
  319. notify_property_list_changed();
  320. }
  321. bool SkeletonModification2DJiggle::get_use_colliders() const {
  322. return use_colliders;
  323. }
  324. void SkeletonModification2DJiggle::set_collision_mask(int p_mask) {
  325. collision_mask = p_mask;
  326. }
  327. int SkeletonModification2DJiggle::get_collision_mask() const {
  328. return collision_mask;
  329. }
  330. // Jiggle joint data functions
  331. int SkeletonModification2DJiggle::get_jiggle_data_chain_length() {
  332. return jiggle_data_chain.size();
  333. }
  334. void SkeletonModification2DJiggle::set_jiggle_data_chain_length(int p_length) {
  335. ERR_FAIL_COND(p_length < 0);
  336. jiggle_data_chain.resize(p_length);
  337. notify_property_list_changed();
  338. }
  339. void SkeletonModification2DJiggle::set_jiggle_joint_bone2d_node(int p_joint_idx, const NodePath &p_target_node) {
  340. ERR_FAIL_INDEX_MSG(p_joint_idx, jiggle_data_chain.size(), "Jiggle joint out of range!");
  341. jiggle_data_chain.write[p_joint_idx].bone2d_node = p_target_node;
  342. jiggle_joint_update_bone2d_cache(p_joint_idx);
  343. notify_property_list_changed();
  344. }
  345. NodePath SkeletonModification2DJiggle::get_jiggle_joint_bone2d_node(int p_joint_idx) const {
  346. ERR_FAIL_INDEX_V_MSG(p_joint_idx, jiggle_data_chain.size(), NodePath(), "Jiggle joint out of range!");
  347. return jiggle_data_chain[p_joint_idx].bone2d_node;
  348. }
  349. void SkeletonModification2DJiggle::set_jiggle_joint_bone_index(int p_joint_idx, int p_bone_idx) {
  350. ERR_FAIL_INDEX_MSG(p_joint_idx, jiggle_data_chain.size(), "Jiggle joint out of range!");
  351. ERR_FAIL_COND_MSG(p_bone_idx < 0, "Bone index is out of range: The index is too low!");
  352. if (is_setup) {
  353. if (stack->skeleton) {
  354. ERR_FAIL_INDEX_MSG(p_bone_idx, stack->skeleton->get_bone_count(), "Passed-in Bone index is out of range!");
  355. jiggle_data_chain.write[p_joint_idx].bone_idx = p_bone_idx;
  356. jiggle_data_chain.write[p_joint_idx].bone2d_node_cache = stack->skeleton->get_bone(p_bone_idx)->get_instance_id();
  357. jiggle_data_chain.write[p_joint_idx].bone2d_node = stack->skeleton->get_path_to(stack->skeleton->get_bone(p_bone_idx));
  358. } else {
  359. WARN_PRINT("Cannot verify the Jiggle joint " + itos(p_joint_idx) + " bone index for this modification...");
  360. jiggle_data_chain.write[p_joint_idx].bone_idx = p_bone_idx;
  361. }
  362. } else {
  363. WARN_PRINT("Cannot verify the Jiggle joint " + itos(p_joint_idx) + " bone index for this modification...");
  364. jiggle_data_chain.write[p_joint_idx].bone_idx = p_bone_idx;
  365. }
  366. notify_property_list_changed();
  367. }
  368. int SkeletonModification2DJiggle::get_jiggle_joint_bone_index(int p_joint_idx) const {
  369. ERR_FAIL_INDEX_V_MSG(p_joint_idx, jiggle_data_chain.size(), -1, "Jiggle joint out of range!");
  370. return jiggle_data_chain[p_joint_idx].bone_idx;
  371. }
  372. void SkeletonModification2DJiggle::set_jiggle_joint_override(int p_joint_idx, bool p_override) {
  373. ERR_FAIL_INDEX(p_joint_idx, jiggle_data_chain.size());
  374. jiggle_data_chain.write[p_joint_idx].override_defaults = p_override;
  375. _update_jiggle_joint_data();
  376. notify_property_list_changed();
  377. }
  378. bool SkeletonModification2DJiggle::get_jiggle_joint_override(int p_joint_idx) const {
  379. ERR_FAIL_INDEX_V(p_joint_idx, jiggle_data_chain.size(), false);
  380. return jiggle_data_chain[p_joint_idx].override_defaults;
  381. }
  382. void SkeletonModification2DJiggle::set_jiggle_joint_stiffness(int p_joint_idx, float p_stiffness) {
  383. ERR_FAIL_COND_MSG(p_stiffness < 0, "Stiffness cannot be set to a negative value!");
  384. ERR_FAIL_INDEX(p_joint_idx, jiggle_data_chain.size());
  385. jiggle_data_chain.write[p_joint_idx].stiffness = p_stiffness;
  386. }
  387. float SkeletonModification2DJiggle::get_jiggle_joint_stiffness(int p_joint_idx) const {
  388. ERR_FAIL_INDEX_V(p_joint_idx, jiggle_data_chain.size(), -1);
  389. return jiggle_data_chain[p_joint_idx].stiffness;
  390. }
  391. void SkeletonModification2DJiggle::set_jiggle_joint_mass(int p_joint_idx, float p_mass) {
  392. ERR_FAIL_COND_MSG(p_mass < 0, "Mass cannot be set to a negative value!");
  393. ERR_FAIL_INDEX(p_joint_idx, jiggle_data_chain.size());
  394. jiggle_data_chain.write[p_joint_idx].mass = p_mass;
  395. }
  396. float SkeletonModification2DJiggle::get_jiggle_joint_mass(int p_joint_idx) const {
  397. ERR_FAIL_INDEX_V(p_joint_idx, jiggle_data_chain.size(), -1);
  398. return jiggle_data_chain[p_joint_idx].mass;
  399. }
  400. void SkeletonModification2DJiggle::set_jiggle_joint_damping(int p_joint_idx, float p_damping) {
  401. ERR_FAIL_COND_MSG(p_damping < 0, "Damping cannot be set to a negative value!");
  402. ERR_FAIL_INDEX(p_joint_idx, jiggle_data_chain.size());
  403. jiggle_data_chain.write[p_joint_idx].damping = p_damping;
  404. }
  405. float SkeletonModification2DJiggle::get_jiggle_joint_damping(int p_joint_idx) const {
  406. ERR_FAIL_INDEX_V(p_joint_idx, jiggle_data_chain.size(), -1);
  407. return jiggle_data_chain[p_joint_idx].damping;
  408. }
  409. void SkeletonModification2DJiggle::set_jiggle_joint_use_gravity(int p_joint_idx, bool p_use_gravity) {
  410. ERR_FAIL_INDEX(p_joint_idx, jiggle_data_chain.size());
  411. jiggle_data_chain.write[p_joint_idx].use_gravity = p_use_gravity;
  412. notify_property_list_changed();
  413. }
  414. bool SkeletonModification2DJiggle::get_jiggle_joint_use_gravity(int p_joint_idx) const {
  415. ERR_FAIL_INDEX_V(p_joint_idx, jiggle_data_chain.size(), false);
  416. return jiggle_data_chain[p_joint_idx].use_gravity;
  417. }
  418. void SkeletonModification2DJiggle::set_jiggle_joint_gravity(int p_joint_idx, Vector2 p_gravity) {
  419. ERR_FAIL_INDEX(p_joint_idx, jiggle_data_chain.size());
  420. jiggle_data_chain.write[p_joint_idx].gravity = p_gravity;
  421. }
  422. Vector2 SkeletonModification2DJiggle::get_jiggle_joint_gravity(int p_joint_idx) const {
  423. ERR_FAIL_INDEX_V(p_joint_idx, jiggle_data_chain.size(), Vector2(0, 0));
  424. return jiggle_data_chain[p_joint_idx].gravity;
  425. }
  426. void SkeletonModification2DJiggle::_bind_methods() {
  427. ClassDB::bind_method(D_METHOD("set_target_node", "target_nodepath"), &SkeletonModification2DJiggle::set_target_node);
  428. ClassDB::bind_method(D_METHOD("get_target_node"), &SkeletonModification2DJiggle::get_target_node);
  429. ClassDB::bind_method(D_METHOD("set_jiggle_data_chain_length", "length"), &SkeletonModification2DJiggle::set_jiggle_data_chain_length);
  430. ClassDB::bind_method(D_METHOD("get_jiggle_data_chain_length"), &SkeletonModification2DJiggle::get_jiggle_data_chain_length);
  431. ClassDB::bind_method(D_METHOD("set_stiffness", "stiffness"), &SkeletonModification2DJiggle::set_stiffness);
  432. ClassDB::bind_method(D_METHOD("get_stiffness"), &SkeletonModification2DJiggle::get_stiffness);
  433. ClassDB::bind_method(D_METHOD("set_mass", "mass"), &SkeletonModification2DJiggle::set_mass);
  434. ClassDB::bind_method(D_METHOD("get_mass"), &SkeletonModification2DJiggle::get_mass);
  435. ClassDB::bind_method(D_METHOD("set_damping", "damping"), &SkeletonModification2DJiggle::set_damping);
  436. ClassDB::bind_method(D_METHOD("get_damping"), &SkeletonModification2DJiggle::get_damping);
  437. ClassDB::bind_method(D_METHOD("set_use_gravity", "use_gravity"), &SkeletonModification2DJiggle::set_use_gravity);
  438. ClassDB::bind_method(D_METHOD("get_use_gravity"), &SkeletonModification2DJiggle::get_use_gravity);
  439. ClassDB::bind_method(D_METHOD("set_gravity", "gravity"), &SkeletonModification2DJiggle::set_gravity);
  440. ClassDB::bind_method(D_METHOD("get_gravity"), &SkeletonModification2DJiggle::get_gravity);
  441. ClassDB::bind_method(D_METHOD("set_use_colliders", "use_colliders"), &SkeletonModification2DJiggle::set_use_colliders);
  442. ClassDB::bind_method(D_METHOD("get_use_colliders"), &SkeletonModification2DJiggle::get_use_colliders);
  443. ClassDB::bind_method(D_METHOD("set_collision_mask", "collision_mask"), &SkeletonModification2DJiggle::set_collision_mask);
  444. ClassDB::bind_method(D_METHOD("get_collision_mask"), &SkeletonModification2DJiggle::get_collision_mask);
  445. // Jiggle joint data functions
  446. ClassDB::bind_method(D_METHOD("set_jiggle_joint_bone2d_node", "joint_idx", "bone2d_node"), &SkeletonModification2DJiggle::set_jiggle_joint_bone2d_node);
  447. ClassDB::bind_method(D_METHOD("get_jiggle_joint_bone2d_node", "joint_idx"), &SkeletonModification2DJiggle::get_jiggle_joint_bone2d_node);
  448. ClassDB::bind_method(D_METHOD("set_jiggle_joint_bone_index", "joint_idx", "bone_idx"), &SkeletonModification2DJiggle::set_jiggle_joint_bone_index);
  449. ClassDB::bind_method(D_METHOD("get_jiggle_joint_bone_index", "joint_idx"), &SkeletonModification2DJiggle::get_jiggle_joint_bone_index);
  450. ClassDB::bind_method(D_METHOD("set_jiggle_joint_override", "joint_idx", "override"), &SkeletonModification2DJiggle::set_jiggle_joint_override);
  451. ClassDB::bind_method(D_METHOD("get_jiggle_joint_override", "joint_idx"), &SkeletonModification2DJiggle::get_jiggle_joint_override);
  452. ClassDB::bind_method(D_METHOD("set_jiggle_joint_stiffness", "joint_idx", "stiffness"), &SkeletonModification2DJiggle::set_jiggle_joint_stiffness);
  453. ClassDB::bind_method(D_METHOD("get_jiggle_joint_stiffness", "joint_idx"), &SkeletonModification2DJiggle::get_jiggle_joint_stiffness);
  454. ClassDB::bind_method(D_METHOD("set_jiggle_joint_mass", "joint_idx", "mass"), &SkeletonModification2DJiggle::set_jiggle_joint_mass);
  455. ClassDB::bind_method(D_METHOD("get_jiggle_joint_mass", "joint_idx"), &SkeletonModification2DJiggle::get_jiggle_joint_mass);
  456. ClassDB::bind_method(D_METHOD("set_jiggle_joint_damping", "joint_idx", "damping"), &SkeletonModification2DJiggle::set_jiggle_joint_damping);
  457. ClassDB::bind_method(D_METHOD("get_jiggle_joint_damping", "joint_idx"), &SkeletonModification2DJiggle::get_jiggle_joint_damping);
  458. ClassDB::bind_method(D_METHOD("set_jiggle_joint_use_gravity", "joint_idx", "use_gravity"), &SkeletonModification2DJiggle::set_jiggle_joint_use_gravity);
  459. ClassDB::bind_method(D_METHOD("get_jiggle_joint_use_gravity", "joint_idx"), &SkeletonModification2DJiggle::get_jiggle_joint_use_gravity);
  460. ClassDB::bind_method(D_METHOD("set_jiggle_joint_gravity", "joint_idx", "gravity"), &SkeletonModification2DJiggle::set_jiggle_joint_gravity);
  461. ClassDB::bind_method(D_METHOD("get_jiggle_joint_gravity", "joint_idx"), &SkeletonModification2DJiggle::get_jiggle_joint_gravity);
  462. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "target_nodepath", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Node2D"), "set_target_node", "get_target_node");
  463. ADD_PROPERTY(PropertyInfo(Variant::INT, "jiggle_data_chain_length", PROPERTY_HINT_RANGE, "0,100,1"), "set_jiggle_data_chain_length", "get_jiggle_data_chain_length");
  464. ADD_GROUP("Default Joint Settings", "");
  465. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "stiffness"), "set_stiffness", "get_stiffness");
  466. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "mass"), "set_mass", "get_mass");
  467. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "damping", PROPERTY_HINT_RANGE, "0, 1, 0.01"), "set_damping", "get_damping");
  468. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_gravity"), "set_use_gravity", "get_use_gravity");
  469. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "gravity"), "set_gravity", "get_gravity");
  470. ADD_GROUP("", "");
  471. }
  472. SkeletonModification2DJiggle::SkeletonModification2DJiggle() {
  473. stack = nullptr;
  474. is_setup = false;
  475. jiggle_data_chain = Vector<Jiggle_Joint_Data2D>();
  476. stiffness = 3;
  477. mass = 0.75;
  478. damping = 0.75;
  479. use_gravity = false;
  480. gravity = Vector2(0, 6.0);
  481. enabled = true;
  482. editor_draw_gizmo = false; // Nothing to really show in a gizmo right now.
  483. }
  484. SkeletonModification2DJiggle::~SkeletonModification2DJiggle() {
  485. }