Tiled map Positionen not updating

So i pretty much follew the Tutorial (https://www.monogameextended.net/docs/features/tiled/tiled) and copied the code. The Maps renders with _tiledMapRenderer.Draw(); but not if I want to move the camera _tiledMapRenderer.Draw(_camera.GetViewMatrix());. What could be the Problem? I googeld already and tried some things but nothing worked.

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using MonoGame.Extended.Tiled;
using MonoGame.Extended.Tiled.Renderers;
using MonoGame.Extended;
using MonoGame.Extended.ViewportAdapters;
using System.Diagnostics;


namespace Humboldt
{
public class Humboldt : Game
{
    private GraphicsDeviceManager mGraphics;
    private SpriteBatch _spriteBatch;
    TiledMap _tiledMap;
    TiledMapRenderer _tiledMapRenderer;
    private Vector2 _cameraPosition;
    private OrthographicCamera _camera;

    internal Humboldt()
    {
        mGraphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        IsMouseVisible = true;
    }

    private Vector2 GetMovementDirection()
    {
        var movementDirection = Vector2.Zero;
        var state = Keyboard.GetState();
        if (state.IsKeyDown(Keys.Down))
        {
            movementDirection -= Vector2.UnitY;
        }
        if (state.IsKeyDown(Keys.Up))
        {
            movementDirection += Vector2.UnitY;
        }
        if (state.IsKeyDown(Keys.Left))
        {
            movementDirection -= Vector2.UnitX;
        }
        if (state.IsKeyDown(Keys.Right))
        {
            movementDirection += Vector2.UnitX;
        }
        movementDirection.Normalize();

        return movementDirection;
    }

    private void MoveCamera(GameTime gameTime)
    {
        var speed = 200;
        var seconds = gameTime.GetElapsedSeconds();
        var movementDirection = GetMovementDirection();
        _cameraPosition += speed * movementDirection * seconds;
    }
    protected override void Initialize()
    {
       /* Debug.WriteLine(mGraphics.PreferredBackBufferWidth);
        if (mSpriteBatch != null)
        {
            Debug.WriteLine(mSpriteBatch.GraphicsDevice);
        } */

        var viewportadapter = new BoxingViewportAdapter(Window, GraphicsDevice, 800, 600);
        _camera = new OrthographicCamera(viewportadapter);
        base.Initialize();
    }

    protected override void LoadContent()
    {
        _tiledMap = Content.Load<TiledMap>("samplemap");
        _tiledMapRenderer = new TiledMapRenderer(GraphicsDevice, _tiledMap);
        _spriteBatch = new SpriteBatch(GraphicsDevice);
    }

    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed ||
            Keyboard.GetState().IsKeyDown(Keys.Escape))
        {
            Exit();
        }

        MoveCamera(gameTime);
        _camera.LookAt(_cameraPosition);
        _tiledMapRenderer.Update(gameTime);
        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        // TODO: Add your drawing code here
        var projectionMatrix = Matrix.CreateOrthographicOffCenter(0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, 0, 0f, -1f);
        //_spriteBatch.Begin(transformMatrix: _camera.GetViewMatrix());
        _spriteBatch.Begin();
        GraphicsDevice.Clear(Color.Black);

        //_tiledMapRenderer.Draw();

        _tiledMapRenderer.Draw(_camera.GetViewMatrix());
        _spriteBatch.End();
        //Debug.WriteLine(_camera.GetViewMatrix());
        base.Draw(gameTime);
    }
}
}

I hope you can help me. It looks like that if I use the GetViewMatrix in Draw


and normal if i just say _tiledMapRenderer.Draw();

Okay I found one Problem that the _cameraPoition is Nan,
var speed = 200;
var seconds = gameTime.GetElapsedSeconds();
var movementDirection = GetMovementDirection();
_cameraPosition += speed * movementDirection * seconds;
I try to fix that.

Okay I found out that the normalization produces a NaN Vector and than the Location cant be shown. How do I avoid that?

Right, the docs should be updated:

private Vector2 GetMovementDirection()
    {
        var movementDirection = Vector2.Zero;
        var state = Keyboard.GetState();
        if (state.IsKeyDown(Keys.Down))
        {
            movementDirection -= Vector2.UnitY;
        }
        if (state.IsKeyDown(Keys.Up))
        {
            movementDirection += Vector2.UnitY;
        }
        if (state.IsKeyDown(Keys.Left))
        {
            movementDirection -= Vector2.UnitX;
        }
        if (state.IsKeyDown(Keys.Right))
        {
            movementDirection += Vector2.UnitX;
        }

        if (movementDirection != Vector2.Zero)
        {
            movementDirection.Normalize();
        }

        return movementDirection;
    }
1 Like

Yeah that would be real good, I wasted hours on finding out. Thanks for the conformation.

Submitted a proposed change to the document. I’m surprised that this made it through any sort of QA process because it is basic math that you cannot normalize a vector with zero length.

Okay thank you very much im glad that I could help a bit. :smiley:

It’s been fixed.