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 "trigger.h"
29#include "scenenode.h"
30
31namespace SBS {
32
33SceneNode::SceneNode(Object *parent, std::string name) : ObjectBase(parent)
34{
35 SetName(name);
36 node = 0;
37 Rotation = Vector3::ZERO;
38
39 if (!sbs->mSceneManager || !parent)
40 return;
41
42 //set up scene node
43 std::string node_name = GetNameBase() + name;
44
45 try
46 {
47 node = sbs->mSceneManager->createSceneNode(node_name);
48 }
49 catch (Ogre::Exception &e)
50 {
51 ReportError("Error creating scene node:\n" + e.getDescription());
52 return;
53 }
54
55 //attach scene node to root, if no parent exists (for engine root object)
56 if (!parent->GetParent() && !parent->GetSceneNode())
57 sbs->mSceneManager->getRootSceneNode()->addChild(node);
58}
59
61{
62 if (node)
63 sbs->mSceneManager->destroySceneNode(node);
64}
65
67{
68 //detach all movable objects from this scene node
69
70 if (node)
71 node->detachAllObjects();
72}
73
75{
76 //add a child scene node
77
78 if (node && scenenode && scenenode->GetRawSceneNode())
79 node->addChild(scenenode->GetRawSceneNode());
80}
81
83{
84 //remove a child scene node
85
86 if (node && scenenode)
87 {
88 Ogre::SceneNode *rawnode = scenenode->GetRawSceneNode();
89 if (rawnode)
90 node->removeChild(rawnode);
91 }
92}
93
95{
96 //show scene node's 3D bounding box
97
98 if (node)
99 node->showBoundingBox(value);
100}
101
102void SceneNode::SetPosition(const Vector3 &position, bool relative, bool force)
103{
104 //set position of scene node
105
106 if (!node)
107 return;
108
109 //prevent setting position outside of sim engine boundaries
110 if (sbs->GetAreaTrigger() && force == false)
111 {
112 if (sbs->GetAreaTrigger()->IsOutside(position) == true && relative == false)
113 {
114 if (sbs->Verbose)
115 ReportError("Cannot move outside of engine boundaries");
116 return;
117 }
118 }
119
120 if (relative == false)
121 {
122 //set absolute position
123 if (IsRoot() == false)
124 node->_setDerivedPosition(sbs->ToRemote(sbs->ToGlobal(position)));
125 else
126 node->_setDerivedPosition(sbs->ToRemote(position));
127 }
128 else
129 {
130 //set relative position
131 Vector3 pos = sbs->ToRemote(position);
132 node->setPosition(pos);
133 }
134 Update();
135}
136
138{
139 //get position of scene node
140 //if relative is true, position is relative of parent scene node
141
142 if (!node)
143 return Vector3::ZERO;
144
145 if (relative == false)
146 {
148 if (IsRoot() == false)
149 return sbs->FromGlobal(pos);
150 else
151 return pos;
152 }
153
154 return sbs->ToLocal(node->getPosition());
155}
156
157void SceneNode::SetRotation(const Vector3 &rotation)
158{
159 //set rotation of scene node in degrees
160 //this sets the rotation of all three vectors
161
162 if (!node)
163 return;
164
165 Rotation = rotation;
166
167 if (Rotation.x > 359)
168 Rotation.x -= 360;
169 if (Rotation.y > 359)
170 Rotation.y -= 360;
171 if (Rotation.z > 359)
172 Rotation.z -= 360;
173 if (Rotation.x < 0)
174 Rotation.x += 360;
175 if (Rotation.y < 0)
176 Rotation.y += 360;
177 if (Rotation.z < 0)
178 Rotation.z += 360;
179
180 Quaternion x(Degree(Rotation.x), Vector3::UNIT_X);
181 Quaternion y(Degree(Rotation.y), Vector3::NEGATIVE_UNIT_Y);
182 Quaternion z(Degree(Rotation.z), Vector3::UNIT_Z);
183 Quaternion rot = x * y * z;
184
185 SetOrientation(rot, true);
186}
187
189{
190 //get rotation of scene node in degrees
191 //this returns the rotation of all three vectors
192
193 return Rotation;
194}
195
197{
198 //sync positioning
199 //this mainly needs to be called on child scenenodes of a parent that has moved/rotated,
200 //to recalculate offsets
201
202 if (node)
203 node->needUpdate();
204}
205
207{
208 //return raw orientation
209
210 if (node)
211 {
212 if (relative == false)
213 {
214 if (IsRoot() == false)
216 else
217 return GetDerivedOrientation();
218 }
219 else
220 return node->getOrientation();
221 }
222
223 return Quaternion::ZERO;
224}
225
226void SceneNode::SetOrientation(const Quaternion &q, bool relative)
227{
228 //set raw orientation
229
230 if (!node)
231 return;
232
233 if (relative == false)
234 {
235 if (IsRoot() == false)
236 node->_setDerivedOrientation(sbs->ToGlobal(q));
237 else
238 node->_setDerivedOrientation(q);
239 }
240 else
241 node->setOrientation(q);
242
243 Update();
244}
245
246void SceneNode::Move(const Vector3 &vector, Real speed, bool force)
247{
248 //move this scene node
249
250 if (!node)
251 return;
252
253 Vector3 v = vector * speed;
254
255 //prevent movement outside sim engine boundaries
256 if (sbs->GetAreaTrigger() && force == false)
257 {
258 if (sbs->GetAreaTrigger()->IsOutside(GetPosition() + v) == true)
259 {
260 if (sbs->Verbose)
261 ReportError("Cannot move outside of engine boundaries");
262 return;
263 }
264 }
265
266 node->translate(sbs->ToRemote(v));
267
268 Update();
269}
270
271void SceneNode::AttachObject(Ogre::MovableObject *object)
272{
273 //attach a movable object to this node
274
275 if (node && object)
276 {
277 try
278 {
279 node->attachObject(object);
280 }
281 catch (Ogre::Exception &e)
282 {
283 ReportError("Error attaching object:\n" + e.getDescription());
284 }
285 }
286}
287
288void SceneNode::DetachObject(Ogre::MovableObject *object)
289{
290 //detach a movable object from this node
291
292 if (node && object)
293 {
294 try
295 {
296 node->detachObject(object);
297 }
298 catch (Ogre::Exception &e)
299 {
300 if (sbs->Verbose)
301 ReportError("Error detaching object:\n" + e.getDescription());
302 }
303 }
304}
305
307{
308 //get scaling factor
309
310 if (node)
311 return node->getScale().x;
312 return 0.0;
313}
314
316{
317 //set scaling factor
318
319 if (!node)
320 return;
321
322 node->setScale(Vector3(scale, scale, scale));
323}
324
325SceneNode* SceneNode::CreateChild(std::string name, const Vector3 &offset)
326{
327 //create a raw child scenenode, at the specified offset
328
329 if (!node || !GetParent())
330 return 0;
331
332 SceneNode *scenenode = new SceneNode(GetParent(), name);
333 node->addChild(scenenode->GetRawSceneNode());
334 scenenode->Move(offset);
335
336 return scenenode;
337}
338
340{
341 //get full name of a scene node, which includes object ID information
342
343 if (!node)
344 return "";
345
346 return node->getName();
347}
348
350{
351 return (this == sbs->GetSceneNode());
352}
353
355{
356 //rotate on X axis
357
358 node->pitch(Degree(degree));
359}
360
361void SceneNode::Yaw(Real &degree)
362{
363 //rotate on Y axis
364
365 node->yaw(Degree(-degree));
366}
367
369{
370 //rotate on Z axis
371
372 node->roll(Degree(degree));
373}
374
376{
377 //gets the position of the node as derived from all parents
378 return sbs->ToLocal(node->_getDerivedPosition());
379}
380
382{
383 //gets the orientation of the node as derived from all parents
384
385 return node->_getDerivedOrientation();
386}
387
388void SceneNode::SetDirection(const Vector3 &direction)
389{
390 node->setDirection(sbs->ToRemote(direction));
391}
392
393void SceneNode::LookAt(const Vector3 &point)
394{
395 node->lookAt(sbs->ToRemote(point), Ogre::Node::TS_PARENT);
396}
397
398}
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:246
Ogre::SceneManager * mSceneManager
Definition sbs.h:138
Trigger * GetAreaTrigger()
Definition sbs.cpp:4765
Vector3 ToGlobal(const Vector3 &position)
Definition sbs.cpp:4439
Real ToLocal(Real remote_value)
Definition sbs.cpp:2397
Vector3 FromGlobal(const Vector3 &position)
Definition sbs.cpp:4446
Real ToRemote(Real local_value)
Definition sbs.cpp:2437
bool Verbose
Definition sbs.h:186
void DetachObject(Ogre::MovableObject *object)
void ShowBoundingBox(bool value)
Definition scenenode.cpp:94
void RemoveChild(SceneNode *scenenode)
Definition scenenode.cpp:82
void Roll(Real &degree)
Vector3 GetRotation()
Ogre::SceneNode * GetRawSceneNode()
Definition scenenode.h:35
Ogre::SceneNode * node
Definition scenenode.h:65
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:66
void Pitch(Real &degree)
void SetPosition(const Vector3 &position, bool relative=false, bool force=false)
void Yaw(Real &degree)
void AttachObject(Ogre::MovableObject *object)
void Move(const Vector3 &vector, Real speed=1.0, bool force=false)
void SetScale(Real scale)
void DetachAllObjects()
Definition scenenode.cpp:66
void SetRotation(const Vector3 &rotation)
void SetDirection(const Vector3 &direction)
Vector3 GetPosition(bool relative=false)
Quaternion GetOrientation(bool relative=false)
SceneNode(Object *parent, std::string name)
Definition scenenode.cpp:33
void AddChild(SceneNode *scenenode)
Definition scenenode.cpp:74
Vector3 GetDerivedPosition()
void LookAt(const Vector3 &point)
std::string GetFullName()
bool IsOutside(const Vector3 &position)
Definition trigger.cpp:370
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