2D Side scroller collision detection

Posted by Shanon Simmonds on Game Development See other posts from Game Development or by Shanon Simmonds
Published on 2013-10-29T06:54:13Z Indexed on 2013/10/29 10:22 UTC
Read the original article Hit count: 292

I am trying to do some collision detection between objects and tiles, but the tiles do not have there own x and y position, they are just rendered to the x and y position given, there is an array of integers which has the ids of the tiles to use(which are given from an image and all the different colors are assigned different tiles)

int x0 = camera.x / 16;
int y0 = camera.y / 16;
int x1 = (camera.x + screen.width) / 16;
int y1 = (camera.y + screen.height) / 16;
for(int y = y0; y < y1; y++) {
    if(y < 0 || y >= height) continue; // height is the height of the level
    for(int x = x0; x < x1; x++) {
        if(x < 0 || x >= width) continue; // width is the width of the level
        getTile(x, y).render(screen, x * 16, y * 16);
    }
}

I tried using the levels getTile method to see if the tile that the object was going to advance to, to see if it was a certain tile, but, it seems to only work in some directions. Any ideas on what I'm doing wrong and fixes would be greatly appreciated.

What's wrong is that it doesn't collide properly in every direction and also this is how I tested for a collision in the objects class

if(!level.getTile((x + xa) / 16, (y + ya) / 16).isSolid()) {
    x += xa;
    y += ya;
}

EDIT: xa and ya represent the direction as well as the movement, if xa is negative it means the object is moving left, if its positive it is moving right, and same with ya except negative for up, positive for down.

© Game Development or respective owner

Related posts about java

Related posts about collision-detection