private void ChangeCameraBehavior(Camera.Behavior behavior) { if (camera.CurrentBehavior == behavior) return; camera.CurrentBehavior = behavior; // Position the camera behind and 30 degrees above the target. if (behavior == Camera.Behavior.Orbit) camera.Rotate(0.0f, -30.0f, 0.0f); }
/// <summary> /// Constructs a new instance of the CameraComponent class. The /// camera will have a spectator behavior, and will be initially /// positioned at the world origin looking down the world negative /// z axis. An initial perspective projection matrix is created /// as well as setting up initial key bindings to the actions. /// </summary> public CameraComponent(BlockGame game) : base(game) { camera = new Camera(); camera.CurrentBehavior = Camera.Behavior.Spectator; movingAlongPosX = false; movingAlongNegX = false; movingAlongPosY = false; movingAlongNegY = false; movingAlongPosZ = false; movingAlongNegZ = false; savedMousePosX = -1; savedMousePosY = -1; rotationSpeed = DEFAULT_SPEED_ROTATION; orbitRollSpeed = DEFAULT_SPEED_ORBIT_ROLL; flightYawSpeed = DEFAULT_SPEED_FLIGHT_YAW; mouseWheelSpeed = DEFAULT_SPEED_MOUSE_WHEEL; mouseSmoothingSensitivity = DEFAULT_MOUSE_SMOOTHING_SENSITIVITY; acceleration = new Vector3(DEFAULT_ACCELERATION_X, DEFAULT_ACCELERATION_Y, DEFAULT_ACCELERATION_Z); velocity = new Vector3(DEFAULT_VELOCITY_X, DEFAULT_VELOCITY_Y, DEFAULT_VELOCITY_Z); mouseSmoothingCache = new Vector2[MOUSE_SMOOTHING_CACHE_SIZE]; mouseIndex = 0; mouseMovement = new Vector2[2]; mouseMovement[0].X = 0.0f; mouseMovement[0].Y = 0.0f; mouseMovement[1].X = 0.0f; mouseMovement[1].Y = 0.0f; Rectangle clientBounds = Game.Window.ClientBounds; float aspect = (float)clientBounds.Width / (float)clientBounds.Height; Perspective(Camera.DEFAULT_FOVX, aspect, Camera.DEFAULT_ZNEAR, Camera.DEFAULT_ZFAR); actionKeys = new Dictionary<Actions, Keys>(); actionKeys.Add(Actions.FlightYawLeftPrimary, Keys.Left); actionKeys.Add(Actions.FlightYawLeftAlternate, Keys.A); actionKeys.Add(Actions.FlightYawRightPrimary, Keys.Right); actionKeys.Add(Actions.FlightYawRightAlternate, Keys.D); actionKeys.Add(Actions.MoveForwardsPrimary, Keys.Up); actionKeys.Add(Actions.MoveForwardsAlternate, Keys.W); actionKeys.Add(Actions.MoveBackwardsPrimary, Keys.Down); actionKeys.Add(Actions.MoveBackwardsAlternate, Keys.S); actionKeys.Add(Actions.MoveDownPrimary, Keys.Q); actionKeys.Add(Actions.MoveDownAlternate, Keys.PageDown); actionKeys.Add(Actions.MoveUpPrimary, Keys.E); actionKeys.Add(Actions.MoveUpAlternate, Keys.PageUp); actionKeys.Add(Actions.OrbitRollLeftPrimary, Keys.Left); actionKeys.Add(Actions.OrbitRollLeftAlternate, Keys.A); actionKeys.Add(Actions.OrbitRollRightPrimary, Keys.Right); actionKeys.Add(Actions.OrbitRollRightAlternate, Keys.D); actionKeys.Add(Actions.StrafeRightPrimary, Keys.Right); actionKeys.Add(Actions.StrafeRightAlternate, Keys.D); actionKeys.Add(Actions.StrafeLeftPrimary, Keys.Left); actionKeys.Add(Actions.StrafeLeftAlternate, Keys.A); Game.Activated += new EventHandler<EventArgs>(HandleGameActivatedEvent); Game.Deactivated += new EventHandler<EventArgs>(HandleGameDeactivatedEvent); UpdateOrder = 1; }