Jump to content

  • Log In with Google      Sign In   
  • Create Account


[XNA] Camera Centralizing around player


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 Xooch   Members   -  Reputation: 179

Like
0Likes
Like

Posted 02 July 2013 - 05:22 PM

Hey there,

 

started to create a game in XNA in my spare time, its a 2D platform game. I'm trying to get a similar effect to a game such as Terraria, where the camera is centralized around the player's character/avatar.

 

Is there any tutorials out there which can explain how this is done in XNA?

 

Thanks in advanced



Sponsor:

#2 burnt_casadilla   Members   -  Reputation: 423

Like
0Likes
Like

Posted 03 July 2013 - 11:27 PM

public class Camera
    {
        public Matrix viewMatrix;
        private Vector2 m_position;
        private Vector2 m_halfViewSize;

        public Camera(Rectangle clientRect)
        {
            m_halfViewSize = new Vector2(clientRect.Width * 0.5f, clientRect.Height * 0.5f);
            UpdateViewMatrix();
        }

        public Vector2 Pos
        {
            get
            {
                return m_position;
            }

            set
            {
                m_position = value;
                UpdateViewMatrix();
            }
        }

        private void UpdateViewMatrix()
        {
            viewMatrix = Matrix.CreateTranslation(m_halfViewSize.X - m_position.X, m_halfViewSize.Y - m_position.Y, 0.0f);
        }
    }

Pasted from this: http://stackoverflow.com/questions/4662876/xna-2d-camera-how-to-lock-center-it-to-an-animated-sprite

 

HalfViewSize makes sure the camera always has the player focused in the middle. 


If you see a post from me, you can safely assume its C# and XNA :)





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS