Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 15, 2008, 12:53:05 AM (15 years ago)
Author:
rgrieder
Message:

Merged physics_merge back to presentation branch.

Location:
code/branches/presentation
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/presentation

  • code/branches/presentation/src/orxonox/objects/worldentities/pawns/SpaceShip.cc

    r2371 r2459  
    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"
    3438#include "core/XMLPort.h"
    35 #include "util/Math.h"
    3639
    3740namespace orxonox
    3841{
     42    const float orientationGain = 100;
    3943    CreateFactory(SpaceShip);
    4044
     
    4347        RegisterObject(SpaceShip);
    4448
    45         this->zeroDegree_ = 0;
     49        this->primaryThrust_  = 100;
     50        this->auxilaryThrust_ =  30;
     51        this->rotationThrust_ =  10;
    4652
    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;
     53        this->localLinearAcceleration_.setValue(0, 0, 0);
     54        this->localAngularAcceleration_.setValue(0, 0, 0);
    5755
    5856        this->bInvertYAxis_ = false;
    5957
    6058        this->setDestroyWhenPlayerLeft(true);
     59
     60        // SpaceShip is always a physical object per default
     61        // Be aware of this call: The collision type legality check will not reach derived classes!
     62        this->setCollisionType(WorldEntity::Dynamic);
    6163
    6264        this->setConfigValues();
     
    7274        SUPER(SpaceShip, XMLPort, xmlelement, mode);
    7375
    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);
     76        XMLPortParamVariable(SpaceShip, "primaryThrust",  primaryThrust_,  xmlelement, mode);
     77        XMLPortParamVariable(SpaceShip, "auxilaryThrust", auxilaryThrust_, xmlelement, mode);
     78        XMLPortParamVariable(SpaceShip, "rotationThrust", rotationThrust_, xmlelement, mode);
    8079    }
    8180
    8281    void SpaceShip::registerVariables()
    8382    {
    84         registerVariable(this->maxSpeed_,                variableDirection::toclient);
    85         registerVariable(this->maxSecondarySpeed_,       variableDirection::toclient);
    86         registerVariable(this->maxRotation_,             variableDirection::toclient);
    87         registerVariable(this->translationAcceleration_, variableDirection::toclient);
    88         registerVariable(this->rotationAcceleration_,    variableDirection::toclient);
    89         registerVariable(this->translationDamping_,      variableDirection::toclient);
     83        registerVariable(this->primaryThrust_,  variableDirection::toclient);
     84        registerVariable(this->auxilaryThrust_, variableDirection::toclient);
     85        registerVariable(this->rotationThrust_, variableDirection::toclient);
    9086    }
    9187
     
    9591    }
    9692
     93    bool SpaceShip::isCollisionTypeLegal(WorldEntity::CollisionType type) const
     94    {
     95        if (type != WorldEntity::Dynamic)
     96        {
     97            CCOUT(1) << "Error: Cannot tell a SpaceShip not to be dynamic! Ignoring." << std::endl;
     98            assert(false); // Only in debug mode
     99            return false;
     100        }
     101        else
     102            return true;
     103    }
     104
    97105    void SpaceShip::tick(float dt)
    98106    {
    99         if (this->isLocallyControlled())
    100         {
    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)
    121             {
    122                 if (this->acceleration_[dimension] == 0)
    123                 {
    124                     if (velocity[dimension] > 0)
    125                     {
    126                         velocity[dimension] -= (this->translationDamping_ * dt);
    127                         if (velocity[dimension] < 0)
    128                             velocity[dimension] = 0;
    129                     }
    130                     else if (velocity[dimension] < 0)
    131                     {
    132                         velocity[dimension] += (this->translationDamping_ * dt);
    133                         if (velocity[dimension] > 0)
    134                             velocity[dimension] = 0;
    135                     }
    136                 }
    137             }
    138 
    139             this->setVelocity(velocity);
    140         }
    141 
    142 
    143107        SUPER(SpaceShip, tick, dt);
    144 
    145108
    146109        if (this->isLocallyControlled())
    147110        {
    148             this->yaw(this->yawRotation_ * dt);
    149             if (this->bInvertYAxis_)
    150                 this->pitch(Degree(-this->pitchRotation_ * dt));
     111            this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() * getMass() * this->auxilaryThrust_);
     112            this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() * getMass() * this->auxilaryThrust_);
     113            if (this->localLinearAcceleration_.z() > 0)
     114                this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() * getMass() * this->auxilaryThrust_);
    151115            else
    152                 this->pitch(Degree( this->pitchRotation_ * dt));
    153             this->roll(this->rollRotation_ * dt);
     116                this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() * getMass() * this->primaryThrust_);
     117            this->physicalBody_->applyCentralForce(physicalBody_->getWorldTransform().getBasis() * this->localLinearAcceleration_);
     118            this->localLinearAcceleration_.setValue(0, 0, 0);
    154119
    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_;
     120            this->localAngularAcceleration_ *= this->getLocalInertia() * this->rotationThrust_;
     121            this->physicalBody_->applyTorque(physicalBody_->getWorldTransform().getBasis() * this->localAngularAcceleration_);
     122            this->localAngularAcceleration_.setValue(0, 0, 0);
    162123        }
    163124    }
     
    165126    void SpaceShip::moveFrontBack(const Vector2& value)
    166127    {
    167         this->acceleration_.z = -this->translationAcceleration_ * value.x;
     128        this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() - value.x);
    168129    }
    169130
    170131    void SpaceShip::moveRightLeft(const Vector2& value)
    171132    {
    172         this->acceleration_.x = this->translationAcceleration_ * value.x;
     133        this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() + value.x);
    173134    }
    174135
    175136    void SpaceShip::moveUpDown(const Vector2& value)
    176137    {
    177         this->acceleration_.y = this->translationAcceleration_ * value.x;
     138        this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() + value.x);
    178139    }
    179140
    180141    void SpaceShip::rotateYaw(const Vector2& value)
    181142    {
    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);
     143        this->localAngularAcceleration_.setY(this->localLinearAcceleration_.y() + value.x);
    188144    }
    189145
    190146    void SpaceShip::rotatePitch(const Vector2& value)
    191147    {
    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);
     148        this->localAngularAcceleration_.setX(this->localLinearAcceleration_.x() + value.x);
    198149    }
    199150
    200151    void SpaceShip::rotateRoll(const Vector2& value)
    201152    {
    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);
     153        this->localAngularAcceleration_.setZ(this->localLinearAcceleration_.z() - value.x);
    208154    }
    209155
Note: See TracChangeset for help on using the changeset viewer.