ben berizovsky
ben berizovsky

Reputation: 761

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

So I figured out on how to move something in a curve, when you have 3 points. I move my sprite in a curve like this:

The following code is in render method, which loops every tick.

    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();
        }
    }

When I run this, 80% of the time it runs smoothly with no problem, but sometimes it will run and just have some weird lag between the 2 moves (if i do first curve and then another curve), like something is fishy there that I don't understand.

Did I use the delta wrong?

Upvotes: 0

Views: 237

Answers (1)

Tejay
Tejay

Reputation: 236

As @Tenfour04 commented on your question. It's probably the garbage collector that kicks in and creates the lag. Do not create new objects inside the update/render loop.

// 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
);

Upvotes: 1

Related Questions