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
RevLine 
[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
[1791]29/**
[2087]30    @file
[1791]31    @brief Implementation of several math-functions.
32*/
33
[2087]34#include "Math.h"
35
[1564]36#include <OgrePlane.h>
[3196]37
[2087]38#include "MathConvert.h"
39#include "SubString.h"
[3196]40// Do not remove this include, it avoids linker errors.
[2171]41#include "mbool.h"
[1505]42
[7532]43#ifdef ORXONOX_ARCH_64
44class IsArch64 {};
45struct IsArch64 {};
46#else
47class IsArch32 {};
48struct IsArch32 {};
49#endif
50
[2171]51namespace orxonox
[1505]52{
[6417]53#if OGRE_VERSION < 0x010603
[2171]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    }
[1505]62
[2171]63    /**
[6417]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    /**
[2171]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    }
[1505]83
[2171]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    }
[1564]94
[1791]95
[2171]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
[7401]101        @return The angle in radian
[1564]102
[7401]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.
[2171]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    }
[1791]117
[2171]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
[1564]125
[7401]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>.
[2171]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;
[1564]136
[2171]137        // project difference vector on our plane
138        orxonox::Vector3 projection = Ogre::Plane(mydirection, myposition).projectVector(distance);
[1608]139
[2171]140        float projectionlength = projection.length();
[3049]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        }
[6417]148
[3304]149        float cos_value = clamp<float>(myorthonormal.dotProduct(projection) / projectionlength, -1, 1);
150        float sin_value = sqrt( 1 - cos_value*cos_value );
[6417]151
[2171]152        if ((mydirection.crossProduct(myorthonormal)).dotProduct(distance) > 0)
[3304]153            return orxonox::Vector2( sin_value, cos_value );
[2171]154        else
[3304]155            return orxonox::Vector2( -sin_value, cos_value );
[2171]156    }
[1791]157
[2171]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
[1564]165
[7401]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>.
[2171]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;
[1564]176
[2171]177        // project difference vector on our plane
178        orxonox::Vector3 projection = Ogre::Plane(mydirection, myposition).projectVector(distance);
[1608]179
[2171]180        float projectionlength = projection.length();
[3049]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        }
[3304]188        //float angle = acos(clamp<float>(myorthonormal.dotProduct(projection) / projectionlength, -1, 1));
[6417]189
[3304]190        float cos_value = clamp<float>(myorthonormal.dotProduct(projection) / projectionlength, -1, 1);
191        float sin_value = sqrt( 1 - cos_value*cos_value );
[1608]192
[2171]193        float distancelength = distance.length();
194        if (distancelength == 0) return orxonox::Vector2(0, 0);
[7184]195        float radius = acos(clamp<float>(mydirection.dotProduct(distance) / distancelength, -1, 1)) / math::pi;
[1566]196
[2171]197        if ((mydirection.crossProduct(myorthonormal)).dotProduct(distance) > 0)
[3304]198            return orxonox::Vector2( sin_value * radius, cos_value * radius);
[2171]199        else
[3304]200            return orxonox::Vector2( -sin_value * radius, cos_value * radius);
[2171]201    }
[1791]202
[2171]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
[1566]210
[2171]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();
[1566]220
[2171]221        float temp = 4*squaredProjectilespeed*c + a*a - 4*b*c;
222        if (temp < 0)
223            return orxonox::Vector3::ZERO;
[1625]224
[2171]225        temp = sqrt(temp);
226        float time = (temp + a) / (2 * (squaredProjectilespeed - b));
227        return (targetposition + targetvelocity * time);
228    }
[1625]229
[7401]230    /**
231        @brief Returns a unique number. This function will never return the same value twice.
232    */
[2171]233    unsigned long getUniqueNumber()
234    {
235        static unsigned long aNumber = 135;
236        return aNumber++;
237    }
[2087]238
239
[2171]240    //////////////////////////
241    // Conversion functions //
242    //////////////////////////
[2087]243
[2171]244    // std::string to Vector2
245    bool ConverterFallback<std::string, orxonox::Vector2>::convert(orxonox::Vector2* output, const std::string& input)
[2087]246    {
[7284]247        size_t opening_parenthesis, closing_parenthesis = input.find('}');
248        if ((opening_parenthesis = input.find('{')) == std::string::npos)
[2171]249            opening_parenthesis = 0;
250        else
251            opening_parenthesis++;
[2087]252
[2171]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        {
[3196]257            if (!convertValue(&(output->x), tokens[0]))
[2171]258                return false;
[3196]259            if (!convertValue(&(output->y), tokens[1]))
[2171]260                return false;
261
262            return true;
263        }
264        return false;
[2087]265    }
266
[2171]267    // std::string to Vector3
268    bool ConverterFallback<std::string, orxonox::Vector3>::convert(orxonox::Vector3* output, const std::string& input)
[2087]269    {
[7284]270        size_t opening_parenthesis, closing_parenthesis = input.find('}');
271        if ((opening_parenthesis = input.find('{')) == std::string::npos)
[2171]272            opening_parenthesis = 0;
273        else
274            opening_parenthesis++;
[2087]275
[2171]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        {
[3196]280            if (!convertValue(&(output->x), tokens[0]))
[2171]281                return false;
[3196]282            if (!convertValue(&(output->y), tokens[1]))
[2171]283                return false;
[3196]284            if (!convertValue(&(output->z), tokens[2]))
[2171]285                return false;
286
287            return true;
288        }
289        return false;
[2087]290    }
291
[2171]292    // std::string to Vector4
293    bool ConverterFallback<std::string, orxonox::Vector4>::convert(orxonox::Vector4* output, const std::string& input)
[2087]294    {
[7284]295        size_t opening_parenthesis, closing_parenthesis = input.find('}');
296        if ((opening_parenthesis = input.find('{')) == std::string::npos)
[2171]297            opening_parenthesis = 0;
298        else
299            opening_parenthesis++;
[2087]300
[2171]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        {
[3196]305            if (!convertValue(&(output->x), tokens[0]))
[2171]306                return false;
[3196]307            if (!convertValue(&(output->y), tokens[1]))
[2171]308                return false;
[3196]309            if (!convertValue(&(output->z), tokens[2]))
[2171]310                return false;
[3196]311            if (!convertValue(&(output->w), tokens[3]))
[2171]312                return false;
313
314            return true;
315        }
316        return false;
[2087]317    }
318
[2171]319    // std::string to Quaternion
320    bool ConverterFallback<std::string, orxonox::Quaternion>::convert(orxonox::Quaternion* output, const std::string& input)
[2087]321    {
[7284]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++;
[2087]327
[2171]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        {
[3196]331            if (!convertValue(&(output->w), tokens[0]))
[2171]332                return false;
[3196]333            if (!convertValue(&(output->x), tokens[1]))
[2171]334                return false;
[3196]335            if (!convertValue(&(output->y), tokens[2]))
[2171]336                return false;
[3196]337            if (!convertValue(&(output->z), tokens[3]))
[2171]338                return false;
339
340            return true;
341        }
342        return false;
[2087]343    }
344
[2171]345    // std::string to ColourValue
346    bool ConverterFallback<std::string, orxonox::ColourValue>::convert(orxonox::ColourValue* output, const std::string& input)
347    {
[7284]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++;
[2087]353
[2171]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)
[2087]356        {
[3196]357            if (!convertValue(&(output->r), tokens[0]))
[2087]358                return false;
[3196]359            if (!convertValue(&(output->g), tokens[1]))
[2171]360                return false;
[3196]361            if (!convertValue(&(output->b), tokens[2]))
[2171]362                return false;
363            if (tokens.size() >= 4)
364            {
[3196]365                if (!convertValue(&(output->a), tokens[3]))
[2171]366                    return false;
367            }
368            else
369                output->a = 1.0;
370
371            return true;
[2087]372        }
[2171]373        return false;
[2087]374    }
375}
Note: See TracBrowser for help on using the repository browser.