How can I dynamically set the movement speed of the X and Y of the projectile so that it moves at a constant speed when the projectile heads at the player's position? I know this has something to do with vectors but I don't really have an idea how to do it. My current implementation of the speed is getting the difference of the x and y positions so what happens is that the projectile will be slower the nearer the player is to the enemy. I set the projectile's speed using the code below:
projectile.moveSpeedX = Math.abs(position.x - targetPlayer.position.x);
projectile.moveSpeedY = Math.abs(position.y - targetPlayer.position.y);
Below is the contents of the update(float delta) method of the projectile:
if(xdirection == LEFT) {
position.x -= moveSpeedX * delta;
}
else if(xdirection == RIGHT) {
position.x += moveSpeedX * delta;
}
if(ydirection == UP){
position.y -= moveSpeedY * delta;
}
else if(ydirection == DOWN) {
position.y += moveSpeedY * delta;
}
The movement will slow down when the player is near the enemy since the distance between them is small. The speed is the problem although this already makes the projectile head to the player's position.