6 #include "VsMovableItem.h"
9 #include "VsMotorizedJoint.h"
10 #include "VsRigidBody.h"
12 #include "VsSimulator.h"
13 #include "VsOsgUserData.h"
14 #include "VsOsgUserDataVisitor.h"
15 #include "VsDragger.h"
39 void VsJoint::Physics_SetParent(MovableItem *lpParent)
42 m_lpVsParent =
dynamic_cast<VsRigidBody *
>(lpParent);
45 void VsJoint::Physics_SetChild(MovableItem *lpChild)
50 void VsJoint::SetThisPointers()
52 VsBody::SetThisPointers();
54 m_lpThisJoint =
dynamic_cast<Joint *
>(
this);
56 THROW_TEXT_ERROR(Vs_Err_lThisPointerNotDefined, Vs_Err_strThisPointerNotDefined,
"m_lpThisJoint, " + m_lpThisAB->Name());
58 m_lpThisJoint->PhysicsBody(
this);
61 VxVector3 VsJoint::NormalizeAxis(CStdFPoint vLocalRot)
63 osg::Vec3 vPosN(1, 0, 0);
64 CStdFPoint vMatrixPos(0, 0, 0);
66 osg::Matrix osgMT = SetupMatrix(vMatrixPos, vLocalRot);
68 osg::Vec3 vNorm = vPosN * osgMT;
70 VxVector3 axis((
double) vNorm[0], (
double) vNorm[1], (
double) vNorm[2]);
75 void VsJoint::UpdatePosition()
78 m_vxJoint->getPartAttachmentPosition(0, vPos);
81 m_lpThisMI->AbsolutePosition(vPos[0], vPos[1], vPos[2]);
84 float VsJoint::GetCurrentVxJointPos()
86 float fltDistanceUnits = m_lpThisAB->GetSimulator()->DistanceUnits();
87 float fltMassUnits = m_lpThisAB->GetSimulator()->MassUnits();
89 if(m_vxJoint->isAngular(m_iCoordID) ==
true)
90 return m_vxJoint->getCoordinateCurrentPosition (m_iCoordID);
92 return m_vxJoint->getCoordinateCurrentPosition (m_iCoordID) * fltDistanceUnits;
95 void VsJoint::Physics_CollectData()
97 if(m_lpThisJoint && m_vxJoint)
105 float fltDistanceUnits = m_lpThisAB->GetSimulator()->DistanceUnits();
106 float fltMassUnits = m_lpThisAB->GetSimulator()->MassUnits();
108 if(m_vxJoint->isAngular(m_iCoordID) ==
true)
110 m_lpThisJoint->JointPosition(m_vxJoint->getCoordinateCurrentPosition (m_iCoordID));
111 m_lpThisJoint->JointVelocity(m_vxJoint->getCoordinateVelocity (m_iCoordID));
112 m_lpThisJoint->JointForce(m_vxJoint->getCoordinateForce(m_iCoordID) * fltMassUnits * fltDistanceUnits * fltDistanceUnits);
116 m_lpThisJoint->JointPosition(m_vxJoint->getCoordinateCurrentPosition (m_iCoordID) * fltDistanceUnits);
117 m_lpThisJoint->JointVelocity(m_vxJoint->getCoordinateVelocity(m_iCoordID) * fltDistanceUnits);
118 m_lpThisJoint->JointForce(m_vxJoint->getCoordinateForce(m_iCoordID) * fltMassUnits * fltDistanceUnits);
124 osg::Group *VsJoint::ParentOSG()
127 return m_lpVsParent->GetMatrixTransform();
132 osg::Group *VsJoint::ChildOSG()
135 return m_lpVsChild->GetMatrixTransform();
140 void VsJoint::SetAlpha()
148 void VsJoint::Physics_PositionChanged()
150 VsBody::Physics_PositionChanged();
151 Physics_ResetGraphicsAndPhysics();
154 void VsJoint::Physics_RotationChanged()
156 Physics_ResetGraphicsAndPhysics();
159 void VsJoint::Physics_Resize()
163 DeleteJointGraphics();
165 ResetDraggerOnResize();
169 void VsJoint::DeleteGraphics()
171 DeleteJointGraphics();
172 VsBody::DeleteGraphics();
175 void VsJoint::DeleteJointGraphics()
183 void VsJoint::ResetDraggerOnResize()
186 if(m_osgDragger.valid())
187 m_osgDragger->SetupMatrix();
190 if(m_osgNodeGroup.valid())
193 osgVisitor->traverse(*m_osgNodeGroup);
207 osg::ref_ptr<osg::Geode> osgBall =
new osg::Geode;
210 CStdFPoint vPos(0, 0, 0), vRot(VX_PI/2, 0, 0);
223 m_osgDefaultBallMat->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(0.1, 0.1, 0.1, 1));
225 m_osgDefaultBallMat->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(0.25, 0.25, 0.25, 1));
227 m_osgDefaultBallSS->setMode(GL_BLEND, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
251 m_lpVsParent =
dynamic_cast<VsRigidBody *
>(m_lpThisJoint->Parent());
253 THROW_ERROR(Vs_Err_lUnableToConvertToVsRigidBody, Vs_Err_strUnableToConvertToVsRigidBody);
255 m_lpVsChild =
dynamic_cast<VsRigidBody *
>(m_lpThisJoint->Child());
257 THROW_ERROR(Vs_Err_lUnableToConvertToVsRigidBody, Vs_Err_strUnableToConvertToVsRigidBody);
260 m_osgParent = ParentOSG();
261 osg::ref_ptr<osg::Group> osgChild = ChildOSG();
263 if(m_osgParent.valid())
266 CStdFPoint vPos(0, 0, 0), vRot(VX_PI/2, 0, 0);
267 vPos.Set(0, 0, 0); vRot.Set(0, VX_PI/2, 0);
281 SetVisible(m_lpThisMI->IsVisible());
284 m_osgParent->addChild(m_osgRoot.get());
287 Physics_UpdateAbsolutePosition();
292 osgVisitor->traverse(*m_osgMT);
296 void VsJoint::SetupPhysics()
300 void VsJoint::Initialize()
304 void VsJoint::SetBody()
308 void VsJoint::LocalMatrix(osg::Matrix osgLocalMT)
310 m_osgLocalMatrix = osgLocalMT;
311 m_osgFinalMatrix = m_osgLocalMatrix * m_osgChildOffsetMatrix;
315 void VsJoint::ChildOffsetMatrix(osg::Matrix osgMT)
317 m_osgChildOffsetMatrix = osgMT;
318 m_osgFinalMatrix = m_osgLocalMatrix * m_osgChildOffsetMatrix;
319 m_osgChildOffsetMT->setMatrix(m_osgChildOffsetMatrix);
322 void VsJoint::UpdatePositionAndRotationFromMatrix()
324 VsBody::UpdatePositionAndRotationFromMatrix();
328 void VsJoint::Physics_UpdateMatrix()
330 LocalMatrix(SetupMatrix(m_lpThisMI->Position(), m_lpThisMI->Rotation()));
331 m_osgMT->setMatrix(m_osgLocalMatrix);
332 m_osgDragger->SetupMatrix();
335 VsBody *lpVsChild =
dynamic_cast<VsBody *
>(m_lpThisJoint->Child());
337 ChildOffsetMatrix(lpVsChild->LocalMatrix());
340 Physics_UpdateAbsolutePosition();
343 void VsJoint::BuildLocalMatrix()
345 VsBody::BuildLocalMatrix();
348 void VsJoint::BuildLocalMatrix(CStdFPoint localPos, CStdFPoint localRot, std::string strName)
352 m_osgMT =
new osgManipulator::Selection;
353 m_osgMT->setName(strName +
"_MT");
356 if(!m_osgChildOffsetMT.valid())
358 m_osgChildOffsetMT =
new osg::MatrixTransform;
359 m_osgChildOffsetMT->setName(strName +
"ChildOffsetMT");
360 m_osgChildOffsetMT->addChild(m_osgMT.get());
363 if(!m_osgRoot.valid())
365 m_osgRoot =
new osg::Group;
366 m_osgRoot->setName(strName +
"_Root");
367 m_osgRoot->addChild(m_osgChildOffsetMT.get());
370 LocalMatrix(SetupMatrix(localPos, localRot));
373 m_osgMT->setMatrix(m_osgLocalMatrix);
374 m_osgMT->setName(strName.c_str());
377 VsBody *lpVsChild =
dynamic_cast<VsBody *
>(m_lpThisJoint->Child());
379 ChildOffsetMatrix(lpVsChild->LocalMatrix());
383 if(!m_osgNodeGroup.valid())
385 m_osgNodeGroup =
new osg::Group();
386 m_osgNodeGroup->addChild(m_osgNode.get());
387 m_osgNodeGroup->setName(strName +
"_NodeGroup");
389 m_osgMT->addChild(m_osgNodeGroup.get());
391 CreateSelectedGraphics(strName);
395 bool VsJoint::Physics_CalculateLocalPosForWorldPos(
float fltWorldX,
float fltWorldY,
float fltWorldZ, CStdFPoint &vLocalPos)
399 if(lpParent && m_lpVsChild)
401 fltWorldX *= m_lpThisAB->GetSimulator()->InverseDistanceUnits();
402 fltWorldY *= m_lpThisAB->GetSimulator()->InverseDistanceUnits();
403 fltWorldZ *= m_lpThisAB->GetSimulator()->InverseDistanceUnits();
405 CStdFPoint vPos(fltWorldX, fltWorldY, fltWorldZ), vRot(0, 0, 0);
406 osg::Matrix osgWorldPos = SetupMatrix(vPos, vRot);
409 osg::Matrix osgInverse = osg::Matrix::inverse(m_lpVsChild->GetWorldMatrix());
411 osg::Matrix osgCalc = osgWorldPos * osgInverse;
413 osg::Vec3 vCoord = osgCalc.getTrans();
414 vLocalPos.Set(vCoord[0] * m_lpThisAB->GetSimulator()->DistanceUnits(),
415 vCoord[1] * m_lpThisAB->GetSimulator()->DistanceUnits(),
416 vCoord[2] * m_lpThisAB->GetSimulator()->DistanceUnits());
424 void VsJoint::Physics_ResetSimulation()
428 m_vxJoint->resetDynamics();
431 m_lpThisJoint->JointPosition(0);
432 m_lpThisJoint->JointVelocity(0);
433 m_lpThisJoint->JointForce(0);
438 bool VsJoint::Physics_SetData(
const std::string &strDataType,
const std::string &strValue)
441 if(strDataType ==
"ATTACHEDPARTMOVEDORROTATED")
443 AttachedPartMovedOrRotated(strValue);
450 void VsJoint::Physics_QueryProperties(CStdPtrArray<TypeProperty> &aryProperties)
osg::ref_ptr< osg::StateSet > m_osgDefaultBallSS
The osg default ball state set.
osg::ref_ptr< osg::Geometry > m_osgDefaultBall
The osg default ball geometry.
osg::ref_ptr< osg::MatrixTransform > m_osgDefaultBallMT
The osg default ball matrix transform.
A common class for all rigid body data specific to vortex.
virtual void CreateJointGraphics()
Creates the default ball graphics.
osg::ref_ptr< osg::Material > m_osgDefaultBallMat
The osg default ball material.
osg::Geometry * CreateSphereGeometry(int latres, int longres, float radius)
Classes for implementing the cm-labs vortex physics engine for AnimatLab.
osg::ref_ptr< osg::MatrixTransform > m_osgJointMT
The osg joint matrix transform.
virtual void SetupGraphics()
Sets up the graphics for the joint.
Declares the vortex structure class.