即使在碰撞之后,LibGDX矩形的y位置也会更新

即使在碰撞之后,LibGDX矩形的y位置也会更新,libgdx,collision,overlap,rectangles,Libgdx,Collision,Overlap,Rectangles,我正在使用2D platformer,我想制作从天上掉下来的盒子。当箱子和地面发生碰撞时,箱子应停止下落。下面是我在Box类中的碰撞检测和更新代码 这是我在主类中的代码 但问题是,当箱子和地面相撞时,我的箱子还是会掉下来,只是速度很小。为什么我会得到这个奇怪的结果?我知道我可以删除hits=false;在我的hits2方法中,当长方形和地面碰撞时,我的盒子不会掉下来,但为什么长方形之间的碰撞如此奇怪呢? 更新: 这里是它的外观您是否通过绘制来检查地面的碰撞矩形?并绘制长方体的碰撞并检查碰撞点。因

我正在使用2D platformer,我想制作从天上掉下来的盒子。当箱子和地面发生碰撞时,箱子应停止下落。下面是我在Box类中的碰撞检测和更新代码

这是我在主类中的代码

但问题是,当箱子和地面相撞时,我的箱子还是会掉下来,只是速度很小。为什么我会得到这个奇怪的结果?我知道我可以删除hits=false;在我的hits2方法中,当长方形和地面碰撞时,我的盒子不会掉下来,但为什么长方形之间的碰撞如此奇怪呢? 更新:
这里是它的外观

您是否通过绘制来检查地面的碰撞矩形?并绘制长方体的碰撞并检查碰撞点。因为我在我的一个游戏中就这样做了,我在底部画了一个用于碰撞的地面矩形,所以当演员离开屏幕时,就这样想它碰撞


libgdx中的碰撞与重叠方法完美结合

谢谢,我都查过了。此外,我还更新了我的帖子,并添加了关于我的问题的短片
public Box(int x, int y){

        hitBox = new Rectangle(x, y, 64, 64);
        bottom = new Rectangle(x + 5, y, 54, 14);
        top = new Rectangle(x + 5, y + 5 + 50, 54, 14);
        // other code
}
@Override
public void update(float delta) {
    if(!hits){
        velocity -= (1 * delta);
        System.out.println("velc " + velocity);
        hitBox.y += velocity;
        top.y += velocity;
        bottom.y += velocity;
        sprite.setPosition(hitBox.x, hitBox.y);
    }
}

@Override
public int hits2(Rectangle r) {
    if(bottom.overlaps(r)){
        velocity = 0;
        hits = true;
        System.out.println("hit");
        return 1;
    }
    System.out.println("n");
    hits = false;
    return 0;
}
for(int i=0; i<list.size(); i++){
            Box tmpBox = (Box) (list.get(i) instanceof Box ? list.get(i) : null);
            for(int j=0; j<list.size(); j++){
                if(tmpBox != null){
                    Brick brick = (Brick) (list.get(j) instanceof Brick ? list.get(j) : null);
                    Box tmpOtherBox = (Box) (list.get(j) instanceof Box ? list.get(j) : null);
                    if(brick != null){ // if collision with ground rectangle
                        tmpBox.hits2(brick.getTop());
                    }
                    if(tmpOtherBox != null && tmpOtherBox.hashCode() != tmpBox.hashCode()){ if collision with other box rectangle
                        tmpBox.hits2(tmpOtherBox.getHitBox());
                    }
                }
            }
            if(tmpBox != null){
                tmpBox.update(Gdx.graphics.getDeltaTime());
            }
}