routine BOUNCE given BALL
''
'' COPYRIGHT (C) 1993, CACI Products Company.
'' AUTHOR: Steve Bailey
'' DATE: 9-22-93
'' DESCRIPTION:
''     This routine is called automatically by simscript through the simulation
''     process manager.  Its job is to set variables D.X(BALL), D.Y(BALL), and
''     D.VY(BALL) so that simscript can compute X(BALL), Y(BALL) and VY(BALL).
''

define BALL as a pointer variable
define OTHER.BALL, LOWEST.BALL as a pointer variable
define FLOOR.P, LEFT.P, RIGHT.P as a double variable

'' the lowest ball in the set is always pushing on the spring.  See if this 
'' is the lowest ball
for each OTHER.BALL in BALL.SET,
    compute LOWEST.BALL as the minimum(OTHER.BALL) of Y(OTHER.BALL)

'' if the the given ball is lowest out of all others, and its pushing on the
'' floor, compute the penetration.  Assume the walls are a not a solid bar
'' spring, but more like a cushion
if BALL eq LOWEST.BALL and Y(BALL) lt .FLOOR.POS
    let FLOOR.P = .FLOOR.POS - Y(BALL)
always
let LEFT.P = MAX.F(0, .LEFT.WALL.POS - X(BALL))
let RIGHT.P = MAX.F(0, X(BALL) - .RIGHT.WALL.POS)

'' differential equation for dynamics of moving object with friction
let D.VX = - (EVRN.AIRRES / MASS(BALL)) * VX(BALL) + 
            (.WALL.STIFFNESS / MASS(BALL)) * (LEFT.P - RIGHT.P)
let D.VY = -EVRN.GRAVITY - (EVRN.AIRRES / MASS(BALL)) * VY(BALL) + 
            (EVRN.FLOOR / MASS(BALL)) * FLOOR.P
            
let D.X = VX(BALL)
let D.Y = VY(BALL)

'' bounce ball off the left and right walls to keep in window
''if (X(BALL) lt -46.0 and VX(BALL) lt 0) or
   ''(X(BALL) gt 46.0 and VX(BALL) gt 0)
    ''let VX(BALL) = -VX(BALL)
''always

end
