[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 | /** |
---|
| 30 | @file Math.h |
---|
| 31 | @brief Declaration and implementation of several math-functions, typedefs of some Ogre::Math classes to the orxonox namespace. |
---|
| 32 | */ |
---|
| 33 | |
---|
[1505] | 34 | #ifndef _Util_Math_H__ |
---|
| 35 | #define _Util_Math_H__ |
---|
| 36 | |
---|
| 37 | #include "UtilPrereqs.h" |
---|
| 38 | |
---|
| 39 | #include <ostream> |
---|
[1625] | 40 | #include <string> |
---|
[1505] | 41 | |
---|
| 42 | #include <OgreMath.h> |
---|
| 43 | #include <OgreVector2.h> |
---|
| 44 | #include <OgreVector3.h> |
---|
| 45 | #include <OgreVector4.h> |
---|
| 46 | #include <OgreMatrix3.h> |
---|
[1625] | 47 | #include <OgreMatrix4.h> |
---|
[1505] | 48 | #include <OgreQuaternion.h> |
---|
| 49 | #include <OgreColourValue.h> |
---|
| 50 | |
---|
| 51 | namespace orxonox |
---|
| 52 | { |
---|
| 53 | typedef Ogre::Radian Radian; |
---|
| 54 | typedef Ogre::Degree Degree; |
---|
| 55 | typedef Ogre::Vector2 Vector2; |
---|
| 56 | typedef Ogre::Vector3 Vector3; |
---|
| 57 | typedef Ogre::Vector4 Vector4; |
---|
| 58 | typedef Ogre::Matrix3 Matrix3; |
---|
[1625] | 59 | typedef Ogre::Matrix4 Matrix4; |
---|
[1505] | 60 | typedef Ogre::Quaternion Quaternion; |
---|
| 61 | typedef Ogre::ColourValue ColourValue; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | _UtilExport std::ostream& operator<<(std::ostream& out, const orxonox::Radian& radian); |
---|
| 65 | _UtilExport std::istream& operator>>(std::istream& in, orxonox::Radian& radian); |
---|
| 66 | _UtilExport std::ostream& operator<<(std::ostream& out, const orxonox::Degree& degree); |
---|
| 67 | _UtilExport std::istream& operator>>(std::istream& in, orxonox::Degree& degree); |
---|
| 68 | |
---|
[1564] | 69 | _UtilExport float getAngle(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& otherposition); |
---|
| 70 | _UtilExport orxonox::Vector2 get2DViewdirection(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& myorthonormal, const orxonox::Vector3& otherposition); |
---|
| 71 | _UtilExport orxonox::Vector2 get2DViewcoordinates(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& myorthonormal, const orxonox::Vector3& otherposition); |
---|
[1566] | 72 | _UtilExport orxonox::Vector3 getPredictedPosition(const orxonox::Vector3& myposition, float projectilespeed, const orxonox::Vector3& targetposition, const orxonox::Vector3& targetvelocity); |
---|
[1564] | 73 | |
---|
[1781] | 74 | //Get around Windows hackery |
---|
[1791] | 75 | #if ORXONOX_PLATFORM == ORXONOX_PLATFORM_WIN32 |
---|
[1781] | 76 | # ifdef max |
---|
| 77 | # undef max |
---|
| 78 | # endif |
---|
| 79 | # ifdef min |
---|
| 80 | # undef min |
---|
| 81 | # endif |
---|
| 82 | #endif |
---|
| 83 | |
---|
[1791] | 84 | /** |
---|
| 85 | @brief Returns the sign of the given value. |
---|
| 86 | @param x The value |
---|
| 87 | @return 1 if the value is positive or zero, -1 if the value is negative |
---|
| 88 | */ |
---|
[1505] | 89 | template <typename T> |
---|
| 90 | inline T sgn(T x) |
---|
| 91 | { |
---|
| 92 | return (x >= 0) ? 1 : -1; |
---|
| 93 | } |
---|
| 94 | |
---|
[1791] | 95 | /** |
---|
| 96 | @brief Returns the smaller of two values. |
---|
| 97 | */ |
---|
[1505] | 98 | template <typename T> |
---|
| 99 | inline T min(T a, T b) |
---|
| 100 | { |
---|
| 101 | return (a <= b) ? a : b; |
---|
| 102 | } |
---|
| 103 | |
---|
[1791] | 104 | /** |
---|
| 105 | @brief Returns the greater of two values. |
---|
| 106 | */ |
---|
[1505] | 107 | template <typename T> |
---|
| 108 | inline T max(T a, T b) |
---|
| 109 | { |
---|
| 110 | return (a >= b) ? a : b; |
---|
| 111 | } |
---|
| 112 | |
---|
[1791] | 113 | /** |
---|
| 114 | @brief Keeps a value between a lower and an upper limit. |
---|
| 115 | @param x The value |
---|
| 116 | @param min The lower limit |
---|
| 117 | @param max The upper limit |
---|
| 118 | */ |
---|
[1505] | 119 | template <typename T> |
---|
| 120 | inline T clamp(T x, T min, T max) |
---|
| 121 | { |
---|
| 122 | if (x < min) |
---|
| 123 | return min; |
---|
| 124 | |
---|
| 125 | if (x > max) |
---|
| 126 | return max; |
---|
| 127 | |
---|
| 128 | return x; |
---|
| 129 | } |
---|
| 130 | |
---|
[1791] | 131 | /** |
---|
| 132 | @brief Returns the square value (x^2). |
---|
| 133 | */ |
---|
[1505] | 134 | template <typename T> |
---|
| 135 | inline T square(T x) |
---|
| 136 | { |
---|
| 137 | return x*x; |
---|
| 138 | } |
---|
| 139 | |
---|
[1791] | 140 | /** |
---|
| 141 | @brief Returns the cube value (x^3). |
---|
| 142 | */ |
---|
[1505] | 143 | template <typename T> |
---|
| 144 | inline T cube(T x) |
---|
| 145 | { |
---|
| 146 | return x*x*x; |
---|
| 147 | } |
---|
| 148 | |
---|
[1791] | 149 | /** |
---|
| 150 | @brief Rounds the value down. |
---|
| 151 | */ |
---|
[1505] | 152 | template <typename T> |
---|
| 153 | inline int floor(T x) |
---|
| 154 | { |
---|
| 155 | return (int)(x); |
---|
| 156 | } |
---|
| 157 | |
---|
[1791] | 158 | /** |
---|
| 159 | @brief Rounds the value up. |
---|
| 160 | */ |
---|
[1505] | 161 | template <typename T> |
---|
| 162 | inline int ceil(T x) |
---|
| 163 | { |
---|
| 164 | int temp = floor(x); |
---|
| 165 | return (temp != x) ? (temp + 1) : temp; |
---|
| 166 | } |
---|
| 167 | |
---|
[1791] | 168 | /** |
---|
| 169 | @brief Rounds the value. |
---|
| 170 | */ |
---|
[1505] | 171 | template <typename T> |
---|
| 172 | inline int round(T x) |
---|
| 173 | { |
---|
| 174 | return (int)(x + 0.5); |
---|
| 175 | } |
---|
| 176 | |
---|
[1791] | 177 | /** |
---|
| 178 | @brief The modulo operation, enhanced to work properly with negative values. |
---|
| 179 | @param x The value |
---|
| 180 | @param max The operand |
---|
| 181 | */ |
---|
[1505] | 182 | template <typename T> |
---|
| 183 | inline int mod(T x, int max) |
---|
| 184 | { |
---|
| 185 | if (x >= 0) |
---|
| 186 | return (x % max); |
---|
| 187 | else |
---|
| 188 | return ((x % max) + max); |
---|
| 189 | } |
---|
| 190 | |
---|
[1791] | 191 | /** |
---|
| 192 | @brief Interpolates between two values for a time between 0 and 1. |
---|
| 193 | @param time The time is a value between 0 and 1 - the function returns start if time is 0 and end if time is 1 and interpolates if time is between 0 and 1. |
---|
| 194 | @param start The value at time = 0 |
---|
| 195 | @param end The value at time = 1 |
---|
| 196 | @return The interpolation at a given time |
---|
| 197 | */ |
---|
[1505] | 198 | template <typename T> |
---|
| 199 | T interpolate(float time, const T& start, const T& end) |
---|
| 200 | { |
---|
| 201 | return time * (end - start) + start; |
---|
| 202 | } |
---|
| 203 | |
---|
[1791] | 204 | /** |
---|
| 205 | @brief Interpolates smoothly between two values for a time between 0 and 1. The function starts slowly, increases faster and stops slowly again. |
---|
| 206 | @param time The time is a value between 0 and 1 - the function returns start if time is 0 and end if time is 1 and interpolates if time is between 0 and 1. |
---|
| 207 | @param start The value at time = 0 |
---|
| 208 | @param end The value at time = 1 |
---|
| 209 | @return The smoothed interpolation at a given time |
---|
| 210 | */ |
---|
[1505] | 211 | template <typename T> |
---|
| 212 | T interpolateSmooth(float time, const T& start, const T& end) |
---|
| 213 | { |
---|
| 214 | return (-2 * (end - start) * cube(time)) + (3 * (end - start) * square(time)) + start; |
---|
| 215 | } |
---|
| 216 | |
---|
[1791] | 217 | /** |
---|
| 218 | @brief Returns a random number between 0 and 1. |
---|
| 219 | */ |
---|
[1505] | 220 | inline _UtilExport float rnd() |
---|
| 221 | { |
---|
| 222 | return ((float)rand() / RAND_MAX); |
---|
| 223 | } |
---|
| 224 | |
---|
[1791] | 225 | /** |
---|
| 226 | @brief Returns a random number between 0 and max. |
---|
| 227 | @param max The maximum |
---|
| 228 | */ |
---|
[1505] | 229 | inline _UtilExport float rnd(float max) |
---|
| 230 | { |
---|
| 231 | return rnd() * max; |
---|
| 232 | } |
---|
| 233 | |
---|
[1791] | 234 | /** |
---|
| 235 | @brief Returns a random number between min and max. |
---|
| 236 | @param min The minimum |
---|
| 237 | @param max The maximum |
---|
| 238 | */ |
---|
[1505] | 239 | inline _UtilExport float rnd(float min, float max) |
---|
| 240 | { |
---|
| 241 | return rnd(max - min) + min; |
---|
| 242 | } |
---|
| 243 | |
---|
[1625] | 244 | _UtilExport unsigned long getUniqueNumber(); |
---|
| 245 | _UtilExport std::string getUniqueNumberStr(); |
---|
| 246 | |
---|
[1505] | 247 | class _UtilExport IntVector2 |
---|
| 248 | { |
---|
| 249 | public: |
---|
| 250 | IntVector2() : x(0), y(0) { } |
---|
| 251 | IntVector2(int _x, int _y) : x(_x), y(_y) { } |
---|
| 252 | int x; |
---|
| 253 | int y; |
---|
| 254 | }; |
---|
| 255 | |
---|
| 256 | class _UtilExport IntVector3 |
---|
| 257 | { |
---|
| 258 | public: |
---|
| 259 | IntVector3() : x(0), y(0), z(0) { } |
---|
| 260 | IntVector3(int _x, int _y, int _z) : x(_x), y(_y), z(_z) { } |
---|
| 261 | int x; |
---|
| 262 | int y; |
---|
| 263 | int z; |
---|
| 264 | }; |
---|
| 265 | |
---|
| 266 | #endif /* _Util_Math_H__ */ |
---|