'aiming prediction in 2d game using projectile

I am trying to make an agent predict the velocity required to throw a ball at a target and take into consideration the target. So what I need is a ball following a projectile path until it hit the target. I want an equation to calculate the required velocity of this based on some parameters like the velocity of the target, its position, and my projectile speed. Currently, I am using an algorithm from this website but it doesn't take the consideration the gravity applied to the velocity.

The current algorithm I am using. It returns a velocity that will hit a moving target but it doesn't consider gravity.

t = 0
        a = (self.world.targets[0].vel.x**2) + (self.world.targets[0].vel.y**2) - (self.speed**2)

        b = 2 * (self.world.targets[0].vel.x * (self.world.targets[0].pos.x - self.world.agents[0].pos.x) +
                 self.world.targets[0].vel.y * (self.world.targets[0].pos.y - self.world.agents[0].pos.y))

        c = ((self.world.targets[0].pos.x - self.world.agents[0].pos.x)**2) + ((self.world.targets[0].pos.y - self.world.agents[0].pos.y)**2)

        disc = (b**2) - 4 * a * c
        if disc < 0:
            return None
        else:
            t1 = (-b + (disc**2)) / (2*a)
            t2 = (-b - (disc**2)) / (2*a)

            if t1 > 0 and t2 > 0:
                t = min(t1, t2)
            elif t1 > 0 and t2 < 0:
                t = t1
            elif t1 < 0 and t2 > 0:
                t = t2

        #print(t)
        aim_x = t * (self.world.targets[0].vel.x + self.world.targets[0].pos.x)
        aim_y = t * (self.world.targets[0].vel.y + self.world.targets[0].pos.y)

        self.vel = Vector2D(aim_x, aim_y)
        self.vel.normalise()
        self.vel = self.vel * self.speed



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source