1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * |
---|
4 | * |
---|
5 | * License notice: |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or |
---|
8 | * modify it under the terms of the GNU General Public License |
---|
9 | * as published by the Free Software Foundation; either version 2 |
---|
10 | * of the License, or (at your option) any later version. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * GNU General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License |
---|
18 | * along with this program; if not, write to the Free Software |
---|
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
20 | * |
---|
21 | * Author: |
---|
22 | * ... |
---|
23 | * Co-authors: |
---|
24 | * ... |
---|
25 | * |
---|
26 | */ |
---|
27 | |
---|
28 | #include "Ogre.h" |
---|
29 | #include "spaceship_steering.h" |
---|
30 | using namespace Ogre; |
---|
31 | |
---|
32 | SpaceshipSteering::SpaceshipSteering(float maxSpeedForward, float |
---|
33 | maxSpeedRotateUpDown, float maxSpeedRotateRightLeft, float |
---|
34 | maxSpeedLoopRightLeft) { |
---|
35 | moveForward_ = 0; |
---|
36 | rotateUp_ = 0; |
---|
37 | rotateDown_ = 0; |
---|
38 | rotateRight_ = 0; |
---|
39 | rotateLeft_ = 0; |
---|
40 | loopRight_ = 0; |
---|
41 | loopLeft_ = 0; |
---|
42 | brakeForward_ = 0; |
---|
43 | brakeRotate_ = 0; |
---|
44 | brakeLoop_ = 0; |
---|
45 | speedForward_ = 0; |
---|
46 | speedRotateUpDown_ = 0; |
---|
47 | speedRotateRightLeft_ = 0; |
---|
48 | speedLoopRightLeft_ = 0; |
---|
49 | maxSpeedForward_ = maxSpeedForward; |
---|
50 | maxSpeedRotateUpDown_ = maxSpeedRotateUpDown; |
---|
51 | maxSpeedRotateRightLeft_ = maxSpeedRotateRightLeft; |
---|
52 | maxSpeedLoopRightLeft_ = maxSpeedLoopRightLeft; |
---|
53 | accelerationForward_ = 0; |
---|
54 | accelerationRotateUpDown_ = 0; |
---|
55 | accelerationRotateRightLeft_ = 0; |
---|
56 | accelerationLoopRightLeft_ = 0; |
---|
57 | } |
---|
58 | |
---|
59 | void SpaceshipSteering::tick(float time) { |
---|
60 | |
---|
61 | if(moveForward_ > 0) { |
---|
62 | accelerationForward_ = moveForward_; |
---|
63 | if(speedForward_ < maxSpeedForward_) |
---|
64 | speedForward_ += accelerationForward_*time; |
---|
65 | if(speedForward_ > maxSpeedForward_) |
---|
66 | speedForward_ = maxSpeedForward_; |
---|
67 | } |
---|
68 | if(moveForward_ <= 0) { |
---|
69 | accelerationForward_ = brakeForward_; |
---|
70 | if(speedForward_ > 0) |
---|
71 | speedForward_ -= accelerationForward_*time; |
---|
72 | if(speedForward_ < 0) |
---|
73 | speedForward_ = 0; |
---|
74 | } |
---|
75 | |
---|
76 | if(rotateUp_ > 0) { |
---|
77 | accelerationRotateUpDown_ = rotateUp_; |
---|
78 | if(speedRotateUpDown_ < maxSpeedRotateUpDown_) |
---|
79 | speedRotateUpDown_ += accelerationRotateUpDown_*time; |
---|
80 | if(speedRotateUpDown_ > maxSpeedRotateUpDown_) |
---|
81 | speedRotateUpDown_ = maxSpeedRotateUpDown_; |
---|
82 | } |
---|
83 | if(rotateDown_ > 0) { |
---|
84 | accelerationRotateUpDown_ = rotateDown_; |
---|
85 | if(speedRotateUpDown_ > -maxSpeedRotateUpDown_) |
---|
86 | speedRotateUpDown_ -= accelerationRotateUpDown_*time; |
---|
87 | if(speedRotateUpDown_ < -maxSpeedRotateUpDown_) |
---|
88 | speedRotateUpDown_ = -maxSpeedRotateUpDown_; |
---|
89 | } |
---|
90 | if(rotateUp_ == 0 && rotateDown_ == 0) { |
---|
91 | accelerationRotateUpDown_ = brakeRotate_; |
---|
92 | if(speedRotateUpDown_ > 0) |
---|
93 | speedRotateUpDown_ -= accelerationRotateUpDown_*time; |
---|
94 | if(speedRotateUpDown_ < 0) |
---|
95 | speedRotateUpDown_ += accelerationRotateUpDown_*time; |
---|
96 | if(abs(speedRotateUpDown_)<accelerationRotateUpDown_*time) |
---|
97 | speedRotateUpDown_ = 0; |
---|
98 | } |
---|
99 | |
---|
100 | if(rotateRight_ > 0) { |
---|
101 | accelerationRotateRightLeft_ = rotateRight_; |
---|
102 | if(speedRotateRightLeft_ > -maxSpeedRotateRightLeft_) |
---|
103 | speedRotateRightLeft_ -= accelerationRotateRightLeft_*time; |
---|
104 | if(speedRotateRightLeft_ < -maxSpeedRotateRightLeft_) |
---|
105 | speedRotateRightLeft_ = -maxSpeedRotateRightLeft_; |
---|
106 | } |
---|
107 | if(rotateLeft_ > 0) { |
---|
108 | accelerationRotateRightLeft_ = rotateLeft_; |
---|
109 | if(speedRotateRightLeft_ < maxSpeedRotateRightLeft_) |
---|
110 | speedRotateRightLeft_ += accelerationRotateRightLeft_*time; |
---|
111 | if(speedRotateRightLeft_ > maxSpeedRotateRightLeft_) |
---|
112 | speedRotateRightLeft_ = maxSpeedRotateRightLeft_; |
---|
113 | } |
---|
114 | if(rotateRight_ == 0 && rotateLeft_ == 0) { |
---|
115 | accelerationRotateRightLeft_ = brakeRotate_; |
---|
116 | if(speedRotateRightLeft_ > 0) |
---|
117 | speedRotateRightLeft_ -= accelerationRotateRightLeft_*time; |
---|
118 | if(speedRotateRightLeft_ < 0) |
---|
119 | speedRotateRightLeft_ += accelerationRotateRightLeft_*time; |
---|
120 | if(abs(speedRotateRightLeft_)<accelerationRotateRightLeft_*time) |
---|
121 | speedRotateRightLeft_ = 0; |
---|
122 | } |
---|
123 | |
---|
124 | if(loopRight_ > 0) { |
---|
125 | accelerationLoopRightLeft_ = loopRight_; |
---|
126 | if(speedLoopRightLeft_ < maxSpeedLoopRightLeft_) |
---|
127 | speedLoopRightLeft_ += accelerationLoopRightLeft_*time; |
---|
128 | if(speedLoopRightLeft_ > maxSpeedLoopRightLeft_) |
---|
129 | speedLoopRightLeft_ = maxSpeedLoopRightLeft_; |
---|
130 | } |
---|
131 | if(loopLeft_ > 0) { |
---|
132 | accelerationLoopRightLeft_ = loopLeft_; |
---|
133 | if(speedLoopRightLeft_ > -maxSpeedLoopRightLeft_) |
---|
134 | speedLoopRightLeft_ -= accelerationLoopRightLeft_*time; |
---|
135 | if(speedLoopRightLeft_ < -maxSpeedLoopRightLeft_) |
---|
136 | speedLoopRightLeft_ = -maxSpeedLoopRightLeft_; |
---|
137 | } |
---|
138 | if(loopLeft_ == 0 && loopRight_ == 0) { |
---|
139 | accelerationLoopRightLeft_ = brakeLoop_; |
---|
140 | if(speedLoopRightLeft_ > 0) |
---|
141 | speedLoopRightLeft_ -= accelerationLoopRightLeft_*time; |
---|
142 | if(speedLoopRightLeft_ < 0) |
---|
143 | speedLoopRightLeft_ += accelerationLoopRightLeft_*time; |
---|
144 | if(abs(speedLoopRightLeft_)<accelerationLoopRightLeft_*time) |
---|
145 | speedLoopRightLeft_ = 0; |
---|
146 | } |
---|
147 | |
---|
148 | Vector3 transVector = Vector3::ZERO; |
---|
149 | transVector.z = 1; |
---|
150 | steeringNode_->translate(transVector*speedForward_*time, |
---|
151 | Node::TS_LOCAL); |
---|
152 | steeringNode_->pitch(Degree(speedRotateUpDown_*time), |
---|
153 | Node::TS_LOCAL); |
---|
154 | steeringNode_->yaw(Degree(speedRotateRightLeft_*time), |
---|
155 | Node::TS_LOCAL); |
---|
156 | steeringNode_->roll(Degree(speedLoopRightLeft_*time), |
---|
157 | Node::TS_LOCAL); |
---|
158 | |
---|
159 | } |
---|
160 | |
---|
161 | void SpaceshipSteering::moveForward(float moveForward) { |
---|
162 | moveForward_ = moveForward; |
---|
163 | } |
---|
164 | |
---|
165 | void SpaceshipSteering::rotateUp(float rotateUp) { |
---|
166 | rotateUp_ = rotateUp; |
---|
167 | } |
---|
168 | |
---|
169 | void SpaceshipSteering::rotateDown(float rotateDown) { |
---|
170 | rotateDown_ = rotateDown; |
---|
171 | } |
---|
172 | |
---|
173 | void SpaceshipSteering::rotateLeft(float rotateLeft) { |
---|
174 | rotateLeft_ = rotateLeft; |
---|
175 | } |
---|
176 | |
---|
177 | void SpaceshipSteering::rotateRight(float rotateRight) { |
---|
178 | rotateRight_ = rotateRight; |
---|
179 | } |
---|
180 | |
---|
181 | void SpaceshipSteering::loopLeft(float loopLeft) { |
---|
182 | loopLeft_ = loopLeft; |
---|
183 | } |
---|
184 | |
---|
185 | void SpaceshipSteering::loopRight(float loopRight) { |
---|
186 | loopRight_ = loopRight; |
---|
187 | } |
---|
188 | |
---|
189 | void SpaceshipSteering::brakeForward(float brakeForward) { |
---|
190 | brakeForward_ = brakeForward; |
---|
191 | } |
---|
192 | |
---|
193 | void SpaceshipSteering::brakeRotate(float brakeRotate) { |
---|
194 | brakeRotate_ = brakeRotate; |
---|
195 | } |
---|
196 | |
---|
197 | void SpaceshipSteering::brakeLoop(float brakeLoop) { |
---|
198 | brakeLoop_ = brakeLoop; |
---|
199 | } |
---|
200 | |
---|
201 | void SpaceshipSteering::maxSpeedForward(float maxSpeedForward) { |
---|
202 | maxSpeedForward_ = maxSpeedForward; |
---|
203 | } |
---|
204 | |
---|
205 | void SpaceshipSteering::maxSpeedRotateUpDown(float maxSpeedRotateUpDown) { |
---|
206 | maxSpeedRotateUpDown_ = maxSpeedRotateUpDown; |
---|
207 | } |
---|
208 | |
---|
209 | void SpaceshipSteering::maxSpeedRotateRightLeft(float maxSpeedRotateRightLeft) { |
---|
210 | maxSpeedRotateRightLeft_ = maxSpeedRotateRightLeft; |
---|
211 | } |
---|
212 | |
---|
213 | void SpaceshipSteering::maxSpeedLoopRightLeft(float maxSpeedLoopRightLeft) { |
---|
214 | maxSpeedLoopRightLeft_ = maxSpeedLoopRightLeft; |
---|
215 | } |
---|
216 | |
---|
217 | void SpaceshipSteering::addNode(Ogre::SceneNode *steeringNode) { |
---|
218 | steeringNode_ = steeringNode; |
---|
219 | } |
---|