Project 8: Object-Oriented Physics Simulation
The focus on this project is to provide you with more experience writing classes. Now that you have a working ball class, it's time to for the balls to collide with stuff.
Tasks
- Write a Block class
Write a Block class that is similar to the Circle class but uses a Rectangle to represent it in the window. The __init__ method should have one required argument, which is the GraphWin object in which it is to be drawn.
A Block does not need a radius field, but it does need fields for dx and dy that specify its horizontal and vertical size, respectively. Define the visualization rectangle so that the Block's position is in the middle of the rectangle.
In addition to its __init__, the Block class needs to have the following methods.
def draw(self)- draw the visualization objects into the window.def undraw(self)- undraw the visualization objects.def getPosition(self)- return the position as a 2-element tuple.def setPosition(self, px, py)- update the Block's position. The function must also update the graphics object's position.def getVelocity(self)- return the Block's velocity as a 2-element tuple.def setVelocity(self, vx, vy)- update the Block's velocity.def getAcceleration(self)- return the Block's acceleration as a 2-element tuple.def setAcceleration(self, ax, ay)- update the Block's acceleration.def getWidth(self)- return the Block's width (dx)def setWidth(self)- (**optional**) changes the Block's width. This method will need to undraw the shapes in the vis list, create a new vis list, then draw the shapes.def getHeight(self)- return the Block's height (dy)def setHeight(self)- (**optional**) changes the Block's height. This method will need to undraw the shapes in the vis list, create a new vis list, then draw the shapes.def update(self, dt)- implement the equations of motion for the block.
- Write a collision function
def collision(self, ball):Write a function that returns True if the ball is intersecting the block. Use the Ball's getPosition and getRadius functions to get that information. Then check if the ball is intersecting the block. You can assume the ball is a square for the purposes of collision, if that makes it easier.
If the ball is not colliding with the Block, the function should return False.
Explaining your collision function should be a key part of your report.
You can use this test function to test the intersection code. When you run it, hit the space bar to move the ball to a randomized location.
- Add blocks to the fall program
Add blocks to at least the bottom of the screen in your fall program from the lab. A block should disappear if it is hit by the ball.
Required item 1: Capture a short video of this program running. Submit it either in your report or in your handin directory.
- Create some kind of interactive demo
Give the user some kind of control over how to launch a ball into the scene. Have the ball collide with blocks and do something. The blocks could change color, disappear, move, or do anything else you can code. Likewise, the ball could disappear or bounce, enabling it to hit other blocks.
You can use the win.checkKey() function to see if the user has typed a key. If the user has typed the left, right, up, or down arrow keys, then checkKey will return 'Left', 'Right', 'Up', or 'Down' as the return value. The space bar returns the string 'space'.
Required item 2 Capture a short video of this program running. Submit it either in your report or in your handin directory.
Follow-up Questions
- What is the purpose of having get and set functions for a class? Why not just access the object fields directly?
- Given a list of Zelle Graphics objects, write a for loop that would move each object in the list by (5, 10) in x and y.
- The Zelle GraphWin class has functions getMouse() and checkMouse(). What is the difference between them?
- What is the difference between simulation space and visualization space?
Extensions
Extensions are your opportunity to customize your project, learn something else of interest to you, and improve your grade. The following are some suggested extensions, but you are free to choose your own. Be sure to describe any extensions you complete in your report.
- Create more elaborate or additional setups/scenes/interactions. Include screen videos in your report/handin folder.
- Make other object classes for the simulation. Add other collision functions. A reasonable thing to do is to approximate an object with a box or a circle, even if it is not one of those shapes.
- Add the capabilility for balls to collide with one another. Keep it simple and get it working with just 2 balls first.
- Make the simulation more visually stimulating. Add colors, or change the visualization of a the ball or block class to something more complex (e.g. a smaller ball inside the larger ball.
Submit your code
Turn in your code (all files ending with .py) by putting it in a directory in the Courses server. On the Courses server, you should have access to a directory called CS152, and within that, a directory with your user name. Within this directory is a directory named Private. Files that you put into that private directory you can edit, read, and write, and the professor can edit, read, and write, but no one else. To hand in your code and other materials, create a new directory, such as project6, and then copy your code into the project directory for that week. Please submit only code that you want to be graded.
When submitting your code, double check the following.
- Is your name at the top of each code file?
- Does every function have a comment or docstring specifying what it does?
- Is your handin project directory inside your Private folder on Courses?
Write your project report
For CS 152 please use Google Docs to write your report. Create a new doc for each project. Start the doc with a title and your name. Attach the doc to your project on Google classroom. Make sure you click submit when you are done. The graders cannot provide feedback unless you click submit.
Your intended audience for your report is your peers not in the class. From week to week you can assume your audience has read your prior reports. Your goal should be to be able to use it to explain to friends what you accomplished in this project and to give them a sense of how you did it.
Your project report should contain the following elements.
-
A brief summary of the project, in your own words. This should be no more than a few sentences. Give the reader context and identify the key purpose of the assignment.
Writing an effective abstract is an important skill. Consider the following questions while writing it.
- Does it describe the CS concepts of the project (e.g. writing well-organized and efficient code)?
- Does it describe the specific project application?
- Does it describe your the solution or how it was developed (e.g. what code did you write)?
- Does it describe the results or outputs (e.g. did your code work as expected)?
- Is it concise?
- Are all of the terms well-defined?
- Does it read logically and in the proper order?
- A description of your solution to the tasks, including any text output or images you created (including the three required images mentioned above). This should be a description of the form and functionality of your final code. Note any unique computational solutions you developed or any insights you gained from your code's output.
- A description of any extensions you undertook, including text output or images demonstrating those extensions. If you added any modules, functions, or other design components, note their structure and the algorithms you used.
- The answers to any follow-up questions (there will be 3-4 for each project).
- A brief description (1-3 sentences) of what you learned. Think about the answer to this question in terms of the stated purpose of the project. What are some specific things you had to learn or discover in order to complete the project?
- A list of people you worked with, including TAs and professors. Include in that list anyone whose code you may have seen, such as those of friends who have taken the course in a previous semester.
Project 8