Visual Python (VPython) is a module in Python programming language which provides a platform to make 3D models and simulations. In VPython, you can create various objects like sphere, box, cone, cylinder etc. You can assign different attributes to these objects like size, mass, color, position, velocity, acceleration etc. You can use all the commands and codes of Python language in VPython also. In this post, we are going to simulate a ball which falls under the effect of gravity and bounces off after striking the floor.
You do not need to know deep physics to understand this simulation. You just need to know that when an object falls under the effect of gravity, it experiences an acceleration due to gravity (g = 9.8 m/s2). The acceleration results in the change in velocity of the object. Consider the initial velocity of the object be v(t) and after a time interval of dt, the velocity change to v(t+dt) which is given as:
The velocity results in the change in position of the object. Consider, the position of the object at time t to be represented as x(t), after a time dt, the position changes to x(t+dt) which is given as:
Now we can look at the VPython but before you start writing any code, you need to install VPython on your computer. You need to install both Python and VPython compilers. You can download the Vpython from the official site. The VPython provides support for Windows, Macintosh and Linux. You need to install Python first then Vpyhton. The support for Vpython is not so good, so you can either use windows .exe file setup using wine or you can use the older version provided in Ubuntu Software Center. Alternatively, you can go to Glowscript website and run code on online compiler. There you just need to sign in with google account and you can edit and run your code. If you don’t want to edit but just want to run, you don’t need to sign in. You can run code without even sign in.
After installing VPython, you need to open VIDLE, in which you can write your codes and save file with extension .py (for example ball.py). You can also write code in some simple text editor and save as .py file and go to the terminal or command prompt run using VPython.
I assume now you have completed all the installation procedure and are able to access VIDLE or text editor. I am using Linux which mostly uses a text editor and run .py file using the terminal. The VIDLE will look some like IDLE which is used for python but VIDLE has other tools also.
We want to use visual python module so we need to write the following code as the first line:
from visual import*
If you are using glowscript then you don’t need to write above line but you need to replace that with
GlowScript 2.4 VPython
This line already present in the newly created files. Now we will create a sphere with the following command:
from visual import* sphere()
After writing the code, we will save the file with name ball.py (you can use any name) and click on run in the toolbar and select run module or press F5 which will open a python shell and a VPython window with a sphere.
Sphere() creates a white color sphere because we have not specified anything for the sphere. We can specify a radius, position, and color etc. inside the brackets. The position in VPython is mentioned using vector. The x-axis is towards the right, the y-axis is upward and the z-axis is towards you (means outward of the screen). Suppose, we want to place the ball at some height, say 10m, so we can select the position as (0,10,0). We will select radius as 1 and color as red color. We will also give a name to this sphere and call it “ball”. The new code will look like:
from visual import* ball = sphere(pos=vector(0,10,0), radius=1, color=color.red)
The code says that the ball is sphere with position = (0,10,0), radius = 1 and color as red color. We have created a ball which is placed at a height of 10m. Now, we will create a floor which is actually a box with position at (0,0,0) and has size as (10,0.5,10) which means height = 0.5, width = 10, breadth = 10. We will name this box as “floor” and give it a green color. The code will look like:
from visual import* ball = sphere(pos=vector(0,10,0), radius=1, color=color.red) floor = box(pos=vector(0,0,0), size = vector(10,0.5,10), color=color.green
The position of the ball can be changed if we give some other value to the position of the ball. We want to change the position of the ball according to velocity as given above in Equation 2. Now we will create a new attribute of the ball that is called velocity. You can either add a new line or modify the second line in the code. We will write a new line which will define a velocity vector for the ball.
Any attribute can be specified by writing it after the object as object.attribute. Let us also choose a time interval dt = 1, the Equation2 can be used to find the new position of the ball as:
This line says that take the product of velocity and time interval and add that to the position of the ball, then store the result into ball.pos replacing older value. The addition will be done using vector addition which will calculate all three component of the vector. The total code will look like:
from visual import* ball = sphere(pos=vector(0,10,0), radius=1, color=color.red) floor = box(pos=vector(0,0,0), size = vector(10,0.5,10), color=color.green) ball.velocity=vector(0,-9,0) dt=1 ball.pos=ball.pos+ball.velocity*dt
The position of the ball will now change to the new position, the calculation is done so fast that you were unable to see the change in position. Don’t worry we will take care of the rate. We want to change the position of the ball not just once but continuously. You don’t need to write that last line repeatedly but instead, we will use a loop which will execute the same statement in repetition. We will use while loop and use a counter variable t which will represent time and will also count the umber of loops. The new code will look like:
from visual import* ball = sphere(pos=vector(0,10,0), radius=1, color=color.red) floor = box(pos=vector(0,0,0), size = vector(10,0.5,10), color=color.green) ball.velocity=vector(0,-1,0) dt=0.1 t=0 while (t<20): rate(100) ball.pos=ball.pos+ball.velocity*dt t=t+dt
We have changed the velocity to (0,-1,0), dt to 0.1 and defined a new variable t = 0 which represent time. While loop says that execute the statements repeatedly if t is less than 20. Please take care of indentation ( 4 spaces) after while statement, all the indented statements will execute repeatedly inside the loop. The rate(100) tells the python that does not perform more than 100 calculation per second. The position will update and t will increase by amount dt with each iteration. When t will become greater than 20 the program will come out of the loop. This time, you can see that the ball slowly comes down.
While coming down the ball just passes through the floor and does not bounce off. If want it to bounce, we need to tell the ball that please change your velocity, you have just approached a floor. We actually do the same thing, we will write an if statement which will change the ball’s velocity if ball’s position becomes less than or equal to floor’s position.
from visual import* ball = sphere(pos=vector(0,10,0), radius=1, color=color.red) floor = box(pos=vector(0,0,0), size = vector(10,0.5,10), color=color.green) ball.velocity=vector(0,-1,0) dt=0.1 t=0 while (t<20): rate(100) ball.pos=ball.pos+ball.velocity*dt if ball.pos.y<floor.pos.y : ball.velocity.y = -ball.velocity.y t=t+dt
The if statement tells that if y component of ball’s position becomes less than y component of floor’s position then change y component of ball’s velocity. Now the ball can be seen falling and then bouncing back from the floor but the ball enters into the surface and pops out at another side which we don’t want. When the surface of ball touches the surface of the floor the distance between the centers of both is equal to the sum of the radius of the ball (1) and half the thickness of floor (0.25). We need to take care of this 1.25 into the if statement.
if ball.pos.y<floor.pos.y+1.25 :
This ball is falling because we have given it a negative vertical velocity but in real life, the initial velocity of a falling object is zero and it gains velocity as a result of acceleration due to gravity (g=9.8 m/s). So, now we will make initial velocity as (0,0,0) and use the equation 1 shown above to change the velocity of the ball as:
This equation will be used before updating the position of the ball.
from visual import* ball = sphere(pos=(0,10,0), radius=1, color=color.red) floor = box(pos=(0,0,0), size = (10,0.5,10), color=color.green) ball.velocity=vector(0,0,0) dt=0.01 t=0 g=-9.8 while (t<20): rate(100) ball.velocity.y=ball.velocity.y+g*dt ball.pos=ball.pos+ball.velocity*dt if ball.pos.y<floor.pos.y+1.25 : ball.velocity.y = -ball.velocity.y t=t+dt
Here, we have defined a new variable g = -9.8 because the direction of acceleration due to gravity is downward. We have also decreased the dt to 0.01. Now, the ball will fall and keep on bouncing many times before the t reaches 20. You can increase the upper limit of t in while statement if you want to run the program for a larger duration.
The accuracy of our program depends upon the value to dt. If dt is very small than the program become more accurate but then it will require a larger number of iteration to do the same thing. Thus, we need to choose some appropriate value so that the error is not very large. According to our concept, the total energy of the ball is conserved and it should never stop but the error due to the large value of dt can cause the ball to show some unexpected results if we run the program for the larger number of bounces.
You can also watch our video which shows all the above steps in detail. If you like this tutorial, then please like, share and subscribe. If you have some doubt you can ask in the comment section. You can follow us on facebook and twitter. You can subscribe us for Email Notification also to get an email whenever we publish a new post.