Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/mergeFS18/src/orxonox/worldentities/AutonomousDrone.cc @ 12234

Last change on this file since 12234 was 12028, checked in by merholzl, 6 years ago

added scriptable controller

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