Projectile Motion Simulation
By Hogan Wong
Want to contribute? Contact us!
By Hogan Wong
The actual simulation can be found here, whereas the link to the repository is located here. The original Python version (the version in the Kwantum Institute repository is in Javascript + HTML) can be located here. This article will focus on the Python version of the simulation since I believe that it is easier to use compared to JS/HTML, so it would be more beginner friendly (it also helps that I'm more fluent in Python).
Note: I added some comments in order to assist myself while programming too, so use them as an educational tool as well!
In computer science, a class is a blueprint for creating various "objects" such as a ball that follows the equations of motion.
To create the simulation, I had to use an object-oriented programming language. Hence, I opted to use Python due to its easier syntax compared to other OOP languages such as Java. For example, Java requires getters and setters to deal with public/private variables, whereas Python doesn't.
Aside from that, I had to use modules from Pygame to get certain functions required for the simulation.
UPDATE: I separated the ball class into a separate file to make formatting easier; this was also implemented in my air resistance simulation.
The first stage in creating the class is setting up the constructor. It defines the attributes of the object. Note that "down" is the positive y-direction in this simulation, whereas "right" is the positive x-direction. This was done to make the coding process easier.
The next integral step is to create some functions, sets of rules that govern how an object of the class "Ball" behaves.
The first function, the "update" function, instructs the ball to behave under the equations of motion (Equations for an object experiencing free-fall). A conditional was added to make sure that if the ball hits the ground, the ball would bounce. However, to simulate real-life conditions, some energy is lost during each bounce. Hence, the velocity would decrease by 0.8 each bounce. The negative sign reverses the direction of the velocity.
Aside from that, the second function is responsible for mapping the ball onto the simulation.
The first line uses the constructor defined in part 1 to create a ball object. Variable names are used in place of numbers as the values of these variables will change throughout the simulation.
The second section of code uses in-built functions from Pygame to create a "ground" object. The trajectory points is a list variable used to store the position of the ball throughout the simulation.
These remaining sections of code detail how the simulation is set up in Pygame.
Function 1: Ensures that the simulation keeps running unless someone exits it
Function 2: Uses the "update" class defined earlier for ball objects. This allows it to follow the equations of motion.
Function 3: Adds x and y coordinates to trajectory points
Function 4: Makes the screen blank
Function 1: Creates a ground via in-built Pygame functions
Function 2/3: Draws trajectory/ball respectively
Function 4/5: Deals with visual details
Function 6: Allows the user to leave the simulation
The former requires the use of an IDE (integrated development environment) such as Visual Studio Code to run, whereas the latter is run on a website.
The Pygame simulation highlights the trajectory of the ball, whereas the latter does not.