1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/**************************************************************************/
/*  slider_joint_sw.h                                                     */
/**************************************************************************/
/*                         This file is part of:                          */
/*                             GODOT ENGINE                               */
/*                        https://godotengine.org                         */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */
/*                                                                        */
/* Permission is hereby granted, free of charge, to any person obtaining  */
/* a copy of this software and associated documentation files (the        */
/* "Software"), to deal in the Software without restriction, including    */
/* without limitation the rights to use, copy, modify, merge, publish,    */
/* distribute, sublicense, and/or sell copies of the Software, and to     */
/* permit persons to whom the Software is furnished to do so, subject to  */
/* the following conditions:                                              */
/*                                                                        */
/* The above copyright notice and this permission notice shall be         */
/* included in all copies or substantial portions of the Software.        */
/*                                                                        */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE      */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                 */
/**************************************************************************/

/*
Adapted to Godot from the Bullet library.
*/

#ifndef SLIDER_JOINT_SW_H
#define SLIDER_JOINT_SW_H

#include "servers/physics/joints/jacobian_entry_sw.h"
#include "servers/physics/joints_sw.h"

/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/

This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/

/*
Added by Roman Ponomarev (rponom@gmail.com)
April 04, 2008

*/

#define SLIDER_CONSTRAINT_DEF_SOFTNESS (real_t(1.0))
#define SLIDER_CONSTRAINT_DEF_DAMPING (real_t(1.0))
#define SLIDER_CONSTRAINT_DEF_RESTITUTION (real_t(0.7))

//-----------------------------------------------------------------------------

class SliderJointSW : public JointSW {
protected:
	union {
		struct {
			BodySW *A;
			BodySW *B;
		};

		BodySW *_arr[2];
	};

	Transform m_frameInA;
	Transform m_frameInB;

	// linear limits
	real_t m_lowerLinLimit;
	real_t m_upperLinLimit;
	// angular limits
	real_t m_lowerAngLimit;
	real_t m_upperAngLimit;
	// softness, restitution and damping for different cases
	// DirLin - moving inside linear limits
	// LimLin - hitting linear limit
	// DirAng - moving inside angular limits
	// LimAng - hitting angular limit
	// OrthoLin, OrthoAng - against constraint axis
	real_t m_softnessDirLin;
	real_t m_restitutionDirLin;
	real_t m_dampingDirLin;
	real_t m_softnessDirAng;
	real_t m_restitutionDirAng;
	real_t m_dampingDirAng;
	real_t m_softnessLimLin;
	real_t m_restitutionLimLin;
	real_t m_dampingLimLin;
	real_t m_softnessLimAng;
	real_t m_restitutionLimAng;
	real_t m_dampingLimAng;
	real_t m_softnessOrthoLin;
	real_t m_restitutionOrthoLin;
	real_t m_dampingOrthoLin;
	real_t m_softnessOrthoAng;
	real_t m_restitutionOrthoAng;
	real_t m_dampingOrthoAng;

	// for interlal use
	bool m_solveLinLim;
	bool m_solveAngLim;

	JacobianEntrySW m_jacLin[3];
	real_t m_jacLinDiagABInv[3];

	JacobianEntrySW m_jacAng[3];

	real_t m_timeStep;
	Transform m_calculatedTransformA;
	Transform m_calculatedTransformB;

	Vector3 m_sliderAxis;
	Vector3 m_realPivotAInW;
	Vector3 m_realPivotBInW;
	Vector3 m_projPivotInW;
	Vector3 m_delta;
	Vector3 m_depth;
	Vector3 m_relPosA;
	Vector3 m_relPosB;

	real_t m_linPos;

	real_t m_angDepth;
	real_t m_kAngle;

	bool m_poweredLinMotor;
	real_t m_targetLinMotorVelocity;
	real_t m_maxLinMotorForce;
	real_t m_accumulatedLinMotorImpulse;

	bool m_poweredAngMotor;
	real_t m_targetAngMotorVelocity;
	real_t m_maxAngMotorForce;
	real_t m_accumulatedAngMotorImpulse;

	//------------------------
	void initParams();

private:
	_FORCE_INLINE_ real_t atan2fast(real_t y, real_t x);

public:
	// constructors
	SliderJointSW(BodySW *rbA, BodySW *rbB, const Transform &frameInA, const Transform &frameInB);
	//SliderJointSW();
	// overrides

	// access
	const BodySW *getRigidBodyA() const { return A; }
	const BodySW *getRigidBodyB() const { return B; }
	const Transform &getCalculatedTransformA() const { return m_calculatedTransformA; }
	const Transform &getCalculatedTransformB() const { return m_calculatedTransformB; }
	const Transform &getFrameOffsetA() const { return m_frameInA; }
	const Transform &getFrameOffsetB() const { return m_frameInB; }
	Transform &getFrameOffsetA() { return m_frameInA; }
	Transform &getFrameOffsetB() { return m_frameInB; }
	real_t getLowerLinLimit() { return m_lowerLinLimit; }
	void setLowerLinLimit(real_t lowerLimit) { m_lowerLinLimit = lowerLimit; }
	real_t getUpperLinLimit() { return m_upperLinLimit; }
	void setUpperLinLimit(real_t upperLimit) { m_upperLinLimit = upperLimit; }
	real_t getLowerAngLimit() { return m_lowerAngLimit; }
	void setLowerAngLimit(real_t lowerLimit) { m_lowerAngLimit = lowerLimit; }
	real_t getUpperAngLimit() { return m_upperAngLimit; }
	void setUpperAngLimit(real_t upperLimit) { m_upperAngLimit = upperLimit; }

	real_t getSoftnessDirLin() { return m_softnessDirLin; }
	real_t getRestitutionDirLin() { return m_restitutionDirLin; }
	real_t getDampingDirLin() { return m_dampingDirLin; }
	real_t getSoftnessDirAng() { return m_softnessDirAng; }
	real_t getRestitutionDirAng() { return m_restitutionDirAng; }
	real_t getDampingDirAng() { return m_dampingDirAng; }
	real_t getSoftnessLimLin() { return m_softnessLimLin; }
	real_t getRestitutionLimLin() { return m_restitutionLimLin; }
	real_t getDampingLimLin() { return m_dampingLimLin; }
	real_t getSoftnessLimAng() { return m_softnessLimAng; }
	real_t getRestitutionLimAng() { return m_restitutionLimAng; }
	real_t getDampingLimAng() { return m_dampingLimAng; }
	real_t getSoftnessOrthoLin() { return m_softnessOrthoLin; }
	real_t getRestitutionOrthoLin() { return m_restitutionOrthoLin; }
	real_t getDampingOrthoLin() { return m_dampingOrthoLin; }
	real_t getSoftnessOrthoAng() { return m_softnessOrthoAng; }
	real_t getRestitutionOrthoAng() { return m_restitutionOrthoAng; }
	real_t getDampingOrthoAng() { return m_dampingOrthoAng; }
	void setSoftnessDirLin(real_t softnessDirLin) { m_softnessDirLin = softnessDirLin; }
	void setRestitutionDirLin(real_t restitutionDirLin) { m_restitutionDirLin = restitutionDirLin; }
	void setDampingDirLin(real_t dampingDirLin) { m_dampingDirLin = dampingDirLin; }
	void setSoftnessDirAng(real_t softnessDirAng) { m_softnessDirAng = softnessDirAng; }
	void setRestitutionDirAng(real_t restitutionDirAng) { m_restitutionDirAng = restitutionDirAng; }
	void setDampingDirAng(real_t dampingDirAng) { m_dampingDirAng = dampingDirAng; }
	void setSoftnessLimLin(real_t softnessLimLin) { m_softnessLimLin = softnessLimLin; }
	void setRestitutionLimLin(real_t restitutionLimLin) { m_restitutionLimLin = restitutionLimLin; }
	void setDampingLimLin(real_t dampingLimLin) { m_dampingLimLin = dampingLimLin; }
	void setSoftnessLimAng(real_t softnessLimAng) { m_softnessLimAng = softnessLimAng; }
	void setRestitutionLimAng(real_t restitutionLimAng) { m_restitutionLimAng = restitutionLimAng; }
	void setDampingLimAng(real_t dampingLimAng) { m_dampingLimAng = dampingLimAng; }
	void setSoftnessOrthoLin(real_t softnessOrthoLin) { m_softnessOrthoLin = softnessOrthoLin; }
	void setRestitutionOrthoLin(real_t restitutionOrthoLin) { m_restitutionOrthoLin = restitutionOrthoLin; }
	void setDampingOrthoLin(real_t dampingOrthoLin) { m_dampingOrthoLin = dampingOrthoLin; }
	void setSoftnessOrthoAng(real_t softnessOrthoAng) { m_softnessOrthoAng = softnessOrthoAng; }
	void setRestitutionOrthoAng(real_t restitutionOrthoAng) { m_restitutionOrthoAng = restitutionOrthoAng; }
	void setDampingOrthoAng(real_t dampingOrthoAng) { m_dampingOrthoAng = dampingOrthoAng; }
	void setPoweredLinMotor(bool onOff) { m_poweredLinMotor = onOff; }
	bool getPoweredLinMotor() { return m_poweredLinMotor; }
	void setTargetLinMotorVelocity(real_t targetLinMotorVelocity) { m_targetLinMotorVelocity = targetLinMotorVelocity; }
	real_t getTargetLinMotorVelocity() { return m_targetLinMotorVelocity; }
	void setMaxLinMotorForce(real_t maxLinMotorForce) { m_maxLinMotorForce = maxLinMotorForce; }
	real_t getMaxLinMotorForce() { return m_maxLinMotorForce; }
	void setPoweredAngMotor(bool onOff) { m_poweredAngMotor = onOff; }
	bool getPoweredAngMotor() { return m_poweredAngMotor; }
	void setTargetAngMotorVelocity(real_t targetAngMotorVelocity) { m_targetAngMotorVelocity = targetAngMotorVelocity; }
	real_t getTargetAngMotorVelocity() { return m_targetAngMotorVelocity; }
	void setMaxAngMotorForce(real_t maxAngMotorForce) { m_maxAngMotorForce = maxAngMotorForce; }
	real_t getMaxAngMotorForce() { return m_maxAngMotorForce; }
	real_t getLinearPos() { return m_linPos; }

	// access for ODE solver
	bool getSolveLinLimit() { return m_solveLinLim; }
	real_t getLinDepth() { return m_depth[0]; }
	bool getSolveAngLimit() { return m_solveAngLim; }
	real_t getAngDepth() { return m_angDepth; }
	// shared code used by ODE solver
	void calculateTransforms();
	void testLinLimits();
	void testAngLimits();
	// access for PE Solver
	Vector3 getAncorInA();
	Vector3 getAncorInB();

	void set_param(PhysicsServer::SliderJointParam p_param, real_t p_value);
	real_t get_param(PhysicsServer::SliderJointParam p_param) const;

	bool setup(real_t p_step);<--- Function in derived class
	void solve(real_t p_step);<--- Function in derived class

	virtual PhysicsServer::JointType get_type() const { return PhysicsServer::JOINT_SLIDER; }<--- Function in derived class
};

#endif // SLIDER_JOINT_SW_H