Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/util/Math.cc @ 1683

Last change on this file since 1683 was 1625, checked in by rgrieder, 16 years ago

merged hud branch back to trunk

  • Property svn:eol-style set to native
File size: 4.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 *      ...
26 *
27 */
28
29#include <OgrePlane.h>
30
31#include "Math.h"
32#include "Convert.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    float distancelength = distance.length();
79    if (distancelength == 0)
80        return 0;
81    else
82        return acos(clamp<float>(mydirection.dotProduct(distance) / distancelength, -1, 1));
83}
84
85orxonox::Vector2 get2DViewdirection(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& myorthonormal, const orxonox::Vector3& otherposition)
86{
87    orxonox::Vector3 distance = otherposition - myposition;
88
89    // project difference vector on our plane
90    orxonox::Vector3 projection = Ogre::Plane(mydirection, myposition).projectVector(distance);
91
92    float projectionlength = projection.length();
93    if (projectionlength == 0) return orxonox::Vector2(0, 0);
94    float angle = acos(clamp<float>(myorthonormal.dotProduct(projection) / projectionlength, -1, 1));
95
96    if ((mydirection.crossProduct(myorthonormal)).dotProduct(distance) > 0)
97        return orxonox::Vector2(sin(angle), cos(angle));
98    else
99        return orxonox::Vector2(-sin(angle), cos(angle));
100}
101
102orxonox::Vector2 get2DViewcoordinates(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& myorthonormal, const orxonox::Vector3& otherposition)
103{
104    orxonox::Vector3 distance = otherposition - myposition;
105
106    // project difference vector on our plane
107    orxonox::Vector3 projection = Ogre::Plane(mydirection, myposition).projectVector(distance);
108
109    float projectionlength = projection.length();
110    if (projectionlength == 0) return orxonox::Vector2(0, 0);
111    float angle = acos(clamp<float>(myorthonormal.dotProduct(projection) / projectionlength, -1, 1));
112
113    float distancelength = distance.length();
114    if (distancelength == 0) return orxonox::Vector2(0, 0);
115    float radius = acos(clamp<float>(mydirection.dotProduct(distance) / distancelength, -1, 1)) / Ogre::Math::PI;
116
117    if ((mydirection.crossProduct(myorthonormal)).dotProduct(distance) > 0)
118        return orxonox::Vector2(sin(angle) * radius, cos(angle) * radius);
119    else
120        return orxonox::Vector2(-sin(angle) * radius, cos(angle) * radius);
121}
122
123orxonox::Vector3 getPredictedPosition(const orxonox::Vector3& myposition, float projectilespeed, const orxonox::Vector3& targetposition, const orxonox::Vector3& targetvelocity)
124{
125    float squaredProjectilespeed = projectilespeed * projectilespeed;
126    orxonox::Vector3 distance = targetposition - myposition;
127    float a = distance.squaredLength();
128    float b = 2 * (distance.x + distance.y + distance.z) * (targetvelocity.x + targetvelocity.y + targetvelocity.z);
129    float c = targetvelocity.squaredLength();
130
131    float temp = 4*squaredProjectilespeed*c + a*a - 4*b*c;
132    if (temp < 0)
133        return orxonox::Vector3::ZERO;
134
135    temp = sqrt(temp);
136    float time = (temp + a) / (2 * (squaredProjectilespeed - b));
137    return (targetposition + targetvelocity * time);
138}
139
140unsigned long getUniqueNumber()
141{
142    static unsigned long aNumber = 135;
143    return aNumber++;
144}
145
146std::string getUniqueNumberStr()
147{
148    return convertToString(getUniqueNumber());
149}
Note: See TracBrowser for help on using the repository browser.