I'm writing a simple tower defence and I'm stuck on a bit where my tower has to shoot 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 method arguments are target position and speed. It's supposed to move bullet along vector till it reaches the target but the vector changes because the target is moving. For now the bullet ends up like this (black is the tower, blue is the bullet and red is enemy)
And I know the problem is that my bullet is targeting something that already moved so my question is - how to improve this code so it works properly? I don't know how to wrap my head around vectors and balistics so thanks in advance for keeping it simple.