LibGDX - 基本物理错误?无法检测到玩家与平台的一侧(平铺)之间的碰撞

时间:2014-12-02 02:25:24

标签: android libgdx 2d collision detection

无法在线找到任何有用的教程,所以我尝试了自由式算法来解决这个问题。

基本上,我已经将播放器(主角)和平台播放器可能会发生碰撞的磁贴)建模为矩形为了碰撞检测。在我的代码中,playerRect是表示播放器的矩形,getRect()是表示平台的矩形。

现在我的播放器只能从左向右移动,因此他只能与平台的3个侧面(左侧,上侧和下侧)发生碰撞。我设法检测到平台顶部和底部的碰撞,但由于某种原因,我无法检测到左侧的碰撞。

如果有人可以查看我的代码并可能找到我出错的地方,我们将非常感激。

public class Platform extends GameObject {
...
public void update() {
    // Rectangle of player
    playerRect = player.getRect();
    //A boolean that checks whether or not player's rectangle 
    //and platform's rectangle overlap
    isOverlapping = playerRect.overlaps(getRect());

    if (isOverlapping) {

        // If the player hits the bottom part of the platform, 
        // have gravity pull him down
        if ((playerRect.getX() > getRect().getX() && 
             playerRect.getX() < getRect().getX() +   
             getRect().getWidth()) && 
             playerRect.getY() > getRect().getY()); {

                player.setySpeed(player.getGravity());
                // If the player is on a platform or on the ground, 
                // allow him to jump
                if (playerOnPlatform() || player.onGround()) {
                    player.setAllow("true", "jump");
                } else {
                    player.setAllow("false", "jump");
                }
            }

        // If the player is on the top part of the platform, 
        // set his ySpeed to 0 so he remains on the platform rather 
        // than fall due to gravity
        if ((playerRect.getX() > getRect().getX() && 
             playerRect.getX() < getRect().getX() + 
             getRect().getWidth()) && 
             playerRect.getY() < getRect().getY()) {

            player.setySpeed(0);
            // If the player is on a platform or on the ground, allow him to jump
            if (playerOnPlatform() || player.onGround()) {
                player.setAllow("true", "jump");
            } else {
                player.setAllow("false", "jump");
            }

           // If the player hits the side of the platform, 
           // set his xSpeed to 0 so that he does not go through the platform
            if(playerRect.getX() > getRect().getX() && 
              (playerRect.getY() > getRect().getY() && 
               playerRect.getY() < getRect().getY() + 
               getRect().getHeight())) {
                player.setySpeed(20);
            }
        }
...
}

提前感谢任何输入

1 个答案:

答案 0 :(得分:0)

如果您的代码在评估时是否有错误,为了获得所需的结果,或者只是您说它为0并且您编写了设置为20 player.setySpeed(20);。当时可能还不太了解:)但不应该是setx。 ..(0),没有setY

// If the player hits the side of the platform, 
           // set his xSpeed to 0 so that he does not go through the platform
            if(playerRect.getX() > getRect().getX() && 
              (playerRect.getY() > getRect().getY() && 
               playerRect.getY() < getRect().getY() + 
               getRect().getHeight())) {
                player.setySpeed(20);
            }

。 另一方面,如果这是正确的,内部,而不是外部可以 我没有测试它,你看上面

// If the player is on the top part of the platform, 
        // set his ySpeed to 0 so he remains on the platform rather 
        // than fall due to gravity
        if ((playerRect.getX() > getRect().getX() && 
             playerRect.getX() < getRect().getX() + 
             getRect().getWidth()) && 
             playerRect.getY() < getRect().getY()) {

            player.setySpeed(0);
            // If the player is on a platform or on the ground, allow him to jump
            if (playerOnPlatform() || player.onGround()) {
                player.setAllow("true", "jump");
            } else {
                player.setAllow("false", "jump");
            }

           // If the player hits the side of the platform, 
           // set his xSpeed to 0 so that he does not go through the platform
            if(playerRect.getX() > getRect().getX() && 
              (playerRect.getY() > getRect().getY() && 
               playerRect.getY() < getRect().getY() + 
               getRect().getHeight())) {
                player.setySpeed(20);
            }
        }

您可以使用日志,如果有时满足此条件

   // If the player hits the side of the platform, 
           // set his xSpeed to 0 so that he does not go through the platform
            if(playerRect.getX() > getRect().getX() && 
              (playerRect.getY() > getRect().getY() && 
               playerRect.getY() < getRect().getY() + 
               getRect().getHeight())) {
                player.setySpeed(20);

Gdx.app.log("inside", "test");

            }