Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jul 3, 2011, 5:42:19 PM (13 years ago)
Author:
dafrick
Message:

Cleaning up in SpaceShip and Engine. Fixed several bugs.
Kicked localLinearAcceleration, primaryThrust and auxiliaryThrust out of the SpaceShip, since it wasn't used anymore.
Moved the trail lights back a bit.
Added some documentation to SpaceShip and Engine.
SpeedPickup is working again, will need some further tuning.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/trunk/src/orxonox/items/Engine.cc

    r8706 r8727  
    2929#include "Engine.h"
    3030
    31 #include "util/Math.h"
    3231#include "core/CoreIncludes.h"
    3332#include "core/ConfigValueIncludes.h"
     33#include "core/Template.h"
    3434#include "core/XMLPort.h"
     35#include "util/Math.h"
     36
    3537#include "Scene.h"
    3638#include "worldentities/pawns/SpaceShip.h"
    37 #include "core/Template.h"
    3839
    3940namespace orxonox
     
    4142    CreateFactory(Engine);
    4243
     44    /**
     45    @brief
     46        Constructor. Registers and initializes the object.
     47    */
    4348    Engine::Engine(BaseObject* creator) : Item(creator)
    4449    {
     
    4752        this->ship_ = 0;
    4853        this->shipID_ = OBJECTID_UNKNOWN;
    49         this->relativePosition_ = Vector3(0,0,0);
    50 
    51         this->boostFactor_ = 1.5;
    52         this->speedFactor_ = 1.0;
    53 
    54         this->maxSpeedFront_ = 0.0;
    55         this->maxSpeedBack_ = 0.0;
    56         this->maxSpeedLeftRight_ = 0.0;
    57         this->maxSpeedUpDown_ = 0.0;
    58 
    59         this->accelerationFront_ = 0.0;
    60         this->accelerationBrake_ = 0.0;
    61         this->accelerationBack_ = 0.0;
    62         this->accelerationLeftRight_ = 0.0;
    63         this->accelerationUpDown_ = 0.0;
    64 
    65         this->speedAdd_ = 0.0;
    66         this->speedMultiply_ = 1.0;
     54        this->relativePosition_ = Vector3::ZERO;
     55
     56        this->boostFactor_ = 1.5f;
     57
     58        this->maxSpeedFront_ = 0.0f;
     59        this->maxSpeedBack_ = 0.0f;
     60        this->maxSpeedLeftRight_ = 0.0f;
     61        this->maxSpeedUpDown_ = 0.0f;
     62
     63        this->accelerationFront_ = 0.0f;
     64        this->accelerationBrake_ = 0.0f;
     65        this->accelerationBack_ = 0.0f;
     66        this->accelerationLeftRight_ = 0.0f;
     67        this->accelerationUpDown_ = 0.0f;
     68
     69        this->speedAdd_ = 0.0f;
     70        this->speedMultiply_ = 1.0f;
    6771
    6872        this->setConfigValues();
     
    7074    }
    7175
     76    /**
     77    @brief
     78        Destructor. Destroys the object and removes it from the SpaceShip.
     79    */
    7280    Engine::~Engine()
    7381    {
    7482        if (this->isInitialized())
    7583        {
     84            // Remove the engine from the ShapeShip.
    7685            if (this->ship_ && this->ship_->hasEngine(this))
    7786                this->ship_->removeEngine(this);
     
    108117        registerVariable(this->shipID_, VariableDirection::ToClient, new NetworkCallback<Engine>(this, &Engine::networkcallback_shipID));
    109118
    110         registerVariable(this->speedFactor_, VariableDirection::ToClient);
    111119        registerVariable(this->boostFactor_, VariableDirection::ToClient);
    112120
     
    138146    }
    139147
    140     void Engine::tick(float dt)
    141     {
    142         if (!this->ship_)
    143         {
    144             if (this->shipID_)
     148    /**
     149    @brief
     150        Run the engine for a given time interval.
     151        Is called each tick by SpaceShip.
     152    @param dt
     153        The time since last tick.
     154    */
     155    void Engine::run(float dt)
     156    {
     157        if (this->ship_ == NULL)
     158        {
     159            if (this->shipID_ != 0)
    145160            {
    146161                this->networkcallback_shipID();
    147162
    148                 if (!this->ship_)
     163                if (this->ship_ == NULL)
    149164                    return;
    150165            }
     
    156171            return;
    157172
    158         SUPER(Engine, tick, dt);
    159 
    160         Vector3 direction = this->getDirection();
    161         float directionLength = direction.length();
    162         if (directionLength > 1.0f)
    163             direction /= directionLength; // normalize
    164        
     173        // Get the desired steering direction and amount, clipped to length 1 at maximum.
     174        Vector3 steering = (this->getSteering().length() > 1.0f ? this->getSteering().normalisedCopy() : this->getSteering());
     175
     176        // Get the ships velocity.
    165177        Vector3 velocity = this->ship_->getLocalVelocity();
    166178        Vector3 acceleration = Vector3::ZERO;
    167179
    168         float factor = 1.0f / this->speedFactor_;
    169         velocity *= factor;
    170 
    171         if (direction.z < 0)
     180        // If there is forward steering action.
     181        if (steering.z < 0)
    172182        {
    173183            if (this->maxSpeedFront_ != 0)
    174184            {
    175                 float boostfactor = (this->ship_->getBoost() ? this->boostFactor_ : 1.0f);
    176                 acceleration.z = direction.z * this->accelerationFront_ * boostfactor * clamp((this->maxSpeedFront_ - -velocity.z/boostfactor) / this->maxSpeedFront_, 0.0f, 1.0f);
     185                float boostfactor = (this->ship_->isBoosting() ? this->boostFactor_ : 1.0f); // Boost factor is 1.0 if not boosting.
     186                // Boosting can lead to velocities larger the maximal forward velocity.
     187                acceleration.z = steering.z * this->accelerationFront_ * boostfactor * clamp((this->maxSpeedFront_ - -velocity.z/boostfactor) / this->maxSpeedFront_, 0.0f, 1.0f);
    177188            }
    178189        }
    179         else if (direction.z > 0)
    180         {
     190        // If there is backward steering action.
     191        else if (steering.z > 0)
     192        {
     193            // Either breaking
    181194            if (velocity.z < 0)
    182                 acceleration.z = direction.z * this->accelerationBrake_;
     195                acceleration.z = steering.z * this->accelerationBrake_;
     196            // or backward flight.
    183197            else if (this->maxSpeedBack_ != 0)
    184                 acceleration.z = direction.z * this->accelerationBack_ * clamp((this->maxSpeedBack_ - velocity.z) / this->maxSpeedBack_, 0.0f, 1.0f);
    185         }
    186 
     198                acceleration.z = steering.z * this->accelerationBack_ * clamp((this->maxSpeedBack_ - velocity.z) / this->maxSpeedBack_, 0.0f, 1.0f);
     199        }
     200        // If there is left-right steering action.
    187201        if (this->maxSpeedLeftRight_ != 0)
    188202        {
    189             if (direction.x < 0)
    190                 acceleration.x = direction.x * this->accelerationLeftRight_ * clamp((this->maxSpeedLeftRight_ - -velocity.x) / this->maxSpeedLeftRight_, 0.0f, 1.0f);
    191             else if (direction.x > 0)
    192                 acceleration.x = direction.x * this->accelerationLeftRight_ * clamp((this->maxSpeedLeftRight_ - velocity.x) / this->maxSpeedLeftRight_, 0.0f, 1.0f);
    193         }
    194 
     203            if (steering.x < 0)
     204                acceleration.x = steering.x * this->accelerationLeftRight_ * clamp((this->maxSpeedLeftRight_ - -velocity.x) / this->maxSpeedLeftRight_, 0.0f, 1.0f);
     205            else if (steering.x > 0)
     206                acceleration.x = steering.x * this->accelerationLeftRight_ * clamp((this->maxSpeedLeftRight_ - velocity.x) / this->maxSpeedLeftRight_, 0.0f, 1.0f);
     207        }
     208        // If there is up-down steering action.
    195209        if (this->maxSpeedUpDown_ != 0)
    196210        {
    197             if (direction.y < 0)
    198                 acceleration.y = direction.y * this->accelerationUpDown_ * clamp((this->maxSpeedUpDown_ - -velocity.y) / this->maxSpeedUpDown_, 0.0f, 1.0f);
    199             else if (direction.y > 0)
    200                 acceleration.y = direction.y * this->accelerationUpDown_ * clamp((this->maxSpeedUpDown_ - velocity.y) / this->maxSpeedUpDown_, 0.0f, 1.0f);
     211            if (steering.y < 0)
     212                acceleration.y = steering.y * this->accelerationUpDown_ * clamp((this->maxSpeedUpDown_ - -velocity.y) / this->maxSpeedUpDown_, 0.0f, 1.0f);
     213            else if (steering.y > 0)
     214                acceleration.y = steering.y * this->accelerationUpDown_ * clamp((this->maxSpeedUpDown_ - velocity.y) / this->maxSpeedUpDown_, 0.0f, 1.0f);
    201215        }
    202216
    203217        // NOTE: Bullet always uses global coordinates.
    204218        this->ship_->addAcceleration(this->ship_->getOrientation() * (acceleration*this->getSpeedMultiply()+Vector3(0,0,-this->getSpeedAdd())), this->ship_->getOrientation() * this->relativePosition_);
    205 
    206         // Hack to reset a temporary variable "direction"
    207         this->ship_->oneEngineTickDone();
    208         if(!this->ship_->hasEngineTicksRemaining())
    209         {
    210             this->ship_->setSteeringDirection(Vector3::ZERO);
    211             this->ship_->resetEngineTicks();
    212         }
    213     }
    214 
    215     void Engine::changedActivity()
    216     {
    217         SUPER(Engine, changedActivity);
    218     }
    219 
     219    }
     220
     221    /**
     222    @brief
     223        Adds the Engine to the input SpaceShip.
     224    @param ship
     225        A pointer to the SpaceShip to which the engine is added.
     226    */
    220227    void Engine::addToSpaceShip(SpaceShip* ship)
    221228    {
     
    230237    }
    231238
    232     const Vector3& Engine::getDirection() const
     239    /**
     240    @brief
     241        Get the direction and magnitude of the steering imposed upon the Engine.
     242    @return
     243        Returns the direction and magnitude of the steering imposed upon the Engine.
     244        Is the zero vector if the Engine doesn't belong to a ship.
     245    */
     246    const Vector3& Engine::getSteering() const
    233247    {
    234248        if (this->ship_)
     
    238252    }
    239253
    240     PickupCarrier* Engine::getCarrierParent(void) const
    241     {
    242         return this->ship_;
    243     }
    244 
    245     const Vector3& Engine::getCarrierPosition(void) const
    246     {
    247         return this->ship_->getWorldPosition();
    248     }
    249 
     254    /**
     255    @brief
     256        Load the engine template.
     257        Causes all parameters specified by the template to be applied to the Engine.
     258    */
    250259    void Engine::loadEngineTemplate()
    251260    {
     
    260269        }
    261270    }
     271
    262272}
Note: See TracChangeset for help on using the changeset viewer.