joint_2d.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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_rid() const { return joint; }
  63. Joint2D();
  64. ~Joint2D();
  65. };
  66. class PinJoint2D : public Joint2D {
  67. GDCLASS(PinJoint2D, Joint2D);
  68. real_t softness = 0.0;
  69. real_t angular_limit_lower = 0.0;
  70. real_t angular_limit_upper = 0.0;
  71. real_t motor_target_velocity = 0.0;
  72. bool motor_enabled = false;
  73. bool angular_limit_enabled = false;
  74. protected:
  75. void _notification(int p_what);
  76. virtual void _configure_joint(RID p_joint, PhysicsBody2D *body_a, PhysicsBody2D *body_b) override;
  77. static void _bind_methods();
  78. public:
  79. void set_softness(real_t p_softness);
  80. real_t get_softness() const;
  81. void set_angular_limit_lower(real_t p_angular_limit_lower);
  82. real_t get_angular_limit_lower() const;
  83. void set_angular_limit_upper(real_t p_angular_limit_upper);
  84. real_t get_angular_limit_upper() const;
  85. void set_motor_target_velocity(real_t p_motor_target_velocity);
  86. real_t get_motor_target_velocity() const;
  87. void set_motor_enabled(bool p_motor_enabled);
  88. bool is_motor_enabled() const;
  89. void set_angular_limit_enabled(bool p_angular_limit_enabled);
  90. bool is_angular_limit_enabled() const;
  91. PinJoint2D();
  92. };
  93. class GrooveJoint2D : public Joint2D {
  94. GDCLASS(GrooveJoint2D, Joint2D);
  95. real_t length = 50.0;
  96. real_t initial_offset = 25.0;
  97. protected:
  98. void _notification(int p_what);
  99. virtual void _configure_joint(RID p_joint, PhysicsBody2D *body_a, PhysicsBody2D *body_b) override;
  100. static void _bind_methods();
  101. public:
  102. void set_length(real_t p_length);
  103. real_t get_length() const;
  104. void set_initial_offset(real_t p_initial_offset);
  105. real_t get_initial_offset() const;
  106. GrooveJoint2D();
  107. };
  108. class DampedSpringJoint2D : public Joint2D {
  109. GDCLASS(DampedSpringJoint2D, Joint2D);
  110. real_t stiffness = 20.0;
  111. real_t damping = 1.0;
  112. real_t rest_length = 0.0;
  113. real_t length = 50.0;
  114. protected:
  115. void _notification(int p_what);
  116. virtual void _configure_joint(RID p_joint, PhysicsBody2D *body_a, PhysicsBody2D *body_b) override;
  117. static void _bind_methods();
  118. public:
  119. void set_length(real_t p_length);
  120. real_t get_length() const;
  121. void set_rest_length(real_t p_rest_length);
  122. real_t get_rest_length() const;
  123. void set_damping(real_t p_damping);
  124. real_t get_damping() const;
  125. void set_stiffness(real_t p_stiffness);
  126. real_t get_stiffness() const;
  127. DampedSpringJoint2D();
  128. };
  129. #endif // JOINT_2D_H