AnimatLab  2
Test
VsSpring.cpp
1 // VsSpring.cpp: implementation of the VsSpring class.
2 //
4 
5 #include "StdAfx.h"
6 #include "VsMovableItem.h"
7 #include "VsBody.h"
8 #include "VsJoint.h"
9 #include "VsMotorizedJoint.h"
10 #include "VsRigidBody.h"
11 #include "VsLine.h"
12 #include "VsSpring.h"
13 #include "VsSimulator.h"
14 #include "VsDragger.h"
15 
16 namespace VortexAnimatSim
17 {
18  namespace Environment
19  {
20  namespace Bodies
21  {
22 
24 // Construction/Destruction
26 
27 VsSpring::VsSpring()
28 {
29  SetThisPointers();
30  m_vxSpring = NULL;
31 }
32 
33 VsSpring::~VsSpring()
34 {
35  try
36  {
37  DeleteGraphics();
38  DeletePhysics();
39  }
40  catch(...)
41  {Std_TraceMsg(0, "Caught Error in desctructor of VsSpring/\r\n", "", -1, false, true);}
42 }
43 
44 void VsSpring::NaturalLength(float fltVal, bool bUseScaling)
45 {
46  Spring::NaturalLength(fltVal, bUseScaling);
47  if(m_vxSpring)
48  m_vxSpring->setNaturalLength(m_fltNaturalLength);
49 }
50 
51 void VsSpring::Stiffness(float fltVal, bool bUseScaling)
52 {
53  Spring::Stiffness(fltVal, bUseScaling);
54  if(m_vxSpring)
55  m_vxSpring->setStiffness(m_fltStiffness);
56 }
57 
58 void VsSpring::Damping(float fltVal, bool bUseScaling)
59 {
60  Spring::Damping(fltVal, bUseScaling);
61  if(m_vxSpring)
62  m_vxSpring->setDamping(m_fltDamping);
63 }
64 
66 {
67  Spring::InitializeAttachments();
68  SetupPhysics();
69 }
70 
71 void VsSpring::Physics_Resize()
72 {
73  VsRigidBody::Physics_Resize();
74  SetupPhysics();
75 }
76 
77 void VsSpring::DeletePhysics()
78 {
79  if(!m_vxSpring)
80  return;
81 
82  if(GetVsSimulator() && GetVsSimulator()->Universe())
83  {
84  GetVsSimulator()->Universe()->removeConstraint(m_vxSpring);
85  delete m_vxSpring;
86  }
87 
88  m_vxSpring = NULL;
89 }
90 
91 void VsSpring::SetupPhysics()
92 {
93  if(m_vxSpring)
94  DeletePhysics();
95 
96  if(m_aryAttachmentPoints.GetSize() == 2)
97  {
98  Attachment *lpPrimaryAttachment = m_aryAttachmentPoints[0];
99  Attachment *lpSecondaryAttachment = m_aryAttachmentPoints[1];
100 
101  if(!lpPrimaryAttachment && !lpSecondaryAttachment)
102  {
103  Enabled(false);
104  return;
105  }
106 
107  VsRigidBody *lpVsPrimary = dynamic_cast<VsRigidBody *>(lpPrimaryAttachment->Parent());
108  VsRigidBody *lpVsSecondary = dynamic_cast<VsRigidBody *>(lpSecondaryAttachment->Parent());
109 
110  if(!lpVsPrimary && !lpVsSecondary)
111  {
112  Enabled(false);
113  return;
114  }
115 
116  m_vxSpring = new AnimatVxSpring(lpVsPrimary->Part(), lpVsSecondary->Part(), m_fltNaturalLength, m_fltStiffness, m_fltDamping); // attached to the reference frame.
117 
118  CStdFPoint vPrimPos = lpPrimaryAttachment->AbsolutePosition();
119  CStdFPoint vSecPos = lpSecondaryAttachment->AbsolutePosition();
120 
121  m_vxSpring->setPartAttachmentPosition(0, vPrimPos.x, vPrimPos.y, vPrimPos.z);
122  m_vxSpring->setPartAttachmentPosition(1, vSecPos.x, vSecPos.y, vSecPos.z);
123 
124  m_vxSpring->enable(m_bEnabled);
125  GetVsSimulator()->Universe()->addConstraint(m_vxSpring);
126  }
127  else
128  Enabled(false);
129 }
130 
132 {
133  Spring::CreateJoints();
134  VsLine::CreateParts();
135 
136  SetupPhysics();
137 }
138 
139 void VsSpring::Enabled(bool bVal)
140 {
141  Spring::Enabled(bVal);
142 
143  if(m_vxSpring)
144  {
145  m_vxSpring->enable(m_bEnabled);
146  m_vxSpring->EnableBodies();
147  }
148 }
149 
150 void VsSpring::Physics_CollectData()
151 {
152  if(m_vxSpring)
153  {
155  if(m_bEnabled)
157  else
158  m_fltDisplacement = 0;
159 
161  m_fltEnergy = 0.5f*m_fltStiffnessNotScaled*m_fltDisplacement*m_fltDisplacement;
162  }
163 }
164 
166 {
167  Spring::ResetSimulation();
168  VsLine::ResetSimulation();
169 }
170 
171 void VsSpring::AfterResetSimulation()
172 {
173  Spring::AfterResetSimulation();
174  VsLine::AfterResetSimulation();
175 }
176 
178 {
179  Spring::StepSimulation();
180  VsLine::DrawLine();
181 }
182 
183 
184  } //Joints
185  } // Environment
186 } //VortexAnimatSim
virtual float Damping()
Gets the damping of the spring.
Definition: Spring.cpp:116
virtual float CalculateLength()
Calculates the length of the line.
Definition: LineBase.cpp:169
float m_fltStiffness
The stiffness of the spring.
Definition: Spring.h:38
virtual float NaturalLength()
Gets the natural length of the spring.
Definition: Spring.cpp:90
float m_fltNaturalLength
The natural length of the spring.
Definition: Spring.h:32
float m_fltDamping
The damping of the spring.
Definition: Spring.h:44
A common class for all rigid body data specific to vortex.
Definition: VsRigidBody.h:55
virtual void ResetSimulation()
Resets the simulation back to time 0.
Definition: VsSpring.cpp:165
virtual void InitializeAttachments()
Initializes the attachments.
Definition: VsSpring.cpp:65
virtual bool Enabled()
Tells whether this node is enabled.
Definition: Node.cpp:84
virtual void CreateJoints()
Allows the rigid body to create its joints using the chosen physics engine.
Definition: VsSpring.cpp:131
virtual void StepSimulation()
Step the simulation for this object.
Definition: VsSpring.cpp:177
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.
CStdArray< Attachment * > m_aryAttachmentPoints
A pointer to the primary attachment part.
Definition: LineBase.h:38
virtual float Stiffness()
Gets the stiffness of the spring.
Definition: Spring.cpp:103
bool m_bEnabled
Tells if this item is enabled or not. If it is not enabled then it is not run.
Definition: AnimatBase.h:40
float m_fltEnergy
The current energy contained in the spring.
Definition: Spring.h:56
float m_fltStiffnessNotScaled
The unscaled stiffness. This is used to calcuate the energy.
Definition: Spring.h:41
float m_fltLength
Current length of the line.
Definition: LineBase.h:29
float m_fltDisplacement
The current displacement of the spring from its natural length.
Definition: Spring.h:50
float m_fltNaturalLengthNotScaled
The unscaled natural length. This is used to calcuate the tension and displacement.
Definition: Spring.h:35
float m_fltTension
The current force being applied by the spring.
Definition: Spring.h:53