free_ice_cream
free_ice_cream

Reputation: 85

Collision not working

I have a problem in my game's collision. The game is "tiled based". The problem comes in when I place a tile that is supposed to block the player from walking into it, it produces a infinite vertical and horizontal barrier adjacent to the tile.

Here is my code:

double d = delta;
Point oldVerticalPoint = new Point(point.getX(), point.getY());
if (Keyboard.isKeyDown(Keyboard.KEY_UP))
    point.setY((float) (point.getY() - (speed * d)));
if (Keyboard.isKeyDown(Keyboard.KEY_DOWN))
    point.setY((float) (point.getY() + (speed * d)));
for (int x = 1; x <= game.map.width; x++) {
    for (int y = 1; y <= game.map.height; y++) {
        Block block = game.map.getBlock(x, y);
        if (block instanceof BlockWall) {
            BlockWall b = (BlockWall) block;
            Point blockPoint = Block.getStandardBlockCoords(x, y);
            Point playerPoint = game.thePlayer.point;
            if (playerPoint.getY() > blockPoint.getY() && playerPoint.getY() < <blockPoint.getY() + Block.LENGTH)
                game.thePlayer.point = oldVerticalPoint;
        }
    }
}

Point oldHorizontalPoint = new Point(point.getX(), point.getY());;
if (Keyboard.isKeyDown(Keyboard.KEY_LEFT))
    point.setX((float) (point.getX() - (speed * d)));
if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT))
    point.setX((float) (point.getX() + (speed * d)));
for (int x = 1; x <= game.map.width; x++) {
    for (int y = 1; y <= game.map.height; y++) {
        Block block = game.map.getBlock(x, y);
        if (block instanceof BlockWall) {
            BlockWall b = (BlockWall) block;
            Point blockPoint = Block.getStandardBlockCoords(x, y);
            Point playerPoint = game.thePlayer.point;
            if (playerPoint.getX() > blockPoint.getX() && playerPoint.getX() < blockPoint.getX() + Block.LENGTH)
                game.thePlayer.point = oldHorizontalPoint;
        }
    }
}

I don't know why it does this. One thing to be clear about is the reason why there is two collision detections is so if the player is moving horizontally into a wall and is moving vertically on something such as a simple tile the player wont stop moving completely. Also getStandardBlockCoords is used because for example if a block x and y is x = 1 and y = 2 that means the block is in the second column and the first row. The method simply converts this to the pixel coordinates.

I'm using LWJGL and Slick2D. Help?

Upvotes: 0

Views: 100

Answers (1)

Twister1002
Twister1002

Reputation: 557

You should use the methods Slick has in order to detect collision. That would be a very good idea.

if (block instanceof BlockWall)
   {
        BlockWall b = (BlockWall)block;
        Point blockPoint= Block.getStandardBlockCoords(x, y);
        Point playerPoint = game.thePlayer.point;
            //You should use playerPoint.contains(blockPoint){ };
        if (playerPoint.getY() > blockPoint.getY() && playerPoint.getY() <
            blockPoint.getY() + Block.LENGTH)
            game.thePlayer.point = oldVerticalPoint; 
        }
    }
}

Now an issue with your code and my code.

Your Code: Your code will pull 2 points, an X and Y location. It will ONLY validate THOSE POINTS. So say if your block is 30 x 50 at x = 250 and y = 300. Now, you want to be able to check for the WHOLE block. However it is only check against the X and Y locations.

if(playerPoint.contains(blockPoint)){
    //if only playerPoint x = 250 and y = 300 will this ever be true
}

So if you want to check for x = 260 and y = 330, the if statement would return false since the points do not contain each other. Now, if you used a Rectangle, Polygon, etc shape, it would have a size parameter that would be created when the object is created. Which means the contains would check the WHOLE shape. so if the point is 260 and 330, the if statement would come out true, because the player CONTAINS the blockPoint.

If that doesn't make sense let me know and I'll explain it further if I need to.

Also your code is very confusing. Please indent! It makes the world of difference.

Upvotes: 1

Related Questions