6 #include "VsMovableItem.h"
9 #include "VsMotorizedJoint.h"
10 #include "VsRigidBody.h"
13 #include "VsSimulator.h"
14 #include "VsDragger.h"
37 {
Std_TraceMsg(0,
"Caught Error in desctructor of VsLine\r\n",
"", -1,
false,
true);}
40 void VsLine::SetThisPointers()
42 VsRigidBody::SetThisPointers();
44 m_lpLineBase =
dynamic_cast<LineBase *
>(
this);
46 THROW_TEXT_ERROR(Vs_Err_lThisPointerNotDefined, Vs_Err_strThisPointerNotDefined,
"m_lpLineBase, " + m_lpThisAB->Name());
49 osg::Geometry *VsLine::CreateLineGeometry()
51 osg::Geometry* linesGeom =
new osg::Geometry();
52 int iCount = BuildLines(linesGeom);
55 CStdColor &aryDiffuse = *m_lpThisRB->Diffuse();
56 osg::Vec4Array* colors =
new osg::Vec4Array;
57 colors->push_back(osg::Vec4(aryDiffuse[0], aryDiffuse[1], aryDiffuse[2], aryDiffuse[3]));
58 linesGeom->setColorArray(colors);
59 linesGeom->setColorBinding(osg::Geometry::BIND_OVERALL);
63 osg::Vec3Array* normals =
new osg::Vec3Array;
64 normals->push_back(osg::Vec3(0.0f,-1.0f,0.0f));
65 linesGeom->setNormalArray(normals);
66 linesGeom->setNormalBinding(osg::Geometry::BIND_OVERALL);
71 osg::PrimitiveSet *lpSet =
new osg::DrawArrays(osg::PrimitiveSet::LINES, 0, iCount);
72 linesGeom->addPrimitiveSet(lpSet);
74 linesGeom->setDataVariance(osg::Object::DYNAMIC);
75 linesGeom->setUseDisplayList(
false);
80 int VsLine::BuildLines(osg::Geometry *linesGeom)
83 CStdArray<Attachment *> *aryAttachments = m_lpLineBase->AttachmentPoints();
84 int iCount = aryAttachments->GetSize();
87 if(m_aryLines.valid())
90 m_aryLines =
new osg::Vec3Array;
96 Attachment *lpPrevAttach=NULL;
97 Attachment *lpAttach=NULL;
99 for(
int iIndex=1; iIndex<iCount; iIndex++)
101 lpPrevAttach = aryAttachments->at(iIndex-1);
102 lpAttach = aryAttachments->at(iIndex);
104 vPos = lpPrevAttach->AbsolutePosition();
105 m_aryLines->push_back(osg::Vec3(vPos.x, vPos.y, vPos.z));
107 vPos = lpAttach->AbsolutePosition();
108 m_aryLines->push_back(osg::Vec3(vPos.x, vPos.y, vPos.z));
112 linesGeom->setVertexArray(m_aryLines.get());
113 linesGeom->dirtyBound();
115 return m_aryLines->getNumElements();
118 void VsLine::DrawLine()
120 CStdArray<Attachment *> *aryAttachments = m_lpLineBase->AttachmentPoints();
121 int iCount = aryAttachments->GetSize();
127 Attachment *lpPrevAttach=NULL;
128 Attachment *lpAttach=NULL;
131 for(
int iIndex=1; iIndex<iCount; iIndex++)
133 lpPrevAttach = aryAttachments->at(iIndex-1);
134 lpAttach = aryAttachments->at(iIndex);
136 vPos = lpPrevAttach->AbsolutePosition();
137 (*m_aryLines)[iLineIdx].set(vPos.x, vPos.y, vPos.z);
139 vPos = lpAttach->AbsolutePosition();
140 (*m_aryLines)[iLineIdx+1].set(vPos.x, vPos.y, vPos.z);
145 m_osgGeometry->dirtyBound();
149 void VsLine::SetupGraphics()
152 GetVsSimulator()->OSGRoot()->addChild(m_osgNode.get());
153 SetVisible(m_lpThisMI->IsVisible());
156 void VsLine::DeleteGraphics()
158 if(m_osgGeometry.valid())
160 m_osgGeometry->setDataVariance(osg::Object::STATIC);
161 m_osgGeometry->dirtyBound();
165 VsRigidBody::DeleteGraphics();
168 void VsLine::CreateGraphicsGeometry()
171 m_osgGeometry = CreateLineGeometry();
174 void VsLine::CreatePhysicsGeometry()
179 void VsLine::CreateParts()
183 VsRigidBody::CreateItem();
184 VsRigidBody::SetBody();
187 void VsLine::CalculateForceVector(Attachment *lpPrim, Attachment *lpSec,
float fltTension, CStdFPoint &oPrimPos, CStdFPoint &oSecPos, CStdFPoint &oPrimForce)
189 oPrimPos = lpPrim->AbsolutePosition();
190 oSecPos = lpSec->AbsolutePosition();
192 oPrimForce = oSecPos - oPrimPos;
193 oPrimForce.Normalize();
194 oPrimForce *= fltTension;
197 void VsLine::StepSimulation(
float fltTension)
199 if(m_lpThisBP->Enabled())
202 if(fltTension > 1e-5)
204 CStdArray<Attachment *> *aryAttachments = m_lpLineBase->AttachmentPoints();
205 int iCount = aryAttachments->GetSize();
206 Attachment *lpAttach1 = aryAttachments->at(0), *lpAttach2 = NULL;
207 CStdFPoint oPrimPos, oPrimPlusPos, oSecPos, oSecMinusPos;
208 CStdFPoint oPrimForce, oSecForce;
209 RigidBody *lpAttach1Parent, *lpAttach2Parent;
216 for(
int iIndex=1; iIndex<iCount; iIndex++)
218 lpAttach2 = aryAttachments->at(iIndex);
220 lpAttach1Parent = lpAttach1->Parent();
221 lpAttach2Parent = lpAttach2->Parent();
223 CalculateForceVector(lpAttach1, lpAttach2, fltTension, oPrimPos, oPrimPlusPos, oPrimForce);
224 CalculateForceVector(lpAttach2, lpAttach1, fltTension, oSecPos, oSecMinusPos, oSecForce);
226 lpAttach1Parent->AddForceAtWorldPos(oPrimPos.x, oPrimPos.y, oPrimPos.z, oPrimForce.x, oPrimForce.y, oPrimForce.z,
true);
227 lpAttach2Parent->AddForceAtWorldPos(oSecPos.x, oSecPos.y, oSecPos.z, oSecForce.x, oSecForce.y, oSecForce.z,
true);
229 lpAttach1 = lpAttach2;
237 void VsLine::ResetSimulation()
242 void VsLine::AfterResetSimulation()
Classes for implementing the cm-labs vortex physics engine for AnimatLab.
void Std_TraceMsg(const int iLevel, std::string strMessage, std::string strSourceFile, int iSourceLine, bool bLogToFile, bool bPrintHeader)
Traces a message to the debugger window.