Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/AI_HS15/src/orxonox/controllers/CommonController.cc @ 10729

Last change on this file since 10729 was 10729, checked in by gania, 9 years ago

move functions were added, everyone stays in formations

File size: 4.9 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/CommonController.h"
29
30namespace orxonox
31{
32
33    RegisterClass(CommonController);
34    static const float SPEED = 0.6f;
35    static const float ROTATEFACTOR = 0.2f;
36
37    /*static const float SPEED_FREE = 0.8f;
38    static const float ROTATEFACTOR_FREE = 0.8f;*/
39   
40    bool CommonController::setWingman (CommonController* wingman)
41    {
42        return false;
43    }
44    bool CommonController::isLeader ()
45    {
46        return false;
47    }
48    bool CommonController::hasWingman()
49    {
50        return true;
51    }
52
53    CommonController::CommonController(Context* context) : Controller(context)
54    {
55
56        RegisterObject(CommonController);
57    }
58
59
60    CommonController::~CommonController()
61    {
62    }
63    //copy the Roll orientation of given Quaternion.
64    void CommonController::copyOrientation(const Quaternion& orient)
65    {
66        //roll angle difference in radian
67        float diff=orient.getRoll(false).valueRadians()-(this->getControllableEntity()->getOrientation().getRoll(false).valueRadians());
68        while(diff>math::twoPi) diff-=math::twoPi;
69        while(diff<-math::twoPi) diff+=math::twoPi;
70        this->getControllableEntity()->rotateRoll(-diff);
71
72
73
74    }
75    void CommonController::setTargetPosition(const Vector3& target)
76    {
77        this->targetPosition_ = target;
78        this->bHasTargetPosition_ = true;
79    }
80
81    void CommonController::copyTargetOrientation()
82    {
83        if (bHasTargetOrientation_)
84        {   
85            copyOrientation(targetOrientation_);
86        }
87    }
88    void CommonController::setTargetOrientation(const Quaternion& orient)
89    {
90        this->targetOrientation_=orient;
91        this->bHasTargetOrientation_=true;
92    }
93    void CommonController::setTargetOrientation(ControllableEntity* target)
94    {
95        if (target)
96            setTargetOrientation(target->getOrientation());
97    }
98
99
100    void CommonController::moveToPosition(const Vector3& target)
101    {
102        if (!this->getControllableEntity())
103            return;
104       
105        //100 is (so far) the smallest tolerance (empirically found) that can be reached,
106        //with smaller distance spaceships can't reach position and go circles around it instead
107        int tolerance = 100;
108
109        ControllableEntity* entity = this->getControllableEntity();
110        Vector2 coord = get2DViewCoordinates
111            (entity->getPosition(), 
112            entity->getOrientation() * WorldEntity::FRONT, 
113            entity->getOrientation() * WorldEntity::UP, 
114            target);
115
116        float distance = (target - this->getControllableEntity()->getPosition()).length();
117
118        //rotates should be in range [-1,+1], clamp cuts off all that is not
119        float rotateX = clamp(coord.x * 10, -1.0f, 1.0f);
120        float rotateY = clamp(coord.y * 10, -1.0f, 1.0f);
121
122       
123        if (distance > tolerance)
124        {
125            //Yaw and Pitch are enough to start facing the target
126            this->getControllableEntity()->rotateYaw(-2.0f * ROTATEFACTOR * rotateX);
127            this->getControllableEntity()->rotatePitch(2.0f * ROTATEFACTOR * rotateY);
128
129            //300 works, maybe less is better
130            if (distance < 300)
131            {
132                //Change roll when close. When Spaceship faces target, roll doesn't affect it's trajectory
133                //It's important that roll is not changed in the process of changing yaw and pitch
134                //Wingmen won't face same direction as Leaders, but when Leaders start moving
135                //Yaw and Pitch will adapt.
136                if (bHasTargetOrientation_)
137                {
138                    copyTargetOrientation();
139                }
140            }
141            this->getControllableEntity()->moveFrontBack(1.2f*SPEED);
142        }
143        else
144        {     
145            bHasTargetPosition_ = false;
146            bHasTargetOrientation_ = false;
147        }
148
149     
150    }
151    void CommonController::moveToTargetPosition()
152    {
153        this->moveToPosition(this->targetPosition_);
154    }
155 
156
157}
Note: See TracBrowser for help on using the repository browser.