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