所以我正在开发我的第一个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;
答案 0 :(得分:0)
!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
}