C# 2D Camera Max Zoom

Posted by Craig on Game Development See other posts from Game Development or by Craig
Published on 2012-11-21T19:40:19Z Indexed on 2012/11/21 23:12 UTC
Read the original article Hit count: 240

Filed under:
|
|

I have a simple ship sprite moving around the screen along with a 2D Camera.

I have zooming in and out working, however when I zoom out it goes past the world bounds and has the cornflower blue background showing.

How do I sort it that I can only zoom out as far as showing the entire world (which is a picture of OZ) and thats it? I dont want any of the cornflower blue showing.

Cheers!

namespace GamesCoursework_1
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;


    // player variables
    Texture2D Ship;
    Vector2 Ship_Position;
    float Ship_Rotation = 0.0f;
    Vector2 Ship_Origin;
    Vector2 Ship_Velocity;
    const float tangentialVelocity = 4f;
    float friction = 0.05f;

    static Point CameraViewport = new Point(800, 800);
    Camera2d cam = new Camera2d((int)CameraViewport.X, (int)CameraViewport.Y);
    //Size of world
    static Point worldSize = new Point(1600, 1600);
    // Screen variables
    static Point worldCenter = new Point(worldSize.X / 2, worldSize.Y / 2);

    Rectangle playerBounds = new Rectangle(CameraViewport.X / 2, CameraViewport.Y / 2, worldSize.X - CameraViewport.X, worldSize.Y - CameraViewport.Y);
    Rectangle worldBounds = new Rectangle(0, 0, worldSize.X, worldSize.Y);

    Texture2D background;


    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);

        graphics.PreferredBackBufferWidth = CameraViewport.X;
        graphics.PreferredBackBufferHeight = CameraViewport.Y;
        Content.RootDirectory = "Content";
    }

    /// <summary>
    /// Allows the game to perform any initialization it needs to before starting to run.
    /// This is where it can query for any required services and load any non-graphic
    /// related content.  Calling base.Initialize will enumerate through any components
    /// and initialize them as well.
    /// </summary>
    protected override void Initialize()
    {
        // TODO: Add your initialization logic here

        base.Initialize();
    }

    /// <summary>
    /// LoadContent will be called once per game and is the place to load
    /// all of your content.
    /// </summary>
    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);

        // TODO: use this.Content to load your game content here
        Ship = Content.Load<Texture2D>("Ship");
        Ship_Origin.X = Ship.Width / 2;
        Ship_Origin.Y = Ship.Height / 2;

        background = Content.Load<Texture2D>("aus");

        Ship_Position = new Vector2(worldCenter.X, worldCenter.Y);
        cam.Pos = Ship_Position;
        cam.Zoom = 1f;


    }

    /// <summary>
    /// UnloadContent will be called once per game and is the place to unload
    /// all content.
    /// </summary>
    protected override void UnloadContent()
    {
        // TODO: Unload any non ContentManager content here
    }

    /// <summary>
    /// Allows the game to run logic such as updating the world,
    /// checking for collisions, gathering input, and playing audio.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        // TODO: Add your update logic here
        Ship_Position = Ship_Velocity + Ship_Position;
        keyPressed();

        base.Update(gameTime);
    }

    /// <summary>
    /// This is called when the game should draw itself.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        // TODO: Add your drawing code here
        spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, null, null,null, cam.get_transformation(GraphicsDevice));
        spriteBatch.Draw(background, Vector2.Zero, Color.White);
        spriteBatch.Draw(Ship, Ship_Position, Ship.Bounds, Color.White, Ship_Rotation, Ship_Origin, 1.0f, SpriteEffects.None, 0f); 
        spriteBatch.End();



        base.Draw(gameTime);
    }

    private void Ship_Move(Vector2 move)
    {
        Ship_Position += move;
    }

    private void keyPressed()
    {
        KeyboardState keyState;
        // Move right
        keyState = Keyboard.GetState();

        if (keyState.IsKeyDown(Keys.Right))
        {
            Ship_Rotation = Ship_Rotation + 0.1f;
        }
        if (keyState.IsKeyDown(Keys.Left))
        {
            Ship_Rotation = Ship_Rotation - 0.1f;
        }
        if (keyState.IsKeyDown(Keys.Up))
        {

            Ship_Velocity.X = (float)Math.Cos(Ship_Rotation) * tangentialVelocity;
            Ship_Velocity.Y = (float)Math.Sin(Ship_Rotation) * tangentialVelocity;

            if ((int)Ship_Position.Y < playerBounds.Bottom && (int)Ship_Position.Y > playerBounds.Top) cam._pos.Y = Ship_Position.Y;
            if ((int)Ship_Position.X > playerBounds.Left && (int)Ship_Position.X < playerBounds.Right) cam._pos.X = Ship_Position.X;

            Ship_Position += new Vector2(tangentialVelocity, 0);
            if (!worldBounds.Contains(new Point((int)Ship_Position.X, (int)Ship_Position.Y))) Ship_Position -= new Vector2(tangentialVelocity * 2, 0.0f);
            Ship_Position += new Vector2(-tangentialVelocity, 0.0f);
            if (!worldBounds.Contains(new Point((int)Ship_Position.X, (int)Ship_Position.Y))) Ship_Position -= new Vector2(-tangentialVelocity * 2, 0.0f);
            Ship_Position += new Vector2(0.0f, -tangentialVelocity);
            if (!worldBounds.Contains(new Point((int)Ship_Position.X, (int)Ship_Position.Y))) Ship_Position -= new Vector2(0.0f, -tangentialVelocity * 2);
            Ship_Position += new Vector2(0.0f, tangentialVelocity);
            if (!worldBounds.Contains(new Point((int)Ship_Position.X, (int)Ship_Position.Y))) Ship_Position -= new Vector2(0.0f, 2 * tangentialVelocity);




        } 
        else if(Ship_Velocity != Vector2.Zero)
        {
            float i = Ship_Velocity.X;
            float j = Ship_Velocity.Y;

            Ship_Velocity.X = i -= friction * i;
            Ship_Velocity.Y = j -= friction * j;

            if ((int)Ship_Position.Y < playerBounds.Bottom && (int)Ship_Position.Y > playerBounds.Top) cam._pos.Y = Ship_Position.Y;
            if ((int)Ship_Position.X > playerBounds.Left && (int)Ship_Position.X < playerBounds.Right) cam._pos.X = Ship_Position.X;


            Ship_Position += new Vector2(tangentialVelocity, 0);
            if (!worldBounds.Contains(new Point((int)Ship_Position.X, (int)Ship_Position.Y))) Ship_Position -= new Vector2(tangentialVelocity * 2, 0.0f);
            Ship_Position += new Vector2(-tangentialVelocity, 0.0f);
            if (!worldBounds.Contains(new Point((int)Ship_Position.X, (int)Ship_Position.Y))) Ship_Position -= new Vector2(-tangentialVelocity * 2, 0.0f);
            Ship_Position += new Vector2(0.0f, -tangentialVelocity);
            if (!worldBounds.Contains(new Point((int)Ship_Position.X, (int)Ship_Position.Y))) Ship_Position -= new Vector2(0.0f, -tangentialVelocity * 2);
            Ship_Position += new Vector2(0.0f, tangentialVelocity);
            if (!worldBounds.Contains(new Point((int)Ship_Position.X, (int)Ship_Position.Y))) Ship_Position -= new Vector2(0.0f, 2 * tangentialVelocity);

        }
        if (keyState.IsKeyDown(Keys.Q))
        {
            if (cam.Zoom < 2f) cam.Zoom += 0.05f;
        }
        if (keyState.IsKeyDown(Keys.A))
        {
            if (cam.Zoom > 0.3f) cam.Zoom -= 0.05f;
        }

    }
}
}

© Game Development or respective owner

Related posts about c#

Related posts about xna-4.0