AnimatLab  2
Test
VsHingeLimit.cpp
1 
2 #include "StdAfx.h"
3 #include "VsMovableItem.h"
4 #include "VsBody.h"
5 #include "VsJoint.h"
6 #include "VsMotorizedJoint.h"
7 #include "VsRigidBody.h"
8 #include "VsHingeLimit.h"
9 #include "VsStructure.h"
10 #include "VsSimulator.h"
11 #include "VsOsgUserData.h"
12 #include "VsOsgUserDataVisitor.h"
13 #include "VsDragger.h"
14 
15 namespace VortexAnimatSim
16 {
17  namespace Environment
18  {
19  namespace Joints
20  {
21 
22 VsHingeLimit::VsHingeLimit()
23 {
24  m_vxHinge = NULL;
25 }
26 
27 VsHingeLimit::~VsHingeLimit()
28 {
29 }
30 
31 void VsHingeLimit::HingeRef(Vx::VxHinge *vxHinge)
32 {
33  m_vxHinge = vxHinge;
34 }
35 
36 void VsHingeLimit::Alpha(float fltA)
37 {
38  m_vColor.a(fltA);
39 
40  if(m_osgFlapMat.valid() && m_osgFlapSS.valid())
41  {
42  m_osgFlapMat->setAlpha(osg::Material::FRONT_AND_BACK, fltA);
43 
44  if(fltA < 1)
45  m_osgFlapSS->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
46  else
47  m_osgFlapSS->setRenderingHint(osg::StateSet::OPAQUE_BIN);
48  }
49 }
50 
52 {
53 
54  CStdFPoint vPos(0, 0, 0), vRot(0, 0, 0);
55 
56  Hinge *lpHinge = dynamic_cast<Hinge *>(m_lpJoint);
57 
58  //Reset the position and rotation of the flap.
59  if(m_osgFlapRotateMT.valid() && m_osgFlapTranslateMT.valid() && lpHinge)
60  {
61 
62  float fltHeight = lpHinge->CylinderHeight();
63 
64  vPos.Set(0, 0, 0); vRot.Set(0, 0, -m_fltLimitPos);
65  m_osgFlapRotateMT->setMatrix(SetupMatrix(vPos, vRot));
66 
67  vPos.Set((fltHeight/2)*sin(-m_fltLimitPos), -(fltHeight/2)*cos(-m_fltLimitPos), 0); vRot.Set(0, 0, 0);
68  m_osgFlapTranslateMT->setMatrix(SetupMatrix(vPos, vRot));
69  }
70 
71  //Set the limit on the physics hinge object.
73 }
74 
76 {
77  if(m_vxHinge)
78  {
79  if(m_bIsLowerLimit)
80  m_vxHinge->setLowerLimit(m_vxHinge->kAngularCoordinate, m_fltLimitPos, 0, m_fltRestitution, m_fltStiffness, m_fltDamping);
81  else
82  m_vxHinge->setUpperLimit(m_vxHinge->kAngularCoordinate, m_fltLimitPos, 0, m_fltRestitution, m_fltStiffness, m_fltDamping);
83  }
84 }
85 
87 {
88  m_osgFlapRotateMT.release();
89  m_osgFlap.release();
90  m_osgFlapMat.release();
91  m_osgFlapSS.release();
92 }
93 
95 {
96  //The parent osg object for the joint is actually the child rigid body object.
97  Hinge *lpHinge = dynamic_cast<Hinge *>(m_lpJoint);
98 
99  if(lpHinge)
100  {
101  //Create the min flap
102  m_osgFlap = CreateBoxGeometry(lpHinge->FlapWidth(), (lpHinge->CylinderHeight()/2),
103  lpHinge->CylinderHeight(), lpHinge->FlapWidth(),
104  (lpHinge->CylinderHeight()/2), lpHinge->CylinderHeight());
105  osg::ref_ptr<osg::Geode> osgFlap = new osg::Geode;
106  osgFlap->addDrawable(m_osgFlap.get());
107 
108  CStdFPoint vPos(0, 0, 0), vRot(0, 0, 0);
109 
110  //Rotate flap first then translate.
111  vPos.Set(0, 0, 0); vRot.Set(0, 0, -m_fltLimitPos);
112  m_osgFlapRotateMT = new osg::MatrixTransform();
113  m_osgFlapRotateMT->setMatrix(SetupMatrix(vPos, vRot));
114  m_osgFlapRotateMT->addChild(osgFlap.get());
115 
116  vPos.Set((lpHinge->CylinderHeight()/2)*sin(-m_fltLimitPos), -(lpHinge->CylinderHeight()/2)*cos(-m_fltLimitPos), 0);
117  vRot.Set(0, 0, 0);
118  m_osgFlapTranslateMT = new osg::MatrixTransform();
119  m_osgFlapTranslateMT->setMatrix(SetupMatrix(vPos, vRot));
120  m_osgFlapTranslateMT->addChild(m_osgFlapRotateMT.get());
121 
122  //create a material to use with the pos flap
123  if(!m_osgFlapMat.valid())
124  m_osgFlapMat = new osg::Material();
125 
126  //create a stateset for this node
127  m_osgFlapSS = m_osgFlapTranslateMT->getOrCreateStateSet();
128 
129  //set the diffuse property of this node to the color of this body
130  m_osgFlapMat->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(0.1, 0.1, 0.1, 1));
131  m_osgFlapMat->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(m_vColor.r(), m_vColor.g(), m_vColor.b(), m_vColor.a()));
132  m_osgFlapMat->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(0.25, 0.25, 0.25, 1));
133  m_osgFlapMat->setShininess(osg::Material::FRONT_AND_BACK, 64);
134  m_osgFlapSS->setMode(GL_BLEND, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
135 
136  //apply the material
137  m_osgFlapSS->setAttribute(m_osgFlapMat.get(), osg::StateAttribute::ON);
138  }
139 }
140 
141  } //Bodies
142  } // Environment
143 } //VortexAnimatSim
float m_fltLimitPos
The limit position for the constraint. This can be in radians or meters depending on the type of join...
virtual void DeleteGraphics()
Deletes up the graphics for the constraint.
float m_fltRestitution
The restitution coefficient for the constraint.
virtual void SetLimitPos()
Sets the limit position using the current value set within the object.
virtual float Alpha()
Gets the alpha value.
Joint * m_lpJoint
Pointer to parent joint.
float m_fltDamping
The damping for the constraint.
virtual void SetupGraphics()
Sets up the graphics for the constraint.
Classes for implementing the cm-labs vortex physics engine for AnimatLab.
float m_fltStiffness
The stiffness of the constraint.
bool m_bIsLowerLimit
If true then this is the lower limit of a pair of ConstraintLimits, else it is the upper limit...
virtual void SetLimitValues()
Sets the limit values of the joint in the child object.
CStdColor m_vColor
The color used to display the limit.
Declares the vortex structure class.