Given a sprite with an x and y coordinate, how would I move it toward another sprite with an x and y coordinate?
I've already made it face towards the other sprite, and I've made it move towards the other sprite, however because I'm using sin
and cos
it's circling the player and slowly moving in on them. Here's the method:
Enemy.prototype.update = function(playerX, playerY) {
// Rotate us to face the player
this.rotation = Math.atan2(this.y - playerY, this.x - playerX) - 2.35;
// Move towards the player
this.x += Math.sin(this.rotation) * this.speed;
this.y -= Math.cos(this.rotation) * this.speed;
}
How can I get the sprite to just move towards the player in a straight line rather than a circle? I found a good example but it uses XNA Vector2
's so it's not working with x and y positions directly.