CS 152
Project 7: Object-Oriented Physics Simulation
The focus of this project is to provide you with more experience writing classes. Now that you have a working Ball class, it’s time to model a second physics object, a block, and then we will model collisions between balls and blocks.
Tasks
T1. Write a Block class
Write a Block class that is similar to the Ball class, but uses a Zelle 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.
de f und raw (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.
T2. Write a collision function
Here is Prof. Maxwell talking about detecting collisions: Ball-block collisions
Add the collision method to your Block class.
def collision (self, ball):
Write a function that returns True if the ball is intersecting the block and False otherwise.
Use the Ball’s get Position and get Radius functions to get that information. Then check if the ball is intersecting the block. Assume that the ball is a square for the purposes of detecting collisions. It will make the calculations easier. Simplifying assumptions are often used in this manner.
Required Element 1: 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 spacebar to move the ball to a randomized location.
T3. 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 Element 2: Capture a short video of this program running. Submit it with your report.
T4. Create an 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 Element 3: Capture a short video of your interactive program and include it with your report.
Required Element 4:
Follow-up Questions
1. What is the purpose of having get and set functions for a class? Why not just access the object fields directly?
2. 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.
3. The Zelle GraphWin class has functions getMouse() and checkMouse(). What is the difference between them?
4. 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 as it simplifies the calculations.
● Add the capability for balls to collide with one another. Keep it simple and get it working with just 2 balls first.
● Make the simulation more visually interesting. Add colors, or change the visualization of the ball or block class to something more complex (e.g. a smaller ball inside the larger ball.
Write your project report
Reports are not included in the compressed file! Please don’t make the graders hunt for your report.
You can write your report in any word processor you like and submit a PDF document in the Google Classroom assignment folder. Or use a Google Document format.
Review the Writeup Guidelines document in the Labs and Projects folder.
Your intended audience for your report is your peers who are not taking CS classes.
From week to week, you can assume your audience has read your prior reports. Your goal should be to explain to peers what you accomplished in the project and to give them a sense of how you did it. The following is a list and description of the mandatory sections you must include in your report. Do not include the descriptions in your report, but use them as a guide in writing your report.
● Abstract
A 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. An abstract should define the project’s key lecture concepts in your own words for a general, non-CS audience. It should also describe the program’s context and output, highlighting a couple of important algorithmic and/or scientific details. 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 (e.g. generating data)?
○ Does it describe your solution and 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 and what did the results tell you)?
○ Is it concise?
○ Are all of the terms well-defined?
○ Does it read logically and in the proper order?
● Methods
The method section should describe in clear sentences (without pasting any code) at least one example of your own computational thinking that helped you complete your project. This could involve illustrating how a key lecture concept was applied to creating an image, how you solved a challenging problem, or explaining an algorithmic feature that is essential to your program as well as why it is so essential. The explanation should be suitable for a general audience who does not know Python.
● Results
Present your results in a clear manner using human-friendly images or graphs labeled with captions and interpreted for a general audience such as your peers not in the course. Explain, for a general, non-CS audience, what your output means and whether it makes sense.
● Reflection and Follow-up questions
Draw connections between lecture concepts utilized in this project and real-world problems that interest you. How else could these concepts apply to our everyday lives? What are some specific things you had to learn or discover in order to complete the project? Look for a set of short answer questions in this section of the report template.
● Extensions (Required even if you did not do any)
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.
● References/Acknowledgements (Required even if there are none)
Identify your collaborators, 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. Cite any other sources, imported libraries, or tutorials you used to complete the project.
Submitting your Project Report and Code
Turn in your code by zipping the file and uploading it to Google Classroom. When submitting, double check the following.
1. Is your name at the top of each Python file?
2. Does every function have a docstring (‘’’ ‘’’) specifying what it does?
3. All your code is in your Project 07 folder?
4. Have you checked to make sure you have included all required elements and outputs in your project report?
5. If you have done an Extension, have you included this information in your report under the Extension heading? Even if you have not done any extensions, include a section in your report where you state this.
6. Have you acknowledged any help you may have received from classmates, your instructor, the TAs, or outside sources (internet, books, videos, etc.)? If you received no help at all, have you indicated that under the Sources heading of the report?
Reviews
There are no reviews yet.