summaryrefslogtreecommitdiff
path: root/servers/physics/joints/cone_twist_joint_sw.cpp
blob: d97d8c599f4622ffc88ec20d014f248f2b78c200 (plain)
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include "cone_twist_joint_sw.h"

static void plane_space(const Vector3& n, Vector3& p, Vector3& q) {

  if (Math::abs(n.z) > 0.707106781186547524400844362) {
    // choose p in y-z plane
    real_t a = n[1]*n[1] + n[2]*n[2];
    real_t k = 1.0/Math::sqrt(a);
    p=Vector3(0,-n[2]*k,n[1]*k);
    // set q = n x p
    q=Vector3(a*k,-n[0]*p[2],n[0]*p[1]);
  }
  else {
    // choose p in x-y plane
    real_t a = n.x*n.x + n.y*n.y;
    real_t k = 1.0/Math::sqrt(a);
    p=Vector3(-n.y*k,n.x*k,0);
    // set q = n x p
    q=Vector3(-n.z*p.y,n.z*p.x,a*k);
  }
}


static _FORCE_INLINE_ real_t atan2fast(real_t y, real_t x)
{
	real_t coeff_1 = Math_PI / 4.0f;
	real_t coeff_2 = 3.0f * coeff_1;
	real_t abs_y = Math::abs(y);
	real_t angle;
	if (x >= 0.0f) {
		real_t r = (x - abs_y) / (x + abs_y);
		angle = coeff_1 - coeff_1 * r;
	} else {
		real_t r = (x + abs_y) / (abs_y - x);
		angle = coeff_2 - coeff_1 * r;
	}
	return (y < 0.0f) ? -angle : angle;
}

ConeTwistJointSW::ConeTwistJointSW(BodySW* rbA,BodySW* rbB,const Transform& rbAFrame, const Transform& rbBFrame) :  JointSW(_arr,2) {

	A=rbA;
	B=rbB;


	m_rbAFrame=rbAFrame;
	m_rbBFrame=rbBFrame;

	m_swingSpan1 = Math_PI/4.0;
	m_swingSpan2 = Math_PI/4.0;
	m_twistSpan  = Math_PI*2;
	m_biasFactor = 0.3f;
	m_relaxationFactor = 1.0f;

	m_solveTwistLimit = false;
	m_solveSwingLimit = false;

	A->add_constraint(this,0);
	B->add_constraint(this,1);

	m_appliedImpulse=0;
}


bool	ConeTwistJointSW::setup(float p_step) {
	m_appliedImpulse = real_t(0.);

	//set bias, sign, clear accumulator
	m_swingCorrection = real_t(0.);
	m_twistLimitSign = real_t(0.);
	m_solveTwistLimit = false;
	m_solveSwingLimit = false;
	m_accTwistLimitImpulse = real_t(0.);
	m_accSwingLimitImpulse = real_t(0.);

	if (!m_angularOnly)
	{
		Vector3 pivotAInW = A->get_transform().xform(m_rbAFrame.origin);
		Vector3 pivotBInW = B->get_transform().xform(m_rbBFrame.origin);
		Vector3 relPos = pivotBInW - pivotAInW;

		Vector3 normal[3];
		if (relPos.length_squared() > CMP_EPSILON)
		{
			normal[0] = relPos.normalized();
		}
		else
		{
			normal[0]=Vector3(real_t(1.0),0,0);
		}

		plane_space(normal[0], normal[1], normal[2]);

		for (int i=0;i<3;i++)
		{
			memnew_placement(&m_jac[i], JacobianEntrySW(
				A->get_transform().basis.transposed(),
				B->get_transform().basis.transposed(),
				pivotAInW - A->get_transform().origin,
				pivotBInW - B->get_transform().origin,
				normal[i],
				A->get_inv_inertia(),
				A->get_inv_mass(),
				B->get_inv_inertia(),
				B->get_inv_mass()));
		}
	}

	Vector3 b1Axis1,b1Axis2,b1Axis3;
	Vector3 b2Axis1,b2Axis2;

	b1Axis1 = A->get_transform().basis.xform( this->m_rbAFrame.basis.get_axis(0) );
	b2Axis1 = B->get_transform().basis.xform( this->m_rbBFrame.basis.get_axis(0) );

	real_t swing1=real_t(0.),swing2 = real_t(0.);

	real_t swx=real_t(0.),swy = real_t(0.);
	real_t thresh = real_t(10.);
	real_t fact;

	// Get Frame into world space
	if (m_swingSpan1 >= real_t(0.05f))
	{
		b1Axis2 = A->get_transform().basis.xform( this->m_rbAFrame.basis.get_axis(1) );
//		swing1  = btAtan2Fast( b2Axis1.dot(b1Axis2),b2Axis1.dot(b1Axis1) );
		swx = b2Axis1.dot(b1Axis1);
		swy = b2Axis1.dot(b1Axis2);
		swing1  = atan2fast(swy, swx);
		fact = (swy*swy + swx*swx) * thresh * thresh;
		fact = fact / (fact + real_t(1.0));
		swing1 *= fact;

	}

	if (m_swingSpan2 >= real_t(0.05f))
	{
		b1Axis3 = A->get_transform().basis.xform( this->m_rbAFrame.basis.get_axis(2) );
//		swing2 = btAtan2Fast( b2Axis1.dot(b1Axis3),b2Axis1.dot(b1Axis1) );
		swx = b2Axis1.dot(b1Axis1);
		swy = b2Axis1.dot(b1Axis3);
		swing2  = atan2fast(swy, swx);
		fact = (swy*swy + swx*swx) * thresh * thresh;
		fact = fact / (fact + real_t(1.0));
		swing2 *= fact;
	}

	real_t RMaxAngle1Sq = 1.0f / (m_swingSpan1*m_swingSpan1);
	real_t RMaxAngle2Sq = 1.0f / (m_swingSpan2*m_swingSpan2);
	real_t EllipseAngle = Math::abs(swing1*swing1)* RMaxAngle1Sq + Math::abs(swing2*swing2) * RMaxAngle2Sq;

	if (EllipseAngle > 1.0f)
	{
		m_swingCorrection = EllipseAngle-1.0f;
		m_solveSwingLimit = true;

		// Calculate necessary axis & factors
		m_swingAxis = b2Axis1.cross(b1Axis2* b2Axis1.dot(b1Axis2) + b1Axis3* b2Axis1.dot(b1Axis3));
		m_swingAxis.normalize();

		real_t swingAxisSign = (b2Axis1.dot(b1Axis1) >= 0.0f) ? 1.0f : -1.0f;
		m_swingAxis *= swingAxisSign;

		m_kSwing =  real_t(1.) / (A->compute_angular_impulse_denominator(m_swingAxis) +
			B->compute_angular_impulse_denominator(m_swingAxis));

	}

	// Twist limits
	if (m_twistSpan >= real_t(0.))
	{
		Vector3 b2Axis2 = B->get_transform().basis.xform( this->m_rbBFrame.basis.get_axis(1) );
		Quat rotationArc = Quat(b2Axis1,b1Axis1);
		Vector3 TwistRef = rotationArc.xform(b2Axis2);
		real_t twist = atan2fast( TwistRef.dot(b1Axis3), TwistRef.dot(b1Axis2) );

		real_t lockedFreeFactor = (m_twistSpan > real_t(0.05f)) ? m_limitSoftness : real_t(0.);
		if (twist <= -m_twistSpan*lockedFreeFactor)
		{
			m_twistCorrection = -(twist + m_twistSpan);
			m_solveTwistLimit = true;

			m_twistAxis = (b2Axis1 + b1Axis1) * 0.5f;
			m_twistAxis.normalize();
			m_twistAxis *= -1.0f;

			m_kTwist = real_t(1.) / (A->compute_angular_impulse_denominator(m_twistAxis) +
				B->compute_angular_impulse_denominator(m_twistAxis));

		}	else
			if (twist >  m_twistSpan*lockedFreeFactor)
			{
				m_twistCorrection = (twist - m_twistSpan);
				m_solveTwistLimit = true;

				m_twistAxis = (b2Axis1 + b1Axis1) * 0.5f;
				m_twistAxis.normalize();

				m_kTwist = real_t(1.) / (A->compute_angular_impulse_denominator(m_twistAxis) +
					B->compute_angular_impulse_denominator(m_twistAxis));

			}
	}

	return true;
}

void	ConeTwistJointSW::solve(real_t	timeStep)
{

	Vector3 pivotAInW = A->get_transform().xform(m_rbAFrame.origin);
	Vector3 pivotBInW = B->get_transform().xform(m_rbBFrame.origin);

	real_t tau = real_t(0.3);

	//linear part
	if (!m_angularOnly)
	{
		Vector3 rel_pos1 = pivotAInW - A->get_transform().origin;
		Vector3 rel_pos2 = pivotBInW - B->get_transform().origin;

		Vector3 vel1 = A->get_velocity_in_local_point(rel_pos1);
		Vector3 vel2 = B->get_velocity_in_local_point(rel_pos2);
		Vector3 vel = vel1 - vel2;

		for (int i=0;i<3;i++)
		{
			const Vector3& normal = m_jac[i].m_linearJointAxis;
			real_t jacDiagABInv = real_t(1.) / m_jac[i].getDiagonal();

			real_t rel_vel;
			rel_vel = normal.dot(vel);
			//positional error (zeroth order error)
			real_t depth = -(pivotAInW - pivotBInW).dot(normal); //this is the error projected on the normal
			real_t impulse = depth*tau/timeStep  * jacDiagABInv -  rel_vel * jacDiagABInv;
			m_appliedImpulse += impulse;
			Vector3 impulse_vector = normal * impulse;
			A->apply_impulse(pivotAInW - A->get_transform().origin, impulse_vector);
			B->apply_impulse(pivotBInW - B->get_transform().origin, -impulse_vector);
		}
	}

	{
		///solve angular part
		const Vector3& angVelA = A->get_angular_velocity();
		const Vector3& angVelB = B->get_angular_velocity();

		// solve swing limit
		if (m_solveSwingLimit)
		{
			real_t amplitude = ((angVelB - angVelA).dot( m_swingAxis )*m_relaxationFactor*m_relaxationFactor + m_swingCorrection*(real_t(1.)/timeStep)*m_biasFactor);
			real_t impulseMag = amplitude * m_kSwing;

			// Clamp the accumulated impulse
			real_t temp = m_accSwingLimitImpulse;
			m_accSwingLimitImpulse = MAX(m_accSwingLimitImpulse + impulseMag, real_t(0.0) );
			impulseMag = m_accSwingLimitImpulse - temp;

			Vector3 impulse = m_swingAxis * impulseMag;

			A->apply_torque_impulse(impulse);
			B->apply_torque_impulse(-impulse);

		}

		// solve twist limit
		if (m_solveTwistLimit)
		{
			real_t amplitude = ((angVelB - angVelA).dot( m_twistAxis )*m_relaxationFactor*m_relaxationFactor + m_twistCorrection*(real_t(1.)/timeStep)*m_biasFactor );
			real_t impulseMag = amplitude * m_kTwist;

			// Clamp the accumulated impulse
			real_t temp = m_accTwistLimitImpulse;
			m_accTwistLimitImpulse = MAX(m_accTwistLimitImpulse + impulseMag, real_t(0.0) );
			impulseMag = m_accTwistLimitImpulse - temp;

			Vector3 impulse = m_twistAxis * impulseMag;

			A->apply_torque_impulse(impulse);
			B->apply_torque_impulse(-impulse);

		}

	}

}

void ConeTwistJointSW::set_param(PhysicsServer::ConeTwistJointParam p_param, float p_value) {

	switch(p_param) {
		case PhysicsServer::CONE_TWIST_JOINT_SWING_SPAN: {

			m_swingSpan1=p_value;
			m_swingSpan2=p_value;
		} break;
		case PhysicsServer::CONE_TWIST_JOINT_TWIST_SPAN: {

			m_twistSpan=p_value;
		} break;
		case PhysicsServer::CONE_TWIST_JOINT_BIAS: {

			m_biasFactor=p_value;
		} break;
		case PhysicsServer::CONE_TWIST_JOINT_SOFTNESS: {

			m_limitSoftness=p_value;
		} break;
		case PhysicsServer::CONE_TWIST_JOINT_RELAXATION: {

			m_relaxationFactor=p_value;
		} break;
	}
}

float ConeTwistJointSW::get_param(PhysicsServer::ConeTwistJointParam p_param) const{

	switch(p_param) {
		case PhysicsServer::CONE_TWIST_JOINT_SWING_SPAN: {

			return m_swingSpan1;
		} break;
		case PhysicsServer::CONE_TWIST_JOINT_TWIST_SPAN: {

			return m_twistSpan;
		} break;
		case PhysicsServer::CONE_TWIST_JOINT_BIAS: {

			return m_biasFactor;
		} break;
		case PhysicsServer::CONE_TWIST_JOINT_SOFTNESS: {

			return m_limitSoftness;
		} break;
		case PhysicsServer::CONE_TWIST_JOINT_RELAXATION: {

			return m_relaxationFactor;
		} break;
	}

	return 0;
}