Project

General

Profile

SO3Engine
OgreNewt_Joint.cpp
Go to the documentation of this file.
5#include "CustomUserBlank.h"
6#include "CustomJoint.h"
7
8namespace OgreNewt
9{
10
11
12
14{
15 m_joint = NULL;
16}
17
19{
20 if (m_joint)
21 {
22 m_joint->SetUserData (NULL);
23 m_joint->SetUserDestructorCallback(NULL);
24 delete m_joint;
25 }
26}
27
29void Joint::SetSupportJoint (CustomJoint* supportJoint)
30{
31 m_joint = supportJoint;
32 supportJoint->SetUserData (this);
33 supportJoint->SetUserDestructorCallback ((JointUserDestructorCallback) destructorCallback);
34 supportJoint->SetUserSubmintConstraintCallback((JointUserSubmitConstraintCallback) submitConstraintCallback);
35}
36
38CustomJoint* Joint::GetSupportJoint () const
39{
40 return m_joint;
41}
42
45{
46 Body* body = 0;
47 if (m_joint) {
48 const NewtonBody* ntBody;
49 ntBody = m_joint->GetBody0();
50 body = (Body*) NewtonBodyGetUserData(ntBody);
51 }
52 return body;
53
54}
55
58{
59 Body* body = 0;
60 if (m_joint) {
61 const NewtonBody* ntBody;
62 ntBody = m_joint->GetBody1();
63 if (ntBody) {
64 body = (Body*) NewtonBodyGetUserData(ntBody);
65 }
66 }
67 return body;
68}
69
71{
72 Body* body = 0;
73 if (m_joint) {
74 const NewtonBody* ntBody;
75 ntBody = m_joint->GetBody0();
76 body = (Body*) NewtonBodyGetUserData(ntBody);
77 if (body)
78 return body->getWorld();
79 }
80
81 return 0;
82}
83
84void Joint::addLinearRow( const Ogre::Vector3& pt0, const Ogre::Vector3& pt1, const Ogre::Vector3& dir ) const
85{
86 NewtonUserJointAddLinearRow ( m_joint->GetJoint(), &pt0.x, &pt1.x, &dir.x );
87}
88
89void Joint::addAngularRow( Ogre::Radian relativeAngleError, const Ogre::Vector3& dir ) const
90{
91 NewtonUserJointAddAngularRow( m_joint->GetJoint(), relativeAngleError.valueRadians(), &dir.x );
92}
93
94void Joint::addGeneralRow(const Ogre::Vector3& linear0, const Ogre::Vector3& angular0, const Ogre::Vector3& linear1, const Ogre::Vector3& angular1) const
95{
96 dFloat jacobian0[6], jacobian1[6];
97
98 jacobian0[0] = linear0.x;
99 jacobian0[1] = linear0.y;
100 jacobian0[2] = linear0.z;
101 jacobian0[3] = angular0.x;
102 jacobian0[4] = angular0.y;
103 jacobian0[5] = angular0.z;
104
105 jacobian1[0] = linear1.x;
106 jacobian1[1] = linear1.y;
107 jacobian1[2] = linear1.z;
108 jacobian1[3] = angular1.x;
109 jacobian1[4] = angular1.y;
110 jacobian1[5] = angular1.z;
111
112 NewtonUserJointAddGeneralRow( m_joint->GetJoint(), jacobian0, jacobian1 );
113}
114
115
116void Joint::setRowMinimumFriction( Ogre::Real friction ) const
117{
118 NewtonUserJointSetRowMinimumFriction( m_joint->GetJoint(), friction );
119}
120
121
122void Joint::setRowMaximumFriction( Ogre::Real friction ) const
123{
124 NewtonUserJointSetRowMaximumFriction( m_joint->GetJoint(), friction );
125}
126
127
128void Joint::setRowAcceleration( Ogre::Real accel ) const
129{
130 NewtonUserJointSetRowAcceleration( m_joint->GetJoint(), accel );
131}
132
133void Joint::setRowStiffness( Ogre::Real stiffness ) const
134{
135 NewtonUserJointSetRowStiffness( m_joint->GetJoint(), stiffness );
136}
137
138void Joint::setRowSpringDamper(Ogre::Real springK, Ogre::Real springD) const
139{
140 NewtonUserJointSetRowSpringDamperAcceleration( m_joint->GetJoint(), springK, springD );
141}
142
144{
145 return NewtonJointGetCollisionState( m_joint->GetJoint() );
146}
147
148void Joint::setCollisionState( int state ) const
149{
150 NewtonJointSetCollisionState( m_joint->GetJoint(), state );
151}
152
153Ogre::Real Joint::getStiffness() const
154{
155 return (Ogre::Real)NewtonJointGetStiffness( m_joint->GetJoint() );
156}
157
158void Joint::setStiffness( Ogre::Real stiffness ) const
159{
160 NewtonJointSetStiffness( m_joint->GetJoint(), stiffness );
161}
162
164void _CDECL Joint::destructorCallback( const CustomJoint* me )
165{
166 Joint* jnt;
167 jnt = (Joint*)me->GetUserData ();
168 jnt->m_joint = NULL;
169}
170
171
173void _CDECL Joint::submitConstraintCallback( const CustomJoint* me, dFloat timestep, int threadIndex )
174{
175 Joint* jnt;
176
177 jnt = (Joint*)me->GetUserData ();
178 jnt->submitConstraint( timestep, threadIndex );
179}
180
181OgCustomJoint::OgCustomJoint( unsigned int maxDOF, const Body* child, const Body* parent ) : Joint()
182{
183 CustomJoint* suppportJoint;
184
185 suppportJoint = new CustomUserBlank(maxDOF, child->getNewtonBody(), parent ? parent->getNewtonBody() : NULL);
186 SetSupportJoint(suppportJoint);
187
188 m_maxDOF = maxDOF;
189
190 m_body0 = child;
191 m_body1 = parent;
192}
193
197
198void OgCustomJoint::pinAndDirToLocal( const Ogre::Vector3& pinpt, const Ogre::Vector3& pindir,
199 Ogre::Quaternion& localOrient0, Ogre::Vector3& localPos0, Ogre::Quaternion& localOrient1, Ogre::Vector3& localPos1 ) const
200{
201 localOrient0 = localOrient1 = Ogre::Quaternion::IDENTITY;
202 localPos0 = localPos1 = Ogre::Vector3::ZERO;
203
204 Ogre::Quaternion bodyOrient0 = Ogre::Quaternion::IDENTITY;
205 Ogre::Quaternion bodyOrient1 = Ogre::Quaternion::IDENTITY;
206 Ogre::Vector3 bodyPos0 = Ogre::Vector3::ZERO;
207 Ogre::Vector3 bodyPos1 = Ogre::Vector3::ZERO;
208
209 Ogre::Quaternion pinOrient = grammSchmidt(pindir);
210
211 getBody0()->getPositionOrientation( bodyPos0, bodyOrient0 );
212
213 if (getBody1() != NULL)
214 {
215 getBody1()->getPositionOrientation( bodyPos1, bodyOrient1 );
216 }
217
218 localPos0 = bodyOrient0.Inverse() * (pinpt - bodyPos0);
219 localOrient0 = pinOrient * bodyOrient0.Inverse();
220
221 localPos1 = bodyOrient1.Inverse() * (pinpt - bodyPos1);
222 localOrient1 = pinOrient * bodyOrient1.Inverse();
223
224}
225
226void OgCustomJoint::localToGlobal( const Ogre::Quaternion& localOrient, const Ogre::Vector3& localPos, Ogre::Quaternion& globalOrient, Ogre::Vector3& globalPos, int bodyIndex ) const
227{
228 globalOrient = Ogre::Quaternion::IDENTITY;
229 globalPos= Ogre::Vector3::ZERO;
230
231 const Body* bdy = NULL;
232 if (bodyIndex == 0)
233 bdy = getBody0();
234 else if (getBody1())
235 bdy = getBody1();
236
237 Ogre::Quaternion bodyOrient = Ogre::Quaternion::IDENTITY;
238 Ogre::Vector3 bodyPos = Ogre::Vector3::ZERO;
239
240 if (bdy)
241 {
242 bdy->getPositionOrientation( bodyPos, bodyOrient );
243 }
244
245 globalPos = (bodyOrient * localPos) + bodyPos;
246 globalOrient = bodyOrient * localOrient;
247}
248
249void OgCustomJoint::localToGlobalVisual( const Ogre::Quaternion& localOrient, const Ogre::Vector3& localPos, Ogre::Quaternion& globalOrient, Ogre::Vector3& globalPos, int bodyIndex ) const
250{
251 globalOrient = Ogre::Quaternion::IDENTITY;
252 globalPos= Ogre::Vector3::ZERO;
253
254 const Body* bdy = NULL;
255 if (bodyIndex == 0)
256 bdy = getBody0();
257 else if (getBody1())
258 bdy = getBody1();
259
260 Ogre::Quaternion bodyOrient = Ogre::Quaternion::IDENTITY;
261 Ogre::Vector3 bodyPos = Ogre::Vector3::ZERO;
262
263 if (bdy)
264 {
265 bdy->getVisualPositionOrientation( bodyPos, bodyOrient );
266 }
267
268 globalPos = (bodyOrient * localPos) + bodyPos;
269 globalOrient = bodyOrient * localOrient;
270}
271
272void OgCustomJoint::globalToLocal( const Ogre::Quaternion& globalOrient, const Ogre::Vector3& globalPos, Ogre::Quaternion& localOrient, Ogre::Vector3& localPos, int bodyIndex ) const
273{
274 localOrient = Ogre::Quaternion::IDENTITY;
275 localPos= Ogre::Vector3::ZERO;
276
277 const Body* bdy = NULL;
278 if (bodyIndex == 0)
279 bdy = getBody0();
280 else if (getBody1() != NULL)
281 bdy = getBody1();
282
283 Ogre::Quaternion bodyOrient = Ogre::Quaternion::IDENTITY;
284 Ogre::Vector3 bodyPos = Ogre::Vector3::ZERO;
285
286 if (bdy)
287 bdy->getPositionOrientation( bodyPos, bodyOrient );
288
289 Ogre::Quaternion bodyOrientInv = bodyOrient.Inverse();
290
291 localOrient = bodyOrientInv * globalOrient;
292 localPos = bodyOrientInv * (globalPos - bodyPos);
293}
294
295Ogre::Quaternion OgCustomJoint::grammSchmidt( const Ogre::Vector3& pin ) const
296{
297 Ogre::Vector3 front, up, right;
298 front = pin;
299
300 front.normalise();
301 if (Ogre::Math::Abs( front.z ) > 0.577f)
302 right = front.crossProduct( Ogre::Vector3(-front.y, front.z, 0.0f) );
303 else
304 right = front.crossProduct( Ogre::Vector3(-front.y, front.x, 0.0f) );
305 right.normalise();
306 up = right.crossProduct( front );
307
308 Ogre::Matrix3 ret;
309 ret.FromAxes( front, up, right );
310
311 Ogre::Quaternion quat;
312 quat.FromRotationMatrix( ret );
313
314 return quat;
315}
316
317} // end NAMESPACE OgreNewt
318
main class for all Rigid Bodies in the system.
OgreNewt::World *const getWorld() const
get a pointer to the OgreNewt::World this body belongs to.
NewtonBody * getNewtonBody() const
get a pointer to the NewtonBody object
void getVisualPositionOrientation(Ogre::Vector3 &pos, Ogre::Quaternion &orient) const
get the node position and orientation in form of an Ogre::Vector(position) and Ogre::Quaternion(orien...
void getPositionOrientation(Ogre::Vector3 &pos, Ogre::Quaternion &orient) const
get position and orientation in form of an Ogre::Vector(position) and Ogre::Quaternion(orientation)
base class for all joints.
void setRowMaximumFriction(Ogre::Real friction) const
Body * getBody0() const
get the pointer to the first rigid body
void setStiffness(Ogre::Real stiffness) const
set joint stiffness
void addGeneralRow(const Ogre::Vector3 &linear0, const Ogre::Vector3 &angular0, const Ogre::Vector3 &linear1, const Ogre::Vector3 &angular1) const
virtual void submitConstraint(Ogre::Real timeStep, int threadIndex)
must be define for a functioning joint.
void setRowAcceleration(Ogre::Real accel) const
CustomJoint * GetSupportJoint() const
GetSupportJoint.
void setRowMinimumFriction(Ogre::Real friction) const
virtual ~Joint()
destructor
void setRowStiffness(Ogre::Real stiffness) const
Joint()
constructor
CustomJoint * m_joint
int getCollisionState() const
returns collision state
void setCollisionState(int state) const
sets the collision state
void addAngularRow(Ogre::Radian relativeAngleError, const Ogre::Vector3 &dir) const
void setRowSpringDamper(Ogre::Real springK, Ogre::Real springD) const
const OgreNewt::World * getWorld()
get the pointer to the world
void addLinearRow(const Ogre::Vector3 &pt0, const Ogre::Vector3 &pt1, const Ogre::Vector3 &dir) const
static void _CDECL destructorCallback(const CustomJoint *me)
joint destructor callback glue
Ogre::Real getStiffness() const
get joint stiffness
void SetSupportJoint(CustomJoint *supportJoint)
SetSupportJoint.
static void _CDECL submitConstraintCallback(const CustomJoint *me, dFloat timestep, int threadIndex)
submintContraint
Body * getBody1() const
get the pointer to the first rigid body
OgCustomJoint(unsigned int maxDOF, const OgreNewt::Body *child, const OgreNewt::Body *parent)
void globalToLocal(const Ogre::Quaternion &globalOrient, const Ogre::Vector3 &globalPos, Ogre::Quaternion &localOrient, Ogre::Vector3 &localPos, int bodyIndex) const
const OgreNewt::Body * m_body0
void pinAndDirToLocal(const Ogre::Vector3 &pinpt, const Ogre::Vector3 &pindir, Ogre::Quaternion &localOrient0, Ogre::Vector3 &localPos0, Ogre::Quaternion &localOrient1, Ogre::Vector3 &localPos1) const
Ogre::Quaternion grammSchmidt(const Ogre::Vector3 &pin) const
const OgreNewt::Body * m_body1
void localToGlobalVisual(const Ogre::Quaternion &localOrient, const Ogre::Vector3 &localPos, Ogre::Quaternion &globalOrient, Ogre::Vector3 &globalPos, int bodyIndex) const
void localToGlobal(const Ogre::Quaternion &localOrient, const Ogre::Vector3 &localPos, Ogre::Quaternion &globalOrient, Ogre::Vector3 &globalPos, int bodyIndex) const
represents a physics world.