2D Isometric rendering with camera/viewport

Started by
5 comments, last by Matthiee 11 years, 3 months ago

Hello everyone!

I'm at the early stages of making a 2D Isometric game.
But I'm not trying to implement the camera system from my older 2D game note this one was not isometric. And it works but I expected the result to be different.

I will now try to explain the problem but here is already a link to the source code.

http://www.mediafire.com/download.php?23ybba3uamaadzj

Here is the camera class:


public class Camera
{
    public Int32 X { get; private set; }
    public Int32 Y { get; private set; }
    public Int32 Width { get; private set; }
    public Int32 Height { get; private set; }

    public Camera(Int32 x, Int32 y, Int32 width, Int32 height)
    {
        this.X = x;
        this.Y = y;
        this.Width = width;
        this.Height = height;
    }

    public void SetLoc(Point pt)
    {
        this.X = pt.X;
        this.Y = pt.Y;
    }

    public void SetLoc(Int32 x, Int32 y)
    {
        this.X = x;
        this.Y = y;
    }

    public void SetSize(Size sz)
    {
        this.Width = sz.Width;
        this.Height = sz.Height;
    }

    public void SetSize(Int32 w, Int32 h)
    {
        this.Width = w;
        this.Height = h;
    }
}

The player class:


public class Player
{
    public static Bitmap bmpPlayer = Helper.loadImage(@".\char.png");
    public Camera playerCam;
    public Int32 X;
    public Int32 Y;

    public Player(Int32 x, Int32 y)
    {
        X = x;
        Y = y;
        playerCam = new Camera(x - 20, y - 15, 40, 30);
    }

    public void Render(Graphics g)
    {
        Int32 xx = (Y * bmpPlayer.Width / 2) + (X * bmpPlayer.Width / 2) - 100;
        Int32 yy = (X * bmpPlayer.Height / 2) - (Y * bmpPlayer.Height / 2) + 390;

        g.DrawImageUnscaled(bmpPlayer, new Point(xx, yy));
    }
}

The Tile class:


public class Tile
{
    public static Bitmap bmpTile = Helper.loadImage(@".\tile.png");
    public static Bitmap bmpDot = Helper.loadImage(@".\dot.png");
    public Int32 x;
    public Int32 y;
    public Int32 tileId;
    public Int32 xoffset;
    public Int32 yoffset;
    public Tile(Int32 x, Int32 y, Int32 id)
    {
        this.x = x;
        this.y = y;
        this.tileId = id;
    }

    public void render(Graphics g)
    {
        Int32 xx = (y * bmpTile.Width / 2) + (x * bmpTile.Width / 2) +xoffset;
        Int32 yy = (x * bmpTile.Height / 2) - (y * bmpTile.Height / 2) +yoffset  ;
           
        g.DrawImageUnscaled(bmpTile, new Point(xx, yy));
        if (tileId == 1)
            g.DrawImageUnscaled(bmpDot, new Point(xx, yy));
    }
}

The actual form:


public partial class Form1 : Form
{
    public Player p = new Player(20, 20);
    public Tile[,] map;
    public Boolean GameEnded = false;
    public Form1()
    {
        InitializeComponent();
        map = new Tile[1000, 1000];

        for (Int32 x = 0; x < 1000; x++)
            for (Int32 y = 0; y < 1000; y++)
            {
                    map[x, y] = new Tile(x, y, 0);
            }
    }

    private void Form1_Shown(object sender, EventArgs e)
    {
        Bitmap buffer = new Bitmap(this.Width,this.Height);
        Int32 width = map.GetLength(0);
        Int32 height = map.Length / width;
        while (!GameEnded)
        {
            buffer = new Bitmap(this.Width, this.Height);
            Graphics g = Graphics.FromImage(buffer);
            g.Clear(Color.Black);
            for (Int32 x = p.playerCam.X - 1; x < p.playerCam.Width + p.playerCam.X; x++)
            {
                for (Int32 y = p.playerCam.Height + p.playerCam.Y - 1; y >= p.playerCam.Y - 1; y--)
                {
                    if (x > -1 && y > -1 && x < 1000 && y < 1000)
                    {
                        map[x, y].xoffset = -100;
                        map[x, y].yoffset = 390;
                        map[x, y].render(g);
                    }
                }
            }
            p.Render(g);
            g.Dispose();

            Graphics gg = this.CreateGraphics();
            gg.DrawImageUnscaled(buffer, Point.Empty);
            gg.Dispose();
            buffer.Dispose();

            Thread.Sleep(10);
            Application.DoEvents();
        }
    }

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Up)
        { p.playerCam.SetLoc(p.playerCam.X - 1, p.playerCam.Y + 1); }
        else if (e.KeyCode == Keys.Down)
        { p.playerCam.SetLoc(p.playerCam.X + 1, p.playerCam.Y - 1); }
        else if (e.KeyCode == Keys.Right)
        { p.playerCam.SetLoc(p.playerCam.X + 1, p.playerCam.Y + 1); }
        else if (e.KeyCode == Keys.Left)
        { p.playerCam.SetLoc(p.playerCam.X - 1, p.playerCam.Y - 1); }
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        this.GameEnded = true;
    }
}

public class Helper
{
    public static Bitmap loadImage(string path)
    {
        Bitmap loadedImage = (Bitmap)Image.FromFile(path);
        loadedImage.MakeTransparent(Color.Fuchsia);
        return loadedImage;

    }
}

I know this code isn't very clear but this is just for testing purposes.

So the problem here is when I get to the end of the map the map would get some strange transformations:

when going to the left:

08mVS.png

when going to the right:

KpEHP.png

As you see when it goes to the right it looks like it is zooming out and I also have some other weird formations, and when moving the camera it doesn't feel right :\

Can anyone help me or give me some tips smile.png?

Advertisement

Look closely at form1_shown.

You tell it to draw a space with the following properties:

Y_draw_space = height of cam + it's current y position.

X_draw_space =width of cam + it's current x position.

As you move right the y,x values increase. So the space drawn increases, thus the zoom out effect occurs.

Uhm isn't that supposed to be like that?

Lets say for example:

My cam x = 10 and y = 10 and the width and height both are 20

then it will be start rendering at 10 (x) -> 10 (x) + 20 (width) = result it renders only 20 tiles in x. But if x was 100 I would get 100 (x) -> 100 (x) + 20 (width) = same result only first 20 tiles on x will be rendered?

Or am I thinking wrong ?

More tiles drawn == Smaller tiles == Zoom out

Fewer tiles drawn == larger tiles == Zoom in

You are right but that is not what the code shows.

You start at a high left point and scan downward, only allowing tiles within a certain range to be drawn. This is inefficient and wrong.

Instead have a loop iterate through the tiles already in range, no need to filter out tiles out of range. Then clip the cam values to within a reasonable distance from the edges, based on height and width of the cam.

Do you mean something like this?:


for (Int32 y = 0; y < height; y++)
{
    for (Int32 x = 0; x < width; x++)
    {
        if (x > -1 && y > -1 && x < width && y < height)
        {
            if (x > p.playerCam.X && x < (p.playerCam.X + p.playerCam.Width) &&
                y > p.playerCam.Y && y < (p.playerCam.Y + p.playerCam.Height))
            {
                map[x, y].xoffset = -100;
                map[x, y].yoffset = 390;
                map[x, y].render(g);
            }
        }
    }
}

Why are you starting at 0, start where the camera starts and end where the camera ends. Just draw whatever the camera shows us regardless of what that is. Then get rid of the range check you don't need that, the range check was shrinking the view on you. To ensure that the camera doesn't draw anything out of range make certain the camera position can never stray beyond the defined range, 1000X1000 in your case.

edit: In the camera's case the range is (0,0) and (1000-width,1000-height).


    public class Camera
    {
      public void SetLoc(Point pt)
      {
        if(X_&&_Y_in_range)
        {
          this.X = pt.X;
          this.Y = pt.Y;
        }
        else if(X_is_too_small)
        {
          this.X = 0
          this.Y = pt.Y
        }
        ... Yada Yada Yada...
      }
     
    

Thank you :)

This topic is closed to new replies.

Advertisement