Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/tutorial2/src/orxonox/worldentities/AutonomousDrone.cc @ 7444

Last change on this file since 7444 was 7444, checked in by dafrick, 14 years ago

Adding Drone (Which is now called AutonomousDrone) and its controller.

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 *      Oli Scheuss
24 *   Co-authors:
25 *      Damian 'Mozork' Frick
26 *
27 */
28
29#include "AutonomousDrone.h"
30
31#include "core/CoreIncludes.h"
32#include "BulletDynamics/Dynamics/btRigidBody.h"
33
34namespace orxonox
35{
36    //TODO: Put your code in here:
37    // Create the factory for the drone.
38    //TODO: Remove.
39    CreateFactory(AutonomousDrone);
40
41    /**
42    @brief
43        Constructor. Registers the object and initializes some default values.
44    @param creator
45        The creator of this object.
46    */
47    AutonomousDrone::AutonomousDrone(BaseObject* creator) : ControllableEntity(creator)
48    {
49        //TODO: Put your code in here:
50        // Register the drone class to the core.
51        //TODO: Remove.
52        RegisterObject(AutonomousDrone);
53
54        this->myController_ = NULL;
55
56        this->localLinearAcceleration_.setValue(0, 0, 0);
57        this->localAngularAcceleration_.setValue(0, 0, 0);
58        this->primaryThrust_  = 100;
59        this->auxiliaryThrust_ = 100;
60        this->rotationThrust_ = 10;
61
62        this->setCollisionType(WorldEntity::Dynamic);
63
64        this->myController_ = new AutonomousDroneController(this); // Creates a new controller and passes our this pointer to it as creator.
65    }
66
67    /**
68    @brief
69        Destructor. Destroys controller, if present.
70    */
71    AutonomousDrone::~AutonomousDrone()
72    {
73        // Deletes the controller if the object was initialized and the pointer to the controller is not NULL.
74        if( this->isInitialized() && this->myController_ != NULL )
75            delete this->myController_;
76    }
77
78    /**
79    @brief
80        Method for creating a AutonomousDrone through XML.
81    */
82    void AutonomousDrone::XMLPort(Element& xmlelement, XMLPort::Mode mode)
83    {
84        // This calls the XMLPort function of the parent class
85        SUPER(AutonomousDrone, XMLPort, xmlelement, mode);
86
87        XMLPortParam(AutonomousDrone, "primaryThrust", setPrimaryThrust, getPrimaryThrust, xmlelement, mode);
88        //TODO: Put your code in here:
89        // Make sure you add the variables auxiliaryThrust_ and rotationThrust_ to XMLPort.
90        // Make sure that the set- and get-functions exist.
91        // Variables can be added by the following command
92        // XMLPortParam(Classname, "xml-attribute-name (i.e. variablename)", setFunction, getFunction, xmlelement, mode)
93        //TODO: Remove.
94        XMLPortParam(AutonomousDrone, "auxiliaryThrus", setAuxiliaryThrust, getAuxiliaryThrust, xmlelement, mode);
95        XMLPortParam(AutonomousDrone, "rotationThrust", setRotationThrust, getRotationThrust, xmlelement, mode);
96
97    }
98
99    /**
100    @brief
101        Defines which actions the AutonomousDrone has to take in each tick.
102    @param dt
103        The length of the tick.
104    */
105    void AutonomousDrone::tick(float dt)
106    {
107        SUPER(AutonomousDrone, tick, dt);
108
109        this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() * getMass() * this->auxiliaryThrust_);
110        this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() * getMass() * this->auxiliaryThrust_);
111        if (this->localLinearAcceleration_.z() > 0)
112            this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() * getMass() * this->auxiliaryThrust_);
113        else
114            this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() * getMass() * this->primaryThrust_);
115        this->physicalBody_->applyCentralForce(physicalBody_->getWorldTransform().getBasis() * this->localLinearAcceleration_);
116        this->localLinearAcceleration_.setValue(0, 0, 0);
117
118        this->localAngularAcceleration_ *= this->getLocalInertia() * this->rotationThrust_;
119        this->physicalBody_->applyTorque(physicalBody_->getWorldTransform().getBasis() * this->localAngularAcceleration_);
120        this->localAngularAcceleration_.setValue(0, 0, 0);
121    }
122
123    /**
124    @brief
125        Moves the AutonomousDrone in the negative z-direction (Front/Back) by an amount specified by the first component of the input 2-dim vector.
126    @param value
127        The vector determining the amount of the movement.
128    */
129    void AutonomousDrone::moveFrontBack(const Vector2& value)
130    {
131        this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() - value.x);
132    }
133
134    /**
135    @brief
136        Moves the AutonomousDrone in the x-direction (Right/Left) by an amount specified by the first component of the input 2-dim vector.
137    @param value
138        The vector determining the amount of the movement.
139    */
140    void AutonomousDrone::moveRightLeft(const Vector2& value)
141    {
142        this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() + value.x);
143    }
144
145    /**
146    @brief
147        Moves the AutonomousDrone in the y-direction (Up/Down) by an amount specified by the first component of the input 2-dim vector.
148    @param value
149        The vector determining the amount of the movement.
150    */
151    void AutonomousDrone::moveUpDown(const Vector2& value)
152    {
153        this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() + value.x);
154    }
155
156    /**
157    @brief
158        Rotates the AutonomousDrone around the y-axis by the amount specified by the first component of the input 2-dim vector.
159    @param value
160        The vector determining the amount of the angular movement.
161    */
162    void AutonomousDrone::rotateYaw(const Vector2& value)
163    {
164        this->localAngularAcceleration_.setY(this->localAngularAcceleration_.y() - value.x);
165    }
166
167    /**
168    @brief
169        Rotates the AutonomousDrone around the x-axis by the amount specified by the first component of the input 2-dim vector.
170    @param value
171        The vector determining the amount of the angular movement.
172    */
173    void AutonomousDrone::rotatePitch(const Vector2& value)
174    {
175        this->localAngularAcceleration_.setX(this->localAngularAcceleration_.x() + value.x);
176    }
177
178    /**
179    @brief
180        Rotates the AutonomousDrone around the z-axis by the amount specified by the first component of the input 2-dim vector.
181    @param value
182        The vector determining the amount of the angular movement.
183    */
184    void AutonomousDrone::rotateRoll(const Vector2& value)
185    {
186        this->localAngularAcceleration_.setZ(this->localAngularAcceleration_.z() + value.x);
187    }
188
189}
Note: See TracBrowser for help on using the repository browser.