joint_2d.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /**************************************************************************/
  2. /* joint_2d.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 "joint_2d.h"
  31. #include "scene/2d/physics/physics_body_2d.h"
  32. void Joint2D::_disconnect_signals() {
  33. Node *node_a = get_node_or_null(a);
  34. PhysicsBody2D *body_a = Object::cast_to<PhysicsBody2D>(node_a);
  35. if (body_a) {
  36. body_a->disconnect(SceneStringName(tree_exiting), callable_mp(this, &Joint2D::_body_exit_tree));
  37. }
  38. Node *node_b = get_node_or_null(b);
  39. PhysicsBody2D *body_b = Object::cast_to<PhysicsBody2D>(node_b);
  40. if (body_b) {
  41. body_b->disconnect(SceneStringName(tree_exiting), callable_mp(this, &Joint2D::_body_exit_tree));
  42. }
  43. }
  44. void Joint2D::_body_exit_tree() {
  45. _disconnect_signals();
  46. _update_joint(true);
  47. update_configuration_warnings();
  48. }
  49. void Joint2D::_update_joint(bool p_only_free) {
  50. if (ba.is_valid() && bb.is_valid() && exclude_from_collision) {
  51. PhysicsServer2D::get_singleton()->joint_disable_collisions_between_bodies(joint, false);
  52. }
  53. ba = RID();
  54. bb = RID();
  55. configured = false;
  56. if (p_only_free || !is_inside_tree()) {
  57. PhysicsServer2D::get_singleton()->joint_clear(joint);
  58. warning = String();
  59. return;
  60. }
  61. Node *node_a = get_node_or_null(a);
  62. Node *node_b = get_node_or_null(b);
  63. PhysicsBody2D *body_a = Object::cast_to<PhysicsBody2D>(node_a);
  64. PhysicsBody2D *body_b = Object::cast_to<PhysicsBody2D>(node_b);
  65. bool valid = false;
  66. if (node_a && !body_a && node_b && !body_b) {
  67. warning = RTR("Node A and Node B must be PhysicsBody2Ds");
  68. } else if (node_a && !body_a) {
  69. warning = RTR("Node A must be a PhysicsBody2D");
  70. } else if (node_b && !body_b) {
  71. warning = RTR("Node B must be a PhysicsBody2D");
  72. } else if (!body_a || !body_b) {
  73. warning = RTR("Joint is not connected to two PhysicsBody2Ds");
  74. } else if (body_a == body_b) {
  75. warning = RTR("Node A and Node B must be different PhysicsBody2Ds");
  76. } else {
  77. warning = String();
  78. valid = true;
  79. }
  80. update_configuration_warnings();
  81. if (!valid) {
  82. PhysicsServer2D::get_singleton()->joint_clear(joint);
  83. return;
  84. }
  85. if (body_a) {
  86. body_a->force_update_transform();
  87. }
  88. if (body_b) {
  89. body_b->force_update_transform();
  90. }
  91. configured = true;
  92. _configure_joint(joint, body_a, body_b);
  93. ERR_FAIL_COND_MSG(!joint.is_valid(), "Failed to configure the joint.");
  94. PhysicsServer2D::get_singleton()->joint_set_param(joint, PhysicsServer2D::JOINT_PARAM_BIAS, bias);
  95. ba = body_a->get_rid();
  96. bb = body_b->get_rid();
  97. if (!body_a->is_connected(SceneStringName(tree_exiting), callable_mp(this, &Joint2D::_body_exit_tree))) {
  98. body_a->connect(SceneStringName(tree_exiting), callable_mp(this, &Joint2D::_body_exit_tree));
  99. }
  100. if (!body_b->is_connected(SceneStringName(tree_exiting), callable_mp(this, &Joint2D::_body_exit_tree))) {
  101. body_b->connect(SceneStringName(tree_exiting), callable_mp(this, &Joint2D::_body_exit_tree));
  102. }
  103. PhysicsServer2D::get_singleton()->joint_disable_collisions_between_bodies(joint, exclude_from_collision);
  104. }
  105. void Joint2D::set_node_a(const NodePath &p_node_a) {
  106. if (a == p_node_a) {
  107. return;
  108. }
  109. if (is_configured()) {
  110. _disconnect_signals();
  111. }
  112. a = p_node_a;
  113. if (Engine::get_singleton()->is_editor_hint()) {
  114. // When in editor, the setter may be called as a result of node rename.
  115. // It happens before the node actually changes its name, which triggers false warning.
  116. callable_mp(this, &Joint2D::_update_joint).call_deferred(false);
  117. } else {
  118. _update_joint();
  119. }
  120. }
  121. NodePath Joint2D::get_node_a() const {
  122. return a;
  123. }
  124. void Joint2D::set_node_b(const NodePath &p_node_b) {
  125. if (b == p_node_b) {
  126. return;
  127. }
  128. if (is_configured()) {
  129. _disconnect_signals();
  130. }
  131. b = p_node_b;
  132. if (Engine::get_singleton()->is_editor_hint()) {
  133. callable_mp(this, &Joint2D::_update_joint).call_deferred(false);
  134. } else {
  135. _update_joint();
  136. }
  137. }
  138. NodePath Joint2D::get_node_b() const {
  139. return b;
  140. }
  141. void Joint2D::_notification(int p_what) {
  142. switch (p_what) {
  143. case NOTIFICATION_POST_ENTER_TREE: {
  144. if (is_configured()) {
  145. _disconnect_signals();
  146. }
  147. _update_joint();
  148. } break;
  149. case NOTIFICATION_EXIT_TREE: {
  150. if (is_configured()) {
  151. _disconnect_signals();
  152. }
  153. _update_joint(true);
  154. } break;
  155. }
  156. }
  157. void Joint2D::set_bias(real_t p_bias) {
  158. bias = p_bias;
  159. if (joint.is_valid()) {
  160. PhysicsServer2D::get_singleton()->joint_set_param(joint, PhysicsServer2D::JOINT_PARAM_BIAS, bias);
  161. }
  162. }
  163. real_t Joint2D::get_bias() const {
  164. return bias;
  165. }
  166. void Joint2D::set_exclude_nodes_from_collision(bool p_enable) {
  167. if (exclude_from_collision == p_enable) {
  168. return;
  169. }
  170. if (is_configured()) {
  171. _disconnect_signals();
  172. }
  173. _update_joint(true);
  174. exclude_from_collision = p_enable;
  175. _update_joint();
  176. }
  177. bool Joint2D::get_exclude_nodes_from_collision() const {
  178. return exclude_from_collision;
  179. }
  180. PackedStringArray Joint2D::get_configuration_warnings() const {
  181. PackedStringArray warnings = Node2D::get_configuration_warnings();
  182. if (!warning.is_empty()) {
  183. warnings.push_back(warning);
  184. }
  185. return warnings;
  186. }
  187. void Joint2D::_bind_methods() {
  188. ClassDB::bind_method(D_METHOD("set_node_a", "node"), &Joint2D::set_node_a);
  189. ClassDB::bind_method(D_METHOD("get_node_a"), &Joint2D::get_node_a);
  190. ClassDB::bind_method(D_METHOD("set_node_b", "node"), &Joint2D::set_node_b);
  191. ClassDB::bind_method(D_METHOD("get_node_b"), &Joint2D::get_node_b);
  192. ClassDB::bind_method(D_METHOD("set_bias", "bias"), &Joint2D::set_bias);
  193. ClassDB::bind_method(D_METHOD("get_bias"), &Joint2D::get_bias);
  194. ClassDB::bind_method(D_METHOD("set_exclude_nodes_from_collision", "enable"), &Joint2D::set_exclude_nodes_from_collision);
  195. ClassDB::bind_method(D_METHOD("get_exclude_nodes_from_collision"), &Joint2D::get_exclude_nodes_from_collision);
  196. ClassDB::bind_method(D_METHOD("get_rid"), &Joint2D::get_rid);
  197. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "node_a", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "PhysicsBody2D"), "set_node_a", "get_node_a");
  198. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "node_b", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "PhysicsBody2D"), "set_node_b", "get_node_b");
  199. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bias", PROPERTY_HINT_RANGE, "0,0.9,0.001"), "set_bias", "get_bias");
  200. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disable_collision"), "set_exclude_nodes_from_collision", "get_exclude_nodes_from_collision");
  201. }
  202. Joint2D::Joint2D() {
  203. joint = PhysicsServer2D::get_singleton()->joint_create();
  204. set_hide_clip_children(true);
  205. }
  206. Joint2D::~Joint2D() {
  207. ERR_FAIL_NULL(PhysicsServer2D::get_singleton());
  208. PhysicsServer2D::get_singleton()->free(joint);
  209. }