• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Matthiee

2D Isometric rendering with camera/viewport

6 posts in this topic

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?

1

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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 ?

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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);
            }
        }
    }
}
0

Share this post


Link to post
Share on other sites

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...
      }
     
    
Edited by kd7tck
2

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0