joint_2d.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**************************************************************************/
  2. /* joint_2d.h */
  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. #ifndef JOINT_2D_H
  31. #define JOINT_2D_H
  32. #include "node_2d.h"
  33. class PhysicsBody2D;
  34. class Joint2D : public Node2D {
  35. GDCLASS(Joint2D, Node2D);
  36. RID joint;
  37. RID ba, bb;
  38. NodePath a;
  39. NodePath b;
  40. real_t bias = 0.0;
  41. bool exclude_from_collision = true;
  42. bool configured = false;
  43. String warning;
  44. protected:
  45. void _disconnect_signals();
  46. void _body_exit_tree();
  47. void _update_joint(bool p_only_free = false);
  48. void _notification(int p_what);
  49. virtual void _configure_joint(RID p_joint, PhysicsBody2D *body_a, PhysicsBody2D *body_b) = 0;
  50. static void _bind_methods();
  51. _FORCE_INLINE_ bool is_configured() const { return configured; }
  52. public:
  53. virtual PackedStringArray get_configuration_warnings() const override;
  54. void set_node_a(const NodePath &p_node_a);
  55. NodePath get_node_a() const;
  56. void set_node_b(const NodePath &p_node_b);
  57. NodePath get_node_b() const;
  58. void set_bias(real_t p_bias);
  59. real_t get_bias() const;
  60. void set_exclude_nodes_from_collision(bool p_enable);
  61. bool get_exclude_nodes_from_collision() const;
  62. RID get_joint() const { return joint; }
  63. Joint2D();
  64. ~Joint2D();
  65. };
  66. class PinJoint2D : public Joint2D {
  67. GDCLASS(PinJoint2D, Joint2D);
  68. real_t softness = 0.0;
  69. protected:
  70. void _notification(int p_what);
  71. virtual void _configure_joint(RID p_joint, PhysicsBody2D *body_a, PhysicsBody2D *body_b) override;
  72. static void _bind_methods();
  73. public:
  74. void set_softness(real_t p_softness);
  75. real_t get_softness() const;
  76. PinJoint2D();
  77. };
  78. class GrooveJoint2D : public Joint2D {
  79. GDCLASS(GrooveJoint2D, Joint2D);
  80. real_t length = 50.0;
  81. real_t initial_offset = 25.0;
  82. protected:
  83. void _notification(int p_what);
  84. virtual void _configure_joint(RID p_joint, PhysicsBody2D *body_a, PhysicsBody2D *body_b) override;
  85. static void _bind_methods();
  86. public:
  87. void set_length(real_t p_length);
  88. real_t get_length() const;
  89. void set_initial_offset(real_t p_initial_offset);
  90. real_t get_initial_offset() const;
  91. GrooveJoint2D();
  92. };
  93. class DampedSpringJoint2D : public Joint2D {
  94. GDCLASS(DampedSpringJoint2D, Joint2D);
  95. real_t stiffness = 20.0;
  96. real_t damping = 1.0;
  97. real_t rest_length = 0.0;
  98. real_t length = 50.0;
  99. protected:
  100. void _notification(int p_what);
  101. virtual void _configure_joint(RID p_joint, PhysicsBody2D *body_a, PhysicsBody2D *body_b) override;
  102. static void _bind_methods();
  103. public:
  104. void set_length(real_t p_length);
  105. real_t get_length() const;
  106. void set_rest_length(real_t p_rest_length);
  107. real_t get_rest_length() const;
  108. void set_damping(real_t p_damping);
  109. real_t get_damping() const;
  110. void set_stiffness(real_t p_stiffness);
  111. real_t get_stiffness() const;
  112. DampedSpringJoint2D();
  113. };
  114. #endif // JOINT_2D_H