Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am using Android API's and using the RectF class for rectangles. I have a list of blocks that are on the screen at one time. Each block is given a random position, and then I go through all of the tiles previous, to check if there is an intersection. If there is, it recursively calls the method again to get a new random position and check it against all the other tiles.

I feel like this should work perfectly, except for one thing that may be affecting it. Since recursion opens up so many instances of a method, it needs to finish them off, so I don't know if the rectangle is just being drawn at the initial position, and the rest of the calls are doing nothing?

I have tried quite a few different methods and this seems to be the closest, but something weird is happening and I cant figure it out.

Start method:

    private void start() {

    timerTask = new TimerTask() {
        @Override
        public void run() {

            if(Game.STATE == Game.State.Playing) {                  
                removeFailedTiles();

                Random deathRate = new Random();
                int i = deathRate.nextInt(100);
                if(i % 10 == 0) {
                    System.out.println("Death tile");
                    activeDeathTiles.add(new DeathTile(getRandomPosition()));
                    //System.out.println(activeDeathTiles.toString());
                } else {
                    System.out.println("Normal Tile");
                    activeTiles.add(new Tile(tileLifetime, getRandomPosition()));
                }

            }

            if(Game.STATE == Game.State.Lost) {
                timerTask.cancel();
                timer.cancel();
            }
        }
    };

    timer.schedule(timerTask, 1000, tileDelay);

}

Get Random Position method:

private RectF getRandomPosition() {
    int screenWidth = (int) Game.SCREEN_WIDTH;
    int screenHeight = (int) Game.SCREEN_HEIGHT;

    RectF pos = new RectF();

    Random random = new Random();
    int maxX = (int) (screenWidth - (Tile.TILE_WIDTH * 1.5f)), minX = (int) (Tile.TILE_WIDTH / 1.5f);
    int maxY = (int) (screenHeight - (Tile.TILE_HEIGHT * 1.5f)), minY = (int) (Tile.TILE_HEIGHT / 1.5f);

    pos.top = random.nextInt(maxY - minY) + minY;
    pos.left = random.nextInt(maxX - minX) + minX;
    pos.bottom = pos.top + Tile.TILE_HEIGHT;
    pos.right = pos.left + Tile.TILE_HEIGHT;

    if(isOverlap(pos)) {
        return getRandomPosition();
    } else {
        return pos;
    }
}

Helper methods:

private List<RectF> getAllTiles() {
    List<RectF> allTiles = new ArrayList<RectF>();

    for(RectF t : activeDeathTiles) 
        allTiles.add(t);
    for(RectF t : activeTiles) 
        allTiles.add(t);

    return allTiles;
}

private boolean isOverlap(RectF position) {

    for(RectF r : getAllTiles()) {
        if(RectF.intersects(position, r)) {
            return true;
        }
    }

    return false;
}

If someone could please help me figure out what is going wrong here that would be stellar!

share|improve this question
    
What exactly is going wrong with this code? –  MadEqua Jun 21 at 15:08
    
The blocks are still placed on top of each other, although im saying if there is a collision to get a new position –  Kyle Jensen Jun 21 at 15:30

1 Answer 1

All of your code seems fine exept for this typo on getRandomPosition():

pos.right = pos.left + Tile.TILE_HEIGHT;

which should be

pos.right = pos.left + Tile.TILE_WIDTH;

I'm not sure if that can be the root cause of the problem, but there's always the option of not using recursion and use a loop, should be easier to debug:

private RectF getRandomPosition() {
    RectF pos = new RectF();

    int screenWidth = (int) Game.SCREEN_WIDTH;
    int screenHeight = (int) Game.SCREEN_HEIGHT;

    Random random = new Random();
    int maxX = (int) (screenWidth - (Tile.TILE_WIDTH * 1.5f)), minX = (int) (Tile.TILE_WIDTH / 1.5f);
    int maxY = (int) (screenHeight - (Tile.TILE_HEIGHT * 1.5f)), minY = (int) (Tile.TILE_HEIGHT / 1.5f);

    do {
        pos.top = random.nextInt(maxY - minY) + minY;
        pos.left = random.nextInt(maxX - minX) + minX;
        pos.bottom = pos.top + Tile.TILE_HEIGHT;
        pos.right = pos.left + Tile.TILE_WIDTH;
    }
    while(isOverlap(pos));

    return pos;
}

Bear in mind that if the screen gets too full the loop may not be able to finish. To fix it, something like a maximum number of loops before giving up can be added.

share|improve this answer
    
The tiles are squares so width height is the same but technically yes thats right but wouldnt do it. The way this works the screen will never be that full so it should be okay on that end. Il try the loop –  Kyle Jensen Jun 21 at 16:16
    
The loop you have there doesnt do anything? Shouldnt i be resetting the position in there? –  Kyle Jensen Jun 21 at 16:18
    
Writing on pos.top, pos.left, etc is effectively resetting the position. –  MadEqua Jun 21 at 16:25
    
Ohh i didnt see the do { thats why i was confused –  Kyle Jensen Jun 21 at 16:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.