21 #include "mathutils.h"
22 #include "private/phyjointprivate.h"
23 #include "private/phyobjectprivate.h"
24 #include "private/worldprivate.h"
28 void PhyHinge::construct(
const wVector& axis,
const wVector& centre,
real startAngle, PhyObject* parent, PhyObject* child,
bool cp ) {
29 dof =
new PhyDOF(
this, axis, centre,
false );
39 wVector end = child->matrix().w_pos;
41 start -= parent->matrix().w_pos;
43 wVector newx = (start-end).normalize();
44 if ( isnan(newx[0]) || isnan(newx[1]) || isnan(newx[2]) ) {
53 real angle = atan2( sinAngle, cosAngle );
54 wQuaternion ql( axis, angle );
72 wQuaternion q( axis, startAngle );
87 qWarning(
"Sanity Check Failed on localMatrixParent" );
90 qWarning(
"Sanity Check Failed on localMatrixChild" );
100 construct( axis, centre, startAngle, parent, child, cp );
105 construct( axis, centre, 0, parent, child, cp );
110 construct( axis,
wVector(0,0,0), startAngle, parent, child, cp );
115 construct( axis,
wVector(0,0,0), 0, parent, child, cp );
120 dof =
new PhyDOF(
this, axis, centre,
false );
150 qWarning(
"Sanity Check Failed on localMatrixParent" );
153 qWarning(
"Sanity Check Failed on localMatrixChild" );
175 #ifdef WORLDSIM_USE_NEWTON
176 priv->joint = NewtonConstraintCreateUserJoint( worldpriv->world, 6, PhyJointPrivate::userBilateralHandler, 0,
priv->child,
priv->parent );
177 NewtonJointSetUserData(
priv->joint,
this );
191 qWarning(
"Sanity Check Failed on globalMatrixParent" );
194 qWarning(
"Sanity Check Failed on globalMatrixChild" );
207 real angle = atan2( sinAngle, cosAngle );
217 wVector omegaParent, omegaChild;
218 #ifdef WORLDSIM_USE_NEWTON
220 NewtonBodyGetOmega(
priv->parent, &omegaParent[0] );
222 omegaParent =
wVector(0, 0, 0);
224 NewtonBodyGetOmega(
priv->child, &omegaChild[0] );
233 dof->setPosition( angle );
234 dof->setVelocity( vel );
237 void PhyHinge::updateJoint(
real timestep ) {
246 const real angle = dof->position();
247 const real vel = dof->velocity();
249 #ifdef WORLDSIM_USE_NEWTON
252 NewtonUserJointSetRowStiffness(
priv->joint, 1.0 );
254 NewtonUserJointSetRowStiffness(
priv->joint, 1.0 );
256 NewtonUserJointSetRowStiffness(
priv->joint, 1.0 );
268 NewtonUserJointSetRowStiffness(
priv->joint, 1.0 );
270 NewtonUserJointSetRowStiffness(
priv->joint, 1.0 );
272 if ( dof->isLimited() ) {
275 dof->limits( lolimit, hilimit );
276 if ( angle < lolimit ) {
277 real relAngle = lolimit - angle;
281 NewtonUserJointSetRowStiffness(
priv->joint, dof->stiffness() );
282 NewtonUserJointSetRowSpringDamperAcceleration(
priv->joint, 50000.0, 100 );
284 NewtonUserJointSetRowMinimumFriction(
priv->joint, 0.0 );
286 }
else if ( angle > hilimit ) {
287 real relAngle = hilimit - angle;
291 NewtonUserJointSetRowStiffness(
priv->joint, dof->stiffness() );
292 NewtonUserJointSetRowSpringDamperAcceleration(
priv->joint, 50000.0, 100 );
294 NewtonUserJointSetRowMaximumFriction(
priv->joint, 0.0 );
301 real force, accel, wishangle, wishvel, sign;
302 switch( dof->motion() ) {
308 force = fabs( dof->appliedForce() );
309 if ( ( dof->maxForce() > 0.0 ) && ( force > dof->maxForce() ) ) {
310 force = dof->maxForce();
312 accel = 2.0*dof->appliedForce();
314 NewtonUserJointSetRowMinimumFriction(
priv->joint, -force );
315 NewtonUserJointSetRowMaximumFriction(
priv->joint, +force );
316 NewtonUserJointSetRowAcceleration(
priv->joint, accel );
317 NewtonUserJointSetRowStiffness(
priv->joint, dof->stiffness() );
321 accel = 0.8*( dof->desiredVelocity() - vel ) / ( timestep );
322 wishangle = (dof->desiredVelocity()*timestep);
324 NewtonUserJointSetRowAcceleration(
priv->joint, accel );
325 NewtonUserJointSetRowStiffness(
priv->joint, dof->stiffness() );
326 if ( dof->maxForce() > 0.0 ) {
327 NewtonUserJointSetRowMinimumFriction(
priv->joint, -dof->maxForce() );
328 NewtonUserJointSetRowMaximumFriction(
priv->joint, +dof->maxForce() );
332 wishangle = dof->desiredPosition() - angle;
333 wishvel = wishangle / timestep;
334 if ( fabs(wishvel) > dof->maxVelocity() ) {
335 sign = (wishvel<0) ? -1.0 : +1.0;
336 wishvel = sign*dof->maxVelocity();
338 accel = 0.8*( wishvel - vel ) / ( timestep );
340 NewtonUserJointSetRowAcceleration(
priv->joint, accel );
341 NewtonUserJointSetRowStiffness(
priv->joint, dof->stiffness() );
342 if ( dof->maxForce() > 0.0 ) {
343 NewtonUserJointSetRowMinimumFriction(
priv->joint, -dof->maxForce() );
344 NewtonUserJointSetRowMaximumFriction(
priv->joint, +dof->maxForce() );