This project has moved. For the latest updates, please go here.

Camera2D alignment

Topics: User Forum
Nov 9, 2011 at 9:27 AM


I'm playing around with the Camera2D class given in the samples. I think this class is easy to use and has good possibilities to manipulate the view of the gameplay scene.

There is only one thing I'm stumbling with. I created a border larger than my viewport. By default the cameras relativ x position is at the half way between left and right border and the cameras relativ y position is bottom border y - half viewport height.

That means I can't see the left, right and top border but the bottom of my border is at the bottom of my viewport when cameras position is (0, 0). Now I can prevent that the area below the borders  bottom is shown by setting cameras MaxPosition to (0, 0) -> Camera can't be moved down. 

Unfortunatly I can still zoom out so that areas below my bottom border are shown.

In fact outside the border shouldn't be anything at all. It isn't necessary to see anything outside my border. Is there any possibility to tell my camera that it should move directly after zooming in or out if area outside the border is shown?

I don't even know which values are necessary and important to calculate the offset. :-(

Thanking you in anticiption!


Nov 11, 2011 at 7:50 PM


is there nobody out there who has an idea? I'm still trying to solve my problem... This is my Zoom-Setter at the moment but it does not work correctly:

public float Zoom {
            get { return _currentZoom; }
            set {
                _currentZoom = value;
                _currentZoom = MathHelper.Clamp (_currentZoom, _minZoom, _maxZoom);

                Matrix translateCenterMat = Matrix.CreateTranslation (new Vector3 (this._translateCenter, 0f));
                Matrix scaleMat = Matrix.CreateScale (this._currentZoom);
                translateCenterMat *= scaleMat;
                translateCenterMat -= Matrix.CreateTranslation (new Vector3 (this._translateCenter, 0f));

                Vector3 scal;
                Vector3 trans;
                Quaternion rot;
                translateCenterMat.Decompose (out scal, out rot, out trans);

                this._minPosition = this._orgMinPosition - new Vector2 (trans.X, trans.Y);
                this._maxPosition = this._orgMaxPosition + new Vector2 (trans.X, trans.Y);

                this.MoveCamera (new Vector2 (0, 0));

Before I start I should say that minPosition/maxPosition is the current value ande orgMinPosition/orgMaxPosition is the original value I set at the beginning of my code.

First I clamp the zoom I want to set. Than I create a Matrix with the translate center. This matrix is scaled with the current zoom. The difference between the normal translate center matrix and the scaledtranslate center matrix should be my offset. 

This works pretty good up to an zoom of 96 percent. With an zoom of 94 percent the area below my border is shown. There must a tiny detail that I don't see...