Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/hud/src/util/Math.h @ 1588

Last change on this file since 1588 was 1588, checked in by rgrieder, 16 years ago
  • added XML loadable HUD
  • Radar and navi are not yet done
  • explanations follow with when things are finished
  • Property svn:eol-style set to native
File size: 4.3 KB
Line 
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 <OgreMatrix4.h>
42#include <OgreQuaternion.h>
43#include <OgreColourValue.h>
44
45namespace 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::Matrix4 Matrix4;
54  typedef Ogre::Quaternion Quaternion;
55  typedef Ogre::ColourValue ColourValue;
56}
57
58_UtilExport std::ostream& operator<<(std::ostream& out, const orxonox::Radian& radian);
59_UtilExport std::istream& operator>>(std::istream& in, orxonox::Radian& radian);
60_UtilExport std::ostream& operator<<(std::ostream& out, const orxonox::Degree& degree);
61_UtilExport std::istream& operator>>(std::istream& in, orxonox::Degree& degree);
62
63_UtilExport float getAngle(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& otherposition);
64_UtilExport orxonox::Vector2 get2DViewdirection(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& myorthonormal, const orxonox::Vector3& otherposition);
65_UtilExport orxonox::Vector2 get2DViewcoordinates(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& myorthonormal, const orxonox::Vector3& otherposition);
66_UtilExport orxonox::Vector3 getPredictedPosition(const orxonox::Vector3& myposition, float projectilespeed, const orxonox::Vector3& targetposition, const orxonox::Vector3& targetvelocity);
67
68template <typename T>
69inline T sgn(T x)
70{
71    return (x >= 0) ? 1 : -1;
72}
73
74template <typename T>
75inline T min(T a, T b)
76{
77    return (a <= b) ? a : b;
78}
79
80template <typename T>
81inline T max(T a, T b)
82{
83    return (a >= b) ? a : b;
84}
85
86template <typename T>
87inline T clamp(T x, T min, T max)
88{
89    if (x < min)
90        return min;
91
92    if (x > max)
93        return max;
94
95    return x;
96}
97
98template <typename T>
99inline T square(T x)
100{
101    return x*x;
102}
103
104template <typename T>
105inline T cube(T x)
106{
107    return x*x*x;
108}
109
110template <typename T>
111inline int floor(T x)
112{
113    return (int)(x);
114}
115
116template <typename T>
117inline int ceil(T x)
118{
119    int temp = floor(x);
120    return (temp != x) ? (temp + 1) : temp;
121}
122
123template <typename T>
124inline int round(T x)
125{
126    return (int)(x + 0.5);
127}
128
129template <typename T>
130inline int mod(T x, int max)
131{
132    if (x >= 0)
133        return (x % max);
134    else
135        return ((x % max) + max);
136}
137
138template <typename T>
139T interpolate(float time, const T& start, const T& end)
140{
141    return time * (end - start) + start;
142}
143
144template <typename T>
145T interpolateSmooth(float time, const T& start, const T& end)
146{
147    return (-2 * (end - start) * cube(time)) + (3 * (end - start) * square(time)) + start;
148}
149
150inline _UtilExport float rnd()
151{
152    return ((float)rand() / RAND_MAX);
153}
154
155inline _UtilExport float rnd(float max)
156{
157    return rnd() * max;
158}
159
160inline _UtilExport float rnd(float min, float max)
161{
162    return rnd(max - min) + min;
163}
164
165class _UtilExport IntVector2
166{
167public:
168  IntVector2() : x(0), y(0) { }
169  IntVector2(int _x, int _y) : x(_x), y(_y) { }
170  int x;
171  int y;
172};
173
174class _UtilExport IntVector3
175{
176public:
177  IntVector3() : x(0), y(0), z(0) { }
178  IntVector3(int _x, int _y, int _z) : x(_x), y(_y), z(_z) { }
179  int x;
180  int y;
181  int z;
182};
183
184#endif /* _Util_Math_H__ */
Note: See TracBrowser for help on using the repository browser.