LibGDX - Collision & Rendering Issues

Hi!

I am currently using LibGDX & Tiled in order to create a test game for collision and other mechanics, to get used to them.
The problem is that the collision method I am using is causing a weird issue. I believe it is most likely a problem with the cell size… anyways, check out my code:

        private boolean isCellBlocked(float xx, float yy){
                Cell cell = collisionLayer.getCell((int) (xx/collisionLayer.getTileWidth()), (int) (yy/collisionLayer.getTileHeight()));
                return cell != null && cell.getTile() != null && cell.getTile().getProperties().containsKey(blockedKey);
        }

        public boolean collidesRight() {
                for(float step = 0; step < player.height; step += collisionLayer.getTileHeight())
                        if(isCellBlocked(player.x + player.width , player.y + step))
                                return true;
                return false;
        }
 
        public boolean collidesLeft() {
                for(float step = 0; step < player.height; step += collisionLayer.getTileHeight())
                        if(isCellBlocked(player.x, player.y + step))
                                return true;
                return false;
        }
 
        public boolean collidesTop() {
                for(float step = 0; step < player.width; step += collisionLayer.getTileWidth())
                        if(isCellBlocked(player.x + step, player.y + player.height))
                                return true;
                return false;
 
        }
 
        public boolean collidesBottom() {
                for(float step = 0; step < player.width; step += collisionLayer.getTileWidth())
                        if(isCellBlocked(player.x + step, player.y))
                                return true;
                return false;
        }

The issue I am having is shown below.

I believe the issue is with the cell grid. When a collisionLeft occurs, it is right next to the block (well, 1 pixel off…). The same goes for the bottom collision, it is perfect! Top & Right collision though… not even close. The size of the tiles are 32x32 if that helps.
Any advice would be wonderful! :slight_smile:
Also, I notice that when using tiled and moving, you can see the map being de-rendered(?) slightly on the sides. How would I fix this issue?

Thanks!
-A

Still having this issue. Not to bump, but I cannot find any help online. ):

I would add a sys.out check to see if player.height + player.width are set right (and are not 0 f.e.).
If right and top are not working correctly that might just be because width and height are not giving you the output you think they would.

I will definitely give that a try. I tried printing tilewidth/height, and the player width/height and they were all 32.
Also, any idea why I can see the culling edges on the screen?

no idea. not enough info. need to see some code

oww, maybe i didnt understand what your actual question was.
In the drawing I see there is a blue square that is colliding on the right side with the black wall? And you are wondering why there is white space in between?

Yes. It seems like the collision grid is off.

& For the camera culling… I am simply .render() the tile map. It shows the map de-rendering slightly on all corners.

de de-rendering i don’t understand. I never had that issue when rendering my own tilemaps. so without any code I can’t say much.

The issue of having some white space in between the wall and the player (the blue square) could be due to the fact that you’re updating your player’s movement every frame by factoring in delta time. f.e.:

movementspeed =…
player.x += movementspeed * (float) delta

because delta varies every frame your players movement is not updated with the same amount every frame and it could be that when the player comes to the point like you show in the picture where movementspeed * delta > width of the white space and when you perform the collisioncheck it keeps returning true and thus the player cant cover the white space anymore.

@Norakomi but then it should also happen for the other sides, but as much as i understoo, it only happens for left and top, so i guess there is something worng with the width and heigth.

Try to print the following things, if the

if(isCellBlocked(player.x + player.width , player.y + step))

condition inside the

public boolean collidesRight()

is true:

player.x
player.width
player.y
player.heigth

as well as:

(int) (xx/collisionLayer.getTileWidth())

and

(int) (yy/collisionLayer.getTileHeight())

inside the

isCellBlocked(int xx, int yy)

method.

Maybe that showes us the problem.

When suggesting lengthy debugging like this, use break points.

It is much easier.

@Gibbo3771 you are right, he could also use breakpoints or conditional break points. They help a lot.

But if that does not solve his problem he should show use the values of the variables in the “wrong collision case”

I made a video about detecting simple AABB collisions:
Game Development Math - Collision Detection (AABB): http://youtu.be/wlYkneC3iFA

I definitely understand this. The process or method anyways. I guess now I have realized that the problem I am actually having is finding the tiles and which ones are blocked. I would like to do so using the map[col][row] method.

• Set Array[col][row] for tiles in tile map.
• Find all with property “blocked”.
• If player collides with tile that is “blocked”, check side and move back one tile in opposite direction.

im not sure if you want to do that.
I think you are on the right track and almost there, you just need to find you’re bug.

There is no need to create an array to put all your tiles in.
LibGDX’s TiledMapTileLayer & Cell classes allow you to get the tiles you need for your collision detection and check their value just as you did now in your isCellBlocked().
And besides that your collision check method is working… just not as you would want it to.
But that might just have to do with something suggested earlier in the post and not with the way you get your tiles.

Also:

You dont want to be looking for every tile with property blocked, every frame update.
Just checking in the player’s vicinity like you are doing right now makes sure that your program is not continuesly looping over all the tiles you don’t need to check anyway.

I see you have been struggling with this for a while already. If you want you can put all your game’s code (game & player class would suffice i guess) in a pastebin and I can have a look at it and see if I can find the issue.

Haha, I am definitely not updating the tile grabbing every frame, no worries. The only reason I mention the array is because I have been talking to the developer of Gun Slugs 2 and he recommended it. His collision is what I am trying to achieve. He provided this example: http://pastebin.com/FiQBGTkw - That is for the actual collision reaction… but he didn’t really explain much of the array[col][row] for the tiles.

I don’t know. I am pretty stumped on the route to take. I know that tiled collision is apparently not talked about on the internet. At all. -_-