Projectile motion with VPython

Projectile motion is a form of motion experienced by an object thrown into the air at some angle. Since it is a two-dimensional motion, it is characterized by two components:

  1. Horizontal motion: The motion of the object along the x-axis is the horizontal motion which is given by the following equation:

where xx is the horizontal displacement, v0xv_{0x} is the x-component of the initial velocity with which the object is launched, and tt is the instantaneous time.simple pendulum

  1. Vertical motion: The motion of the object along the y-axis is the vertical motion which is given by the following equation:

where yy is the horizontal displacement, v0yv_{0y} is the y-component of the initial velocity with which the object is launched, gg is the gravitational acceleration (which is 9.8ms29.8ms^{2}), and tt is the instantaneous time.

Code

We can simulate the projectile motion using the VPython library. To execute the provided code, you have to follow below mentioned steps:

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

The sample code is as follows:

from vpython import *

g = 9.8  # Acceleration due to gravity (m/s^2)

# Scene setup
scene = canvas(width=1200, height=600, center=vec(0, 10, 0), background=color.white)
ground = box(pos=vec(-10, 0, 0), size=vec(50, 0.2, 2), color=color.gray(0.7))

# Projectile properties
ball = sphere(pos=vec(-20, 0, 0), radius=1.0, color=color.blue)
velocity = 20.0  # Initial velocity (m/s)
angle = 65.0     # Launch angle (degrees)
angle_rad = radians(angle)
v0x = velocity * cos(angle_rad)
v0y = velocity * sin(angle_rad)

# Time step and total time
dt = 0.01

# Simulation loop
while ball.pos.y >= 0:
    # Limit the animation speed
    rate(100)  
    # Update position and velocity
    ball.pos.x += v0x * dt
    ball.pos.y += v0y * dt - 0.5 * g * dt**2

    # Update velocity components
    v0y -= g * dt

Code explanation

  • Line 1: Importing the VPython library.

  • Line 2: Storing the gravitational acceleration in the variable g.

  • Line 6: The scene variable stores a canvas object, which creates a window of dimensions 1200×6001200 \times600 and a white background color. The center of the window is 1010 points below the origin in the y-axis.

  • Line 7: The ground variable stores a box object, which creates a road-like structure the projectile will be launched from and land at.

  • Line 10: The ball variable stores a sphere object, creating a blue three-dimensional object that takes off from the ground object.

  • Line 11–13: The initial velocity (ms2ms^{2}) and angle (degreesdegrees) are stored in the velocity and angle variables respectively. The angle is then converted to the radiansradians using the radians() method and stored in the angle_rad variable.

  • Line 14–15: The x and y components of the initial velocity are stored in the v0x and v0y variables respectively.

  • Line 18: The dt variable stores the instantaneous time of the projectile motion.

  • Line 21: The while loop here will keep executing until the vertical displacement gets zero i.e., the ball hits the ground.

  • Line 23: Sets frames per second.

  • Line 25–26: The horizontal and vertical position of the ball is getting updated with each iteration of the loop.

  • Line 29: The vertical velocity is getting updated because the speed decreases gradually in the projectile motion as the ball approaches the highest point in its trajectory. After that point, the speed increases until the ball hits the ground.

Note: Learn how to simulate a simple pendulum in VPython.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved