joint_2d.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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. ClassDB::bind_method(D_METHOD("get_rid"), &Joint2D::get_rid);
  194. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "node_a", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "PhysicsBody2D"), "set_node_a", "get_node_a");
  195. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "node_b", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "PhysicsBody2D"), "set_node_b", "get_node_b");
  196. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "bias", PROPERTY_HINT_RANGE, "0,0.9,0.001"), "set_bias", "get_bias");
  197. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disable_collision"), "set_exclude_nodes_from_collision", "get_exclude_nodes_from_collision");
  198. }
  199. Joint2D::Joint2D() {
  200. joint = PhysicsServer2D::get_singleton()->joint_create();
  201. set_hide_clip_children(true);
  202. }
  203. Joint2D::~Joint2D() {
  204. ERR_FAIL_NULL(PhysicsServer2D::get_singleton());
  205. PhysicsServer2D::get_singleton()->free(joint);
  206. }
  207. ///////////////////////////////////////////////////////////////////////////////
  208. ///////////////////////////////////////////////////////////////////////////////
  209. ///////////////////////////////////////////////////////////////////////////////
  210. void PinJoint2D::_notification(int p_what) {
  211. switch (p_what) {
  212. case NOTIFICATION_DRAW: {
  213. if (!is_inside_tree()) {
  214. break;
  215. }
  216. if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) {
  217. break;
  218. }
  219. draw_line(Point2(-10, 0), Point2(+10, 0), Color(0.7, 0.6, 0.0, 0.5), 3);
  220. draw_line(Point2(0, -10), Point2(0, +10), Color(0.7, 0.6, 0.0, 0.5), 3);
  221. } break;
  222. }
  223. }
  224. void PinJoint2D::_configure_joint(RID p_joint, PhysicsBody2D *body_a, PhysicsBody2D *body_b) {
  225. PhysicsServer2D::get_singleton()->joint_make_pin(p_joint, get_global_position(), body_a->get_rid(), body_b ? body_b->get_rid() : RID());
  226. PhysicsServer2D::get_singleton()->pin_joint_set_param(p_joint, PhysicsServer2D::PIN_JOINT_SOFTNESS, softness);
  227. PhysicsServer2D::get_singleton()->pin_joint_set_param(p_joint, PhysicsServer2D::PIN_JOINT_LIMIT_UPPER, angular_limit_upper);
  228. PhysicsServer2D::get_singleton()->pin_joint_set_param(p_joint, PhysicsServer2D::PIN_JOINT_LIMIT_LOWER, angular_limit_lower);
  229. PhysicsServer2D::get_singleton()->pin_joint_set_param(p_joint, PhysicsServer2D::PIN_JOINT_MOTOR_TARGET_VELOCITY, motor_target_velocity);
  230. PhysicsServer2D::get_singleton()->pin_joint_set_flag(p_joint, PhysicsServer2D::PIN_JOINT_FLAG_MOTOR_ENABLED, motor_enabled);
  231. PhysicsServer2D::get_singleton()->pin_joint_set_flag(p_joint, PhysicsServer2D::PIN_JOINT_FLAG_ANGULAR_LIMIT_ENABLED, angular_limit_enabled);
  232. }
  233. void PinJoint2D::set_softness(real_t p_softness) {
  234. if (softness == p_softness) {
  235. return;
  236. }
  237. softness = p_softness;
  238. queue_redraw();
  239. if (is_configured()) {
  240. PhysicsServer2D::get_singleton()->pin_joint_set_param(get_rid(), PhysicsServer2D::PIN_JOINT_SOFTNESS, p_softness);
  241. }
  242. }
  243. real_t PinJoint2D::get_softness() const {
  244. return softness;
  245. }
  246. void PinJoint2D::set_angular_limit_lower(real_t p_angular_limit_lower) {
  247. if (angular_limit_lower == p_angular_limit_lower) {
  248. return;
  249. }
  250. angular_limit_lower = p_angular_limit_lower;
  251. queue_redraw();
  252. if (is_configured()) {
  253. PhysicsServer2D::get_singleton()->pin_joint_set_param(get_rid(), PhysicsServer2D::PIN_JOINT_LIMIT_LOWER, p_angular_limit_lower);
  254. }
  255. }
  256. real_t PinJoint2D::get_angular_limit_lower() const {
  257. return angular_limit_lower;
  258. }
  259. void PinJoint2D::set_angular_limit_upper(real_t p_angular_limit_upper) {
  260. if (angular_limit_upper == p_angular_limit_upper) {
  261. return;
  262. }
  263. angular_limit_upper = p_angular_limit_upper;
  264. queue_redraw();
  265. if (is_configured()) {
  266. PhysicsServer2D::get_singleton()->pin_joint_set_param(get_rid(), PhysicsServer2D::PIN_JOINT_LIMIT_UPPER, p_angular_limit_upper);
  267. }
  268. }
  269. real_t PinJoint2D::get_angular_limit_upper() const {
  270. return angular_limit_upper;
  271. }
  272. void PinJoint2D::set_motor_target_velocity(real_t p_motor_target_velocity) {
  273. if (motor_target_velocity == p_motor_target_velocity) {
  274. return;
  275. }
  276. motor_target_velocity = p_motor_target_velocity;
  277. queue_redraw();
  278. if (is_configured()) {
  279. PhysicsServer2D::get_singleton()->pin_joint_set_param(get_rid(), PhysicsServer2D::PIN_JOINT_MOTOR_TARGET_VELOCITY, motor_target_velocity);
  280. }
  281. }
  282. real_t PinJoint2D::get_motor_target_velocity() const {
  283. return motor_target_velocity;
  284. }
  285. void PinJoint2D::set_motor_enabled(bool p_motor_enabled) {
  286. if (motor_enabled == p_motor_enabled) {
  287. return;
  288. }
  289. motor_enabled = p_motor_enabled;
  290. queue_redraw();
  291. if (is_configured()) {
  292. PhysicsServer2D::get_singleton()->pin_joint_set_flag(get_rid(), PhysicsServer2D::PIN_JOINT_FLAG_MOTOR_ENABLED, motor_enabled);
  293. }
  294. }
  295. bool PinJoint2D::is_motor_enabled() const {
  296. return motor_enabled;
  297. }
  298. void PinJoint2D::set_angular_limit_enabled(bool p_angular_limit_enabled) {
  299. if (angular_limit_enabled == p_angular_limit_enabled) {
  300. return;
  301. }
  302. angular_limit_enabled = p_angular_limit_enabled;
  303. queue_redraw();
  304. if (is_configured()) {
  305. PhysicsServer2D::get_singleton()->pin_joint_set_flag(get_rid(), PhysicsServer2D::PIN_JOINT_FLAG_ANGULAR_LIMIT_ENABLED, angular_limit_enabled);
  306. }
  307. }
  308. bool PinJoint2D::is_angular_limit_enabled() const {
  309. return angular_limit_enabled;
  310. }
  311. void PinJoint2D::_bind_methods() {
  312. ClassDB::bind_method(D_METHOD("set_softness", "softness"), &PinJoint2D::set_softness);
  313. ClassDB::bind_method(D_METHOD("get_softness"), &PinJoint2D::get_softness);
  314. ClassDB::bind_method(D_METHOD("set_angular_limit_lower", "angular_limit_lower"), &PinJoint2D::set_angular_limit_lower);
  315. ClassDB::bind_method(D_METHOD("get_angular_limit_lower"), &PinJoint2D::get_angular_limit_lower);
  316. ClassDB::bind_method(D_METHOD("set_angular_limit_upper", "angular_limit_upper"), &PinJoint2D::set_angular_limit_upper);
  317. ClassDB::bind_method(D_METHOD("get_angular_limit_upper"), &PinJoint2D::get_angular_limit_upper);
  318. ClassDB::bind_method(D_METHOD("set_motor_target_velocity", "motor_target_velocity"), &PinJoint2D::set_motor_target_velocity);
  319. ClassDB::bind_method(D_METHOD("get_motor_target_velocity"), &PinJoint2D::get_motor_target_velocity);
  320. ClassDB::bind_method(D_METHOD("set_motor_enabled", "enabled"), &PinJoint2D::set_motor_enabled);
  321. ClassDB::bind_method(D_METHOD("is_motor_enabled"), &PinJoint2D::is_motor_enabled);
  322. ClassDB::bind_method(D_METHOD("set_angular_limit_enabled", "enabled"), &PinJoint2D::set_angular_limit_enabled);
  323. ClassDB::bind_method(D_METHOD("is_angular_limit_enabled"), &PinJoint2D::is_angular_limit_enabled);
  324. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "softness", PROPERTY_HINT_RANGE, "0.00,16,0.01,exp"), "set_softness", "get_softness");
  325. ADD_GROUP("Angular Limit", "angular_limit_");
  326. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "angular_limit_enabled"), "set_angular_limit_enabled", "is_angular_limit_enabled");
  327. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "angular_limit_lower", PROPERTY_HINT_RANGE, "-180,180,0.1,radians_as_degrees"), "set_angular_limit_lower", "get_angular_limit_lower");
  328. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "angular_limit_upper", PROPERTY_HINT_RANGE, "-180,180,0.1,radians_as_degrees"), "set_angular_limit_upper", "get_angular_limit_upper");
  329. ADD_GROUP("Motor", "motor_");
  330. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "motor_enabled"), "set_motor_enabled", "is_motor_enabled");
  331. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "motor_target_velocity", PROPERTY_HINT_RANGE, U"-200,200,0.01,or_greater,or_less,radians_as_degrees,suffix:\u00B0/s"), "set_motor_target_velocity", "get_motor_target_velocity");
  332. }
  333. PinJoint2D::PinJoint2D() {
  334. }
  335. ///////////////////////////////////////////////////////////////////////////////
  336. ///////////////////////////////////////////////////////////////////////////////
  337. ///////////////////////////////////////////////////////////////////////////////
  338. void GrooveJoint2D::_notification(int p_what) {
  339. switch (p_what) {
  340. case NOTIFICATION_DRAW: {
  341. if (!is_inside_tree()) {
  342. break;
  343. }
  344. if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) {
  345. break;
  346. }
  347. draw_line(Point2(-10, 0), Point2(+10, 0), Color(0.7, 0.6, 0.0, 0.5), 3);
  348. draw_line(Point2(-10, length), Point2(+10, length), Color(0.7, 0.6, 0.0, 0.5), 3);
  349. draw_line(Point2(0, 0), Point2(0, length), Color(0.7, 0.6, 0.0, 0.5), 3);
  350. draw_line(Point2(-10, initial_offset), Point2(+10, initial_offset), Color(0.8, 0.8, 0.9, 0.5), 5);
  351. } break;
  352. }
  353. }
  354. void GrooveJoint2D::_configure_joint(RID p_joint, PhysicsBody2D *body_a, PhysicsBody2D *body_b) {
  355. Transform2D gt = get_global_transform();
  356. Vector2 groove_A1 = gt.get_origin();
  357. Vector2 groove_A2 = gt.xform(Vector2(0, length));
  358. Vector2 anchor_B = gt.xform(Vector2(0, initial_offset));
  359. PhysicsServer2D::get_singleton()->joint_make_groove(p_joint, groove_A1, groove_A2, anchor_B, body_a->get_rid(), body_b->get_rid());
  360. }
  361. void GrooveJoint2D::set_length(real_t p_length) {
  362. length = p_length;
  363. queue_redraw();
  364. }
  365. real_t GrooveJoint2D::get_length() const {
  366. return length;
  367. }
  368. void GrooveJoint2D::set_initial_offset(real_t p_initial_offset) {
  369. initial_offset = p_initial_offset;
  370. queue_redraw();
  371. }
  372. real_t GrooveJoint2D::get_initial_offset() const {
  373. return initial_offset;
  374. }
  375. void GrooveJoint2D::_bind_methods() {
  376. ClassDB::bind_method(D_METHOD("set_length", "length"), &GrooveJoint2D::set_length);
  377. ClassDB::bind_method(D_METHOD("get_length"), &GrooveJoint2D::get_length);
  378. ClassDB::bind_method(D_METHOD("set_initial_offset", "offset"), &GrooveJoint2D::set_initial_offset);
  379. ClassDB::bind_method(D_METHOD("get_initial_offset"), &GrooveJoint2D::get_initial_offset);
  380. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "length", PROPERTY_HINT_RANGE, "1,65535,1,exp,suffix:px"), "set_length", "get_length");
  381. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "initial_offset", PROPERTY_HINT_RANGE, "1,65535,1,exp,suffix:px"), "set_initial_offset", "get_initial_offset");
  382. }
  383. GrooveJoint2D::GrooveJoint2D() {
  384. }
  385. ///////////////////////////////////////////////////////////////////////////////
  386. ///////////////////////////////////////////////////////////////////////////////
  387. ///////////////////////////////////////////////////////////////////////////////
  388. void DampedSpringJoint2D::_notification(int p_what) {
  389. switch (p_what) {
  390. case NOTIFICATION_DRAW: {
  391. if (!is_inside_tree()) {
  392. break;
  393. }
  394. if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) {
  395. break;
  396. }
  397. draw_line(Point2(-10, 0), Point2(+10, 0), Color(0.7, 0.6, 0.0, 0.5), 3);
  398. draw_line(Point2(-10, length), Point2(+10, length), Color(0.7, 0.6, 0.0, 0.5), 3);
  399. draw_line(Point2(0, 0), Point2(0, length), Color(0.7, 0.6, 0.0, 0.5), 3);
  400. } break;
  401. }
  402. }
  403. void DampedSpringJoint2D::_configure_joint(RID p_joint, PhysicsBody2D *body_a, PhysicsBody2D *body_b) {
  404. Transform2D gt = get_global_transform();
  405. Vector2 anchor_A = gt.get_origin();
  406. Vector2 anchor_B = gt.xform(Vector2(0, length));
  407. PhysicsServer2D::get_singleton()->joint_make_damped_spring(p_joint, anchor_A, anchor_B, body_a->get_rid(), body_b->get_rid());
  408. if (rest_length) {
  409. PhysicsServer2D::get_singleton()->damped_spring_joint_set_param(p_joint, PhysicsServer2D::DAMPED_SPRING_REST_LENGTH, rest_length);
  410. }
  411. PhysicsServer2D::get_singleton()->damped_spring_joint_set_param(p_joint, PhysicsServer2D::DAMPED_SPRING_STIFFNESS, stiffness);
  412. PhysicsServer2D::get_singleton()->damped_spring_joint_set_param(p_joint, PhysicsServer2D::DAMPED_SPRING_DAMPING, damping);
  413. }
  414. void DampedSpringJoint2D::set_length(real_t p_length) {
  415. length = p_length;
  416. queue_redraw();
  417. }
  418. real_t DampedSpringJoint2D::get_length() const {
  419. return length;
  420. }
  421. void DampedSpringJoint2D::set_rest_length(real_t p_rest_length) {
  422. rest_length = p_rest_length;
  423. queue_redraw();
  424. if (is_configured()) {
  425. PhysicsServer2D::get_singleton()->damped_spring_joint_set_param(get_rid(), PhysicsServer2D::DAMPED_SPRING_REST_LENGTH, p_rest_length ? p_rest_length : length);
  426. }
  427. }
  428. real_t DampedSpringJoint2D::get_rest_length() const {
  429. return rest_length;
  430. }
  431. void DampedSpringJoint2D::set_stiffness(real_t p_stiffness) {
  432. stiffness = p_stiffness;
  433. queue_redraw();
  434. if (is_configured()) {
  435. PhysicsServer2D::get_singleton()->damped_spring_joint_set_param(get_rid(), PhysicsServer2D::DAMPED_SPRING_STIFFNESS, p_stiffness);
  436. }
  437. }
  438. real_t DampedSpringJoint2D::get_stiffness() const {
  439. return stiffness;
  440. }
  441. void DampedSpringJoint2D::set_damping(real_t p_damping) {
  442. damping = p_damping;
  443. queue_redraw();
  444. if (is_configured()) {
  445. PhysicsServer2D::get_singleton()->damped_spring_joint_set_param(get_rid(), PhysicsServer2D::DAMPED_SPRING_DAMPING, p_damping);
  446. }
  447. }
  448. real_t DampedSpringJoint2D::get_damping() const {
  449. return damping;
  450. }
  451. void DampedSpringJoint2D::_bind_methods() {
  452. ClassDB::bind_method(D_METHOD("set_length", "length"), &DampedSpringJoint2D::set_length);
  453. ClassDB::bind_method(D_METHOD("get_length"), &DampedSpringJoint2D::get_length);
  454. ClassDB::bind_method(D_METHOD("set_rest_length", "rest_length"), &DampedSpringJoint2D::set_rest_length);
  455. ClassDB::bind_method(D_METHOD("get_rest_length"), &DampedSpringJoint2D::get_rest_length);
  456. ClassDB::bind_method(D_METHOD("set_stiffness", "stiffness"), &DampedSpringJoint2D::set_stiffness);
  457. ClassDB::bind_method(D_METHOD("get_stiffness"), &DampedSpringJoint2D::get_stiffness);
  458. ClassDB::bind_method(D_METHOD("set_damping", "damping"), &DampedSpringJoint2D::set_damping);
  459. ClassDB::bind_method(D_METHOD("get_damping"), &DampedSpringJoint2D::get_damping);
  460. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "length", PROPERTY_HINT_RANGE, "1,65535,1,exp,suffix:px"), "set_length", "get_length");
  461. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "rest_length", PROPERTY_HINT_RANGE, "0,65535,1,exp,suffix:px"), "set_rest_length", "get_rest_length");
  462. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "stiffness", PROPERTY_HINT_RANGE, "0.1,64,0.1,exp"), "set_stiffness", "get_stiffness");
  463. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "damping", PROPERTY_HINT_RANGE, "0.01,16,0.01,exp"), "set_damping", "get_damping");
  464. }
  465. DampedSpringJoint2D::DampedSpringJoint2D() {
  466. }