AnimatLab  2
Test
VsPrismaticLimit.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 "VsPrismaticLimit.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 VsPrismaticLimit::VsPrismaticLimit()
23 {
24  m_vxPrismatic = NULL;
25 }
26 
27 VsPrismaticLimit::~VsPrismaticLimit()
28 {
29 }
30 
31 void VsPrismaticLimit::PrismaticRef(Vx::VxPrismatic *vxPrismatic)
32 {
33  m_vxPrismatic = vxPrismatic;
34 }
35 
36 void VsPrismaticLimit::Alpha(float fltA)
37 {
38  m_vColor.a(fltA);
39 
40  if(m_osgBoxMat.valid() && m_osgBoxSS.valid())
41  {
42  m_osgBoxMat->setAlpha(osg::Material::FRONT_AND_BACK, fltA);
43 
44  if(fltA < 1)
45  m_osgBoxSS->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
46  else
47  m_osgBoxSS->setRenderingHint(osg::StateSet::OPAQUE_BIN);
48  }
49 
50  if(m_osgCylinderMat.valid() && m_osgCylinderSS.valid())
51  {
52  m_osgCylinderMat->setAlpha(osg::Material::FRONT_AND_BACK, fltA);
53 
54  if(fltA < 1)
55  m_osgCylinderSS->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
56  else
57  m_osgCylinderSS->setRenderingHint(osg::StateSet::OPAQUE_BIN);
58  }
59 }
60 
61 osg::Geometry *VsPrismaticLimit::BoxGeometry() {return m_osgBox.get();}
62 
63 osg::MatrixTransform *VsPrismaticLimit::BoxMT() {return m_osgBoxMT.get();}
64 
65 osg::Material *VsPrismaticLimit::BoxMat() {return m_osgBoxMat.get();}
66 
67 osg::StateSet *VsPrismaticLimit::BoxSS() {return m_osgBoxSS.get();}
68 
69 osg::Geometry *VsPrismaticLimit::CylinderGeometry() {return m_osgCylinder.get();}
70 
71 osg::MatrixTransform *VsPrismaticLimit::CylinderMT() {return m_osgCylinderMT.get();}
72 
73 osg::Material *VsPrismaticLimit::CylinderMat() {return m_osgCylinderMat.get();}
74 
75 osg::StateSet *VsPrismaticLimit::CylinderSS() {return m_osgCylinderSS.get();}
76 
78 {
79  CStdFPoint vPos(0, 0, 0), vRot(0, 0, 0);
80 
81  Prismatic *lpPrismatic = dynamic_cast<Prismatic *>(m_lpJoint);
82 
83  //Reset the position of the Box.
84  if(m_osgBoxMT.valid() && lpPrismatic)
85  {
86  vPos.Set(0, 0, -m_fltLimitPos); vRot.Set(0, 0, 0);
87  m_osgBoxMT->setMatrix(SetupMatrix(vPos, vRot));
88  }
89 
90  //Reset the position and size of the Cylinder.
91  if(m_osgCylinderMT.valid() && m_osgCylinder.valid() && m_osgCylinderGeode.valid() && lpPrismatic)
92  {
93  //First delete the cylinder geometry that currently exists.
94  if(m_osgCylinderGeode->containsDrawable(m_osgCylinder.get()))
95  m_osgCylinderGeode->removeDrawable(m_osgCylinder.get());
96  m_osgCylinder.release();
97 
98  //Now recreate the cylinder using the new limit position.
99  m_osgCylinder = CreateConeGeometry(fabs(m_fltLimitPos), lpPrismatic->CylinderRadius(), lpPrismatic->CylinderRadius(), 10, true, true, true);
100  m_osgCylinderGeode->addDrawable(m_osgCylinder.get());
101 
102  vPos.Set(0, 0, (-m_fltLimitPos/2)); vRot.Set(VX_PI/2, 0, 0);
103  m_osgCylinderMT->setMatrix(SetupMatrix(vPos, vRot));
104  }
105 
106  //Set the limit on the physics Prismatic object.
107  SetLimitValues();
108 }
109 
111 {
112  if(m_vxPrismatic)
113  {
114  if(m_bIsLowerLimit)
115  m_vxPrismatic->setLowerLimit(m_vxPrismatic->kLinearCoordinate, m_fltLimitPos, 0, m_fltRestitution, m_fltStiffness, m_fltDamping);
116  else
117  m_vxPrismatic->setUpperLimit(m_vxPrismatic->kLinearCoordinate, m_fltLimitPos, 0, m_fltRestitution, m_fltStiffness, m_fltDamping);
118  }
119 }
120 
122 {
123  m_osgBox.release();
124  m_osgBoxMT.release();
125  m_osgBoxMat.release();
126  m_osgBoxSS.release();
127  m_osgCylinder.release();
128  m_osgCylinderGeode.release();
129  m_osgCylinderMT.release();
130  m_osgCylinderMat.release();
131  m_osgCylinderSS.release();
132 }
133 
135 {
136  //The parent osg object for the joint is actually the child rigid body object.
137  Prismatic *lpPrismatic = dynamic_cast<Prismatic *>(m_lpJoint);
138 
139  if(lpPrismatic)
140  {
141  //Create the LIMIT Box
142  m_osgBox = CreateBoxGeometry(lpPrismatic->BoxSize(), lpPrismatic->BoxSize(),
143  lpPrismatic->BoxSize(), lpPrismatic->BoxSize(),
144  lpPrismatic->BoxSize(), lpPrismatic->BoxSize());
145  osg::ref_ptr<osg::Geode> osgBox = new osg::Geode;
146  osgBox->addDrawable(m_osgBox.get());
147 
148  CStdFPoint vPos(0, 0, 0), vRot(0, 0, 0);
149 
150  //Translate box
151  vPos.Set(0, 0, -m_fltLimitPos);
152  vRot.Set(0, 0, 0);
153  m_osgBoxMT = new osg::MatrixTransform();
154  m_osgBoxMT->setMatrix(SetupMatrix(vPos, vRot));
155  m_osgBoxMT->addChild(osgBox.get());
156 
157  //create a material to use with the pos Box
158  if(!m_osgBoxMat.valid())
159  m_osgBoxMat = new osg::Material();
160 
161  //create a stateset for this node
162  m_osgBoxSS = m_osgBoxMT->getOrCreateStateSet();
163 
164  //set the diffuse property of this node to the color of this body
165  m_osgBoxMat->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(0.1, 0.1, 0.1, 1));
166  m_osgBoxMat->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(m_vColor.r(), m_vColor.g(), m_vColor.b(), m_vColor.a()));
167  m_osgBoxMat->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(0.25, 0.25, 0.25, 1));
168  m_osgBoxMat->setShininess(osg::Material::FRONT_AND_BACK, 64);
169  m_osgBoxSS->setMode(GL_BLEND, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
170 
171  //apply the material
172  m_osgBoxSS->setAttribute(m_osgBoxMat.get(), osg::StateAttribute::ON);
173 
174 
175  //Create the cylinder for the Prismatic
176  //If this is the limit for showing the position then we should not create a cylinder. We only do that for the
177  // upper and lower limits.
178  if(!m_bIsShowPosition)
179  {
180  m_osgCylinder = CreateConeGeometry(fabs(m_fltLimitPos), lpPrismatic->CylinderRadius(), lpPrismatic->CylinderRadius(), 10, true, true, true);
181  m_osgCylinderGeode = new osg::Geode;
182  m_osgCylinderGeode->addDrawable(m_osgCylinder.get());
183 
184  CStdFPoint vPos(0, 0, (-m_fltLimitPos/2)), vRot(VX_PI/2, 0, 0);
185  m_osgCylinderMT = new osg::MatrixTransform();
186  m_osgCylinderMT->setMatrix(SetupMatrix(vPos, vRot));
187  m_osgCylinderMT->addChild(m_osgCylinderGeode.get());
188 
189  //create a material to use with the pos flap
190  if(!m_osgCylinderMat.valid())
191  m_osgCylinderMat = new osg::Material();
192 
193  //create a stateset for this node
194  m_osgCylinderSS = m_osgCylinderMT->getOrCreateStateSet();
195 
196  //set the diffuse property of this node to the color of this body
197  m_osgCylinderMat->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(0.1, 0.1, 0.1, 1));
198  m_osgCylinderMat->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(m_vColor.r(), m_vColor.g(), m_vColor.b(), m_vColor.a()));
199  //m_osgCylinderMat->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(1, 0.25, 1, 1));
200  m_osgCylinderMat->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(0.25, 0.25, 0.25, 1));
201  m_osgCylinderMat->setShininess(osg::Material::FRONT_AND_BACK, 64);
202  m_osgCylinderSS->setMode(GL_BLEND, osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
203 
204  //apply the material
205  m_osgCylinderSS->setAttribute(m_osgCylinderMat.get(), osg::StateAttribute::ON);
206  }
207  }
208 }
209 
210  } //Bodies
211  } // Environment
212 } //VortexAnimatSim
float m_fltLimitPos
The limit position for the constraint. This can be in radians or meters depending on the type of join...
float m_fltRestitution
The restitution coefficient for the constraint.
virtual float Alpha()
Gets the alpha value.
osg::Geometry * CreateConeGeometry(float height, float topradius, float botradius, int sides, bool doSide, bool doTop, bool doBottom)
Joint * m_lpJoint
Pointer to parent joint.
virtual void SetupGraphics()
Sets up the graphics for the constraint.
float m_fltDamping
The damping for the constraint.
virtual void SetLimitValues()
Sets the limit values of the joint in the child object.
virtual void DeleteGraphics()
Deletes 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 SetLimitPos()
Sets the limit position using the current value set within the object.
CStdColor m_vColor
The color used to display the limit.
Declares the vortex structure class.