Java渲染仅相交?
所以我想知道如何在不必使用for循环来测试对象是否与矩形相交的情况下运行多维数组,然后渲染有趣的对象 目前,我使用两个for循环来遍历它,在嵌套循环中,我必须使用Java渲染仅相交?,java,render,slick2d,Java,Render,Slick2d,所以我想知道如何在不必使用for循环来测试对象是否与矩形相交的情况下运行多维数组,然后渲染有趣的对象 目前,我使用两个for循环来遍历它,在嵌套循环中,我必须使用intersecting()方法,因为这需要在每一帧都发生,所以我的游戏的FPS很低。我想这是因为我的数组中有650x350个实体。我将在下面展示代码并重申问题。所以我的官方问题是,我如何测试一个实体是否与我的矩形相机相交,这样它就不会滞后于游戏了 for (int x = 0; x < entities.length; x++)
intersecting()
方法,因为这需要在每一帧都发生,所以我的游戏的FPS很低。我想这是因为我的数组中有650x350个实体。我将在下面展示代码并重申问题。所以我的官方问题是,我如何测试一个实体是否与我的矩形相机相交,这样它就不会滞后于游戏了
for (int x = 0; x < entities.length; x++) // entities.length is 650
{
for (int y = 0; y < entities[0].length; y++) // entities[0].length is 350
{
if (camera.intersecting(entities[x][y]))
{
entities[x][y].render(g); // X and Y indices are multiplied by 32 to get the position
}
}
}
for(int x=0;x
这在很大程度上取决于游戏的机制。但在一般意义上,我建议研究四叉树()。然后检查相机重叠的节点并绘制它们的内容。当我不久前遇到这个问题时,我让渲染函数使用相机的x、y、宽度和高度值,通过更改for循环的起始位置来确定渲染的位置
public void render(Camera camera, Graphics g) {
// --------------------------------------------------------- //
// Optimized rendering function to only draw what the camera //
// can see //
// --------------------------------------------------------- //
int minX = toTileX(camera.getX());
int minY = toTileY(camera.getY());
int maxX = toTileX(camera.getX()) + toTileX(camera.getViewWidth());
int maxY = toTileY(camera.getY()) + toTileY(camera.getViewHeight());
for (TiledLayer l : layers) {
for (int y = minY; y < maxY + 1; y++) {
if (y < height && y >= 0) {
for (int x = minX; x < maxX + 1; x++) {
if (x < width && x >= 0) {
Tile tile = l.getTile(x, y);
TileSheet sheet = null;
try {
sheet = tileSheets.get(tile.getTileSetId());
} catch (InvalidKeyException e) {
e.printStackTrace();
}
Image image = sheet.getImage(tile.getTileId());
g.drawImage(image,
x * tileWidth,
y * tileHeight,
(x * tileWidth) + tileWidth,
(y * tileHeight) + tileHeight,
0,
0,
tileWidth,
tileHeight);
}
}
}
}
}
}
public void渲染(摄影机、图形g){
// --------------------------------------------------------- //
//优化渲染功能,仅绘制相机所需的内容//
//看得见//
// --------------------------------------------------------- //
int minX=toTileX(camera.getX());
int minY=toTileY(camera.getY());
int maxX=toTileX(camera.getX())+toTileX(camera.getViewWidth());
int maxY=toTileY(camera.getY())+toTileY(camera.getViewHeight());
对于(平铺层l:层){
对于(int y=minY;y=0){
对于(int x=minX;x=0){
Tile-Tile=l.getTile(x,y);
TileSheet表=空;
试一试{
sheet=tileSheets.get(tile.getTileSetId());
}捕获(InvalidKeyException e){
e、 printStackTrace();
}
Image=sheet.getImage(tile.getTileId());
g、 drawImage(图像,
x*tileWidth,
y*tileHeight,
(x*tileWidth)+tileWidth,
(y*瓷砖高度)+瓷砖高度,
0,
0,
瓦莱维特,
瓷砖重量);
}
}
}
}
}
}
你可以检查实体的位置和大小是否与它们在数组中的位置相关?是的,每个索引都乘以宽度/高度以获得位置。此外,世界被缩小到0.8英尺。这很好,但我在游戏中缩小了,我使用的是Slick2D库,如果我尝试缩放,则不会渲染任何东西。我不知道Slick2D如何根据比例转换位置,所以有人能帮忙吗?我的分数是0.8