| 1 | /* | 
|---|
| 2 |  *   ORXONOX - the hottest 3D action shooter ever to exist | 
|---|
| 3 |  *                    > www.orxonox.net < | 
|---|
| 4 |  * | 
|---|
| 5 |  * | 
|---|
| 6 |  *   License notice: | 
|---|
| 7 |  * | 
|---|
| 8 |  *   This program is free software; you can redistribute it and/or | 
|---|
| 9 |  *   modify it under the terms of the GNU General Public License | 
|---|
| 10 |  *   as published by the Free Software Foundation; either version 2 | 
|---|
| 11 |  *   of the License, or (at your option) any later version. | 
|---|
| 12 |  * | 
|---|
| 13 |  *   This program is distributed in the hope that it will be useful, | 
|---|
| 14 |  *   but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 15 |  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 16 |  *   GNU General Public License for more details. | 
|---|
| 17 |  * | 
|---|
| 18 |  *   You should have received a copy of the GNU General Public License | 
|---|
| 19 |  *   along with this program; if not, write to the Free Software | 
|---|
| 20 |  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
|---|
| 21 |  * | 
|---|
| 22 |  *   Author: | 
|---|
| 23 |  *      Fabian 'x3n' Landau | 
|---|
| 24 |  *   Co-authors: | 
|---|
| 25 |  *      ... | 
|---|
| 26 |  * | 
|---|
| 27 |  */ | 
|---|
| 28 |  | 
|---|
| 29 | #include "OrxonoxStableHeaders.h" | 
|---|
| 30 | #include "WorldEntity.h" | 
|---|
| 31 |  | 
|---|
| 32 | #include <string> | 
|---|
| 33 | #include <sstream> | 
|---|
| 34 |  | 
|---|
| 35 | #include "util/tinyxml/tinyxml.h" | 
|---|
| 36 | #include "util/SubString.h" | 
|---|
| 37 | #include "core/CoreIncludes.h" | 
|---|
| 38 | #include "GraphicsEngine.h" | 
|---|
| 39 | #include "core/XMLPort.h" | 
|---|
| 40 |  | 
|---|
| 41 | namespace orxonox | 
|---|
| 42 | { | 
|---|
| 43 |     CreateFactory(WorldEntity); | 
|---|
| 44 |  | 
|---|
| 45 |     unsigned int WorldEntity::worldEntityCounter_s = 0; | 
|---|
| 46 |  | 
|---|
| 47 |     WorldEntity::WorldEntity() | 
|---|
| 48 |     { | 
|---|
| 49 |         RegisterObject(WorldEntity); | 
|---|
| 50 |  | 
|---|
| 51 |  | 
|---|
| 52 |         this->bStatic_ = true; | 
|---|
| 53 |         this->velocity_ = Vector3(0, 0, 0); | 
|---|
| 54 |         this->acceleration_ = Vector3(0, 0, 0); | 
|---|
| 55 |         this->rotationAxis_ = Vector3(0, 1, 0); | 
|---|
| 56 |         this->rotationRate_ = 0; | 
|---|
| 57 |         this->momentum_ = 0; | 
|---|
| 58 |  | 
|---|
| 59 |         if (GraphicsEngine::getSingleton().getSceneManager()) | 
|---|
| 60 |         { | 
|---|
| 61 |           std::ostringstream name; | 
|---|
| 62 |           name << (WorldEntity::worldEntityCounter_s++); | 
|---|
| 63 |           this->setName("WorldEntity" + name.str()); | 
|---|
| 64 |           this->node_ = GraphicsEngine::getSingleton().getSceneManager()->getRootSceneNode()->createChildSceneNode(this->getName()); | 
|---|
| 65 |  | 
|---|
| 66 |           registerAllVariables(); | 
|---|
| 67 |         } | 
|---|
| 68 |         else | 
|---|
| 69 |         { | 
|---|
| 70 |           this->node_ = 0; | 
|---|
| 71 |         } | 
|---|
| 72 |     } | 
|---|
| 73 |      | 
|---|
| 74 |  | 
|---|
| 75 |     WorldEntity::~WorldEntity() | 
|---|
| 76 |     { | 
|---|
| 77 |     } | 
|---|
| 78 |  | 
|---|
| 79 |     void WorldEntity::tick(float dt) | 
|---|
| 80 |     { | 
|---|
| 81 |         if (!this->bStatic_) | 
|---|
| 82 |         { | 
|---|
| 83 | //             COUT(4) << "acceleration: " << this->acceleration_ << " velocity: " << this->velocity_ << std::endl; | 
|---|
| 84 |             this->velocity_ += (dt * this->acceleration_); | 
|---|
| 85 |             this->translate(dt * this->velocity_, Ogre::Node::TS_LOCAL); | 
|---|
| 86 |  | 
|---|
| 87 |             this->rotationRate_ += (dt * this->momentum_); | 
|---|
| 88 |             this->rotate(this->rotationAxis_, dt * this->rotationRate_); | 
|---|
| 89 |         } | 
|---|
| 90 |     } | 
|---|
| 91 |  | 
|---|
| 92 |     void WorldEntity::loadParams(TiXmlElement* xmlElem) | 
|---|
| 93 |     { | 
|---|
| 94 |  | 
|---|
| 95 |         BaseObject::loadParams(xmlElem); | 
|---|
| 96 |         create(); | 
|---|
| 97 | /* | 
|---|
| 98 |         if (xmlElem->Attribute("position")) | 
|---|
| 99 |         { | 
|---|
| 100 |             std::vector<std::string> pos = tokenize(xmlElem->Attribute("position"),","); | 
|---|
| 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->setPosition(x, y, z); | 
|---|
| 106 |         } | 
|---|
| 107 |  | 
|---|
| 108 |         if (xmlElem->Attribute("direction")) | 
|---|
| 109 |         { | 
|---|
| 110 |             std::vector<std::string> pos = tokenize(xmlElem->Attribute("direction"),","); | 
|---|
| 111 |             float x, y, z; | 
|---|
| 112 |             String2Number<float>(x, pos[0]); | 
|---|
| 113 |             String2Number<float>(y, pos[1]); | 
|---|
| 114 |             String2Number<float>(z, pos[2]); | 
|---|
| 115 |             this->setDirection(x, y, z); | 
|---|
| 116 |         } | 
|---|
| 117 |  | 
|---|
| 118 |         if (xmlElem->Attribute("yaw") || xmlElem->Attribute("pitch") || xmlElem->Attribute("roll")) | 
|---|
| 119 |         { | 
|---|
| 120 |             float yaw = 0.0, pitch = 0.0, roll = 0.0; | 
|---|
| 121 |             if (xmlElem->Attribute("yaw")) | 
|---|
| 122 |                 String2Number<float>(yaw,xmlElem->Attribute("yaw")); | 
|---|
| 123 |  | 
|---|
| 124 |             if (xmlElem->Attribute("pitch")) | 
|---|
| 125 |                 String2Number<float>(pitch,xmlElem->Attribute("pitch")); | 
|---|
| 126 |  | 
|---|
| 127 |             if (xmlElem->Attribute("roll")) | 
|---|
| 128 |                 String2Number<float>(roll,xmlElem->Attribute("roll")); | 
|---|
| 129 |  | 
|---|
| 130 |             this->yaw(Degree(yaw)); | 
|---|
| 131 |             this->pitch(Degree(pitch)); | 
|---|
| 132 |             this->roll(Degree(roll)); | 
|---|
| 133 |         } | 
|---|
| 134 |  | 
|---|
| 135 |         if (xmlElem->Attribute("scale")) | 
|---|
| 136 |         { | 
|---|
| 137 |             std::string scaleStr = xmlElem->Attribute("scale"); | 
|---|
| 138 |             float scale; | 
|---|
| 139 |             String2Number<float>(scale, scaleStr); | 
|---|
| 140 |             this->setScale(scale); | 
|---|
| 141 |         } | 
|---|
| 142 |  | 
|---|
| 143 |         if (xmlElem->Attribute("rotationAxis")) | 
|---|
| 144 |         { | 
|---|
| 145 |             std::vector<std::string> pos = tokenize(xmlElem->Attribute("rotationAxis"),","); | 
|---|
| 146 |             float x, y, z; | 
|---|
| 147 |             String2Number<float>(x, pos[0]); | 
|---|
| 148 |             String2Number<float>(y, pos[1]); | 
|---|
| 149 |             String2Number<float>(z, pos[2]); | 
|---|
| 150 |             this->setRotationAxis(x, y, z); | 
|---|
| 151 |         } | 
|---|
| 152 |  | 
|---|
| 153 |         if (xmlElem->Attribute("rotationRate")) | 
|---|
| 154 |         { | 
|---|
| 155 |             float rotationRate; | 
|---|
| 156 |             String2Number<float>(rotationRate, xmlElem->Attribute("rotationRate")); | 
|---|
| 157 |             this->setRotationRate(Degree(rotationRate)); | 
|---|
| 158 |             if (rotationRate != 0) | 
|---|
| 159 |                 this->setStatic(false); | 
|---|
| 160 |         } | 
|---|
| 161 |  | 
|---|
| 162 |         create(); | 
|---|
| 163 | */ | 
|---|
| 164 |     } | 
|---|
| 165 |      | 
|---|
| 166 |  | 
|---|
| 167 |     void WorldEntity::setYawPitchRoll(const Degree& yaw, const Degree& pitch, const Degree& roll) | 
|---|
| 168 |     { | 
|---|
| 169 |         this->yaw(yaw); | 
|---|
| 170 |         this->pitch(pitch); | 
|---|
| 171 |         this->roll(roll); | 
|---|
| 172 |     } | 
|---|
| 173 |  | 
|---|
| 174 |     /** | 
|---|
| 175 |         @brief XML loading and saving. | 
|---|
| 176 |         @param xmlelement The XML-element | 
|---|
| 177 |         @param loading Loading (true) or saving (false) | 
|---|
| 178 |         @return The XML-element | 
|---|
| 179 |     */ | 
|---|
| 180 |     void WorldEntity::XMLPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
| 181 |     { | 
|---|
| 182 |         BaseObject::XMLPort(xmlelement, mode); | 
|---|
| 183 |  | 
|---|
| 184 |         XMLPortParam(WorldEntity, "position", setPositionLoader2, getPosition, xmlelement, mode); | 
|---|
| 185 |         XMLPortParamLoadOnly(WorldEntity, "direction", setDirectionLoader, xmlelement, mode); | 
|---|
| 186 |         XMLPortParamLoadOnly(WorldEntity, "yawpitchroll", setYawPitchRoll, xmlelement, mode); | 
|---|
| 187 |         XMLPortParam(WorldEntity, "scale", setTotalScale, getScale, xmlelement, mode); | 
|---|
| 188 |         XMLPortParam(WorldEntity, "rotationAxis", setRotationAxisLoader, getRotationAxis, xmlelement, mode); | 
|---|
| 189 |         XMLPortParam(WorldEntity, "rotationRate", setRotationRate, getRotationRate, xmlelement, mode); | 
|---|
| 190 |  | 
|---|
| 191 |         XMLPortObject(WorldEntity, WorldEntity, "attached", attachWorldEntity, getAttachedWorldEntity, xmlelement, mode, false, true); | 
|---|
| 192 |          | 
|---|
| 193 |         WorldEntity::create(); | 
|---|
| 194 |     } | 
|---|
| 195 |  | 
|---|
| 196 |  | 
|---|
| 197 |     void WorldEntity::registerAllVariables() | 
|---|
| 198 |     { | 
|---|
| 199 |       // register coordinates | 
|---|
| 200 |       registerVar( (void*) &(this->getPosition().x), sizeof(this->getPosition().x), network::DATA, 0x3); | 
|---|
| 201 |       registerVar( (void*) &(this->getPosition().y), sizeof(this->getPosition().y), network::DATA, 0x3); | 
|---|
| 202 |       registerVar( (void*) &(this->getPosition().z), sizeof(this->getPosition().z), network::DATA, 0x3); | 
|---|
| 203 |       // register orientation | 
|---|
| 204 |       registerVar( (void*) &(this->getOrientation().w), sizeof(this->getOrientation().w), network::DATA, 0x3); | 
|---|
| 205 |       registerVar( (void*) &(this->getOrientation().x), sizeof(this->getOrientation().x), network::DATA, 0x3); | 
|---|
| 206 |       registerVar( (void*) &(this->getOrientation().y), sizeof(this->getOrientation().y), network::DATA, 0x3); | 
|---|
| 207 |       registerVar( (void*) &(this->getOrientation().z), sizeof(this->getOrientation().z), network::DATA, 0x3); | 
|---|
| 208 |       // register velocity_ | 
|---|
| 209 |       registerVar( (void*) &(this->getVelocity().x), sizeof(this->getVelocity().x), network::DATA, 0x3); | 
|---|
| 210 |       registerVar( (void*) &(this->getVelocity().y), sizeof(this->getVelocity().y), network::DATA, 0x3); | 
|---|
| 211 |       registerVar( (void*) &(this->getVelocity().z), sizeof(this->getVelocity().z), network::DATA, 0x3); | 
|---|
| 212 |       // register rotationAxis/rate | 
|---|
| 213 |       registerVar( (void*) &(this->getRotationRate()), sizeof(this->getRotationRate()), network::DATA, 0x3); | 
|---|
| 214 |       registerVar( (void*) &(this->getRotationAxis().x), sizeof(this->getRotationAxis().x), network::DATA, 0x3); | 
|---|
| 215 |       registerVar( (void*) &(this->getRotationAxis().y), sizeof(this->getRotationAxis().y), network::DATA, 0x3); | 
|---|
| 216 |       registerVar( (void*) &(this->getRotationAxis().z), sizeof(this->getRotationAxis().z), network::DATA, 0x3); | 
|---|
| 217 |       // register scale of node | 
|---|
| 218 |       registerVar( (void*) &(this->getScale().x), sizeof(this->getScale().x), network::DATA, 0x3); | 
|---|
| 219 |       registerVar( (void*) &(this->getScale().y), sizeof(this->getScale().y), network::DATA, 0x3); | 
|---|
| 220 |       registerVar( (void*) &(this->getScale().z), sizeof(this->getScale().z), network::DATA, 0x3); | 
|---|
| 221 |       //register staticity | 
|---|
| 222 |       registerVar( (void*) &(this->bStatic_), sizeof(this->bStatic_), network::DATA, 0x3); | 
|---|
| 223 |       //register acceleration | 
|---|
| 224 |       // register velocity_ | 
|---|
| 225 | //       registerVar( (void*) &(this->getAcceleration().x), sizeof(this->getAcceleration().x), network::DATA, 0x3); | 
|---|
| 226 | //       registerVar( (void*) &(this->getAcceleration().y), sizeof(this->getAcceleration().y), network::DATA, 0x3); | 
|---|
| 227 | //       registerVar( (void*) &(this->getAcceleration().z), sizeof(this->getAcceleration().z), network::DATA, 0x3); | 
|---|
| 228 |     } | 
|---|
| 229 |  | 
|---|
| 230 |     void WorldEntity::attachWorldEntity(WorldEntity* entity) | 
|---|
| 231 |     { | 
|---|
| 232 |         this->attachedWorldEntities_.push_back(entity); | 
|---|
| 233 |     } | 
|---|
| 234 |  | 
|---|
| 235 |     const WorldEntity* WorldEntity::getAttachedWorldEntity(unsigned int index) const | 
|---|
| 236 |     { | 
|---|
| 237 |         if (index < this->attachedWorldEntities_.size()) | 
|---|
| 238 |             return this->attachedWorldEntities_[index]; | 
|---|
| 239 |         else | 
|---|
| 240 |             return 0; | 
|---|
| 241 |     } | 
|---|
| 242 | } | 
|---|