• Advertisement
Sign in to follow this  

Problem with Camera and Mousepos XNA

This topic is 1661 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Dear Guys,


I use a Camera in my 2D XNA Game, and have really Problems to get the right Position of my Mouse Cursor you have maybe a Solution?

        //Klassen Variablen

        public Matrix transform;
        Viewport view;
        public Vector2 center;


        float _width { get; set; }
        float _height { get; set; }
        float _rotation { get; set; }
        float _zoom { get; set; }
        float _camSpeed { get; set; }
        bool _follow { get; set; }

        public Camera(Viewport newView)
            view = newView;
            _zoom = 1f;
            _rotation = 0f;
            center = new Vector2(0, 0);
            _camSpeed = 5f;
            _follow = true;

        #region Kamera Kontrolle

        //Camera Position Updaten

        public void Update(IFocus focus)
            if(_follow == true)
            center = focus.getFocus();
            _width = focus.getWidth() / 2;
            _height = focus.getHeight() / 2;

            transform = Matrix.CreateTranslation(new Vector3(-center.X - _width, -center.Y - _height, 0)) *
                Matrix.CreateRotationZ(_rotation) *
                Matrix.CreateScale(_zoom) *
                Matrix.CreateTranslation(new Vector3(view.Width / 2, view.Height / 2, 0));

If i try to transform the MousePosition aswell, the Mouse is still a bit off the Center if im in negativ Values of the Screen.


mousePos = Vector2.Transform(mousePos, Matrix.Invert(cam.transform));


I cant do any precise shooting or anything else.


maybe you know a answer.


Greetings Move




Share this post

Link to post
Share on other sites

hi there,

there is something strange with your camera class, maybe you should provide code for IFocus. why don't you look here, I am using this camera, it is similar to yours. the way you are taking world coordinates for the mouse is right, I am doing it the same way and it is working.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement