UEXDev

Camera Optimierung

May 28th, 2012
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.38 KB | None | 0 0
  1. public sealed class Camera2D : GameComponent, ICamera
  2. {
  3.     bool updateViewRequired = false;
  4.  
  5.     float zoom;
  6.     public float Zoom
  7.     {
  8.         get { return zoom; }
  9.         set
  10.         {
  11.             zoom = value;
  12.             UpdateZoomViewPort();
  13.         }
  14.     }
  15.  
  16.     float rotation;
  17.     public float Rotation
  18.     {
  19.         get { return rotation; }
  20.         set
  21.         {
  22.             rotation = value;
  23.             UpdateRotation();
  24.         }
  25.     }
  26.  
  27.  
  28.     public Vector2 Position { get; private set; }
  29.  
  30.     Viewport viewPort;
  31.     Vector3 viewPortCenter;
  32.  
  33.     Matrix scaleMatrix;
  34.     Matrix rotationMatrix;
  35.     Matrix translationMatrix;
  36.  
  37.     Matrix focusPointMatrix;
  38.  
  39.     public Matrix View { get; private set; }
  40.  
  41.     int zoomWidth;
  42.     int zoomHeight;
  43.  
  44.     public Camera2D(Game game, float zoom)
  45.         : base(game)
  46.     {
  47.     }
  48.  
  49.     public Camera2D(Game game)
  50.         : base(game)
  51.     {
  52.         UpdateViewport();
  53.     }
  54.  
  55.     public void UpdateViewport()
  56.     {
  57.         viewPort = Game.GraphicsDevice.Viewport;
  58.         viewPortCenter = new Vector3(viewPort.Width * 0.5f, viewPort.Height * 0.5f, 0);
  59.  
  60.         // optimiert für eine breite von 1680! wenn schmäler, dann mehr zoomen!
  61.        
  62.         focusPointMatrix = Matrix.CreateTranslation(viewPortCenter);
  63.         Zoom = (float)Math.Round(viewPort.Width / 16.8 / 100, 2);
  64.  
  65.         Rotation = -0.5f;
  66.         Position = Vector2.Zero;
  67.  
  68.         UpdateTranslation();
  69.  
  70.         updateViewRequired = true;
  71.     }
  72.  
  73.     public void MoveBy(Vector2 direction)
  74.     {
  75.         Position += direction;
  76.         UpdateTranslation();
  77.     }
  78.  
  79.     public void MoveTo(Vector2 position)
  80.     {
  81.         if(Vector2.Distance(position, Position) < 0.1f) return;
  82.  
  83.         Position = position;
  84.         UpdateTranslation();
  85.     }
  86.  
  87.     public override void Update(GameTime gameTime)
  88.     {
  89.         if (!updateViewRequired) return;
  90.  
  91.         View = translationMatrix *
  92.                 rotationMatrix *
  93.                 scaleMatrix *
  94.                 focusPointMatrix;
  95.     }
  96.  
  97.     private void UpdateZoomViewPort()
  98.     {
  99.         zoomWidth = (int)(viewPort.Width * (1 / Zoom) * 1.5f);
  100.         zoomHeight = (int)(viewPort.Height * (1 / Zoom) * 2.5f);
  101.  
  102.         scaleMatrix = Matrix.CreateScale(new Vector3(Zoom, Zoom, Zoom));
  103.  
  104.         updateViewRequired = true;
  105.     }
  106.  
  107.     private void UpdateRotation()
  108.     {
  109.         rotationMatrix = Matrix.CreateRotationZ(Rotation);
  110.  
  111.         Up = Vector2.Transform(-Vector2.UnitY, Matrix.CreateRotationZ(-Rotation));
  112.  
  113.         updateViewRequired = true;
  114.     }
  115.  
  116.     private void UpdateTranslation()
  117.     {
  118.         translationMatrix = Matrix.CreateTranslation(new Vector3(-Position.X, -Position.Y, 0));
  119.  
  120.         updateViewRequired = true;
  121.     }
  122.  
  123.     public Vector2 Up
  124.     {
  125.         get; private set;
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment