Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/tutorial/src/orxonox/worldentities/AutonomousDrone.cc @ 8012

Last change on this file since 8012 was 8012, checked in by dafrick, 13 years ago

Committing muster-lösung.

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