Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/steering/src/spaceship_steering.cc @ 209

Last change on this file since 209 was 209, checked in by mbiber, 12 years ago

added steering class

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