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