Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/FICN/src/orxonox/objects/WorldEntity.cc @ 768

Last change on this file since 768 was 742, checked in by landauf, 17 years ago

moved all files from misc and the tinyxml folder into the new util folder

File size: 6.6 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *
4 *
5 *   License notice:
6 *
7 *   This program is free software; you can redistribute it and/or
8 *   modify it under the terms of the GNU General Public License
9 *   as published by the Free Software Foundation; either version 2
10 *   of the License, or (at your option) any later version.
11 *
12 *   This program is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with this program; if not, write to the Free Software
19 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 *
21 *   Author:
22 *      Fabian 'x3n' Landau
23 *   Co-authors:
24 *      ...
25 *
26 */
27
28#include <string>
29#include <sstream>
30
31#include "util/tinyxml/tinyxml.h"
32#include "util/Tokenizer.h"
33#include "util/String2Number.h"
34#include "../core/CoreIncludes.h"
35#include "../Orxonox.h"
36#include "WorldEntity.h"
37
38namespace orxonox
39{
40    CreateFactory(WorldEntity);
41
42    unsigned int WorldEntity::worldEntityCounter_s = 0;
43
44    WorldEntity::WorldEntity()
45    {
46        RegisterObject(WorldEntity);
47
48        if (Orxonox::getSingleton()->getSceneManager())
49        {
50            std::ostringstream name;
51            name << (WorldEntity::worldEntityCounter_s++);
52            this->setName("WorldEntity" + name.str());
53            this->node_ = Orxonox::getSingleton()->getSceneManager()->getRootSceneNode()->createChildSceneNode(this->getName());
54        }
55        else
56        {
57            this->node_ = 0;
58        }
59
60        this->bStatic_ = true;
61        this->velocity_ = Vector3(0, 0, 0);
62        this->acceleration_ = Vector3(0, 0, 0);
63        this->rotationAxis_ = Vector3(0, 1, 0);
64        this->rotationRate_ = 0;
65        this->momentum_ = 0;
66    }
67
68    WorldEntity::~WorldEntity()
69    {
70    }
71
72    void WorldEntity::tick(float dt)
73    {
74        if (!this->bStatic_)
75        {
76            this->velocity_ += (dt * this->acceleration_);
77            this->translate(dt * this->velocity_, Ogre::Node::TS_LOCAL);
78
79            this->rotationRate_ += (dt * this->momentum_);
80            this->rotate(this->rotationAxis_, dt * this->rotationRate_);
81        }
82    }
83
84    void WorldEntity::loadParams(TiXmlElement* xmlElem)
85    {
86        BaseObject::loadParams(xmlElem);
87
88        if (xmlElem->Attribute("position"))
89        {
90            std::vector<std::string> pos = tokenize(xmlElem->Attribute("position"),",");
91            float x, y, z;
92            String2Number<float>(x, pos[0]);
93            String2Number<float>(y, pos[1]);
94            String2Number<float>(z, pos[2]);
95            this->setPosition(x, y, z);
96        }
97
98        if (xmlElem->Attribute("direction"))
99        {
100            std::vector<std::string> pos = tokenize(xmlElem->Attribute("direction"),",");
101            float x, y, z;
102            String2Number<float>(x, pos[0]);
103            String2Number<float>(y, pos[1]);
104            String2Number<float>(z, pos[2]);
105            this->setDirection(x, y, z);
106        }
107
108        if (xmlElem->Attribute("yaw") || xmlElem->Attribute("pitch") || xmlElem->Attribute("roll"))
109        {
110            float yaw = 0.0, pitch = 0.0, roll = 0.0;
111            if (xmlElem->Attribute("yaw"))
112                String2Number<float>(yaw,xmlElem->Attribute("yaw"));
113
114            if (xmlElem->Attribute("pitch"))
115                String2Number<float>(pitch,xmlElem->Attribute("pitch"));
116
117            if (xmlElem->Attribute("roll"))
118                String2Number<float>(roll,xmlElem->Attribute("roll"));
119
120            this->yaw(Degree(yaw));
121            this->pitch(Degree(pitch));
122            this->roll(Degree(roll));
123        }
124
125        if (xmlElem->Attribute("scale"))
126        {
127            std::string scaleStr = xmlElem->Attribute("scale");
128            float scale;
129            String2Number<float>(scale, scaleStr);
130            this->setScale(scale);
131        }
132
133        if (xmlElem->Attribute("rotationAxis"))
134        {
135            std::vector<std::string> pos = tokenize(xmlElem->Attribute("rotationAxis"),",");
136            float x, y, z;
137            String2Number<float>(x, pos[0]);
138            String2Number<float>(y, pos[1]);
139            String2Number<float>(z, pos[2]);
140            this->setRotationAxis(x, y, z);
141        }
142
143        if (xmlElem->Attribute("rotationRate"))
144        {
145            float rotationRate;
146            String2Number<float>(rotationRate, xmlElem->Attribute("rotationRate"));
147            this->setRotationRate(Degree(rotationRate));
148            if (rotationRate != 0)
149                this->setStatic(false);
150        }
151
152        create();
153
154    }
155
156    bool WorldEntity::create(){
157      registerAllVariables();
158      return true;
159    }
160
161    void WorldEntity::registerAllVariables()
162    {
163      // register coordinates
164      registerVar( (void*) &(this->getPosition().x), sizeof(this->getPosition().x), network::DATA);
165      registerVar( (void*) &(this->getPosition().y), sizeof(this->getPosition().y), network::DATA);
166      registerVar( (void*) &(this->getPosition().z), sizeof(this->getPosition().z), network::DATA);
167      // register orientation
168      registerVar( (void*) &(this->getOrientation().w), sizeof(this->getOrientation().w), network::DATA);
169      registerVar( (void*) &(this->getOrientation().x), sizeof(this->getOrientation().x), network::DATA);
170      registerVar( (void*) &(this->getOrientation().y), sizeof(this->getOrientation().y), network::DATA);
171      registerVar( (void*) &(this->getOrientation().z), sizeof(this->getOrientation().z), network::DATA);
172      // not needed at the moment, because we don't have prediction yet
173      /*// register velocity_
174      registerVar( (void*) &(this->getVelocity().x), sizeof(this->getVelocity().x), network::DATA);
175      registerVar( (void*) &(this->getVelocity().y), sizeof(this->getVelocity().y), network::DATA);
176      registerVar( (void*) &(this->getVelocity().z), sizeof(this->getVelocity().z), network::DATA);
177      // register rotationAxis/rate
178      registerVar( (void*) &(this->getRotationRate()), sizeof(this->getRotationRate()), network::DATA);
179      registerVar( (void*) &(this->getRotationAxis().x), sizeof(this->getRotationAxis().x), network::DATA);
180      registerVar( (void*) &(this->getRotationAxis().y), sizeof(this->getRotationAxis().y), network::DATA);
181      registerVar( (void*) &(this->getRotationAxis().z), sizeof(this->getRotationAxis().z), network::DATA);*/
182    }
183}
Note: See TracBrowser for help on using the repository browser.