Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9740 was 9740, checked in by wroennin, 10 years ago

HUDRadar:function call get3DProjection updated; Math.cc: get3DProjection new transform matrix; Math.h updated

  • Property svn:eol-style set to native
File size: 16.9 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 *      Wolfgang Roenninger
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
41namespace orxonox
42{
43#if OGRE_VERSION < 0x010603
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    }
52
53    /**
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    /**
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    }
73
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    }
84
85
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
91        @return The angle in radian
92
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.
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    }
107
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
115
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>.
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;
126
127        // project difference vector on our plane
128        orxonox::Vector3 projection = Ogre::Plane(mydirection, myposition).projectVector(distance);
129
130        float projectionlength = projection.length();
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        }
138
139        float cos_value = clamp<float>(myorthonormal.dotProduct(projection) / projectionlength, -1, 1);
140        float sin_value = sqrt( 1 - cos_value*cos_value );
141
142        if ((mydirection.crossProduct(myorthonormal)).dotProduct(distance) > 0)
143            return orxonox::Vector2( sin_value, cos_value );
144        else
145            return orxonox::Vector2( -sin_value, cos_value );
146    }
147
148    /**
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).
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
155
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>.
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;
166
167        // project difference vector on our plane
168        orxonox::Vector3 projection = Ogre::Plane(mydirection, myposition).projectVector(distance);
169
170        float projectionlength = projection.length();
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        }
178        //float angle = acos(clamp<float>(myorthonormal.dotProduct(projection) / projectionlength, -1, 1));
179
180        float cos_value = clamp<float>(myorthonormal.dotProduct(projection) / projectionlength, -1, 1);
181        float sin_value = sqrt( 1 - cos_value*cos_value );
182
183        float distancelength = distance.length();
184        if (distancelength == 0) return orxonox::Vector2(0, 0);
185        float radius = acos(clamp<float>(mydirection.dotProduct(distance) / distancelength, -1, 1)) / math::pi;
186
187        if ((mydirection.crossProduct(myorthonormal)).dotProduct(distance) > 0)
188            return orxonox::Vector2( sin_value * radius, cos_value * radius);
189        else
190            return orxonox::Vector2( -sin_value * radius, cos_value * radius);
191    }
192
193
194    /**
195            @brief Gets the 2D project vector for the 3D Radar .
196            @param myposition My position
197            @param mydirection My viewing direction
198            @param myorthonormal My orthonormalvector (pointing upwards through my head)
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        */
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)
208    {
209        //
210        orxonox::Vector3 distance = otherposition - myposition;
211
212        // new coordinate system base y_coordinate
213        orxonox::Vector3 myside = -mydirection.crossProduct(myorthonormal);
214
215        // inverse of the transform matrix
216        float determinant = +mydirection.x * (myside.y*myorthonormal.z - myorthonormal.y*myside.z)
217                                                -mydirection.y * (myside.x*myorthonormal.z - myside.z*myorthonormal.x)
218                                                +mydirection.z * (myside.x*myorthonormal.y - myside.y*myorthonormal.x);
219        float invdet = 1/determinant;
220        orxonox::Vector3 xinvtransform;
221        orxonox::Vector3 yinvtransform;
222        orxonox::Vector3 zinvtransform;
223
224        xinvtransform.x =  (myside.y      * myorthonormal.z - myorthonormal.y * myside.z       )*invdet;
225        xinvtransform.y = -(mydirection.y * myorthonormal.z - mydirection.z   * myorthonormal.y)*invdet;
226        xinvtransform.z =  (mydirection.y * myside.z        - mydirection.z   * myside.y       )*invdet;
227        yinvtransform.x = -(myside.x      * myorthonormal.z - myside.z        * myorthonormal.x)*invdet;
228        yinvtransform.y =  (mydirection.x * myorthonormal.z - mydirection.z   * myorthonormal.x)*invdet;
229        yinvtransform.z = -(mydirection.x * myside.z        - myside.x        * mydirection.)*invdet;
230        zinvtransform.x =  (myside.x      * myorthonormal.y - myorthonormal.x * myside.y       )*invdet;
231        zinvtransform.y = -(mydirection.x * myorthonormal.y - myorthonormal.x * mydirection.)*invdet;
232        zinvtransform.z =  (mydirection.x * myside.y        - myside.x        * mydirection.)*invdet;
233
234        // coordinate transformation
235        distance.x = (xinvtransform.x + yinvtransform.x + zinvtransform.x) * distance.x;
236        distance.y = (xinvtransform.y + yinvtransform.y + zinvtransform.y) * distance.y;
237        distance.z = (xinvtransform.z + yinvtransform.z + zinvtransform.z) * distance.z;
238
239        // project vector for the rotated 3DMap on screen
240        float xcoordinate = distance.y/(2*detectionlimit);
241        float ycoordinate = (distance.x*sin(mapangle)+distance.z*cos(mapangle))/(2*detectionlimit);
242        return orxonox::Vector2(xcoordinate , ycoordinate);
243    }
244
245
246    /**
247        @brief Returns the predicted position I have to aim at, if I want to hit a moving target with a moving projectile.
248        @param myposition My position
249        @param projectilespeed The speed of my projectile
250        @param targetposition The position of my target
251        @param targetvelocity The velocity of my target
252        @return The predicted position
253
254        The function predicts the position based on a linear velocity of the target. If the target changes speed or direction, the projectile will miss.
255    */
256    orxonox::Vector3 getPredictedPosition(const orxonox::Vector3& myposition, float projectilespeed, const orxonox::Vector3& targetposition, const orxonox::Vector3& targetvelocity)
257    {
258        float squaredProjectilespeed = projectilespeed * projectilespeed;
259        orxonox::Vector3 distance = targetposition - myposition;
260        float a = distance.squaredLength();
261        float b = 2 * (distance.x + distance.y + distance.z) * (targetvelocity.x + targetvelocity.y + targetvelocity.z);
262        float c = targetvelocity.squaredLength();
263
264        float temp = 4*squaredProjectilespeed*c + a*a - 4*b*c;
265        if (temp < 0)
266            return orxonox::Vector3::ZERO;
267
268        temp = sqrt(temp);
269        float time = (temp + a) / (2 * (squaredProjectilespeed - b));
270        return (targetposition + targetvelocity * time);
271    }
272
273    /**
274        @brief Returns a unique number. This function will never return the same value twice.
275    */
276    unsigned long getUniqueNumber()
277    {
278        static unsigned long aNumber = 135;
279        return aNumber++;
280    }
281
282
283    //////////////////////////
284    // Conversion functions //
285    //////////////////////////
286
287    // std::string to Vector2
288    bool ConverterFallback<std::string, orxonox::Vector2>::convert(orxonox::Vector2* output, const std::string& input)
289    {
290        size_t opening_parenthesis, closing_parenthesis = input.find('}');
291        if ((opening_parenthesis = input.find('{')) == std::string::npos)
292            opening_parenthesis = 0;
293        else
294            opening_parenthesis++;
295
296        SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis),
297                         ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0');
298        if (tokens.size() >= 2)
299        {
300            if (!convertValue(&(output->x), tokens[0]))
301                return false;
302            if (!convertValue(&(output->y), tokens[1]))
303                return false;
304
305            return true;
306        }
307        return false;
308    }
309
310    // std::string to Vector3
311    bool ConverterFallback<std::string, orxonox::Vector3>::convert(orxonox::Vector3* output, const std::string& input)
312    {
313        size_t opening_parenthesis, closing_parenthesis = input.find('}');
314        if ((opening_parenthesis = input.find('{')) == std::string::npos)
315            opening_parenthesis = 0;
316        else
317            opening_parenthesis++;
318
319        SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis),
320                         ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0');
321        if (tokens.size() >= 3)
322        {
323            if (!convertValue(&(output->x), tokens[0]))
324                return false;
325            if (!convertValue(&(output->y), tokens[1]))
326                return false;
327            if (!convertValue(&(output->z), tokens[2]))
328                return false;
329
330            return true;
331        }
332        return false;
333    }
334
335    // std::string to Vector4
336    bool ConverterFallback<std::string, orxonox::Vector4>::convert(orxonox::Vector4* output, const std::string& input)
337    {
338        size_t opening_parenthesis, closing_parenthesis = input.find('}');
339        if ((opening_parenthesis = input.find('{')) == std::string::npos)
340            opening_parenthesis = 0;
341        else
342            opening_parenthesis++;
343
344        SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis),
345                         ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0');
346        if (tokens.size() >= 4)
347        {
348            if (!convertValue(&(output->x), tokens[0]))
349                return false;
350            if (!convertValue(&(output->y), tokens[1]))
351                return false;
352            if (!convertValue(&(output->z), tokens[2]))
353                return false;
354            if (!convertValue(&(output->w), tokens[3]))
355                return false;
356
357            return true;
358        }
359        return false;
360    }
361
362    // std::string to Quaternion
363    bool ConverterFallback<std::string, orxonox::Quaternion>::convert(orxonox::Quaternion* output, const std::string& input)
364    {
365        size_t opening_parenthesis, closing_parenthesis = input.find('}');
366        if ((opening_parenthesis = input.find('{')) == std::string::npos)
367            opening_parenthesis = 0;
368        else
369            opening_parenthesis++;
370
371        SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0');
372        if (tokens.size() >= 4)
373        {
374            if (!convertValue(&(output->w), tokens[0]))
375                return false;
376            if (!convertValue(&(output->x), tokens[1]))
377                return false;
378            if (!convertValue(&(output->y), tokens[2]))
379                return false;
380            if (!convertValue(&(output->z), tokens[3]))
381                return false;
382
383            return true;
384        }
385        return false;
386    }
387
388    // std::string to ColourValue
389    bool ConverterFallback<std::string, orxonox::ColourValue>::convert(orxonox::ColourValue* output, const std::string& input)
390    {
391        size_t opening_parenthesis, closing_parenthesis = input.find('}');
392        if ((opening_parenthesis = input.find('{')) == std::string::npos)
393            opening_parenthesis = 0;
394        else
395            opening_parenthesis++;
396
397        SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0');
398        if (tokens.size() >= 3)
399        {
400            if (!convertValue(&(output->r), tokens[0]))
401                return false;
402            if (!convertValue(&(output->g), tokens[1]))
403                return false;
404            if (!convertValue(&(output->b), tokens[2]))
405                return false;
406            if (tokens.size() >= 4)
407            {
408                if (!convertValue(&(output->a), tokens[3]))
409                    return false;
410            }
411            else
412                output->a = 1.0;
413
414            return true;
415        }
416        return false;
417    }
418}
Note: See TracBrowser for help on using the repository browser.