Last modified: Wednesday, September 26, 2007
| 9/26/2007 |
|
| 9/21/2007 | Program released. The handin directories have not yet been created - watch this space (and your email) for an update when they have been. |
In this assignment you will write a class that simulates the motion of physical objects in freefall near the Earth's surface. Then you will use this class to animate golf balls in flight and a fireworks display. (Don't worry; we take care of most of the animation code, and all the physics you need to know is right in this document!)
You will model physical objects by using the instantiable class Particle; each object from this class represents a different physical object. After implementing and testing the Particle class, you will use it in an application class AnimationPlayer, where you will set up the starting position and velocity of Particle objects in such a way that the golf animations will be holes-in-one. You will create multiple instances of Particle objects to represent the different golf balls and use them with an instance of a class called Animator to make the animation come to life. The fireworks display is also based directly on your Particle implementation but in such a way that you don't actually instantiate any new Particle objects. That is, your Particle class is used to implement a fireworks display but how this is done is completely hidden from your point of view! Finally, you will be required to answer some questions in a separate file.
Your program will have to implement some basic physics to satisfy the requirements of the project. The purpose of this section is to give you the physics background you will need in order to complete this project.
We will be representing the position of physical objects in a 2-dimensional coordinate system. In this system, the
y-axis is vertical ("goes up") and corresonds to "height", whereas the x-axis is horizontal ("goes to the right"). Every physical object has a location which is uniquely determined by a pair of decimal numbers that represent its position with respect to the origin of the coorindate system, just like in algebra. For our purposes, these numbers are always in units of meters. For example, the ball's x-position is -20.24 meters and y-position is 4.37 meters in the figure below
(the ball is white). (Note that the origin is roughly in the middle of the tee box
at the right of the figure. Also note that the "fake 3D" look of the program is just for show; it's really a 2D world, i.e., the tee is literally higher than the green.)

We will also need to deal with velocities. Every physical object has an x-velocity (which represents "how fast the physical object is going in the x-direction") and a y-velocity (which represents "how fast the physical object is going in the y-direction"). The values can be positive or negative. For the y-velocity, positive would indicate that the object is going up, whereas negative indicates that the object is going down. In the case of x-velocity, positive values indicate that the object is going to the right while negative values mean the object is going left (for our coordinate system, anyway). A velocity of zero in either case means that there is currently no motion in that particular direction. We will always use units of meters per second to describe velocities (which basically means "how many meters would it go per second, if it kept going at the current speed"). The following illustration shows the x and y velocities of the ball as "arrows" coming out of it. The (very approximate) path of the ball is also shown for reference. Note that the y-velocity is negative since the ball is going down (also note that these annotations won't be displayed when the program is running).

One final note on terminology: when we just say "velocity" or "position" (without specifying x or y), we mean both x and y components. The main thing we will need for the program is to know an answer to the question: "Given we know the current position and velocity of some physical object, how do we figure out what the position and velocity will be t seconds from now (for some given positive value of t)?".
Suppose that we have a physical object and we know its current x-position px, y-position py, x-velocity vx, and y-velocity vy. We want to find the new position and velocity t seconds from now (we will denote these future position and velocity values as x-position Px, y-position Py, x-velocity Vx, and y-velocity Vy). Then these new position and velocity values are given by the following equations. These equations (based upon a 9.8 m/s/s acceleration down the negative y axis as the given number of seconds of time goes by) take into account gravity near the Earth's surface, as well as the original position and velocity of the object and how much time goes by.
To complete this assignment you will need to write three classes:
The Particle class is an instantiable class representing a single physical object. Different instances of it will be used to represent distinct physical objects. Each Particle object needs to keep track of its x and y position and its x and y velocity. These values should all be doubles. Another thing that the Particle objects need to keep track of is the starting x and y positions and velocities. This will be needed in order to implement the reset() method which resets the position and velocity back to the ones the particle was given when it was first created (i.e., the ones passed to the constructor).
The Particle class must have the following constructor:
The Particle class must have the following public accessor methods, each of which returns the corresponding current (x or y) position or velocity value.
The Particle class must have the following two public mutator methods:
The Particle class must also have the following public method:
| ParticleTester code |
System.out.println("*** Testing Constructor " + "Particle(0.0, 0.0, 2.0, 0.0) ***"); Particle rock = new Particle(0.0, 0.0, 2.0, 0.0); System.out.println("*** Testing print() ***"); rock.print(); System.out.println("*** Testing passTime(1.0) ***"); rock.passTime(1.0); rock.print(); |
|---|---|
| expected.out |
*** Testing Constructor Particle(0.0, 0.0, 2.0, 0.0) *** *** Testing print() *** Particle at (0.0, 0.0) with velocity (2.0, 0.0) *** Testing passTime(1.0) *** Particle at (2.0, -4.9) with velocity (2.0, -9.8) |
There should be a line in your expected.out file for every line printed out by your ParticleTester class.
The Animator class has already been written and is provided to you for use in the AnimationPlayer class. Animator provides methods that let you control which golf animations play (and in what order). Refer to Animator documentation for full details.
You will need to download Animator.jar to the Eclipse folder you are using for programming assignment 1. (You can do this by right-clicking on the link for Animator.jar and then choosing the "Save Link As..." option.) After you have downloaded, you still have one more step to do in Eclipse to get it to recognize the Animator.jar file as one containing Java classes:
AnimationPlayer class is the application that creates an Animator object and tells the Animator object to play animations for holes 1,7, and 9 in that order. The calls should be made in such a way that each animation turns out to be a hole-in-one. Then a fireworks display should be given. In order to do this you will need to study the appropriate methods from the Animator class in the javadocs. The playerName parameter to the constructor needs to be a string that matches your real name (or both of your names, if you are pair programming). Note that you may need to experiment with the method calls to get all the holes-in-one. Also, you may want to try the fireworks first once you think you have your Particle class working before going on to try the golf animations.
After the animations for the holes of golf are played, the AnimationPlayer class should do the following, in the order given here:
Physics simulators are important for achieving realistic physical effects in video games and other simulations. These systems rely on mathematical modeling like the basic particle acceleration we are modeling in the Particle class. In a video game, you might imagine that the physical models are larger and more complex, and the number of Particles is dramatically higher. As a result, it takes increasing amounts of processor power to perform these calculations.
Instead of continually producing chips that simply execute programs faster and faster (as the industry used to do), an important emerging trend in the processor industry has been to increase the number of processors on a chip, so processing work can be done in parallel. The Intel Core 2 Duo is an example of a chip with two processing cores, and chips like these are now standard in new home computers. The Cell processor in the Sony PlayStation 3 is an example of how the video console industry is following the same pattern. However, computer programs have to be designed specifically to take advantage of this parallel processing power.
Consequently, real physics systems, like you would find in video game engines, divide the work up into smaller tasks and have multiple units called "threads" work on different parts simultaneously. In fact, the Animator code in this project is implemented this way. One thread spends all its time handling the physics calculations, and another thread handles drawing the graphics and taking user input. One effect this has in our Animator, or in video games, is to allow the simulation code to do most of the work while the graphics code can keep the frame rate (the rate at which graphics are drawn) relatively constant, meaning that the graphics can appear to the eye to be moving smoothly and continuously, when in fact they're being erased and redrawn very rapidly on the screen.
Understanding the difficult problems and challenges associated with writing multi-threaded applications is a major part of CS 537, Introduction to Operating Systems. Video game design and development is taught in CS 642.
For this assignment you are required to generate javadoc documentation (i.e., the html file) for your Particle class. See the section on Generating documentation in Lab 3 for information on how to use Eclipse to generate Javadoc webpages from your source files. Don't forget to look at your Javadoc documentation before you turn it in to make sure it is what you expect!
Make sure also to follow the CS 302 standards for style as well as commenting.
This section lists several questions that should deepen your understanding of the assignment. Create a text-only file named questions.txt and answer these questions. Note that some of the questions ask you to try things out in code and give or explain the results you get. While you can modify your AnimationPlayer class to do this, it is recommended that you make another Java application class and put the code for you use to answer the questions in this new class.
Be sure to include the descriptive information listed below as well as the answers to each question:
Assignment Number:
Assignment Name:
Date Completed:
Partner 1 Name:
Partner 1 Login:
Partner 1 Lecturer's Name:
Partner 1 Lab Section:
Partner 2 Name:
Partner 2 Login:
Partner 2 Lecturer's Name:
Partner 2 Lab Section:
This section outlines the major requirements of the assignment. Your solution to the assignment must meet each of these requirements. Be sure to read the announcements frequently and ask questions if you need clarification.
Use the Handin program to hand in your work (see the instructions on how to use the Handin program). Students working in pairs will only submit one copy of all program files, except the README file described below.
Only hand in the files listed below; do not hand in any other files. These files may be handed in any time prior to the due date. Note: you may hand in your files before the deadline as many times as you wish. We suggest you use your handin directory to keep your most recent working copy as you develop your solution.
Required files for this assignment