[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 | |
---|
| 29 | #ifndef _Util_Math_H__ |
---|
| 30 | #define _Util_Math_H__ |
---|
| 31 | |
---|
| 32 | #include "UtilPrereqs.h" |
---|
| 33 | |
---|
| 34 | #include <ostream> |
---|
[1625] | 35 | #include <string> |
---|
[1505] | 36 | |
---|
| 37 | #include <OgreMath.h> |
---|
| 38 | #include <OgreVector2.h> |
---|
| 39 | #include <OgreVector3.h> |
---|
| 40 | #include <OgreVector4.h> |
---|
| 41 | #include <OgreMatrix3.h> |
---|
[1625] | 42 | #include <OgreMatrix4.h> |
---|
[1505] | 43 | #include <OgreQuaternion.h> |
---|
| 44 | #include <OgreColourValue.h> |
---|
| 45 | |
---|
| 46 | namespace orxonox |
---|
| 47 | { |
---|
| 48 | typedef Ogre::Radian Radian; |
---|
| 49 | typedef Ogre::Degree Degree; |
---|
| 50 | typedef Ogre::Vector2 Vector2; |
---|
| 51 | typedef Ogre::Vector3 Vector3; |
---|
| 52 | typedef Ogre::Vector4 Vector4; |
---|
| 53 | typedef Ogre::Matrix3 Matrix3; |
---|
[1625] | 54 | typedef Ogre::Matrix4 Matrix4; |
---|
[1505] | 55 | typedef Ogre::Quaternion Quaternion; |
---|
| 56 | typedef Ogre::ColourValue ColourValue; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | _UtilExport std::ostream& operator<<(std::ostream& out, const orxonox::Radian& radian); |
---|
| 60 | _UtilExport std::istream& operator>>(std::istream& in, orxonox::Radian& radian); |
---|
| 61 | _UtilExport std::ostream& operator<<(std::ostream& out, const orxonox::Degree& degree); |
---|
| 62 | _UtilExport std::istream& operator>>(std::istream& in, orxonox::Degree& degree); |
---|
| 63 | |
---|
[1564] | 64 | _UtilExport float getAngle(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& otherposition); |
---|
| 65 | _UtilExport orxonox::Vector2 get2DViewdirection(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& myorthonormal, const orxonox::Vector3& otherposition); |
---|
| 66 | _UtilExport orxonox::Vector2 get2DViewcoordinates(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& myorthonormal, const orxonox::Vector3& otherposition); |
---|
[1566] | 67 | _UtilExport orxonox::Vector3 getPredictedPosition(const orxonox::Vector3& myposition, float projectilespeed, const orxonox::Vector3& targetposition, const orxonox::Vector3& targetvelocity); |
---|
[1564] | 68 | |
---|
[1505] | 69 | template <typename T> |
---|
| 70 | inline T sgn(T x) |
---|
| 71 | { |
---|
| 72 | return (x >= 0) ? 1 : -1; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | template <typename T> |
---|
| 76 | inline T min(T a, T b) |
---|
| 77 | { |
---|
| 78 | return (a <= b) ? a : b; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | template <typename T> |
---|
| 82 | inline T max(T a, T b) |
---|
| 83 | { |
---|
| 84 | return (a >= b) ? a : b; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | template <typename T> |
---|
| 88 | inline T clamp(T x, T min, T max) |
---|
| 89 | { |
---|
| 90 | if (x < min) |
---|
| 91 | return min; |
---|
| 92 | |
---|
| 93 | if (x > max) |
---|
| 94 | return max; |
---|
| 95 | |
---|
| 96 | return x; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | template <typename T> |
---|
| 100 | inline T square(T x) |
---|
| 101 | { |
---|
| 102 | return x*x; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | template <typename T> |
---|
| 106 | inline T cube(T x) |
---|
| 107 | { |
---|
| 108 | return x*x*x; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | template <typename T> |
---|
| 112 | inline int floor(T x) |
---|
| 113 | { |
---|
| 114 | return (int)(x); |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | template <typename T> |
---|
| 118 | inline int ceil(T x) |
---|
| 119 | { |
---|
| 120 | int temp = floor(x); |
---|
| 121 | return (temp != x) ? (temp + 1) : temp; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | template <typename T> |
---|
| 125 | inline int round(T x) |
---|
| 126 | { |
---|
| 127 | return (int)(x + 0.5); |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | template <typename T> |
---|
| 131 | inline int mod(T x, int max) |
---|
| 132 | { |
---|
| 133 | if (x >= 0) |
---|
| 134 | return (x % max); |
---|
| 135 | else |
---|
| 136 | return ((x % max) + max); |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | template <typename T> |
---|
| 140 | T interpolate(float time, const T& start, const T& end) |
---|
| 141 | { |
---|
| 142 | return time * (end - start) + start; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | template <typename T> |
---|
| 146 | T interpolateSmooth(float time, const T& start, const T& end) |
---|
| 147 | { |
---|
| 148 | return (-2 * (end - start) * cube(time)) + (3 * (end - start) * square(time)) + start; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | inline _UtilExport float rnd() |
---|
| 152 | { |
---|
| 153 | return ((float)rand() / RAND_MAX); |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | inline _UtilExport float rnd(float max) |
---|
| 157 | { |
---|
| 158 | return rnd() * max; |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | inline _UtilExport float rnd(float min, float max) |
---|
| 162 | { |
---|
| 163 | return rnd(max - min) + min; |
---|
| 164 | } |
---|
| 165 | |
---|
[1625] | 166 | _UtilExport unsigned long getUniqueNumber(); |
---|
| 167 | _UtilExport std::string getUniqueNumberStr(); |
---|
| 168 | |
---|
[1505] | 169 | class _UtilExport IntVector2 |
---|
| 170 | { |
---|
| 171 | public: |
---|
| 172 | IntVector2() : x(0), y(0) { } |
---|
| 173 | IntVector2(int _x, int _y) : x(_x), y(_y) { } |
---|
| 174 | int x; |
---|
| 175 | int y; |
---|
| 176 | }; |
---|
| 177 | |
---|
| 178 | class _UtilExport IntVector3 |
---|
| 179 | { |
---|
| 180 | public: |
---|
| 181 | IntVector3() : x(0), y(0), z(0) { } |
---|
| 182 | IntVector3(int _x, int _y, int _z) : x(_x), y(_y), z(_z) { } |
---|
| 183 | int x; |
---|
| 184 | int y; |
---|
| 185 | int z; |
---|
| 186 | }; |
---|
| 187 | |
---|
| 188 | #endif /* _Util_Math_H__ */ |
---|