00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00113 #include "../include/IPSA/SoJointHinge.h"
00114 #include "../include/IPSA/SoPhysics.h"
00115
00116 #include <Inventor/nodes/SoSphere.h>
00117 #include <Inventor/nodes/SoCylinder.h>
00118 #include <Inventor/nodes/SoRotation.h>
00119 #include <Inventor/nodes/SoTransform.h>
00120 #include <Inventor/actions/SoCallbackAction.h>
00121
00122
00123 SO_KIT_SOURCE(SoJointHinge);
00124
00127 void
00128 SoJointHinge::initClass()
00129 {
00130 SO_KIT_INIT_CLASS(SoJointHinge, SoJoint, "Joint");
00131 }
00132
00133
00140 SoJointHinge::SoJointHinge(const SbString& BodyOne, const SbString& BodyTwo, const SbString& nodeName)
00141 : SoJoint(BodyOne,BodyTwo,nodeName)
00142 {
00143 SO_KIT_CONSTRUCTOR(SoJointHinge);
00144 isBuiltIn = TRUE;
00145
00146 SO_KIT_ADD_FIELD(soAnchor,(0.0f, 0.0f, 0.0f));
00147 SO_KIT_ADD_FIELD(soAxis,(1.0f, 0.0f, 0.0f));
00148
00149 SO_KIT_ADD_FIELD(AngleOffset,(0.0));
00150 SO_KIT_ADD_FIELD(LoStop,(-dInfinity));
00151 SO_KIT_ADD_FIELD(HiStop,(dInfinity));
00152 SO_KIT_ADD_FIELD(Velocity,(0.0));
00153 SO_KIT_ADD_FIELD(MaxForce,(0.0));
00154 SO_KIT_ADD_FIELD(FudgeFactor,(1.0));
00155 SO_KIT_ADD_FIELD(Bounce,(0.0));
00156 SO_KIT_ADD_FIELD(CFM,(INIT_CFM));
00157 SO_KIT_ADD_FIELD(StopERP,(INIT_ERP));
00158 SO_KIT_ADD_FIELD(StopCFM,(INIT_CFM));
00159 SO_KIT_ADD_FIELD(SuspensionERP,(INIT_ERP));
00160 SO_KIT_ADD_FIELD(SuspensionCFM,(INIT_CFM));
00161
00162 SO_KIT_INIT_INSTANCE();
00163
00164 updateOdeParametersTrigger.appendConnection(&AngleOffset);
00165 updateOdeParametersTrigger.appendConnection(&LoStop);
00166 updateOdeParametersTrigger.appendConnection(&HiStop);
00167 updateOdeParametersTrigger.appendConnection(&Velocity);
00168 updateOdeParametersTrigger.appendConnection(&MaxForce);
00169 updateOdeParametersTrigger.appendConnection(&FudgeFactor);
00170 updateOdeParametersTrigger.appendConnection(&Bounce);
00171 updateOdeParametersTrigger.appendConnection(&CFM);
00172 updateOdeParametersTrigger.appendConnection(&StopERP);
00173 updateOdeParametersTrigger.appendConnection(&StopCFM);
00174 updateOdeParametersTrigger.appendConnection(&SuspensionERP);
00175 updateOdeParametersTrigger.appendConnection(&SuspensionCFM);
00176 }
00177
00178
00187 void SoJointHinge::setTorque(dReal torque1 , dReal torque2 , dReal torque3 )
00188 {
00189 dJointAddHingeTorque(this->soJointId, torque1);
00190 }
00191
00192
00198 void SoJointHinge::createOdeJoint(const dWorldID soWorldId)
00199 {
00200
00201 initVelocity = Velocity.getValue();
00202 initMaxForce = MaxForce.getValue();
00203
00204 soJointId = dJointCreateHinge(soWorldId, soJointGroupId);
00205 }
00206
00207
00214 void SoJointHinge::updateOdeJointProperties(const SbMatrix& modelMatrix)
00215 {
00216 Velocity.setValue(initVelocity);
00217 MaxForce.setValue(initMaxForce);
00218
00219
00220
00221 SbVec3f anchorbase = soAnchor.getValue();
00222 SbVec3f axisbase = soAxis.getValue();
00223 SbVec3f raxbase,ranchorbase;
00224
00225 SbMatrix mm = modelMatrix;
00226 mm.multDirMatrix(axisbase,raxbase);
00227 mm.multVecMatrix(anchorbase,ranchorbase);
00228
00229 dJointSetHingeAnchor(this->soJointId, ranchorbase[0], ranchorbase[1], ranchorbase[2]);
00230 dJointSetHingeAxis(this->soJointId, raxbase[0], raxbase[1], raxbase[2]);
00231 }
00232
00233
00234 SoNode* SoJointHinge::createVisualisation()
00235 {
00236 SoSeparator* result = new SoSeparator;
00237
00238 SoSphere* anchorPoint = new SoSphere;
00239 anchorPoint->radius.setValue(30 * VisualisationScale.getValue());
00240
00241 SoCylinder* axis = new SoCylinder;
00242 axis->radius.setValue(anchorPoint->radius.getValue() / 5.0f);
00243 axis->height.setValue(anchorPoint->radius.getValue() * 6.0f);
00244
00245 SoTransform* axisTransform = new SoTransform;
00246 SoJoint::GenerateCylinderAlignmentRotation(soAxis.getValue(), *axisTransform);
00247 axisTransform->translation.setValue(this->soAnchor.getValue());
00248
00249 result->addChild(axisTransform);
00250 result->addChild(anchorPoint);
00251 result->addChild(axis);
00252
00253 return result;
00254 }
00255
00256
00257 void SoJointHinge::setParams()
00258 {
00259 dJointSetHingeParam(soJointId, dParamLoStop, LoStop.getValue() - AngleOffset.getValue());
00260 dJointSetHingeParam(soJointId, dParamHiStop ,HiStop.getValue() - AngleOffset.getValue());
00261 dJointSetHingeParam(soJointId, dParamVel, Velocity.getValue());
00262 dJointSetHingeParam(soJointId, dParamFMax, MaxForce.getValue());
00263 dJointSetHingeParam(soJointId, dParamFudgeFactor, FudgeFactor.getValue());
00264 dJointSetHingeParam(soJointId, dParamBounce, Bounce.getValue());
00265 dJointSetHingeParam(soJointId, dParamCFM, CFM.getValue());
00266 dJointSetHingeParam(soJointId, dParamStopERP, StopERP.getValue());
00267 dJointSetHingeParam(soJointId, dParamStopCFM, StopCFM.getValue());
00268 dJointSetHingeParam(soJointId, dParamSuspensionERP, SuspensionERP.getValue());
00269 dJointSetHingeParam(soJointId, dParamSuspensionCFM, SuspensionCFM.getValue());
00270 }
00271
00272
00273 SoSFFloat SoJointHinge::getAngle()
00274 {
00275 SoSFFloat ret;
00276 ret.setValue(dJointGetHingeAngle(this->soJointId) + AngleOffset.getValue());
00277 return ret;
00278 }
00279
00280
00281 SoSFFloat SoJointHinge::getAngleRate()
00282 {
00283 SoSFFloat ret;
00284 ret.setValue(dJointGetHingeAngleRate(this->soJointId));
00285 return ret;
00286 }
00287