Jump to content
  • Advertisement
Sign in to follow this  
Bruno Filip

XNA camera bounderies

This topic is 743 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

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 this post


Link to post
Share on other sites
Advertisement

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

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!