Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation/src/orxonox/objects/worldentities/MovableEntity.cc @ 2470

Last change on this file since 2470 was 2470, checked in by rgrieder, 15 years ago

Fixed bug that caused MovableEntities to jump around on the client.

  • Property svn:eol-style set to native
File size: 3.6 KB
RevLine 
[2072]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
[2459]24 *      Reto Grieder
[2072]25 *   Co-authors:
26 *      ...
27 *
28 */
29
30#include "OrxonoxStableHeaders.h"
31#include "MovableEntity.h"
32
33#include "core/CoreIncludes.h"
34#include "core/XMLPort.h"
35#include "core/Executor.h"
[2470]36#include "core/Core.h"
[2072]37#include "tools/Timer.h"
38
39namespace orxonox
40{
41    static const float MAX_RESYNCHRONIZE_TIME = 3.0f;
[2459]42    static const float CONTINUOUS_SYNCHRONIZATION_TIME = 10.0f;
[2072]43
44    CreateFactory(MovableEntity);
45
[2459]46    MovableEntity::MovableEntity(BaseObject* creator) : MobileEntity(creator)
[2072]47    {
48        RegisterObject(MovableEntity);
49
[2459]50        this->overwrite_position_    = Vector3::ZERO;
[2072]51        this->overwrite_orientation_ = Quaternion::IDENTITY;
52
[2470]53        this->continuousResynchroTimer_ = 0;
[2459]54
[2470]55        this->setPriority(priority::low);
56
[2072]57        this->registerVariables();
58    }
59
60    MovableEntity::~MovableEntity()
61    {
[2459]62        if (this->isInitialized())
[2470]63            if (this->continuousResynchroTimer_)
64                delete this->continuousResynchroTimer_;
[2072]65    }
66
67    void MovableEntity::XMLPort(Element& xmlelement, XMLPort::Mode mode)
68    {
69        SUPER(MovableEntity, XMLPort, xmlelement, mode);
70    }
71
[2459]72    void MovableEntity::registerVariables()
[2072]73    {
[2459]74        registerVariable(this->linearVelocity_,        variableDirection::toclient, new NetworkCallback<MovableEntity>(this, &MovableEntity::processLinearVelocity));
75        registerVariable(this->angularVelocity_,       variableDirection::toclient, new NetworkCallback<MovableEntity>(this, &MovableEntity::processAngularVelocity));
[2072]76
[2371]77        registerVariable(this->overwrite_position_,    variableDirection::toclient, new NetworkCallback<MovableEntity>(this, &MovableEntity::overwritePosition));
78        registerVariable(this->overwrite_orientation_, variableDirection::toclient, new NetworkCallback<MovableEntity>(this, &MovableEntity::overwriteOrientation));
[2072]79    }
80
81    void MovableEntity::clientConnected(unsigned int clientID)
82    {
83        new Timer<MovableEntity>(rnd() * MAX_RESYNCHRONIZE_TIME, false, this, createExecutor(createFunctor(&MovableEntity::resynchronize)), true);
84    }
85
86    void MovableEntity::clientDisconnected(unsigned int clientID)
87    {
88    }
89
90    void MovableEntity::resynchronize()
91    {
[2470]92        if (Core::isMaster() && !this->continuousResynchroTimer_)
93        {
94            // Resynchronise every few seconds because we only work with velocities (no positions)
95            continuousResynchroTimer_ = new Timer<MovableEntity>(CONTINUOUS_SYNCHRONIZATION_TIME + rnd(-1, 1),
96                true, this, createExecutor(createFunctor(&MovableEntity::resynchronize)), false);
97        }
98
[2072]99        this->overwrite_position_ = this->getPosition();
100        this->overwrite_orientation_ = this->getOrientation();
101    }
102}
Note: See TracBrowser for help on using the repository browser.