5

我正在使用图块制作 Java 游戏。我遇到了碰撞元素的问题。我正在为地图上的每个图块定义矩形,并为玩家定义另一个矩形。我遇到的麻烦是知道玩家在击中矩形时来自哪一侧,然后将玩家推向玩家来自的方向。我已经制作了一个方法来检查字符在矩形内有多少,因此它可以知道将其推出多少,但我不知道如何判断字符来自哪一侧。

这是我当前的碰撞方法 - 注意 rect1 是角色, rect2 是瓷砖

public void collision(Rectangle rect1, Rectangle rect2) {
    float xAdd;
    float xAdd2;
    float yAdd;
    float yAdd2;

    boolean hitRight = false;
    boolean hitLeft = false;
    boolean hitTop = false;
    boolean hitBot = false;

    Vector2f rect1Origin = new Vector2f(rect1.x, rect1.y);
    Vector2f rect2Origin = new Vector2f(rect2.x, rect2.y);
    Vector2f rect1Mid = new Vector2f((rect1.x + rect1.width) / 2,(rect1.y + rect1.height) / 2);
    Vector2f rect2Mid = new Vector2f((rect2.x + rect2.width) / 2,(rect2.y + rect2.height) / 2);

    Vector2f rect1A = new Vector2f(rect1Origin.x + rect1.width, rect1.y);
    Vector2f rect1B = new Vector2f(rect1Origin.x, rect1Origin.y+ rect1.height);
    Vector2f rect1C = new Vector2f(rect1Origin.x + rect1.width,rect1Origin.y + rect1.height);

    Vector2f rect2A = new Vector2f(rect2Origin.x + rect2.width, rect2.y);
    Vector2f rect2B = new Vector2f(rect2Origin.x, rect2Origin.y
            + rect2.height);
    Vector2f rect2C = new Vector2f(rect2Origin.x + rect2.width,
            rect2Origin.y + rect2.height);

    xAdd = rect2C.x - rect1B.x;
    xAdd2 = rect1C.x - rect2B.x;

    yAdd = rect2A.y - rect1B.y;
    yAdd2 = rect2C.y - rect1A.y;


    if (rect1Mid.y < rect2Mid.y) {
        if (rect1.intersects(rect2)) {
            y_pos += yAdd;
        }
    }
    if (rect1Mid.y > rect2Mid.y) {
        if (rect1.intersects(rect2)) {
            System.out.println(yAdd2);
            y_pos += yAdd2;
        }

    }
    if(rect1Mid.x > rect2Mid.x){
        if(rect1.intersects(rect2)){
            hitRight = true; x_pos += xAdd;
        }
    } 
    if(rect1Mid.x< rect2Mid.x){ 
          if(rect1.intersects(rect2)) {
              x_pos += -xAdd2;
          } 
    }
}

任何帮助将不胜感激

谢谢

4

4 回答 4

2

为你的角色保留两个位置——它在哪里(最后一帧、移动等),以及你想要移动它的位置。然后仅在未检测到碰撞时才移动它 - 如果您不允许损坏状态,则不必修复它。

编辑:方法collision应该是boolean- 它应该在实际移动角色之前完成,比如

if (!collision(character, tile))
{
    doMove(character);
}
else
{
    //custom handling if required
}

Edit2:前面的只适用于一小步,如果你需要部分移动,你真的需要知道角色的原始位置,比如move(originalPosition, desiredPosition, tile),你可以从originalPosition和推断出方向tile

要点是,在您获得有效位置之前,您实际上并没有移动角色。

于 2012-04-29T15:27:21.403 回答
1

首先,任何Sprite(这里的角色和图块)都应该有四个成员:xPos、yPos、xVec、yVec。考虑到这一点,您就知道角色在哪里以及下一帧将在哪里。

sprite.xPos += sprite.xVec;
sprite.yPos += sprite.yVec;

还要看看你的代码,你应该改进命题(例如,你检查四个if语句中的if(rect1.intersects(rect2)) )。相反,只检查一次intersects(),并在内部检查每种可能的情况(左、右、上、下命中)。

最后,collision 方法应该接收 2 个 sprite 对象(例如sprite1sprite2),然后检查intersects()方法,同时考虑 sprite 及其向量的原始位置。如果精灵在新位置相交,则相应地反转对象的矢量。

于 2012-04-29T16:20:08.030 回答
1

假设您可以检测到与代码的冲突。这是一种确定角色相对于矩形对象的位置的方法。

  1. 找到矩形的中点。(Rx,Ry)。

  2. 找到角色精灵的中点。(Sx,Sy)。

  3. 现在您可以比较 Rx,Ry , Sx , Sy 以确定 Rx 和 Ry 的 Sx 和 Sy 在哪一侧

  4. 例如:

    if Sx < Rx then
      Sx = left side of Rx
    
于 2012-05-03T09:42:55.783 回答
1

我自己也在为此苦苦挣扎,但经过一番思考,可以通过将所有实体都放在一个列表中来完成这项检查,在我的代码中,我不得不阻止玩家通过块移动。所有块都在一个列表中。现在,在检查玩家位置时,我创建一个新的 Rectangle 并将玩家位置向前一帧放在下一帧将发生更新的方向上,如果矩形与该方向的更新不会发生相交。这是我的代码:

    if (isKeyPressed(KeyEvent.VK_LEFT)) {
        if(!collisionWithBlocks(1)){
            pl.x = pl.x - updatePlayerPosition;
        }
    }
    if (isKeyPressed(KeyEvent.VK_RIGHT)) {
        if(!collisionWithBlocks(0)){
            pl.x = pl.x + updatePlayerPosition;
        }
    }
    if (isKeyPressed(KeyEvent.VK_UP)) {
        if(!collisionWithBlocks(3)){
            pl.y = pl.y - updatePlayerPosition;
        }
    }
    if (isKeyPressed(KeyEvent.VK_DOWN)) {
        if(!collisionWithBlocks(2)){
            pl.y = pl.y + updatePlayerPosition;
        }
    }

碰撞块():

public boolean collisionWithBlocks(int side){
    for(Block b : main.blocks){
        Rectangle block = b.getBounds();
        Rectangle player = null;
        if(side == 0){
            player = new Rectangle(pl.x + updatePlayerPosition, pl.y, pl.getWidth(), pl.getHeight());
        } else if(side == 1){
            player = new Rectangle(pl.x - updatePlayerPosition, pl.y, pl.getWidth(), pl.getHeight());
        } else if(side == 2){
            player = new Rectangle(pl.x, pl.y + updatePlayerPosition, pl.getWidth(), pl.getHeight());
        } else if(side == 3){
            player = new Rectangle(pl.x, pl.y - updatePlayerPosition, pl.getWidth(), pl.getHeight());
        }

        if(player.intersects(block)){
            return true;
        }
    }

    return false;
}

updatePlayerPosition 是 2,并且我的代码发生了变化,但这已经足够了。在您的情况下,我建议将实体放在列表中,然后像我一样检查。

于 2013-08-12T13:08:26.230 回答