Skyscraper 2.0
scenenode.cpp
Go to the documentation of this file.
1/*
2 Scalable Building Simulator - SceneNode Object
3 The Skyscraper Project - Version 2.0
4 Copyright (C)2004-2024 Ryan Thoryk
5 https://www.skyscrapersim.net
6 https://sourceforge.net/projects/skyscraper/
7 Contact - ryan@skyscrapersim.net
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22*/
23
24#include <OgreSceneManager.h>
25#include <OgreSceneNode.h>
26#include "globals.h"
27#include "sbs.h"
28#include "scenenode.h"
29
30namespace SBS {
31
32SceneNode::SceneNode(Object *parent, std::string name) : ObjectBase(parent)
33{
34 SetName(name);
35 node = 0;
36 Rotation = Vector3::ZERO;
37
38 if (!sbs->mSceneManager || !parent)
39 return;
40
41 //set up scene node
42 std::string node_name = GetNameBase() + name;
43
44 try
45 {
46 node = sbs->mSceneManager->createSceneNode(node_name);
47 }
48 catch (Ogre::Exception &e)
49 {
50 ReportError("Error creating scene node:\n" + e.getDescription());
51 return;
52 }
53
54 //attach scene node to root, if no parent exists (for engine root object)
55 if (!parent->GetParent() && !parent->GetSceneNode())
56 sbs->mSceneManager->getRootSceneNode()->addChild(node);
57}
58
60{
61 if (node)
62 sbs->mSceneManager->destroySceneNode(node);
63}
64
66{
67 //detach all movable objects from this scene node
68
69 if (node)
70 node->detachAllObjects();
71}
72
74{
75 //add a child scene node
76
77 if (node && scenenode && scenenode->GetRawSceneNode())
78 node->addChild(scenenode->GetRawSceneNode());
79}
80
82{
83 //remove a child scene node
84
85 if (node && scenenode)
86 {
87 Ogre::SceneNode *rawnode = scenenode->GetRawSceneNode();
88 if (rawnode)
89 node->removeChild(rawnode);
90 }
91}
92
94{
95 //show scene node's 3D bounding box
96
97 if (node)
98 node->showBoundingBox(value);
99}
100
101void SceneNode::SetPosition(const Vector3 &position)
102{
103 //set position of scene node
104
105 if (!node)
106 return;
107
108 if (IsRoot() == false)
109 node->_setDerivedPosition(sbs->ToRemote(sbs->ToGlobal(position)));
110 else
111 node->_setDerivedPosition(sbs->ToRemote(position));
112 Update();
113}
114
116{
117 //set position of scene node
118 //position is relative of parent scene node
119
120 if (!node)
121 return;
122
123 Vector3 pos = sbs->ToRemote(position);
124 node->setPosition(pos);
125 Update();
126}
127
129{
130 //get position of scene node
131 //if relative is true, position is relative of parent scene node
132
133 if (!node)
134 return Vector3::ZERO;
135
136 if (relative == false)
137 {
139 if (IsRoot() == false)
140 return sbs->FromGlobal(pos);
141 else
142 return pos;
143 }
144
145 return sbs->ToLocal(node->getPosition());
146}
147
148void SceneNode::SetRotation(const Vector3 &rotation)
149{
150 //set rotation of scene node in degrees
151 //this sets the rotation of all three vectors
152
153 if (!node)
154 return;
155
156 Rotation = rotation;
157
158 if (Rotation.x > 359)
159 Rotation.x -= 360;
160 if (Rotation.y > 359)
161 Rotation.y -= 360;
162 if (Rotation.z > 359)
163 Rotation.z -= 360;
164 if (Rotation.x < 0)
165 Rotation.x += 360;
166 if (Rotation.y < 0)
167 Rotation.y += 360;
168 if (Rotation.z < 0)
169 Rotation.z += 360;
170
171 Quaternion x(Degree(Rotation.x), Vector3::UNIT_X);
172 Quaternion y(Degree(Rotation.y), Vector3::NEGATIVE_UNIT_Y);
173 Quaternion z(Degree(Rotation.z), Vector3::UNIT_Z);
174 Quaternion rot = x * y * z;
175
176 SetOrientation(rot, true);
177}
178
180{
181 //get rotation of scene node in degrees
182 //this returns the rotation of all three vectors
183
184 return Rotation;
185}
186
188{
189 //sync positioning
190 //this mainly needs to be called on child scenenodes of a parent that has moved/rotated,
191 //to recalculate offsets
192
193 if (node)
194 node->needUpdate();
195}
196
198{
199 //return raw orientation
200
201 if (node)
202 {
203 if (relative == false)
204 {
205 if (IsRoot() == false)
207 else
208 return GetDerivedOrientation();
209 }
210 else
211 return node->getOrientation();
212 }
213
214 return Quaternion::ZERO;
215}
216
217void SceneNode::SetOrientation(const Quaternion &q, bool relative)
218{
219 //set raw orientation
220
221 if (!node)
222 return;
223
224 if (relative == false)
225 {
226 if (IsRoot() == false)
227 node->_setDerivedOrientation(sbs->ToGlobal(q));
228 else
229 node->_setDerivedOrientation(q);
230 }
231 else
232 node->setOrientation(q);
233
234 Update();
235}
236
237void SceneNode::Move(const Vector3 &vector, Real speed)
238{
239 //move this scene node
240
241 if (!node)
242 return;
243
244 Vector3 v = vector * speed;
245 node->translate(sbs->ToRemote(v));
246
247 Update();
248}
249
250void SceneNode::AttachObject(Ogre::MovableObject *object)
251{
252 //attach a movable object to this node
253
254 if (node && object)
255 {
256 try
257 {
258 node->attachObject(object);
259 }
260 catch (Ogre::Exception &e)
261 {
262 ReportError("Error attaching object:\n" + e.getDescription());
263 }
264 }
265}
266
267void SceneNode::DetachObject(Ogre::MovableObject *object)
268{
269 //detach a movable object from this node
270
271 if (node && object)
272 {
273 try
274 {
275 node->detachObject(object);
276 }
277 catch (Ogre::Exception &e)
278 {
279 if (sbs->Verbose)
280 ReportError("Error detaching object:\n" + e.getDescription());
281 }
282 }
283}
284
286{
287 //get scaling factor
288
289 if (node)
290 return node->getScale().x;
291 return 0.0;
292}
293
295{
296 //set scaling factor
297
298 if (!node)
299 return;
300
301 node->setScale(Vector3(scale, scale, scale));
302}
303
304SceneNode* SceneNode::CreateChild(std::string name, const Vector3 &offset)
305{
306 //create a raw child scenenode, at the specified offset
307
308 if (!node || !GetParent())
309 return 0;
310
311 SceneNode *scenenode = new SceneNode(GetParent(), name);
312 node->addChild(scenenode->GetRawSceneNode());
313 scenenode->Move(offset);
314
315 return scenenode;
316}
317
319{
320 //get full name of a scene node, which includes object ID information
321
322 if (!node)
323 return "";
324
325 return node->getName();
326}
327
329{
330 return (this == sbs->GetSceneNode());
331}
332
334{
335 //rotate on X axis
336
337 node->pitch(Degree(degree));
338}
339
340void SceneNode::Yaw(Real &degree)
341{
342 //rotate on Y axis
343
344 node->yaw(Degree(-degree));
345}
346
348{
349 //rotate on Z axis
350
351 node->roll(Degree(degree));
352}
353
355{
356 //gets the position of the node as derived from all parents
357 return sbs->ToLocal(node->_getDerivedPosition());
358}
359
361{
362 //gets the orientation of the node as derived from all parents
363
364 return node->_getDerivedOrientation();
365}
366
367void SceneNode::SetDirection(const Vector3 &direction)
368{
369 node->setDirection(sbs->ToRemote(direction));
370}
371
372void SceneNode::LookAt(const Vector3 &point)
373{
374 node->lookAt(sbs->ToRemote(point), Ogre::Node::TS_PARENT);
375}
376
377}
std::string GetNameBase()
Definition object.cpp:59
virtual bool ReportError(const std::string &message)
Definition object.cpp:84
Object * GetParent()
Definition object.cpp:42
void SetName(const std::string &name)
Definition object.cpp:72
SceneNode * GetSceneNode()
Definition object.cpp:240
Ogre::SceneManager * mSceneManager
Definition sbs.h:138
Vector3 ToGlobal(const Vector3 &position)
Definition sbs.cpp:4409
Real ToLocal(Real remote_value)
Definition sbs.cpp:2367
Vector3 FromGlobal(const Vector3 &position)
Definition sbs.cpp:4416
Real ToRemote(Real local_value)
Definition sbs.cpp:2407
bool Verbose
Definition sbs.h:186
void DetachObject(Ogre::MovableObject *object)
void ShowBoundingBox(bool value)
Definition scenenode.cpp:93
void RemoveChild(SceneNode *scenenode)
Definition scenenode.cpp:81
void Roll(Real &degree)
void SetPosition(const Vector3 &position)
Vector3 GetRotation()
Ogre::SceneNode * GetRawSceneNode()
Definition scenenode.h:35
Ogre::SceneNode * node
Definition scenenode.h:66
void SetOrientation(const Quaternion &q, bool relative=false)
SceneNode * CreateChild(std::string name, const Vector3 &offset=Vector3::ZERO)
Quaternion GetDerivedOrientation()
Vector3 Rotation
Definition scenenode.h:67
void Pitch(Real &degree)
void Yaw(Real &degree)
void AttachObject(Ogre::MovableObject *object)
void SetScale(Real scale)
void Move(const Vector3 &vector, Real speed=1.0)
void DetachAllObjects()
Definition scenenode.cpp:65
void SetRotation(const Vector3 &rotation)
void SetPositionRelative(const Vector3 &position)
void SetDirection(const Vector3 &direction)
Vector3 GetPosition(bool relative=false)
Quaternion GetOrientation(bool relative=false)
SceneNode(Object *parent, std::string name)
Definition scenenode.cpp:32
void AddChild(SceneNode *scenenode)
Definition scenenode.cpp:73
Vector3 GetDerivedPosition()
void LookAt(const Vector3 &point)
std::string GetFullName()
Ogre::Vector3 Vector3
Definition globals.h:58
Ogre::Real Real
Definition globals.h:57
Ogre::Quaternion Quaternion
Definition globals.h:60
Ogre::Degree Degree
Definition globals.h:61