簡體   English   中英

LibGDX-增量時間有時會導致精靈在曲線移動時產生奇怪的作用

[英]LibGDX - Delta time causes the sprite to act weird sometimes on curve move

所以我想出了當您有3個點時如何在曲線上移動某物的方法。 我將精靈移動成這樣的曲線:

以下代碼位於render方法中,該方法循環每個刻度。

    if (ship.isMoving()){
        // Normalized direction vector towards target
        Vector2 dir = ship.getEndPoint().cpy().sub(ship.getLinearVector()).nor();

        // Move towards target by adding direction vector multiplied by speed and delta time to linearVector
        ship.getLinearVector().add(dir.scl(2 * Gdx.graphics.getDeltaTime()));

        // calculate step based on progress towards target (0 -> 1)
        float step = 1 - (ship.getEndPoint().dst(ship.getLinearVector()) / ship.getDistanceToEndPoint());

        if (ship.getCurrentPerformingMove() != MoveType.FORWARD) {
            // step on curve (0 -> 1), first bezier point, second bezier point, third bezier point, temporary vector for calculations
            Bezier.quadratic(ship.getCurrentAnimationLocation(), step, ship.getStartPoint().cpy(),
                    ship.getInbetweenPoint().cpy(), ship.getEndPoint().cpy(), new Vector2());
        }
        else {
            Bezier.quadratic(ship.getCurrentAnimationLocation(), step, ship.getStartPoint().cpy(),
                    new Vector2(ship.getStartPoint().x, ship.getEndPoint().y), ship.getEndPoint().cpy(), new Vector2());
        }

        // check if the step is reached to the end, and dispose the movement
        if (step >= 0.99f) {
            ship.setX(ship.getEndPoint().x);
            ship.setY(ship.getEndPoint().y);
            ship.setMoving(false);
            System.out.println("ENDED MOVE AT  "+ ship.getX() + " " + ship.getY());
        }
        else {
            // process move
            ship.setX(ship.getCurrentAnimationLocation().x);
            ship.setY(ship.getCurrentAnimationLocation().y);
        }

        // tick rotation of the ship image
        if (System.currentTimeMillis() - ship.getLastAnimationUpdate() >= Vessel.ROTATION_TICK_DELAY) {
            ship.tickRotation();
        }
    }

當我執行此操作時,它有80%的時間運行順利,沒有問題,但是有時它會運行,並且在兩次移動之間(如果我先彎曲然后再彎曲)只是有些怪異的滯后,就像那里有些魚腥我不明白

我是否使用了錯誤的增量?

正如@ Tenfour04對您的問題發表評論。 可能是垃圾收集器啟動並產生了滯后。 不要在更新/渲染循環內創建新對象。

// instance variable tmp
private Vector2 tmp = new Vector2();

// dir is now a reference to tmp no new objects allocated
Vector2 dir = this.tmp.set(ship.getEndPoint()).sub(ship.getLinearVector()).nor();

// the same thing with Bezier.quadratic equation
// only the currentAnimationLocation and tmp will be modified in the method
Bezier.quadratic(
    ship.getCurrentAnimationLocation(), step, ship.getStartPoint(),
    ship.getInbetweenPoint(), ship.getEndPoint(), this.tmp
);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM