# XNA camera bounderies

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

## Recommended Posts

Hy everyone, take a look at this pitcure please:

[attachment=32472:Screenshot_2.png]

Well how do I make my camera stop at where my map ends?

Here is my Camera class:

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Katrina
{
class Camera
{
public float scale = 1f;
public Matrix transform;
Viewport view;
Vector2 centar;

public Camera(Viewport _view)
{
view = _view;
}

public void Update(GameTime gameTime, Animated kat)
{
centar = new Vector2(kat.position.X + (kat.kR.Width / 2) - 400,
kat.position.Y + (kat.kR.Height / 2) - 300); // kat is player
transform = Matrix.CreateScale(new Vector3(scale, scale, 0)) *
Matrix.CreateTranslation(new Vector3(-centar.X, -centar.Y, 0));
if(Keyboard.GetState().IsKeyDown(Keys.A))
{
scale += 0.001f;
}else if(Keyboard.GetState().IsKeyDown(Keys.D))
{
scale -= 0.001f;
}
}

}
}



So how could I make camera stop when I want it to stop, lets say top left corner x:0 y:0 is the last point it can show on the top left side :/

##### Share on other sites

With the simplicity of how you are working your camera update, the easiest solution is to simply set a hard boundary to check.

if(Keyboard.GetState().IsKeyDown(Keys.A) && //centar.Y is within bounds) //Don't move camera down if outside of range
{
scale += 0.001f;
}else if(Keyboard.GetState().IsKeyDown(Keys.D) && //centar.X is within bounds) //Don't move camera right if outside range
{
scale -= 0.001f;
}


If you wanted to do it the correct and dynamic way, to work with any ground or level, you would have to convert the four corners of your ground mesh vertices to screen space coordinates and reference that to stop the camera. This would apply to all levels you worked on in the future.

A quick google brought me to this thread. I guess XNA has a built in function to do this. http://gamedev.stackexchange.com/questions/35263/converting-world-space-coordinate-to-screen-space-coordinate-and-getting-incorre

Edited by ExErvus

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
5. 5

• 9
• 12
• 16
• 26
• 10
• ### Forum Statistics

• Total Topics
633769
• Total Posts
3013758
×