java - screen coordinates inside a grid libgdx -
i've created grid using shaperenderer.line. draws 10x10 grid.
private void setupcamera() { // todo auto-generated method stub camera = new orthographiccamera(viewport_width, viewport_height); camera.position.set(camera.viewportwidth / 2, camera.viewportheight / 2, 0f); camera.update(); } @override public void draw() { for(int x = 0; x < rows; x++){ for(int y = 0; y < columns; y++){ shaperenderer.line(x+1, 12-y, x+2, 12-y); shaperenderer.line(x+1, 12-y, x+1, 11-y); shaperenderer.line(x+2, 12-y, x+2, 11-y); shaperenderer.line(x+1, 11-y, x+2, 11-y); } } } public class cell { private static int gridwidth = 1; private static int gridheight = 1; public int coordinatesx; public int coordinatesy; public cell(){} public cell(int x, int y){ this.coordinatesx = x; this.coordinatesy = y; } public void drawline(shaperenderer shaperenderer){ int left = coordinatesx * gridwidth; int right = left + gridwidth; int bottom = coordinatesy * gridheight; int top= bottom + gridheight; shaperenderer.line(left , top, left, bottom); shaperenderer.line(right , top, right , bottom); shaperenderer.line(left , top, right , top); shaperenderer.line(left , bottom, right , bottom); } public void drawimagestartposition(spritebatch spritebatch, int x, int y, texture texture){ int left = x * texture.getwidth(); int bottom = y * texture.getheight(); spritebatch.draw(texture, left, bottom, texture.getwidth(), texture.getheight()); } }
i've created texture (ball.png) , i'm trying accomplish want place texture inside cell (using screen coordinates of particular cell) based on grid i've created.
anybody can give me pointer how should go it?
try turning each grid cell object.
public class gridsquare { private static int gridwidth = 10; private static int gridheight = 10; public final int xpos; public final int ypos; public gridsquare(int x, int y){ xpos = x; ypos = y; } public void drawoutline(shaperenderer shaperenderer){ /* if gridwidth , gridheight don't change on time can move calculation of left, right, bottom , top positions constructor better performance. */ int left = xpos * gridwidth; int right = left + gridwidth; int bottom = ypos * gridheight; int top= bottom + gridheight; shaperenderer.line(left , top, left, bottom); shaperenderer.line(right , top, right , bottom); shaperenderer.line(left , top, right , top); shaperenderer.line(left , bottom, right , bottom); } public void drawimage(spritebatch spritebatch, texture texture){ /* if gridwidth , gridheight don't change on time can move calculation of left , bottom positions constructor better performance. */ int left = xpos * gridwidth; int bottom = ypos * gridheight; spritebatch.draw( texture, left, bottom, gridwidth, gridheight ); } } then can iterate on them , tell them render , know how it.
shaperenderer.begin(); for(int x = 0; x < rows; x++){ for(int y = 0; y < columns; y++){ gridsquares[x][y].drawoutline(shaperenderer); } } shaperenderer.end(); spritebatch.begin(); for(int x = 0; x < rows; x++){ for(int y = 0; y < columns; y++){ // assuming have loaded png image variable named balltexture gridsquares[x][y].drawimage(spritebatch, balltexture); } } spritebatch.end();
Comments
Post a Comment