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'm working on a pacman styled dungeon crawler, using the free oryx sprites. I've created the map using Tiled, separating the floor, walls and treasure in three different layers. After importing the map in libGDX, it renders fine. I also added the player character, for now it just moves into one direction, the player cannot control it yet. I wanted to add collision and I was planning to do this by checking if the player's new position is on a wall tile. Therefore as you can see in the following code snippet, I get the tile type of the appropriate tile and if it is not zero (since on that layer there is nothing except the wall tile) it is a collision and the player cannot move further:

final Vector2 newPos = charController.move(warrior.getX(), warrior.getY());
if(!collided(newPos)) {
    warrior.setPosition(newPos.x, newPos.y);
    warrior.flip(charController.flipX(), charController.flipY());
}

[..]

private boolean collided(Vector2 newPos) {
    int row = (int) Math.floor((newPos.x / 32));
    int col = (int) Math.floor((newPos.y / 32));

    int tileType = tiledMap.layers.get(1).tiles[row][col];
    if (tileType == 0) {
        return false;
    }

    return true;
}

The character only moves one tile with this code:

Only one tile

If I reduce the col value by two it two more tiles. I think the problem will be around indexing, but I'm totally confused because the zero in the coordinate system of libGDX is in the bottom left corner of the screen, and I don't know the tiles array's indexing is similair or not. The size of the map is 19x21 tiles and looks like the following (the starting position of the player is marked with blue:

The whole map

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

This looks suspect,

int row = (int) Math.floor((newPos.x / 32));
int col = (int) Math.floor((newPos.y / 32));

row would make more sense if you used newPos.y and not newPos.x, so try swapping them round like I have done below

int col = (int) Math.floor((newPos.x / 32));
int row = (int) Math.floor((newPos.y / 32));

Edit: Try the following, flip the y by map height which you say is 21, given it starts at 0 and not 1, then its 20-row and not 21-row

int tileType = tiledMap.layers.get(1).tiles[20-row][col];

If that doesn't work then you need to do a for next loop for all rows and cols and print the map value out on screen. Once you see your patterns of 0 and 1 on screen you will soon see where it is wrong.

share|improve this answer
    
Yeah, make more room to move around in so you get the feeling if you need to say flip and or rotate the map to get it to work correctly. –  Valmond Nov 22 '12 at 15:44
    
Yes it makes more sense the way you suggested but it still does not work properly. –  nosferat Nov 23 '12 at 8:20
    
Added some more information. –  John Nov 23 '12 at 11:52
    
These were exactly my problems, thank you! –  nosferat Dec 5 '12 at 19:53
add comment

If your character is only moving one block, then it looks like they are actually moving to the right instead of the left, as there is only one block to move to on the right. Make sure all your coordinates are the same.

The fact that you do warrior.flip(charController.flipX(), charController.flipY()); makes seems to confirm that. Try flipping it before doing the collision check and see if that helps.

EDIT: Coincidently I am also making a game based on pacman, so I will give you a hint, if you are moving your character on a pixel basis, you are going to need to add some extra code to your collision check so they he isn't halfway in a wall. This will depend on the direction your character is moving, since you are just doing right now, I will use that. You want to make sure that the left side of the sprite, not necessarily the characters position, isn't impacting the wall.

EDIT: Did you try flipping the newPos before calling collided?

i.e.

final Vector2 newPos = charController.move(warrior.getX(), warrior.getY());  
flip(newPos);  
if(!collided(newPos)) {  
    warrior.setPosition(newPos.x, newPos.y);  
    warrior.flip(charController.flipX(), charController.flipY());  
}
share|improve this answer
    
Commented out the flip line, still no proper movement, but what you wrote makes sense I will go on this route. Also I'm moving the character on a pixel basis. –  nosferat Nov 23 '12 at 8:22
add comment

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.