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 | * Marian Runo |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include "Turret.h" |
---|
30 | #include "core/CoreIncludes.h" |
---|
31 | #include "OgreQuaternion.h" |
---|
32 | #include "core/XMLPort.h" |
---|
33 | #include "controllers/WaypointPatrolController.h" |
---|
34 | |
---|
35 | namespace orxonox |
---|
36 | { |
---|
37 | RegisterClass(Turret); |
---|
38 | |
---|
39 | |
---|
40 | |
---|
41 | /** |
---|
42 | * @brief Constructor |
---|
43 | */ |
---|
44 | Turret::Turret(Context* context) : SpaceShip(context) |
---|
45 | { |
---|
46 | RegisterObject(Turret); |
---|
47 | this->startOrientInv_ = Quaternion::IDENTITY; |
---|
48 | this->maxPitch_ = 0; |
---|
49 | this->maxYaw_ = 0; |
---|
50 | this->gotOrient_ = false; |
---|
51 | } |
---|
52 | |
---|
53 | /** |
---|
54 | * @brief Destructor |
---|
55 | */ |
---|
56 | Turret::~Turret() |
---|
57 | { |
---|
58 | |
---|
59 | } |
---|
60 | |
---|
61 | |
---|
62 | void Turret::rotatePitch(const Vector2& value) |
---|
63 | { |
---|
64 | if (this->maxPitch_ == 0) |
---|
65 | { |
---|
66 | return; |
---|
67 | } |
---|
68 | if (this->maxPitch_ >= 180) //no need to check, if the limit too big |
---|
69 | { |
---|
70 | SpaceShip::rotatePitch(value); |
---|
71 | return; |
---|
72 | } |
---|
73 | |
---|
74 | Quaternion drot = startOrientInv_ * this->getOrientation(); |
---|
75 | |
---|
76 | Ogre::Real val = boundBetween(drot.getPitch(false).valueDegrees(), -180, 180); |
---|
77 | Ogre::Real offset = boundBetween(Degree(value.x).valueDegrees(), -180, 180); |
---|
78 | Ogre::Real lowerBound = offset - this->maxPitch_; |
---|
79 | Ogre::Real upperBound = offset + this->maxPitch_; |
---|
80 | if (lowerBound < -180) //Avoid wrapping around of the boundaries |
---|
81 | { |
---|
82 | lowerBound += this->maxPitch_; |
---|
83 | upperBound += this->maxPitch_; |
---|
84 | val = boundBetween(val + this->maxPitch_, -180, 180); //val might wrap around here |
---|
85 | } |
---|
86 | else if (upperBound >= 180) //Avoid wrapping around of the boundaries (the other side) |
---|
87 | { |
---|
88 | lowerBound -= this->maxPitch_; |
---|
89 | upperBound -= this->maxPitch_; |
---|
90 | val = boundBetween(val-this->maxPitch_, -180, 180); //val might wrap around here |
---|
91 | } |
---|
92 | if ((val >= lowerBound || value.x > 0) && (val <= upperBound || value.x < 0)) |
---|
93 | { |
---|
94 | SpaceShip::rotatePitch(value); |
---|
95 | } |
---|
96 | return; |
---|
97 | } |
---|
98 | |
---|
99 | void Turret::rotateYaw(const Vector2& value) |
---|
100 | { |
---|
101 | if (this->maxPitch_ == 0) |
---|
102 | { |
---|
103 | return; |
---|
104 | } |
---|
105 | if (this->maxPitch_ >= 180) //no need to check, if the limit too big |
---|
106 | { |
---|
107 | SpaceShip::rotateYaw(value); |
---|
108 | return; |
---|
109 | } |
---|
110 | |
---|
111 | Quaternion drot = startOrientInv_ * this->getOrientation(); |
---|
112 | |
---|
113 | Ogre::Real val = boundBetween(drot.getYaw(false).valueDegrees(), -180, 180); |
---|
114 | Ogre::Real offset = boundBetween(Degree(value.x).valueDegrees(), -180, 180); |
---|
115 | Ogre::Real lowerBound = offset - this->maxPitch_; |
---|
116 | Ogre::Real upperBound = offset + this->maxPitch_; |
---|
117 | if (lowerBound < -180) //Avoid wrapping around of the boundaries |
---|
118 | { |
---|
119 | lowerBound += this->maxPitch_; |
---|
120 | upperBound += this->maxPitch_; |
---|
121 | val = boundBetween(val + this->maxPitch_, -180, 180); //val might wrap around here |
---|
122 | } |
---|
123 | else if (upperBound >= 180) //Avoid wrapping around of the boundaries (the other side) |
---|
124 | { |
---|
125 | lowerBound -= this->maxPitch_; |
---|
126 | upperBound -= this->maxPitch_; |
---|
127 | val = boundBetween(val-this->maxPitch_, -180, 180); //val might wrap around here |
---|
128 | } |
---|
129 | if ((val >= lowerBound || value.x > 0) && (val <= upperBound || value.x < 0)) |
---|
130 | { |
---|
131 | SpaceShip::rotateYaw(value); |
---|
132 | } |
---|
133 | return; |
---|
134 | } |
---|
135 | |
---|
136 | void Turret::rotateRoll(const Vector2& value) |
---|
137 | { |
---|
138 | return; //Standard turrets don't roll |
---|
139 | } |
---|
140 | |
---|
141 | void Turret::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
142 | { |
---|
143 | XMLPortParam(Turret, "maxPitch", setMaxPitch, getMaxPitch, xmlelement, mode); |
---|
144 | XMLPortParam(Turret, "maxYaw", setMaxYaw, getMaxYaw, xmlelement, mode); |
---|
145 | SUPER(Turret, XMLPort, xmlelement, mode); |
---|
146 | } |
---|
147 | |
---|
148 | void Turret::tick(float dt) |
---|
149 | { |
---|
150 | if(!gotOrient_) |
---|
151 | { |
---|
152 | startOrientInv_ = this->getOrientation().Inverse(); |
---|
153 | gotOrient_ = true; |
---|
154 | } |
---|
155 | Quaternion drot = startOrientInv_ * this->getOrientation(); |
---|
156 | orxout() << "Pitch: " << drot.getPitch(false).valueDegrees() << "\tYaw: " << drot.getYaw(false).valueDegrees() << "\tRoll: " << drot.getRoll(false).valueDegrees() << endl; |
---|
157 | SUPER(Turret, tick, dt); |
---|
158 | } |
---|
159 | |
---|
160 | |
---|
161 | Ogre::Real Turret::boundBetween(Ogre::Real val, Ogre::Real lowerBound, Ogre::Real upperBound) |
---|
162 | { |
---|
163 | if (lowerBound > upperBound){ std::swap(lowerBound, upperBound); } |
---|
164 | val -= lowerBound; //adjust to 0 |
---|
165 | Ogre::Real rangeSize = upperBound - lowerBound; |
---|
166 | if (rangeSize == 0){ return upperBound; } //avoid dividing by 0 |
---|
167 | return val - (rangeSize * std::floor(val / rangeSize)) + lowerBound; |
---|
168 | } |
---|
169 | |
---|
170 | } |
---|