Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core3/src/util/Math.cc @ 1852

Last change on this file since 1852 was 1779, checked in by rgrieder, 16 years ago

gcc didn't like me..

  • Property svn:eol-style set to native
File size: 8.7 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#include "Math.h"
30
31#include <OgrePlane.h>
32#include "MathConvert.h"
33
34/**
35    @brief Function for writing to a stream.
36*/
37std::ostream& operator<<(std::ostream& out, const orxonox::Radian& radian)
38{
39    out << radian.valueRadians();
40    return out;
41}
42
43/**
44    @brief Function for reading from a stream.
45*/
46std::istream& operator>>(std::istream& in, orxonox::Radian& radian)
47{
48    float temp;
49    in >> temp;
50    radian = temp;
51    return in;
52}
53
54/**
55    @brief Function for writing to a stream.
56*/
57std::ostream& operator<<(std::ostream& out, const orxonox::Degree& degree)
58{
59    out << degree.valueDegrees();
60    return out;
61}
62
63/**
64    @brief Function for reading from a stream.
65*/
66std::istream& operator>>(std::istream& in, orxonox::Degree& degree)
67{
68    float temp;
69    in >> temp;
70    degree = temp;
71    return in;
72}
73
74
75float getAngle(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& otherposition)
76{
77    orxonox::Vector3 distance = otherposition - myposition;
78    return acos(mydirection.dotProduct(distance) / distance.length());
79}
80
81orxonox::Vector2 get2DViewdirection(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& myorthonormal, const orxonox::Vector3& otherposition)
82{
83    orxonox::Vector3 distance = otherposition - myposition;
84
85    // project difference vector on our plane
86    orxonox::Vector3 projection = Ogre::Plane(mydirection, myposition).projectVector(distance);
87    float angle = acos(myorthonormal.dotProduct(projection) / projection.length());
88
89    if ((mydirection.crossProduct(myorthonormal)).dotProduct(distance) > 0)
90        return orxonox::Vector2(sin(angle), cos(angle));
91    else
92        return orxonox::Vector2(-sin(angle), cos(angle));
93}
94
95orxonox::Vector2 get2DViewcoordinates(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& myorthonormal, const orxonox::Vector3& otherposition)
96{
97    orxonox::Vector3 distance = otherposition - myposition;
98
99    // project difference vector on our plane
100    orxonox::Vector3 projection = Ogre::Plane(mydirection, myposition).projectVector(distance);
101    float angle = acos(myorthonormal.dotProduct(projection) / projection.length());
102    float radius = acos(mydirection.dotProduct(distance) / distance.length()) / Ogre::Math::PI;
103
104    if ((mydirection.crossProduct(myorthonormal)).dotProduct(distance) > 0)
105        return orxonox::Vector2(sin(angle) * radius, cos(angle) * radius);
106    else
107        return orxonox::Vector2(-sin(angle) * radius, cos(angle) * radius);
108}
109
110orxonox::Vector3 getPredictedPosition(const orxonox::Vector3& myposition, float projectilespeed, const orxonox::Vector3& targetposition, const orxonox::Vector3& targetvelocity)
111{
112    float squaredProjectilespeed = projectilespeed * projectilespeed;
113    orxonox::Vector3 distance = targetposition - myposition;
114    float a = distance.squaredLength();
115    float b = 2 * (distance.x + distance.y + distance.z) * (targetvelocity.x + targetvelocity.y + targetvelocity.z);
116    float c = targetvelocity.squaredLength();
117
118    float temp = 4*squaredProjectilespeed*c + a*a - 4*b*c;
119    if (temp < 0)
120        return orxonox::Vector3::ZERO;
121
122    temp = sqrt(temp);
123    float time = (temp + a) / (2 * (squaredProjectilespeed - b));
124    return (targetposition + targetvelocity * time);
125}
126
127//////////////////////////
128// Conversion functions //
129//////////////////////////
130
131// std::string to Vector2
132bool ConverterFallback<orxonox::Vector2, std::string>::convert(orxonox::Vector2* output, const std::string& input)
133{
134    unsigned int opening_parenthesis, closing_parenthesis = input.find(')');
135    if ((opening_parenthesis = input.find('(')) == std::string::npos)
136        opening_parenthesis = 0;
137    else
138        opening_parenthesis++;
139
140    SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis),
141                     ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0');
142    if (tokens.size() >= 2)
143    {
144        if (!ConvertValue(&(output->x), tokens[0]))
145            return false;
146        if (!ConvertValue(&(output->y), tokens[1]))
147            return false;
148
149        return true;
150    }
151    return false;
152}
153
154// std::string to Vector3
155bool ConverterFallback<orxonox::Vector3, std::string>::convert(orxonox::Vector3* output, const std::string& input)
156{
157    unsigned int opening_parenthesis, closing_parenthesis = input.find(')');
158    if ((opening_parenthesis = input.find('(')) == std::string::npos)
159        opening_parenthesis = 0;
160    else
161        opening_parenthesis++;
162
163    SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis),
164                     ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0');
165    if (tokens.size() >= 3)
166    {
167        if (!ConvertValue(&(output->x), tokens[0]))
168            return false;
169        if (!ConvertValue(&(output->y), tokens[1]))
170            return false;
171        if (!ConvertValue(&(output->z), tokens[2]))
172            return false;
173
174        return true;
175    }
176    return false;
177}
178
179// std::string to Vector4
180bool ConverterFallback<orxonox::Vector4, std::string>::convert(orxonox::Vector4* output, const std::string& input)
181{
182    unsigned int opening_parenthesis, closing_parenthesis = input.find(')');
183    if ((opening_parenthesis = input.find('(')) == std::string::npos)
184        opening_parenthesis = 0;
185    else
186        opening_parenthesis++;
187
188    SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis),
189                     ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0');
190    if (tokens.size() >= 4)
191    {
192        if (!ConvertValue(&(output->x), tokens[0]))
193            return false;
194        if (!ConvertValue(&(output->y), tokens[1]))
195            return false;
196        if (!ConvertValue(&(output->z), tokens[2]))
197            return false;
198        if (!ConvertValue(&(output->w), tokens[3]))
199            return false;
200
201        return true;
202    }
203    return false;
204}
205
206// std::string to Quaternion
207bool ConverterFallback<orxonox::Quaternion, std::string>::convert(orxonox::Quaternion* output, const std::string& input)
208{
209    unsigned int opening_parenthesis, closing_parenthesis = input.find(')');
210    if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; }
211
212    SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0');
213    if (tokens.size() >= 4)
214    {
215        if (!ConvertValue(&(output->w), tokens[0]))
216            return false;
217        if (!ConvertValue(&(output->x), tokens[1]))
218            return false;
219        if (!ConvertValue(&(output->y), tokens[2]))
220            return false;
221        if (!ConvertValue(&(output->z), tokens[3]))
222            return false;
223
224        return true;
225    }
226    return false;
227}
228
229// std::string to ColourValue
230bool ConverterFallback<orxonox::ColourValue, std::string>::convert(orxonox::ColourValue* output, const std::string& input)
231{
232    unsigned int opening_parenthesis, closing_parenthesis = input.find(')');
233    if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; }
234
235    SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0');
236    if (tokens.size() >= 4)
237    {
238        if (!ConvertValue(&(output->r), tokens[0]))
239            return false;
240        if (!ConvertValue(&(output->g), tokens[1]))
241            return false;
242        if (!ConvertValue(&(output->b), tokens[2]))
243            return false;
244        if (!ConvertValue(&(output->a), tokens[3]))
245            return false;
246
247        return true;
248    }
249    return false;
250}
251
Note: See TracBrowser for help on using the repository browser.