[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 | |
---|
[1564] | 29 | #include <OgrePlane.h> |
---|
| 30 | |
---|
[1505] | 31 | #include "Math.h" |
---|
| 32 | |
---|
| 33 | /** |
---|
| 34 | @brief Function for writing to a stream. |
---|
| 35 | */ |
---|
| 36 | std::ostream& operator<<(std::ostream& out, const orxonox::Radian& radian) |
---|
| 37 | { |
---|
| 38 | out << radian.valueRadians(); |
---|
| 39 | return out; |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | /** |
---|
| 43 | @brief Function for reading from a stream. |
---|
| 44 | */ |
---|
| 45 | std::istream& operator>>(std::istream& in, orxonox::Radian& radian) |
---|
| 46 | { |
---|
| 47 | float temp; |
---|
| 48 | in >> temp; |
---|
| 49 | radian = temp; |
---|
| 50 | return in; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | /** |
---|
| 54 | @brief Function for writing 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 | |
---|
| 62 | /** |
---|
| 63 | @brief Function for reading from a stream. |
---|
| 64 | */ |
---|
| 65 | std::istream& operator>>(std::istream& in, orxonox::Degree& degree) |
---|
| 66 | { |
---|
| 67 | float temp; |
---|
| 68 | in >> temp; |
---|
| 69 | degree = temp; |
---|
| 70 | return in; |
---|
| 71 | } |
---|
[1564] | 72 | |
---|
| 73 | |
---|
| 74 | float getAngle(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& otherposition) |
---|
| 75 | { |
---|
| 76 | orxonox::Vector3 distance = otherposition - myposition; |
---|
[1608] | 77 | float distancelength = distance.length(); |
---|
| 78 | if (distancelength == 0) |
---|
| 79 | return 0; |
---|
| 80 | else |
---|
| 81 | return acos(clamp<float>(mydirection.dotProduct(distance) / distancelength, -1, 1)); |
---|
[1564] | 82 | } |
---|
| 83 | |
---|
| 84 | orxonox::Vector2 get2DViewdirection(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& myorthonormal, const orxonox::Vector3& otherposition) |
---|
| 85 | { |
---|
| 86 | orxonox::Vector3 distance = otherposition - myposition; |
---|
| 87 | |
---|
| 88 | // project difference vector on our plane |
---|
| 89 | orxonox::Vector3 projection = Ogre::Plane(mydirection, myposition).projectVector(distance); |
---|
| 90 | |
---|
[1608] | 91 | float projectionlength = projection.length(); |
---|
| 92 | if (projectionlength == 0) return orxonox::Vector2(0, 0); |
---|
| 93 | float angle = acos(clamp<float>(myorthonormal.dotProduct(projection) / projectionlength, -1, 1)); |
---|
| 94 | |
---|
[1564] | 95 | if ((mydirection.crossProduct(myorthonormal)).dotProduct(distance) > 0) |
---|
| 96 | return orxonox::Vector2(sin(angle), cos(angle)); |
---|
| 97 | else |
---|
| 98 | return orxonox::Vector2(-sin(angle), cos(angle)); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | orxonox::Vector2 get2DViewcoordinates(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& myorthonormal, const orxonox::Vector3& otherposition) |
---|
| 102 | { |
---|
| 103 | orxonox::Vector3 distance = otherposition - myposition; |
---|
| 104 | |
---|
| 105 | // project difference vector on our plane |
---|
| 106 | orxonox::Vector3 projection = Ogre::Plane(mydirection, myposition).projectVector(distance); |
---|
| 107 | |
---|
[1608] | 108 | float projectionlength = projection.length(); |
---|
| 109 | if (projectionlength == 0) return orxonox::Vector2(0, 0); |
---|
| 110 | float angle = acos(clamp<float>(myorthonormal.dotProduct(projection) / projectionlength, -1, 1)); |
---|
| 111 | |
---|
| 112 | float distancelength = distance.length(); |
---|
| 113 | if (distancelength == 0) return orxonox::Vector2(0, 0); |
---|
| 114 | float radius = acos(clamp<float>(mydirection.dotProduct(distance) / distancelength, -1, 1)) / Ogre::Math::PI; |
---|
| 115 | |
---|
[1564] | 116 | if ((mydirection.crossProduct(myorthonormal)).dotProduct(distance) > 0) |
---|
| 117 | return orxonox::Vector2(sin(angle) * radius, cos(angle) * radius); |
---|
| 118 | else |
---|
| 119 | return orxonox::Vector2(-sin(angle) * radius, cos(angle) * radius); |
---|
| 120 | } |
---|
[1566] | 121 | |
---|
| 122 | orxonox::Vector3 getPredictedPosition(const orxonox::Vector3& myposition, float projectilespeed, const orxonox::Vector3& targetposition, const orxonox::Vector3& targetvelocity) |
---|
| 123 | { |
---|
| 124 | float squaredProjectilespeed = projectilespeed * projectilespeed; |
---|
| 125 | orxonox::Vector3 distance = targetposition - myposition; |
---|
| 126 | float a = distance.squaredLength(); |
---|
| 127 | float b = 2 * (distance.x + distance.y + distance.z) * (targetvelocity.x + targetvelocity.y + targetvelocity.z); |
---|
| 128 | float c = targetvelocity.squaredLength(); |
---|
| 129 | |
---|
| 130 | float temp = 4*squaredProjectilespeed*c + a*a - 4*b*c; |
---|
| 131 | if (temp < 0) |
---|
| 132 | return orxonox::Vector3::ZERO; |
---|
| 133 | |
---|
| 134 | temp = sqrt(temp); |
---|
| 135 | float time = (temp + a) / (2 * (squaredProjectilespeed - b)); |
---|
| 136 | return (targetposition + targetvelocity * time); |
---|
| 137 | } |
---|