Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/physics/src/orxonox/objects/worldentities/MovableEntity.cc @ 2421

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

Added resynchronisation timer in MovableEntity to ensure the right position on the client if we only synchronise the velocity.

  • Property svn:eol-style set to native
File size: 3.9 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 "OrxonoxStableHeaders.h"
30#include "MovableEntity.h"
31
32#include "core/CoreIncludes.h"
33#include "core/XMLPort.h"
34#include "core/Executor.h"
35#include "tools/Timer.h"
36
37namespace orxonox
38{
39    static const float MAX_RESYNCHRONIZE_TIME = 3.0f;
40    static const float CONTINUOUS_SYNCHRONIZATION_TIME = 10.0f;
41
42    CreateFactory(MovableEntity);
43
44    MovableEntity::MovableEntity(BaseObject* creator) : MobileEntity(creator)
45    {
46        RegisterObject(MovableEntity);
47
48        this->overwrite_position_    = Vector3::ZERO;
49        this->overwrite_orientation_ = Quaternion::IDENTITY;
50
51        // Resynchronise every few seconds because we only work with velocities (no positions)
52        continuousResynchroTimer_ = new Timer<MovableEntity>(CONTINUOUS_SYNCHRONIZATION_TIME + rnd(-1, 1),
53                true, this, createExecutor(createFunctor(&MovableEntity::resynchronize)), false);
54
55        this->registerVariables();
56    }
57
58    MovableEntity::~MovableEntity()
59    {
60        if (this->isInitialized())
61            delete this->continuousResynchroTimer_;
62    }
63
64    void MovableEntity::XMLPort(Element& xmlelement, XMLPort::Mode mode)
65    {
66        SUPER(MovableEntity, XMLPort, xmlelement, mode);
67    }
68
69    void MovableEntity::registerVariables()
70    {
71        REGISTERDATA(this->linearVelocity_,        network::direction::toclient, new network::NetworkCallback<MovableEntity>(this, &MovableEntity::processLinearVelocity));
72        REGISTERDATA(this->angularVelocity_,       network::direction::toclient, new network::NetworkCallback<MovableEntity>(this, &MovableEntity::processAngularVelocity));
73
74        REGISTERDATA(this->overwrite_position_,    network::direction::toclient, new network::NetworkCallback<MovableEntity>(this, &MovableEntity::overwritePosition));
75        REGISTERDATA(this->overwrite_orientation_, network::direction::toclient, new network::NetworkCallback<MovableEntity>(this, &MovableEntity::overwriteOrientation));
76    }
77
78    void MovableEntity::tick(float dt)
79    {
80        MobileEntity::tick(dt);
81
82        if (this->isActive())
83        {
84        }
85    }
86
87    void MovableEntity::overwritePosition()
88    {
89        this->setPosition(this->overwrite_position_);
90    }
91
92    void MovableEntity::overwriteOrientation()
93    {
94        this->setOrientation(this->overwrite_orientation_);
95    }
96
97    void MovableEntity::clientConnected(unsigned int clientID)
98    {
99        new Timer<MovableEntity>(rnd() * MAX_RESYNCHRONIZE_TIME, false, this, createExecutor(createFunctor(&MovableEntity::resynchronize)), true);
100    }
101
102    void MovableEntity::clientDisconnected(unsigned int clientID)
103    {
104    }
105
106    void MovableEntity::resynchronize()
107    {
108        positionChanged(false);
109        orientationChanged(false);
110    }
111
112    void MovableEntity::positionChanged(bool bContinuous)
113    {
114        if (!bContinuous)
115            this->overwrite_position_ = this->getPosition();
116    }
117
118    void MovableEntity::orientationChanged(bool bContinuous)
119    {
120        if (!bContinuous)
121            this->overwrite_orientation_ = this->getOrientation();
122    }
123}
Note: See TracBrowser for help on using the repository browser.