2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans https://bulletphysics.org
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
11 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.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
16 #include "b3TypedConstraint.h"
17 //#include "Bullet3Common/b3Serializer.h"
19 #define B3_DEFAULT_DEBUGDRAW_SIZE b3Scalar(0.3f)
21 b3TypedConstraint::b3TypedConstraint(b3TypedConstraintType type, int rbA, int rbB)
22 : b3TypedObject(type),
23 m_userConstraintType(-1),
24 m_userConstraintPtr((void*)-1),
25 m_breakingImpulseThreshold(B3_INFINITY),
27 m_needsFeedback(false),
28 m_overrideNumSolverIterations(-1),
31 m_appliedImpulse(b3Scalar(0.)),
32 m_dbgDrawSize(B3_DEFAULT_DEBUGDRAW_SIZE),
37 b3Scalar b3TypedConstraint::getMotorFactor(b3Scalar pos, b3Scalar lowLim, b3Scalar uppLim, b3Scalar vel, b3Scalar timeFact)
41 return b3Scalar(1.0f);
43 else if (lowLim == uppLim)
45 return b3Scalar(0.0f);
47 b3Scalar lim_fact = b3Scalar(1.0f);
48 b3Scalar delta_max = vel / timeFact;
49 if (delta_max < b3Scalar(0.0f))
51 if ((pos >= lowLim) && (pos < (lowLim - delta_max)))
53 lim_fact = (lowLim - pos) / delta_max;
55 else if (pos < lowLim)
57 lim_fact = b3Scalar(0.0f);
61 lim_fact = b3Scalar(1.0f);
64 else if (delta_max > b3Scalar(0.0f))
66 if ((pos <= uppLim) && (pos > (uppLim - delta_max)))
68 lim_fact = (uppLim - pos) / delta_max;
70 else if (pos > uppLim)
72 lim_fact = b3Scalar(0.0f);
76 lim_fact = b3Scalar(1.0f);
81 lim_fact = b3Scalar(0.0f);
86 void b3AngularLimit::set(b3Scalar low, b3Scalar high, b3Scalar _softness, b3Scalar _biasFactor, b3Scalar _relaxationFactor)
88 m_halfRange = (high - low) / 2.0f;
89 m_center = b3NormalizeAngle(low + m_halfRange);
90 m_softness = _softness;
91 m_biasFactor = _biasFactor;
92 m_relaxationFactor = _relaxationFactor;
95 void b3AngularLimit::test(const b3Scalar angle)
101 if (m_halfRange >= 0.0f)
103 b3Scalar deviation = b3NormalizeAngle(angle - m_center);
104 if (deviation < -m_halfRange)
107 m_correction = -(deviation + m_halfRange);
110 else if (deviation > m_halfRange)
113 m_correction = m_halfRange - deviation;
119 b3Scalar b3AngularLimit::getError() const
121 return m_correction * m_sign;
124 void b3AngularLimit::fit(b3Scalar& angle) const
126 if (m_halfRange > 0.0f)
128 b3Scalar relativeAngle = b3NormalizeAngle(angle - m_center);
129 if (!b3Equal(relativeAngle, m_halfRange))
131 if (relativeAngle > 0.0f)
143 b3Scalar b3AngularLimit::getLow() const
145 return b3NormalizeAngle(m_center - m_halfRange);
148 b3Scalar b3AngularLimit::getHigh() const
150 return b3NormalizeAngle(m_center + m_halfRange);