joints_2d.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*************************************************************************/
  2. /* joints_2d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "joints_2d.h"
  31. #include "engine.h"
  32. #include "physics_body_2d.h"
  33. #include "servers/physics_2d_server.h"
  34. void Joint2D::_update_joint(bool p_only_free) {
  35. if (joint.is_valid()) {
  36. if (ba.is_valid() && bb.is_valid())
  37. Physics2DServer::get_singleton()->body_remove_collision_exception(ba, bb);
  38. Physics2DServer::get_singleton()->free(joint);
  39. joint = RID();
  40. ba = RID();
  41. bb = RID();
  42. }
  43. if (p_only_free || !is_inside_tree())
  44. return;
  45. Node *node_a = has_node(get_node_a()) ? get_node(get_node_a()) : (Node *)NULL;
  46. Node *node_b = has_node(get_node_b()) ? get_node(get_node_b()) : (Node *)NULL;
  47. if (!node_a || !node_b)
  48. return;
  49. PhysicsBody2D *body_a = Object::cast_to<PhysicsBody2D>(node_a);
  50. PhysicsBody2D *body_b = Object::cast_to<PhysicsBody2D>(node_b);
  51. if (!body_a || !body_b)
  52. return;
  53. if (!body_a) {
  54. SWAP(body_a, body_b);
  55. }
  56. joint = _configure_joint(body_a, body_b);
  57. if (!joint.is_valid())
  58. return;
  59. Physics2DServer::get_singleton()->get_singleton()->joint_set_param(joint, Physics2DServer::JOINT_PARAM_BIAS, bias);
  60. ba = body_a->get_rid();
  61. bb = body_b->get_rid();
  62. if (exclude_from_collision)
  63. Physics2DServer::get_singleton()->body_add_collision_exception(body_a->get_rid(), body_b->get_rid());
  64. }
  65. void Joint2D::set_node_a(const NodePath &p_node_a) {
  66. if (a == p_node_a)
  67. return;
  68. a = p_node_a;
  69. _update_joint();
  70. }
  71. NodePath Joint2D::get_node_a() const {
  72. return a;
  73. }
  74. void Joint2D::set_node_b(const NodePath &p_node_b) {
  75. if (b == p_node_b)
  76. return;
  77. b = p_node_b;
  78. _update_joint();
  79. }
  80. NodePath Joint2D::get_node_b() const {
  81. return b;
  82. }
  83. void Joint2D::_notification(int p_what) {
  84. switch (p_what) {
  85. case NOTIFICATION_READY: {
  86. _update_joint();
  87. } break;
  88. case NOTIFICATION_EXIT_TREE: {
  89. if (joint.is_valid()) {
  90. _update_joint(true);
  91. }
  92. } break;
  93. }
  94. }
  95. void Joint2D::set_bias(real_t p_bias) {
  96. bias = p_bias;
  97. if (joint.is_valid())
  98. Physics2DServer::get_singleton()->get_singleton()->joint_set_param(joint, Physics2DServer::JOINT_PARAM_BIAS, bias);
  99. }
  100. real_t Joint2D::get_bias() const {
  101. return bias;
  102. }
  103. void Joint2D::set_exclude_nodes_from_collision(bool p_enable) {
  104. if (exclude_from_collision == p_enable)
  105. return;
  106. exclude_from_collision = p_enable;
  107. _update_joint();
  108. }
  109. bool Joint2D::get_exclude_nodes_from_collision() const {
  110. return exclude_from_collision;
  111. }
  112. void Joint2D::_bind_methods() {
  113. ClassDB::bind_method(D_METHOD("set_node_a", "node"), &Joint2D::set_node_a);
  114. ClassDB::bind_method(D_METHOD("get_node_a"), &Joint2D::get_node_a);
  115. ClassDB::bind_method(D_METHOD("set_node_b", "node"), &Joint2D::set_node_b);
  116. ClassDB::bind_method(D_METHOD("get_node_b"), &Joint2D::get_node_b);
  117. ClassDB::bind_method(D_METHOD("set_bias", "bias"), &Joint2D::set_bias);
  118. ClassDB::bind_method(D_METHOD("get_bias"), &Joint2D::get_bias);
  119. ClassDB::bind_method(D_METHOD("set_exclude_nodes_from_collision", "enable"), &Joint2D::set_exclude_nodes_from_collision);
  120. ClassDB::bind_method(D_METHOD("get_exclude_nodes_from_collision"), &Joint2D::get_exclude_nodes_from_collision);
  121. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "node_a"), "set_node_a", "get_node_a");
  122. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "node_b"), "set_node_b", "get_node_b");
  123. ADD_PROPERTY(PropertyInfo(Variant::REAL, "bias", PROPERTY_HINT_RANGE, "0,0.9,0.001"), "set_bias", "get_bias");
  124. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disable_collision"), "set_exclude_nodes_from_collision", "get_exclude_nodes_from_collision");
  125. }
  126. Joint2D::Joint2D() {
  127. bias = 0;
  128. exclude_from_collision = true;
  129. }
  130. ///////////////////////////////////////////////////////////////////////////////
  131. ///////////////////////////////////////////////////////////////////////////////
  132. ///////////////////////////////////////////////////////////////////////////////
  133. void PinJoint2D::_notification(int p_what) {
  134. switch (p_what) {
  135. case NOTIFICATION_DRAW: {
  136. if (!is_inside_tree())
  137. break;
  138. if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) {
  139. break;
  140. }
  141. draw_line(Point2(-10, 0), Point2(+10, 0), Color(0.7, 0.6, 0.0, 0.5), 3);
  142. draw_line(Point2(0, -10), Point2(0, +10), Color(0.7, 0.6, 0.0, 0.5), 3);
  143. } break;
  144. }
  145. }
  146. RID PinJoint2D::_configure_joint(PhysicsBody2D *body_a, PhysicsBody2D *body_b) {
  147. RID pj = Physics2DServer::get_singleton()->pin_joint_create(get_global_transform().get_origin(), body_a->get_rid(), body_b ? body_b->get_rid() : RID());
  148. Physics2DServer::get_singleton()->pin_joint_set_param(pj, Physics2DServer::PIN_JOINT_SOFTNESS, softness);
  149. return pj;
  150. }
  151. void PinJoint2D::set_softness(real_t p_softness) {
  152. softness = p_softness;
  153. update();
  154. if (get_joint().is_valid())
  155. Physics2DServer::get_singleton()->pin_joint_set_param(get_joint(), Physics2DServer::PIN_JOINT_SOFTNESS, p_softness);
  156. }
  157. real_t PinJoint2D::get_softness() const {
  158. return softness;
  159. }
  160. void PinJoint2D::_bind_methods() {
  161. ClassDB::bind_method(D_METHOD("set_softness", "softness"), &PinJoint2D::set_softness);
  162. ClassDB::bind_method(D_METHOD("get_softness"), &PinJoint2D::get_softness);
  163. ADD_PROPERTY(PropertyInfo(Variant::REAL, "softness", PROPERTY_HINT_EXP_RANGE, "0.00,16,0.01"), "set_softness", "get_softness");
  164. }
  165. PinJoint2D::PinJoint2D() {
  166. softness = 0;
  167. }
  168. ///////////////////////////////////////////////////////////////////////////////
  169. ///////////////////////////////////////////////////////////////////////////////
  170. ///////////////////////////////////////////////////////////////////////////////
  171. void GrooveJoint2D::_notification(int p_what) {
  172. switch (p_what) {
  173. case NOTIFICATION_DRAW: {
  174. if (!is_inside_tree())
  175. break;
  176. if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) {
  177. break;
  178. }
  179. draw_line(Point2(-10, 0), Point2(+10, 0), Color(0.7, 0.6, 0.0, 0.5), 3);
  180. draw_line(Point2(-10, length), Point2(+10, length), Color(0.7, 0.6, 0.0, 0.5), 3);
  181. draw_line(Point2(0, 0), Point2(0, length), Color(0.7, 0.6, 0.0, 0.5), 3);
  182. draw_line(Point2(-10, initial_offset), Point2(+10, initial_offset), Color(0.8, 0.8, 0.9, 0.5), 5);
  183. } break;
  184. }
  185. }
  186. RID GrooveJoint2D::_configure_joint(PhysicsBody2D *body_a, PhysicsBody2D *body_b) {
  187. Transform2D gt = get_global_transform();
  188. Vector2 groove_A1 = gt.get_origin();
  189. Vector2 groove_A2 = gt.xform(Vector2(0, length));
  190. Vector2 anchor_B = gt.xform(Vector2(0, initial_offset));
  191. return Physics2DServer::get_singleton()->groove_joint_create(groove_A1, groove_A2, anchor_B, body_a->get_rid(), body_b->get_rid());
  192. }
  193. void GrooveJoint2D::set_length(real_t p_length) {
  194. length = p_length;
  195. update();
  196. }
  197. real_t GrooveJoint2D::get_length() const {
  198. return length;
  199. }
  200. void GrooveJoint2D::set_initial_offset(real_t p_initial_offset) {
  201. initial_offset = p_initial_offset;
  202. update();
  203. }
  204. real_t GrooveJoint2D::get_initial_offset() const {
  205. return initial_offset;
  206. }
  207. void GrooveJoint2D::_bind_methods() {
  208. ClassDB::bind_method(D_METHOD("set_length", "length"), &GrooveJoint2D::set_length);
  209. ClassDB::bind_method(D_METHOD("get_length"), &GrooveJoint2D::get_length);
  210. ClassDB::bind_method(D_METHOD("set_initial_offset", "offset"), &GrooveJoint2D::set_initial_offset);
  211. ClassDB::bind_method(D_METHOD("get_initial_offset"), &GrooveJoint2D::get_initial_offset);
  212. ADD_PROPERTY(PropertyInfo(Variant::REAL, "length", PROPERTY_HINT_EXP_RANGE, "1,65535,1"), "set_length", "get_length");
  213. ADD_PROPERTY(PropertyInfo(Variant::REAL, "initial_offset", PROPERTY_HINT_EXP_RANGE, "1,65535,1"), "set_initial_offset", "get_initial_offset");
  214. }
  215. GrooveJoint2D::GrooveJoint2D() {
  216. length = 50;
  217. initial_offset = 25;
  218. }
  219. ///////////////////////////////////////////////////////////////////////////////
  220. ///////////////////////////////////////////////////////////////////////////////
  221. ///////////////////////////////////////////////////////////////////////////////
  222. void DampedSpringJoint2D::_notification(int p_what) {
  223. switch (p_what) {
  224. case NOTIFICATION_DRAW: {
  225. if (!is_inside_tree())
  226. break;
  227. if (!Engine::get_singleton()->is_editor_hint() && !get_tree()->is_debugging_collisions_hint()) {
  228. break;
  229. }
  230. draw_line(Point2(-10, 0), Point2(+10, 0), Color(0.7, 0.6, 0.0, 0.5), 3);
  231. draw_line(Point2(-10, length), Point2(+10, length), Color(0.7, 0.6, 0.0, 0.5), 3);
  232. draw_line(Point2(0, 0), Point2(0, length), Color(0.7, 0.6, 0.0, 0.5), 3);
  233. } break;
  234. }
  235. }
  236. RID DampedSpringJoint2D::_configure_joint(PhysicsBody2D *body_a, PhysicsBody2D *body_b) {
  237. Transform2D gt = get_global_transform();
  238. Vector2 anchor_A = gt.get_origin();
  239. Vector2 anchor_B = gt.xform(Vector2(0, length));
  240. RID dsj = Physics2DServer::get_singleton()->damped_spring_joint_create(anchor_A, anchor_B, body_a->get_rid(), body_b->get_rid());
  241. if (rest_length)
  242. Physics2DServer::get_singleton()->damped_string_joint_set_param(dsj, Physics2DServer::DAMPED_STRING_REST_LENGTH, rest_length);
  243. Physics2DServer::get_singleton()->damped_string_joint_set_param(dsj, Physics2DServer::DAMPED_STRING_STIFFNESS, stiffness);
  244. Physics2DServer::get_singleton()->damped_string_joint_set_param(dsj, Physics2DServer::DAMPED_STRING_DAMPING, damping);
  245. return dsj;
  246. }
  247. void DampedSpringJoint2D::set_length(real_t p_length) {
  248. length = p_length;
  249. update();
  250. }
  251. real_t DampedSpringJoint2D::get_length() const {
  252. return length;
  253. }
  254. void DampedSpringJoint2D::set_rest_length(real_t p_rest_length) {
  255. rest_length = p_rest_length;
  256. update();
  257. if (get_joint().is_valid())
  258. Physics2DServer::get_singleton()->damped_string_joint_set_param(get_joint(), Physics2DServer::DAMPED_STRING_REST_LENGTH, p_rest_length ? p_rest_length : length);
  259. }
  260. real_t DampedSpringJoint2D::get_rest_length() const {
  261. return rest_length;
  262. }
  263. void DampedSpringJoint2D::set_stiffness(real_t p_stiffness) {
  264. stiffness = p_stiffness;
  265. update();
  266. if (get_joint().is_valid())
  267. Physics2DServer::get_singleton()->damped_string_joint_set_param(get_joint(), Physics2DServer::DAMPED_STRING_STIFFNESS, p_stiffness);
  268. }
  269. real_t DampedSpringJoint2D::get_stiffness() const {
  270. return stiffness;
  271. }
  272. void DampedSpringJoint2D::set_damping(real_t p_damping) {
  273. damping = p_damping;
  274. update();
  275. if (get_joint().is_valid())
  276. Physics2DServer::get_singleton()->damped_string_joint_set_param(get_joint(), Physics2DServer::DAMPED_STRING_DAMPING, p_damping);
  277. }
  278. real_t DampedSpringJoint2D::get_damping() const {
  279. return damping;
  280. }
  281. void DampedSpringJoint2D::_bind_methods() {
  282. ClassDB::bind_method(D_METHOD("set_length", "length"), &DampedSpringJoint2D::set_length);
  283. ClassDB::bind_method(D_METHOD("get_length"), &DampedSpringJoint2D::get_length);
  284. ClassDB::bind_method(D_METHOD("set_rest_length", "rest_length"), &DampedSpringJoint2D::set_rest_length);
  285. ClassDB::bind_method(D_METHOD("get_rest_length"), &DampedSpringJoint2D::get_rest_length);
  286. ClassDB::bind_method(D_METHOD("set_stiffness", "stiffness"), &DampedSpringJoint2D::set_stiffness);
  287. ClassDB::bind_method(D_METHOD("get_stiffness"), &DampedSpringJoint2D::get_stiffness);
  288. ClassDB::bind_method(D_METHOD("set_damping", "damping"), &DampedSpringJoint2D::set_damping);
  289. ClassDB::bind_method(D_METHOD("get_damping"), &DampedSpringJoint2D::get_damping);
  290. ADD_PROPERTY(PropertyInfo(Variant::REAL, "length", PROPERTY_HINT_EXP_RANGE, "1,65535,1"), "set_length", "get_length");
  291. ADD_PROPERTY(PropertyInfo(Variant::REAL, "rest_length", PROPERTY_HINT_EXP_RANGE, "0,65535,1"), "set_rest_length", "get_rest_length");
  292. ADD_PROPERTY(PropertyInfo(Variant::REAL, "stiffness", PROPERTY_HINT_EXP_RANGE, "0.1,64,0.1"), "set_stiffness", "get_stiffness");
  293. ADD_PROPERTY(PropertyInfo(Variant::REAL, "damping", PROPERTY_HINT_EXP_RANGE, "0.01,16,0.01"), "set_damping", "get_damping");
  294. }
  295. DampedSpringJoint2D::DampedSpringJoint2D() {
  296. length = 50;
  297. rest_length = 0;
  298. stiffness = 20;
  299. damping = 1;
  300. }