Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/merger/src/orxonox/spaceship_steering.cc @ 489

Last change on this file since 489 was 392, checked in by mbiber, 17 years ago

fixed a bug in spaceship_steering.cc

File size: 5.9 KB
Line 
1#include "Ogre.h"
2#include "spaceship_steering.h"
3using namespace Ogre;
4
5SpaceshipSteering::SpaceshipSteering(float maxSpeedForward, float
6maxSpeedRotateUpDown, float maxSpeedRotateRightLeft, float
7maxSpeedLoopRightLeft) {
8  moveForward_ = 0;
9  rotateUp_ = 0;
10  rotateDown_ = 0;
11  rotateRight_ = 0;
12  rotateLeft_ = 0;
13  loopRight_ = 0;
14  loopLeft_ = 0;
15  brakeForward_ = 0;
16  brakeRotate_ = 0;
17  brakeLoop_ = 0;
18  speedForward_ = 0;
19  speedRotateUpDown_ = 0;
20  speedRotateRightLeft_ = 0;
21  speedLoopRightLeft_ = 0;
22  maxSpeedForward_ = maxSpeedForward;
23  maxSpeedRotateUpDown_ = maxSpeedRotateUpDown;
24  maxSpeedRotateRightLeft_ = maxSpeedRotateRightLeft;
25  maxSpeedLoopRightLeft_ = maxSpeedLoopRightLeft;
26  accelerationForward_ = 0;
27  accelerationRotateUpDown_ = 0;
28  accelerationRotateRightLeft_ = 0;
29  accelerationLoopRightLeft_ = 0;
30}
31
32void SpaceshipSteering::tick(float time) {
33
34  if(moveForward_ > 0) {
35    accelerationForward_ = moveForward_;
36    if(speedForward_ < maxSpeedForward_)
37      speedForward_ += accelerationForward_*time;
38    if(speedForward_ > maxSpeedForward_)
39      speedForward_ = maxSpeedForward_;
40  }
41  if(moveForward_ <= 0) {
42    accelerationForward_ = brakeForward_;
43    if(speedForward_ > 0)
44      speedForward_ -= accelerationForward_*time;
45    if(speedForward_ < 0)
46      speedForward_ = 0;
47  }
48
49  if(rotateUp_ > 0) {
50    accelerationRotateUpDown_ = rotateUp_;
51    if(speedRotateUpDown_ < maxSpeedRotateUpDown_)
52      speedRotateUpDown_ += accelerationRotateUpDown_*time;
53    if(speedRotateUpDown_ > maxSpeedRotateUpDown_)
54      speedRotateUpDown_ = maxSpeedRotateUpDown_;
55  }
56  if(rotateDown_ > 0) {
57    accelerationRotateUpDown_ = rotateDown_;
58    if(speedRotateUpDown_ > -maxSpeedRotateUpDown_)
59      speedRotateUpDown_ -= accelerationRotateUpDown_*time;
60    if(speedRotateUpDown_ < -maxSpeedRotateUpDown_)
61      speedRotateUpDown_ = -maxSpeedRotateUpDown_;
62  }
63  if(rotateUp_ == 0 && rotateDown_ == 0) {
64    accelerationRotateUpDown_ = brakeRotate_;
65    if(speedRotateUpDown_ > 0)
66      speedRotateUpDown_ -= accelerationRotateUpDown_*time;
67    if(speedRotateUpDown_ < 0)
68      speedRotateUpDown_ += accelerationRotateUpDown_*time;
69    if(abs(speedRotateUpDown_)<accelerationRotateUpDown_*time)
70      speedRotateUpDown_ = 0;
71  }
72
73  if(rotateRight_ > 0) {
74    accelerationRotateRightLeft_ = rotateRight_;
75    if(speedRotateRightLeft_ > -maxSpeedRotateRightLeft_)
76      speedRotateRightLeft_ -= accelerationRotateRightLeft_*time;
77    if(speedRotateRightLeft_ < -maxSpeedRotateRightLeft_)
78      speedRotateRightLeft_ = -maxSpeedRotateRightLeft_;
79  }
80  if(rotateLeft_ > 0) {
81    accelerationRotateRightLeft_ = rotateLeft_;
82    if(speedRotateRightLeft_ < maxSpeedRotateRightLeft_)
83      speedRotateRightLeft_ += accelerationRotateRightLeft_*time;
84    if(speedRotateRightLeft_ > maxSpeedRotateRightLeft_)
85      speedRotateRightLeft_ = maxSpeedRotateRightLeft_;
86  }
87  if(rotateRight_ == 0 && rotateLeft_ == 0) {
88    accelerationRotateRightLeft_ = brakeRotate_;
89    if(speedRotateRightLeft_ > 0)
90      speedRotateRightLeft_ -= accelerationRotateRightLeft_*time;
91    if(speedRotateRightLeft_ < 0)
92      speedRotateRightLeft_ += accelerationRotateRightLeft_*time;
93    if(abs(speedRotateRightLeft_)<accelerationRotateRightLeft_*time)
94      speedRotateRightLeft_ = 0;
95  }
96
97  if(loopRight_ > 0) {
98    accelerationLoopRightLeft_ = loopRight_;
99    if(speedLoopRightLeft_ < maxSpeedLoopRightLeft_)
100      speedLoopRightLeft_ += accelerationLoopRightLeft_*time;
101    if(speedLoopRightLeft_ > maxSpeedLoopRightLeft_)
102      speedLoopRightLeft_ = maxSpeedLoopRightLeft_;
103  }
104  if(loopLeft_ > 0) {
105    accelerationLoopRightLeft_ = loopLeft_;
106    if(speedLoopRightLeft_ > -maxSpeedLoopRightLeft_)
107      speedLoopRightLeft_ -= accelerationLoopRightLeft_*time;
108    if(speedLoopRightLeft_ < -maxSpeedLoopRightLeft_)
109      speedLoopRightLeft_ = -maxSpeedLoopRightLeft_;
110  }
111  if(loopLeft_ == 0 && loopRight_ == 0) {
112    accelerationLoopRightLeft_ = brakeLoop_;
113    if(speedLoopRightLeft_ > 0)
114      speedLoopRightLeft_ -= accelerationLoopRightLeft_*time;
115    if(speedLoopRightLeft_ < 0)
116      speedLoopRightLeft_ += accelerationLoopRightLeft_*time;
117    if(abs(speedLoopRightLeft_)<accelerationLoopRightLeft_*time)
118      speedLoopRightLeft_ = 0;
119  }
120
121  Vector3 transVector = Vector3::ZERO;
122  transVector.z = 1;
123  steeringNode_->translate(transVector*speedForward_*time,
124  Node::TS_LOCAL);
125  steeringNode_->pitch(Degree(speedRotateUpDown_*time),
126  Node::TS_LOCAL);
127  steeringNode_->yaw(Degree(speedRotateRightLeft_*time),
128  Node::TS_LOCAL);
129  steeringNode_->roll(Degree(speedLoopRightLeft_*time),
130  Node::TS_LOCAL);
131
132}
133
134void SpaceshipSteering::moveForward(float moveForward) {
135  moveForward_ = moveForward;
136}
137
138void SpaceshipSteering::rotateUp(float rotateUp) {
139  rotateUp_ = rotateUp;
140}
141
142void SpaceshipSteering::rotateDown(float rotateDown) {
143  rotateDown_ = rotateDown;
144}
145
146void SpaceshipSteering::rotateLeft(float rotateLeft) {
147  rotateLeft_ = rotateLeft;
148}
149
150void SpaceshipSteering::rotateRight(float rotateRight) {
151  rotateRight_ = rotateRight;
152}
153
154void SpaceshipSteering::loopLeft(float loopLeft) {
155  loopLeft_ = loopLeft;
156}
157
158void SpaceshipSteering::loopRight(float loopRight) {
159  loopRight_ = loopRight;
160}
161
162void SpaceshipSteering::brakeForward(float brakeForward) {
163  brakeForward_ = brakeForward;
164}
165
166void SpaceshipSteering::brakeRotate(float brakeRotate) {
167  brakeRotate_ = brakeRotate;
168}
169
170void SpaceshipSteering::brakeLoop(float brakeLoop) {
171  brakeLoop_ = brakeLoop;
172}
173
174void SpaceshipSteering::maxSpeedForward(float maxSpeedForward) {
175  maxSpeedForward_ = maxSpeedForward;
176}
177
178void SpaceshipSteering::maxSpeedRotateUpDown(float maxSpeedRotateUpDown) {
179  maxSpeedRotateUpDown_ = maxSpeedRotateUpDown;
180}
181
182void SpaceshipSteering::maxSpeedRotateRightLeft(float maxSpeedRotateRightLeft) {
183  maxSpeedRotateRightLeft_ = maxSpeedRotateRightLeft;
184}
185
186void SpaceshipSteering::maxSpeedLoopRightLeft(float maxSpeedLoopRightLeft) {
187  maxSpeedLoopRightLeft_ = maxSpeedLoopRightLeft;
188}
189
190void SpaceshipSteering::addNode(Ogre::SceneNode *steeringNode) {
191  steeringNode_ = steeringNode;
192}
Note: See TracBrowser for help on using the repository browser.