Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 14, 2008, 4:16:52 PM (15 years ago)
Author:
rgrieder
Message:

Finally merged physics stuff. Target is physics_merge because I'll have to do some testing first.

Location:
code/branches/physics_merge
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/physics_merge

  • code/branches/physics_merge/src/orxonox/objects/worldentities/ControllableEntity.cc

    r2415 r2442  
    2323 *      Fabian 'x3n' Landau
    2424 *   Co-authors:
    25  *      ...
     25 *      Reto Grieder
    2626 *
    2727 */
     
    4444    CreateFactory(ControllableEntity);
    4545
    46     ControllableEntity::ControllableEntity(BaseObject* creator) : WorldEntity(creator)
     46    ControllableEntity::ControllableEntity(BaseObject* creator) : MobileEntity(creator)
    4747    {
    4848        RegisterObject(ControllableEntity);
     
    5757        this->bDestroyWhenPlayerLeft_ = false;
    5858
    59         this->velocity_ = Vector3::ZERO;
    60         this->acceleration_ = Vector3::ZERO;
    61 
    62         this->server_position_ = Vector3::ZERO;
    63         this->client_position_ = Vector3::ZERO;
    64         this->server_velocity_ = Vector3::ZERO;
    65         this->client_velocity_ = Vector3::ZERO;
    66         this->server_orientation_ = Quaternion::IDENTITY;
    67         this->client_orientation_ = Quaternion::IDENTITY;
    68        
     59        this->server_position_        = Vector3::ZERO;
     60        this->client_position_        = Vector3::ZERO;
     61        this->server_linear_velocity_  = Vector3::ZERO;
     62        this->client_linear_velocity_ = Vector3::ZERO;
     63        this->server_orientation_      = Quaternion::IDENTITY;
     64        this->client_orientation_      = Quaternion::IDENTITY;
     65        this->server_angular_velocity_ = Vector3::ZERO;
     66        this->client_angular_velocity_ = Vector3::ZERO;
     67
     68
    6969        this->setPriority( priority::very_high );
    70 
    7170        this->registerVariables();
    7271    }
     
    229228    void ControllableEntity::tick(float dt)
    230229    {
     230        MobileEntity::tick(dt);
     231
    231232        if (this->isActive())
    232233        {
    233             this->velocity_ += (dt * this->acceleration_);
    234             this->node_->translate(dt * this->velocity_, Ogre::Node::TS_LOCAL);
    235 
    236             if (Core::isMaster())
     234            // Check whether Bullet doesn't do the physics for us
     235            if (!this->isDynamic())
    237236            {
    238                 this->server_velocity_ = this->velocity_;
    239                 this->server_position_ = this->node_->getPosition();
     237                if (Core::isMaster())
     238                {
     239                    this->server_position_ = this->getPosition();
     240                    this->server_orientation_ = this->getOrientation();
     241                    this->server_linear_velocity_ = this->getVelocity();
     242                    this->server_angular_velocity_ = this->getAngularVelocity();
     243                }
     244                else if (this->bControlled_)
     245                {
     246                    this->client_position_ = this->getPosition();
     247                    this->client_orientation_ = this->getOrientation();
     248                    this->client_linear_velocity_ = this->getVelocity();
     249                    this->client_angular_velocity_ = this->getAngularVelocity();
     250                }
    240251            }
    241             else if (this->bControlled_)
    242             {
    243 //                COUT(2) << "setting client position" << endl;
    244                 this->client_velocity_ = this->velocity_;
    245                 this->client_position_ = this->node_->getPosition();
    246             }
    247252        }
    248253    }
     
    251256    {
    252257        registerVariable(this->cameraPositionTemplate_, variableDirection::toclient);
    253  
    254         registerVariable(this->client_overwrite_,   variableDirection::toserver);
    255          
    256         registerVariable(this->server_position_,    variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerPosition));
    257         registerVariable(this->server_velocity_,    variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerVelocity));
    258         registerVariable(this->server_orientation_, variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerOrientation));
    259         registerVariable(this->server_overwrite_,   variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processOverwrite));
    260  
    261         registerVariable(this->client_position_,    variableDirection::toserver, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientPosition));
    262         registerVariable(this->client_velocity_,    variableDirection::toserver, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientVelocity));
    263         registerVariable(this->client_orientation_, variableDirection::toserver, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientOrientation));
    264  
    265  
    266         registerVariable(this->playerID_, variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::networkcallback_changedplayerID));
     258
     259        registerVariable(this->server_position_,         variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerPosition));
     260        registerVariable(this->server_linear_velocity_,  variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerLinearVelocity));
     261        registerVariable(this->server_orientation_,      variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerOrientation));
     262        registerVariable(this->server_angular_velocity_, variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerAngularVelocity));
     263
     264        registerVariable(this->server_overwrite_,        variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processOverwrite));
     265        registerVariable(this->client_overwrite_,        variableDirection::toserver);
     266
     267        registerVariable(this->client_position_,         variableDirection::toserver, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientPosition));
     268        registerVariable(this->client_linear_velocity_,  variableDirection::toserver, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientLinearVelocity));
     269        registerVariable(this->client_orientation_,      variableDirection::toserver, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientOrientation));
     270        registerVariable(this->client_angular_velocity_, variableDirection::toserver, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientAngularVelocity));
     271
     272        registerVariable(this->playerID_,                variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::networkcallback_changedplayerID));
    267273    }
    268274
     
    270276    {
    271277        if (!this->bControlled_)
    272             this->node_->setPosition(this->server_position_);
    273     }
    274 
    275     void ControllableEntity::processServerVelocity()
     278            this->setPosition(this->server_position_);
     279    }
     280
     281    void ControllableEntity::processServerLinearVelocity()
    276282    {
    277283        if (!this->bControlled_)
    278             this->velocity_ = this->server_velocity_;
     284            this->setVelocity(this->server_linear_velocity_);
    279285    }
    280286
     
    282288    {
    283289        if (!this->bControlled_)
    284             this->node_->setOrientation(this->server_orientation_);
     290            this->setOrientation(this->server_orientation_);
     291    }
     292
     293    void ControllableEntity::processServerAngularVelocity()
     294    {
     295        if (!this->bControlled_)
     296            this->setAngularVelocity(this->server_angular_velocity_);
    285297    }
    286298
     
    290302        {
    291303            this->setPosition(this->server_position_);
    292             this->setVelocity(this->server_velocity_);
    293304            this->setOrientation(this->server_orientation_);
     305            this->setVelocity(this->server_linear_velocity_);
     306            this->setAngularVelocity(this->server_angular_velocity_);
    294307
    295308            this->client_overwrite_ = this->server_overwrite_;
     
    301314        if (this->server_overwrite_ == this->client_overwrite_)
    302315        {
    303 //            COUT(2) << "callback: setting client position" << endl;
    304             this->node_->setPosition(this->client_position_);
    305             this->server_position_ = this->client_position_;
    306         }
    307 //        else
    308 //          COUT(2) << "callback: not setting client position" << endl;
    309     }
    310 
    311     void ControllableEntity::processClientVelocity()
     316            this->setPosition(this->client_position_);
     317            // The call above increments the overwrite. This is not desired to reduce traffic
     318            --this->server_overwrite_;
     319        }
     320    }
     321
     322    void ControllableEntity::processClientLinearVelocity()
    312323    {
    313324        if (this->server_overwrite_ == this->client_overwrite_)
    314325        {
    315             this->velocity_ = this->client_velocity_;
    316             this->server_velocity_ = this->client_velocity_;
     326            this->setVelocity(this->client_linear_velocity_);
     327            // The call above increments the overwrite. This is not desired to reduce traffic
     328            --this->server_overwrite_;
    317329        }
    318330    }
     
    322334        if (this->server_overwrite_ == this->client_overwrite_)
    323335        {
    324             this->node_->setOrientation(this->client_orientation_);
    325             this->server_orientation_ = this->client_orientation_;
    326         }
    327     }
    328 
     336            this->setOrientation(this->client_orientation_);
     337            // The call above increments the overwrite. This is not desired to reduce traffic
     338            --this->server_overwrite_;
     339        }
     340    }
     341
     342    void ControllableEntity::processClientAngularVelocity()
     343    {
     344        if (this->server_overwrite_ == this->client_overwrite_)
     345        {
     346            this->setAngularVelocity(this->client_angular_velocity_);
     347            // The call above increments the overwrite. This is not desired to reduce traffic
     348            --this->server_overwrite_;
     349        }
     350    }
    329351
    330352    void ControllableEntity::setPosition(const Vector3& position)
     
    332354        if (Core::isMaster())
    333355        {
    334             this->node_->setPosition(position);
    335             this->server_position_ = position;
     356            MobileEntity::setPosition(position);
     357            this->server_position_ = this->getPosition();
    336358            ++this->server_overwrite_;
    337359        }
    338360        else if (this->bControlled_)
    339361        {
    340             this->node_->setPosition(position);
    341             this->client_position_ = position;
     362            MobileEntity::setPosition(position);
     363            this->client_position_ = this->getPosition();
     364        }
     365    }
     366
     367    void ControllableEntity::setOrientation(const Quaternion& orientation)
     368    {
     369        if (Core::isMaster())
     370        {
     371            MobileEntity::setOrientation(orientation);
     372            this->server_orientation_ = this->getOrientation();
     373            ++this->server_overwrite_;
     374        }
     375        else if (this->bControlled_)
     376        {
     377            MobileEntity::setOrientation(orientation);
     378            this->client_orientation_ = this->getOrientation();
    342379        }
    343380    }
     
    347384        if (Core::isMaster())
    348385        {
    349             this->velocity_ = velocity;
    350             this->server_velocity_ = velocity;
     386            MobileEntity::setVelocity(velocity);
     387            this->server_linear_velocity_ = this->getVelocity();
    351388            ++this->server_overwrite_;
    352389        }
    353390        else if (this->bControlled_)
    354391        {
    355             this->velocity_ = velocity;
    356             this->client_velocity_ = velocity;
    357         }
    358     }
    359 
    360     void ControllableEntity::translate(const Vector3& distance, Ogre::Node::TransformSpace relativeTo)
     392            MobileEntity::setVelocity(velocity);
     393            this->client_linear_velocity_ = this->getVelocity();
     394        }
     395    }
     396
     397    void ControllableEntity::setAngularVelocity(const Vector3& velocity)
    361398    {
    362399        if (Core::isMaster())
    363400        {
    364             this->node_->translate(distance, relativeTo);
    365             this->server_position_ = this->node_->getPosition();
     401            MobileEntity::setAngularVelocity(velocity);
     402            this->server_angular_velocity_ = this->getAngularVelocity();
    366403            ++this->server_overwrite_;
    367404        }
    368405        else if (this->bControlled_)
    369406        {
    370             this->node_->translate(distance, relativeTo);
    371             this->client_position_ = this->node_->getPosition();
    372         }
    373     }
    374 
    375     void ControllableEntity::setOrientation(const Quaternion& orientation)
    376     {
     407            MobileEntity::setAngularVelocity(velocity);
     408            this->client_angular_velocity_ = this->getAngularVelocity();
     409        }
     410    }
     411
     412    void ControllableEntity::setWorldTransform(const btTransform& worldTrans)
     413    {
     414        MobileEntity::setWorldTransform(worldTrans);
    377415        if (Core::isMaster())
    378416        {
    379             this->node_->setOrientation(orientation);
    380             this->server_orientation_ = orientation;
    381             ++this->server_overwrite_;
     417            this->server_position_ = this->getPosition();
     418            this->server_orientation_ = this->getOrientation();
     419            this->server_linear_velocity_ = this->getVelocity();
     420            this->server_angular_velocity_ = this->getAngularVelocity();
    382421        }
    383422        else if (this->bControlled_)
    384423        {
    385             this->node_->setOrientation(orientation);
    386             this->client_orientation_ = orientation;
    387         }
    388     }
    389 
    390     void ControllableEntity::rotate(const Quaternion& rotation, Ogre::Node::TransformSpace relativeTo)
    391     {
    392         if (Core::isMaster())
    393         {
    394             this->node_->rotate(rotation, relativeTo);
    395             this->server_orientation_ = this->node_->getOrientation();
    396             ++this->server_overwrite_;
    397         }
    398         else if (this->bControlled_)
    399         {
    400             this->node_->rotate(rotation, relativeTo);
    401             this->client_orientation_ = this->node_->getOrientation();
    402         }
    403     }
    404 
    405     void ControllableEntity::yaw(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
    406     {
    407         if (Core::isMaster())
    408         {
    409             this->node_->yaw(angle, relativeTo);
    410             this->server_orientation_ = this->node_->getOrientation();
    411             ++this->server_overwrite_;
    412         }
    413         else if (this->bControlled_)
    414         {
    415             this->node_->yaw(angle, relativeTo);
    416             this->client_orientation_ = this->node_->getOrientation();
    417         }
    418     }
    419 
    420     void ControllableEntity::pitch(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
    421     {
    422         if (Core::isMaster())
    423         {
    424             this->node_->pitch(angle, relativeTo);
    425             this->server_orientation_ = this->node_->getOrientation();
    426             ++this->server_overwrite_;
    427         }
    428         else if (this->bControlled_)
    429         {
    430             this->node_->pitch(angle, relativeTo);
    431             this->client_orientation_ = this->node_->getOrientation();
    432         }
    433     }
    434 
    435     void ControllableEntity::roll(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
    436     {
    437         if (Core::isMaster())
    438         {
    439             this->node_->roll(angle, relativeTo);
    440             this->server_orientation_ = this->node_->getOrientation();
    441             ++this->server_overwrite_;
    442         }
    443         else if (this->bControlled_)
    444         {
    445             this->node_->roll(angle, relativeTo);
    446             this->client_orientation_ = this->node_->getOrientation();
    447         }
    448     }
    449 
    450     void ControllableEntity::lookAt(const Vector3& target, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector)
    451     {
    452         if (Core::isMaster())
    453         {
    454             this->node_->lookAt(target, relativeTo, localDirectionVector);
    455             this->server_orientation_ = this->node_->getOrientation();
    456             ++this->server_overwrite_;
    457         }
    458         else if (this->bControlled_)
    459         {
    460             this->node_->lookAt(target, relativeTo, localDirectionVector);
    461             this->client_orientation_ = this->node_->getOrientation();
    462         }
    463     }
    464 
    465     void ControllableEntity::setDirection(const Vector3& direction, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector)
    466     {
    467         if (Core::isMaster())
    468         {
    469             this->node_->setDirection(direction, relativeTo, localDirectionVector);
    470             this->server_orientation_ = this->node_->getOrientation();
    471             ++this->server_overwrite_;
    472         }
    473         else if (this->bControlled_)
    474         {
    475             this->node_->setDirection(direction, relativeTo, localDirectionVector);
    476             this->client_orientation_ = this->node_->getOrientation();
     424            this->client_position_ = this->getPosition();
     425            this->client_orientation_ = this->getOrientation();
     426            this->client_linear_velocity_ = this->getVelocity();
     427            this->client_angular_velocity_ = this->getAngularVelocity();
    477428        }
    478429    }
Note: See TracChangeset for help on using the changeset viewer.