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