7 #include "OsgMovableItem.h"
10 #include "OsgRigidBody.h"
12 #include "OsgUserData.h"
13 #include "OsgUserDataVisitor.h"
15 #include "OsgMouseSpring.h"
17 #include "OsgCameraManipulator.h"
18 #include "OsgDragger.h"
40 void OsgJoint::Physics_SetParent(MovableItem *lpParent)
43 m_lpVsParent =
dynamic_cast<OsgBody *
>(lpParent);
46 void OsgJoint::Physics_SetChild(MovableItem *lpChild)
51 void OsgJoint::SetThisPointers()
53 OsgBody::SetThisPointers();
55 m_lpThisJoint =
dynamic_cast<Joint *
>(
this);
57 THROW_TEXT_ERROR(Osg_Err_lThisPointerNotDefined, Osg_Err_strThisPointerNotDefined,
"m_lpThisJoint, " + m_lpThisAB->Name());
59 m_lpThisJoint->PhysicsBody(
this);
62 osg::Vec3d OsgJoint::NormalizeAxis(CStdFPoint vLocalRot)
64 osg::Vec3 vPosN(1, 0, 0);
65 CStdFPoint vMatrixPos(0, 0, 0);
67 osg::Matrix osgMT = SetupMatrix(vMatrixPos, vLocalRot);
69 osg::Vec3 vNorm = vPosN * osgMT;
74 osg::MatrixTransform *OsgJoint::ParentOSG()
77 return m_lpVsParent->GetMatrixTransform();
82 osg::MatrixTransform *OsgJoint::ChildOSG()
85 return m_lpVsChild->GetMatrixTransform();
90 void OsgJoint::SetAlpha()
98 void OsgJoint::Physics_PositionChanged()
100 OsgBody::Physics_PositionChanged();
101 Physics_ResetGraphicsAndPhysics();
104 void OsgJoint::Physics_RotationChanged()
106 Physics_ResetGraphicsAndPhysics();
109 void OsgJoint::Physics_ResetGraphicsAndPhysics()
111 OsgMovableItem::Physics_ResetGraphicsAndPhysics();
113 if(m_osgDragger.valid())
114 m_osgDragger->SetupMatrix();
117 void OsgJoint::DeleteGraphics()
119 DeleteJointGraphics();
120 OsgBody::DeleteGraphics();
123 void OsgJoint::DeleteJointGraphics()
131 void OsgJoint::ResetDraggerOnResize()
134 if(m_osgDragger.valid())
135 m_osgDragger->SetupMatrix();
138 if(m_osgNodeGroup.valid())
141 osgVisitor->traverse(*m_osgNodeGroup);
155 osg::ref_ptr<osg::Geode> osgBall =
new osg::Geode;
158 CStdFPoint vPos(0, 0, 0), vRot(osg::PI/2, 0, 0);
171 m_osgDefaultBallMat->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(0.1, 0.1, 0.1, 1));
173 m_osgDefaultBallMat->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(0.25, 0.25, 0.25, 1));
175 m_osgDefaultBallSS->setMode(GL_BLEND, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
199 m_lpVsParent =
dynamic_cast<OsgRigidBody *
>(m_lpThisJoint->Parent());
201 THROW_ERROR(Osg_Err_lUnableToConvertToVsRigidBody, Osg_Err_strUnableToConvertToVsRigidBody);
203 m_lpVsChild =
dynamic_cast<OsgRigidBody *
>(m_lpThisJoint->Child());
205 THROW_ERROR(Osg_Err_lUnableToConvertToVsRigidBody, Osg_Err_strUnableToConvertToVsRigidBody);
208 m_osgParent = ParentOSG();
210 if(m_osgParent.valid())
227 SetVisible(m_lpThisMI->IsVisible());
230 m_osgParent->addChild(m_osgRoot.get());
233 Physics_UpdateAbsolutePosition();
238 osgVisitor->traverse(*m_osgMT);
242 void OsgJoint::SetupPhysics()
246 void OsgJoint::Initialize()
250 osg::Matrix OsgJoint::GetChildWorldMatrix()
253 return m_lpVsChild->GetWorldMatrix();
255 osg::Matrix osgMatrix;
256 osgMatrix.makeIdentity();
260 osg::Matrix OsgJoint::GetChildPhysicsWorldMatrix()
263 return m_lpVsChild->GetPhysicsWorldMatrix();
265 osg::Matrix osgMatrix;
266 osgMatrix.makeIdentity();
270 osg::Matrix OsgJoint::GetChildComMatrix(
bool bInvert)
273 return m_lpVsChild->GetComMatrix(bInvert);
275 osg::Matrix osgMatrix;
276 osgMatrix.makeIdentity();
282 void OsgJoint::StartGripDrag()
285 DeletePhysics(
false);
289 void OsgJoint::EndGripDrag()
291 UpdatePositionAndRotationFromMatrix();
303 void OsgJoint::UpdatePositionAndRotationFromMatrix()
305 osg::Matrix mtParent = GetParentWorldMatrix();
306 osg::Matrix mtChild = GetChildWorldMatrix();
307 osg::Matrix mtLocal = m_osgMT->getMatrix();
310 osg::Matrix mtWorld = mtLocal * mtParent;
313 osg::Matrix mtJointRelToChild = mtWorld * osg::Matrix::inverse(mtChild);
315 OsgBody::UpdatePositionAndRotationFromMatrix(mtJointRelToChild);
318 void OsgJoint::Physics_UpdateMatrix()
320 CStdFPoint vPos = m_lpThisMI->Position();
321 CStdFPoint vRot = m_lpThisMI->Rotation();
322 LocalMatrix(SetupMatrix(vPos, vRot));
323 m_osgMT->setMatrix(m_osgLocalMatrix);
326 Physics_UpdateAbsolutePosition();
341 OsgBody::BuildLocalMatrix();
348 m_osgMT =
new osgManipulator::Selection;
349 m_osgMT->setName(strName +
"_MT");
352 if(!m_osgRoot.valid())
354 m_osgRoot =
new osg::Group;
355 m_osgRoot->setName(strName +
"_Root");
358 if(!m_osgRoot->containsNode(m_osgMT.get()))
359 m_osgRoot->addChild(m_osgMT.get());
364 osg::Matrix mtParent = GetParentWorldMatrix();
365 osg::Matrix mtChild = GetChildWorldMatrix();
366 osg::Matrix mtLocal = SetupMatrix(localPos, localRot);
368 osg::Matrix mtJointMTFromChild = mtLocal * mtChild;
369 osg::Matrix mtLocalRelToParent = mtJointMTFromChild * osg::Matrix::inverse(mtParent);
371 LocalMatrix(mtLocalRelToParent);
374 m_osgMT->setMatrix(m_osgLocalMatrix);
375 m_osgMT->setName(strName.c_str());
379 if(!m_osgNodeGroup.valid())
381 m_osgNodeGroup =
new osg::Group();
382 m_osgNodeGroup->addChild(m_osgNode.get());
383 m_osgNodeGroup->setName(strName +
"_NodeGroup");
385 m_osgMT->addChild(m_osgNodeGroup.get());
387 CreateSelectedGraphics(strName);
391 bool OsgJoint::Physics_CalculateLocalPosForWorldPos(
float fltWorldX,
float fltWorldY,
float fltWorldZ, CStdFPoint &vLocalPos)
393 if(m_lpVsParent && m_lpVsChild)
395 fltWorldX *= m_lpThisAB->GetSimulator()->InverseDistanceUnits();
396 fltWorldY *= m_lpThisAB->GetSimulator()->InverseDistanceUnits();
397 fltWorldZ *= m_lpThisAB->GetSimulator()->InverseDistanceUnits();
399 CStdFPoint vPos(fltWorldX, fltWorldY, fltWorldZ), vRot(0, 0, 0);
400 osg::Matrix osgWorldPos = SetupMatrix(vPos, vRot);
403 osg::Matrix osgInverse = osg::Matrix::inverse(m_lpVsChild->GetWorldMatrix());
405 osg::Matrix osgCalc = osgWorldPos * osgInverse;
407 osg::Vec3 vCoord = osgCalc.getTrans();
408 vLocalPos.Set(vCoord[0] * m_lpThisAB->GetSimulator()->DistanceUnits(),
409 vCoord[1] * m_lpThisAB->GetSimulator()->DistanceUnits(),
410 vCoord[2] * m_lpThisAB->GetSimulator()->DistanceUnits());
418 bool OsgJoint::Physics_SetData(
const std::string &strDataType,
const std::string &strValue)
421 if(strDataType ==
"ATTACHEDPARTMOVEDORROTATED")
423 AttachedPartMovedOrRotated(strValue);
430 void OsgJoint::Physics_QueryProperties(CStdPtrArray<TypeProperty> &aryProperties)
434 void OsgJoint::Physics_Resize()
436 if(Physics_IsDefined())
438 DeleteJointGraphics();
440 ResetDraggerOnResize();
444 void OsgJoint::Physics_ResetSimulation()
446 if(Physics_IsDefined())
448 m_lpThisJoint->JointPosition(0);
449 m_lpThisJoint->JointVelocity(0);
450 m_lpThisJoint->JointForce(0);
virtual void CreateJointGraphics()
Creates the default ball graphics.
Declares the vortex Light class.
A common class for all rigid body data specific to vortex.
virtual void SetupGraphics()
Sets up the graphics for the joint.
osg::ref_ptr< osg::Geometry > m_osgDefaultBall
The osg default ball geometry.
Classes for implementing the cm-labs vortex physics engine for AnimatLab.
virtual void BuildLocalMatrix()
Builds the local matrix.
osg::ref_ptr< osg::StateSet > m_osgDefaultBallSS
The osg default ball state set.
CStdFPoint m_vJointGraphicsRotOffset
Rotational offset needed to make the joint graphics match the physics.
osg::ref_ptr< osg::MatrixTransform > m_osgJointMT
The osg joint matrix transform.
osg::ref_ptr< osg::Material > m_osgDefaultBallMat
The osg default ball material.
osg::ref_ptr< osg::MatrixTransform > m_osgDefaultBallMT
The osg default ball matrix transform.
Declares the vortex structure class.
osg::Geometry ANIMAT_OSG_PORT * CreateSphereGeometry(int latres, int longres, float radius)