1 /*************************************************************************/
2 /*  joints_2d_sw.h                                                       */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 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 
31 #ifndef JOINTS_2D_SW_H
32 #define JOINTS_2D_SW_H
33 
34 #include "body_2d_sw.h"
35 #include "constraint_2d_sw.h"
36 
37 class Joint2DSW : public Constraint2DSW {
38 
39 	real_t max_force;
40 	real_t bias;
41 	real_t max_bias;
42 
43 public:
set_max_force(real_t p_force)44 	_FORCE_INLINE_ void set_max_force(real_t p_force) { max_force = p_force; }
get_max_force()45 	_FORCE_INLINE_ real_t get_max_force() const { return max_force; }
46 
set_bias(real_t p_bias)47 	_FORCE_INLINE_ void set_bias(real_t p_bias) { bias = p_bias; }
get_bias()48 	_FORCE_INLINE_ real_t get_bias() const { return bias; }
49 
set_max_bias(real_t p_bias)50 	_FORCE_INLINE_ void set_max_bias(real_t p_bias) { max_bias = p_bias; }
get_max_bias()51 	_FORCE_INLINE_ real_t get_max_bias() const { return max_bias; }
52 
53 	virtual Physics2DServer::JointType get_type() const = 0;
54 	Joint2DSW(Body2DSW **p_body_ptr = NULL, int p_body_count = 0) :
Constraint2DSW(p_body_ptr,p_body_count)55 			Constraint2DSW(p_body_ptr, p_body_count) {
56 		bias = 0;
57 		max_force = max_bias = 3.40282e+38;
58 	};
59 };
60 
61 class PinJoint2DSW : public Joint2DSW {
62 
63 	union {
64 		struct {
65 			Body2DSW *A;
66 			Body2DSW *B;
67 		};
68 
69 		Body2DSW *_arr[2];
70 	};
71 
72 	Transform2D M;
73 	Vector2 rA, rB;
74 	Vector2 anchor_A;
75 	Vector2 anchor_B;
76 	Vector2 bias;
77 	Vector2 P;
78 	real_t softness;
79 
80 public:
get_type()81 	virtual Physics2DServer::JointType get_type() const { return Physics2DServer::JOINT_PIN; }
82 
83 	virtual bool setup(real_t p_step);
84 	virtual void solve(real_t p_step);
85 
86 	void set_param(Physics2DServer::PinJointParam p_param, real_t p_value);
87 	real_t get_param(Physics2DServer::PinJointParam p_param) const;
88 
89 	PinJoint2DSW(const Vector2 &p_pos, Body2DSW *p_body_a, Body2DSW *p_body_b = NULL);
90 	~PinJoint2DSW();
91 };
92 
93 class GrooveJoint2DSW : public Joint2DSW {
94 
95 	union {
96 		struct {
97 			Body2DSW *A;
98 			Body2DSW *B;
99 		};
100 
101 		Body2DSW *_arr[2];
102 	};
103 
104 	Vector2 A_groove_1;
105 	Vector2 A_groove_2;
106 	Vector2 A_groove_normal;
107 	Vector2 B_anchor;
108 	Vector2 jn_acc;
109 	Vector2 gbias;
110 	real_t jn_max;
111 	real_t clamp;
112 	Vector2 xf_normal;
113 	Vector2 rA, rB;
114 	Vector2 k1, k2;
115 
116 	bool correct;
117 
118 public:
get_type()119 	virtual Physics2DServer::JointType get_type() const { return Physics2DServer::JOINT_GROOVE; }
120 
121 	virtual bool setup(real_t p_step);
122 	virtual void solve(real_t p_step);
123 
124 	GrooveJoint2DSW(const Vector2 &p_a_groove1, const Vector2 &p_a_groove2, const Vector2 &p_b_anchor, Body2DSW *p_body_a, Body2DSW *p_body_b);
125 	~GrooveJoint2DSW();
126 };
127 
128 class DampedSpringJoint2DSW : public Joint2DSW {
129 
130 	union {
131 		struct {
132 			Body2DSW *A;
133 			Body2DSW *B;
134 		};
135 
136 		Body2DSW *_arr[2];
137 	};
138 
139 	Vector2 anchor_A;
140 	Vector2 anchor_B;
141 
142 	real_t rest_length;
143 	real_t damping;
144 	real_t stiffness;
145 
146 	Vector2 rA, rB;
147 	Vector2 n;
148 	real_t n_mass;
149 	real_t target_vrn;
150 	real_t v_coef;
151 
152 public:
get_type()153 	virtual Physics2DServer::JointType get_type() const { return Physics2DServer::JOINT_DAMPED_SPRING; }
154 
155 	virtual bool setup(real_t p_step);
156 	virtual void solve(real_t p_step);
157 
158 	void set_param(Physics2DServer::DampedStringParam p_param, real_t p_value);
159 	real_t get_param(Physics2DServer::DampedStringParam p_param) const;
160 
161 	DampedSpringJoint2DSW(const Vector2 &p_anchor_a, const Vector2 &p_anchor_b, Body2DSW *p_body_a, Body2DSW *p_body_b);
162 	~DampedSpringJoint2DSW();
163 };
164 
165 #endif // JOINTS_2D_SW_H
166