Collision detection on a scrolling world

Hi all !

I’m new, I come from php and have already done a web browser game, and now I code in java for android, and I’m making my first game on android/java from scratch (no framework, I know, I’m mad!)

I know the theory of collision detection with Rectangles

But here is a problem for me:

The game : the hero can only jump, like https://play.google.com/store/apps/details?id=com.andoop.freerunning

The world : is scrolling to the left with obstacle to jump above

For my question I set for example this:

Integer world_speed = 50; // in pixel
Integer block_height = 32; // in pixel
Integer block_width = 32; // in pixel

so, each frame the world will draw 50 pixel to the left (the world is scrolling to the left)

http://img15.hostingpics.net/pics/613354questionjavagaming.jpg

THE QUESTION :

How to detect collision in the case of a infinite scrolling world between blocks and hero when the world is scrolling left with N pixels for each frames?


Best regards and thanks for having reading this

Instead of


player.x += 50;
player.checkCollision();

do this:


int step = 5;
for (int i = 0; i < 50 / step; i++) 
{
     player.x += step;
     player.checkCollision();
}

@trollwarrior1: thanks for replying

But:

if Mario has the same width as the blocks, so the step in your code should be equal to 1, and it will generate too much detection loops

I’m sure that there is a better solution, is’nt it?

Bests regards

Try simply resizing the mario character bounds to a smaller width and height so he wont collide with so much at a time.

So you can basically trade off accuracy and speed. Using trollwarrior’s reply, the more you steps you have, the more accurate the collision detection is. The fewer steps you use, the faster it will run. You shouldn’t notice any difference with fewer than a hundred colliders though, so you shouldn’t worry about collision detection slowing the game down.

Ok. Thanks everybody for the replies. I will try it asap

Hi AndroidAddict :slight_smile:

For the collision detection in tile based games you have a few options depending on how fast you want to make it.

Note: i suppose you are using a two dimensional array for the tile map, that way it is much easier to get the right tile simply by knowing the coordinates of the tile in tile coordinates ((int)(ScreenPosition/tileSize))

Method A:

  1. Create an array in the class where you run you collision code (should not be a local variable, but an instance variable)
  2. Before checking collisions, get every tile around marion by tile coordinates (this works good with objects that are, at best just a little smaller than a tile)
    Then you add the tiles around mario to the array, by getting them mathematically, based on marios center position.

collisionArray.add(tilemap.getTile(mario.tilePositionX-1, mario.tilePositionY-1)); /*Bottom Left*/
collisionArray.add(tilemap.getTile(mario.tilePositionX, mario.tilePositionY-1)); /*Bottom Center*/
collisionArray.add(tilemap.getTile(mario.tilePositionX+1, mario.tilePositionY-1)); /*Bottom Right*/
collisionArray.add(tilemap.getTile(mario.tilePositionX-1, mario.tilePositionY)); /*Center Left*/
collisionArray.add(tilemap.getTile(mario.tilePositionX+1, mario.tilePositionY)); /*Center Right*/
collisionArray.add(tilemap.getTile(mario.tilePositionX-1, mario.tilePositionY+1)); /*Top Left*/
collisionArray.add(tilemap.getTile(mario.tilePositionX, mario.tilePositionY+1)); /*Top Center*/
collisionArray.add(tilemap.getTile(mario.tilePositionX+1, mario.tilePositionY+1)); /*Top Right*/

  1. iterate the collision array and check collision against the saved tiles + check if is collideable etc.
  2. remove all the added tiles (empty out the collision array)
  3. do it again

Method B:
Get a certain region of tiles from the tile map based on marios center position.
You can loop through the tile map for this, with an offset to the left, right, top, bottom, to make sure there are always tiles in the collision array that mario collides with.


for(int y = 0; y < collisionChunkHeight; y++){
     for(int x = 0; x < collisionChunkWidth; x++){
          collisionArray.add(tilemap.getTile.((mario.tilePositionX-collisionChunkWidth)+x, (mario.tilePositionY-collisionChunkHeight)+y));
     }
}

And then you have a region of tiles that are around mario, that you can then check the collision against.

There are a few more, in case, but i guess these are pretty simple and performant enough.