Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/turretFS14/src/modules/objects/Turret.cc @ 10021

Last change on this file since 10021 was 10021, checked in by muemart, 11 years ago

Turret: Move the turret up in the hierarchy, edit the template accordingly

File size: 6.0 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Marian Runo
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "Turret.h"
30#include "core/CoreIncludes.h"
31#include "core/XMLPort.h"
32#include "BulletDynamics/Dynamics/btRigidBody.h"
33
34namespace orxonox
35{
36    RegisterClass(Turret);
37
38
39
40    /**
41     * @brief Constructor
42     */
43    Turret::Turret(Context* context) : Pawn(context)
44    {
45        RegisterObject(Turret);
46        this->startOrientInv_ = Quaternion::IDENTITY;
47        this->maxPitch_ = 0;
48        this->maxYaw_ = 0;
49        this->gotOrient_ = false;
50        this->rotationThrust_ = 50;
51
52        this->localAngularAcceleration_.setValue(0, 0, 0);
53    }
54
55    /**
56     * @brief Destructor
57     */
58    Turret::~Turret()
59    {
60
61    }
62
63
64    void Turret::rotatePitch(const Vector2& value)
65    {
66        if (this->maxPitch_ == 0)
67        {
68            return;
69        }
70        if (this->maxPitch_ >= 180) //no need to check, if the limit too big
71        {
72            this->localAngularAcceleration_.setX(this->localAngularAcceleration_.x() + value.x*0.8f);
73            return;
74        }
75
76        Quaternion drot = startOrientInv_ * this->getOrientation();
77
78        Ogre::Real val = boundBetween(drot.getPitch(false).valueDegrees(), -180, 180);
79        Ogre::Real offset = boundBetween(Degree(value.x).valueDegrees(), -180, 180);
80        Ogre::Real lowerBound = offset - this->maxPitch_;
81        Ogre::Real upperBound = offset + this->maxPitch_;
82        if (lowerBound < -180) //Avoid wrapping around of the boundaries
83        {
84            lowerBound += this->maxPitch_;
85            upperBound += this->maxPitch_;
86            val = boundBetween(val + this->maxPitch_, -180, 180); //val might wrap around here
87        }
88        else if (upperBound >= 180) //Avoid wrapping around of the boundaries (the other side)
89        {
90            lowerBound -= this->maxPitch_;
91            upperBound -= this->maxPitch_;
92            val = boundBetween(val-this->maxPitch_, -180, 180); //val might wrap around here
93        }
94        if ((val >= lowerBound || value.x > 0) && (val <= upperBound || value.x < 0)) 
95        {
96            this->localAngularAcceleration_.setX(this->localAngularAcceleration_.x() + value.x*0.8f);
97        }
98        return;
99    }
100
101    void Turret::rotateYaw(const Vector2& value)
102    {
103        if (this->maxPitch_ == 0)
104        {
105            return;
106        }
107        if (this->maxPitch_ >= 180) //no need to check, if the limit too big
108        {
109            this->localAngularAcceleration_.setY(this->localAngularAcceleration_.y() + value.x*0.8f);
110            return;
111        }
112
113        Quaternion drot = startOrientInv_ * this->getOrientation();
114
115        Ogre::Real val = boundBetween(drot.getYaw(false).valueDegrees(), -180, 180);
116        Ogre::Real offset = boundBetween(Degree(value.x).valueDegrees(), -180, 180);
117        Ogre::Real lowerBound = offset - this->maxPitch_;
118        Ogre::Real upperBound = offset + this->maxPitch_;
119        if (lowerBound < -180) //Avoid wrapping around of the boundaries
120        {
121            lowerBound += this->maxPitch_;
122            upperBound += this->maxPitch_;
123            val = boundBetween(val + this->maxPitch_, -180, 180); //val might wrap around here
124        }
125        else if (upperBound >= 180) //Avoid wrapping around of the boundaries (the other side)
126        {
127            lowerBound -= this->maxPitch_;
128            upperBound -= this->maxPitch_;
129            val = boundBetween(val-this->maxPitch_, -180, 180); //val might wrap around here
130        }
131        if ((val >= lowerBound || value.x > 0) && (val <= upperBound || value.x < 0)) 
132        {
133           this->localAngularAcceleration_.setY(this->localAngularAcceleration_.y() + value.x*0.8f);
134        }
135        return;
136    }
137
138    void Turret::rotateRoll(const Vector2& value)
139    {
140        return; //Standard turrets don't roll
141    }
142
143    void Turret::XMLPort(Element& xmlelement, XMLPort::Mode mode)
144    {
145        XMLPortParam(Turret, "maxPitch", setMaxPitch, getMaxPitch, xmlelement, mode);
146        XMLPortParam(Turret, "maxYaw", setMaxYaw, getMaxYaw, xmlelement, mode);
147        SUPER(Turret, XMLPort, xmlelement, mode);
148    }
149
150    void Turret::tick(float dt)
151    {
152        SUPER(Turret, tick, dt);
153
154        if(!gotOrient_)
155        {
156            startOrientInv_ = this->getOrientation().Inverse();
157            gotOrient_ = true;
158        }
159        Quaternion drot = startOrientInv_ * this->getOrientation();
160        orxout() << "Pitch: " << drot.getPitch(false).valueDegrees() << "\tYaw: " << drot.getYaw(false).valueDegrees() << "\tRoll: " << drot.getRoll(false).valueDegrees() << endl;
161       
162        this->localAngularAcceleration_ *= this->getLocalInertia() * this->rotationThrust_;
163        this->physicalBody_->applyTorque(physicalBody_->getWorldTransform().getBasis() * this->localAngularAcceleration_);
164        this->localAngularAcceleration_.setValue(0, 0, 0);
165    }
166
167
168    Ogre::Real Turret::boundBetween(Ogre::Real val, Ogre::Real lowerBound, Ogre::Real upperBound)
169    {
170        if (lowerBound > upperBound){ std::swap(lowerBound, upperBound); }
171        val -= lowerBound; //adjust to 0
172        Ogre::Real rangeSize = upperBound - lowerBound;
173        if (rangeSize == 0){ return upperBound; } //avoid dividing by 0
174        return val - (rangeSize * std::floor(val / rangeSize)) + lowerBound;
175    }
176
177}
Note: See TracBrowser for help on using the repository browser.