Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy/src/orxonox/objects/worldentities/MovableEntity.cc @ 1993

Last change on this file since 1993 was 1993, checked in by landauf, 16 years ago

added several new classes

File size: 6.3 KB
Line 
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 "MovableEntity.h"
30
31#include "core/CoreIncludes.h"
32#include "core/XMLPort.h"
33#include "core/Executor.h"
34#include "tools/Timer.h"
35
36namespace orxonox
37{
38    static const float MAX_RESYNCHRONIZE_TIME = 3.0f;
39
40    CreateFactory(MovableEntity);
41
42    MovableEntity::MovableEntity()
43    {
44        RegisterObject(MovableEntity);
45
46        this->velocity_ = Vector3::ZERO;
47        this->acceleration_ = Vector3::ZERO;
48        this->rotationAxis_ = Vector3::ZERO;
49        this->rotationRate_ = 0;
50        this->momentum_ = 0;
51
52        this->overwrite_position_ = Vector3::ZERO;
53        this->overwrite_orientation_ = Quaternion::IDENTITY;
54
55        this->registerVariables();
56    }
57
58    MovableEntity::~MovableEntity()
59    {
60    }
61
62    void MovableEntity::XMLPort(Element& xmlelement, XMLPort::Mode mode)
63    {
64        SUPER(MovableEntity, XMLPort, xmlelement, mode);
65
66        XMLPortParamTemplate(MovableEntity, "velocity", setVelocity, getVelocity, xmlelement, mode, const Vector3&);
67        XMLPortParamTemplate(MovableEntity, "rotationaxis", setRotationAxis, getRotationAxis, xmlelement, mode, const Vector3&);
68        XMLPortParamTemplate(MovableEntity, "rotationrate", setRotationRate, getRotationRate, xmlelement, mode, const Degree&);
69    }
70
71    void MovableEntity::tick(float dt)
72    {
73        if (this->isActive())
74        {
75            this->velocity_ += (dt * this->acceleration_);
76            this->node_->translate(dt * this->velocity_);
77
78            this->rotationRate_ += (dt * this->momentum_);
79            this->node_->rotate(this->rotationAxis_, this->rotationRate_  * dt);
80        }
81    }
82
83    void MovableEntity::registerVariables()
84    {
85        REGISTERDATA(this->velocity_.x, network::direction::toclient);
86        REGISTERDATA(this->velocity_.y, network::direction::toclient);
87        REGISTERDATA(this->velocity_.z, network::direction::toclient);
88
89        REGISTERDATA(this->rotationAxis_.x, network::direction::toclient);
90        REGISTERDATA(this->rotationAxis_.y, network::direction::toclient);
91        REGISTERDATA(this->rotationAxis_.z, network::direction::toclient);
92
93        REGISTERDATA(this->rotationRate_, network::direction::toclient);
94
95        REGISTERDATA(this->overwrite_position_,    network::direction::toclient, new network::NetworkCallback<MovableEntity>(this, &MovableEntity::overwritePosition));
96        REGISTERDATA(this->overwrite_orientation_, network::direction::toclient, new network::NetworkCallback<MovableEntity>(this, &MovableEntity::overwriteOrientation));
97    }
98
99    void MovableEntity::overwritePosition()
100    {
101        this->node_->setPosition(this->overwrite_position_);
102    }
103
104    void MovableEntity::overwriteOrientation()
105    {
106        this->node_->setOrientation(this->overwrite_orientation_);
107    }
108
109    void MovableEntity::clientConnected(unsigned int clientID)
110    {
111        new Timer<MovableEntity>(rnd() * MAX_RESYNCHRONIZE_TIME, false, this, createExecutor(createFunctor(&MovableEntity::resynchronize)), true);
112    }
113
114    void MovableEntity::clientDisconnected(unsigned int clientID)
115    {
116    }
117
118    void MovableEntity::resynchronize()
119    {
120        this->overwrite_position_ = this->getPosition();
121        this->overwrite_orientation_ = this->getOrientation();
122    }
123
124    void MovableEntity::setPosition(const Vector3& position)
125    {
126        this->node_->setPosition(position);
127        this->overwrite_position_ = this->node_->getPosition();
128    }
129
130    void MovableEntity::translate(const Vector3& distance, Ogre::Node::TransformSpace relativeTo)
131    {
132        this->node_->translate(distance, relativeTo);
133        this->overwrite_position_ = this->node_->getPosition();
134    }
135
136    void MovableEntity::setOrientation(const Quaternion& orientation)
137    {
138        this->node_->setOrientation(orientation);
139        this->overwrite_orientation_ = this->node_->getOrientation();
140    }
141
142    void MovableEntity::rotate(const Quaternion& rotation, Ogre::Node::TransformSpace relativeTo)
143    {
144        this->node_->rotate(rotation, relativeTo);
145        this->overwrite_orientation_ = this->node_->getOrientation();
146    }
147
148    void MovableEntity::yaw(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
149    {
150        this->node_->yaw(angle, relativeTo);
151        this->overwrite_orientation_ = this->node_->getOrientation();
152    }
153
154    void MovableEntity::pitch(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
155    {
156        this->node_->pitch(angle, relativeTo);
157        this->overwrite_orientation_ = this->node_->getOrientation();
158    }
159
160    void MovableEntity::roll(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
161    {
162        this->node_->roll(angle, relativeTo);
163        this->overwrite_orientation_ = this->node_->getOrientation();
164    }
165
166    void MovableEntity::lookAt(const Vector3& target, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector)
167    {
168        this->node_->lookAt(target, relativeTo, localDirectionVector);
169        this->overwrite_orientation_ = this->node_->getOrientation();
170    }
171
172    void MovableEntity::setDirection(const Vector3& direction, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector)
173    {
174        this->node_->setDirection(direction, relativeTo, localDirectionVector);
175        this->overwrite_orientation_ = this->node_->getOrientation();
176    }
177}
Note: See TracBrowser for help on using the repository browser.