Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Feb 14, 2009, 10:17:35 PM (15 years ago)
Author:
rgrieder
Message:

Merged presentation branch back to trunk.

Location:
code/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/src/orxonox/objects/worldentities/MovableEntity.cc

    r2171 r2662  
    2222 *   Author:
    2323 *      Fabian 'x3n' Landau
     24 *      Reto Grieder
    2425 *   Co-authors:
    2526 *      ...
     
    3334#include "core/XMLPort.h"
    3435#include "core/Executor.h"
    35 #include "tools/Timer.h"
     36#include "core/Core.h"
    3637
    3738namespace orxonox
    3839{
    3940    static const float MAX_RESYNCHRONIZE_TIME = 3.0f;
     41    static const float CONTINUOUS_SYNCHRONIZATION_TIME = 10.0f;
    4042
    4143    CreateFactory(MovableEntity);
    4244
    43     MovableEntity::MovableEntity(BaseObject* creator) : WorldEntity(creator)
     45    MovableEntity::MovableEntity(BaseObject* creator) : MobileEntity(creator)
    4446    {
    4547        RegisterObject(MovableEntity);
    4648
    47         this->velocity_ = Vector3::ZERO;
    48         this->acceleration_ = Vector3::ZERO;
    49         this->rotationAxis_ = Vector3::ZERO;
    50         this->rotationRate_ = 0;
    51         this->momentum_ = 0;
     49        this->overwrite_position_    = Vector3::ZERO;
     50        this->overwrite_orientation_ = Quaternion::IDENTITY;
    5251
    53         this->overwrite_position_ = Vector3::ZERO;
    54         this->overwrite_orientation_ = Quaternion::IDENTITY;
     52        this->continuousResynchroTimer_ = 0;
     53
     54        this->setPriority(priority::low);
    5555
    5656        this->registerVariables();
     
    5959    MovableEntity::~MovableEntity()
    6060    {
     61        if (this->isInitialized())
     62            if (this->continuousResynchroTimer_)
     63                delete this->continuousResynchroTimer_;
    6164    }
    6265
     
    6467    {
    6568        SUPER(MovableEntity, XMLPort, xmlelement, mode);
    66 
    67         XMLPortParamTemplate(MovableEntity, "velocity", setVelocity, getVelocity, xmlelement, mode, const Vector3&);
    68         XMLPortParamTemplate(MovableEntity, "rotationaxis", setRotationAxis, getRotationAxis, xmlelement, mode, const Vector3&);
    69         XMLPortParamTemplate(MovableEntity, "rotationrate", setRotationRate, getRotationRate, xmlelement, mode, const Degree&);
    70     }
    71 
    72     void MovableEntity::tick(float dt)
    73     {
    74         if (this->isActive())
    75         {
    76             this->velocity_ += (dt * this->acceleration_);
    77             this->node_->translate(dt * this->velocity_);
    78 
    79             this->rotationRate_ += (dt * this->momentum_);
    80             this->node_->rotate(this->rotationAxis_, this->rotationRate_  * dt);
    81         }
    8269    }
    8370
    8471    void MovableEntity::registerVariables()
    8572    {
    86         REGISTERDATA(this->velocity_.x, direction::toclient);
    87         REGISTERDATA(this->velocity_.y, direction::toclient);
    88         REGISTERDATA(this->velocity_.z, direction::toclient);
     73        registerVariable(this->linearVelocity_,        variableDirection::toclient, new NetworkCallback<MovableEntity>(this, &MovableEntity::processLinearVelocity));
     74        registerVariable(this->angularVelocity_,       variableDirection::toclient, new NetworkCallback<MovableEntity>(this, &MovableEntity::processAngularVelocity));
    8975
    90         REGISTERDATA(this->rotationAxis_.x, direction::toclient);
    91         REGISTERDATA(this->rotationAxis_.y, direction::toclient);
    92         REGISTERDATA(this->rotationAxis_.z, direction::toclient);
    93 
    94         REGISTERDATA(this->rotationRate_, direction::toclient);
    95 
    96         REGISTERDATA(this->overwrite_position_,    direction::toclient, new NetworkCallback<MovableEntity>(this, &MovableEntity::overwritePosition));
    97         REGISTERDATA(this->overwrite_orientation_, direction::toclient, new NetworkCallback<MovableEntity>(this, &MovableEntity::overwriteOrientation));
    98     }
    99 
    100     void MovableEntity::overwritePosition()
    101     {
    102         this->node_->setPosition(this->overwrite_position_);
    103     }
    104 
    105     void MovableEntity::overwriteOrientation()
    106     {
    107         this->node_->setOrientation(this->overwrite_orientation_);
     76        registerVariable(this->overwrite_position_,    variableDirection::toclient, new NetworkCallback<MovableEntity>(this, &MovableEntity::overwritePosition));
     77        registerVariable(this->overwrite_orientation_, variableDirection::toclient, new NetworkCallback<MovableEntity>(this, &MovableEntity::overwriteOrientation));
    10878    }
    10979
    11080    void MovableEntity::clientConnected(unsigned int clientID)
    11181    {
    112         new Timer<MovableEntity>(rnd() * MAX_RESYNCHRONIZE_TIME, false, this, createExecutor(createFunctor(&MovableEntity::resynchronize)), true);
     82        this->resynchronizeTimer_.setTimer(rnd() * MAX_RESYNCHRONIZE_TIME, false, this, createExecutor(createFunctor(&MovableEntity::resynchronize)));
    11383    }
    11484
     
    11989    void MovableEntity::resynchronize()
    12090    {
     91        if (Core::isMaster() && !this->continuousResynchroTimer_)
     92        {
     93            // Resynchronise every few seconds because we only work with velocities (no positions)
     94            continuousResynchroTimer_ = new Timer<MovableEntity>(CONTINUOUS_SYNCHRONIZATION_TIME + rnd(-1, 1),
     95                true, this, createExecutor(createFunctor(&MovableEntity::resynchronize)), false);
     96        }
     97
    12198        this->overwrite_position_ = this->getPosition();
    12299        this->overwrite_orientation_ = this->getOrientation();
    123100    }
    124 
    125     void MovableEntity::setPosition(const Vector3& position)
    126     {
    127         this->node_->setPosition(position);
    128         this->overwrite_position_ = this->node_->getPosition();
    129     }
    130 
    131     void MovableEntity::translate(const Vector3& distance, Ogre::Node::TransformSpace relativeTo)
    132     {
    133         this->node_->translate(distance, relativeTo);
    134         this->overwrite_position_ = this->node_->getPosition();
    135     }
    136 
    137     void MovableEntity::setOrientation(const Quaternion& orientation)
    138     {
    139         this->node_->setOrientation(orientation);
    140         this->overwrite_orientation_ = this->node_->getOrientation();
    141     }
    142 
    143     void MovableEntity::rotate(const Quaternion& rotation, Ogre::Node::TransformSpace relativeTo)
    144     {
    145         this->node_->rotate(rotation, relativeTo);
    146         this->overwrite_orientation_ = this->node_->getOrientation();
    147     }
    148 
    149     void MovableEntity::yaw(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
    150     {
    151         this->node_->yaw(angle, relativeTo);
    152         this->overwrite_orientation_ = this->node_->getOrientation();
    153     }
    154 
    155     void MovableEntity::pitch(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
    156     {
    157         this->node_->pitch(angle, relativeTo);
    158         this->overwrite_orientation_ = this->node_->getOrientation();
    159     }
    160 
    161     void MovableEntity::roll(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
    162     {
    163         this->node_->roll(angle, relativeTo);
    164         this->overwrite_orientation_ = this->node_->getOrientation();
    165     }
    166 
    167     void MovableEntity::lookAt(const Vector3& target, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector)
    168     {
    169         this->node_->lookAt(target, relativeTo, localDirectionVector);
    170         this->overwrite_orientation_ = this->node_->getOrientation();
    171     }
    172 
    173     void MovableEntity::setDirection(const Vector3& direction, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector)
    174     {
    175         this->node_->setDirection(direction, relativeTo, localDirectionVector);
    176         this->overwrite_orientation_ = this->node_->getOrientation();
    177     }
    178101}
Note: See TracChangeset for help on using the changeset viewer.