java - Java 游戏中的 2D 碰撞问题

标签 java collision-detection

几周来我一直在与 LWJGL 合作开发这款游戏。自从我添加了跳跃的能力,向上的碰撞就给我带来了很多问题。

该游戏是一款基于 2D 图 block 的横向卷轴游戏。总的来说,碰撞几乎是完美的,除了玩家跳跃的时候。起初我想“哦,也许我只需要改变跳跃机制”,但后来我意识到它只发生在玩家经过某个 x 坐标时。

现在,对于实际问题本身:如果玩家在通过某个 x 坐标时跳跃,他们将穿过图 block 并且顶部碰撞测试返回 false。

这是整个 Player 类:

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;
    }
}

}

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

编辑:我不知道为什么,但是当我的角色靠近 map 的 0 x 坐标时,角色的 y 坐标增加得非常缓慢。这可能与我遇到的问题有关。我正在调查它,我怀疑当我将玩家的 x 和 y 值从 double 转换为整数以用于顶部、底部、左侧和右侧矩形时,它可能与此有关。

再次编辑: 我不知道这是否重要,但我一直在像这样检查碰撞:(这是在“引导”类中。)

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);
            }
        }
}

最佳答案

为什么要将玩家传递给 checkCollision?似乎您不应该传入玩家,而是使用调用 checkCollision 方法的玩家成员。我认为这可能会给您带来一些困惑。如:

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

这看起来像是您试图将玩家推到方 block 下方 b/c 他们在跳跃过程中与方 block 相交。既然如此,那就应该了

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

我会从函数参数中删除播放器并重写函数,看看会得到什么。希望对您有所帮助。

编辑

您的评论表明您不能再将播放器传递给该函数。不确定您的确切实现方式,但这是我通常看到的游戏的样子:

 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);
     }
 }

关于java - Java 游戏中的 2D 碰撞问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10558879/

相关文章:

javascript - 线接触或矩形或圆形

java - 使用 Java EE API 替换已弃用的 JPMS 模块

c++ - For 循环中的 Switch 语句无法检查特定值的变量

java - Typemap javaimports不适用于内部C++类

Java Hibernate 注解 @JoinTable 关系

c++ - 使用分离轴定理的碰撞检测

javascript - 球与球弹性碰撞 : ball bounces in an unequal proportion

swift - spritekit碰撞检测不一致(无法获取两个节点的位置)

java - 如何检查 JFrame 是否存在

java - 收到 sql 错误 1265 且不确定原因