简体   繁体   中英

2D Collision issues in Java game

I've been working on this game with LWJGL for a few weeks now. Ever since I added the ability to jump, the upward collision has been giving me a lot of problems.

The game is a 2D tile-based sidescroller. Overall, the collision is almost perfect except for when the player jumps. At first I thought "Oh, maybe I just need to change the jumping mechanics," but then I realized that it only happens when the player is passed a certain x coordinate.

Now, for the actual problem itself: if the player jumps when passed a certain x coordinate, they will pass through the tile and testing for top collision returns false.

This is the entire Player class:

package Minecraft2D;

import static Minecraft2D.World.BLOCK_SIZE;
import Minecraft2D.Tools.Tools;
import Minecraft2D.UI.Inventory;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import static Minecraft2D.Boot.*;

import org.lwjgl.util.Rectangle;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;

public class Player {

private float x;
private float y;
public int width = 32;
public int height = 50;
private float DX = 0;
private float DY = 0;
private Texture left = null;
private Texture right = null;
Texture texture = null;

public boolean direction[] = { false, false, false, false };
public boolean collision = false;
public boolean ground = false;
public boolean jump = false;
public boolean top = false;

public Player(float x, float y) {
    this.x = x;
    this.y = y;
    try {

        this.left = TextureLoader.getTexture("PNG", new FileInputStream(new File(path + "player_left.png")));
        this.right = TextureLoader.getTexture("PNG", new FileInputStream(new File(path + "player_right.png")));
        this.texture = this.right;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void draw() {
    Tools.drawTexture((int)x, (int)y, width, height, texture);
}

public void checkCollision(Player player, Block block) {

    if (player.getY() < block.getY() + BLOCK_SIZE && player.getX() < block.getX() + BLOCK_SIZE && player.getY() + this.height > block.getY() && player.getX() + this.width > block.getX() && block.getType() != BlockType.AIR) {
        Rectangle top = new Rectangle();
        top.setBounds((int) player.x + 4, (int) player.y + 1, this.width - 8, 1);
        Rectangle bottom = new Rectangle();
        bottom.setBounds((int) player.x + 4, (int) player.y + this.height, this.width - 8, 1);
        Rectangle left = new Rectangle();
        left.setBounds((int) player.x, (int) player.y + 1, 1, this.height - 5);
        Rectangle right = new Rectangle();
        right.setBounds((int) player.x + player.width, (int) player.y + 1, 1, this.height - 5);
        Rectangle blok = new Rectangle();
        blok.setBounds((int) block.getX(), (int) block.getY(), BLOCK_SIZE, BLOCK_SIZE);
        if (bottom.intersects(blok)) {
            player.setY((block.getY() - this.height - 1));
            ground = true;
            jump = false;
        } else if (top.intersects(blok)) {
            DY = 0;
            this.top = true;
            y -= (player.y) - (block.getY() + BLOCK_SIZE);
        }
        if (!top.intersects(blok)) {
            if (left.intersects(blok)) {
                player.setX(block.getX() + this.width);
            } else if (right.intersects(blok)) {
                player.setX(block.getX() - this.width);
            }
        }

    } else {
        collision = false;
        ground = false;
    }


    if (!collision && !jump) {
        setDY(.003f);
    }
    if (ground && !jump) {
        DY = 0;
    }

    if (jump && DY < 0.003f) {
        DY += 0.0001;
    } else {
        // jump = false;
    }

    if (top) {
        DY = 0f;
        top = false;
    }

    x += DX;
    y += DY;

    if (x > Boot.SCREEN_WIDTH) {
        x = 0;
    }
    if (x < 0) {
        x = Boot.SCREEN_WIDTH;
    }
}

public float getX() {
    return x;
}

public void setX(float x) {
    this.x = x;
}

public float getY() {
    return y;
}

public void setY(float y) {
    this.y = y;
}

public void setDX(float dx) {
    this.DX = dx;
}

public void setDY(float dy) {
    this.DY = dy;
}

public void setJump() {
    if (!jump) {
        jump = true;
        ground = false;
        DY = -0.13f;
        y -= 1;
    }
}

public void setTexture(int tex) {
    if (tex == 0) {
        this.texture = this.left;
    }
    if (tex == 1) {
        this.texture = this.right;
    }
}

}

==============

EDIT: I have no clue why, but as my character moves closer to the map's 0 x-coordinate, the character's y coordinate increases very slowly. This probably has something to do with the problem I have been getting. I am looking into it and I have a suspicion that it may have something to do when I cast the player's x and y values from doubles to integers for use in the top, bottom, left, and right Rectangles.

EDIT again: I don't know if this matters, but I have been checking collision like this: (This is in the "Boot" class.)

private void checkCollision() {
            for (int x = 0; x < BLOCKS_WIDTH - 1; x++) {
              for (int y = 0; y < BLOCKS_HEIGHT - 1; y++) {
                Block blk = grid.getAt(x, y);
                player.checkCollision(blk);
            }
        }
}

Why are you passing a player into checkCollision? It seems like you should not be passing in a player, but instead using the members of the player that's calling the checkCollision method. I think that may be leading to some confusion for you. Such as:

y -= (player.y) - (block.getY() + BLOCK_SIZE);

This looks like you are trying to push the player below the block b/c they intersected it during their jump. If that's the case, it should just be

y = (block.getY() + BLOCK_SIZE);

I would remove the player from the function argument and rewrite the function and see what you get. Hope that helps.

EDIT

Your comment states that you can no longer pass the player into the function. Not sure about your exact implementation, but here's what games that I have typically seen look like:

 public class Player 
 {
    private int x, y, dx, dy;

    public void checkCollision(Block block)
    {
       if (isTopCollision(block))
         fall(block.getY() + block.getHeight());
    }

    private boolean isTopCollision(Block block)
    {
       return y > block.getY() + block.getSize() && y < block.getY();
    }

    private void fall(int adjustedY)
    {
       y = adjustedY;
       top = true;
       dy = 0;
       // etc
    }
 }

 public class MyGame 
 {
     public void gameloop()
     {
       for (Block b : blocks)
         player.checkCollision(b);
     }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM