有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java LWJGL让3D FPS相机跳跃

所以我正在做我的第一个3D项目,一切都很顺利,直到我决定让FPS角色(相机)跳转。我尝试了一些方法,但直到我成功地做了一件大多数时候都有效的事情,它们看起来都不太好。现在的问题是“大部分时间”——应该是所有时间

问题是,当我连续点击跳转按钮时,它开始发狂。当我超过它时,我试图将位置设置回零,但有时仍然会出现异常,尽管不太常见。为了让它一直工作,有什么建议吗?(我使用的是OpenGL和java,没有引擎)

跳跃部分代码:

if (Keyboard.isKeyDown(Keyboard.KEY_SPACE) && !jumping
    && position.y >= temp) { //temp - floor height 
jumping = true;
}
gravity = (float) ((walkingSpeed * 0.00003) * delta);//delta - time
//System.out.println(delta);
if (position.y < temp) { //In the air
    speed = speed + gravity;
    position.y = position.y + speed;
}
if (jumping) {
    speed = speed - (float) ((walkingSpeed * 0.0006) * delta);
    position.y = position.y + speed;
    jumping = false;
}
if (position.y > temp) //**Supposed** to solve the problem
    position.y = temp;

共 (1) 个答案

  1. # 1 楼答案

    代码中有许多问题与您描述的内容不同步:

    1. 你几乎所有的东西都有相反的标志
    2. 您正在测试的条件是总是评估为!jumping

    // Only start a jump while on the ground (position.y <= temp)
    if (Keyboard.isKeyDown(Keyboard.KEY_SPACE) && position.y <= temp) {
      speed = speed + (float) ((walkingSpeed * 0.0006));
      // Don't care about the delta time when you jump, just add a fixed amount of
      // positive vertical velocity (which gravity will work out over time).
    }
    
    // You had gravity going the wrong direction initially
    gravity = (float) -((walkingSpeed * 0.00003) * delta);
    
    //System.out.println(delta);
    speed      = speed + gravity;
    position.y = position.y + speed;
    
    if (position.y < temp) { // Don't sink into the ground
      position.y = temp;
      speed      = 0.0; // Scrub off acceleration due to gravity
    }