TrueReality  v0.1.1912
RingArray.cpp
Go to the documentation of this file.
1 /*
2 * True Reality Open Source Game and Simulation Engine
3 * Copyright © 2021 Acid Rain Studios LLC
4 *
5 * This library is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU Lesser General Public License as published by the Free
7 * Software Foundation; either version 3.0 of the License, or (at your option)
8 * any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; if not, write to the Free Software Foundation, Inc.,
17 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * @author Maxim Serebrennik
20 */
21 
23 
24 #include <trBase/SmrtPtr.h>
25 #include <trBase/Vec3.h>
26 #include <trBase/Quat.h>
27 #include <trUtil/Math.h>
28 #include <trUtil/PathUtils.h>
29 
30 #include <osg/Math>
31 #include <osgDB/ReadFile>
32 
33 #include <iostream>
34 
35 namespace trCore::SceneObjects
36 {
37  const std::string RingArray::RING_1_FILE_NAME("/RingArray/Ring1.osgb");
38  const std::string RingArray::RING_2_FILE_NAME("/RingArray/Ring2.osgb");
39  const std::string RingArray::RING_3_FILE_NAME("/RingArray/Ring3.osgb");
40  const std::string RingArray::RING_4_FILE_NAME("/RingArray/Ring4.osgb");
41 
44  {
45  LoadModels();
47  }
48 
50  RingArray::RingArray(double angleCorrection) : RingArray()
51  {
52  mAngleCorrection->setAttitude(*new trBase::Quat(trUtil::Math::Deg2Rad(angleCorrection), trBase::Z_AXIS));
53  }
54 
56  void RingArray::Update(double deltaTime)
57  {
58  UpdateRing1Slide(deltaTime);
59  }
60 
63  {
64  }
65 
68  {
69  //Creates nodes for the first ring
70  mRing1 = new osg::Group();
71  mRing1->addChild(osgDB::readNodeFile(trUtil::PathUtils::GetStaticMeshesPath() + RING_1_FILE_NAME));
72  mRing1Slide = new osg::PositionAttitudeTransform();
73 
74  //Creates nodes for the second ring
75  mRing2Rotation = new osg::PositionAttitudeTransform();
76  mRing2 = new osg::Group();
77  mRing2->addChild(osgDB::readNodeFile(trUtil::PathUtils::GetStaticMeshesPath() + RING_2_FILE_NAME));
78  mRing2Slide = new osg::PositionAttitudeTransform();
79 
80  //Creates Nodes for the third ring
81  mRing3Rotation = new osg::PositionAttitudeTransform();
82  mRing3 = new osg::Group();
83  mRing3->addChild(osgDB::readNodeFile(trUtil::PathUtils::GetStaticMeshesPath() + RING_3_FILE_NAME));
84  mRing3Slide = new osg::PositionAttitudeTransform();
85 
86  //Creates nodes for the smallest, internal ring.
87  mRing4Rotation = new osg::PositionAttitudeTransform();
89 
90  //Creates the angle correction transform.
91  mAngleCorrection = new osg::PositionAttitudeTransform();
92 
93  }
94 
97  {
98  //Create the Ring tree
99  mRing1->addChild(mRing1Slide);
100  mRing1Slide->addChild(mRing2Rotation);
101  mRing2Rotation->addChild(mRing2);
102  mRing2->addChild(mRing2Slide);
103  mRing2Slide->addChild(mRing3Rotation);
104  mRing3Rotation->addChild(mRing3);
105  mRing3->addChild(mRing3Slide);
106  mRing3Slide->addChild(mRing4Rotation);
107  mRing4Rotation->addChild(mRing4);
108 
109  //Add the initial angle correction.
110  mAngleCorrection->addChild(mRing1);
111 
112  //Add the tree to root
113  addChild(mAngleCorrection);
114  }
115 
117  void RingArray::UpdateRing1Slide(double deltaTime)
118  {
120  {
121  //Calculate the speed
123  {
124  mRing1SlideSpeed += RING_ACCEL * deltaTime;
125  }
126 
127  //Calculate the new position
128  mRing1SlideAngle += mRing1SlideSpeed * deltaTime;
129 
131  {
133  }
134 
136 
137  UpdateRing2Angle(deltaTime);
138  }
139  else
140  {
141  mRing1SlideDelay += deltaTime;
142  }
143  }
144 
146  void RingArray::UpdateRing2Angle(double deltaTime)
147  {
149  {
150  //Calculate the speed
152  {
153  mRing2RotationSpeed += RING_ACCEL * deltaTime;
154  }
155 
156  //Calculate the new position
158 
160  {
162  }
163 
164 
166 
167  UpdateRing2Slide(deltaTime);
168  }
169  else
170  {
171  mRing2RotationDelay += deltaTime;
172  }
173  }
174 
176  void RingArray::UpdateRing2Slide(double deltaTime)
177  {
179  {
180  //Calculate the speed
182  {
183  mRing2SlideSpeed += RING_ACCEL * deltaTime;
184  }
185 
186  //Calculate the new position
187  mRing2SlideAngle += mRing2SlideSpeed * deltaTime;
188 
190  {
192  }
193 
195 
196  UpdateRing3Angle(deltaTime);
197  }
198  else
199  {
200  mRing2SlideDelay += deltaTime;
201  }
202  }
203 
205  void RingArray::UpdateRing3Angle(double deltaTime)
206  {
208  {
209  //Calculate the speed
211  {
212  mRing3RotationSpeed += RING_ACCEL * deltaTime;
213  }
214 
215  //Calculate the new position
217 
219  {
221  }
222 
224 
225  UpdateRing3Slide(deltaTime);
226  }
227  else
228  {
229  mRing3RotationDelay += deltaTime;
230  }
231  }
232 
234  void RingArray::UpdateRing3Slide(double deltaTime)
235  {
237  {
238  //Calculate the speed
240  {
241  mRing3SlideSpeed += RING_ACCEL * deltaTime;
242  }
243 
244  //Calculate the new position
245  mRing3SlideAngle += mRing3SlideSpeed * deltaTime;
246 
248  {
250  }
251 
253 
254  UpdateRing4Angle(deltaTime);
255  }
256  else
257  {
258  mRing3SlideDelay += deltaTime;
259  }
260  }
261 
263  void RingArray::UpdateRing4Angle(double deltaTime)
264  {
266  {
267  //Calculate the speed
269  {
270  mRing4RotationSpeed += RING_ACCEL * deltaTime;
271  }
272 
273  //Calculate the new position
275 
277  {
279  }
280 
282  }
283  else
284  {
285  mRing4RotationDelay += deltaTime;
286  }
287  }
288 }
A rotating ring array model.
Definition: RingArray.h:47
RingArray()
Default constructor.
Definition: RingArray.cpp:43
static const double TWO_PI
/ pi*2.
Definition: Math.h:37
static const std::string RING_1_FILE_NAME
Definition: RingArray.h:51
Namespace that contains controls for sample scene objects.
double Deg2Rad(double degree)
Takes Degrees and converts them to Radians.
Definition: Math.h:92
trBase::SmrtPtr< osg::PositionAttitudeTransform > mRing2Slide
Definition: RingArray.h:174
trBase::SmrtPtr< osg::PositionAttitudeTransform > mRing3Slide
Definition: RingArray.h:183
const Vec3 Z_AXIS(0.0, 0.0, 1.0)
void UpdateRing4Angle(double deltaTime)
Updates the ring 4 angle.
Definition: RingArray.cpp:263
trBase::SmrtPtr< osg::PositionAttitudeTransform > mRing3Rotation
Definition: RingArray.h:179
void UpdateRing2Angle(double deltaTime)
Updates the ring 2 angle.
Definition: RingArray.cpp:146
void UpdateRing2Slide(double deltaTime)
Updates the ring 2 slide.
Definition: RingArray.cpp:176
trBase::SmrtPtr< osg::PositionAttitudeTransform > mRing2Rotation
Definition: RingArray.h:170
TR_UTIL_EXPORT std::string GetStaticMeshesPath()
Gets static meshes path.
Definition: PathUtils.cpp:488
trBase::SmrtPtr< osg::Group > mRing2
Definition: RingArray.h:169
Represents a quaternion, that is used for angular calculations and transformations.
Definition: Quat.h:44
trBase::SmrtPtr< osg::Group > mRing1
Definition: RingArray.h:164
trBase::SmrtPtr< osg::PositionAttitudeTransform > mAngleCorrection
Definition: RingArray.h:193
trBase::SmrtPtr< osg::Node > mRing4
Definition: RingArray.h:187
void UpdateRing3Slide(double deltaTime)
Updates the ring 3 slide.
Definition: RingArray.cpp:234
static const std::string RING_2_FILE_NAME
Definition: RingArray.h:52
static const std::string RING_3_FILE_NAME
Definition: RingArray.h:53
trBase::SmrtPtr< osg::PositionAttitudeTransform > mRing4Rotation
Definition: RingArray.h:188
const Vec3 Y_AXIS(0.0, 1.0, 0.0)
trBase::SmrtPtr< osg::PositionAttitudeTransform > mRing1Slide
Definition: RingArray.h:165
const Vec3 X_AXIS(1.0, 0.0, 0.0)
virtual void Update(double deltaTime)
This function gets called by the RingArrayCallback.
Definition: RingArray.cpp:56
trBase::SmrtPtr< osg::Group > mRing3
Definition: RingArray.h:178
static const std::string RING_4_FILE_NAME
Definition: RingArray.h:54
void UpdateRing3Angle(double deltaTime)
Updates the ring 3 angle.
Definition: RingArray.cpp:205
void SetupModelTree()
Sets up the model tree.
Definition: RingArray.cpp:96
void LoadModels()
Loads the models and creates transform nodes for the model tree.
Definition: RingArray.cpp:67
void UpdateRing1Slide(double deltaTime)
Updates the ring 1 slide.
Definition: RingArray.cpp:117