Tile-based Collision Detection Where Sprite is bigger than Tiles

Hey guys, like the title says, I’m having a tough time getting my collision detection to work properly. My game is a sidescrolling platformer and I’m wanting to use a tiled-based collision scheme, where each tile is either passable or impassable. I already know how to assign each tile a rectangle, but I’m not sure how to iterate over the tiles surrounding the player because my player’s bounding box is twice as wide and 4 times as high as one tile (16 x 16 vs. 32 x 64). My actual sprite’s dimensions are 64 x 64 (not that it matters).

Does anyone have any experience with this kind of a collision detection setup?

public boolean collide(Rectangle in)
    {
        for (int y = (int)((in.y) / TEXTURE_SIZE)-4; y <= (int)((in.y+in.height) / TEXTURE_SIZE)+4; y++)
        {
            for (int x = (int)((in.x) / TEXTURE_SIZE)-4; x <= (int)((in.x+in.width) / TEXTURE_SIZE)+4; x++)
            {
                if (x >= 0 && y >= 0 && x < width && y < height)
                {
                    if (tiles[x][y] != null)
                    {
                        checkRect.setBounds(x*TEXTURE_SIZE,y*TEXTURE_SIZE, TEXTURE_SIZE, TEXTURE_SIZE);
                        if (in.intersects(checkRect))
                        {
                            return true;
                        }
                    }
                }
            }
        }

      return false;
    }

Doesn’t matter how big the object is. This is my method.

note: checkRect is just a Rectangle in the class to do these calculations, just so you dont usw “new” as often, reusing this object

Aha, I see what you’re doing. You’ve basically made a bounding box that’s bigger than the object (player in my case) by 4 pixels all around and you’re looping through all of the box’s edge pixels ( or is it 4 tiles all around the object instead? ) to check for intersection with a collide-able tile.

Do you mind if I ask you how you resolve your collisions, specifically what you do with you player to prevent them from passing through the collide-able object?

its 4 tiles around. You can also use this method to render, then you will see what tiles are checked for collision, as only those are rendered on screen. or rendering all, but overlaying those with a color or something

This is a really broad topic, and I can’t say that after long time of development I got it right. It obviously has a lot to do with what constraints you have.
gravity, shaped and nature of objects, what can the player do
specifically diagonal tiles, going up and down can be a nightmare if your player can do much to break it.

But basically my sprites have a x and y speed
I will create another Rectangle(reuse this object) copy the values of the player or whatever sprite we are talking about.
then I do x+=speedX and y+=speedY obviously
and then I take this changed Rectangle and ask if there is a collision with it.
If yes I wont apply that speed for the sprite, if no, I do.

Basically.
there are always little problems and stuff thats 1 pixel off and so on and so forth, hard to get perfect depending on how complex the game is.
I mean even in super mario you can jump into the wall if you know how

clipping and collision issues are found in most commercial games even today

hard to get right

Ok I think I get it now, I can basically make a copy of the player’s rect, apply the intended movement to it, then check collision for that rect, and if there was no collision then set the player’s actual rect to the values of the temp rect.

I do have one question though. Why do you check for collision 4 tiles around the object/player instead of just the immediate neighboring tiles that surround the object/player?

not written in concrete or anything. you check how many you think is good, I just wanna be sure
partially it has to do with the fact that I had different sizes tiles once

Oh ok I see. I’ll probably end up doing just 1 or two tiles away for mine. I’m going to try and code this up tonight after work and hopefully I can get it working. Thanks for your help thus far!