void CCamera::CheckForMovement() { // Once we have the frame interval, we find the current speed float speed = kSpeed * g_FrameInterval; // Check if we hit the Up arrow or the 'w' key if( movingForward ) // if we are supposed to move the camera forward { // Move our camera forward by a positive SPEED MoveCamera(speed); } // Check if we hit the Down arrow or the 's' key if( movingBackward ) // if we are supposed to move the camera backward { // Move our camera backward by a negative SPEED MoveCamera(-speed); } // Check if we hit the Left arrow or the 'a' key if( movingLeft ) // if camera has to strafe towards its left { // Strafe the camera left StrafeCamera(-speed); } // Check if we hit the Right arrow or the 'd' key if( movingRight ) // if camera has to strafe towards its right { // Strafe the camera right StrafeCamera(speed); } }
void CCamera::CheckForMovement() { // Once we have the frame interval, we find the current speed float speed = kSpeed * g_FrameInterval; // Check if we hit the Up arrow or the 'w' key if(GetKeyState(VK_UP) & 0x80 || GetKeyState('W') & 0x80) { // Move our camera forward by a positive SPEED MoveCamera(speed); } // Check if we hit the Down arrow or the 's' key if(GetKeyState(VK_DOWN) & 0x80 || GetKeyState('S') & 0x80) { // Move our camera backward by a negative SPEED MoveCamera(-speed); } // Check if we hit the Left arrow or the 'a' key if(GetKeyState(VK_LEFT) & 0x80 || GetKeyState('A') & 0x80) { // Strafe the camera left StrafeCamera(-speed); } // Check if we hit the Right arrow or the 'd' key if(GetKeyState(VK_RIGHT) & 0x80 || GetKeyState('D') & 0x80) { // Strafe the camera right StrafeCamera(speed); } }
void CCamera::Keyboard() { if (MouseFlag) { float speed = kSpeed * g_FrameInterval; if ((GetKeyState(VK_UP) & 0x80) || (GetKeyState('W') & 0x80)) MoveCamera(speed); if ((GetKeyState(VK_DOWN) & 0x80) || (GetKeyState('S') & 0x80)) MoveCamera(-speed); if ((GetKeyState(VK_LEFT) & 0x80) || (GetKeyState('A') & 0x80)) StrafeCamera(-speed); if ((GetKeyState(VK_RIGHT) & 0x80) || (GetKeyState('D') & 0x80)) StrafeCamera(speed); } }
/** * Mouse active motion callback (when button is pressed) */ void MouseMotionCallback(int x, int y) { if (gPreviousMouseX >= 0 && gPreviousMouseY >= 0) { //compute delta float deltaX = x-gPreviousMouseX; float deltaY = y-gPreviousMouseY; gPreviousMouseX = x; gPreviousMouseY = y; //orbit, strafe, or zoom if (gMouseButton == GLUT_LEFT_BUTTON) { RotateCamera(deltaX, deltaY); } else if (gMouseButton == GLUT_MIDDLE_BUTTON) { StrafeCamera(deltaX, deltaY); } else if (gMouseButton == GLUT_RIGHT_BUTTON) { ZoomCamera(deltaY); } } else { gPreviousMouseX = x; gPreviousMouseY = y; } }
void CCamera::CheckForMovement() { /////// * /////////// * /////////// * NEW * /////// * /////////// * /////////// * // Once we have the delta time, we just need to find the current speed. // This is done by multiplying our speed by the elapsed time between frames. // We can then pass that speed into our camera movement functions. float speed = kSpeed * g_DT; /////// * /////////// * /////////// * NEW * /////// * /////////// * /////////// * // Check if we hit the Up arrow or the 'w' key if(GetKeyState(VK_UP) & 0x80 || GetKeyState('W') & 0x80) { // Move our camera forward by a positive SPEED MoveCamera(speed); } // Check if we hit the Down arrow or the 's' key if(GetKeyState(VK_DOWN) & 0x80 || GetKeyState('S') & 0x80) { // Move our camera backward by a negative SPEED MoveCamera(-speed); } // Check if we hit the Left arrow or the 'a' key if(GetKeyState(VK_LEFT) & 0x80 || GetKeyState('A') & 0x80) { // Strafe the camera left StrafeCamera(-speed); } // Check if we hit the Right arrow or the 'd' key if(GetKeyState(VK_RIGHT) & 0x80 || GetKeyState('D') & 0x80) { // Strafe the camera right StrafeCamera(speed); } }
void SpecialKeyCallback(int key, int x, int y) { switch(key) { case GLUT_KEY_LEFT: StrafeCamera(10,0); break; case GLUT_KEY_RIGHT: StrafeCamera(-10,0); break; case GLUT_KEY_DOWN: StrafeCamera(0,-10); break; case GLUT_KEY_UP: StrafeCamera(0,10); break; default: break; } glutPostRedisplay(); }
void Camera::CheckForMovement() { float speed = kSpeed; if (GetKeyState('W') & 0x80) { MoveCamera(speed); } // ÂÍÈÇ èëè S if (GetKeyState('S') & 0x80) { MoveCamera(-speed); } if (GetKeyState('A') & 0x80) { StrafeCamera(-speed); } if (GetKeyState('D') & 0x80) { StrafeCamera(speed); } }
void CCamera::CheckForMovement() { // Once we have the frame interval, we find the current speed float speed = (float)(kSpeed * g_FrameInterval); // Before we move our camera we want to store the old position. We then use // this data to test collision detection. CVector3 vOldPosition = Position(); /////// * /////////// * /////////// * NEW * /////// * /////////// * /////////// * // Store the old view vector to restore it if we collided backwards CVector3 vOldView = View(); // Use a flag to see if we movement backwards or not bool bMovedBack = false; /////// * /////////// * /////////// * NEW * /////// * /////////// * /////////// * // Check if we hit the Up arrow or the 'w' key if(GetKeyState(VK_UP) & 0x80 || GetKeyState('W') & 0x80) { // Move our camera forward by a positive SPEED MoveCamera(speed); } // Check if we hit the Down arrow or the 's' key if(GetKeyState(VK_DOWN) & 0x80 || GetKeyState('S') & 0x80) { // Move our camera backward by a negative SPEED MoveCamera(-speed); bMovedBack = true; } // Check if we hit the Left arrow or the 'a' key if(GetKeyState(VK_LEFT) & 0x80 || GetKeyState('A') & 0x80) { // Strafe the camera left StrafeCamera(-speed); } // Check if we hit the Right arrow or the 'd' key if(GetKeyState(VK_RIGHT) & 0x80 || GetKeyState('D') & 0x80) { // Strafe the camera right StrafeCamera(speed); } // Now that we moved, let's get the current position and test our movement // vector against the level data to see if there is a collision. CVector3 vCurrentPosition = Position(); // We will not use sphere collision from now on, but AABB collision (box) // CVector3 vNewPosition = g_Level.TraceSphere(vOldPosition, vCurrentPosition, 25.0f); /////// * /////////// * /////////// * NEW * /////// * /////////// * /////////// * // We are now using AABB collision now, so we no longer use sphere checks. // Below we pass into the TraceBox() function some generic Min and Max values // for our bounding box. We then get the new position if we collided CVector3 vNewPosition = g_Level.TraceBox(vOldPosition, vCurrentPosition, CVector3(-20, -50, -20), CVector3(20, 50, 20)); // We add some code below to make it so when we back into a wall, our view vector // doesn't keep going backwards, since we aren't moving backwards. If we don't // do this check, it will turn our camera around and look like a glitch. Not desired! // Check if we collided and we moved backwards if(g_Level.Collided() && bMovedBack) { // If or x or y didn't move, then we are backed into a wall so restore the view vector if(vNewPosition.x == vOldPosition.x || vNewPosition.z == vOldPosition.z) m_vView = vOldView; } /////// * /////////// * /////////// * NEW * /////// * /////////// * /////////// * // Set the new position that was returned from our trace function m_vPosition = vNewPosition; }
void CCamera::CheckForMovement() { // Once we have the frame interval, we find the current speed float speed = (float)(kSpeed * g_FrameInterval); // Store the last position and view of the camera CVector3 vOldPosition = Position(); CVector3 vOldView = View(); // Use a flag to see if we movement backwards or not bool bMovedBack = false; /////// * /////////// * /////////// * NEW * /////// * /////////// * /////////// * // Here is where we subtract the gravity acceleration from our velocity vector. // We then add that velocity vector to our camera to effect our camera (or player) // This is also how we handle the jump velocity when we hit space bar. // Notice that we multiply the gravity by the frame interval (dt). This makes // it so faster video cards don't do a 2 frame jump, while TNT2 cards do 20 frames :) // This is necessary to make every computer use the same movement and jump speed. g_vVelocity.y -= (float)(kGravity * g_FrameInterval); m_vPosition = m_vPosition + g_vVelocity; /////// * /////////// * /////////// * NEW * /////// * /////////// * /////////// * // Check if we hit the Up arrow or the 'w' key if(GetKeyState(VK_UP) & 0x80 || GetKeyState('W') & 0x80) { // Move our camera forward by a positive SPEED MoveCamera(speed); } // Check if we hit the Down arrow or the 's' key if(GetKeyState(VK_DOWN) & 0x80 || GetKeyState('S') & 0x80) { // Move our camera backward by a negative SPEED MoveCamera(-speed); bMovedBack = true; } // Check if we hit the Left arrow or the 'a' key if(GetKeyState(VK_LEFT) & 0x80 || GetKeyState('A') & 0x80) { // Strafe the camera left StrafeCamera(-speed); } // Check if we hit the Right arrow or the 'd' key if(GetKeyState(VK_RIGHT) & 0x80 || GetKeyState('D') & 0x80) { // Strafe the camera right StrafeCamera(speed); } // Now that we moved, let's get the current position and test our movement // vector against the level data to see if there is a collision. CVector3 vCurrentPosition = Position(); // Check for collision with AABB's and grab the new position CVector3 vNewPosition = g_Level.TraceBox(vOldPosition, vCurrentPosition, CVector3(-20, -50, -20), CVector3(20, 50, 20)); // Check if we collided and we moved backwards if(g_Level.Collided() && bMovedBack) { // If or x or y didn't move, then we are backed into a wall so restore the view vector if(vNewPosition.x == vOldPosition.x || vNewPosition.z == vOldPosition.z) m_vView = vOldView; } // Set the new position that was returned from our trace function m_vPosition = vNewPosition; /////// * /////////// * /////////// * NEW * /////// * /////////// * /////////// * // After we check for collision, we only want to add the velocity vector to // our view vector when we are falling. If we aren't on the ground then // we don't want to push the the camera view down to the ground. It's okay // if the position goes down because the collision detection fixes that so // we don't go through the ground, however, it's not natural to push the view // down too. Well, assuming is strong enough to push our face down to the ground :) if(!g_Level.IsOnGround()) m_vView = m_vView + g_vVelocity; else { // If we ARE on the ground, we want to get rid of the jump acceleration // that we add when the user hits the space bar. Below we check to see // if our velocity is below 0 then we are done with our jump and can just // float back to the ground by the gravity. We do also add our gravity // acceleration to the velocity every frame, so this resets this to zero // for that as well. if(g_vVelocity.y < 0) g_vVelocity.y = 0; } /////// * /////////// * /////////// * NEW * /////// * /////////// * /////////// * }
void CCamera::CheckForMovement() { // Once we have the frame interval, we find the current speed float speed = (float)(kSpeed * g_FrameInterval); /////// * /////////// * /////////// * NEW * /////// * /////////// * /////////// * // Before we move our camera we want to store the old position. We then use // this data to test collision detection. CVector3 vOldPosition = Position(); /////// * /////////// * /////////// * NEW * /////// * /////////// * /////////// * // Check if we hit the Up arrow or the 'w' key if(GetKeyState(VK_UP) & 0x80 || GetKeyState('W') & 0x80) { // Move our camera forward by a positive SPEED MoveCamera(speed); } // Check if we hit the Down arrow or the 's' key if(GetKeyState(VK_DOWN) & 0x80 || GetKeyState('S') & 0x80) { // Move our camera backward by a negative SPEED MoveCamera(-speed); } // Check if we hit the Left arrow or the 'a' key if(GetKeyState(VK_LEFT) & 0x80 || GetKeyState('A') & 0x80) { // Strafe the camera left StrafeCamera(-speed); } // Check if we hit the Right arrow or the 'd' key if(GetKeyState(VK_RIGHT) & 0x80 || GetKeyState('D') & 0x80) { // Strafe the camera right StrafeCamera(speed); } /////// * /////////// * /////////// * NEW * /////// * /////////// * /////////// * // Now that we moved, let's get the current position and test our movement // vector against the level data to see if there is a collision. CVector3 vCurrentPosition = Position(); // Here we call our function TraceSphere() to check our movement vector (last // and current position) against the world. We pass in a radius for our sphere // of 25. If there is anything in the range of our sphere, then we collide. CVector3 vNewPosition = g_Level.TraceSphere(vOldPosition, vCurrentPosition, 25.0f); // Set the new position that was returned from our trace function m_vPosition = vNewPosition; /////// * /////////// * /////////// * NEW * /////// * /////////// * /////////// * }