1
\$\begingroup\$

Whenever I seem to create a rotation matrix or try to zoom, the meshes that I draw to the screen get stretched and distorted.

I've tried to make a simple rotation matrix like this:

Matrix transform = Matrix.CreateRotationZ(MathHelper.ToRadians(ang))

And then I do this when I make the vertices:

verts[i].Position = Vector3.Transform(verts[i].Position, transform);

And it does rotate it, but it also gets stretched at the same time.

Similarly, I made a simple Camera class:

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;

namespace _04_texturedquad
{
    public class Camera
    {
        public Vector2 position {get; private set;}

        public Matrix ortho;

        float zoom;

        public float rotation;

        Viewport viewport;

        public Camera(Viewport v)
        {
            this.viewport = v;

            ortho = Matrix.CreateOrthographicOffCenter(
            0, 
            viewport.Width, 
            0, 
            viewport.Height, 
            0, 1);
        }

        public void Move(Vector2 vec)
        {
            this.position += vec;
            Update();
        }

        public void Zoom(float amount)
        {
            zoom += amount;
            Update();
        }

        public void SetPosition(Vector2 vec)
        {
            this.position = vec;
        }

        public void Update()
        {
            ortho = Matrix.CreateOrthographicOffCenter(
            0               + position.X - zoom, 
            viewport.Width  + position.X + zoom, 
            0               + position.Y - zoom, 
            viewport.Height + position.Y + zoom, 
            0, 1);
        }
    }
}

And moving around works, but when I zoom, it does zoom, but it also gets stretched vertically when I do so.

Here's what I'm doing when I'm sending the buffers to the GPU:

public void Draw()
    {

        RasterizerState rs = new RasterizerState();
        game.GraphicsDevice.RasterizerState = rs;

        effect.Projection = c.ortho;

        foreach (EffectPass pass in effect.CurrentTechnique.Passes)
        {   
            pass.Apply();
            game.GraphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, _vertices, 0, _vertices.Length, _indices, 0, _indices.Length / 3, VertexPositionColor.VertexDeclaration);
        }
    }

I've also tried setting effect.View = Matrix.Identity, but that hasn't worked either.

Why does everything get distorted when I zoom/rotate? I don't think this is a problem with how I'm making the meshes, because they look fine otherwise.

enter image description here

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

Your camera is an Orthographic projection. Zoom should have no effect; When non-axis aligned, the view produces distortion on all non-90 degree angles by definition.

Use the following code in place of:

//ortho = Matrix.CreateOrthographicOffCenter(
//...
//with:

CreatePerspectiveOffCenter(new Rectangle(0,0,viewport.Width,viewport.Height),0,500);

The signature is: CreatePerspectiveOffCenter(Rectangle viewingVolume, float nearPlaneDistance, float farPlaneDistance)

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .