Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

AMath.cpp

Go to the documentation of this file.
00001 /*
00002 CS Senior Project 2003
00003 Team : Leftfield
00004 Project : ModernWarfare
00005 Members :
00006 - Russ Christensen              <rchriste@cs.utah.edu>
00007 - Todd Smith                    <tcsmith@cs.utah.edu>
00008 - Usit Duongsaa                 <duongsaa@cs.utah.edu>
00009 Copyright 2003 Russ Christensen, Usit Duongsaa, and Todd Smith. All rights reserved.
00010 
00011 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
00012 
00013 Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 
00014 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 
00015 THIS SOFTWARE IS PROVIDED BY RUSS CHRISTENSEN, USIT DUONGSAA, AND TODD SMITH ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RUSS, USIT, TODD OR OTHER CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00016 */
00017 
00018 
00023 #include "AEngine.h"
00024 #include "AState.h"
00025 #include "AConstants.h"
00026 #include "ADevice.h"
00027 #include "ASharedFunc.h"
00028 
00029 
00030 //------------------------------------------------------------------------------------
00031 Vec3D math_translateScreenToWorldPlane( const Vec2D pt, FLOAT z )
00032 {
00033         // Get screen space vector
00034         Vec3D v;
00035     v.x =  ( 2.0f*pt[0] - 1 ) / AS.camera_projMatrix._11;
00036     v.y = -( 2.0f*pt[1] - 1 ) / AS.camera_projMatrix._22;
00037     v.z =  1.0f;
00038 
00039         // Get the inverse view matrix
00040     D3DXMATRIX inv;
00041         D3DXMatrixInverse( &inv, NULL, &AS.camera_viewMatrix );
00042         
00043     // Transform to world space vector
00044         Vec3D pt1, pt2, dir;
00045     dir.x  = v.x*inv._11 + v.y*inv._21 + v.z*inv._31;
00046     dir.y  = v.x*inv._12 + v.y*inv._22 + v.z*inv._32;
00047     dir.z  = v.x*inv._13 + v.y*inv._23 + v.z*inv._33;
00048     pt1.x = inv._41;
00049     pt1.y = inv._42;
00050     pt1.z = inv._43;
00051         pt2 = pt1 + 10000.0f * dir;
00052 
00053         // construct a plane with specified height
00054         D3DXPLANE plane;
00055         Vec3D planePt(0,0,z), planeNormal(0,0,1);
00056         D3DXPlaneFromPointNormal( &plane, &planePt, &planeNormal );
00057 
00058         // find where the vector intersect with the plane
00059         Vec3D intersectPt;
00060         D3DXPlaneIntersectLine( &intersectPt, &plane, &pt1, &pt2 );
00061 
00062         return intersectPt;
00063 }
00064 //------------------------------------------------------------------------------------
00065 Vec2D math_translateWorldToScreen( const Vec3D pt )
00066 {       
00067         Vec3D scrPt;
00068         D3DXVec3TransformCoord( &scrPt, &pt, &AS.camera_world2projMatrix );
00069         if(scrPt.z > 1)                         // fix "off-screen line clipping bug"
00070         {
00071                 scrPt.x = -scrPt.x;
00072                 scrPt.y = -scrPt.y;
00073         }
00074         return Vec2D( 0.5f+0.5f*scrPt[0], 0.5f-0.5f*scrPt[1] );
00075 }
00076 //------------------------------------------------------------------------------------
00077 Vec3D math_translateObjectToWorld( const Vec3D objPos, const Vec3D objDir, const Vec3D objPt )
00078 {
00079         Matx matTrans, matTheta, matFinal;
00080         Vec3D result;
00081 
00082         D3DXMatrixTranslation( &matTrans, objPos[0], objPos[1], objPos[2] );
00083         D3DXMatrixRotationZ( &matTheta, D3DXToRadian(objDir[0]) );      
00084         matFinal = matTheta * matTrans;
00085         D3DXVec3TransformCoord( &result, &objPt, &matFinal );
00086         return result;
00087 }
00088 //------------------------------------------------------------------------------------
00089 FLOAT math_dist2D( const Vec2D &a, const Vec2D &b )
00090 {
00091         Vec2D delta = a-b;
00092         return D3DXVec2Length(&delta);
00093 }
00094 //------------------------------------------------------------------------------------
00095 FLOAT math_dist3D( const Vec3D &a, const Vec3D &b )
00096 {
00097         Vec3D delta = a-b;
00098         return D3DXVec3Length(&delta);
00099 }
00100 //------------------------------------------------------------------------------------
00101 FLOAT math_distPlanar3D( const Vec3D &a, const Vec3D &b )
00102 {
00103         Vec3D delta = a-b;
00104         return sqrtf( delta.x*delta.x + delta.y*delta.y );
00105 }
00106 //------------------------------------------------------------------------------------
00107 FLOAT math_clipAngle( FLOAT degree )
00108 {
00109         if( degree < 0.0f )             return degree + 360.0f;
00110         if( degree >= 360.0f )  return degree - 360.0f;
00111         return degree;
00112 }
00113 //------------------------------------------------------------------------------------
00114 FLOAT math_getDir( const Vec3D &from, const Vec3D &to )
00115 {
00116         Vec3D targetVec = to - from;
00117         return math_clipAngle( 90-D3DXToDegree( atan2f( targetVec.x, targetVec.y ) ) );
00118 }
00119 //------------------------------------------------------------------------------------
00120 FLOAT math_deltaDir( const FLOAT currentDir, const FLOAT newDir )
00121 {
00122         FLOAT delta;
00123         if(newDir>currentDir)
00124         {
00125                 delta = newDir - currentDir;
00126                 if(delta>180.0f) delta = delta - 360.0f;
00127         }
00128         else
00129         {
00130                 delta = newDir - currentDir;
00131                 if(delta<-180.0f) delta = 360.0f - delta;
00132         }
00133         return delta;
00134 }
00135 //------------------------------------------------------------------------------------

Generated on Wed Apr 23 05:50:13 2003 for Modern Warfare by doxygen1.3-rc2