gpt4 book ai didi

java - 如何阻止我的游戏船向后加速?

转载 作者:行者123 更新时间:2023-12-02 09:27:38 25 4
gpt4 key购买 nike

我有一个游戏,它有一个船舶对象(带有 JavaFX 多边形)。当我用按键(通过事件处理程序和动画计时器)移动飞船时,它会向上移动。当我放手时,它应该将加速度更改为 -4,因此它会不断地从船的速度中减去 -4 直到其假定达到 0。但是,因为船可以向所有方向移动,所以速度有时为负值当船前进时。因此,当我尝试在速度为负时停止船时,它不会发生,并且船继续向后移动。

我尝试了一些计算,当船的速度为负并向前移动时,但它有点太复杂了,我相信它不起作用。

以下是船舶计算速度 x 方法的代码:

AnimationTimer calculateVelocityX = new AnimationTimer() {
@Override
public void handle(long now) {
if (getAcceleration() == 17)
setVelocityX(getVelocityX() + (Math.cos(Math.toRadians(getRotation())) * (getAcceleration() * 0.01)));
else if (acceleration == -4)
setVelocityX(getVelocityX() + (Math.cos(Math.toRadians(getTempRotation())) * (getAcceleration() * 0.01)));
}
};
    AnimationTimer shipAccelerationTimer = new AnimationTimer() {
@Override
public void handle(long now) {
getShipImage().setLayoutX(getShipImage().getLayoutX() + getVelocityX());
getShipImage().setLayoutY(getShipImage().getLayoutY() - getVelocityY());
wrapShip();
if (getVelocityX() == 0 && getVelocityY() == 0) {
getCalculateVelocityX().stop();
getCalculateVelocityY().stop();
setAcceleration(17);
setCounterOne(0);
setCounterTwo(0);
setCounterThree(0);
getShipAccelerationTimer().stop();
}
}
};

此代码将使船向后移动,因为由于小数精度,速度实际上永远不会达到 0。然而,如果我说当速度小于0时,根据我上面所说这是不可能的。

cargo 沿指定方向移动时的速度标志图像:

/image/5sxNi.png

因此,我不能只拥有“当速度小于 0 时”,因为就像在象限 2、3、4 中一样,我可以向前移动,但 x、y 速度要么为负,要么两者都有。

最佳答案

向后加速是行不通的,因为方向不是恒定的。

考虑这样一种情况:船舶向上加速,然后在加速后但在停下来之前向右转向正好 90°。 y方向的零件速度分量永远不会变成0,但x方向的运动方向会不断变化...

如果没有加速,您需要向与当前运动相反的方向减速,而不是根据您的飞船当前面向的方向来减速。

顺便说一句,我建议不要在这里使用 3 个注释来改变速度。这只会让你的代码变得不必要的复杂。您可以在一个动画中完成所有事情。

示例:

(代码保持简单而不是精心设计)

@Override
public void start(Stage primaryStage) throws Exception {
double w = 600;
double h = 600;

Rotate rotation = new Rotate(0, 0, 0);
Line ship = new Line(0, 0, 20, 0);
ship.getTransforms().add(rotation);

ship.setLayoutX(w / 2);
ship.setLayoutY(h / 2);

Pane root = new Pane(ship);
root.setPrefSize(w, h);

class Animator extends AnimationTimer {
// the current speed
double vx;
double vy;

// the direction the ship is facing
double directionX = 1;
double directionY = 0;

// the current acceleration magnitude
double acceleration = 0;

@Override
public void handle(long now) {
if (acceleration > 0) {
// speed up in the direction the ship is currently facing
vx += directionX * acceleration * 0.001;
vy += directionY * acceleration * 0.001;

acceleration -= 0.1;
} else if (vx != 0 || vy != 0) {
// decelerate
double speed = Math.hypot(vx, vy);

// constant deceleration opposite to velocity
double correctionFactor = Math.max((speed - 0.01) / speed, 0);
vx *= correctionFactor;
vy *= correctionFactor;
}

// update position
ship.setLayoutX(ship.getLayoutX() + vx);
ship.setLayoutY(ship.getLayoutY() + vy);
}

}

Animator animator = new Animator();
animator.start();

Scene scene = new Scene(root);

// make sure the ship always faces the mouse
root.setOnMouseMoved(evt -> {
double dx = evt.getX() - ship.getLayoutX();
double dy = evt.getY() - ship.getLayoutY();

if (dx == 0 && dy == 0) {
// handle special case of mouse being in the same position as the ship
dx = 1;
}

// assign normalized direction
double magnitude = Math.hypot(dx, dy);
animator.directionX = dx / magnitude;
animator.directionY = dy / magnitude;

// update ship rotation
rotation.setAngle(Math.toDegrees(Math.atan2(dy, dx)));
});

root.setOnMouseClicked(evt -> {
// accelerate
animator.acceleration = 17;
});
primaryStage.setScene(scene);
primaryStage.show();
}

关于java - 如何阻止我的游戏船向后加速?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58227428/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com