碰撞处理程序在libgdx中检测得太早

时间:2015-09-01 01:17:43

标签: java libgdx collision

我创建了一个2d布尔数组来检测我的碰撞。我的hitbox的每个角落都有四个探测器供玩家使用,碰撞只是说如果左边的点是真的你不能向左移动,反之亦然。右侧完美无瑕地工作,但左侧在他实际到达阻止他的平台之前阻止了我的玩家。这是一张显示正在发生的事情的图片(地面上还有另外两个命中箱是红色的)

Collision example

这是碰撞处理程序的代码

    public void run() {
    /*If entity can fall i.e not on a platform*/
    if(entity instanceof CollidableObject){
        /*Each line is stating that if the current position.x + 1 is true and the y coordinate is true (in grid of course) then
         * there must be something there and so stop falling
         */
        if(/*mapGrid.getMapGrid()[(int) (((CollidableObject) entity).getGridPositionLeft().x)][(int) (((CollidableObject) entity).getGridPositionLeft().y - 1)] || */
           mapGrid.getMapGrid()[(int) (((CollidableObject) entity).getGridPositionLeft().x + 1)][(int) (((CollidableObject) entity).getGridPositionLeft().y)] || 
           mapGrid.getMapGrid()[(int) (((CollidableObject) entity).getGridPositionLeft().x + 1)][(int) (((CollidableObject) entity).getGridPositionLeft().y)]/* || 
           mapGrid.getMapGrid()[(int) (((CollidableObject) entity).getGridPositionRight().x + 3)][(int) (((CollidableObject) entity).getGridPositionLeft().y )]*/){
                    entity.canFall(false);
                    entity.canJump(true);
        }else{
            entity.canFall(true);
            entity.canJump(false);
            }

    }

    if(entity instanceof CollidableObject){

        /*Each line is stating that if the current position.y + 1 is true and the x coordinate is true (in grid of course) then
         * there must be something there and so stop moving right
         */
        if(mapGrid.getMapGrid()[(int) (((CollidableObject) entity).getGridPositionLeft().x + (int) (((CollidableObject) entity).getDimensions().x / MapGrid.TILE_SIZE))]
                   [(int) (((CollidableObject) entity).getGridPositionLeft().y + 1)] || 
           mapGrid.getMapGrid()[(int) (((CollidableObject) entity).getGridPositionLeft().x + (int) (((CollidableObject) entity).getDimensions().x / MapGrid.TILE_SIZE))]
                   [(int) (((CollidableObject) entity).getGridPositionLeft().y + 2)] || 
                   mapGrid.getMapGrid()[(int) (((CollidableObject) entity).getGridPositionLeft().x + (int) (((CollidableObject) entity).getDimensions().x / MapGrid.TILE_SIZE))]
                           [(int) (((CollidableObject) entity).getGridPositionLeft().y + 3)]|| 
                           mapGrid.getMapGrid()[(int) (((CollidableObject) entity).getGridPositionLeft().x + (int) (((CollidableObject) entity).getDimensions().x / MapGrid.TILE_SIZE))]
                                   [(int) (((CollidableObject) entity).getGridPositionLeft().y + 4)]){
                    entity.canMoveRight(false);
        }else
            entity.canMoveRight(true);

        if(entity instanceof CollidableObject){
            /*Each line is stating that if the current position.x is true and the y coordinate is true (or +1 , +2 because
             * of the height of the player) then
             * there must be something there and so stop moving left
             */
            if(mapGrid.getMapGrid()[(int) (((CollidableObject) entity).getGridPositionLeft().x)]
                       [(int) (((CollidableObject) entity).getGridPositionRight().y + 1)] || 
                       mapGrid.getMapGrid()[(int) (((CollidableObject) entity).getGridPositionLeft().x)]
                       [(int) (((CollidableObject) entity).getGridPositionLeft().y + 2)] || 
                       mapGrid.getMapGrid()[(int) (((CollidableObject) entity).getGridPositionLeft().x)]
                       [(int) (((CollidableObject) entity).getGridPositionLeft().y + 3)]){
                        entity.canMoveLeft(false);
            }else
                entity.canMoveLeft(true);
        }
    }

整体问题是玩家不能让它离开,因为它太快检查了,如果它不能很快检查玩家在平台中途结束那么我做错了什么? !非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

答案是在检查左侧碰撞时从右下角而不是左下角进行检查,因为左移动更新速度更快,然后向右移动。这是因为要在向右移动时更新网格位置,您必须在网格位置更新之前一直通过单元格(当它击中单元格的右上方时更新)=。知道这一点,如果向左移动,您将立即点击单元格的右上部分并立即更新。通过从右侧更新,更新一个单元将更新,这正是获得解决方案所需的时间。