Krypton + Farseer's DebugView

Dec 17, 2012 at 7:58 PM
Edited Dec 17, 2012 at 7:58 PM

I have been breaking my mind on this trying to get the matrix right for Krypton as a ton of people on here, however... I tried everything I could find but it didn't give me anything close to what I wanted to achieve. Anyhow, here is my code:

Camera.cs

public class Camera2D
    {
        protected float _zoom; // Camera Zoom 
        Matrix _transform; // Matrix Transform 
        Vector2 _pos; // Camera Position 
        protected float _rotation; // Camera Rotation 
        readonly int _viewportWidth;
        readonly int _viewportHeight;

        public Camera2D(int viewportWidth, int viewportHeight)
        {
            _zoom = 1.0f;
            _rotation = 0.0f;
            _pos = Vector2.Zero;
            _viewportHeight = viewportHeight;
            _viewportWidth = viewportWidth;
        }

        // Sets and gets zoom 
        public float Zoom
        {
            get { return _zoom; }
            set { _zoom = value; if (_zoom < 0.1f) _zoom = 0.1f; } // Negative zoom will flip image 
        }

        public float Rotation
        {
            get { return _rotation; }
            set { _rotation = value; }
        }

        // Auxiliary function to move the camera 
        public void Move(Vector2 amount)
        {
            _pos += amount;
        }

        // Get set position 
        public Vector2 Position
        {
            get { return _pos; }
            set { _pos = value; }
        }

        public Matrix Transform()
        {       
            _transform = 
              Matrix.CreateTranslation(new Vector3(-ConvertUnits.ToDisplayUnits(_pos.X), -ConvertUnits.ToDisplayUnits(_pos.Y), 0)) *
              Matrix.CreateRotationZ(Rotation) *
              Matrix.CreateScale(new Vector3(Zoom, Zoom, 1)) *
              Matrix.CreateTranslation(new Vector3(_viewportWidth / 2, _viewportHeight / 2, 0));
            return _transform;

        }
    }
 

 

And here is my drawing method:

protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here

            // Draw Krypton lightning
            spriteBatch.Begin();

            Matrix world = Matrix.Identity;
            Matrix view = Matrix.CreateTranslation(new Vector3(0, 0, 0) * -1f);
            Matrix projection = Matrix.CreateOrthographic(this.GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, 0, 1);

            Krypton.Matrix = world * view * projection;
            Krypton.LightMapPrepare();
            Krypton.Draw(gameTime);

            spriteBatch.End();

            // Draw Farseer's debugview
            Vector2 size = new Vector2(GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height) / Camera.Zoom;
            var debugProj = Matrix.CreateOrthographic(ConvertUnits.ToSimUnits(size.X), -ConvertUnits.ToSimUnits(size.Y), 0, 1);
            var debugViewMatrix = Matrix.CreateTranslation(new Vector3(-Camera.Position, 0f));
            DebugView.RenderDebugData(ref debugProj, ref debugViewMatrix);

            // Draw according to camera
            spriteBatch.Begin(SpriteSortMode.Texture, BlendState.AlphaBlend, null, null, null, null, Camera.Transform());

            foreach (var chunk in WorldHandler.Chunks)
            {
                foreach (var block in chunk.Value.Blocks)
                {
                    block.Value.Draw(spriteBatch);
                }
            }

            Player.Draw(spriteBatch);

            spriteBatch.End();
            
            base.Draw(gameTime);
        }

 Fact is, the DebugView gets drawn correctly above the tiles however Krypton is not, can someone help me out with this?