Bullet Collision Detection & Physics Library
btTypedConstraint.cpp
Go to the documentation of this file.
1/*
2Bullet Continuous Collision Detection and Physics Library
3Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
4
5This software is provided 'as-is', without any express or implied warranty.
6In no event will the authors be held liable for any damages arising from the use of this software.
7Permission is granted to anyone to use this software for any purpose,
8including commercial applications, and to alter it and redistribute it freely,
9subject to the following restrictions:
10
111. 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.
122. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
133. This notice may not be removed or altered from any source distribution.
14*/
15
16
17#include "btTypedConstraint.h"
20
21
22#define DEFAULT_DEBUGDRAW_SIZE btScalar(0.05f)
23
25:btTypedObject(type),
26m_userConstraintType(-1),
27m_userConstraintPtr((void*)-1),
28m_breakingImpulseThreshold(SIMD_INFINITY),
29m_isEnabled(true),
30m_needsFeedback(false),
31m_overrideNumSolverIterations(-1),
32m_rbA(rbA),
33m_rbB(getFixedBody()),
34m_appliedImpulse(btScalar(0.)),
35m_dbgDrawSize(DEFAULT_DEBUGDRAW_SIZE),
36m_jointFeedback(0)
37{
38}
39
40
42:btTypedObject(type),
43m_userConstraintType(-1),
44m_userConstraintPtr((void*)-1),
45m_breakingImpulseThreshold(SIMD_INFINITY),
46m_isEnabled(true),
47m_needsFeedback(false),
48m_overrideNumSolverIterations(-1),
49m_rbA(rbA),
50m_rbB(rbB),
51m_appliedImpulse(btScalar(0.)),
52m_dbgDrawSize(DEFAULT_DEBUGDRAW_SIZE),
53m_jointFeedback(0)
54{
55}
56
57
58
59
61{
62 if(lowLim > uppLim)
63 {
64 return btScalar(1.0f);
65 }
66 else if(lowLim == uppLim)
67 {
68 return btScalar(0.0f);
69 }
70 btScalar lim_fact = btScalar(1.0f);
71 btScalar delta_max = vel / timeFact;
72 if(delta_max < btScalar(0.0f))
73 {
74 if((pos >= lowLim) && (pos < (lowLim - delta_max)))
75 {
76 lim_fact = (lowLim - pos) / delta_max;
77 }
78 else if(pos < lowLim)
79 {
80 lim_fact = btScalar(0.0f);
81 }
82 else
83 {
84 lim_fact = btScalar(1.0f);
85 }
86 }
87 else if(delta_max > btScalar(0.0f))
88 {
89 if((pos <= uppLim) && (pos > (uppLim - delta_max)))
90 {
91 lim_fact = (uppLim - pos) / delta_max;
92 }
93 else if(pos > uppLim)
94 {
95 lim_fact = btScalar(0.0f);
96 }
97 else
98 {
99 lim_fact = btScalar(1.0f);
100 }
101 }
102 else
103 {
104 lim_fact = btScalar(0.0f);
105 }
106 return lim_fact;
107}
108
110const char* btTypedConstraint::serialize(void* dataBuffer, btSerializer* serializer) const
111{
113
114 tcd->m_rbA = (btRigidBodyData*)serializer->getUniquePointer(&m_rbA);
115 tcd->m_rbB = (btRigidBodyData*)serializer->getUniquePointer(&m_rbB);
116 char* name = (char*) serializer->findNameForPointer(this);
117 tcd->m_name = (char*)serializer->getUniquePointer(name);
118 if (tcd->m_name)
119 {
120 serializer->serializeName(name);
121 }
122
123 tcd->m_objectType = m_objectType;
124 tcd->m_needsFeedback = m_needsFeedback;
125 tcd->m_overrideNumSolverIterations = m_overrideNumSolverIterations;
126 tcd->m_breakingImpulseThreshold = m_breakingImpulseThreshold;
127 tcd->m_isEnabled = m_isEnabled? 1: 0;
128
129 tcd->m_userConstraintId =m_userConstraintId;
130 tcd->m_userConstraintType =m_userConstraintType;
131
132 tcd->m_appliedImpulse = m_appliedImpulse;
133 tcd->m_dbgDrawSize = m_dbgDrawSize;
134
135 tcd->m_disableCollisionsBetweenLinkedBodies = false;
136
137 int i;
138 for (i=0;i<m_rbA.getNumConstraintRefs();i++)
139 if (m_rbA.getConstraintRef(i) == this)
140 tcd->m_disableCollisionsBetweenLinkedBodies = true;
141 for (i=0;i<m_rbB.getNumConstraintRefs();i++)
142 if (m_rbB.getConstraintRef(i) == this)
143 tcd->m_disableCollisionsBetweenLinkedBodies = true;
144
146}
147
149{
150 static btRigidBody s_fixed(0, 0,0);
151 s_fixed.setMassProps(btScalar(0.),btVector3(btScalar(0.),btScalar(0.),btScalar(0.)));
152 return s_fixed;
153}
154
155
156void btAngularLimit::set(btScalar low, btScalar high, btScalar _softness, btScalar _biasFactor, btScalar _relaxationFactor)
157{
158 m_halfRange = (high - low) / 2.0f;
160 m_softness = _softness;
161 m_biasFactor = _biasFactor;
162 m_relaxationFactor = _relaxationFactor;
163}
164
166{
167 m_correction = 0.0f;
168 m_sign = 0.0f;
169 m_solveLimit = false;
170
171 if (m_halfRange >= 0.0f)
172 {
173 btScalar deviation = btNormalizeAngle(angle - m_center);
174 if (deviation < -m_halfRange)
175 {
176 m_solveLimit = true;
177 m_correction = - (deviation + m_halfRange);
178 m_sign = +1.0f;
179 }
180 else if (deviation > m_halfRange)
181 {
182 m_solveLimit = true;
183 m_correction = m_halfRange - deviation;
184 m_sign = -1.0f;
185 }
186 }
187}
188
189
191{
192 return m_correction * m_sign;
193}
194
196{
197 if (m_halfRange > 0.0f)
198 {
199 btScalar relativeAngle = btNormalizeAngle(angle - m_center);
200 if (!btEqual(relativeAngle, m_halfRange))
201 {
202 if (relativeAngle > 0.0f)
203 {
204 angle = getHigh();
205 }
206 else
207 {
208 angle = getLow();
209 }
210 }
211 }
212}
213
215{
217}
218
220{
222}
btScalar btNormalizeAngle(btScalar angleInRadians)
Definition: btScalar.h:759
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:292
#define SIMD_INFINITY
Definition: btScalar.h:522
bool btEqual(btScalar a, btScalar eps)
Definition: btScalar.h:552
#define DEFAULT_DEBUGDRAW_SIZE
#define btTypedConstraintData2
#define btTypedConstraintDataName
btTypedConstraintType
#define btRigidBodyData
btScalar m_relaxationFactor
btScalar getLow() const
btScalar getHigh() const
btScalar getError() const
Returns correction value multiplied by sign value.
void test(const btScalar angle)
Checks conastaint angle against limit.
void set(btScalar low, btScalar high, btScalar _softness=0.9f, btScalar _biasFactor=0.3f, btScalar _relaxationFactor=1.0f)
Sets all limit's parameters.
void fit(btScalar &angle) const
Checks given angle against limit.
The btRigidBody is the main class for rigid body objects.
Definition: btRigidBody.h:63
int getNumConstraintRefs() const
Definition: btRigidBody.h:523
void setMassProps(btScalar mass, const btVector3 &inertia)
btTypedConstraint * getConstraintRef(int index)
Definition: btRigidBody.h:518
virtual void * getUniquePointer(void *oldPtr)=0
virtual void serializeName(const char *ptr)=0
virtual const char * findNameForPointer(const void *ptr) const =0
virtual const char * serialize(void *dataBuffer, btSerializer *serializer) const
fills the dataBuffer and returns the struct name (and 0 on failure)
btScalar getMotorFactor(btScalar pos, btScalar lowLim, btScalar uppLim, btScalar vel, btScalar timeFact)
internal method used by the constraint solver, don't use them directly
static btRigidBody & getFixedBody()
btScalar m_breakingImpulseThreshold
btTypedConstraint(btTypedConstraintType type, btRigidBody &rbA)
btVector3 can be used to represent 3D points and vectors.
Definition: btVector3.h:84
rudimentary class to provide type info
Definition: btScalar.h:778
int m_objectType
Definition: btScalar.h:783