Motion Simulation ------ ---------- Preface ------- You are to simulate a ball moving in a box under the force of gravity and completely elastic collisions. Let the ball's position be given by the coordinates (x,y), its velocity by (vx, vy), and its acceleration by (ax, ay). Then the equations for updating the position and velocity of the ball, in the absence of collisions, are: new x = x + vx * dt new y = y + vy * dt new vx = vx + ax * dt new vy = vy + ay * dt where the initial values of x, y, vx, and vy are for some time t, and the new values are for some time t + dt, where dt is a small time. We will assume that the accelerations ax and ay are constants, independent of time and space. The equations just given can be used to compute in the absence of collisions. Our ball is in a box, and can collide with any of 4 walls, bottom, left, right, and top. When a real ball collides with a real wall, some of the energy goes into changing the molecular structure of the ball and the wall. However, we will assume that no energy is lost this way, so we have what physicists call perfectly elastic collisions. In a perfectly elastic collision, when the ball hits the wall, the component of velocity perpendicular to the wall reverses direction, while the position of the ball, on the surface of the wall, stays constant. Our walls are positioned at x == 0, x == w, y == 0, and y == h, where w is the width of the box, and h is the height of the box. After computing new x, new y, new vx, and new vy above, but BEFORE changing x, y, vx, or vy, we will execute the code: if ( new x is outside the range 0 .. w or new y is outside the range 0 .. h ) { if ( new x is outside the range 0 .. w ) new vx = -vx else new vx = vx if ( new y is outside the range 0 .. h ) new vy = -vy else new vy = vy new x = x new y = y new t = t + dt } In other words, if the ball would move outside the box, we do NOT change its position, and we do not change its velocity except that we reverse the sign (direction) of the velocity components in the direction of the position components that were going to be outside the allowed range. One or both velocities can be reversed. All these equations produce a computation that is only approximate. It becomes more accurate as dt gets smaller. If we could make dt infinitesimally small, then we would get the same accuracy as the mathematicians do when they use calculus. Problem ------- You are given initial values for x, y, vx, and vy at time t == 0. You are given constant values for ax, ay, and dt. You are given the width w and height h of the box containing the ball. The ball is inside the box if and only if: 0 <= x <= w 0 <= y <= h You are given a time `et' to stop the computation, which is to run from t == 0 to t == et. You are given a print interval, pt, such that after every pt units of time you output something. What you output is a mark in a 50 x 50 character graph. The mark is initially `A'. Every time the ball bounces, the mark is changed to the next consecutive letter of the alphabet: i.e. to `B', `C', `D', etc. Input ----- The input consists of an number of runs. Each run consists of the 11 floating point numbers: x y vx vy ax ay w h dt pt et in the indicated order on a single line. Input ends at an end of file. Output ------ The output of a run consists of 53 lines. Th first line contains `Run #' where # is the number of the run, chosen from 1, 2, 3, etc. The second line consists of 52 `-' characters (minus sign, NOT underbar). The next 50 lines each consist of 50 graph points sur- rounded by `|' characters, for a total of 52 characters per line. Each graph point is a single character. It is initial- ized to blank, and may be set to `A', `B', etc. as indi- cated below. The last line consists of 52 `-' characters, like the second line. There are no SPACE or TAB characters in any output line, other than graph points that are SPACEs and a single SPACE after `Run' in the first line. The computation starts at t == 0 with initial x, y, vx, and vy. The computation proceeds through round ( et / dt ) steps, as indicated above. At the end of every round ( pt / dt ) 'th step, a mark is made. The values of x and y at the END of the step are used to make the mark. The mark is made in the graph point with coordinate: floor ( ( x/w ) * 50 ) floor ( ( y/h ) * 50 ) where the graph point coordinates range from (0,0) in the LOWER LEFT to (49,49) in the UPPER RIGHT. Previous marks at a position are overwritten. The mark is initially set to `A'. Every time the ball bounces, the mark is set to the next capital letter. You may assume the mark will NEVER progress past `Z'. NO MARK is made for t == 0. The first mark is made after the round ( pt / dt ) 'th step. The output must be PRECISELY as described. To avoid ambiguities you MUST use double precision floating point numbers for computations. The input is chosen so that `floor' and `round' and inequality tests produce unambiguous results even if computations have small rounding errors. This can be done, as in the example, by adding small amounts to x, y, vx, and vy. Comments -------- The first run in the example input below is for gravity of -32 feet per second per second in the y direction, where the ball is dropped from 100 feet, and the marks are every 0.2 seconds. An x velocity of 25 feet per second is given to the ball initially. dt = 0.1. Small amounts are added to x, vx, and vy to avoid ambiguities. The ball appears to gain energy. This is an artifact of the approximate nature of our equations and a large dt. Because of the approximate nature of our equations, the gravity appears to be weaker when the ball is going up than when the ball is going down. The apparent energy gain disappears at dt = 0.01, as indicated by the second run below. If you drop a ball from 16 feet with normal gravity, you can use this program to discover that the ball will hit the ground with a speed of 32 feet per second, which happens to be about 22 miles per hour. Gravity does the same to you as to a ball, so if you jump from 16 feet, you hit at 22 mph. If you jump from 4*16 = 64 feet you hit at 44 mph; and from 9*16=144 feet, 66 mph. So if your car hits a tree at 66 mph, you have just done the equivalent of jumping off a 144 foot tall building. Example Input ------- ----- 0.001 100 25.001 0.001 0 -32 200 120 0.1 0.2 15 0.001 100 25.001 0.001 0 -32 200 120 0.01 0.2 15 0.001 100 25.001 50.001 0 0 200 200 0.01 0.2 25 Example Output for the First Run Above Only ------- ------ --- --- ----- --- ----- ---- Run 1 ---------------------------------------------------- ( 19 lines deleted here so output fits on page ) | | | D | | D D DD | | D D | | E DBB BB D | | E B B D | | AA B D B | | A B D | | A E D B | | A B D | | E D B | | A B D | | D B | | A E B | | B D | | A D | | E B | | B D | | A D | | E B | | B D | | A | | B D | | E | | A B D| | D C | | B | | E | | A B | | B D | | C | ---------------------------------------------------- File: motion.txt Author: Bob Walton Date: Tue Sep 3 11:04:45 EDT 2002 The authors have placed this file in the public domain; they make no warranty and accept no liability for this file. RCS Info (may not be true date or author): $Author: hc3 $ $Date: 2002/09/03 15:05:02 $ $RCSfile: motion.txt,v $ $Revision: 1.4 $