Monday 27 July 2015

Make a Bouncing Ball Using VPython

Visual Python or VPython as Wikipedia says that VPython is the one of Python Programming Language to make a 3D graphics module called Visual.

As we know that, using VPython we can create objects such as cones, boxes, and spheres in 3D space and displays these objects. This makes it easy to create some illustration especially to visualization physics' subject.

In this tutorial I want to make a simple visualization using VPython it is how to make a bouncing ball :)

1. Firstly open VIDLE Visual Python, I use VPython version 6.10 for Python version 2.7 win32.
if you don't have the program, you can download by click the link below:


2. After you open the VIDLE VPython, type the syntax as shown below. You can copy the syntax.

from visual import *

floor = box (pos=(0, 4, 0), lenght=4, height=0.5, width=4, color=color.green)
ball = sphere (pos=(0,4,0), radius=1, color=color.red)
ball.velocity = vector(0,-1,0)
dt = 0.01

while 1:
    rate (100)
    ball.pos = ball.pos + ball.velocity*dt
    if ball.y < ball.radius:
        ball.velocity.y = abs(ball.velocity.y)
    else:
        ball.velocity.y = ball.velocity.y - 9.8*dt

3. You can change the size of floor, radius of ball, the vellocity and the colour of ball and floor as you want.

4. Before running the syntax you have to save it first with file extension .py (for example: bouncing ball.py)

No comments:

Post a Comment