简体   繁体   中英

I can't fix camera to any object in a 2D Matrix

Problem description: I am trying to develop some 2D game with terrain collision in C# using XNA framework. I have already completed simple terrain generating and object, for example car, can move on this terrain. But now, I get a problem with camera fix to that car. I tried to search a lot on web, but I can't find the right solution for me. When I run the program, it crash.

What I tried? I tried to run this program in debug mode and I always stay stuck with NullReferenceException in Camera constructor.

piece of Camera.cs

public class Camera {
            public Matrix transform;
            private Viewport view;
            private Vector2 centering;

        public Camera(GraphicsDevice view)
        {
            this.view = view.Viewport;
        }

        public void Update(GameTime gameTime, TheGame theGame)
        {
            centering = new Vector2(theGame.movement.position.X + theGame.movement.position.Y / 2);
            transform = Matrix.CreateScale(new Vector3(1, 1, 0)) *
                    Matrix.CreateTranslation(new Vector3(-centering.X, -centering.Y, 0));
        }
    }

camera instance call:

    private Camera camera;
    public TheGame(Game game)
    :base(game)
    {
        this.game = game;
        camera = new Camera(GraphicsDevice); //<---- here is the crash
    }
    private Game game;

My question: How to make camera fixed on any object in the Matrix or what is the standard way to do that, because I think that knowledge is very important.

The reason you are getting NullReferenceException is because GraphicsDevice is not initialized yet .

Instead of creating your Camera in the TheGame constructor, create it in the Initialize method:

protected override void Initialize()
{
    base.Initialize();
    camera = new Camera(GraphicsDevice);
}

How to make camera fixed on any object in the Matrix or what is the standard way to do that, because I think that knowledge is very important.

I'm assuming you are dealing with screen space coordinates where X grows from left to right and Y grows from top to bottom. I'm also assuming that the camera position indicates the top left corner of the camera view and camera is position at {0, 0} in local space.

Based on these assumptions and knowing the position of the entity you want to center your camera on, we can calculate the camera translation transform as following:

transform = Matrix.CreateTranslation(new Vector3(
    theGame.movement.position.X - view.Width / 2f, 
    theGame.movement.position.Y - view.Height / 2f, 
    0));

This transform says: " position the camera's top left corner on game object and subtract half of the camera's viewport to center it ".

Note that you might also want to clamp the camera's coordinates between 0 and game world size to ensure that camera doesn't go out of the bounds of game worlds using MathHelper.Clamp .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM