CS 152: Project 10

Title image Project 10
Fall 2019

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

  1. 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.

    1. Define the __init__ method

      The RotatingBlock.__init__ method should have the following parameters. Only the win parameter should be required.

      ParameterPurpose
      winA reference to a GraphWin object
      x0The x coordinate of the center of the block.
      y0The y coordinate of the center of the block.
      widthThe horizontal extent of the block.
      heightThe vertical extent of the block.
      AxThe x coordinate of the block's center of rotation, with the default value of None.
      AyThe 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.

    2. 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.

    3. 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.

    4. 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.

    5. 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.

  2. 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.

  3. 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

  1. How did inheritance help you on this project?
  2. What is the difference between the position anchor point and the rotational anchor point?
  3. What is the difference between an object field and a local variable in a function?
  4. 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.


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.

  1. Is your name at the top of each code file?
  2. Does every function have a comment or docstring specifying what it does?
  3. 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.