亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何阻止我的游戲船向后加速?

如何阻止我的游戲船向后加速?

桃花長相依 2023-09-20 17:22:12
我有一個游戲,有一個船對象(帶有 JavaFX 多邊形)。當我用按鍵(通過事件處理程序和動畫計時器)移動飛船時,它會向上移動。當我放手時,它應該將加速度更改為 -4,因此它會不斷地從船的速度中減去 -4 直到其假定達到 0。但是,因為船可以向所有方向移動,所以速度有時為負值當船前進時。因此,當我嘗試在速度為負時停止船只時,這種情況不會發生,并且船只繼續向后移動。我嘗試了一些計算,當船的速度為負并向前移動時,但它有點太復雜了,我相信它不起作用。以下是 Ship 計算速度 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時,根據我上面所說這是不可能的。貨物沿指定方向移動時的速度標志圖像:https://i.stack.imgur.com/5sxNi.png因此,我不能只擁有“當速度小于 0 時”,因為就像在象限 2、3、4 中一樣,我可以向前移動,但 x、y 速度要么為負,要么兩者都有。
查看完整描述

2 回答

?
月關寶盒

TA貢獻1772條經驗 獲得超5個贊

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


考慮這樣的情況:船舶向上加速,然后在加速后但在停下來之前向右轉向正好 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();

}


查看完整回答
反對 回復 2023-09-20
?
烙印99

TA貢獻1829條經驗 獲得超13個贊

如果我在你身邊,我會考慮考慮速度及其大小和方向,其中大小>=0,方向為±1,與正X和正Y相比。



查看完整回答
反對 回復 2023-09-20
  • 2 回答
  • 0 關注
  • 106 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號