[209] | 1 | #include "Ogre.h" |
---|
| 2 | #include "spaceship_steering.h" |
---|
| 3 | using namespace Ogre; |
---|
| 4 | |
---|
| 5 | SpaceshipSteering::SpaceshipSteering(float maxSpeedForward, float |
---|
| 6 | maxSpeedRotateUpDown, float maxSpeedRotateRightLeft, float |
---|
| 7 | maxSpeedLoopRightLeft) { |
---|
| 8 | moveForward_ = 0; |
---|
| 9 | rotateUp_ = 0; |
---|
| 10 | rotateDown_ = 0; |
---|
| 11 | rotateRight_ = 0; |
---|
| 12 | rotateLeft_ = 0; |
---|
| 13 | loopRight_ = 0; |
---|
| 14 | loopLeft_ = 0; |
---|
[317] | 15 | brakeForward_ = 0; |
---|
| 16 | brakeRotate_ = 0; |
---|
| 17 | brakeLoop_ = 0; |
---|
[209] | 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 | |
---|
| 32 | void SpaceshipSteering::tick(float time) { |
---|
| 33 | |
---|
| 34 | if(moveForward_ > 0) { |
---|
| 35 | accelerationForward_ = moveForward_; |
---|
| 36 | if(speedForward_ < maxSpeedForward_) |
---|
[317] | 37 | speedForward_ += accelerationForward_*time; |
---|
[209] | 38 | if(speedForward_ > maxSpeedForward_) |
---|
| 39 | speedForward_ = maxSpeedForward_; |
---|
| 40 | } |
---|
[317] | 41 | if(moveForward_ <= 0) { |
---|
| 42 | accelerationForward_ = brakeForward_; |
---|
[209] | 43 | if(speedForward_ > 0) |
---|
[317] | 44 | speedForward_ -= accelerationForward_*time; |
---|
[209] | 45 | if(speedForward_ < 0) |
---|
| 46 | speedForward_ = 0; |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | if(rotateUp_ > 0) { |
---|
| 50 | accelerationRotateUpDown_ = rotateUp_; |
---|
| 51 | if(speedRotateUpDown_ < maxSpeedRotateUpDown_) |
---|
[317] | 52 | speedRotateUpDown_ += accelerationRotateUpDown_*time; |
---|
[209] | 53 | if(speedRotateUpDown_ > maxSpeedRotateUpDown_) |
---|
| 54 | speedRotateUpDown_ = maxSpeedRotateUpDown_; |
---|
| 55 | } |
---|
[317] | 56 | if(rotateDown_ > 0) { |
---|
[209] | 57 | accelerationRotateUpDown_ = rotateDown_; |
---|
| 58 | if(speedRotateUpDown_ > -maxSpeedRotateUpDown_) |
---|
[317] | 59 | speedRotateUpDown_ -= accelerationRotateUpDown_*time; |
---|
[209] | 60 | if(speedRotateUpDown_ < -maxSpeedRotateUpDown_) |
---|
| 61 | speedRotateUpDown_ = -maxSpeedRotateUpDown_; |
---|
| 62 | } |
---|
[317] | 63 | if(rotateUp_ == 0 && rotateDown_ == 0) { |
---|
| 64 | accelerationRotateUpDown_ = brakeRotate_; |
---|
[209] | 65 | if(speedRotateUpDown_ > 0) |
---|
[317] | 66 | speedRotateUpDown_ -= accelerationRotateUpDown_*time; |
---|
[209] | 67 | if(speedRotateUpDown_ < 0) |
---|
[317] | 68 | speedRotateUpDown_ += accelerationRotateUpDown_*time; |
---|
[391] | 69 | if(abs(speedRotateUpDown_)<accelerationRotateUpDown_*time) |
---|
| 70 | speedRotateUpDown_ = 0; |
---|
[209] | 71 | } |
---|
| 72 | |
---|
| 73 | if(rotateRight_ > 0) { |
---|
| 74 | accelerationRotateRightLeft_ = rotateRight_; |
---|
| 75 | if(speedRotateRightLeft_ > -maxSpeedRotateRightLeft_) |
---|
[317] | 76 | speedRotateRightLeft_ -= accelerationRotateRightLeft_*time; |
---|
[209] | 77 | if(speedRotateRightLeft_ < -maxSpeedRotateRightLeft_) |
---|
| 78 | speedRotateRightLeft_ = -maxSpeedRotateRightLeft_; |
---|
| 79 | } |
---|
[317] | 80 | if(rotateLeft_ > 0) { |
---|
[209] | 81 | accelerationRotateRightLeft_ = rotateLeft_; |
---|
| 82 | if(speedRotateRightLeft_ < maxSpeedRotateRightLeft_) |
---|
[317] | 83 | speedRotateRightLeft_ += accelerationRotateRightLeft_*time; |
---|
[209] | 84 | if(speedRotateRightLeft_ > maxSpeedRotateRightLeft_) |
---|
| 85 | speedRotateRightLeft_ = maxSpeedRotateRightLeft_; |
---|
| 86 | } |
---|
[317] | 87 | if(rotateRight_ == 0 && rotateLeft_ == 0) { |
---|
| 88 | accelerationRotateRightLeft_ = brakeRotate_; |
---|
[209] | 89 | if(speedRotateRightLeft_ > 0) |
---|
[317] | 90 | speedRotateRightLeft_ -= accelerationRotateRightLeft_*time; |
---|
[209] | 91 | if(speedRotateRightLeft_ < 0) |
---|
[317] | 92 | speedRotateRightLeft_ += accelerationRotateRightLeft_*time; |
---|
[391] | 93 | if(abs(speedRotateRightLeft_)<accelerationRotateRightLeft_*time) |
---|
| 94 | speedRotateRightLeft_ = 0; |
---|
[209] | 95 | } |
---|
| 96 | |
---|
| 97 | if(loopRight_ > 0) { |
---|
| 98 | accelerationLoopRightLeft_ = loopRight_; |
---|
| 99 | if(speedLoopRightLeft_ < maxSpeedLoopRightLeft_) |
---|
[317] | 100 | speedLoopRightLeft_ += accelerationLoopRightLeft_*time; |
---|
[209] | 101 | if(speedLoopRightLeft_ > maxSpeedLoopRightLeft_) |
---|
| 102 | speedLoopRightLeft_ = maxSpeedLoopRightLeft_; |
---|
| 103 | } |
---|
[317] | 104 | if(loopLeft_ > 0) { |
---|
[209] | 105 | accelerationLoopRightLeft_ = loopLeft_; |
---|
| 106 | if(speedLoopRightLeft_ > -maxSpeedLoopRightLeft_) |
---|
[317] | 107 | speedLoopRightLeft_ -= accelerationLoopRightLeft_*time; |
---|
[209] | 108 | if(speedLoopRightLeft_ < -maxSpeedLoopRightLeft_) |
---|
| 109 | speedLoopRightLeft_ = -maxSpeedLoopRightLeft_; |
---|
| 110 | } |
---|
[317] | 111 | if(loopLeft_ == 0 && loopRight_ == 0) { |
---|
| 112 | accelerationLoopRightLeft_ = brakeLoop_; |
---|
[209] | 113 | if(speedLoopRightLeft_ > 0) |
---|
[317] | 114 | speedLoopRightLeft_ -= accelerationLoopRightLeft_*time; |
---|
[209] | 115 | if(speedLoopRightLeft_ < 0) |
---|
[317] | 116 | speedLoopRightLeft_ += accelerationLoopRightLeft_*time; |
---|
[391] | 117 | if(abs(speedLoopRightLeft_)<accelerationLoopRightLeft_*time) |
---|
| 118 | speedLoopRightLeft_ = 0; |
---|
[209] | 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 | |
---|
| 134 | void SpaceshipSteering::moveForward(float moveForward) { |
---|
| 135 | moveForward_ = moveForward; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | void SpaceshipSteering::rotateUp(float rotateUp) { |
---|
| 139 | rotateUp_ = rotateUp; |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | void SpaceshipSteering::rotateDown(float rotateDown) { |
---|
| 143 | rotateDown_ = rotateDown; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | void SpaceshipSteering::rotateLeft(float rotateLeft) { |
---|
| 147 | rotateLeft_ = rotateLeft; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | void SpaceshipSteering::rotateRight(float rotateRight) { |
---|
| 151 | rotateRight_ = rotateRight; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | void SpaceshipSteering::loopLeft(float loopLeft) { |
---|
| 155 | loopLeft_ = loopLeft; |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | void SpaceshipSteering::loopRight(float loopRight) { |
---|
| 159 | loopRight_ = loopRight; |
---|
| 160 | } |
---|
| 161 | |
---|
[317] | 162 | void SpaceshipSteering::brakeForward(float brakeForward) { |
---|
| 163 | brakeForward_ = brakeForward; |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | void SpaceshipSteering::brakeRotate(float brakeRotate) { |
---|
| 167 | brakeRotate_ = brakeRotate; |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | void SpaceshipSteering::brakeLoop(float brakeLoop) { |
---|
| 171 | brakeLoop_ = brakeLoop; |
---|
| 172 | } |
---|
| 173 | |
---|
[209] | 174 | void SpaceshipSteering::maxSpeedForward(float maxSpeedForward) { |
---|
| 175 | maxSpeedForward_ = maxSpeedForward; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | void SpaceshipSteering::maxSpeedRotateUpDown(float maxSpeedRotateUpDown) { |
---|
| 179 | maxSpeedRotateUpDown_ = maxSpeedRotateUpDown; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | void SpaceshipSteering::maxSpeedRotateRightLeft(float maxSpeedRotateRightLeft) { |
---|
| 183 | maxSpeedRotateRightLeft_ = maxSpeedRotateRightLeft; |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | void SpaceshipSteering::maxSpeedLoopRightLeft(float maxSpeedLoopRightLeft) { |
---|
| 187 | maxSpeedLoopRightLeft_ = maxSpeedLoopRightLeft; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | void SpaceshipSteering::addNode(Ogre::SceneNode *steeringNode) { |
---|
| 191 | steeringNode_ = steeringNode; |
---|
| 192 | } |
---|