Search code examples
c++positiongame-physicssfmlprojectile

SFML - Projectile slow down with different direction


I am creating a Tower Defense game, I created a program which create a projectile starting from a turret and sends it on the enemy as soon as he is in its range of shooting. The projectile moves in the direction of the enemy but on certain directions the projectile is more or less rapid. Here is the code :

//initialPosition is where the projectile is created. 
sf::Vector2f direction = enemyPosition - initialPosition;
projectile.move(direction.x * speed, direction.y * speed);

What am i doing wrong?


Solution

  • I found the solution, here is the code :

    direction = enemyPosition - initialPosition;
    ndir = direction / sqrt(pow(direction.x, 2) + pow(direction.y, 2));
    velocity = ndir * speed;
    pform.move(velocity);