Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core3/src/util/Math.h @ 1624

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