Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/radarDreiD/src/libraries/util/Math.cc @ 9779

Last change on this file since 9779 was 9779, checked in by wroennin, 11 years ago

Math.cc: updated (new comments, transformmatrix changed to adapt to the right axis directions

  • Property svn:eol-style set to native
File size: 18.1 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:
[9719]25 *      Wolfgang Roenninger
[1505]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"
[1505]40
[2171]41namespace orxonox
[1505]42{
[6417]43#if OGRE_VERSION < 0x010603
[2171]44    /**
45        @brief Function for writing a Radian to a stream.
46    */
47    std::ostream& operator<<(std::ostream& out, const orxonox::Radian& radian)
48    {
49        out << radian.valueRadians();
50        return out;
51    }
[1505]52
[2171]53    /**
[6417]54        @brief Function for writing a Degree to a stream.
55    */
56    std::ostream& operator<<(std::ostream& out, const orxonox::Degree& degree)
57    {
58        out << degree.valueDegrees();
59        return out;
60    }
61#endif
62
63    /**
[2171]64        @brief Function for reading a Radian from a stream.
65    */
66    std::istream& operator>>(std::istream& in, orxonox::Radian& radian)
67    {
68        float temp;
69        in >> temp;
70        radian = temp;
71        return in;
72    }
[1505]73
[2171]74    /**
75        @brief Function for reading a Degree from a stream.
76    */
77    std::istream& operator>>(std::istream& in, orxonox::Degree& degree)
78    {
79        float temp;
80        in >> temp;
81        degree = temp;
82        return in;
83    }
[1564]84
[1791]85
[2171]86    /**
87        @brief Gets the angle between my viewing direction and the direction to the position of the other object.
88        @param myposition My position
89        @param mydirection My viewing direction
90        @param otherposition The position of the other object
[7401]91        @return The angle in radian
[1564]92
[7401]93        Examples:
94         - If the other object is exactly in front of me, the function returns 0.
95         - If the other object is exactly behind me, the function returns pi.
96         - If the other object is exactly right/left to me (or above/below), the function returns pi/2.
[2171]97    */
98    float getAngle(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& otherposition)
99    {
100        orxonox::Vector3 distance = otherposition - myposition;
101        float distancelength = distance.length();
102        if (distancelength == 0)
103            return 0;
104        else
105            return acos(clamp<float>(mydirection.dotProduct(distance) / distancelength, -1, 1));
106    }
[1791]107
[2171]108    /**
109        @brief Gets the 2D viewing direction (up/down, left/right) to the position of the other object.
110        @param myposition My position
111        @param mydirection My viewing direction
112        @param myorthonormal My orthonormalvector (pointing upwards through my head)
113        @param otherposition The position of the other object
114        @return The viewing direction
[1564]115
[7401]116        Examples:
117         - If the other object is exactly in front of me, the function returns <tt>Vector2(0, 0)</tt>.
118         - If the other object is exactly at my left, the function returns <tt>Vector2(-1, 0)</tt>.
119         - If the other object is exactly at my right, the function returns <tt>Vector2(1, 0)</tt>.
120         - If the other object is only a bit at my right, the function still returns <tt>Vector2(1, 0)</tt>.
121         - If the other object is exactly above me, the function returns <tt>Vector2(0, 1)</tt>.
[2171]122    */
123    orxonox::Vector2 get2DViewdirection(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& myorthonormal, const orxonox::Vector3& otherposition)
124    {
125        orxonox::Vector3 distance = otherposition - myposition;
[1564]126
[2171]127        // project difference vector on our plane
128        orxonox::Vector3 projection = Ogre::Plane(mydirection, myposition).projectVector(distance);
[1608]129
[2171]130        float projectionlength = projection.length();
[3049]131        if (projectionlength == 0)
132        {
133            if (myposition.dotProduct(otherposition) >= 0)
134                return orxonox::Vector2(0, 0);
135            else
136                return orxonox::Vector2(0, 1);
137        }
[6417]138
[3304]139        float cos_value = clamp<float>(myorthonormal.dotProduct(projection) / projectionlength, -1, 1);
140        float sin_value = sqrt( 1 - cos_value*cos_value );
[6417]141
[2171]142        if ((mydirection.crossProduct(myorthonormal)).dotProduct(distance) > 0)
[3304]143            return orxonox::Vector2( sin_value, cos_value );
[2171]144        else
[3304]145            return orxonox::Vector2( -sin_value, cos_value );
[2171]146    }
[1791]147
[2171]148    /**
[9719]149        @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).
[2171]150        @param myposition My position
151        @param mydirection My viewing direction
152        @param myorthonormal My orthonormalvector (pointing upwards through my head)
153        @param otherposition The position of the other object
154        @return The viewing direction
[1564]155
[7401]156        Examples:
157         - If the other object is exactly in front of me, the function returns <tt>Vector2(0, 0)</tt>.
158         - If the other object is exactly at my left, the function returns <tt>Vector2(-0.5, 0)</tt>.
159         - If the other object is exactly at my right, the function returns <tt>Vector2(0.5, 0)</tt>.
160         - If the other object is only a bit at my right, the function still returns <tt>Vector2(0.01, 0)</tt>.
161         - If the other object is exactly above me, the function returns <tt>Vector2(0, 0.5)</tt>.
[2171]162    */
163    orxonox::Vector2 get2DViewcoordinates(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& myorthonormal, const orxonox::Vector3& otherposition)
164    {
165        orxonox::Vector3 distance = otherposition - myposition;
[1564]166
[2171]167        // project difference vector on our plane
168        orxonox::Vector3 projection = Ogre::Plane(mydirection, myposition).projectVector(distance);
[1608]169
[2171]170        float projectionlength = projection.length();
[3049]171        if (projectionlength == 0)
172        {
173            if (myposition.dotProduct(otherposition) >= 0)
174                return orxonox::Vector2(0, 0);
175            else
176                return orxonox::Vector2(0, 1);
177        }
[3304]178        //float angle = acos(clamp<float>(myorthonormal.dotProduct(projection) / projectionlength, -1, 1));
[6417]179
[3304]180        float cos_value = clamp<float>(myorthonormal.dotProduct(projection) / projectionlength, -1, 1);
181        float sin_value = sqrt( 1 - cos_value*cos_value );
[1608]182
[2171]183        float distancelength = distance.length();
184        if (distancelength == 0) return orxonox::Vector2(0, 0);
[7184]185        float radius = acos(clamp<float>(mydirection.dotProduct(distance) / distancelength, -1, 1)) / math::pi;
[1566]186
[2171]187        if ((mydirection.crossProduct(myorthonormal)).dotProduct(distance) > 0)
[3304]188            return orxonox::Vector2( sin_value * radius, cos_value * radius);
[2171]189        else
[3304]190            return orxonox::Vector2( -sin_value * radius, cos_value * radius);
[2171]191    }
[1791]192
[9719]193
[2171]194    /**
[9719]195            @brief Gets the 2D project vector for the 3D Radar .
196            @param myposition My position
197            @param mydirection My viewing direction
[9740]198            @param myorthonormal My orthonormalvector (pointing upwards through my head)
[9719]199            @param otherposition The position of the other object
200            @param mapangle The angle you look on the 3Dmap
201            @param detectionlimit The limit in which objects are shown on the map
202            @return The viewing direction
203
204            Examples:
205             -
206        */
[9740]207    orxonox::Vector2 get3DProjection(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& myorthonormal, const orxonox::Vector3& otherposition, const float mapangle, const float detectionlimit)
[9719]208    {
[9779]209        // Orxonox Vectors: x_direction you are looking, y_direction points up, z_direction points to the right
[9719]210        orxonox::Vector3 distance = otherposition - myposition;
211
[9779]212        // new coordinate system:       x_axsis:        mydirection             (points front)
213        //                                                      y_axsis:        myorthonormal   (points up)
214        //                                                      z_axsis:        myside                  (points right)
215        orxonox::Vector3 myside = mydirection.crossProduct(myorthonormal);
216        orxout() << "mydirection " << mydirection << endl;
217        orxout() << "mydirection.lenght " << mydirection.length() << endl;
218        orxout() << "myorthonormal " << myorthonormal << endl;
219        orxout() << "myorthonormal.lenght " << myorthonormal.length() << endl;
220        orxout() << "myside " << myside << endl;
221        orxout() << "myside.lenght " << myside.length() << endl;
[9719]222
[9779]223        distance = 5 * distance / detectionlimit; // shrink vector on map
224
[9740]225        // inverse of the transform matrix
[9779]226        float determinant = +mydirection.x * (myorthonormal.y*myside.z - myside.y*myorthonormal.z)
227                                                -mydirection.y * (myorthonormal.x*myside.z - myorthonormal.z*myside.x)
228                                                +mydirection.z * (myorthonormal.x*myside.y - myorthonormal.y*myside.x);
[9740]229        float invdet = 1/determinant;
[9749]230
231        // transform matrix
[9740]232        orxonox::Vector3 xinvtransform;
233        orxonox::Vector3 yinvtransform;
234        orxonox::Vector3 zinvtransform;
[9719]235
[9779]236        xinvtransform.x = (myorthonormal.y * myside.z        - myside.y        * myorthonormal.z)*invdet;
237        xinvtransform.y = (mydirection.z   * myside.y        - mydirection.y   * myside.z       )*invdet;
238        xinvtransform.z = (mydirection.y   * myorthonormal.z - mydirection.z   * myorthonormal.y)*invdet;
239        yinvtransform.x = (myorthonormal.z * myside.x        - myorthonormal.x * myside.z       )*invdet;
240        yinvtransform.y = (mydirection.x   * myside.z        - mydirection.z   * myside.x       )*invdet;
241        yinvtransform.z = (myorthonormal.x * mydirection.z   - mydirection.x   * myorthonormal.z)*invdet;
242        zinvtransform.x = (myorthonormal.x * myside.y        - myside.x        * myorthonormal.y)*invdet;
243        zinvtransform.y = (myside.x        * mydirection.y   - mydirection.x   * myside.y       )*invdet;
244        zinvtransform.z = (mydirection.x   * myorthonormal.y - myorthonormal.x * mydirection.)*invdet;
[9740]245
246        // coordinate transformation
[9779]247        distance.x = xinvtransform.x * distance.x + yinvtransform.x * distance.y + zinvtransform.x * distance.z;
248        distance.y = xinvtransform.y * distance.x + yinvtransform.y * distance.y + zinvtransform.y * distance.z;
249        distance.z = xinvtransform.z * distance.x + yinvtransform.z * distance.y + zinvtransform.z * distance.z;
[9740]250
[9742]251        // cap vector for map
[9749]252        //distance.x = clamp<float>(distance.x, -detectionlimit/5, detectionlimit/5);
253        //distance.y = clamp<float>(distance.y, -detectionlimit/5, detectionlimit/5);
254        //distance.z = clamp<float>(distance.z, -detectionlimit/5, detectionlimit/5);
255        //float distancelength = distance.length();
[9742]256
[9779]257
[9740]258        // project vector for the rotated 3DMap on screen
[9779]259        float xcoordinate = distance.z;
260        float ycoordinate = distance.y;
261
262        //float xcoordinate = distance.z; // z; cause z direction is to the side
263        //float ycoordinate = distance.x*sin(mapangle)+distance.y*cos(mapangle);// -; cause on screen y coordinate points down
[9719]264        return orxonox::Vector2(xcoordinate , ycoordinate);
265    }
266
267
268    /**
[2171]269        @brief Returns the predicted position I have to aim at, if I want to hit a moving target with a moving projectile.
270        @param myposition My position
271        @param projectilespeed The speed of my projectile
272        @param targetposition The position of my target
273        @param targetvelocity The velocity of my target
274        @return The predicted position
[1566]275
[2171]276        The function predicts the position based on a linear velocity of the target. If the target changes speed or direction, the projectile will miss.
277    */
278    orxonox::Vector3 getPredictedPosition(const orxonox::Vector3& myposition, float projectilespeed, const orxonox::Vector3& targetposition, const orxonox::Vector3& targetvelocity)
279    {
280        float squaredProjectilespeed = projectilespeed * projectilespeed;
281        orxonox::Vector3 distance = targetposition - myposition;
282        float a = distance.squaredLength();
283        float b = 2 * (distance.x + distance.y + distance.z) * (targetvelocity.x + targetvelocity.y + targetvelocity.z);
284        float c = targetvelocity.squaredLength();
[1566]285
[2171]286        float temp = 4*squaredProjectilespeed*c + a*a - 4*b*c;
287        if (temp < 0)
288            return orxonox::Vector3::ZERO;
[1625]289
[2171]290        temp = sqrt(temp);
291        float time = (temp + a) / (2 * (squaredProjectilespeed - b));
292        return (targetposition + targetvelocity * time);
293    }
[1625]294
[7401]295    /**
296        @brief Returns a unique number. This function will never return the same value twice.
297    */
[2171]298    unsigned long getUniqueNumber()
299    {
300        static unsigned long aNumber = 135;
301        return aNumber++;
302    }
[2087]303
304
[2171]305    //////////////////////////
306    // Conversion functions //
307    //////////////////////////
[2087]308
[2171]309    // std::string to Vector2
310    bool ConverterFallback<std::string, orxonox::Vector2>::convert(orxonox::Vector2* output, const std::string& input)
[2087]311    {
[7284]312        size_t opening_parenthesis, closing_parenthesis = input.find('}');
313        if ((opening_parenthesis = input.find('{')) == std::string::npos)
[2171]314            opening_parenthesis = 0;
315        else
316            opening_parenthesis++;
[2087]317
[2171]318        SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis),
319                         ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0');
320        if (tokens.size() >= 2)
321        {
[3196]322            if (!convertValue(&(output->x), tokens[0]))
[2171]323                return false;
[3196]324            if (!convertValue(&(output->y), tokens[1]))
[2171]325                return false;
326
327            return true;
328        }
329        return false;
[2087]330    }
331
[2171]332    // std::string to Vector3
333    bool ConverterFallback<std::string, orxonox::Vector3>::convert(orxonox::Vector3* output, const std::string& input)
[2087]334    {
[7284]335        size_t opening_parenthesis, closing_parenthesis = input.find('}');
336        if ((opening_parenthesis = input.find('{')) == std::string::npos)
[2171]337            opening_parenthesis = 0;
338        else
339            opening_parenthesis++;
[2087]340
[2171]341        SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis),
342                         ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0');
343        if (tokens.size() >= 3)
344        {
[3196]345            if (!convertValue(&(output->x), tokens[0]))
[2171]346                return false;
[3196]347            if (!convertValue(&(output->y), tokens[1]))
[2171]348                return false;
[3196]349            if (!convertValue(&(output->z), tokens[2]))
[2171]350                return false;
351
352            return true;
353        }
354        return false;
[2087]355    }
356
[2171]357    // std::string to Vector4
358    bool ConverterFallback<std::string, orxonox::Vector4>::convert(orxonox::Vector4* output, const std::string& input)
[2087]359    {
[7284]360        size_t opening_parenthesis, closing_parenthesis = input.find('}');
361        if ((opening_parenthesis = input.find('{')) == std::string::npos)
[2171]362            opening_parenthesis = 0;
363        else
364            opening_parenthesis++;
[2087]365
[2171]366        SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis),
367                         ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0');
368        if (tokens.size() >= 4)
369        {
[3196]370            if (!convertValue(&(output->x), tokens[0]))
[2171]371                return false;
[3196]372            if (!convertValue(&(output->y), tokens[1]))
[2171]373                return false;
[3196]374            if (!convertValue(&(output->z), tokens[2]))
[2171]375                return false;
[3196]376            if (!convertValue(&(output->w), tokens[3]))
[2171]377                return false;
378
379            return true;
380        }
381        return false;
[2087]382    }
383
[2171]384    // std::string to Quaternion
385    bool ConverterFallback<std::string, orxonox::Quaternion>::convert(orxonox::Quaternion* output, const std::string& input)
[2087]386    {
[7284]387        size_t opening_parenthesis, closing_parenthesis = input.find('}');
388        if ((opening_parenthesis = input.find('{')) == std::string::npos)
389            opening_parenthesis = 0;
390        else
391            opening_parenthesis++;
[2087]392
[2171]393        SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0');
394        if (tokens.size() >= 4)
395        {
[3196]396            if (!convertValue(&(output->w), tokens[0]))
[2171]397                return false;
[3196]398            if (!convertValue(&(output->x), tokens[1]))
[2171]399                return false;
[3196]400            if (!convertValue(&(output->y), tokens[2]))
[2171]401                return false;
[3196]402            if (!convertValue(&(output->z), tokens[3]))
[2171]403                return false;
404
405            return true;
406        }
407        return false;
[2087]408    }
409
[2171]410    // std::string to ColourValue
411    bool ConverterFallback<std::string, orxonox::ColourValue>::convert(orxonox::ColourValue* output, const std::string& input)
412    {
[7284]413        size_t opening_parenthesis, closing_parenthesis = input.find('}');
414        if ((opening_parenthesis = input.find('{')) == std::string::npos)
415            opening_parenthesis = 0;
416        else
417            opening_parenthesis++;
[2087]418
[2171]419        SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0');
420        if (tokens.size() >= 3)
[2087]421        {
[3196]422            if (!convertValue(&(output->r), tokens[0]))
[2087]423                return false;
[3196]424            if (!convertValue(&(output->g), tokens[1]))
[2171]425                return false;
[3196]426            if (!convertValue(&(output->b), tokens[2]))
[2171]427                return false;
428            if (tokens.size() >= 4)
429            {
[3196]430                if (!convertValue(&(output->a), tokens[3]))
[2171]431                    return false;
432            }
433            else
434                output->a = 1.0;
435
436            return true;
[2087]437        }
[2171]438        return false;
[2087]439    }
440}
Note: See TracBrowser for help on using the repository browser.