Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/tutorial/src/orxonox/worldentities/Drone.cc @ 5833

Last change on this file since 5833 was 5833, checked in by dafrick, 15 years ago

Added some more comments to the tutorial.

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