Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/campaignHS15/src/orxonox/controllers/FlyingController.cc @ 10879

Last change on this file since 10879 was 10879, checked in by gania, 8 years ago

finished copyOrientation function. Now ships move smoothly

File size: 7.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 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      Dominik Solenicki
26 *
27 */
28#include "controllers/FlyingController.h"
29#include "core/XMLPort.h"
30#include "worldentities/pawns/SpaceShip.h"
31#include "util/Math.h"
32#include <OgreMatrix3.h>
33namespace orxonox
34{   
35    RegisterClass (FlyingController);
36   
37    FlyingController::FlyingController( Context* context ): CommonController( context )
38    {
39        this->rotationProgress_ = 0;
40        this->spread_ = 200;
41        this->tolerance_ = 80;
42        RegisterObject( FlyingController );
43    }
44    FlyingController::~FlyingController() 
45    {
46
47    }
48
49    void FlyingController::XMLPort( Element& xmlelement, XMLPort::Mode mode )
50    {
51        XMLPortParam( FlyingController, "spread", setSpread, getSpread,  xmlelement, mode );
52        XMLPortParam( FlyingController, "formationMode", setFormationModeXML, getFormationModeXML,  xmlelement, mode );
53        SUPER( FlyingController, XMLPort, xmlelement, mode );
54    }
55   
56    void FlyingController::setFormationModeXML( std::string val )
57    {
58        const std::string valUpper = getUppercase( val );
59        FormationMode::Value value;
60       
61        if ( valUpper == "WALL" )
62            value = FormationMode::WALL;
63        else if ( valUpper == "FINGER4" )
64            value = FormationMode::FINGER4;
65        else if ( valUpper == "DIAMOND" )
66            value = FormationMode::DIAMOND;
67        else
68            ThrowException( ParseError, std::string( "Attempting to set an unknown FormationMode: '" )+ val + "'." );
69        this->setFormationMode( value );
70    }
71    std::string FlyingController::getFormationModeXML() 
72    {
73        switch ( this->formationMode_ )
74        {
75            case FormationMode::WALL:
76            { return "WALL"; break; }
77            case FormationMode::FINGER4:
78            { return "FINGER4"; break; }
79            case FormationMode::DIAMOND:
80            { return "DIAMOND"; break; }
81            default:
82                return "DIAMOND"; break;
83        }
84    }
85
86   
87
88    void FlyingController::stopMoving()
89    {
90        this->bHasTargetPosition_ = false;
91    }
92
93    void FlyingController::moveToPosition( const Vector3& target, float dt )
94    {
95        ControllableEntity* entity = this->getControllableEntity();
96        Vector2 coord = get2DViewCoordinates
97            ( entity->getPosition() , 
98            entity->getOrientation()  * WorldEntity::FRONT, 
99            entity->getOrientation()  * WorldEntity::UP, 
100            target );
101
102        float distance = ( target - this->getControllableEntity() ->getPosition() ).length();
103        float rotateX = -clamp( coord.x * 10, -1.0f, 1.0f );
104        float rotateY = clamp( coord.y * 10, -1.0f, 1.0f );
105
106        if ( distance > this->tolerance_ )
107        {
108            this->getControllableEntity() ->rotateYaw( ROTATEFACTOR * rotateX * dt );
109            this->getControllableEntity() ->rotatePitch( ROTATEFACTOR * rotateY * dt );
110
111         
112            if (distance > this->tolerance_*1.5f || (rotateX > -0.01 && rotateX < 0.01 && rotateY > -0.01 && rotateY < 0.01))
113                this->getControllableEntity() ->moveFrontBack( SPEED * dt );
114         
115            // if ( bHasTargetOrientation_ && (rotateX > -0.005 && rotateX < 0.005 && rotateY > -0.005 && rotateY < 0.005) )
116            // {
117                copyTargetOrientation( dt );
118            // }
119           
120        }
121        else
122        {     
123            bHasTargetPosition_ = false;
124            bHasTargetOrientation_ = false;
125        }
126    }
127    void FlyingController::moveToTargetPosition(float dt)
128    {
129        this->moveToPosition (this->targetPosition_, dt);
130    }
131    void FlyingController::copyOrientation( const Quaternion& orient, float dt )
132    {
133        //inspired by
134        //http://www.ogre3d.org/tikiwiki/tiki-index.php?page=Quaternion+and+Rotation+Primer&structure=Tutorials#Q._How_can_I_make_my_objects_rotate_smoothly_You_mentioned_slerp_etc_
135        //how can I make my objects rotate smoothly?
136       
137        Quaternion myOrient = this->getControllableEntity()->getOrientation();
138        this->rotationProgress_ += dt/50.0f;
139
140        if (this->rotationProgress_ > 1)
141        {
142            this->rotationProgress_ = 0;
143        }
144        else
145        {
146            Quaternion delta = Quaternion::Slerp(this->rotationProgress_, myOrient, orient, true);
147     
148            Matrix3 orientMatrix, myMatrix;
149            delta.ToRotationMatrix(orientMatrix);
150            myOrient.ToRotationMatrix (myMatrix);
151
152            Radian yRad, pRad, rRad, yMy, pMy, rMy;
153            orientMatrix.ToEulerAnglesYXZ(yRad, pRad, rRad);
154            myMatrix.ToEulerAnglesYXZ (yMy, pMy, rMy);
155            orxout (internal_error) << "dt = " << dt << endl;
156            this->getControllableEntity()->rotateRoll (50.0f*dt*(rRad.valueRadians() - rMy.valueRadians()));
157            //this->getControllableEntity()->setOrientation(delta);
158        }
159        //this shit works. How?
160       
161    }
162    //change log: increased precision, increased rotation speed
163    void FlyingController::copyTargetOrientation( float dt )
164    {
165        if ( bHasTargetOrientation_ )
166        {   
167            copyOrientation( targetOrientation_, dt );
168        }
169    }
170   
171    void FlyingController::setTargetPosition( const Vector3& target )
172    {
173        this->targetPosition_ = target;
174        this->bHasTargetPosition_ = true;
175    }
176
177    void FlyingController::setTargetOrientation( const Quaternion& orient )
178    {
179        this->targetOrientation_=orient;
180        this->bHasTargetOrientation_=true;
181    }
182
183    void FlyingController::setTargetOrientation( ControllableEntity* target )
184    {
185        if ( target )
186            setTargetOrientation( target->getOrientation() );
187    }
188    void FlyingController::boostControl()
189    {
190        SpaceShip* ship = orxonox_cast<SpaceShip*>(this->getControllableEntity());
191        if(ship == NULL) return;
192        if(ship->getBoostPower()*1.5f > ship->getInitialBoostPower() ) //upper limit ->boost
193        {
194
195            this->getControllableEntity()->boost(true);
196        }
197        else if(ship->getBoostPower()*4.0f < ship->getInitialBoostPower()) //lower limit ->do not boost
198        {
199           this->getControllableEntity()->boost(false);
200        }
201    }
202   
203}
Note: See TracBrowser for help on using the repository browser.