I am writing a simple tower defense, and I am stuck a little where my tower should shoot at the enemy.
With this code:
void Bullet::move(int x, int y, int speed) { Punkt delta = {(x + speed) - this->x, (y + speed) - this->y}; if (abs(delta.x) > 1 && abs(delta.y) > 1) { this->x += delta.x / this->speed; this->y += delta.y / this->speed; } else { this->dead = true; } }
Where the arguments of the method are the target position and speed. It must move the bullet along the vector until it reaches the goal, but the vector changes because the target moves. At the moment, the bullet is ending (black is the tower, blue is the bullet, and red is the enemy)

And I know, the problem is that my bullet is aimed at something that has already moved, so my question is: how to improve this code so that it works correctly? I don’t know how to wrap my head around vectors and ballistics, so it’s in advance to make it simple.
Deith source share