summaryrefslogtreecommitdiff
path: root/src/Bullet3Dynamics/ConstraintSolver/b3TypedConstraint.cpp
blob: 885e277d8c2c2916ebc54edd478e33a48d62c12e (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
/*
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.
*/

#include "b3TypedConstraint.h"
//#include "Bullet3Common/b3Serializer.h"

#define B3_DEFAULT_DEBUGDRAW_SIZE b3Scalar(0.3f)

b3TypedConstraint::b3TypedConstraint(b3TypedConstraintType type, int rbA, int rbB)
	: b3TypedObject(type),
	  m_userConstraintType(-1),
	  m_userConstraintPtr((void*)-1),
	  m_breakingImpulseThreshold(B3_INFINITY),
	  m_isEnabled(true),
	  m_needsFeedback(false),
	  m_overrideNumSolverIterations(-1),
	  m_rbA(rbA),
	  m_rbB(rbB),
	  m_appliedImpulse(b3Scalar(0.)),
	  m_dbgDrawSize(B3_DEFAULT_DEBUGDRAW_SIZE),
	  m_jointFeedback(0)
{
}

b3Scalar b3TypedConstraint::getMotorFactor(b3Scalar pos, b3Scalar lowLim, b3Scalar uppLim, b3Scalar vel, b3Scalar timeFact)
{
	if (lowLim > uppLim)
	{
		return b3Scalar(1.0f);
	}
	else if (lowLim == uppLim)
	{
		return b3Scalar(0.0f);
	}
	b3Scalar lim_fact = b3Scalar(1.0f);
	b3Scalar delta_max = vel / timeFact;
	if (delta_max < b3Scalar(0.0f))
	{
		if ((pos >= lowLim) && (pos < (lowLim - delta_max)))
		{
			lim_fact = (lowLim - pos) / delta_max;
		}
		else if (pos < lowLim)
		{
			lim_fact = b3Scalar(0.0f);
		}
		else
		{
			lim_fact = b3Scalar(1.0f);
		}
	}
	else if (delta_max > b3Scalar(0.0f))
	{
		if ((pos <= uppLim) && (pos > (uppLim - delta_max)))
		{
			lim_fact = (uppLim - pos) / delta_max;
		}
		else if (pos > uppLim)
		{
			lim_fact = b3Scalar(0.0f);
		}
		else
		{
			lim_fact = b3Scalar(1.0f);
		}
	}
	else
	{
		lim_fact = b3Scalar(0.0f);
	}
	return lim_fact;
}

void b3AngularLimit::set(b3Scalar low, b3Scalar high, b3Scalar _softness, b3Scalar _biasFactor, b3Scalar _relaxationFactor)
{
	m_halfRange = (high - low) / 2.0f;
	m_center = b3NormalizeAngle(low + m_halfRange);
	m_softness = _softness;
	m_biasFactor = _biasFactor;
	m_relaxationFactor = _relaxationFactor;
}

void b3AngularLimit::test(const b3Scalar angle)
{
	m_correction = 0.0f;
	m_sign = 0.0f;
	m_solveLimit = false;

	if (m_halfRange >= 0.0f)
	{
		b3Scalar deviation = b3NormalizeAngle(angle - m_center);
		if (deviation < -m_halfRange)
		{
			m_solveLimit = true;
			m_correction = -(deviation + m_halfRange);
			m_sign = +1.0f;
		}
		else if (deviation > m_halfRange)
		{
			m_solveLimit = true;
			m_correction = m_halfRange - deviation;
			m_sign = -1.0f;
		}
	}
}

b3Scalar b3AngularLimit::getError() const
{
	return m_correction * m_sign;
}

void b3AngularLimit::fit(b3Scalar& angle) const
{
	if (m_halfRange > 0.0f)
	{
		b3Scalar relativeAngle = b3NormalizeAngle(angle - m_center);
		if (!b3Equal(relativeAngle, m_halfRange))
		{
			if (relativeAngle > 0.0f)
			{
				angle = getHigh();
			}
			else
			{
				angle = getLow();
			}
		}
	}
}

b3Scalar b3AngularLimit::getLow() const
{
	return b3NormalizeAngle(m_center - m_halfRange);
}

b3Scalar b3AngularLimit::getHigh() const
{
	return b3NormalizeAngle(m_center + m_halfRange);
}