Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Feb 14, 2009, 10:17:35 PM (16 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/pawns/SpaceShip.cc

    r2171 r2662  
    3030#include "SpaceShip.h"
    3131
     32#include "BulletDynamics/Dynamics/btRigidBody.h"
     33
     34#include "util/Math.h"
     35#include "util/Exception.h"
    3236#include "core/CoreIncludes.h"
    3337#include "core/ConfigValueIncludes.h"
     38#include "core/Template.h"
    3439#include "core/XMLPort.h"
    35 #include "util/Math.h"
     40#include "objects/items/Engine.h"
    3641
    3742namespace orxonox
    3843{
     44    const float orientationGain = 100;
    3945    CreateFactory(SpaceShip);
    4046
     
    4349        RegisterObject(SpaceShip);
    4450
    45         this->zeroDegree_ = 0;
    46 
    47         this->maxSpeed_ = 0;
    48         this->maxSecondarySpeed_ = 0;
    49         this->maxRotation_ = 0;
    50         this->translationAcceleration_ = 0;
    51         this->rotationAcceleration_ = 0;
    52         this->translationDamping_ = 0;
    53 
    54         this->yawRotation_ = 0;
    55         this->pitchRotation_ = 0;
    56         this->rollRotation_ = 0;
     51        this->primaryThrust_  = 100;
     52        this->auxilaryThrust_ =  30;
     53        this->rotationThrust_ =  10;
     54
     55        this->localLinearAcceleration_.setValue(0, 0, 0);
     56        this->localAngularAcceleration_.setValue(0, 0, 0);
     57        this->bBoost_ = false;
     58        this->bPermanentBoost_ = false;
     59        this->steering_ = Vector3::ZERO;
     60        this->engine_ = 0;
     61
    5762
    5863        this->bInvertYAxis_ = false;
    5964
    6065        this->setDestroyWhenPlayerLeft(true);
     66
     67        // SpaceShip is always a physical object per default
     68        // Be aware of this call: The collision type legality check will not reach derived classes!
     69        this->setCollisionType(WorldEntity::Dynamic);
     70        // Get notification about collisions
     71        this->enableCollisionCallback();
    6172
    6273        this->setConfigValues();
     
    6677    SpaceShip::~SpaceShip()
    6778    {
     79        if (this->isInitialized() && this->engine_)
     80            delete this->engine_;
    6881    }
    6982
     
    7285        SUPER(SpaceShip, XMLPort, xmlelement, mode);
    7386
    74         XMLPortParam(SpaceShip, "maxspeed",          setMaxSpeed,          getMaxSpeed,          xmlelement, mode);
    75         XMLPortParam(SpaceShip, "maxsecondaryspeed", setMaxSecondarySpeed, getMaxSecondarySpeed, xmlelement, mode);
    76         XMLPortParam(SpaceShip, "maxrotation",       setMaxRotation,       getMaxRotation,       xmlelement, mode);
    77         XMLPortParam(SpaceShip, "transacc",          setTransAcc,          getTransAcc,          xmlelement, mode);
    78         XMLPortParam(SpaceShip, "rotacc",            setRotAcc,            getRotAcc,            xmlelement, mode);
    79         XMLPortParam(SpaceShip, "transdamp",         setTransDamp,         getTransDamp,         xmlelement, mode);
     87        XMLPortParam(SpaceShip, "engine",            setEngineTemplate,    getEngineTemplate,    xmlelement, mode);
     88        XMLPortParamVariable(SpaceShip, "primaryThrust",  primaryThrust_,  xmlelement, mode);
     89        XMLPortParamVariable(SpaceShip, "auxilaryThrust", auxilaryThrust_, xmlelement, mode);
     90        XMLPortParamVariable(SpaceShip, "rotationThrust", rotationThrust_, xmlelement, mode);
    8091    }
    8192
    8293    void SpaceShip::registerVariables()
    8394    {
    84         REGISTERDATA(this->maxSpeed_,                direction::toclient);
    85         REGISTERDATA(this->maxSecondarySpeed_,       direction::toclient);
    86         REGISTERDATA(this->maxRotation_,             direction::toclient);
    87         REGISTERDATA(this->translationAcceleration_, direction::toclient);
    88         REGISTERDATA(this->rotationAcceleration_,    direction::toclient);
    89         REGISTERDATA(this->translationDamping_,      direction::toclient);
     95        registerVariable(this->primaryThrust_,  variableDirection::toclient);
     96        registerVariable(this->auxilaryThrust_, variableDirection::toclient);
     97        registerVariable(this->rotationThrust_, variableDirection::toclient);
    9098    }
    9199
     
    95103    }
    96104
     105    bool SpaceShip::isCollisionTypeLegal(WorldEntity::CollisionType type) const
     106    {
     107        if (type != WorldEntity::Dynamic)
     108        {
     109            CCOUT(1) << "Error: Cannot tell a SpaceShip not to be dynamic! Ignoring." << std::endl;
     110            assert(false); // Only in debug mode
     111            return false;
     112        }
     113        else
     114            return true;
     115    }
     116
    97117    void SpaceShip::tick(float dt)
    98118    {
    99         if (this->isLocallyControlled())
     119        SUPER(SpaceShip, tick, dt);
     120
     121        if (this->hasLocalController())
    100122        {
    101             // #####################################
    102             // ############# STEERING ##############
    103             // #####################################
    104 
    105             Vector3 velocity = this->getVelocity();
    106             if (velocity.x > this->maxSecondarySpeed_)
    107                 velocity.x = this->maxSecondarySpeed_;
    108             if (velocity.x < -this->maxSecondarySpeed_)
    109                 velocity.x = -this->maxSecondarySpeed_;
    110             if (velocity.y > this->maxSecondarySpeed_)
    111                 velocity.y = this->maxSecondarySpeed_;
    112             if (velocity.y < -this->maxSecondarySpeed_)
    113                 velocity.y = -this->maxSecondarySpeed_;
    114             if (velocity.z > this->maxSecondarySpeed_)
    115                 velocity.z = this->maxSecondarySpeed_;
    116             if (velocity.z < -this->maxSpeed_)
    117                 velocity.z = -this->maxSpeed_;
    118 
    119             // normalize velocity and acceleration
    120             for (size_t dimension = 0; dimension < 3; ++dimension)
     123/*
     124            this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() * getMass() * this->auxilaryThrust_);
     125            this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() * getMass() * this->auxilaryThrust_);
     126            if (this->localLinearAcceleration_.z() > 0)
     127                this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() * getMass() * this->auxilaryThrust_);
     128            else
     129                this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() * getMass() * this->primaryThrust_);
     130            this->physicalBody_->applyCentralForce(physicalBody_->getWorldTransform().getBasis() * this->localLinearAcceleration_);
     131            this->localLinearAcceleration_.setValue(0, 0, 0);
     132*/
     133            if (!this->isInMouseLook())
    121134            {
    122                 if (this->acceleration_[dimension] == 0)
     135                this->localAngularAcceleration_ *= this->getLocalInertia() * this->rotationThrust_;
     136                this->physicalBody_->applyTorque(physicalBody_->getWorldTransform().getBasis() * this->localAngularAcceleration_);
     137                this->localAngularAcceleration_.setValue(0, 0, 0);
     138            }
     139        }
     140    }
     141
     142    void SpaceShip::moveFrontBack(const Vector2& value)
     143    {
     144        this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() - value.x);
     145        this->steering_.z = -value.x;
     146    }
     147
     148    void SpaceShip::moveRightLeft(const Vector2& value)
     149    {
     150        this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() + value.x);
     151        this->steering_.x = value.x;
     152    }
     153
     154    void SpaceShip::moveUpDown(const Vector2& value)
     155    {
     156        this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() + value.x);
     157        this->steering_.y = value.x;
     158    }
     159
     160    void SpaceShip::rotateYaw(const Vector2& value)
     161    {
     162        this->localAngularAcceleration_.setY(this->localAngularAcceleration_.y() - value.x);
     163
     164        Pawn::rotateYaw(value);
     165    }
     166
     167    void SpaceShip::rotatePitch(const Vector2& value)
     168    {
     169        this->localAngularAcceleration_.setX(this->localAngularAcceleration_.x() + value.x);
     170
     171        Pawn::rotatePitch(value);
     172    }
     173
     174    void SpaceShip::rotateRoll(const Vector2& value)
     175    {
     176        this->localAngularAcceleration_.setZ(this->localAngularAcceleration_.z() + value.x);
     177
     178        Pawn::rotateRoll(value);
     179    }
     180
     181    void SpaceShip::fire()
     182    {
     183    }
     184
     185    void SpaceShip::boost()
     186    {
     187        this->bBoost_ = true;
     188    }
     189
     190    void SpaceShip::loadEngineTemplate()
     191    {
     192        if (this->enginetemplate_ != "")
     193        {
     194            Template* temp = Template::getTemplate(this->enginetemplate_);
     195
     196            if (temp)
     197            {
     198                Identifier* identifier = temp->getBaseclassIdentifier();
     199
     200                if (identifier)
    123201                {
    124                     if (velocity[dimension] > 0)
     202                    BaseObject* object = identifier->fabricate(this);
     203                    this->engine_ = dynamic_cast<Engine*>(object);
     204
     205                    if (this->engine_)
    125206                    {
    126                         velocity[dimension] -= (this->translationDamping_ * dt);
    127                         if (velocity[dimension] < 0)
    128                             velocity[dimension] = 0;
     207                        this->engine_->addTemplate(temp);
     208                        this->engine_->addToSpaceShip(this);
    129209                    }
    130                     else if (velocity[dimension] < 0)
     210                    else
    131211                    {
    132                         velocity[dimension] += (this->translationDamping_ * dt);
    133                         if (velocity[dimension] > 0)
    134                             velocity[dimension] = 0;
     212                        delete object;
    135213                    }
    136214                }
    137215            }
    138 
    139             this->setVelocity(velocity);
    140216        }
    141 
    142 
    143         SUPER(SpaceShip, tick, dt);
    144 
    145 
    146         if (this->isLocallyControlled())
    147         {
    148             this->yaw(this->yawRotation_ * dt);
    149             if (this->bInvertYAxis_)
    150                 this->pitch(Degree(-this->pitchRotation_ * dt));
    151             else
    152                 this->pitch(Degree( this->pitchRotation_ * dt));
    153             this->roll(this->rollRotation_ * dt);
    154 
    155             this->acceleration_.x = 0;
    156             this->acceleration_.y = 0;
    157             this->acceleration_.z = 0;
    158 
    159             this->yawRotation_   = this->zeroDegree_;
    160             this->pitchRotation_ = this->zeroDegree_;
    161             this->rollRotation_  = this->zeroDegree_;
    162         }
    163     }
    164 
    165     void SpaceShip::moveFrontBack(const Vector2& value)
    166     {
    167         this->acceleration_.z = -this->translationAcceleration_ * value.x;
    168     }
    169 
    170     void SpaceShip::moveRightLeft(const Vector2& value)
    171     {
    172         this->acceleration_.x = this->translationAcceleration_ * value.x;
    173     }
    174 
    175     void SpaceShip::moveUpDown(const Vector2& value)
    176     {
    177         this->acceleration_.y = this->translationAcceleration_ * value.x;
    178     }
    179 
    180     void SpaceShip::rotateYaw(const Vector2& value)
    181     {
    182         Degree temp = value.x * value.x * sgn(value.x) * this->rotationAcceleration_;
    183         if (temp > this->maxRotation_)
    184             temp = this->maxRotation_;
    185         if (temp < -this->maxRotation_)
    186             temp = -this->maxRotation_;
    187         this->yawRotation_ = Degree(temp);
    188     }
    189 
    190     void SpaceShip::rotatePitch(const Vector2& value)
    191     {
    192         Degree temp = value.x * value.x * sgn(value.x) * this->rotationAcceleration_;
    193         if (temp > this->maxRotation_)
    194             temp = this->maxRotation_;
    195         if (temp < -this->maxRotation_)
    196             temp = -this->maxRotation_;
    197         this->pitchRotation_ = Degree(temp);
    198     }
    199 
    200     void SpaceShip::rotateRoll(const Vector2& value)
    201     {
    202         Degree temp = value.x * value.x * sgn(value.x) * this->rotationAcceleration_;
    203         if (temp > this->maxRotation_)
    204             temp = this->maxRotation_;
    205         if (temp < -this->maxRotation_)
    206             temp = -this->maxRotation_;
    207         this->rollRotation_ = Degree(temp);
    208     }
    209 
    210     void SpaceShip::fire()
    211     {
     217    }
     218
     219    void SpaceShip::setEngine(Engine* engine)
     220    {
     221        this->engine_ = engine;
     222        if (engine && engine->getShip() != this)
     223            engine->addToSpaceShip(this);
    212224    }
    213225}
Note: See TracChangeset for help on using the changeset viewer.