LWJGL - 让3D FPS相机跳跃

时间:2014-05-03 16:02:21

标签: java opengl 3d game-physics

所以我正在开发我的第一个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 个答案:

答案 0 :(得分:0)

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

  1. 几乎所有事情都有反转的标志
  2. 您正在测试 始终 评估为 true !jumping
  3. 的条件
    // 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
    }