2
0

joint_2d.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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 "physics_body_2d.h"
  32. #include "scene/scene_string_names.h"
  33. void Joint2D::_disconnect_signals() {
  34. Node *node_a = get_node_or_null(a);
  35. PhysicsBody2D *body_a = Object::cast_to<PhysicsBody2D>(node_a);
  36. if (body_a) {
  37. body_a->disconnect(SceneStringNames::get_singleton()->tree_exiting, callable_mp(this, &Joint2D::_body_exit_tree));
  38. }
  39. Node *node_b = get_node_or_null(b);
  40. PhysicsBody2D *body_b = Object::cast_to<PhysicsBody2D>(node_b);
  41. if (body_b) {
  42. body_b->disconnect(SceneStringNames::get_singleton()->tree_exiting, callable_mp(this, &Joint2D::_body_exit_tree));
  43. }
  44. }
  45. void Joint2D::_body_exit_tree() {
  46. _disconnect_signals();
  47. _update_joint(true);
  48. update_configuration_warnings();
  49. }
  50. void Joint2D::_update_joint(bool p_only_free) {
  51. if (ba.is_valid() && bb.is_valid() && exclude_from_collision) {
  52. PhysicsServer2D::get_singleton()->joint_disable_collisions_between_bodies(joint, false);
  53. }
  54. ba = RID();
  55. bb = RID();
  56. configured = false;
  57. if (p_only_free || !is_inside_tree()) {
  58. PhysicsServer2D::get_singleton()->joint_clear(joint);
  59. warning = String();
  60. return;
  61. }
  62. Node *node_a = get_node_or_null(a);
  63. Node *node_b = get_node_or_null(b);
  64. PhysicsBody2D *body_a = Object::cast_to<PhysicsBody2D>(node_a);
  65. PhysicsBody2D *body_b = Object::cast_to<PhysicsBody2D>(node_b);
  66. bool valid = false;
  67. if (node_a && !body_a && node_b && !body_b) {
  68. warning = RTR("Node A and Node B must be PhysicsBody2Ds");
  69. } else if (node_a && !body_a) {
  70. warning = RTR("Node A must be a PhysicsBody2D");
  71. } else if (node_b && !body_b) {
  72. warning = RTR("Node B must be a PhysicsBody2D");
  73. } else if (!body_a || !body_b) {
  74. warning = RTR("Joint is not connected to two PhysicsBody2Ds");
  75. } else if (body_a == body_b) {
  76. warning = RTR("Node A and Node B must be different PhysicsBody2Ds");
  77. } else {
  78. warning = String();
  79. valid = true;
  80. }
  81. update_configuration_warnings();
  82. if (!valid) {
  83. PhysicsServer2D::get_singleton()->joint_clear(joint);
  84. return;
  85. }
  86. if (body_a) {
  87. body_a->force_update_transform();
  88. }
  89. if (body_b) {
  90. body_b->force_update_transform();
  91. }
  92. configured = true;
  93. _configure_joint(joint, body_a, body_b);
  94. ERR_FAIL_COND_MSG(!joint.is_valid(), "Failed to configure the joint.");
  95. PhysicsServer2D::get_singleton()->joint_set_param(joint, PhysicsServer2D::JOINT_PARAM_BIAS, bias);
  96. ba = body_a->get_rid();
  97. bb = body_b->get_rid();
  98. body_a->connect(SceneStringNames::get_singleton()->tree_exiting, callable_mp(this, &Joint2D::_body_exit_tree));
  99. body_b->connect(SceneStringNames::get_singleton()->tree_exiting, callable_mp(this, &Joint2D::_body_exit_tree));
  100. PhysicsServer2D::get_singleton()->joint_disable_collisions_between_bodies(joint, exclude_from_collision);
  101. }
  102. void Joint2D::set_node_a(const NodePath &p_node_a) {
  103. if (a == p_node_a) {
  104. return;
  105. }
  106. if (is_configured()) {
  107. _disconnect_signals();
  108. }
  109. a = p_node_a;
  110. if (Engine::get_singleton()->is_editor_hint()) {
  111. // When in editor, the setter may be called as a result of node rename.
  112. // It happens before the node actually changes its name, which triggers false warning.
  113. callable_mp(this, &Joint2D::_update_joint).call_deferred();
  114. } else {
  115. _update_joint();
  116. }
  117. }
  118. NodePath Joint2D::get_node_a() const {
  119. return a;
  120. }
  121. void Joint2D::set_node_b(const NodePath &p_node_b) {
  122. if (b == p_node_b) {
  123. return;
  124. }
  125. if (is_configured()) {
  126. _disconnect_signals();
  127. }
  128. b = p_node_b;
  129. if (Engine::get_singleton()->is_editor_hint()) {
  130. callable_mp(this, &Joint2D::_update_joint).call_deferred();
  131. } else {
  132. _update_joint();
  133. }
  134. }
  135. NodePath Joint2D::get_node_b() const {
  136. return b;
  137. }
  138. void Joint2D::_notification(int p_what) {
  139. switch (p_what) {
  140. case NOTIFICATION_POST_ENTER_TREE: {
  141. if (is_configured()) {
  142. _disconnect_signals();
  143. }
  144. _update_joint();
  145. } break;
  146. case NOTIFICATION_EXIT_TREE: {
  147. if (is_configured()) {
  148. _disconnect_signals();
  149. }
  150. _update_joint(true);
  151. } break;
  152. }
  153. }
  154. void Joint2D::set_bias(real_t p_bias) {
  155. bias = p_bias;
  156. if (joint.is_valid()) {
  157. PhysicsServer2D::get_singleton()->joint_set_param(joint, PhysicsServer2D::JOINT_PARAM_BIAS, bias);
  158. }
  159. }
  160. real_t Joint2D::get_bias() const {
  161. return bias;
  162. }
  163. void Joint2D::set_exclude_nodes_from_collision(bool p_enable) {
  164. if (exclude_from_collision == p_enable) {
  165. return;
  166. }
  167. if (is_configured()) {
  168. _disconnect_signals();
  169. }
  170. _update_joint(true);
  171. exclude_from_collision = p_enable;
  172. _update_joint();
  173. }
  174. bool Joint2D::get_exclude_nodes_from_collision() const {
  175. return exclude_from_collision;
  176. }
  177. PackedStringArray Joint2D::get_configuration_warnings() const {
  178. PackedStringArray warnings = Node2D::get_configuration_warnings();
  179. if (!warning.is_empty()) {
  180. warnings.push_back(warning);
  181. }
  182. return warnings;
  183. }
  184. void Joint2D::_bind_methods() {
  185. ClassDB::bind_method(D_METHOD("set_node_a", "node"), &Joint2D::set_node_a);
  186. ClassDB::bind_method(D_METHOD("get_node_a"), &Joint2D::get_node_a);
  187. ClassDB::bind_method(D_METHOD("set_node_b", "node"), &Joint2D::set_node_b);
  188. ClassDB::bind_method(D_METHOD("get_node_b"), &Joint2D::get_node_b);
  189. ClassDB::bind_method(D_METHOD("set_bias", "bias"), &Joint2D::set_bias);
  190. ClassDB::bind_method(D_METHOD("get_bias"), &Joint2D::get_bias);
  191. ClassDB::bind_method(D_METHOD("set_exclude_nodes_from_collision", "enable"), &Joint2D::set_exclude_nodes_from_collision);
  192. ClassDB::bind_method(D_METHOD("get_exclude_nodes_from_collision"), &Joint2D::get_exclude_nodes_from_collision);
  193. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "node_a", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "PhysicsBody2D"), "set_node_a", "get_node_a");
  194. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "node_b", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "PhysicsBody2D"), "set_node_b", "get_node_b");
  195. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bias", PROPERTY_HINT_RANGE, "0,0.9,0.001"), "set_bias", "get_bias");
  196. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disable_collision"), "set_exclude_nodes_from_collision", "get_exclude_nodes_from_collision");
  197. }
  198. Joint2D::Joint2D() {
  199. joint = PhysicsServer2D::get_singleton()->joint_create();
  200. set_hide_clip_children(true);
  201. }
  202. Joint2D::~Joint2D() {
  203. ERR_FAIL_NULL(PhysicsServer2D::get_singleton());
  204. PhysicsServer2D::get_singleton()->free(joint);
  205. }
  206. ///////////////////////////////////////////////////////////////////////////////
  207. ///////////////////////////////////////////////////////////////////////////////
  208. ///////////////////////////////////////////////////////////////////////////////
  209. void PinJoint2D::_notification(int p_what) {
  210. switch (p_what) {
  211. case NOTIFICATION_DRAW: {
  212. if (!is_inside_tree()) {
  213. break;
  214. }
  215. if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) {
  216. break;
  217. }
  218. draw_line(Point2(-10, 0), Point2(+10, 0), Color(0.7, 0.6, 0.0, 0.5), 3);
  219. draw_line(Point2(0, -10), Point2(0, +10), Color(0.7, 0.6, 0.0, 0.5), 3);
  220. } break;
  221. }
  222. }
  223. void PinJoint2D::_configure_joint(RID p_joint, PhysicsBody2D *body_a, PhysicsBody2D *body_b) {
  224. PhysicsServer2D::get_singleton()->joint_make_pin(p_joint, get_global_position(), body_a->get_rid(), body_b ? body_b->get_rid() : RID());
  225. PhysicsServer2D::get_singleton()->pin_joint_set_param(p_joint, PhysicsServer2D::PIN_JOINT_SOFTNESS, softness);
  226. }
  227. void PinJoint2D::set_softness(real_t p_softness) {
  228. softness = p_softness;
  229. queue_redraw();
  230. if (is_configured()) {
  231. PhysicsServer2D::get_singleton()->pin_joint_set_param(get_joint(), PhysicsServer2D::PIN_JOINT_SOFTNESS, p_softness);
  232. }
  233. }
  234. real_t PinJoint2D::get_softness() const {
  235. return softness;
  236. }
  237. void PinJoint2D::_bind_methods() {
  238. ClassDB::bind_method(D_METHOD("set_softness", "softness"), &PinJoint2D::set_softness);
  239. ClassDB::bind_method(D_METHOD("get_softness"), &PinJoint2D::get_softness);
  240. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "softness", PROPERTY_HINT_RANGE, "0.00,16,0.01,exp"), "set_softness", "get_softness");
  241. }
  242. PinJoint2D::PinJoint2D() {
  243. }
  244. ///////////////////////////////////////////////////////////////////////////////
  245. ///////////////////////////////////////////////////////////////////////////////
  246. ///////////////////////////////////////////////////////////////////////////////
  247. void GrooveJoint2D::_notification(int p_what) {
  248. switch (p_what) {
  249. case NOTIFICATION_DRAW: {
  250. if (!is_inside_tree()) {
  251. break;
  252. }
  253. if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) {
  254. break;
  255. }
  256. draw_line(Point2(-10, 0), Point2(+10, 0), Color(0.7, 0.6, 0.0, 0.5), 3);
  257. draw_line(Point2(-10, length), Point2(+10, length), Color(0.7, 0.6, 0.0, 0.5), 3);
  258. draw_line(Point2(0, 0), Point2(0, length), Color(0.7, 0.6, 0.0, 0.5), 3);
  259. draw_line(Point2(-10, initial_offset), Point2(+10, initial_offset), Color(0.8, 0.8, 0.9, 0.5), 5);
  260. } break;
  261. }
  262. }
  263. void GrooveJoint2D::_configure_joint(RID p_joint, PhysicsBody2D *body_a, PhysicsBody2D *body_b) {
  264. Transform2D gt = get_global_transform();
  265. Vector2 groove_A1 = gt.get_origin();
  266. Vector2 groove_A2 = gt.xform(Vector2(0, length));
  267. Vector2 anchor_B = gt.xform(Vector2(0, initial_offset));
  268. PhysicsServer2D::get_singleton()->joint_make_groove(p_joint, groove_A1, groove_A2, anchor_B, body_a->get_rid(), body_b->get_rid());
  269. }
  270. void GrooveJoint2D::set_length(real_t p_length) {
  271. length = p_length;
  272. queue_redraw();
  273. }
  274. real_t GrooveJoint2D::get_length() const {
  275. return length;
  276. }
  277. void GrooveJoint2D::set_initial_offset(real_t p_initial_offset) {
  278. initial_offset = p_initial_offset;
  279. queue_redraw();
  280. }
  281. real_t GrooveJoint2D::get_initial_offset() const {
  282. return initial_offset;
  283. }
  284. void GrooveJoint2D::_bind_methods() {
  285. ClassDB::bind_method(D_METHOD("set_length", "length"), &GrooveJoint2D::set_length);
  286. ClassDB::bind_method(D_METHOD("get_length"), &GrooveJoint2D::get_length);
  287. ClassDB::bind_method(D_METHOD("set_initial_offset", "offset"), &GrooveJoint2D::set_initial_offset);
  288. ClassDB::bind_method(D_METHOD("get_initial_offset"), &GrooveJoint2D::get_initial_offset);
  289. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "length", PROPERTY_HINT_RANGE, "1,65535,1,exp,suffix:px"), "set_length", "get_length");
  290. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "initial_offset", PROPERTY_HINT_RANGE, "1,65535,1,exp,suffix:px"), "set_initial_offset", "get_initial_offset");
  291. }
  292. GrooveJoint2D::GrooveJoint2D() {
  293. }
  294. ///////////////////////////////////////////////////////////////////////////////
  295. ///////////////////////////////////////////////////////////////////////////////
  296. ///////////////////////////////////////////////////////////////////////////////
  297. void DampedSpringJoint2D::_notification(int p_what) {
  298. switch (p_what) {
  299. case NOTIFICATION_DRAW: {
  300. if (!is_inside_tree()) {
  301. break;
  302. }
  303. if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) {
  304. break;
  305. }
  306. draw_line(Point2(-10, 0), Point2(+10, 0), Color(0.7, 0.6, 0.0, 0.5), 3);
  307. draw_line(Point2(-10, length), Point2(+10, length), Color(0.7, 0.6, 0.0, 0.5), 3);
  308. draw_line(Point2(0, 0), Point2(0, length), Color(0.7, 0.6, 0.0, 0.5), 3);
  309. } break;
  310. }
  311. }
  312. void DampedSpringJoint2D::_configure_joint(RID p_joint, PhysicsBody2D *body_a, PhysicsBody2D *body_b) {
  313. Transform2D gt = get_global_transform();
  314. Vector2 anchor_A = gt.get_origin();
  315. Vector2 anchor_B = gt.xform(Vector2(0, length));
  316. PhysicsServer2D::get_singleton()->joint_make_damped_spring(p_joint, anchor_A, anchor_B, body_a->get_rid(), body_b->get_rid());
  317. if (rest_length) {
  318. PhysicsServer2D::get_singleton()->damped_spring_joint_set_param(p_joint, PhysicsServer2D::DAMPED_SPRING_REST_LENGTH, rest_length);
  319. }
  320. PhysicsServer2D::get_singleton()->damped_spring_joint_set_param(p_joint, PhysicsServer2D::DAMPED_SPRING_STIFFNESS, stiffness);
  321. PhysicsServer2D::get_singleton()->damped_spring_joint_set_param(p_joint, PhysicsServer2D::DAMPED_SPRING_DAMPING, damping);
  322. }
  323. void DampedSpringJoint2D::set_length(real_t p_length) {
  324. length = p_length;
  325. queue_redraw();
  326. }
  327. real_t DampedSpringJoint2D::get_length() const {
  328. return length;
  329. }
  330. void DampedSpringJoint2D::set_rest_length(real_t p_rest_length) {
  331. rest_length = p_rest_length;
  332. queue_redraw();
  333. if (is_configured()) {
  334. PhysicsServer2D::get_singleton()->damped_spring_joint_set_param(get_joint(), PhysicsServer2D::DAMPED_SPRING_REST_LENGTH, p_rest_length ? p_rest_length : length);
  335. }
  336. }
  337. real_t DampedSpringJoint2D::get_rest_length() const {
  338. return rest_length;
  339. }
  340. void DampedSpringJoint2D::set_stiffness(real_t p_stiffness) {
  341. stiffness = p_stiffness;
  342. queue_redraw();
  343. if (is_configured()) {
  344. PhysicsServer2D::get_singleton()->damped_spring_joint_set_param(get_joint(), PhysicsServer2D::DAMPED_SPRING_STIFFNESS, p_stiffness);
  345. }
  346. }
  347. real_t DampedSpringJoint2D::get_stiffness() const {
  348. return stiffness;
  349. }
  350. void DampedSpringJoint2D::set_damping(real_t p_damping) {
  351. damping = p_damping;
  352. queue_redraw();
  353. if (is_configured()) {
  354. PhysicsServer2D::get_singleton()->damped_spring_joint_set_param(get_joint(), PhysicsServer2D::DAMPED_SPRING_DAMPING, p_damping);
  355. }
  356. }
  357. real_t DampedSpringJoint2D::get_damping() const {
  358. return damping;
  359. }
  360. void DampedSpringJoint2D::_bind_methods() {
  361. ClassDB::bind_method(D_METHOD("set_length", "length"), &DampedSpringJoint2D::set_length);
  362. ClassDB::bind_method(D_METHOD("get_length"), &DampedSpringJoint2D::get_length);
  363. ClassDB::bind_method(D_METHOD("set_rest_length", "rest_length"), &DampedSpringJoint2D::set_rest_length);
  364. ClassDB::bind_method(D_METHOD("get_rest_length"), &DampedSpringJoint2D::get_rest_length);
  365. ClassDB::bind_method(D_METHOD("set_stiffness", "stiffness"), &DampedSpringJoint2D::set_stiffness);
  366. ClassDB::bind_method(D_METHOD("get_stiffness"), &DampedSpringJoint2D::get_stiffness);
  367. ClassDB::bind_method(D_METHOD("set_damping", "damping"), &DampedSpringJoint2D::set_damping);
  368. ClassDB::bind_method(D_METHOD("get_damping"), &DampedSpringJoint2D::get_damping);
  369. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "length", PROPERTY_HINT_RANGE, "1,65535,1,exp,suffix:px"), "set_length", "get_length");
  370. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "rest_length", PROPERTY_HINT_RANGE, "0,65535,1,exp,suffix:px"), "set_rest_length", "get_rest_length");
  371. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "stiffness", PROPERTY_HINT_RANGE, "0.1,64,0.1,exp"), "set_stiffness", "get_stiffness");
  372. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "damping", PROPERTY_HINT_RANGE, "0.01,16,0.01,exp"), "set_damping", "get_damping");
  373. }
  374. DampedSpringJoint2D::DampedSpringJoint2D() {
  375. }