Java 游戏的浮动计算没有返回正确的值
Java game's float calculation not returning correct values
我正在使用 Java 开发游戏,但计算有点问题。我有 velX 和 velY。当我按 'W' 时,会发生这种情况:
if(currentKeys[i] == "W") {
velX += accel * Math.cos(angle);
velY += accel * Math.sin(angle);
}
这应该让玩家朝正确的方向加速。然后,当我调用 updateVelocity 时,我尝试像这样衰减速度:
private void updateVelocity() {
x += velX;
y += velY;
System.out.println("Velocity X = " + velX + " | Velocity Y = " + velY);
velX = velX * deccel;
velY = velY * deccel;
System.out.println("Velocity X now = " + velX + " | Velocity Y now = " + velY);
}
加速 = 2.5f;
deccel = 0.98f;
velX, velY, x, y 都是浮点数。
然而 System.out.println 打印了这个:
Velocity X = 1.8965478 | Velocity Y = 1.8243668
Velocity X now = 0.09482739 | Velocity Y now = 0.091218345
Velocity X = 1.8965478 | Velocity Y = 1.8243668
Velocity X now = 0.09482739 | Velocity Y now = 0.091218345
Velocity X = 1.8965478 | Velocity Y = 1.8243668
Velocity X now = 0.09482739 | Velocity Y now = 0.091218345
Velocity X = 1.8965478 | Velocity Y = 1.8243668
Velocity X now = 0.09482739 | Velocity Y now = 0.091218345
应该发生的是:2.5 * 0.98 = 2.45,它会稍微降低速度,然后再次增加加速度,并衰减 that 速度。我已经确保所有涉及的变量都是浮点数,但没有任何改变。 System.out.println 不是 returns 精确数字的函数吗?
编辑: 我写了一段代码应该可以重现这个问题。使用它来保持简洁:
float accel, deccel;
float x, y, velX, velY;
float angle;
public Player(float x, float y) {
this.x = x; // the player's x position
this.y = y; // the player's y position
velX = 0.0f; // how much the player's x position will change this frame
velY = 0.0f; // how much the player's y position will change this frame
accel = 2.5f; // accel is a value that multiplies against the cos and sin of the angle in order to provide a directional increase in velX and velY.
deccel = 0.98f; // deccel is a percentage value used to decay the player's velocity
angle = 0.0f; // set angle to a radians value between 0 and (2 * PI)
}
// update is kept barebones, since we only care about updating the velocity every frame in this example
void update(){
updateControls();
updateVelocity();
}
void updateVelocity(){
x += velX;
y += velY;
velX = velX * deccel; // you can also use velX (or velY) *= deccel, I used the long version for a sanity check
velY = velY * deccel;
}
// normally, update controls would be passed a series of strings, representing keystrokes, to process input logic with. However, for simplicity's sake, I've left out the controls, and have slotted in what happens when the player presses 'W'
void updateControls(){
velX += accel * Math.cos(angle);
velY += accel * Math.sin(angle);
}
如果您还需要 material,请告诉我。
经过大量的试验、跟踪和摸索,我找到了原因。我的游戏有一个功能,当玩家按下 'S' 时,飞船会更快地减速。所以,当玩家没有按下 'S' 时,会发生这种情况:
if(!breaking) {
deccel = 0.05f;
}
由于 deccel 的默认值已更改为 0.98f,因此该代码片段现在应为:
if(!breaking) {
deccel = 0.98f;
}
谢谢大家的帮助。我在编码和调试方面仍然越来越好,所以我很感激。
我正在使用 Java 开发游戏,但计算有点问题。我有 velX 和 velY。当我按 'W' 时,会发生这种情况:
if(currentKeys[i] == "W") {
velX += accel * Math.cos(angle);
velY += accel * Math.sin(angle);
}
这应该让玩家朝正确的方向加速。然后,当我调用 updateVelocity 时,我尝试像这样衰减速度:
private void updateVelocity() {
x += velX;
y += velY;
System.out.println("Velocity X = " + velX + " | Velocity Y = " + velY);
velX = velX * deccel;
velY = velY * deccel;
System.out.println("Velocity X now = " + velX + " | Velocity Y now = " + velY);
}
加速 = 2.5f;
deccel = 0.98f;
velX, velY, x, y 都是浮点数。
然而 System.out.println 打印了这个:
Velocity X = 1.8965478 | Velocity Y = 1.8243668
Velocity X now = 0.09482739 | Velocity Y now = 0.091218345
Velocity X = 1.8965478 | Velocity Y = 1.8243668
Velocity X now = 0.09482739 | Velocity Y now = 0.091218345
Velocity X = 1.8965478 | Velocity Y = 1.8243668
Velocity X now = 0.09482739 | Velocity Y now = 0.091218345
Velocity X = 1.8965478 | Velocity Y = 1.8243668
Velocity X now = 0.09482739 | Velocity Y now = 0.091218345
应该发生的是:2.5 * 0.98 = 2.45,它会稍微降低速度,然后再次增加加速度,并衰减 that 速度。我已经确保所有涉及的变量都是浮点数,但没有任何改变。 System.out.println 不是 returns 精确数字的函数吗?
编辑: 我写了一段代码应该可以重现这个问题。使用它来保持简洁:
float accel, deccel;
float x, y, velX, velY;
float angle;
public Player(float x, float y) {
this.x = x; // the player's x position
this.y = y; // the player's y position
velX = 0.0f; // how much the player's x position will change this frame
velY = 0.0f; // how much the player's y position will change this frame
accel = 2.5f; // accel is a value that multiplies against the cos and sin of the angle in order to provide a directional increase in velX and velY.
deccel = 0.98f; // deccel is a percentage value used to decay the player's velocity
angle = 0.0f; // set angle to a radians value between 0 and (2 * PI)
}
// update is kept barebones, since we only care about updating the velocity every frame in this example
void update(){
updateControls();
updateVelocity();
}
void updateVelocity(){
x += velX;
y += velY;
velX = velX * deccel; // you can also use velX (or velY) *= deccel, I used the long version for a sanity check
velY = velY * deccel;
}
// normally, update controls would be passed a series of strings, representing keystrokes, to process input logic with. However, for simplicity's sake, I've left out the controls, and have slotted in what happens when the player presses 'W'
void updateControls(){
velX += accel * Math.cos(angle);
velY += accel * Math.sin(angle);
}
如果您还需要 material,请告诉我。
经过大量的试验、跟踪和摸索,我找到了原因。我的游戏有一个功能,当玩家按下 'S' 时,飞船会更快地减速。所以,当玩家没有按下 'S' 时,会发生这种情况:
if(!breaking) {
deccel = 0.05f;
}
由于 deccel 的默认值已更改为 0.98f,因此该代码片段现在应为:
if(!breaking) {
deccel = 0.98f;
}
谢谢大家的帮助。我在编码和调试方面仍然越来越好,所以我很感激。