[1505] | 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 | * Fabian 'x3n' Landau |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
[1791] | 29 | /** |
---|
[2087] | 30 | @file |
---|
[1791] | 31 | @brief Implementation of several math-functions. |
---|
| 32 | */ |
---|
| 33 | |
---|
[2087] | 34 | #include "Math.h" |
---|
| 35 | |
---|
[1564] | 36 | #include <OgrePlane.h> |
---|
[3196] | 37 | |
---|
[2087] | 38 | #include "MathConvert.h" |
---|
| 39 | #include "SubString.h" |
---|
[3196] | 40 | // Do not remove this include, it avoids linker errors. |
---|
[2171] | 41 | #include "mbool.h" |
---|
[1505] | 42 | |
---|
[2171] | 43 | namespace orxonox |
---|
[1505] | 44 | { |
---|
[2171] | 45 | /** |
---|
| 46 | @brief Function for writing a Radian to a stream. |
---|
| 47 | */ |
---|
| 48 | std::ostream& operator<<(std::ostream& out, const orxonox::Radian& radian) |
---|
| 49 | { |
---|
| 50 | out << radian.valueRadians(); |
---|
| 51 | return out; |
---|
| 52 | } |
---|
[1505] | 53 | |
---|
[2171] | 54 | /** |
---|
| 55 | @brief Function for reading a Radian from a stream. |
---|
| 56 | */ |
---|
| 57 | std::istream& operator>>(std::istream& in, orxonox::Radian& radian) |
---|
| 58 | { |
---|
| 59 | float temp; |
---|
| 60 | in >> temp; |
---|
| 61 | radian = temp; |
---|
| 62 | return in; |
---|
| 63 | } |
---|
[1505] | 64 | |
---|
[2171] | 65 | /** |
---|
| 66 | @brief Function for writing a Degree to a stream. |
---|
| 67 | */ |
---|
| 68 | std::ostream& operator<<(std::ostream& out, const orxonox::Degree& degree) |
---|
| 69 | { |
---|
| 70 | out << degree.valueDegrees(); |
---|
| 71 | return out; |
---|
| 72 | } |
---|
[1564] | 73 | |
---|
[2171] | 74 | /** |
---|
| 75 | @brief Function for reading a Degree from a stream. |
---|
| 76 | */ |
---|
| 77 | std::istream& operator>>(std::istream& in, orxonox::Degree& degree) |
---|
| 78 | { |
---|
| 79 | float temp; |
---|
| 80 | in >> temp; |
---|
| 81 | degree = temp; |
---|
| 82 | return in; |
---|
| 83 | } |
---|
[1564] | 84 | |
---|
[1791] | 85 | |
---|
[2171] | 86 | /** |
---|
| 87 | @brief Gets the angle between my viewing direction and the direction to the position of the other object. |
---|
| 88 | @param myposition My position |
---|
| 89 | @param mydirection My viewing direction |
---|
| 90 | @param otherposition The position of the other object |
---|
| 91 | @return The angle |
---|
[1564] | 92 | |
---|
[2171] | 93 | @example |
---|
| 94 | If the other object is exactly in front of me, the function returns 0. |
---|
| 95 | If the other object is exactly behind me, the function returns pi. |
---|
| 96 | If the other object is exactly right/left to me (or above/below), the function returns pi/2. |
---|
| 97 | */ |
---|
| 98 | float getAngle(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& otherposition) |
---|
| 99 | { |
---|
| 100 | orxonox::Vector3 distance = otherposition - myposition; |
---|
| 101 | float distancelength = distance.length(); |
---|
| 102 | if (distancelength == 0) |
---|
| 103 | return 0; |
---|
| 104 | else |
---|
| 105 | return acos(clamp<float>(mydirection.dotProduct(distance) / distancelength, -1, 1)); |
---|
| 106 | } |
---|
[1791] | 107 | |
---|
[2171] | 108 | /** |
---|
| 109 | @brief Gets the 2D viewing direction (up/down, left/right) to the position of the other object. |
---|
| 110 | @param myposition My position |
---|
| 111 | @param mydirection My viewing direction |
---|
| 112 | @param myorthonormal My orthonormalvector (pointing upwards through my head) |
---|
| 113 | @param otherposition The position of the other object |
---|
| 114 | @return The viewing direction |
---|
[1564] | 115 | |
---|
[2171] | 116 | @example |
---|
| 117 | If the other object is exactly in front of me, the function returns Vector2(0, 0). |
---|
| 118 | If the other object is exactly at my left, the function returns Vector2(-1, 0). |
---|
| 119 | If the other object is exactly at my right, the function returns Vector2(1, 0). |
---|
| 120 | If the other object is only a bit at my right, the function still returns Vector2(1, 0). |
---|
| 121 | If the other object is exactly above me, the function returns Vector2(0, 1). |
---|
| 122 | */ |
---|
| 123 | orxonox::Vector2 get2DViewdirection(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& myorthonormal, const orxonox::Vector3& otherposition) |
---|
| 124 | { |
---|
| 125 | orxonox::Vector3 distance = otherposition - myposition; |
---|
[1564] | 126 | |
---|
[2171] | 127 | // project difference vector on our plane |
---|
| 128 | orxonox::Vector3 projection = Ogre::Plane(mydirection, myposition).projectVector(distance); |
---|
[1608] | 129 | |
---|
[2171] | 130 | float projectionlength = projection.length(); |
---|
[3049] | 131 | if (projectionlength == 0) |
---|
| 132 | { |
---|
| 133 | if (myposition.dotProduct(otherposition) >= 0) |
---|
| 134 | return orxonox::Vector2(0, 0); |
---|
| 135 | else |
---|
| 136 | return orxonox::Vector2(0, 1); |
---|
| 137 | } |
---|
[3258] | 138 | |
---|
| 139 | float cos_value = clamp<float>(myorthonormal.dotProduct(projection) / projectionlength, -1, 1); |
---|
| 140 | float sin_value = sqrt( 1 - cos_value*cos_value ); |
---|
| 141 | |
---|
[2171] | 142 | if ((mydirection.crossProduct(myorthonormal)).dotProduct(distance) > 0) |
---|
[3258] | 143 | return orxonox::Vector2( sin_value, cos_value ); |
---|
[2171] | 144 | else |
---|
[3258] | 145 | return orxonox::Vector2( -sin_value, cos_value ); |
---|
[2171] | 146 | } |
---|
[1791] | 147 | |
---|
[2171] | 148 | /** |
---|
| 149 | @brief Gets the 2D viewing direction (up/down, left/right) to the position of the other object, multiplied with the viewing distance to the object (0° = 0, 180° = 1). |
---|
| 150 | @param myposition My position |
---|
| 151 | @param mydirection My viewing direction |
---|
| 152 | @param myorthonormal My orthonormalvector (pointing upwards through my head) |
---|
| 153 | @param otherposition The position of the other object |
---|
| 154 | @return The viewing direction |
---|
[1564] | 155 | |
---|
[2171] | 156 | @example |
---|
| 157 | If the other object is exactly in front of me, the function returns Vector2(0, 0). |
---|
| 158 | If the other object is exactly at my left, the function returns Vector2(-0.5, 0). |
---|
| 159 | If the other object is exactly at my right, the function returns Vector2(0.5, 0). |
---|
| 160 | If the other object is only a bit at my right, the function still returns Vector2(0.01, 0). |
---|
| 161 | If the other object is exactly above me, the function returns Vector2(0, 0.5). |
---|
| 162 | */ |
---|
| 163 | orxonox::Vector2 get2DViewcoordinates(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& myorthonormal, const orxonox::Vector3& otherposition) |
---|
| 164 | { |
---|
| 165 | orxonox::Vector3 distance = otherposition - myposition; |
---|
[1564] | 166 | |
---|
[2171] | 167 | // project difference vector on our plane |
---|
| 168 | orxonox::Vector3 projection = Ogre::Plane(mydirection, myposition).projectVector(distance); |
---|
[1608] | 169 | |
---|
[2171] | 170 | float projectionlength = projection.length(); |
---|
[3049] | 171 | if (projectionlength == 0) |
---|
| 172 | { |
---|
| 173 | if (myposition.dotProduct(otherposition) >= 0) |
---|
| 174 | return orxonox::Vector2(0, 0); |
---|
| 175 | else |
---|
| 176 | return orxonox::Vector2(0, 1); |
---|
| 177 | } |
---|
[2171] | 178 | float angle = acos(clamp<float>(myorthonormal.dotProduct(projection) / projectionlength, -1, 1)); |
---|
[1608] | 179 | |
---|
[2171] | 180 | float distancelength = distance.length(); |
---|
| 181 | if (distancelength == 0) return orxonox::Vector2(0, 0); |
---|
| 182 | float radius = acos(clamp<float>(mydirection.dotProduct(distance) / distancelength, -1, 1)) / Ogre::Math::PI; |
---|
[1566] | 183 | |
---|
[2171] | 184 | if ((mydirection.crossProduct(myorthonormal)).dotProduct(distance) > 0) |
---|
| 185 | return orxonox::Vector2(sin(angle) * radius, cos(angle) * radius); |
---|
| 186 | else |
---|
| 187 | return orxonox::Vector2(-sin(angle) * radius, cos(angle) * radius); |
---|
| 188 | } |
---|
[1791] | 189 | |
---|
[2171] | 190 | /** |
---|
| 191 | @brief Returns the predicted position I have to aim at, if I want to hit a moving target with a moving projectile. |
---|
| 192 | @param myposition My position |
---|
| 193 | @param projectilespeed The speed of my projectile |
---|
| 194 | @param targetposition The position of my target |
---|
| 195 | @param targetvelocity The velocity of my target |
---|
| 196 | @return The predicted position |
---|
[1566] | 197 | |
---|
[2171] | 198 | The function predicts the position based on a linear velocity of the target. If the target changes speed or direction, the projectile will miss. |
---|
| 199 | */ |
---|
| 200 | orxonox::Vector3 getPredictedPosition(const orxonox::Vector3& myposition, float projectilespeed, const orxonox::Vector3& targetposition, const orxonox::Vector3& targetvelocity) |
---|
| 201 | { |
---|
| 202 | float squaredProjectilespeed = projectilespeed * projectilespeed; |
---|
| 203 | orxonox::Vector3 distance = targetposition - myposition; |
---|
| 204 | float a = distance.squaredLength(); |
---|
| 205 | float b = 2 * (distance.x + distance.y + distance.z) * (targetvelocity.x + targetvelocity.y + targetvelocity.z); |
---|
| 206 | float c = targetvelocity.squaredLength(); |
---|
[1566] | 207 | |
---|
[2171] | 208 | float temp = 4*squaredProjectilespeed*c + a*a - 4*b*c; |
---|
| 209 | if (temp < 0) |
---|
| 210 | return orxonox::Vector3::ZERO; |
---|
[1625] | 211 | |
---|
[2171] | 212 | temp = sqrt(temp); |
---|
| 213 | float time = (temp + a) / (2 * (squaredProjectilespeed - b)); |
---|
| 214 | return (targetposition + targetvelocity * time); |
---|
| 215 | } |
---|
[1625] | 216 | |
---|
[2171] | 217 | unsigned long getUniqueNumber() |
---|
| 218 | { |
---|
| 219 | static unsigned long aNumber = 135; |
---|
| 220 | return aNumber++; |
---|
| 221 | } |
---|
[2087] | 222 | |
---|
| 223 | |
---|
[2171] | 224 | ////////////////////////// |
---|
| 225 | // Conversion functions // |
---|
| 226 | ////////////////////////// |
---|
[2087] | 227 | |
---|
[2171] | 228 | // std::string to Vector2 |
---|
| 229 | bool ConverterFallback<std::string, orxonox::Vector2>::convert(orxonox::Vector2* output, const std::string& input) |
---|
[2087] | 230 | { |
---|
[2171] | 231 | size_t opening_parenthesis, closing_parenthesis = input.find(')'); |
---|
| 232 | if ((opening_parenthesis = input.find('(')) == std::string::npos) |
---|
| 233 | opening_parenthesis = 0; |
---|
| 234 | else |
---|
| 235 | opening_parenthesis++; |
---|
[2087] | 236 | |
---|
[2171] | 237 | SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), |
---|
| 238 | ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0'); |
---|
| 239 | if (tokens.size() >= 2) |
---|
| 240 | { |
---|
[3196] | 241 | if (!convertValue(&(output->x), tokens[0])) |
---|
[2171] | 242 | return false; |
---|
[3196] | 243 | if (!convertValue(&(output->y), tokens[1])) |
---|
[2171] | 244 | return false; |
---|
| 245 | |
---|
| 246 | return true; |
---|
| 247 | } |
---|
| 248 | return false; |
---|
[2087] | 249 | } |
---|
| 250 | |
---|
[2171] | 251 | // std::string to Vector3 |
---|
| 252 | bool ConverterFallback<std::string, orxonox::Vector3>::convert(orxonox::Vector3* output, const std::string& input) |
---|
[2087] | 253 | { |
---|
[2171] | 254 | size_t opening_parenthesis, closing_parenthesis = input.find(')'); |
---|
| 255 | if ((opening_parenthesis = input.find('(')) == std::string::npos) |
---|
| 256 | opening_parenthesis = 0; |
---|
| 257 | else |
---|
| 258 | opening_parenthesis++; |
---|
[2087] | 259 | |
---|
[2171] | 260 | SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), |
---|
| 261 | ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0'); |
---|
| 262 | if (tokens.size() >= 3) |
---|
| 263 | { |
---|
[3196] | 264 | if (!convertValue(&(output->x), tokens[0])) |
---|
[2171] | 265 | return false; |
---|
[3196] | 266 | if (!convertValue(&(output->y), tokens[1])) |
---|
[2171] | 267 | return false; |
---|
[3196] | 268 | if (!convertValue(&(output->z), tokens[2])) |
---|
[2171] | 269 | return false; |
---|
| 270 | |
---|
| 271 | return true; |
---|
| 272 | } |
---|
| 273 | return false; |
---|
[2087] | 274 | } |
---|
| 275 | |
---|
[2171] | 276 | // std::string to Vector4 |
---|
| 277 | bool ConverterFallback<std::string, orxonox::Vector4>::convert(orxonox::Vector4* output, const std::string& input) |
---|
[2087] | 278 | { |
---|
[2171] | 279 | size_t opening_parenthesis, closing_parenthesis = input.find(')'); |
---|
| 280 | if ((opening_parenthesis = input.find('(')) == std::string::npos) |
---|
| 281 | opening_parenthesis = 0; |
---|
| 282 | else |
---|
| 283 | opening_parenthesis++; |
---|
[2087] | 284 | |
---|
[2171] | 285 | SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), |
---|
| 286 | ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0'); |
---|
| 287 | if (tokens.size() >= 4) |
---|
| 288 | { |
---|
[3196] | 289 | if (!convertValue(&(output->x), tokens[0])) |
---|
[2171] | 290 | return false; |
---|
[3196] | 291 | if (!convertValue(&(output->y), tokens[1])) |
---|
[2171] | 292 | return false; |
---|
[3196] | 293 | if (!convertValue(&(output->z), tokens[2])) |
---|
[2171] | 294 | return false; |
---|
[3196] | 295 | if (!convertValue(&(output->w), tokens[3])) |
---|
[2171] | 296 | return false; |
---|
| 297 | |
---|
| 298 | return true; |
---|
| 299 | } |
---|
| 300 | return false; |
---|
[2087] | 301 | } |
---|
| 302 | |
---|
[2171] | 303 | // std::string to Quaternion |
---|
| 304 | bool ConverterFallback<std::string, orxonox::Quaternion>::convert(orxonox::Quaternion* output, const std::string& input) |
---|
[2087] | 305 | { |
---|
[2171] | 306 | size_t opening_parenthesis, closing_parenthesis = input.find(')'); |
---|
| 307 | if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; } |
---|
[2087] | 308 | |
---|
[2171] | 309 | SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0'); |
---|
| 310 | if (tokens.size() >= 4) |
---|
| 311 | { |
---|
[3196] | 312 | if (!convertValue(&(output->w), tokens[0])) |
---|
[2171] | 313 | return false; |
---|
[3196] | 314 | if (!convertValue(&(output->x), tokens[1])) |
---|
[2171] | 315 | return false; |
---|
[3196] | 316 | if (!convertValue(&(output->y), tokens[2])) |
---|
[2171] | 317 | return false; |
---|
[3196] | 318 | if (!convertValue(&(output->z), tokens[3])) |
---|
[2171] | 319 | return false; |
---|
| 320 | |
---|
| 321 | return true; |
---|
| 322 | } |
---|
| 323 | return false; |
---|
[2087] | 324 | } |
---|
| 325 | |
---|
[2171] | 326 | // std::string to ColourValue |
---|
| 327 | bool ConverterFallback<std::string, orxonox::ColourValue>::convert(orxonox::ColourValue* output, const std::string& input) |
---|
| 328 | { |
---|
| 329 | size_t opening_parenthesis, closing_parenthesis = input.find(')'); |
---|
| 330 | if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; } |
---|
[2087] | 331 | |
---|
[2171] | 332 | SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0'); |
---|
| 333 | if (tokens.size() >= 3) |
---|
[2087] | 334 | { |
---|
[3196] | 335 | if (!convertValue(&(output->r), tokens[0])) |
---|
[2087] | 336 | return false; |
---|
[3196] | 337 | if (!convertValue(&(output->g), tokens[1])) |
---|
[2171] | 338 | return false; |
---|
[3196] | 339 | if (!convertValue(&(output->b), tokens[2])) |
---|
[2171] | 340 | return false; |
---|
| 341 | if (tokens.size() >= 4) |
---|
| 342 | { |
---|
[3196] | 343 | if (!convertValue(&(output->a), tokens[3])) |
---|
[2171] | 344 | return false; |
---|
| 345 | } |
---|
| 346 | else |
---|
| 347 | output->a = 1.0; |
---|
| 348 | |
---|
| 349 | return true; |
---|
[2087] | 350 | } |
---|
[2171] | 351 | return false; |
---|
[2087] | 352 | } |
---|
| 353 | } |
---|