skeleton_modification_2d_jiggle.cpp 26 KB

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