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