//----------------------------------------------------------------------------- public void snapCamera(eCamSnaps snap) { float worldX = TerrainGlobals.getTerrain().getWorldSizeX(); float halfWorldX = worldX / 2.0f; Vector3 center = new Vector3(halfWorldX, 0, halfWorldX); GameCamLookat.X = -30; GameCamLookat.Y = 50; GameCamLookat.Z = 30; switch (snap) { case eCamSnaps.cCamSnap_RTS: mEye.X = mLookAt.X - GameCamLookat.X; mEye.Z = mLookAt.Z - GameCamLookat.Z; mEye.Y = mLookAt.Y + GameCamLookat.Y; break; case eCamSnaps.cCamSnap_Map_Center: mEye.X = halfWorldX; mEye.Z = halfWorldX + 0.5f; mEye.Y = mLookAt.Y + GameCamLookat.Y; mLookAt = center; break; case eCamSnaps.cCamSnap_posY: mEye.X = halfWorldX; mEye.Z = halfWorldX + 0.5f; mEye.Y = worldX * 2; mLookAt = center; break; case eCamSnaps.cCamSnap_posX: mEye.X = worldX * 2; mEye.Z = halfWorldX; mEye.Y = 0; mLookAt = center; break; case eCamSnaps.cCamSnap_negX: mEye.X = -worldX; mEye.Z = halfWorldX; mEye.Y = 0; mLookAt = center; break; case eCamSnaps.cCamSnap_posZ: mEye.X = halfWorldX; mEye.Z = worldX * 2; mEye.Y = 0; mLookAt = center; break; case eCamSnaps.cCamSnap_negZ: mEye.X = halfWorldX; mEye.Z = -worldX; mEye.Y = 0; mLookAt = center; break; case eCamSnaps.cCamSnap_ModelView: { Vector3 modelCenter = mModelBoundingBox.getCenter(); float sizeX = mModelBoundingBox.max.X - mModelBoundingBox.min.X; float sizeY = mModelBoundingBox.max.Y - mModelBoundingBox.min.Y; float sizeZ = mModelBoundingBox.max.Z - mModelBoundingBox.min.Z; float maxSize = float.MinValue; maxSize = Math.Max(maxSize, sizeX); maxSize = Math.Max(maxSize, sizeY); maxSize = Math.Max(maxSize, sizeZ); float cameraDistanceXZ = maxSize * 2.0f; float cameraDistanceY = maxSize * 1.0f; mLookAt = modelCenter; mEye = modelCenter + new Vector3(cameraDistanceXZ, cameraDistanceY, cameraDistanceXZ); } break; } }
//----------------------------------------------------------------------------- public void CameraMovement() { //camera controls float incAmt = 2 * SmartSpeedupCamera(); Dir = mEye - mLookAt; mRight = Vector3.Cross(Dir, mUp); mRight = BMathLib.Normalize(mRight); Vector3 ndir = -Dir; ndir.Y = 0; ndir = BMathLib.Normalize(ndir); Vector3 kDir = BMathLib.Normalize(Dir); Vector3 inc; Matrix matRotation; int mouseDeltaX = 0; int mouseDeltaY = 0; Point currPos = Point.Empty; UIManager.GetCursorPos(ref currPos); if (mbFirstMouseIntput) { mLastMousePosX = currPos.X; mLastMousePosY = currPos.Y; mbFirstMouseIntput = false; } else { mouseDeltaX = mLastMousePosX - currPos.X; mouseDeltaY = mLastMousePosY - currPos.Y; mLastMousePosX = currPos.X; mLastMousePosY = currPos.Y; } switch (mCamMode) { case eCamModes.cCamMode_Free: case eCamModes.cCamMode_RTS: if (mCamMode == eCamModes.cCamMode_RTS) //EDGE PUSH MODE { const int edgeAmt = 20; Point size = new Point(); BRenderDevice.getParentWindowSize(ref size); Point cursorPos = Point.Empty; UIManager.GetCursorPos(ref cursorPos); if (cursorPos.Y < edgeAmt) { CheckAssignMovement(out inc, mLookAt, ndir * incAmt); mEye += inc; mLookAt += inc; } else if (cursorPos.Y > size.Y - edgeAmt) { CheckAssignMovement(out inc, mLookAt, -ndir * incAmt); mEye += inc; mLookAt += inc; } else if (cursorPos.X < edgeAmt) { CheckAssignMovement(out inc, mLookAt, -mRight * incAmt); mEye += inc; mLookAt += inc; } else if (cursorPos.X > size.X - edgeAmt) { CheckAssignMovement(out inc, mLookAt, mRight * incAmt); mEye += inc; mLookAt += inc; } } //artist camera controls { Dir.Scale(Zoom); Zoom = 1; if ((UIManager.GetAsyncKeyStateB(Key.LeftShift)) || (UIManager.GetAsyncKeyStateB(Key.RightShift)) || (UIManager.GetMouseButtonDown(UIManager.eMouseButton.cMiddle))) { if (UIManager.GetAsyncKeyStateB(Key.LeftControl) || UIManager.GetAsyncKeyStateB(Key.RightControl)) { if (mbInvertZoom == true) { mouseDeltaY = -1 * mouseDeltaY; } Zoom = 1 + mouseDeltaY / 100f; mEye = mLookAt + Dir; } else if (!UIManager.GetMouseButtonDown(UIManager.eMouseButton.cLeft)) { //Rotation! if (mouseDeltaX != 0 || mouseDeltaY != 0) { matRotation = Matrix.RotationAxis(mRight, Geometry.DegreeToRadian((float)-mouseDeltaY / 5.0f)); Dir.TransformCoordinate(matRotation); Vector3 t = Vector3.Normalize(Dir); if (Vector3.Dot(t, mUp) < mCamDotLimit && Vector3.Dot(t, mUp) > -mCamDotLimit) { mEye = mLookAt + Dir; } matRotation = Matrix.RotationY(Geometry.DegreeToRadian((float)-mouseDeltaX / 5.0f)); Dir.TransformCoordinate(matRotation); t = Vector3.Normalize(Dir); if (Vector3.Dot(t, mUp) < mCamDotLimit && Vector3.Dot(t, mUp) > -mCamDotLimit) { mEye = mLookAt + Dir; } } } // Do nothing if control key is pressed if (UIManager.GetAsyncKeyStateB(Key.LeftControl) || UIManager.GetAsyncKeyStateB(Key.RightControl)) { return; } } else { if (UIManager.GetAsyncKeyStateB(Key.W) || UIManager.GetAsyncKeyStateB(Key.UpArrow) || UIManager.GetAsyncKeyStateB(Key.NumPad8)) { CheckAssignMovement(out inc, mLookAt, ndir * incAmt); mEye += inc; mLookAt += inc; } if (UIManager.GetAsyncKeyStateB(Key.S) || UIManager.GetAsyncKeyStateB(Key.DownArrow) || UIManager.GetAsyncKeyStateB(Key.NumPad2)) { CheckAssignMovement(out inc, mLookAt, -ndir * incAmt); mEye += inc; mLookAt += inc; } if (UIManager.GetAsyncKeyStateB(Key.A) || UIManager.GetAsyncKeyStateB(Key.LeftArrow) || UIManager.GetAsyncKeyStateB(Key.NumPad4)) { CheckAssignMovement(out inc, mLookAt, -mRight * incAmt); mEye += inc; mLookAt += inc; } if (UIManager.GetAsyncKeyStateB(Key.D) || UIManager.GetAsyncKeyStateB(Key.RightArrow) || UIManager.GetAsyncKeyStateB(Key.NumPad6)) { CheckAssignMovement(out inc, mLookAt, mRight * incAmt); mEye += inc; mLookAt += inc; } } } break; case eCamModes.cCamMode_ModelView: { if (UIManager.GetMouseButtonDown(UIManager.eMouseButton.cMiddle) && (UIManager.GetAsyncKeyStateB(Key.LeftAlt) || UIManager.GetAsyncKeyStateB(Key.RightAlt))) { // Orbit camera if (mouseDeltaX != 0 || mouseDeltaY != 0) { matRotation = Matrix.RotationAxis(mRight, Geometry.DegreeToRadian((float)-mouseDeltaY / 5.0f)); Dir.TransformCoordinate(matRotation); Vector3 t = Vector3.Normalize(Dir); if (Vector3.Dot(t, mUp) < mCamDotLimit && Vector3.Dot(t, mUp) > -mCamDotLimit) { mEye = mLookAt + Dir; } matRotation = Matrix.RotationY(Geometry.DegreeToRadian((float)-mouseDeltaX / 5.0f)); Dir.TransformCoordinate(matRotation); t = Vector3.Normalize(Dir); if (Vector3.Dot(t, mUp) < mCamDotLimit && Vector3.Dot(t, mUp) > -mCamDotLimit) { mEye = mLookAt + Dir; } } } else if (UIManager.GetMouseButtonDown(UIManager.eMouseButton.cMiddle)) { // Translate (pan) float distFromModel = Vector3.Length(mEye - mModelBoundingBox.getCenter()); float translationFactor = distFromModel / 1400.0f; float upInc = (float)mouseDeltaY * translationFactor; float rightInc = (float)mouseDeltaX * translationFactor; Vector3 up = Vector3.Cross(Dir, mRight); up = BMathLib.Normalize(up); Vector3 tranlation = up * upInc + mRight * rightInc; mEye += tranlation; mLookAt += tranlation; } if (UIManager.WheelDelta != 0) { // Zoom float distFromModel = Vector3.Length(mEye - mModelBoundingBox.getCenter()); float translationFactor = -distFromModel / 10.0f; mEye += kDir * (translationFactor * (UIManager.WheelDelta / 120f)); } } break; } }