lundi 23 février 2015

Unable to programmatically lay a brick wall in libgdx



I am trying to recreate a brick wall for a school project. The brick wall would look something like this, but fill the whole screen: brick wall


Currently, I am able to get it to look like this:current impl The texture file I'm using is this: bricktexture


The code I'm using to do this looks like this: Constants file that determines the texture size:



public static final String TEXTURE_BRICK = "tiles/Bricks.png";
public static final Vector2 TEXTURE_BRICK_SIZE = new Vector2(8, 12);
/* Bricks */
public static final Vector2 TILE_LOCATION_LEFT_BRICK = new Vector2(12, 120);
public static final Vector2 TILE_LOCATION_RIGHT_BRICK = new Vector2(0, 120);


From the above code you can see that I'm basically selecting the final brick only, that's essentially a double-brick. I split it into 2 halves, and try to alternate each half, starting with a different half for every other row. You can see this being done here:



for (int i = CANVAS_WIDTH; i >= 0; i--) {
for (int j = CANVAS_HEIGHT; j >= 0 ; j--) {
if (i % 2 == 0)
addDrawable(String.valueOf("brick: " + i + "," + j), new BrickTile(null, new Vector2(i, j), "right"));
else
addDrawable(String.valueOf("brick: " + i + "," + j), new BrickTile(null, new Vector2(i, j), "right"));
}
}


addDrawable basically just adds a particular tile to a collection of tiles that I draw on the screen at the given coordinates.


Finally, the camera settings I'm using are:



mGameContext.setCamera(new OrthographicCamera((float)CANVAS_WIDTH, (float)CANVAS_HEIGHT ));
mGameContext.setViewport(new StretchViewport(CANVAS_WIDTH, CANVAS_HEIGHT));
mGameContext.getCamera().position.set(mGameContext.getCamera().viewportWidth / 2f,
mGameContext.getCamera().viewportHeight / 2f, 0);


The constants you see are:



private static final int CANVAS_WIDTH = 40;
private static final int CANVAS_HEIGHT = 36;


These values represent the fact that i want 40 (half) bricks in each row, and 36 rows of bricks. When two bricks are put together (as seen in the brick wall image above, there should be 20 in each row.


If you look at the screenshot from my device, you'll notice that only 1 half of the texture is being drawn. And that there are lots of black gaps between each of the bricks. If I change the brick texture size to be an entire brick in length (8x24px instead of 8x12px) they overlap each other within their rows (so there are no vertical gaps at least) but there are still horizontal gaps.


I've tried so many permutations of changing texture sizes, camera sizes, viewport sizes, etc. and I am just unable to select the appropriate settings to make it work.


Can anyone who has experience with this suggest a solution so that I can move on from trying to manually lay bricks and actually start drawing stuff on top of the bricks?




Aucun commentaire:

Enregistrer un commentaire