Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ai/src/orxonox/worldentities/Drone.cc @ 6847

Last change on this file since 6847 was 6847, checked in by gasserlu, 14 years ago

drone follows Owner

File size: 6.7 KB
Line 
1
2
3
4
5
6
7/*
8 *   ORXONOX - the hottest 3D action shooter ever to exist
9 *                    > www.orxonox.net <
10 *
11 *
12 *   License notice:
13 *
14 *   This program is free software; you can redistribute it and/or
15 *   modify it under the terms of the GNU General Public License
16 *   as published by the Free Software Foundation; either version 2
17 *   of the License, or (at your option) any later version.
18 *
19 *   This program is distributed in the hope that it will be useful,
20 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
21 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 *   GNU General Public License for more details.
23 *
24 *   You should have received a copy of the GNU General Public License
25 *   along with this program; if not, write to the Free Software
26 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
27 *
28 *   Author:
29 *      Oli Scheuss
30 *   Co-authors:
31 *      ...
32 */
33
34
35#include "Drone.h"
36
37#include "core/XMLPort.h"
38#include "BulletDynamics/Dynamics/btRigidBody.h"
39
40namespace orxonox
41{
42    // put your code in here:
43    // create the factory for the drone
44    CreateFactory(Drone);
45    /**
46    @brief
47        Constructor. Registers the object and initializes some default values.
48    */
49    Drone::Drone(BaseObject* creator) : Pawn(creator)
50    {
51        // put your code in here:
52        // - register the drone class to the core
53        RegisterObject(Drone);
54
55        this->myController_ = 0;
56       
57        this->localLinearAcceleration_.setValue(0, 0, 0);
58        this->localAngularAcceleration_.setValue(0, 0, 0);
59        this->primaryThrust_  = 100;
60        this->auxilaryThrust_ = 100;
61        this->rotationThrust_ = 10;
62       
63        this->setCollisionType(WorldEntity::Dynamic);
64       
65        myController_ = new DroneController(static_cast<BaseObject*>(this)); //!< Creates a new controller and passes our this pointer to it as creator.
66        myController_->setDrone(this);
67
68        this->setController(myController_);
69    }
70
71    /**
72    @brief
73        Destructor. Destroys controller, if present.
74    */
75    Drone::~Drone()
76    {
77        if( this->isInitialized() && this->myController_ )
78            delete this->myController_;
79    }
80
81    /**
82    @brief
83        Method for creating a Drone through XML.
84    */
85    void Drone::XMLPort(Element& xmlelement, XMLPort::Mode mode)
86    {
87        // this calls the XMLPort function of the parent class
88        SUPER(Drone, XMLPort, xmlelement, mode);
89
90        // put your code in here:
91        // make sure you add the variables primaryThrust_, auxilaryThrust_ and rotationThrust_ to xmlport
92        // make sure that the set- and get-functions exist.
93        // variables can be added by the following command
94        // XMLPortParam(Classname, "xml-attribute-name (i.e. variablename)", setFunction, getFunction, xmlelement, mode)
95        XMLPortParam(Drone, "primaryThrust_", setPrimaryThrust, getPrimaryThrust, xmlelement, mode);
96        XMLPortParam(Drone, "auxilaryThrust_", setAuxilaryThrust, getAuxilaryThrust, xmlelement, mode);
97        XMLPortParam(Drone, "rotationThrust_", setRotationThrust, getRotationThrust, xmlelement, mode);
98
99
100    }
101
102
103    /**
104    @brief
105        Defines which actions the Drone has to take in each tick.
106    @param dt
107        The length of the tick.
108    */
109    void Drone::tick(float dt)
110    {
111        SUPER(Drone, tick, dt);
112       
113        //if (this->hasLocalController())
114        //{
115            this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() * getMass() * this->auxilaryThrust_);
116            this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() * getMass() * this->auxilaryThrust_);
117            if (this->localLinearAcceleration_.z() > 0)
118              this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() * getMass() * this->auxilaryThrust_);
119            else
120              this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() * getMass() * this->primaryThrust_);
121            this->physicalBody_->applyCentralForce(physicalBody_->getWorldTransform().getBasis() * this->localLinearAcceleration_);
122            this->localLinearAcceleration_.setValue(0, 0, 0);
123       
124            this->localAngularAcceleration_ *= this->getLocalInertia() * this->rotationThrust_;
125            this->physicalBody_->applyTorque(physicalBody_->getWorldTransform().getBasis() * this->localAngularAcceleration_);
126            this->localAngularAcceleration_.setValue(0, 0, 0);
127        //}
128    }
129   
130    /**
131    @brief
132        Moves the Drone in the negative z-direction (Front/Back) by an amount specified by the first component of the input 2-dim vector.
133    @param value
134        The vector determining the amount of the movement.
135    */
136    void Drone::moveFrontBack(const Vector2& value)
137    {
138        this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() - value.x);
139    }
140
141    /**
142    @brief
143        Moves the Drone in the x-direction (Right/Left) by an amount specified by the first component of the input 2-dim vector.
144    @param value
145        The vector determining the amount of the movement.
146    */
147    void Drone::moveRightLeft(const Vector2& value)
148    {
149        this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() + value.x);
150    }
151
152    /**
153    @brief
154        Moves the Drone in the y-direction (Up/Down) by an amount specified by the first component of the input 2-dim vector.
155    @param value
156        The vector determining the amount of the movement.
157    */
158    void Drone::moveUpDown(const Vector2& value)
159    {
160        this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() + value.x);
161    }
162
163    /**
164    @brief
165        Rotates the Drone around the y-axis by the amount specified by the first component of the input 2-dim vector.
166    @param value
167        The vector determining the amount of the angular movement.
168    */
169    void Drone::rotateYaw(const Vector2& value)
170    {
171        this->localAngularAcceleration_.setY(this->localAngularAcceleration_.y() - value.x);
172    }
173
174    /**
175    @brief
176        Rotates the Drone around the x-axis by the amount specified by the first component of the input 2-dim vector.
177    @param value
178        The vector determining the amount of the angular movement.
179    */
180    void Drone::rotatePitch(const Vector2& value)
181    {
182        this->localAngularAcceleration_.setX(this->localAngularAcceleration_.x() + value.x);
183    }
184
185    /**
186    @brief
187        Rotates the Drone around the z-axis by the amount specified by the first component of the input 2-dim vector.
188    @param value
189        The vector determining the amount of the angular movement.
190    */
191    void Drone::rotateRoll(const Vector2& value)
192    {
193        this->localAngularAcceleration_.setZ(this->localAngularAcceleration_.z() + value.x);
194    }
195   
196}
Note: See TracBrowser for help on using the repository browser.