What is VPython?

VPython (Visual Python) is a 3D graphics library that allows us to create and visualize three-dimensional objects on the screen. It is primarily used to visualize the impact of physics equations on the objects' motion.

The library is built on top of OpenGL and is designed to make 3D graphics accessible to students and beginners in Python programming.

Installation

We can run the following command on the terminal or command prompt to install VPython:

pip install vpython
Command for python installation

Note: VPython is compatible with Python versions 3.x.

Example

The standard workflow for using VPython is as follows:

  1. Create a three-dimensional scene.

  2. Create three-dimensional objects as per the needs.

  3. Manipulate those objects within the scene.

We can always add interactivity and looping to our animations.

Sample code

A sample code using VPython is provided below. The code creates a three-dimensional sphere that moves around the screen and bounces off once it gets hit by the screen edges.

To execute the code, you have to follow below mentioned steps:

Once you click the "Run" button, follow the encircled link
1 of 4

# Import the VPython module
from vpython import *
import random

# Constants
width = 1000    # Width of the screen (units)
height = 600    # Height of the screen (units)
screen_res = (width, height)

# Create the scene
scene = canvas(width=width, height=height, center=vector(0, 0, 0), background=color.black)
scene.title = "Bouncing Sphere"

# Create the ball object
ball = sphere(pos=vector(100, 100, 0), radius=40, color=color.red)

# Random initial velocity of the ball
initial_velocity = vector(random.uniform(-1, 1), random.uniform(-1, 1), 0) * 3

# Animation loop
while True:
    rate(100)  # Limit the animation speed
    
    # Update the ball's position based on its velocity
    ball.pos = ball.pos + initial_velocity
    
    # Check if the ball hits the screen edge and handle the bounce
    if abs(ball.pos.x) >= width / 2 - ball.radius:
        initial_velocity.x = -initial_velocity.x
    
    if abs(ball.pos.y) >= height / 2 - ball.radius:
        initial_velocity.y = -initial_velocity.y

Code explanation

  • Lines 1–3: Importing the necessary libraries.

  • Lines 6–8: Setting a scene with a resolution of 1000×6001000 \times 600.

  • Lines 11: The canvas() object is being created for our scene. The object takes the scene's dimensions, center, and background color as its parameter.

  • Line 14: Creating a sphere using the sphere() object.

  • Line 17: Declaring a vector() object and setting the speed of the moving sphere. The vector is then stored in the initial_velocity variable.

  • Line 20: The loop will keep the sphere moving until someone terminates the code.

  • Line 21: The rate() method is setting the frames per second of the animation to 100.

  • Line 24: The sphere's position is updated based on the defined velocity vector.

  • Lines 27–31: These lines change the sphere's direction as it hits the screen edge.

Learn more about:

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved