joint_2d.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*************************************************************************/
  2. /* joint_2d.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 "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()->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. _update_joint();
  111. }
  112. NodePath Joint2D::get_node_a() const {
  113. return a;
  114. }
  115. void Joint2D::set_node_b(const NodePath &p_node_b) {
  116. if (b == p_node_b) {
  117. return;
  118. }
  119. if (is_configured()) {
  120. _disconnect_signals();
  121. }
  122. b = p_node_b;
  123. _update_joint();
  124. }
  125. NodePath Joint2D::get_node_b() const {
  126. return b;
  127. }
  128. void Joint2D::_notification(int p_what) {
  129. switch (p_what) {
  130. case NOTIFICATION_POST_ENTER_TREE: {
  131. if (is_configured()) {
  132. _disconnect_signals();
  133. }
  134. _update_joint();
  135. } break;
  136. case NOTIFICATION_EXIT_TREE: {
  137. if (is_configured()) {
  138. _disconnect_signals();
  139. }
  140. _update_joint(true);
  141. } break;
  142. }
  143. }
  144. void Joint2D::set_bias(real_t p_bias) {
  145. bias = p_bias;
  146. if (joint.is_valid()) {
  147. PhysicsServer2D::get_singleton()->get_singleton()->joint_set_param(joint, PhysicsServer2D::JOINT_PARAM_BIAS, bias);
  148. }
  149. }
  150. real_t Joint2D::get_bias() const {
  151. return bias;
  152. }
  153. void Joint2D::set_exclude_nodes_from_collision(bool p_enable) {
  154. if (exclude_from_collision == p_enable) {
  155. return;
  156. }
  157. if (is_configured()) {
  158. _disconnect_signals();
  159. }
  160. _update_joint(true);
  161. exclude_from_collision = p_enable;
  162. _update_joint();
  163. }
  164. bool Joint2D::get_exclude_nodes_from_collision() const {
  165. return exclude_from_collision;
  166. }
  167. TypedArray<String> Joint2D::get_configuration_warnings() const {
  168. TypedArray<String> warnings = Node2D::get_configuration_warnings();
  169. if (!warning.is_empty()) {
  170. warnings.push_back(warning);
  171. }
  172. return warnings;
  173. }
  174. void Joint2D::_bind_methods() {
  175. ClassDB::bind_method(D_METHOD("set_node_a", "node"), &Joint2D::set_node_a);
  176. ClassDB::bind_method(D_METHOD("get_node_a"), &Joint2D::get_node_a);
  177. ClassDB::bind_method(D_METHOD("set_node_b", "node"), &Joint2D::set_node_b);
  178. ClassDB::bind_method(D_METHOD("get_node_b"), &Joint2D::get_node_b);
  179. ClassDB::bind_method(D_METHOD("set_bias", "bias"), &Joint2D::set_bias);
  180. ClassDB::bind_method(D_METHOD("get_bias"), &Joint2D::get_bias);
  181. ClassDB::bind_method(D_METHOD("set_exclude_nodes_from_collision", "enable"), &Joint2D::set_exclude_nodes_from_collision);
  182. ClassDB::bind_method(D_METHOD("get_exclude_nodes_from_collision"), &Joint2D::get_exclude_nodes_from_collision);
  183. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "node_a", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "PhysicsBody2D"), "set_node_a", "get_node_a");
  184. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "node_b", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "PhysicsBody2D"), "set_node_b", "get_node_b");
  185. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bias", PROPERTY_HINT_RANGE, "0,0.9,0.001"), "set_bias", "get_bias");
  186. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disable_collision"), "set_exclude_nodes_from_collision", "get_exclude_nodes_from_collision");
  187. }
  188. Joint2D::Joint2D() {
  189. joint = PhysicsServer2D::get_singleton()->joint_create();
  190. }
  191. Joint2D::~Joint2D() {
  192. PhysicsServer2D::get_singleton()->free(joint);
  193. }
  194. ///////////////////////////////////////////////////////////////////////////////
  195. ///////////////////////////////////////////////////////////////////////////////
  196. ///////////////////////////////////////////////////////////////////////////////
  197. void PinJoint2D::_notification(int p_what) {
  198. switch (p_what) {
  199. case NOTIFICATION_DRAW: {
  200. if (!is_inside_tree()) {
  201. break;
  202. }
  203. if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) {
  204. break;
  205. }
  206. draw_line(Point2(-10, 0), Point2(+10, 0), Color(0.7, 0.6, 0.0, 0.5), 3);
  207. draw_line(Point2(0, -10), Point2(0, +10), Color(0.7, 0.6, 0.0, 0.5), 3);
  208. } break;
  209. }
  210. }
  211. void PinJoint2D::_configure_joint(RID p_joint, PhysicsBody2D *body_a, PhysicsBody2D *body_b) {
  212. PhysicsServer2D::get_singleton()->joint_make_pin(p_joint, get_global_position(), body_a->get_rid(), body_b ? body_b->get_rid() : RID());
  213. PhysicsServer2D::get_singleton()->pin_joint_set_param(p_joint, PhysicsServer2D::PIN_JOINT_SOFTNESS, softness);
  214. }
  215. void PinJoint2D::set_softness(real_t p_softness) {
  216. softness = p_softness;
  217. queue_redraw();
  218. if (is_configured()) {
  219. PhysicsServer2D::get_singleton()->pin_joint_set_param(get_joint(), PhysicsServer2D::PIN_JOINT_SOFTNESS, p_softness);
  220. }
  221. }
  222. real_t PinJoint2D::get_softness() const {
  223. return softness;
  224. }
  225. void PinJoint2D::_bind_methods() {
  226. ClassDB::bind_method(D_METHOD("set_softness", "softness"), &PinJoint2D::set_softness);
  227. ClassDB::bind_method(D_METHOD("get_softness"), &PinJoint2D::get_softness);
  228. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "softness", PROPERTY_HINT_RANGE, "0.00,16,0.01,exp"), "set_softness", "get_softness");
  229. }
  230. PinJoint2D::PinJoint2D() {
  231. }
  232. ///////////////////////////////////////////////////////////////////////////////
  233. ///////////////////////////////////////////////////////////////////////////////
  234. ///////////////////////////////////////////////////////////////////////////////
  235. void GrooveJoint2D::_notification(int p_what) {
  236. switch (p_what) {
  237. case NOTIFICATION_DRAW: {
  238. if (!is_inside_tree()) {
  239. break;
  240. }
  241. if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) {
  242. break;
  243. }
  244. draw_line(Point2(-10, 0), Point2(+10, 0), Color(0.7, 0.6, 0.0, 0.5), 3);
  245. draw_line(Point2(-10, length), Point2(+10, length), Color(0.7, 0.6, 0.0, 0.5), 3);
  246. draw_line(Point2(0, 0), Point2(0, length), Color(0.7, 0.6, 0.0, 0.5), 3);
  247. draw_line(Point2(-10, initial_offset), Point2(+10, initial_offset), Color(0.8, 0.8, 0.9, 0.5), 5);
  248. } break;
  249. }
  250. }
  251. void GrooveJoint2D::_configure_joint(RID p_joint, PhysicsBody2D *body_a, PhysicsBody2D *body_b) {
  252. Transform2D gt = get_global_transform();
  253. Vector2 groove_A1 = gt.get_origin();
  254. Vector2 groove_A2 = gt.xform(Vector2(0, length));
  255. Vector2 anchor_B = gt.xform(Vector2(0, initial_offset));
  256. PhysicsServer2D::get_singleton()->joint_make_groove(p_joint, groove_A1, groove_A2, anchor_B, body_a->get_rid(), body_b->get_rid());
  257. }
  258. void GrooveJoint2D::set_length(real_t p_length) {
  259. length = p_length;
  260. queue_redraw();
  261. }
  262. real_t GrooveJoint2D::get_length() const {
  263. return length;
  264. }
  265. void GrooveJoint2D::set_initial_offset(real_t p_initial_offset) {
  266. initial_offset = p_initial_offset;
  267. queue_redraw();
  268. }
  269. real_t GrooveJoint2D::get_initial_offset() const {
  270. return initial_offset;
  271. }
  272. void GrooveJoint2D::_bind_methods() {
  273. ClassDB::bind_method(D_METHOD("set_length", "length"), &GrooveJoint2D::set_length);
  274. ClassDB::bind_method(D_METHOD("get_length"), &GrooveJoint2D::get_length);
  275. ClassDB::bind_method(D_METHOD("set_initial_offset", "offset"), &GrooveJoint2D::set_initial_offset);
  276. ClassDB::bind_method(D_METHOD("get_initial_offset"), &GrooveJoint2D::get_initial_offset);
  277. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "length", PROPERTY_HINT_RANGE, "1,65535,1,exp,suffix:px"), "set_length", "get_length");
  278. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "initial_offset", PROPERTY_HINT_RANGE, "1,65535,1,exp,suffix:px"), "set_initial_offset", "get_initial_offset");
  279. }
  280. GrooveJoint2D::GrooveJoint2D() {
  281. }
  282. ///////////////////////////////////////////////////////////////////////////////
  283. ///////////////////////////////////////////////////////////////////////////////
  284. ///////////////////////////////////////////////////////////////////////////////
  285. void DampedSpringJoint2D::_notification(int p_what) {
  286. switch (p_what) {
  287. case NOTIFICATION_DRAW: {
  288. if (!is_inside_tree()) {
  289. break;
  290. }
  291. if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) {
  292. break;
  293. }
  294. draw_line(Point2(-10, 0), Point2(+10, 0), Color(0.7, 0.6, 0.0, 0.5), 3);
  295. draw_line(Point2(-10, length), Point2(+10, length), Color(0.7, 0.6, 0.0, 0.5), 3);
  296. draw_line(Point2(0, 0), Point2(0, length), Color(0.7, 0.6, 0.0, 0.5), 3);
  297. } break;
  298. }
  299. }
  300. void DampedSpringJoint2D::_configure_joint(RID p_joint, PhysicsBody2D *body_a, PhysicsBody2D *body_b) {
  301. Transform2D gt = get_global_transform();
  302. Vector2 anchor_A = gt.get_origin();
  303. Vector2 anchor_B = gt.xform(Vector2(0, length));
  304. PhysicsServer2D::get_singleton()->joint_make_damped_spring(p_joint, anchor_A, anchor_B, body_a->get_rid(), body_b->get_rid());
  305. if (rest_length) {
  306. PhysicsServer2D::get_singleton()->damped_spring_joint_set_param(p_joint, PhysicsServer2D::DAMPED_SPRING_REST_LENGTH, rest_length);
  307. }
  308. PhysicsServer2D::get_singleton()->damped_spring_joint_set_param(p_joint, PhysicsServer2D::DAMPED_SPRING_STIFFNESS, stiffness);
  309. PhysicsServer2D::get_singleton()->damped_spring_joint_set_param(p_joint, PhysicsServer2D::DAMPED_SPRING_DAMPING, damping);
  310. }
  311. void DampedSpringJoint2D::set_length(real_t p_length) {
  312. length = p_length;
  313. queue_redraw();
  314. }
  315. real_t DampedSpringJoint2D::get_length() const {
  316. return length;
  317. }
  318. void DampedSpringJoint2D::set_rest_length(real_t p_rest_length) {
  319. rest_length = p_rest_length;
  320. queue_redraw();
  321. if (is_configured()) {
  322. PhysicsServer2D::get_singleton()->damped_spring_joint_set_param(get_joint(), PhysicsServer2D::DAMPED_SPRING_REST_LENGTH, p_rest_length ? p_rest_length : length);
  323. }
  324. }
  325. real_t DampedSpringJoint2D::get_rest_length() const {
  326. return rest_length;
  327. }
  328. void DampedSpringJoint2D::set_stiffness(real_t p_stiffness) {
  329. stiffness = p_stiffness;
  330. queue_redraw();
  331. if (is_configured()) {
  332. PhysicsServer2D::get_singleton()->damped_spring_joint_set_param(get_joint(), PhysicsServer2D::DAMPED_SPRING_STIFFNESS, p_stiffness);
  333. }
  334. }
  335. real_t DampedSpringJoint2D::get_stiffness() const {
  336. return stiffness;
  337. }
  338. void DampedSpringJoint2D::set_damping(real_t p_damping) {
  339. damping = p_damping;
  340. queue_redraw();
  341. if (is_configured()) {
  342. PhysicsServer2D::get_singleton()->damped_spring_joint_set_param(get_joint(), PhysicsServer2D::DAMPED_SPRING_DAMPING, p_damping);
  343. }
  344. }
  345. real_t DampedSpringJoint2D::get_damping() const {
  346. return damping;
  347. }
  348. void DampedSpringJoint2D::_bind_methods() {
  349. ClassDB::bind_method(D_METHOD("set_length", "length"), &DampedSpringJoint2D::set_length);
  350. ClassDB::bind_method(D_METHOD("get_length"), &DampedSpringJoint2D::get_length);
  351. ClassDB::bind_method(D_METHOD("set_rest_length", "rest_length"), &DampedSpringJoint2D::set_rest_length);
  352. ClassDB::bind_method(D_METHOD("get_rest_length"), &DampedSpringJoint2D::get_rest_length);
  353. ClassDB::bind_method(D_METHOD("set_stiffness", "stiffness"), &DampedSpringJoint2D::set_stiffness);
  354. ClassDB::bind_method(D_METHOD("get_stiffness"), &DampedSpringJoint2D::get_stiffness);
  355. ClassDB::bind_method(D_METHOD("set_damping", "damping"), &DampedSpringJoint2D::set_damping);
  356. ClassDB::bind_method(D_METHOD("get_damping"), &DampedSpringJoint2D::get_damping);
  357. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "length", PROPERTY_HINT_RANGE, "1,65535,1,exp,suffix:px"), "set_length", "get_length");
  358. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "rest_length", PROPERTY_HINT_RANGE, "0,65535,1,exp,suffix:px"), "set_rest_length", "get_rest_length");
  359. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "stiffness", PROPERTY_HINT_RANGE, "0.1,64,0.1,exp"), "set_stiffness", "get_stiffness");
  360. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "damping", PROPERTY_HINT_RANGE, "0.01,16,0.01,exp"), "set_damping", "get_damping");
  361. }
  362. DampedSpringJoint2D::DampedSpringJoint2D() {
  363. }