0

So I was wondering how to run through a multi-dimensional array without having to use for loops to test whether or not the objects are intersecting in a rectangle and then render the interesting?

Currently I'm using two for loops to go through it and within the nested loop I have to use the intersecting() method and since this needs to happen every frame my game is getting low FPS. I assume it's because I have 650x350 entities in my array. I'll show the code below and restate the question. So my official question is how do I test whether or not an entity is intersecting with my rectangle camera so that it doesn't lag the game?

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
      }
   }
}

2 Answers2

1

This depends quite a bit on the mechanics of your game. But in a general sense I will recommend looking into Quadtrees ( http://en.m.wikipedia.org/wiki/Quadtree). Then check which nodes your camera overlaps and draw their contents.

0

When I ran into this problem awhile ago, I made my render function use the camera's x, y, width, and height values to determine where to render by changing the starting positions of the for loops.

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);
                    }
                }
            }
        }
    }
}
  • This works great but I scale out in my game, I'm using the Slick2D library, and if I try scaling then nothing gets rendered. I don't know how Slick2D translates locations based on scale so can anyone help? My scale is 0.8 – Dreadnought Mar 28 '14 at 17:59