Changeset 5789 for sandbox_light/src/libraries/util
- Timestamp:
- Sep 26, 2009, 12:44:49 AM (15 years ago)
- Location:
- sandbox_light/src/libraries/util
- Files:
-
- 9 deleted
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
sandbox_light/src/libraries/util/CMakeLists.txt
r5782 r5789 19 19 20 20 SET_SOURCE_FILES(UTIL_SRC_FILES 21 CRC32.cc22 21 Exception.cc 23 ExprParser.cc24 22 Math.cc 25 23 MultiType.cc 26 24 OutputBuffer.cc 27 25 OutputHandler.cc 28 Scope.cc29 26 SignalHandler.cc 30 27 Sleep.cc … … 43 40 "UTIL_SHARED_BUILD" 44 41 LINK_LIBRARIES 45 ${CEGUI_LIBRARY} 46 ${OGRE_LIBRARY} 42 ogremath_orxonox 47 43 SOURCE_FILES 48 44 ${UTIL_SRC_FILES} -
sandbox_light/src/libraries/util/Math.cc
r5738 r5789 34 34 #include "Math.h" 35 35 36 #include <OgrePlane.h>37 38 36 #include "MathConvert.h" 39 37 #include "SubString.h" 40 // Do not remove this include, it avoids linker errors.41 #include "mbool.h"42 38 43 39 namespace orxonox … … 81 77 degree = temp; 82 78 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 position89 @param mydirection My viewing direction90 @param otherposition The position of the other object91 @return The angle92 93 @example94 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 else105 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 position111 @param mydirection My viewing direction112 @param myorthonormal My orthonormalvector (pointing upwards through my head)113 @param otherposition The position of the other object114 @return The viewing direction115 116 @example117 If the other object is exactly in front of me, the function returns Vector2(0, 0).118 If the other object is exactly at my left, the function returns Vector2(-1, 0).119 If the other object is exactly at my right, the function returns Vector2(1, 0).120 If the other object is only a bit at my right, the function still returns Vector2(1, 0).121 If the other object is exactly above me, the function returns Vector2(0, 1).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 plane128 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 else136 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 else145 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 position151 @param mydirection My viewing direction152 @param myorthonormal My orthonormalvector (pointing upwards through my head)153 @param otherposition The position of the other object154 @return The viewing direction155 156 @example157 If the other object is exactly in front of me, the function returns Vector2(0, 0).158 If the other object is exactly at my left, the function returns Vector2(-0.5, 0).159 If the other object is exactly at my right, the function returns Vector2(0.5, 0).160 If the other object is only a bit at my right, the function still returns Vector2(0.01, 0).161 If the other object is exactly above me, the function returns Vector2(0, 0.5).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 plane168 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 else176 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)) / Ogre::Math::PI;186 187 if ((mydirection.crossProduct(myorthonormal)).dotProduct(distance) > 0)188 return orxonox::Vector2( sin_value * radius, cos_value * radius);189 else190 return orxonox::Vector2( -sin_value * radius, cos_value * radius);191 }192 193 /**194 @brief Returns the predicted position I have to aim at, if I want to hit a moving target with a moving projectile.195 @param myposition My position196 @param projectilespeed The speed of my projectile197 @param targetposition The position of my target198 @param targetvelocity The velocity of my target199 @return The predicted position200 201 The function predicts the position based on a linear velocity of the target. If the target changes speed or direction, the projectile will miss.202 */203 orxonox::Vector3 getPredictedPosition(const orxonox::Vector3& myposition, float projectilespeed, const orxonox::Vector3& targetposition, const orxonox::Vector3& targetvelocity)204 {205 float squaredProjectilespeed = projectilespeed * projectilespeed;206 orxonox::Vector3 distance = targetposition - myposition;207 float a = distance.squaredLength();208 float b = 2 * (distance.x + distance.y + distance.z) * (targetvelocity.x + targetvelocity.y + targetvelocity.z);209 float c = targetvelocity.squaredLength();210 211 float temp = 4*squaredProjectilespeed*c + a*a - 4*b*c;212 if (temp < 0)213 return orxonox::Vector3::ZERO;214 215 temp = sqrt(temp);216 float time = (temp + a) / (2 * (squaredProjectilespeed - b));217 return (targetposition + targetvelocity * time);218 79 } 219 80 -
sandbox_light/src/libraries/util/Math.h
r5738 r5789 40 40 #include <cmath> 41 41 42 #include < OgreMath.h>43 #include < OgreVector2.h>44 #include < OgreVector3.h>45 #include < OgreVector4.h>46 #include < OgreQuaternion.h>47 #include < OgreColourValue.h>42 #include <ogremath/OgreMath.h> 43 #include <ogremath/OgreVector2.h> 44 #include <ogremath/OgreVector3.h> 45 #include <ogremath/OgreVector4.h> 46 #include <ogremath/OgreQuaternion.h> 47 #include <ogremath/OgreColourValue.h> 48 48 49 49 // Certain headers might define unwanted macros... … … 63 63 _UtilExport std::ostream& operator<<(std::ostream& out, const orxonox::Degree& degree); 64 64 _UtilExport std::istream& operator>>(std::istream& in, orxonox::Degree& degree); 65 66 _UtilExport float getAngle(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& otherposition);67 _UtilExport orxonox::Vector2 get2DViewdirection(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& myorthonormal, const orxonox::Vector3& otherposition);68 _UtilExport orxonox::Vector2 get2DViewcoordinates(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& myorthonormal, const orxonox::Vector3& otherposition);69 _UtilExport orxonox::Vector3 getPredictedPosition(const orxonox::Vector3& myposition, float projectilespeed, const orxonox::Vector3& targetposition, const orxonox::Vector3& targetvelocity);70 65 71 66 /** -
sandbox_light/src/libraries/util/MultiType.h
r5738 r5789 71 71 #include <cassert> 72 72 #include <string> 73 #include < OgreVector2.h>74 #include < OgreVector3.h>75 #include < OgreVector4.h>76 #include < OgreQuaternion.h>77 #include < OgreColourValue.h>73 #include <ogremath/OgreVector2.h> 74 #include <ogremath/OgreVector3.h> 75 #include <ogremath/OgreVector4.h> 76 #include <ogremath/OgreQuaternion.h> 77 #include <ogremath/OgreColourValue.h> 78 78 79 79 #include "TypeTraits.h"
Note: See TracChangeset
for help on using the changeset viewer.