I am quite new to game development (but not programming) so please forgive me if I am not correctly understanding certain concepts.
I am trying to figure out how to use world coordinates rather than pixel coordinates so that my game can work correctly at different resolutions. As I understand it, I need a viewport and I can use camera functions such as project() and unproject() to convert between the two coordinates. At the moment, I am just placing objects every 16 pixels as this is the size of objects and the objects cannot intersect.
For example:
The bottom left block is located at pixel (0, 0), the one to the right at (16, 0), to the right of that is (32, 0), and so on. However, I would like to be able to place them at (0,0), (1, 0), and (2, 0) with the same result so that it looks the same on different resolutions.
Basically, I'm asking how to set this up and have it work properly as world coordinates.
Answer
Here's what I would do in this situation.I'm not using libgdx myself, but I'll give you some pseudo pseudo code. If you haven't already, make a separate class called something like TileMap In that class, create the following variables.
- public int tileSize
- public int tilesX
- public int tilesY
- public int[,] map
The map variable is a two dimensional integer array, meaning that every tile type will be represented as an int. For example, a stone tile could be '3'
In the constructor of the TileMap you need to take in ( tileSize, tilesX, tilesY), and assign them to the local variables.In the bottom of the constructor you need to instantiate the map variable like shown in the example constructor below
public TileMap( tileSize, tilesX, tilesY ){
this.tileSize = tileSize; this.tilesX = tilesX; this.tilesY = tilesY;
this.map = new int[tilesX,tilesY](); // not sure if you need the () at the end, since I'm writing this on my phone.
}
Now you've got a TileMap setup, currently all the tiles should be 0 to my understanding, if they aren't you might have to make a nested for loop for the tilesX and tilesY variables. It's time to create the methods that will return the world position from given tile coordinates, and the method that will return a tile from the given world coordinates. To convert to world position, simply take the X and Y values and multiply them by the tileSize this will give you 0,0 at the tile 0,0 and 48,48 at the tile 2,2 assuming you have tileSize set to 24 You can create a method that returns this easily.
To get the tile from world coordinates you need to do something like this assuming you work with vectors
public int getTileWorldPos(Vector2 worldPos){
float x = Math.floor(worldPos.x/tileSize);
float y = Math.floor(worldPos.y/tileSize);
return map[x,y];
}
This method will return the tile id at the given world coordinates
If you want to change the tile at a given world pos use this method
public void setTileWorldPos(Vector2 worldPos, int newtile){
float x = Math.floor(worldPos.x/tileSize);
float y = Math.floor(worldPos.y/tileSize);
map[x,y] = newtile;
return;
}
Now that you've got a TileMap class, you need to instantiate it in your main class. To do this, simply write
TileMap map = new TileMap (24, 10, 10);
In that case, I'm creating a 10 by 10 TileMap with a tileSize of 24
If you want to manually set a tile you can just do like this
map[6,3] = 3;
And to use the methods you created in the TileMap class, write
map.getTileWorldPos(new Vector2 (362, 49));
I've written all of this on my phone, so I haven't had any way to test my code, and syntax might not be perfect. But hopefully this will give you an idea on how to do this.
if I'm missing anything feel free to ask questions.
I hope this can help you achieve what you want :)
No comments:
Post a Comment