Using tensorflow we will predict the throw velocity and scores a lot
I will be using Pandas, Numpy and Tensorflow for the data analysis. For the GUI visualisations, I will use pygame
First let's dive into the physics of projectile motion.
for a given initial launch angle,
the horizontal and vertical components of the velocity are:
and the components of velocity at any time
For a given change in time dt the new poistion of the ball will be:
def updateVx(self, dt):
self.vx = self.vx + self.ax * dt
return self.vx
def updateVy(self, dt):
self.vy = self.vy + self.ay * dt
return self.vy
def updateX(self, dt):
self.x = int(self.x + 0.5*(self.vx + self.updateVx(dt))*dt)
return self.x
def updateY(self, dt):
self.prev_y = self.y
self.y = int(self.y - 0.5*(self.vy + self.updateVy(dt))*dt)
return self.yAdding a GUI we can test the ball motion:
After an online research I came across the following ratio:
After some styles and text keep track of the score we get:
We are ready to predict some shots!
I let the computer to play with random vectors of : position(x,y), V and angle.
After some time I gathered the Data and saved the throws which got into the basket for the learning process.
I changed the position - (x,y) to polar coordinates where the axes origin is the center of the hoop hence:
I used 70% of the data to train the modal and evaluated the prediction with throws from the test samples. (the remain 30%)
I used tensorflow with 4 hidden layers and activation of relu. the epochs set to 3000 and batch size of 200.
It’s game day!
After to modal trained, I ran the game script with the modal outputs and was glad to see that 98% of the throws was getting in the basket!!
I highly recommend you check source code on Github and prompt any question)



