src/mouse.cpp

Go to the documentation of this file.
00001 #include "mouse.h"
00002 
00003 void MOUSE::Update(CAMERA & cam, int screenw, int screenh, float timefactor, float fps)
00004 {
00005         int mousePos_x,mousePos_y;
00006         int middleX = screenw >> 1;                        // This is a binary shift to get half the width
00007         int middleY = screenh >> 1;                        // This is a binary shift to get half the height
00008 
00009         // Get the mouse's current X,Y position
00010         SDL_GetMouseState(&mousePos_x,&mousePos_y);
00011 
00012         // If our cursor is still in the middle, we never moved... so don't update the screen
00013         //if( (mousePos_x == middleX) && (mousePos_y == middleY) ) return;
00014 
00015         // Set the mouse position to the middle of our window
00016         SDL_WarpMouse(middleX, middleY);
00017 
00018         int angleY = 0;         // This is the direction for looking up or down
00019         int angleZ = 0;         // This will be the value we need to rotate around the Y axis (Left and Right)
00020 
00021         // Get the direction the mouse moved in, but bring the number down to a reasonable amount
00022         angleY = ( (middleX - mousePos_x) );            
00023         angleZ = ( (middleY - mousePos_y) );
00024 
00025 /*
00026         if(SDL_GetMouseState(NULL, NULL)&SDL_BUTTON(2))
00027         {
00028                 if (!mousebounce)
00029                 {
00030                         mousesteering = !mousesteering;
00031                         mousebounce = true;
00032                 }
00033         }
00034         else
00035                 mousebounce = false;
00036 */      
00037 //      if (mousesteering)
00038                 UpdateSteering(angleY, angleZ);
00039 //      else
00040 //              UpdateCamera(cam, angleY, angleZ, timefactor, fps);
00041 }
00042 
00043 MOUSE::MOUSE()
00044 {
00045         mousezoom = 0.5;
00046         steer_x = 0;
00047         steer_y = 0;
00048 //      mousebounce = false;
00049 //      UpdateSettings();
00050 //      ClearBounce();
00051 }
00052 
00053 void MOUSE::UpdateCamera(CAMERA & cam, int x, int y, float timefactor, float fps)
00054 {
00055         static float currentRotX = 0.0f, currentRotY = 0.0f;
00056 
00057         float angleY = (float)x / 500.0f;
00058         float angleZ = (float)y / 500.0f;
00059 
00060         // Here we keep track of the current rotation (for up and down) so that
00061         // we can restrict the camera from doing a full 360 loop.
00062         currentRotX -= angleZ;
00063         currentRotY -= angleY;
00064 
00065         double temp_radius = sqrt(cam.position.x*cam.position.x+cam.position.z*cam.position.z+(cam.position.y+EARTH_RADIUS)*(cam.position.y+EARTH_RADIUS));
00066         double temp_long = atan2((double)(cam.position.y+EARTH_RADIUS),(double)cam.position.x);
00067         double temp_lat = acos(cam.position.z/temp_radius);
00068         temp_lat -= 1.570796f;
00069         temp_long -= 1.570796f;
00070 
00071         VERTEX tpos = cam.position;
00072         tpos.y += EARTH_RADIUS;
00073         tpos = tpos.normalize();
00074 
00075         float wrapat = 3.141592f*2.0f;
00076         if (currentRotY > wrapat)
00077                 currentRotY -= wrapat;
00078         if (currentRotY < -wrapat)
00079                 currentRotY += wrapat;
00080 
00081         // If the current rotation (in radians) is greater than pi/2 ish, we want to cap it.
00082         if(currentRotX > 1.4f)
00083                 currentRotX = 1.4f;
00084         // Check if the rotation is below -1.0, if so we want to make sure it doesn't continue
00085         else if(currentRotX < -1.4f)
00086                 currentRotX = -1.4f;
00087         // Otherwise, we can rotate the view around our position
00088         //else
00089 
00090         // To find the axis we need to rotate around for up and down
00091         // movements, we need to get a perpendicular vector from the
00092         // camera's view vector and up vector.  This will be the axis.
00093         //CVector3 vAxis = Cross(m_vView - m_vPosition, m_vUpVector);
00094         //vAxis = Normalize(vAxis);
00095         VERTEX upvector, rightvector;
00096         upvector.x = 0; upvector.y = 1; upvector.z = 0;
00097         rightvector.x = 1; rightvector.y = 0; rightvector.z = 0;
00098         //VERTEX curangley = cam.dir.RotateVec(upvector);
00099         //VERTEX vAxis = curangley.cross(upvector);
00100 
00101         // Rotate around our perpendicular axis and along the y-axis
00102         //RotateView(angleZ, vAxis.x, vAxis.y, vAxis.z);
00103         //RotateView(angleY, 0, 1, 0);
00104 
00105         //cam.Rotate(-angleZ, vAxis.x, vAxis.y, vAxis.z);
00106         //cam.Rotate(-angleZ, 1, 0, 0);
00107         //cam.Rotate(-angleY, 0,1,0);
00108         //cam.Rotate(-angleY, curangley.x,curangley.y,curangley.z);
00109 
00110         //cam.dir.SetEuler(currentRotX, currentRotY, 0);
00111 
00112         QUATERNION curveoffset;
00113         curveoffset.SetEuler(temp_lat, temp_long, temp_lat);
00114         VERTEX up, posx, posz;
00115         up.y = 1.0f;
00116         posx.x = 1.0f;
00117         posz.z = 1.0f;
00118         VERTEX newup = curveoffset.RotateVec(up);
00119         VERTEX newx = curveoffset.RotateVec(posx);
00120         VERTEX newz = curveoffset.RotateVec(posz);
00121         /*newx.DebugPrint();
00122         newup.DebugPrint();
00123         tpos.DebugPrint();*/
00124 
00125         QUATERNION rightangle;
00126         rightangle.SetAxisAngle(1.570796f, 0,0,1);
00127         newx = rightangle.RotateVec(tpos);
00128         //newx.DebugPrint();
00129         //cout << endl;
00130 
00131         QUATERNION cor, qx, qy, qz;
00132 
00133         cor.SetAxisAngle(temp_long, 0, 0, 1);
00134 
00135         qx.SetAxisAngle(currentRotX, 1, 0, 0);
00136 
00137         qy.SetAxisAngle(currentRotY, -tpos.x, tpos.y, -tpos.z);
00138         //qy.SetAxisAngle(currentRotY, 0.1, 0.9, 0);
00139         //qy.SetAxisAngle(currentRotY, 0, 1, 0);
00140         //cout << currentRotX << endl;
00141         //cout << currentRotY << endl;
00142         //qz.SetAxisAngle(temp_lat+temp_long, newz.x, newz.y, newz.z);
00143         //qz.SetAxisAngle(0.2f, newz.x, newz.y, newz.z);
00144 
00145 
00146         viewdir = cor*qx*qy*qz;
00147 
00148         //cam.dir.SetEuler(currentRotX-temp_lat, currentRotY+temp_long, 0);
00149         //cam.dir.SetEuler(-temp_lat, currentRotY+temp_long*2.0f, 0);
00150 
00151         if(SDL_GetMouseState(NULL, NULL)&SDL_BUTTON(1))
00152         {
00153                 mousezoom += ZOOMSPEED * timefactor/fps;
00154                 if (mousezoom > 1.0f)
00155                         mousezoom = 1.0f;
00156         }
00157 
00158         if(SDL_GetMouseState(NULL, NULL)&SDL_BUTTON(3))
00159         {
00160                 mousezoom -= ZOOMSPEED * timefactor/fps;
00161                 if (mousezoom < 0.0f)
00162                         mousezoom = 0.0f;
00163         }
00164 }
00165 
00166 void MOUSE::UpdateSteering(int x, int y)
00167 {
00168         float fx = ((float) x*mouse_sensitivity_x) / 1500.0;
00169         float fy = ((float) y*mouse_sensitivity_y) / 250.0;
00170 
00171         steer_x += fx;
00172         steer_y += fy;
00173 
00174         if (steer_x > 1.0)
00175                 steer_x = 1.0;
00176         if (steer_y > 1.0)
00177                 steer_y = 1.0;
00178 
00179         if (steer_x < -1.0)
00180                 steer_x = -1.0;
00181         if (steer_y < -1.0)
00182                 steer_y = -1.0;
00183 
00184         //cout << steer_x << "," << steer_y << endl;
00185 }
00186 
00187 bool MOUSE::GetMouseControls(float * x, float * y, bool * click_l, bool * click_r)
00188 {
00189         *x = steer_x;
00190         *y = steer_y;
00191 
00192         if (steer_y >= 0 && steer_y < deadzone_y)
00193                 *y = 0;
00194         else if (steer_y < 0 && steer_y > -deadzone_y)
00195                 *y = 0;
00196         else
00197         {
00198                 if (steer_y < 0)
00199                         *y = (steer_y + deadzone_y)*(1.0/(1.0-deadzone_y));
00200                 else
00201                         *y = (steer_y - deadzone_y)*(1.0/(1.0-deadzone_y));
00202         }
00203 
00204         upbounce = (SDL_GetMouseState(NULL, NULL)&SDL_BUTTON(5));
00205         downbounce = (SDL_GetMouseState(NULL, NULL)&SDL_BUTTON(4));
00206         lbounce = (SDL_GetMouseState(NULL, NULL)&SDL_BUTTON(3));
00207         mbounce = (SDL_GetMouseState(NULL, NULL)&SDL_BUTTON(2));
00208         rbounce = (SDL_GetMouseState(NULL, NULL)&SDL_BUTTON(1));
00209 
00210         *click_l = (lbounce && !lastl);
00211         *click_r = (rbounce && !lastr);
00212 
00213         //ClearBounce();
00214 
00215         lastup = upbounce;
00216         lastdown = downbounce;
00217         lastl = lbounce;
00218         lastm = mbounce;
00219         lastr = rbounce;
00220 
00221         return true;
00222 }
00223 
00224 bool MOUSE::IsPressed( int which_btn )
00225 {
00226         return ( SDL_GetMouseState( NULL, NULL ) & SDL_BUTTON( which_btn ) );
00227 }
00228 
00229 void MOUSE::GetMousePos( int &xpos, int &ypos )
00230 {
00231         int mouse_x, mouse_y;
00232 
00233         // Get the mouse's current X,Y position
00234         SDL_GetMouseState(&mouse_x, &mouse_y);
00235 
00236         xpos = mouse_x;
00237         ypos = mouse_y;
00238 }
00239 
00240 void MOUSE::GetMouseButtons( bool &btn_l, bool &btn_m, bool &btn_r, bool &scroll_up, bool &scroll_down )
00241 {
00242         upbounce = ( SDL_GetMouseState( NULL, NULL ) & SDL_BUTTON( 5 ) );
00243         downbounce = ( SDL_GetMouseState( NULL, NULL ) & SDL_BUTTON( 4 ) );
00244         rbounce = ( SDL_GetMouseState( NULL, NULL ) & SDL_BUTTON( 3 ) );
00245         mbounce = ( SDL_GetMouseState( NULL, NULL ) & SDL_BUTTON( 2 ) );
00246         lbounce = ( SDL_GetMouseState( NULL, NULL ) & SDL_BUTTON( 1 ) );
00247 
00248         scroll_up = upbounce;
00249         scroll_down = downbounce;
00250         btn_r = rbounce;
00251         btn_m = mbounce;
00252         btn_l = lbounce;
00253 }
00254 
00255 void MOUSE::UpdateSettings()
00256 {
00257         settings.Get( "mouse.xsens", mouse_sensitivity_x );
00258         settings.Get( "mouse.ysens", mouse_sensitivity_y );
00259         settings.Get( "mouse.ydead", deadzone_y );
00260 }

Generated on Thu Oct 19 04:05:54 2006 by  doxygen 1.4.6