Saint Louis University |
Computer Science P125
|
Dept. of Math & Computer Science |
Please see the general programming webpage for details about the programming environment for this course, guidelines for programming style, and details on electronic submission of assignments.
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.
Please make sure you adhere to the policies on academic integrity in this regard.
The goal of this assignment is to create a new class, Ball, which simulates the movement of a ball under the effects of gravity. For this assignment, we will provide all of the other portions of the code, including a driver for gathering input from the user as well as all of the necessary graphics for visualizing the simulation. However you must define and implement the Ball class properly for the simulation to be complete.
Your main task is to define and implement a class Ball as described here.
How you choose to represent the private aspects of your class is up to you. In some manner, you will have to define data members which can represent the state of a ball, namely its current position and current velocity.
As far as the public interface of your class, you must include each of the following method prototypes, defined precisely as described here. The precision is necessary because your code must be integrated into the remainder of the project.
position
The position of the ball is relative to the world's coordinate system.
For simplicity, you might choose to represent the position vector as
two separate values, separately representing the X- and Y-components of the
position (though you may choose to use the Position class
which we have seen within EzWindows).
velocity
A velocity is not simply a representation speed, but also of direction
of motion. Therefore, a velocity is also represented by a pair of
coordinates.n the real world, a velocity might be measured in units
such as meters per second (m/s); in our world, we will
measure velocity in terms of cm per clock tick. Therefore, if
a ball had a constant velocity of
gravity
By the law of inertia, in the absence of any outside forces
the velocity would remain unchanged. Yet gravity is such an outside
force. At each clock tick, the gravity does not directly effect the
position of a ball, but it will directly effect the velocity of the
ball.
In general, such a force should also be represented as a vector, and thus have separate X- and Y-coordinates. For simplicity, we assume that gravity in our world is aligned with the Y-axis, and that the magnitude of the gravity is a property of the World. A positive value for the gravity means that balls are being pulled downward (i.e., along the positive Y-axis); in contrast, a negative gravity value would be interpreted as balls being pushed upward.
From within the Update(World& W) routine, you may query the given world for the current gravity setting via W's GetGravity() method.
We will be providing you with a simple user interface titled sim. This text-driven menu first directs you through a series of questions to define the characteristics of the World. After setting up the world, the driver prompts you to introduce a ball with a given set of initial characteristics. It then simulates the motion of that ball so long as the ball remains in the visible portion of the world. Once the ball leaves the world it is destroyed and the driver will again prompt you to create a new ball.
You should note that our driver intentionally suggest default responses for each question posed, and you may signify that you are willing to accept the default by simply pressing return. This should make it much easier for you to repeat certain tests by a series of accepted defaults.
We are providing the following files for your use. You may either download from a browser or copy them directly to your current directory on turing with the comand:
cp -Rp ~goldwasser/csp125/programs/simulation .
There are several files:
ball.h, ball.cpp
These are the two files which you will be developing. The
initial files we are providing are relatively vacuous, though
they do contain some necessary syntax to handle the combination
of files.
world.h, world.cpp
These are files we have created to define and implement the
World class. You do not need to read or understand
these files to complete the assignment, though you are welcome
to peruse if you wish as it provides an example of a class
definition and implementation.
sim.cpp
This file defines the front-end driver we are using. There is
really little reason for you to read this file as it is mostly
'grunt work'.
Makefile
You should simply type make at
the command prompt and then your program will be compiled.
Either syntax errors will be displayed, or else the compilation
will be successful and an executable named sim will
be created which you can run.
ball.h, ball.cpp
These two files are the only source code which you will write.
They contain the Ball class definition and implementation,
respectively.
Again, we also ask for you to estimate the amount of time you spent on the assignment, and to let us know of any difficulties you had or other issues you wish to discuss.
If you worked as a pair, please make sure that both names are given and that you discuss how you each contributed to the submitted work.
You may wish to tackle the assignment in stages, making sure to succeed in each stage before continuing to the next.
The original set of files we have provided will not even compile properly. To get to this point, you will have to define your Ball class, so that its public interface precisely matches the specifications described above.
For the ball to appear properly on the screen, you will already need to use instance variables and to successfully implement the methods for setting and getting the Ball's position.
Start to implement the Update routine, ignoring gravity for the time being. You should use the velocity to control the movement of the ball's position.
Finally, take the gravity into account and put everything together.
For the sake of comparison, we have made our solution available for you to execute. You will find it at:
~goldwasser/csp125/demos/simulation/sim
Here is an image based upon the default configuration settings used in the driver:
The assignment is worth 10 points. If you worked as a pair, you will each be given the same grade.
We will award one additional point to students who successfully meet the following challenge. In the required assignment, the gravity of the world was viewed as a constant and was aligned with the Y-axis. For extra credit, we wish to have you adjust your ball implementation so that it reacts properly to the gravity of a star (in addition to the gravity of the World in the standard case).
Physics 102:
Incorporating the gravity of a star is more complicated than the
gravity of a world, for two reasons. First, the gravity of the world
was assumed to be a vector aligned with the Y-axis, thus of the form
(0,g) for some value g. The gravity between the star and a ball
should be a vector pointing from the ball towards the star.
Secondly, the actual strength of the force depends on the distance between the center of
the ball and the center of the star, as well as the mass of the star.
Specifically, the magnitude of the force should be equal to the star's
mass divided by the square of the distance between the star
and ball's centers.
Coding Issues:
There are some additional issues to cope with, though this is extra
credit after all. For simplicity, at most one star can exist in the
world at any given time. You may query information about the world
through the following methods of the World class:
bool HasStar() -- returns true if there is a star
float GetStarX() -- returns the x-coordinate of the star's position
float GetStarY() -- returns the y-coordinate of the star's position
float GetStarMass() -- returns the mass of the star.
For the sake of comparison, here is an image based upon the default extra credit configuration settings used in the driver: