Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Tile coords  (Read 1236 times)
0 Members and 1 Guest are viewing this topic.
Offline samcp12

Junior Member


Medals: 1



« Posted 2013-06-23 21:09:47 »

Hello, i recently finished creating a tile map, I render it using a 2D array, the problem is the way I am rendering, I never really set an X/Y position of the individual tiles, instead I'm using the tiles render method and a loop for setting the X and Y, but of course this is only the drawing positioning and not the actual coordinates of the tile. So is there anyway I can set an x and y for a tile within a 2D array? The main reason is I'm thinking of implementing path finding and cant think of any other way to do it without knowing the specific x,y of the individual tiles. Thanks for any help!
Offline BurntPizza
« Reply #1 - Posted 2013-06-23 21:17:22 »

You could store the location of the tile in the Tile class, and have it set when the tile is created.
There are other methods, but this is the simplest.
Offline samcp12

Junior Member


Medals: 1



« Reply #2 - Posted 2013-06-23 22:44:10 »

The problem with that is, I am using a static Tile named grass, I saw no reason to create multiply instances of a tile, the way Im doing it, I'm not creating a new tile for every index in the array, if I did surely that would use a lot more resources considering each tile would have a buffered image assigned to it?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline BurntPizza
« Reply #3 - Posted 2013-06-23 22:53:27 »

Ah, I wondered if that's what you were doing. Well you could have non-static tile instances, but each one refers back to a static BufferedImage, stored in the Tile class, E.g:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
public class Tile {
    public static List<BufferedImage> images;

    private BufferedImage image;
    private int x,y;

    public Tile(int x, int y, BufferedImage img) {
        this.x = x;
        this.y = y;
        image = img;
}

and when making a new Tile:
new Tile(5,7,Tile.images.get(0));

You could also use a Map or something instead of a List.
Offline samcp12

Junior Member


Medals: 1



« Reply #4 - Posted 2013-06-23 23:16:22 »

hmm, I may change it to be like that, though I'm not sure, I just really like the idea of only have one instance, I just can't really think of any other way to implement path finding without being able to access specific tile coordinates, because without the coordinates there really isn't a way for me to access the tiles type etc.

Thanks for the help anyway, I'll wait and see if anyone else has any ideas, if not ill try your idea Smiley
Offline nhydock

Junior Member


Medals: 4
Projects: 2



« Reply #5 - Posted 2013-06-23 23:23:06 »

First of all, let's assume your tile class looks like this, or something along these lines.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
public class Tile
{
    public final String name;
    public final BufferedImage image;
    public final boolean passable;

    public Tile(String name, URL imagePath, boolean passable)
    {
       this.name = name;
       this.image = new BufferedImage(ImageIO.read(imagePath));
       this.passable = passable;
    }
}


I don't care if we might load the properties from a file or what, as long as it has at least these things in it


In your map rendering thing, you could have a parallel 2d array

1  
2  
3  
4  
5  
6  
7  
8  
Tile[] tileSet = {all my different tile types};
int[][] tiles = {
  {0,0,0,0,0},
  {0,1,1,1,0},
  {0,1,2,1,0},
  {0,1,1,1,0},
  {0,0,0,0,0}
}


Your tile set is just all your different tile types in the map.  Tiles is an array where all the numbers match with the index of the tile type in tileSet.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
public void render(Graphics g)
{
    ...
    //graphics prep stuff
   ...
    for (int row = 0; row < tiles.length; row++)
    {
        for (int col = 0; col < tiles[0].length; col++)
        {
            BufferedImage image = tileSet[tiles[row][col]].image;
            graphics.draw(image, row*Tile.WIDTH, col*Tile.HEIGHT);
        }
    }
    player.sprite.draw(g);
    batch.end();
}


Now, depending on how complex of pathfinding you need, all you might need to do is check the position in your array.  I recommend having an enum of Directions

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
public static enum Direction
{
    North(0,-1), South(0, 1), East(1, 0), West(-1, 0);
   
    public final int x;
    public final int y;
   
    Direction(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
}


Then check a tile collision like so

1  
2  
3  
4  
public boolean doesCollide(int x, int y, Direction d)
{
    return tileSet[tiles[x + d.x][y + d.y]].passable;
}


Each tile type just needs to have a boolean set on it saying if you can step on it or not.  More complex collision detection idea where tiles can only be passable from certain directions can work the same way.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
[code]
public boolean doesCollide(int x, int y, Direction d)
{
    boolean passable;
    Tile t = tileSet[tiles[x + d.x][y + d.y]];
    switch(d)
    {
        case (North):
            passable = t.passableFromSouth;
            break;
        case (South):
            passable = t.passableFromNorth;
            break;
        case (East):
            passable = t.passableFromEast;
            break;
        case (West):
            passable = t.passableFromSouth;
            break;
    }
    return passable;
}


but requires for values to be set with each tile.


Now if you're doing pixel movement of your character, you can take their pixel position and integer divide it back into the array position and use the same collision detection as above

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
/**
 * Movement by pixels
 */

public void move(Direction d)
{
    if (doesCollide((int)(player.sprite.getX() / Tile.WIDTH) , (int)(player.sprite.getY() / Tile.HEIGHT), d)
    {
       player.sprite.translate(d.x, d.y);
    }
}


and if you don't want to do it by pixels, and instead just by full tiles
1  
2  
3  
4  
5  
6  
7  
8  
9  
public void move(Direction d)
{
    if (doesCollide(player.x , player.y, d)
    {
       player.x += d.x;
       player.y += d.y;
       player.sprite.setPosition(player.x * Tile.WIDTH, player.y * Tile.HEIGHT);
    }
}


That should be enough to handle simple tile maps and collision.  I remember doing something along the same lines awhile ago with another project of mine.  If you want to check it out, it's an RPG engine written using Java2D http://git.io/LQiAFg[/code]
Offline samcp12

Junior Member


Medals: 1



« Reply #6 - Posted 2013-06-24 05:15:20 »

Thanks for the help, I managed to sort out my tile problem and it now works as desired, as for the path finding you suggested, I am actually going to use a* path finding, thanks anyway that was a lot of code and I took note of it Smiley
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (18 views)
2014-07-30 21:08:39

Riven (27 views)
2014-07-29 18:09:19

Riven (16 views)
2014-07-29 18:08:52

Dwinin (14 views)
2014-07-29 10:59:34

E.R. Fleming (35 views)
2014-07-29 03:07:13

E.R. Fleming (13 views)
2014-07-29 03:06:25

pw (44 views)
2014-07-24 01:59:36

Riven (45 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!