Bouncing ball simulation

Print, on STDOUT, a pattern which shows in what direction a bouncing ball will take.

The following assumptions are made:

• The ball starts at the top left corner: `0, 0` with zero initial velocity.
• Gravity is `9.8ms^-2` exactly, towards the floor (y positive.)
• The ball weighs `500g` exactly.
• The ball bounces at 45 or 135 degrees to the floor unless you want to add the appropriate calculations to add variable trajectories. (Bonus arbitrary points!)
• The ball has a spring constant coefficient of restitution/bouncyness constant of `0.8` exactly.
• The ball is perfectly spherical and does not deform when it bounces.
• The room is 25 characters high, 130 characters wide. Each x and y is 1 metre and each ball position represents a discrete sample -- the exact time period is deliberately unspecified, but the display should make the ball's path sufficiently clear. The output should show the path of the ball, not just the final position.
• The floor and ball should be indicated using characters on STDOUT, which may be the same. The presence of no ball or floor surface shall be indicated with a space character.
• You are allowed to assume rounding to three decimal places in any calculations. (Solutions using purely integers may be particularly interested in this rule.)
• The simulation stops when either the ball does not move from the floor or it leaves the room (`x > width of area`.)
• The program must simulate the ball's path, not simply load it from a file or have it encoded somehow in the program. The test for this will be to optionally change one of the constants. If the program does not calculate a new, correct result, then it does not qualify.

Example output:

``````*
*
*
*
*
*
*
*
*
*                         ***********
*                    *****           ****
*                 ****                   ***
*               ***                        ***
*              **                             **
*           ***                                 **
*          **                                     **                          *********
*         *                                        **                      ****       ****
*       **                                           *                   **               **
*      *                                              **               **                   **
*     *                                                 *            **                       **              ********
*   **                                                   *          **                         **          ****      ****
*  *                                                      **      **                             **       **            **
* *                                                        **    **                               **    **                **    **
**                                                          **  **                                 **  **                  **  **
*                                                            ****                                   ****                    ***
**********************************************************************************************************************************
``````

Determination of winner. I will accept the answer which:

1. Meets the rules as defined above.
2. Bonus featureness will be considered.
3. Is the shortest and most elegant (subjective decision.)
-
Is the input and/or starting speed (energy?) missing, or am I blind? As I see it now I could just draw the box and say the ball is stationary, or gzip your image and display it on every run. – shiona Jan 22 '13 at 15:13
@shiona The ball starts as if you have just let it go at 0,0. I will update the rules to clarify that actual calculation must take place. – Thomas O Jan 22 '13 at 15:19
but If I decide the ball has the speed of 0.0001 m/s (or whatever small enough) to the direction of 45°, the ball does not visibly leave the ground with your chosen resolution. I think problems should always have some sort of an input (user, random, etc.) to make hard coding a single answer impossible. – shiona Jan 22 '13 at 15:39
Are the stars on the left the y-axis or are they marking the ball's path? If they mark the ball's path, this physics simulations seems a little dubious because the ball has no motion component to the right, so when bouncing on a flat floor, it should bounce back straight up and not to the right. Also, the angles would get flatter on each bounce (if we have a motion component to the right). – Thomas W. Jan 22 '13 at 15:51
Given that the ball is simply dropped, what makes the ball move rightward? Why doesn't it simply move up and down? – DavidC Jan 23 '13 at 16:59

Python 143 bytes

``````u=v=x=y=0
a=[[s]*130for s in' '*24+'*']
while x<129:
if y+v>24:v*=-.8;u=-v
v+=98e-4;x+=u;y+=v;a[int(y)][int(x)]=s
for r in a:print''.join(r)
``````

The resulting curve is slightly different than the example, but this is because the velocity is adjusted before the ball goes into the floor, instead of after it already has.

``````*
*
*
*
*
*
*
*
*
*                     ***************
*                 ****               ****
*               ***                     ***
*             **                           **
*           ***                              **
*         **                                   **                          ******
*        **                                     **                     ****     *****
*       *                                         **                 ***             ***
*     **                                           **              **                  **
*    *                                               *           **                      **            *********
*   *                                                 **        **                        **         ***       ***
*  *                                                   **     **                            **     ***           **        *****
* *                                                     **   **                              **   **               **   ***    ***
**                                                       ** **                                ** **                 ** **
*                                                         ***                                   **                   ***
**********************************************************************************************************************************
``````

Python 132 bytes

A more realistic version, that starts with a constant x velocity:

``````v=x=y=0
a=[[s]*130for s in' '*24+'*']
while x<129:v=(y+v<24or-.8)*v+98e-4;x+=.3;y+=v;a[int(y)][int(x)]=s
for r in a:print''.join(r)
``````

Produces:

``````****
**
***
**
**
**
**
*
*
**                   *********
*                 **        **
*              **            **
**            **              **
*           **                **
**         **                  **                *****
*        **                    **             ***   ****
*       *                      **          **         **
**     *                        *         **            **
*    **                         *       *               **          *******
*    *                          **    **                 **      ***       **
*  *                            *    *                   **    **           **       *******
* **                             *  *                     *   **             **    ***     ***      ****
**                              ***                       * *                ** **          **  ****  ****
**                               **                        **                 ***             ***        *****
**********************************************************************************************************************************
``````
-
Congratulations, it's amazing! – rubik Apr 26 '13 at 10:07

I'll submit my own solution in Python. Only slightly simplified; I'm sure there are much better ways of doing it! 282 280 characters. The example output in the question post was generated using this program.

``````import sys;o=sys.stdout.write
py=px=vy=vx=0;g=98e-4;h=25;w=130;k=.8;p=[]
while 1:
vy+=g;py+=vy;px+=vx
if py>h:vy=-vy*k;vx=-vy
if px>w:break
p.append([int(px),int(py)])
py=0
while py<=h:
px=0
while px<w:
if [px,py] in p or py==h:o('*')
else:o(' ')
px+=1
o('\n');py+=1
``````
-
The way your ball "bounces always 45 degrees" is highly unphysical. The normal and reasonable way to simulate a bouncing ball in 2D is to leave the x-component of the velocity constant and only mirror the y-component (i.e., to account for repulsion perpendicular to the surface but neglect friction tangent to it). You don't need any trigonometric functions or suchlike to do that! – ceased to turn counterclockwis Jan 22 '13 at 18:47