Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
May 6, 2014, 11:07:07 AM (11 years ago)
Author:
muemart
Message:

Move everything back to the Turret class, set the correct team, and (re)arm the turret. Also, hide it from the radar.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/turretFS14/src/modules/objects/Turret.cc

    r10042 r10044  
    2121 *
    2222 *   Author:
    23  *      Marian Runo
     23 *      Marian Runo, Martin Mueller
    2424 *   Co-authors:
    2525 *      ...
     
    3030#include "core/CoreIncludes.h"
    3131#include "core/XMLPort.h"
    32 #include "BulletDynamics/Dynamics/btRigidBody.h"
    3332
    3433namespace orxonox
     
    4544        RegisterObject(Turret);
    4645        this->rotationThrust_ = 50;
    47 
    48         this->localAngularAcceleration_.setValue(0, 0, 0);
     46        this->startDir_ = Vector3::ZERO;
     47        this->localZ_ = Vector3::UNIT_Z;
     48        this->localY_ = Vector3::UNIT_Y;
     49        this->localX_ = Vector3::UNIT_X;
     50        this->attackRadius_ = 200;
     51        this->maxPitch_ = 90;
     52        this->maxYaw_ = 90;
     53        this->once_ = false;
     54        this->rotation_ = Quaternion::IDENTITY;
     55
     56        this->setRadarVisibility(false);
    4957    }
    5058
     
    5765    }
    5866
     67
     68    bool Turret::isInRange(const Vector3 &position)
     69    {
     70        //Check distance
     71        Vector3 distance = position - this->getWorldPosition();
     72        if(distance.squaredLength() > (this->attackRadius_ * this->attackRadius_))
     73        {
     74            return false;
     75        }
     76
     77        //Check pitch
     78        Vector3 dir = getTransformedVector(distance, this->localX_, this->localY_, this->localZ_);
     79        Vector3 dirProjected = dir;
     80        dirProjected.x = 0;
     81        Vector3 startDirProjected = this->startDir_;
     82        startDirProjected.x = 0;
     83        Ogre::Real angle = startDirProjected.angleBetween(dirProjected).valueDegrees();
     84        if(angle > this->maxPitch_)
     85        {
     86            return false;
     87        }
     88
     89        //Check yaw
     90        dirProjected = dir;
     91        dirProjected.y = 0;
     92        startDirProjected = this->startDir_;
     93        startDirProjected.y = 0;
     94        angle = startDirProjected.angleBetween(dirProjected).valueDegrees();
     95        if(angle > this->maxYaw_)
     96        {
     97            return false;
     98        }
     99        return true;
     100    }
     101
     102    void Turret::aimAtPosition(const Vector3& position)
     103    {
     104        Vector3 currDir = this->getWorldOrientation() * WorldEntity::FRONT;
     105        Vector3 targetDir = position - this->getWorldPosition();
     106
     107        this->rotation_ = currDir.getRotationTo(targetDir);
     108
     109    }
    59110
    60111    void Turret::rotatePitch(const Vector2& value)
     
    74125        }
    75126        */
    76         this->localAngularAcceleration_.setX(this->localAngularAcceleration_.x() + value.x*0.8f);
    77127    }
    78128
     
    93143        }
    94144        */
    95         this->localAngularAcceleration_.setY(this->localAngularAcceleration_.y() + value.x*0.8f);
    96145    }
    97146
    98147    void Turret::rotateRoll(const Vector2& value)
    99148    {
    100         this->localAngularAcceleration_.setZ(this->localAngularAcceleration_.z() + value.x*0.8f);
    101149    }
    102150
     
    104152    {
    105153        SUPER(Turret, XMLPort, xmlelement, mode);
     154       
     155        XMLPortParamVariable(Turret, "rotationThrust", rotationThrust_, xmlelement, mode);
     156        XMLPortParam(Turret, "attackRadius", setAttackRadius, getAttackRadius, xmlelement, mode);
     157        XMLPortParam(Turret, "maxYaw", setMaxYaw, getMaxYaw, xmlelement, mode);
     158        XMLPortParam(Turret, "maxPitch", setMaxPitch, getMaxPitch, xmlelement, mode);
    106159    }
    107160
     
    110163        SUPER(Turret, tick, dt);
    111164
    112         this->localAngularAcceleration_ *= this->getLocalInertia() * this->rotationThrust_;
    113         this->localAngularAcceleration_ = physicalBody_->getWorldTransform().getBasis() * this->localAngularAcceleration_;
    114 
    115         //physics don't work when attached :(
    116         //this->physicalBody_->applyTorque(physicalBody_->getWorldTransform().getBasis() * this->localAngularAcceleration_);
    117 
    118         pitch(Degree(localAngularAcceleration_.x()*dt/1000), WorldEntity::World);
    119         yaw(Degree(localAngularAcceleration_.y()*dt/1000), WorldEntity::World);
    120         roll(Degree(localAngularAcceleration_.z()*dt/1000), WorldEntity::World);
    121 
    122         this->localAngularAcceleration_.setValue(0, 0, 0);
     165
     166        if(!this->once_)
     167        {
     168
     169            Quaternion startOrient = this->getOrientation();
     170            this->localXStart_ = startOrient * this->localX_;
     171            this->localXStart_.normalise();
     172            this->localX_ = this->localXStart_;
     173            this->localYStart_ = startOrient * this->localY_;
     174            this->localYStart_.normalise();
     175            this->localY_ = this->localYStart_;
     176            this->localZStart_ = startOrient * this->localZ_;
     177            this->localZStart_.normalise();
     178            this->localZ_ = this->localZStart_;
     179
     180            //startDir should always be (0,0,-1)
     181            this->startDir_ = getTransformedVector(startOrient * WorldEntity::FRONT, this->localX_, this->localY_, this->localZ_);
     182
     183            this->once_ = true;
     184
     185        }
     186
     187        //Adjust local axes to parent's rotation
     188        WorldEntity* parent = this->getParent();
     189        if(parent)
     190        {
     191            Quaternion parentrot = parent->getWorldOrientation();
     192            this->localX_ = parentrot * this->localXStart_;
     193            this->localY_ = parentrot * this->localYStart_;
     194            this->localZ_ = parentrot * this->localZStart_;
     195        }
     196
     197        //rotate
     198        if(this->rotation_ != Quaternion::IDENTITY)
     199        {
     200            //Don't make the rotation instantaneous
     201            Quaternion drot = Quaternion::Slerp(dt*this->rotationThrust_/20.f, Quaternion::IDENTITY, this->rotation_);
     202            this->rotate(drot, WorldEntity::World);
     203            this->rotation_ = Quaternion::IDENTITY;
     204        }
     205
    123206    }
    124207}
Note: See TracChangeset for help on using the changeset viewer.