2017-08-21 3 views
0

J'ai donc compris comment déplacer quelque chose dans une courbe, quand vous avez 3 points. Je déplace mon sprite dans une courbe comme celle-ci:LibGDX - Le temps du delta fait que le sprite agit bizarrement quelquefois sur le mouvement de la courbe

Le code suivant est dans la méthode render, qui boucle chaque 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(); 
     } 
    } 

Quand je lance cela, 80% du temps, il se passe bien sans problème, mais parfois il fonctionnera et juste un certain décalage bizarre entre les 2 coups (si je fais la première courbe et une autre courbe), comme s'il y avait quelque chose de poisson là que je ne comprends pas.

Ai-je utilisé le delta incorrect?

+1

Vous créez beaucoup d'objets poubelle. Peut-être le GC. Évitez d'allouer de nouveaux objets dans votre boucle de jeu. – Tenfour04

Répondre

1

Comme @ Tenfour04 a commenté votre question. C'est probablement le garbage collector qui entre et crée le décalage. Ne créez pas de nouveaux objets dans la boucle de mise à jour/rendu.

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