Programming Assignment 3

Due: 8pm, Monday 23 February 2004


Contents:


Overview

This assignment is a sequel to the Ball assignment. Our driver has added four walls into the World, and your goal will be to modify your Ball's doTick(World w) method so that the ball will check for potential collisions with the walls, and act accordingly.

Note Well: If you were never able to correctly implement the behavior for the original Ball assignment, please make sure to seek the help of the instructor before attempting to add the additional functionality for the Bounce assignment.

Newly Introduced Techniques


Collaboration Policy

For this assignment, you are allowed to work with one other student if you wish (in fact, we suggest that you do so). If any student wishes to have a partner but has not been able to locate one, please let the instructor know, so that we can match up partners.

In this regard, please make sure you adhere to the policies on academic integrity.


Getting Started

We have placed a template copy of the project, Bounce, in each of your home directories on patel2.slu.edu.

There exists a class BounceDriver for this assignment which is quite similar to the BallDriver for the earlier assignment. It too relies on a class Ball which you must provided. However rather than defining this new class from scratch, you should use your completed class Ball from the earlier assignment as your starting point. The suggested way to do this in BlueJ is to open the Bounce project and then from the "Edit" menu, select "Add Class from File..." and then navigate to select the file "Ball.java" located in your previous Ball project. This has the effect of making a new copy of the file in the Bounce project (while leaving your original copy unchanged in the Ball project).

Next, you must make some modifications to the class Ball. You may notice that in the earlier assignment, the radius of the ball was irrelevant to your class and thus not provided. For this assignment, you must consider the radius of the ball in order to effectively determine when the ball collides with another object. For this reason, you should add a new instance variable to represent the radius of the ball and you should write a new constructor for your class which takes the radius of the ball as a parameter of type double (e.g., a constructor of the form: Ball(double r) )

At this point, you should be able to compile and run the driver. You will notice that the driver for this assignment places four "walls" within the original World. Based on your original code, the ball should move through the world as expected, except that it will continue moving through the walls, since your doTick() routine does not yet consider the presence of walls. Rewriting your doTick() routine accordingly is the primary task of this assignment.


Additional Details

As discussed previously, you will need to know the radius of your ball to simulate collisions. In addition, you will need to understand how the World's walls are represented. We have created a new class Block and added it to the library package csa120.simulation. A block is represented as an axis-aligned rectangle, and the coordinates of the four sides can be queried using the accessor methods: getMinX(), getMaxX(), getMinY(), and getMaxY().

From within your doTick(World w) routine, you will need to query the given World to find references to the four Blocks which represent the walls. This can be done by using four additional methods of class World, getTopWall(), getBottomWall(), getLeftWall(), and getRightWall().

The keys to this assignment will be to:


Physics 103

In modeling the physics, we will assume that our collisions are "elastic" in that no energy is lost. Therefore, the ball's speed (i.e., magnitude of the velocity) should be equal before and after the collision. Of course the direction of the veloicty will have changed. In particular, when the ball hits an edge, its velocity should be reflected symmetrically along the perpendicular between the ball's center and the edge. For example, the following is a time-lapse figure of a ball's trajectory during a collision with a wall:

To simluate the collision properly, you need to determine not only that a collision exists, but specifically the exact location of the ball at the moment of impact. We will let you work on the precise characterization of how the state of the ball should change.


Further Subtleties

Hopefully, after some amount of work, you should be able to get a reasonable simluation of the collisions with the various walls. Yet there are a few additional subtleties that you will want to consider to get a more accurate simulation.

To see some of these occurences in action, try out the following initial settings for a ball:


Submitting Your Assignment

Please see details regarding the submission process from the general programming web page, as well as a discussion of the late policy.

If working as a pair, only one student should submit the project, but please make sure that you clearly document the existence of both authors.


Grading Standards

The assignment is worth 10 points.


Extra Credit

We will award one additional point to students who successfully meet the following challenge.

Earlier in the assignment writeup, we commented on the better approach of writing a single method that compares the ball to a given Block, in evaluating the potential for a collision. Implement a robust such method.

Specifically, our driver allows you to add one additional Block to the world, with coordinates chosen by the user. Select "Block" from the menu which by default reads "Ball" at the right of the program controller, and then click the "Add" button to bring up a dialog box for specifying the Block. From your doTick method, you can access this additional block using the World's getSpecialBlock() method.

Please note that there are several additional complexity for such an interior block.


Michael Goldwasser
Last modified: Wednesday, 25 February 2004