1 | /* |
---|
2 | Bullet Continuous Collision Detection and Physics Library |
---|
3 | Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/ |
---|
4 | |
---|
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: |
---|
10 | |
---|
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. |
---|
14 | */ |
---|
15 | |
---|
16 | |
---|
17 | #include "btTypedConstraint.h" |
---|
18 | #include "BulletDynamics/Dynamics/btRigidBody.h" |
---|
19 | #include "LinearMath/btSerializer.h" |
---|
20 | |
---|
21 | |
---|
22 | #define DEFAULT_DEBUGDRAW_SIZE btScalar(0.3f) |
---|
23 | |
---|
24 | btTypedConstraint::btTypedConstraint(btTypedConstraintType type, btRigidBody& rbA) |
---|
25 | :btTypedObject(type), |
---|
26 | m_userConstraintType(-1), |
---|
27 | m_userConstraintId(-1), |
---|
28 | m_needsFeedback(false), |
---|
29 | m_rbA(rbA), |
---|
30 | m_rbB(getFixedBody()), |
---|
31 | m_appliedImpulse(btScalar(0.)), |
---|
32 | m_dbgDrawSize(DEFAULT_DEBUGDRAW_SIZE) |
---|
33 | { |
---|
34 | } |
---|
35 | |
---|
36 | |
---|
37 | btTypedConstraint::btTypedConstraint(btTypedConstraintType type, btRigidBody& rbA,btRigidBody& rbB) |
---|
38 | :btTypedObject(type), |
---|
39 | m_userConstraintType(-1), |
---|
40 | m_userConstraintId(-1), |
---|
41 | m_needsFeedback(false), |
---|
42 | m_rbA(rbA), |
---|
43 | m_rbB(rbB), |
---|
44 | m_appliedImpulse(btScalar(0.)), |
---|
45 | m_dbgDrawSize(DEFAULT_DEBUGDRAW_SIZE) |
---|
46 | { |
---|
47 | } |
---|
48 | |
---|
49 | |
---|
50 | |
---|
51 | |
---|
52 | btScalar btTypedConstraint::getMotorFactor(btScalar pos, btScalar lowLim, btScalar uppLim, btScalar vel, btScalar timeFact) |
---|
53 | { |
---|
54 | if(lowLim > uppLim) |
---|
55 | { |
---|
56 | return btScalar(1.0f); |
---|
57 | } |
---|
58 | else if(lowLim == uppLim) |
---|
59 | { |
---|
60 | return btScalar(0.0f); |
---|
61 | } |
---|
62 | btScalar lim_fact = btScalar(1.0f); |
---|
63 | btScalar delta_max = vel / timeFact; |
---|
64 | if(delta_max < btScalar(0.0f)) |
---|
65 | { |
---|
66 | if((pos >= lowLim) && (pos < (lowLim - delta_max))) |
---|
67 | { |
---|
68 | lim_fact = (lowLim - pos) / delta_max; |
---|
69 | } |
---|
70 | else if(pos < lowLim) |
---|
71 | { |
---|
72 | lim_fact = btScalar(0.0f); |
---|
73 | } |
---|
74 | else |
---|
75 | { |
---|
76 | lim_fact = btScalar(1.0f); |
---|
77 | } |
---|
78 | } |
---|
79 | else if(delta_max > btScalar(0.0f)) |
---|
80 | { |
---|
81 | if((pos <= uppLim) && (pos > (uppLim - delta_max))) |
---|
82 | { |
---|
83 | lim_fact = (uppLim - pos) / delta_max; |
---|
84 | } |
---|
85 | else if(pos > uppLim) |
---|
86 | { |
---|
87 | lim_fact = btScalar(0.0f); |
---|
88 | } |
---|
89 | else |
---|
90 | { |
---|
91 | lim_fact = btScalar(1.0f); |
---|
92 | } |
---|
93 | } |
---|
94 | else |
---|
95 | { |
---|
96 | lim_fact = btScalar(0.0f); |
---|
97 | } |
---|
98 | return lim_fact; |
---|
99 | } |
---|
100 | |
---|
101 | ///fills the dataBuffer and returns the struct name (and 0 on failure) |
---|
102 | const char* btTypedConstraint::serialize(void* dataBuffer, btSerializer* serializer) const |
---|
103 | { |
---|
104 | btTypedConstraintData* tcd = (btTypedConstraintData*) dataBuffer; |
---|
105 | |
---|
106 | tcd->m_rbA = (btRigidBodyData*)serializer->getUniquePointer(&m_rbA); |
---|
107 | tcd->m_rbB = (btRigidBodyData*)serializer->getUniquePointer(&m_rbB); |
---|
108 | char* name = (char*) serializer->findNameForPointer(this); |
---|
109 | tcd->m_name = (char*)serializer->getUniquePointer(name); |
---|
110 | if (tcd->m_name) |
---|
111 | { |
---|
112 | serializer->serializeName(name); |
---|
113 | } |
---|
114 | |
---|
115 | tcd->m_objectType = m_objectType; |
---|
116 | tcd->m_needsFeedback = m_needsFeedback; |
---|
117 | tcd->m_userConstraintId =m_userConstraintId; |
---|
118 | tcd->m_userConstraintType =m_userConstraintType; |
---|
119 | |
---|
120 | tcd->m_appliedImpulse = float(m_appliedImpulse); |
---|
121 | tcd->m_dbgDrawSize = float(m_dbgDrawSize ); |
---|
122 | |
---|
123 | tcd->m_disableCollisionsBetweenLinkedBodies = false; |
---|
124 | |
---|
125 | int i; |
---|
126 | for (i=0;i<m_rbA.getNumConstraintRefs();i++) |
---|
127 | if (m_rbA.getConstraintRef(i) == this) |
---|
128 | tcd->m_disableCollisionsBetweenLinkedBodies = true; |
---|
129 | for (i=0;i<m_rbB.getNumConstraintRefs();i++) |
---|
130 | if (m_rbB.getConstraintRef(i) == this) |
---|
131 | tcd->m_disableCollisionsBetweenLinkedBodies = true; |
---|
132 | |
---|
133 | return "btTypedConstraintData"; |
---|
134 | } |
---|
135 | |
---|
136 | btRigidBody& btTypedConstraint::getFixedBody() |
---|
137 | { |
---|
138 | static btRigidBody s_fixed(0, 0,0); |
---|
139 | s_fixed.setMassProps(btScalar(0.),btVector3(btScalar(0.),btScalar(0.),btScalar(0.))); |
---|
140 | return s_fixed; |
---|
141 | } |
---|
142 | |
---|