Jump to content

Moving sprite to exact location via velocity ends up with jitter


Pixelguy
 Share

Recommended Posts

Hey there,

I'm trying to get an AI to follow another sprite along the Y axis but as soon the exact Y location is almost reached the AI sprite gets jittery due to constant overshooting.
What would be the correct way to implement such a behavior while using a fixed velocity? I've included a simple example below where the sprite should move to game.height/2 :

The usage of the offset variable seems hacky and wrong. (Set 'offset = 10' to stop the jitter).

 

 

And thanks in advance, it's been a few years since I last touched phaser :)

Link to comment
Share on other sites

 

You can use physics to let the AI/enemy follow the player.  Use the difference in position between the AI and the player and set the velocity equal to that difference . You can use drag and mass properties to adjust acceleration towards player:
 

For synced motion you could just set the exact same y position (no need for physics), velocity, acceleration as the player (commented lines in example). 

Another way is to use tweens to animate without physics, but you can use easing functions to customize the motion of the robot/AI:


In general, you could also make one object follow another object using moveToObject:
 

 So there are different options to consider, but the simplest is just to say robot.y= player.y.
Link to comment
Share on other sites

Thanks, thats what I ended up doing. Thank you for the examples samid737 :)

For future reference here is the code I ended up with:

updateEnemy: function() {
  if ( game.math.difference(this.enemy.y, this.ball.y) > game.globals.enemyMovementDeadzone ) {
    if ( this.ball.y < this.enemy.y ) {
      this.enemy.body.velocity.y = -game.globals.maxEnemySpeed;
    } else {
      this.enemy.body.velocity.y = game.globals.maxEnemySpeed;
    }
  } else {
    this.enemy.body.velocity.y = this.ball.body.velocity.y;
    this.enemy.body.maxVelocity.y = game.globals.maxEnemySpeed;
  }
  
},

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...