Java 网格libgdx内的屏幕坐标
我已经使用shaperender.line创建了一个网格。它绘制了一个10x10的网格Java 网格libgdx内的屏幕坐标,java,android,libgdx,Java,Android,Libgdx,我已经使用shaperender.line创建了一个网格。它绘制了一个10x10的网格 private void setupCamera() { // TODO Auto-generated method stub camera = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_HEIGHT); camera.position.set(camera.viewportWidth / 2, camera
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());
}
private void setupCamera(){
//TODO自动生成的方法存根
摄影机=新正交摄影机(视口宽度、视口高度);
camera.position.set(camera.viewportWidth/2,camera.viewportHeight/2,0f);
camera.update();
}
@凌驾
公众抽签(){
对于(int x=0;x
}
我已经创建了一个纹理(ball.png),我试图完成的是基于我创建的网格将该纹理放置在一个单元格内(使用该特定单元格的屏幕坐标)
任何人都可以给我一个指针,告诉我应该怎么做?试着把每个网格单元变成一个对象
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 the gridWidth and gridHeight don't change over time then you can
move the calculation of the left, right, bottom and top positions
into the constructor for 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 the gridWidth and gridHeight don't change over time then you can
move the calculation of the left and bottom positions
into the constructor for better performance.
*/
int left = xPos * gridWidth;
int bottom = yPos * gridHeight;
spritebatch.draw(
texture,
left,
bottom,
gridWidth,
gridHeight
);
}
}
然后你可以迭代他们,告诉他们自己渲染,他们就会知道怎么做
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 you have loaded the png image into a variable named balltexture
gridSquares[x][y].drawImage(spritebatch, balltexture);
}
}
spritebatch.end();
shaperender.begin();
对于(int x=0;x
谢谢!我会想出那个答案的。另外,在绘制10x10网格之前,我调用了shaperender.setProjectionMatrix(camera.combined)。据我所知,这改变了坐标系。是否有一种方法可以将ShaperEnder.line坐标与spritebatch.draw坐标匹配?spritebatch有一种类似的方法setProjectionMatrix。将ShaperEnder和SpriteBatch的投影矩阵设置为摄影机组合矩阵将确保在相同的坐标空间中绘制形状和图像。我已经用camera.combined(Spritebatch.setProjectionMatrix(camera.combined))设置了Spritebatch setProjectionMatrix,网格看起来还可以;然而,它使balltexture.png图像变得巨大。这是一张32x28像素的小图片。我怎样才能把它恢复到正常尺寸?你用的是什么样的相机?它应该是正交的。另外,您将网格宽度和高度设置为什么?发布你的更新代码会很有帮助。我已经更新了相机的代码。我使用了VIEWPORT_WIDTH=20和VIEWPORT_HEIGHT=13。我没有设置网格的宽度和高度,因为我只是使用线坐标来绘制网格