// this function update the interpolated position of the Camera void UpdateCamera (dFloat interpolationParam) { dFloat rollAngle; dFloat yawAngle; yawAngle = gPrevYawAngle + (gYawAngle - gPrevYawAngle) * interpolationParam; rollAngle = gPrevRollAngle + (gRollAngle - gPrevRollAngle) * interpolationParam; dMatrix cameraDirMat (dRollMatrix(rollAngle) * dYawMatrix(yawAngle)); dVector eye (gPrevCameraEyepoint + (gCameraEyepoint - gPrevCameraEyepoint).Scale (interpolationParam)); dVector target (eye + cameraDirMat.m_front); // set up the view orientation looking at the origin from slightly above SetCamera (eye, target); }
// This function read KeyBoard and Mouse control to control this scene // The control are read at the simulation rate, and two states are kept void InputControl(NewtonWorld* world) { // read the mouse position and set the camera direction static dVector mouse0 (GetMousePos()); dVector mouse1 (GetMousePos()); gPrevYawAngle = gYawAngle; gPrevRollAngle = gRollAngle; if (!MousePick (world, mouse1)) { int leftKetDown; leftKetDown = IsKeyDown (KeyCode_L_BUTTON); // we are not in mouse pick mode, then we are in camera tracking mode if (leftKetDown) { // when click left mouse button the first time, we reset the camera // convert the mouse x position to delta yaw angle if (mouse1.m_x > (mouse0.m_x + 1)) { gYawAngle += 1.0f * 3.1416f / 180.0f; if (gYawAngle > (360.0f * 3.1416f / 180.0f)) { gYawAngle -= (360.0f * 3.1416f / 180.0f); } } else if (mouse1.m_x < (mouse0.m_x - 1)) { gYawAngle -= 1.0f * 3.1416f / 180.0f; if (gYawAngle < 0.0f) { gYawAngle += (360.0f * 3.1416f / 180.0f); } } if (mouse1.m_y > (mouse0.m_y + 1)) { gRollAngle += 1.0f * 3.1416f / 180.0f; if (gRollAngle > (80.0f * 3.1416f / 180.0f)) { gRollAngle = 80.0f * 3.1416f / 180.0f; } } else if (mouse1.m_y < (mouse0.m_y - 1)) { gRollAngle -= 1.0f * 3.1416f / 180.0f; if (gRollAngle < -(80.0f * 3.1416f / 180.0f)) { gRollAngle = -80.0f * 3.1416f / 180.0f; } } dMatrix cameraDirMat (dRollMatrix(gRollAngle) * dYawMatrix(gYawAngle)); gCurrCameraDir = cameraDirMat.m_front; } } // save mouse position and left mouse key state for next frame mouse0 = mouse1; // camera control gPrevCameraEyepoint = gCameraEyepoint; if (IsKeyDown ('W')) { gCameraEyepoint += gCurrCameraDir.Scale (CAMERA_SPEED / 60.0f); } else if (IsKeyDown ('S')) { gCameraEyepoint -= gCurrCameraDir.Scale (CAMERA_SPEED / 60.0f); } if (IsKeyDown ('D')) { dVector up (0.0f, 1.0f, 0.0f); dVector right (gCurrCameraDir * up); gCameraEyepoint += right.Scale (CAMERA_SPEED / 60.0f); } else if (IsKeyDown ('A')) { dVector up (0.0f, 1.0f, 0.0f); dVector right (gCurrCameraDir * up); gCameraEyepoint -= right.Scale (CAMERA_SPEED / 60.0f); } }
// Keyboard handler. void Keyboard() { // check for termination if (dGetKeyState (VK_ESCAPE) & 0x8000) { exit(0); } // read the mouse position and set the camera direction static MOUSE_POINT mouse0; static dFloat yawAngle = 90.0f * 3.1416 / 180.0f; static dFloat rollAngle = 0.0f; MOUSE_POINT mouse1; GetCursorPos(mouse1); if (dGetKeyState (VK_LBUTTON) & 0x8000) { if (mouse1.x > (mouse0.x + 1)) { yawAngle += 1.0f * 3.1416 / 180.0f; if (yawAngle > (360.0f * 3.1416 / 180.0f)) { yawAngle -= (360.0f * 3.1416 / 180.0f); } } else if (mouse1.x < (mouse0.x - 1)) { yawAngle -= 1.0f * 3.1416 / 180.0f; if (yawAngle < 0.0f) { yawAngle += (360.0f * 3.1416 / 180.0f); } } if (mouse1.y > (mouse0.y + 1)) { rollAngle += 1.0f * 3.1416 / 180.0f; if (rollAngle > (80.0f * 3.1416 / 180.0f)) { rollAngle = 80.0f * 3.1416 / 180.0f; } } else if (mouse1.y < (mouse0.y - 1)) { rollAngle -= 1.0f * 3.1416 / 180.0f; if (rollAngle < -(80.0f * 3.1416 / 180.0f)) { rollAngle = -80.0f * 3.1416 / 180.0f; } } dMatrix cameraDirMat (dgRollMatrix(rollAngle) * dgYawMatrix(yawAngle)); cameraDir = cameraDirMat.m_front; } mouse0 = mouse1; // camera control if (dGetKeyState ('W') & 0x8000) { cameraEyepoint += cameraDir.Scale (CAMERA_SPEED / 60.0f); } else if (dGetKeyState ('S') & 0x8000) { cameraEyepoint -= cameraDir.Scale (CAMERA_SPEED / 60.0f); } if (dGetKeyState ('D') & 0x8000) { dVector up (0.0f, 1.0f, 0.0f); dVector right (cameraDir * up); cameraEyepoint += right.Scale (CAMERA_SPEED / 60.0f); } else if (dGetKeyState ('A') & 0x8000) { dVector up (0.0f, 1.0f, 0.0f); dVector right (cameraDir * up); cameraEyepoint -= right.Scale (CAMERA_SPEED / 60.0f); } }