检测到碰撞时停止玩家运动

时间:2019-04-07 16:15:46

标签: java opengl game-engine lwjgl aabb

我设法将Axis-Aligned-Bounding-Box碰撞检测编码到我的lwjgl2 3d游戏中,但是我不确定如何防止玩家进入击中区。在主类中检测到碰撞时,它将在播放器类中将布尔值设置为true。

我尝试使用lastDx float求出运动因子并计算方向。

// HumanEntity运动类

boolean collision = false;

    public void move(){
        if(collision){
            //player inside the hitbox
        }else{
            Vector4f movement = new Vector4f();
            checkInputs();
            float xDistance = currentSpeed * MainGameHandler.getFrameTimeSeconds();
            float zDistance = currentTurnSpeed * MainGameHandler.getFrameTimeSeconds();
            float dx = xDistance;
            float dz = zDistance;
            movement.x += dx;
            movement.z += dz;
            Matrix4f invertedViewMatrix = new Matrix4f();
            Matrix4f.invert(viewMatrix,invertedViewMatrix);
            Matrix4f.transform(invertedViewMatrix,movement,movement);
            dx = movement.x;
            dz = movement.z;
            lastDx = dx;
            lastDz = dz;
            increasePosition(new Vector3f(dx, 0, dz));
            float terrainHeight = MainGameHandler.terrain.getHeightOfTerrainForPlayer(position.x, position.z);
            upwardsSpeed += GRAVITY * MainGameHandler.getFrameTimeSeconds();
            increasePosition(new Vector3f(0,upwardsSpeed * MainGameHandler.getFrameTimeSeconds(),0));
            if(position.y<terrainHeight){
                upwardsSpeed = 0;
                inAir = false;
                setPosition(new Vector3f(position.x,terrainHeight,position.z));
            }
        }
    }

//主类

e-> HumanEntity
e.setCollision();-> boolean冲突在上述类中变为真

if(HitBoxMath.isColliding(e.entity.getBox(), entity.getBox())){
    System.out.println("Yay");
    e.setCollision(true);
}else{
    System.out.println("No");
    e.setCollision(false);
}

0 个答案:

没有答案