Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ois_update/src/libraries/util/Math.cc @ 7532

Last change on this file since 7532 was 7532, checked in by rgrieder, 14 years ago

Adding some compiler errors to test OS X stuff

  • Property svn:eol-style set to native
File size: 14.2 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/**
30    @file
31    @brief Implementation of several math-functions.
32*/
33
34#include "Math.h"
35
36#include <OgrePlane.h>
37
38#include "MathConvert.h"
39#include "SubString.h"
40// Do not remove this include, it avoids linker errors.
41#include "mbool.h"
42
43#ifdef ORXONOX_ARCH_64
44class IsArch64 {};
45struct IsArch64 {};
46#else
47class IsArch32 {};
48struct IsArch32 {};
49#endif
50
51namespace orxonox
52{
53#if OGRE_VERSION < 0x010603
54    /**
55        @brief Function for writing a Radian to a stream.
56    */
57    std::ostream& operator<<(std::ostream& out, const orxonox::Radian& radian)
58    {
59        out << radian.valueRadians();
60        return out;
61    }
62
63    /**
64        @brief Function for writing a Degree to a stream.
65    */
66    std::ostream& operator<<(std::ostream& out, const orxonox::Degree& degree)
67    {
68        out << degree.valueDegrees();
69        return out;
70    }
71#endif
72
73    /**
74        @brief Function for reading a Radian from a stream.
75    */
76    std::istream& operator>>(std::istream& in, orxonox::Radian& radian)
77    {
78        float temp;
79        in >> temp;
80        radian = temp;
81        return in;
82    }
83
84    /**
85        @brief Function for reading a Degree from a stream.
86    */
87    std::istream& operator>>(std::istream& in, orxonox::Degree& degree)
88    {
89        float temp;
90        in >> temp;
91        degree = temp;
92        return in;
93    }
94
95
96    /**
97        @brief Gets the angle between my viewing direction and the direction to the position of the other object.
98        @param myposition My position
99        @param mydirection My viewing direction
100        @param otherposition The position of the other object
101        @return The angle in radian
102
103        Examples:
104         - If the other object is exactly in front of me, the function returns 0.
105         - If the other object is exactly behind me, the function returns pi.
106         - If the other object is exactly right/left to me (or above/below), the function returns pi/2.
107    */
108    float getAngle(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& otherposition)
109    {
110        orxonox::Vector3 distance = otherposition - myposition;
111        float distancelength = distance.length();
112        if (distancelength == 0)
113            return 0;
114        else
115            return acos(clamp<float>(mydirection.dotProduct(distance) / distancelength, -1, 1));
116    }
117
118    /**
119        @brief Gets the 2D viewing direction (up/down, left/right) to the position of the other object.
120        @param myposition My position
121        @param mydirection My viewing direction
122        @param myorthonormal My orthonormalvector (pointing upwards through my head)
123        @param otherposition The position of the other object
124        @return The viewing direction
125
126        Examples:
127         - If the other object is exactly in front of me, the function returns <tt>Vector2(0, 0)</tt>.
128         - If the other object is exactly at my left, the function returns <tt>Vector2(-1, 0)</tt>.
129         - If the other object is exactly at my right, the function returns <tt>Vector2(1, 0)</tt>.
130         - If the other object is only a bit at my right, the function still returns <tt>Vector2(1, 0)</tt>.
131         - If the other object is exactly above me, the function returns <tt>Vector2(0, 1)</tt>.
132    */
133    orxonox::Vector2 get2DViewdirection(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& myorthonormal, const orxonox::Vector3& otherposition)
134    {
135        orxonox::Vector3 distance = otherposition - myposition;
136
137        // project difference vector on our plane
138        orxonox::Vector3 projection = Ogre::Plane(mydirection, myposition).projectVector(distance);
139
140        float projectionlength = projection.length();
141        if (projectionlength == 0)
142        {
143            if (myposition.dotProduct(otherposition) >= 0)
144                return orxonox::Vector2(0, 0);
145            else
146                return orxonox::Vector2(0, 1);
147        }
148
149        float cos_value = clamp<float>(myorthonormal.dotProduct(projection) / projectionlength, -1, 1);
150        float sin_value = sqrt( 1 - cos_value*cos_value );
151
152        if ((mydirection.crossProduct(myorthonormal)).dotProduct(distance) > 0)
153            return orxonox::Vector2( sin_value, cos_value );
154        else
155            return orxonox::Vector2( -sin_value, cos_value );
156    }
157
158    /**
159        @brief Gets the 2D viewing direction (up/down, left/right) to the position of the other object, multiplied with the viewing distance to the object (0° = 0, 180° = 1).
160        @param myposition My position
161        @param mydirection My viewing direction
162        @param myorthonormal My orthonormalvector (pointing upwards through my head)
163        @param otherposition The position of the other object
164        @return The viewing direction
165
166        Examples:
167         - If the other object is exactly in front of me, the function returns <tt>Vector2(0, 0)</tt>.
168         - If the other object is exactly at my left, the function returns <tt>Vector2(-0.5, 0)</tt>.
169         - If the other object is exactly at my right, the function returns <tt>Vector2(0.5, 0)</tt>.
170         - If the other object is only a bit at my right, the function still returns <tt>Vector2(0.01, 0)</tt>.
171         - If the other object is exactly above me, the function returns <tt>Vector2(0, 0.5)</tt>.
172    */
173    orxonox::Vector2 get2DViewcoordinates(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& myorthonormal, const orxonox::Vector3& otherposition)
174    {
175        orxonox::Vector3 distance = otherposition - myposition;
176
177        // project difference vector on our plane
178        orxonox::Vector3 projection = Ogre::Plane(mydirection, myposition).projectVector(distance);
179
180        float projectionlength = projection.length();
181        if (projectionlength == 0)
182        {
183            if (myposition.dotProduct(otherposition) >= 0)
184                return orxonox::Vector2(0, 0);
185            else
186                return orxonox::Vector2(0, 1);
187        }
188        //float angle = acos(clamp<float>(myorthonormal.dotProduct(projection) / projectionlength, -1, 1));
189
190        float cos_value = clamp<float>(myorthonormal.dotProduct(projection) / projectionlength, -1, 1);
191        float sin_value = sqrt( 1 - cos_value*cos_value );
192
193        float distancelength = distance.length();
194        if (distancelength == 0) return orxonox::Vector2(0, 0);
195        float radius = acos(clamp<float>(mydirection.dotProduct(distance) / distancelength, -1, 1)) / math::pi;
196
197        if ((mydirection.crossProduct(myorthonormal)).dotProduct(distance) > 0)
198            return orxonox::Vector2( sin_value * radius, cos_value * radius);
199        else
200            return orxonox::Vector2( -sin_value * radius, cos_value * radius);
201    }
202
203    /**
204        @brief Returns the predicted position I have to aim at, if I want to hit a moving target with a moving projectile.
205        @param myposition My position
206        @param projectilespeed The speed of my projectile
207        @param targetposition The position of my target
208        @param targetvelocity The velocity of my target
209        @return The predicted position
210
211        The function predicts the position based on a linear velocity of the target. If the target changes speed or direction, the projectile will miss.
212    */
213    orxonox::Vector3 getPredictedPosition(const orxonox::Vector3& myposition, float projectilespeed, const orxonox::Vector3& targetposition, const orxonox::Vector3& targetvelocity)
214    {
215        float squaredProjectilespeed = projectilespeed * projectilespeed;
216        orxonox::Vector3 distance = targetposition - myposition;
217        float a = distance.squaredLength();
218        float b = 2 * (distance.x + distance.y + distance.z) * (targetvelocity.x + targetvelocity.y + targetvelocity.z);
219        float c = targetvelocity.squaredLength();
220
221        float temp = 4*squaredProjectilespeed*c + a*a - 4*b*c;
222        if (temp < 0)
223            return orxonox::Vector3::ZERO;
224
225        temp = sqrt(temp);
226        float time = (temp + a) / (2 * (squaredProjectilespeed - b));
227        return (targetposition + targetvelocity * time);
228    }
229
230    /**
231        @brief Returns a unique number. This function will never return the same value twice.
232    */
233    unsigned long getUniqueNumber()
234    {
235        static unsigned long aNumber = 135;
236        return aNumber++;
237    }
238
239
240    //////////////////////////
241    // Conversion functions //
242    //////////////////////////
243
244    // std::string to Vector2
245    bool ConverterFallback<std::string, orxonox::Vector2>::convert(orxonox::Vector2* output, const std::string& input)
246    {
247        size_t opening_parenthesis, closing_parenthesis = input.find('}');
248        if ((opening_parenthesis = input.find('{')) == std::string::npos)
249            opening_parenthesis = 0;
250        else
251            opening_parenthesis++;
252
253        SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis),
254                         ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0');
255        if (tokens.size() >= 2)
256        {
257            if (!convertValue(&(output->x), tokens[0]))
258                return false;
259            if (!convertValue(&(output->y), tokens[1]))
260                return false;
261
262            return true;
263        }
264        return false;
265    }
266
267    // std::string to Vector3
268    bool ConverterFallback<std::string, orxonox::Vector3>::convert(orxonox::Vector3* output, const std::string& input)
269    {
270        size_t opening_parenthesis, closing_parenthesis = input.find('}');
271        if ((opening_parenthesis = input.find('{')) == std::string::npos)
272            opening_parenthesis = 0;
273        else
274            opening_parenthesis++;
275
276        SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis),
277                         ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0');
278        if (tokens.size() >= 3)
279        {
280            if (!convertValue(&(output->x), tokens[0]))
281                return false;
282            if (!convertValue(&(output->y), tokens[1]))
283                return false;
284            if (!convertValue(&(output->z), tokens[2]))
285                return false;
286
287            return true;
288        }
289        return false;
290    }
291
292    // std::string to Vector4
293    bool ConverterFallback<std::string, orxonox::Vector4>::convert(orxonox::Vector4* output, const std::string& input)
294    {
295        size_t opening_parenthesis, closing_parenthesis = input.find('}');
296        if ((opening_parenthesis = input.find('{')) == std::string::npos)
297            opening_parenthesis = 0;
298        else
299            opening_parenthesis++;
300
301        SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis),
302                         ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0');
303        if (tokens.size() >= 4)
304        {
305            if (!convertValue(&(output->x), tokens[0]))
306                return false;
307            if (!convertValue(&(output->y), tokens[1]))
308                return false;
309            if (!convertValue(&(output->z), tokens[2]))
310                return false;
311            if (!convertValue(&(output->w), tokens[3]))
312                return false;
313
314            return true;
315        }
316        return false;
317    }
318
319    // std::string to Quaternion
320    bool ConverterFallback<std::string, orxonox::Quaternion>::convert(orxonox::Quaternion* output, const std::string& input)
321    {
322        size_t opening_parenthesis, closing_parenthesis = input.find('}');
323        if ((opening_parenthesis = input.find('{')) == std::string::npos)
324            opening_parenthesis = 0;
325        else
326            opening_parenthesis++;
327
328        SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0');
329        if (tokens.size() >= 4)
330        {
331            if (!convertValue(&(output->w), tokens[0]))
332                return false;
333            if (!convertValue(&(output->x), tokens[1]))
334                return false;
335            if (!convertValue(&(output->y), tokens[2]))
336                return false;
337            if (!convertValue(&(output->z), tokens[3]))
338                return false;
339
340            return true;
341        }
342        return false;
343    }
344
345    // std::string to ColourValue
346    bool ConverterFallback<std::string, orxonox::ColourValue>::convert(orxonox::ColourValue* output, const std::string& input)
347    {
348        size_t opening_parenthesis, closing_parenthesis = input.find('}');
349        if ((opening_parenthesis = input.find('{')) == std::string::npos)
350            opening_parenthesis = 0;
351        else
352            opening_parenthesis++;
353
354        SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0');
355        if (tokens.size() >= 3)
356        {
357            if (!convertValue(&(output->r), tokens[0]))
358                return false;
359            if (!convertValue(&(output->g), tokens[1]))
360                return false;
361            if (!convertValue(&(output->b), tokens[2]))
362                return false;
363            if (tokens.size() >= 4)
364            {
365                if (!convertValue(&(output->a), tokens[3]))
366                    return false;
367            }
368            else
369                output->a = 1.0;
370
371            return true;
372        }
373        return false;
374    }
375}
Note: See TracBrowser for help on using the repository browser.