Bc1151 Bc1151 - 2 months ago 13
Java Question

Drawing squares that attach to adjacent squares libgdx

I am attempting to draw a tile based floor for a game.

Basically each tile has a height value, and I am trying to get each cornet to connect to the adjacent tiles in a smooth way. So like, if the tile was at a height of 2, and the tile beside it was at a height of 3, then they would smoothly connect on a slope.

Here is my code for getting this as of now:

--see below for updated code --

The height of the tile is obtained by Tile.getCenter();
I am not sure where to go from here, I am using libGDX.

px and py are just the center of the loading point, and floor is the LibGDX model batch for the floor, hopefully containing each floor time in the end.

Thanks in advance!

-Bc

--Edit--

Just a little more info, The vertex class basically just stores two ints.

The tl, tr, bl, br, are for the tile to know Top left height, top right height (etc). -- I renamed everything to be a little more clear

This class is just to find the angles that I need, basically where each corner is rendered, once I have that I can find out how to render it on my own.

On the note of tl, tr, etc. should I be finding the corners? or the center of the sides? I have never done 3d gfx before.

--Edit 3--

I worked a bit more out on my own, hopefully this is closer:

private Map<Vertex, Tile> tiles = new HashMap<Vertex, Tile>();
public float getMode(float[] data)
{
int total = 0;
for (int i = 1; i < data.length; i++){
total += data[i];
}
return total / data.length;
}
protected void loadTiles(int px, int py){
//load tile from file
int FIXTHISTOSIZE = 10;
for (int x = px - (FIXTHISTOSIZE / 2); x <= FIXTHISTOSIZE; x++){
for (int y = (py - FIXTHISTOSIZE / 2); y <= FIXTHISTOSIZE; y++){
int aY = py - y;
int aX = px - x;
Tile tile = new Tile(aX, aY);
tiles.put(new Vertex(aX, aY) , tile);
}
}
for (int x = px - (FIXTHISTOSIZE / 2); x <= FIXTHISTOSIZE; x++){
for (int y = py - (FIXTHISTOSIZE / 2); y <= FIXTHISTOSIZE; y++){
int aY = py + y;
int aX = px + x;
Vertex pos = new Vertex(aX, aY);

float TOP_LEFT = -65536, TOP_RIGHT = -65536, BOTTOM_LEFT = -65536, BOTTOM_RIGHT = -65536; //TODO these should be 0, they are this number to demonstrate unloaded tiles for now
Tile TILE_CENTER = tiles.get(pos);
Tile TILE_TOP_RIGHT = tiles.get(new Vertex(aX + 1, aY + 1));
Tile TILE_TOP = tiles.get(new Vertex(aX, aY + 1));
Tile TILE_RIGHT = tiles.get(new Vertex(aX + 1, aY));
Tile TILE_BOTTOM_RIGHT = tiles.get(new Vertex(aX + 1, aY - 1));
Tile TILE_BOTTOM = tiles.get(new Vertex(aX, aY - 1));
Tile TILE_BOTTOM_LEFT = tiles.get(new Vertex(aX - 1, aY - 1));
Tile TILE_LEFT = tiles.get(new Vertex(aX - 1, aY));
Tile TILE_TOP_LEFT = tiles.get(new Vertex(aX - 1, aY + 1));
//TOP_RIGHT
if (TILE_TOP != null && TILE_TOP_RIGHT != null && TILE_RIGHT != null){
float[] dub = {TILE_TOP.getCenter(), TILE_TOP_RIGHT.getCenter(),TILE_RIGHT.getCenter(),TILE_CENTER.getCenter()};
TOP_RIGHT = getMode(dub);
}
//TOP_LEFT
if (TILE_TOP != null && TILE_TOP_LEFT != null && TILE_LEFT != null){
float[] dub = {TILE_TOP.getCenter(), TILE_TOP_LEFT.getCenter(),TILE_LEFT.getCenter(),TILE_CENTER.getCenter()};
TOP_LEFT = getMode(dub);
}
//TODO BOTTOM_RIGHT
//TODO BOTTOM_LEFT
TILE_CENTER.setConditions(TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT);
}
}

Answer

I FINALLY figured it out!

initialize:

int FIXTHISTOSIZE = 10;
    for (int x = px - (FIXTHISTOSIZE / 2); x <= FIXTHISTOSIZE; x++){
        for (int y = py -(FIXTHISTOSIZE / 2); y <= FIXTHISTOSIZE; y++){
            int aY = py + y;
            int aX = px + x;
            Tile tile = new Tile(aX, aY);
            tiles.put("" + aX + aY, tile);
        }
    }
    this.loadTiles(px, py);

main loop (loadTiles):

int FIXTHISTOSIZE = 10;
    for (int x = px - (FIXTHISTOSIZE / 2); x <= FIXTHISTOSIZE; x++){
        for (int y = py - (FIXTHISTOSIZE / 2); y <= FIXTHISTOSIZE; y++){
            int aY = py + y;
            int aX = px + x;

            float TOP_LEFT = 0, TOP_RIGHT = 0, BOTTOM_LEFT = 0, BOTTOM_RIGHT = 0;
            Tile TILE_CENTER = tiles.get("" + aX + aY);
            if (TILE_CENTER.isLoaded()) {
                continue;
            }
            Tile TILE_TOP_RIGHT = tiles.get("" + (aX + 1) + (aY + 1));
            Tile TILE_TOP = tiles.get("" + aX + (aY + 1));
            Tile TILE_RIGHT = tiles.get("" + (aX + 1) + aY);
            Tile TILE_BOTTOM_RIGHT = tiles.get("" + (aX + 1) + (aY - 1));
            Tile TILE_BOTTOM = tiles.get("" + aX + (aY - 1));
            Tile TILE_BOTTOM_LEFT = tiles.get("" + (aX - 1) + (aY - 1));
            Tile TILE_LEFT = tiles.get("" + (aX - 1) + aY);
            Tile TILE_TOP_LEFT = tiles.get("" + (aX - 1) + (aY + 1));
            //TOP_RIGHT
            if (TILE_TOP != null && TILE_TOP_RIGHT != null && TILE_RIGHT != null){
                float[] dub = {TILE_TOP.getCenter(), TILE_TOP_RIGHT.getCenter(),TILE_RIGHT.getCenter(),TILE_CENTER.getCenter()};
                TOP_RIGHT = getMedian(dub);
            }
            //TOP_LEFT
            if (TILE_TOP != null && TILE_TOP_LEFT != null && TILE_LEFT != null){
                float[] dub = {TILE_TOP.getCenter(), TILE_TOP_LEFT.getCenter(),TILE_LEFT.getCenter(),TILE_CENTER.getCenter()};
                TOP_LEFT = getMedian(dub);
            }   
            //BOTTOM_LEFT
            if (TILE_BOTTOM != null && TILE_BOTTOM_LEFT != null && TILE_LEFT != null){
                float[] dub = {TILE_BOTTOM.getCenter(), TILE_BOTTOM_LEFT.getCenter(),TILE_LEFT.getCenter(),TILE_CENTER.getCenter()};
                BOTTOM_LEFT = getMedian(dub);
            }   
            //BOTTOM_RIGHT
            if (TILE_BOTTOM != null && TILE_BOTTOM_RIGHT != null && TILE_RIGHT != null){
                float[] dub = {TILE_BOTTOM.getCenter(), TILE_BOTTOM_RIGHT.getCenter(),TILE_RIGHT.getCenter(),TILE_CENTER.getCenter()};
                BOTTOM_RIGHT = getMedian(dub);
            }   

            TILE_CENTER.setConditions(TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT);

median:

public static float getMedian(float[] data) {
    float[] copy = Arrays.copyOf(data, data.length);
    Arrays.sort(copy);
    return (copy.length % 2 != 0) ? copy[copy.length / 2] : (copy[copy.length / 2] + copy[(copy.length / 2) - 1]) / 2;
}

Hopefully this will help somebody in the future

Result:

http://i.imgur.com/X66vDxT.png