Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

added MovableEntity with network optimization for constant velocity and rotation

added new Timer feature to destroy a Timer right after it called the function

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