Project 10: Collisions with Rotation
The focus on this project is to give you more experience writing child classes, working with equations and code, and integrating user input.
Tasks
- Create a RotatingBlock class
The first task is to create a RotatingBlock class. You can add it to your physics_objects.py file or create a new file; the choice is yours. The class should be called RotatingBlock, and it should inherit from the Thing class, just like Ball, Block classes.
- Define the __init__ method
The RotatingBlock.__init__ method should have the following parameters. Only the win parameter should be required.
Parameter Purpose win A reference to a GraphWin object x0 The x coordinate of the center of the block. y0 The y coordinate of the center of the block. width The horizontal extent of the block. height The vertical extent of the block. Ax The x coordinate of the block's center of rotation, with the default value of None. Ay The y coordinate of the block's center of rotation, with the default value of None. Using the same terminology as for rotating lines, [x0, y0] is the anchor point for positioning the block (i.e., the center of its rectangular shape), whereas [Ax, Ay] refers to the anchor point for the rotation of the block (i.e., the point around which the block rotates). Also, as with the RotatingLine class, if there are no values input for Ax and Ay, we'll use [x0, y0] as the anchor point for the rotation.
As with the Ball class, the first action in the __init__ method is to call the parent Thing.__init__ method. Specify the type as the string "rotating block", and pass in x0 and y0 as the position. Next, assign the parameters width and height to two fields width and height.
Next, create the list of vertex points (self.points), which should be a list of four 2-element lists that represent four (x, y) points. The coordinates of the block should be (-width/2, -height/2), (width/2, -height/2), (width/2, height/2), and (-width/2, height/2).
The remaining unique fields should be identical to the ones you created in the RotatingLine class: angle, rvel, anchor, and drawn. Note that scale and vis are already created in the Thing parent class.
- Write the refresh, get, and set methods
Make the methods refresh, getAngle, setAngle, getAnchor, setAnchor, getRotVelocity, setRotVelocity, and rotate. These should be very similar, if not identical to the RotatingLine class from lab. The refresh method will use the render method (defined next). Any set method that changes the orientation or size of the RotatingBlock needs to call refresh after updating the object's field.
- Write a render method for RotatingBlock
Make the render method. Again, this will be identical to the RotatingLine version except that the last step will be to create a graphics Polygon instead of a Line, using the list of rotated points. At this point, you can run the first test function. You may need to modify the test code, depending on where you wrote your RotatingBlock class. The block should spin around its center.
- Write an update method for RotatingBlock
Create an update method in the RotatingBlock class, since the Thing.update method does not include rotational velocity. The update method needs to take self and dt (time step) as arguments. The update method should first calculate how much the angle changes during the time step (self.rvel * dt) and assign that to a local variable (e.g. da). If the da value is not equal to zero, then call the rotate method, passing in da as the argument. Then, call the update method in the Thing class, passing in self and dt.
Once you have the update method done, try out the second second test function. Since the anchor point is different, the block should appear to rotate around the center of the screen once.
- Test RotatingBlock with collisions
Download the new collision.py file. Copy your collision_router code from last week and update it to include a key for ('ball, 'rotating block'). Then run the third test function.
If you want to incorporate the rotating line in your scene, you can make it collide by giving it a small height and using the ('ball', 'rotating block') collision function.
- Define the __init__ method
- Make a simple obstacle course with rotating objects
Create a very simple obstacle course with one or two rotating objects. Launch one or more balls into the scene.
Create a screen video of your simple obstacle course in action and include a link to it on your wiki.
- Make a scene with user interaction
Create a scene that includes at least one rotating object, at least one instance of each of your other objects, and incorporates some form of user interaction. For example, add paddles to your pinball-style scene from last week that activate when the user hits the space bar. Make something very simple that responds to user input before doing anything more complex.
Create a screen video of your reactive scene in action and include a link to it on your wiki.
Follow-up Questions
- How did inheritance help you on this project?
- What is the difference between the position anchor point and the rotational anchor point?
- What is the difference between an object field and a local variable in a function?
- What is your favorite rotating amusement ride?
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 a second rotating object that consists of multiple graphics objects.
- Create more elaborate spaces with more complex interaction and capture interesting additional simulation videos. Need inspiration? Check out this project.
- Create more shape classes, or more complex shapes, and show that they collide and simulate appropriately.
- This simulation does not take into account masses. A challenging extension would be to modify the collision code so that when two things collide the velocity changes take into account the masses. (Conservation of momentum, energy, and geometry. Feel free to approximate.)
- Create a separate scene like putt-putt golf and let the user shoot the golf ball (see the project linked above).
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 required images/videos 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 10