summaryrefslogtreecommitdiff
path: root/thirdparty/bullet/Bullet3Dynamics/ConstraintSolver/b3FixedConstraint.cpp
blob: ace4b183884d3a566bf8d052c35a6bbca28cd3bc (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

#include "b3FixedConstraint.h"
#include "Bullet3Collision/NarrowPhaseCollision/shared/b3RigidBodyData.h"
#include "Bullet3Common/b3TransformUtil.h"
#include <new>

b3FixedConstraint::b3FixedConstraint(int rbA, int rbB, const b3Transform& frameInA, const b3Transform& frameInB)
	: b3TypedConstraint(B3_FIXED_CONSTRAINT_TYPE, rbA, rbB)
{
	m_pivotInA = frameInA.getOrigin();
	m_pivotInB = frameInB.getOrigin();
	m_relTargetAB = frameInA.getRotation() * frameInB.getRotation().inverse();
}

b3FixedConstraint::~b3FixedConstraint()
{
}

void b3FixedConstraint::getInfo1(b3ConstraintInfo1* info, const b3RigidBodyData* bodies)
{
	info->m_numConstraintRows = 6;
	info->nub = 6;
}

void b3FixedConstraint::getInfo2(b3ConstraintInfo2* info, const b3RigidBodyData* bodies)
{
	//fix the 3 linear degrees of freedom

	const b3Vector3& worldPosA = bodies[m_rbA].m_pos;
	const b3Quaternion& worldOrnA = bodies[m_rbA].m_quat;
	const b3Vector3& worldPosB = bodies[m_rbB].m_pos;
	const b3Quaternion& worldOrnB = bodies[m_rbB].m_quat;

	info->m_J1linearAxis[0] = 1;
	info->m_J1linearAxis[info->rowskip + 1] = 1;
	info->m_J1linearAxis[2 * info->rowskip + 2] = 1;

	b3Vector3 a1 = b3QuatRotate(worldOrnA, m_pivotInA);
	{
		b3Vector3* angular0 = (b3Vector3*)(info->m_J1angularAxis);
		b3Vector3* angular1 = (b3Vector3*)(info->m_J1angularAxis + info->rowskip);
		b3Vector3* angular2 = (b3Vector3*)(info->m_J1angularAxis + 2 * info->rowskip);
		b3Vector3 a1neg = -a1;
		a1neg.getSkewSymmetricMatrix(angular0, angular1, angular2);
	}

	if (info->m_J2linearAxis)
	{
		info->m_J2linearAxis[0] = -1;
		info->m_J2linearAxis[info->rowskip + 1] = -1;
		info->m_J2linearAxis[2 * info->rowskip + 2] = -1;
	}

	b3Vector3 a2 = b3QuatRotate(worldOrnB, m_pivotInB);

	{
		//	b3Vector3 a2n = -a2;
		b3Vector3* angular0 = (b3Vector3*)(info->m_J2angularAxis);
		b3Vector3* angular1 = (b3Vector3*)(info->m_J2angularAxis + info->rowskip);
		b3Vector3* angular2 = (b3Vector3*)(info->m_J2angularAxis + 2 * info->rowskip);
		a2.getSkewSymmetricMatrix(angular0, angular1, angular2);
	}

	// set right hand side for the linear dofs
	b3Scalar k = info->fps * info->erp;
	b3Vector3 linearError = k * (a2 + worldPosB - a1 - worldPosA);
	int j;
	for (j = 0; j < 3; j++)
	{
		info->m_constraintError[j * info->rowskip] = linearError[j];
		//printf("info->m_constraintError[%d]=%f\n",j,info->m_constraintError[j]);
	}

	//fix the 3 angular degrees of freedom

	int start_row = 3;
	int s = info->rowskip;
	int start_index = start_row * s;

	// 3 rows to make body rotations equal
	info->m_J1angularAxis[start_index] = 1;
	info->m_J1angularAxis[start_index + s + 1] = 1;
	info->m_J1angularAxis[start_index + s * 2 + 2] = 1;
	if (info->m_J2angularAxis)
	{
		info->m_J2angularAxis[start_index] = -1;
		info->m_J2angularAxis[start_index + s + 1] = -1;
		info->m_J2angularAxis[start_index + s * 2 + 2] = -1;
	}

	// set right hand side for the angular dofs

	b3Vector3 diff;
	b3Scalar angle;
	b3Quaternion qrelCur = worldOrnA * worldOrnB.inverse();

	b3TransformUtil::calculateDiffAxisAngleQuaternion(m_relTargetAB, qrelCur, diff, angle);
	diff *= -angle;
	for (j = 0; j < 3; j++)
	{
		info->m_constraintError[(3 + j) * info->rowskip] = k * diff[j];
	}
}