MotionGenesis: F=ma Software, textbooks, training, consulting. Advanced tutorial: Bouncing ball
Event handling in MATLABŪ
 
The MotionGenesis file   BouncingBallWithGroundDetection.txt
writes an event-aware MATLABŪ file for a free-flying soccer ball.

With minor modifications (shown below), it detects the instant
the ball first hits ground and/or simulates a bouncing ball.

MotionGenesis Bouncing Ball Simulation with event handling  
Or try a more difficult   coin-flip with bounce
(dual event handling in MATLABŪ)
MotionGenesis: Coin flip simulation with event handling
 
BouncingBallNoGroundDetection.m
(as produced by MotionGenesis)
MotionGenesis: Soccer ball projectile motion results .jpg
Click to enlarge image
BouncingBallGroundDetectionStop.m
(minor modification)
MotionGenesis: BouncingBallGroundDetectionStop.jpg
Click to enlarge image
BouncingBallGroundDetectionBounce.m
(minor modification)
MotionGenesis: BouncingBallGroundDetectionBounce.jpg
Click to enlarge image
 
To modify   BouncingBallNoGroundDetection.m   to   BouncingBallGroundDetectionStop.m
to detect the first instant at which the ball strikes the ground, simply change the MATLABŪ lines from:
% mdlDerivatives( t, VAR, uSimulink );        % UNCOMMENT FOR EVENT HANDLING
% mdlOutputs(     t, VAR, uSimulink );        % UNCOMMENT FOR EVENT HANDLING
functionsToEvaluateForEvent = [];
eventTerminatesIntegration1Otherwise0ToContinue = [];
eventDirection_AscendingIs1_CrossingIs0_DescendingIsNegative1 = [];
to
mdlDerivatives( t, VAR, uSimulink );
mdlOutputs(     t, VAR, uSimulink );
functionsToEvaluateForEvent = [ y ];
eventTerminatesIntegration1Otherwise0ToContinue = [ 1 ];
eventDirection_AscendingIs1_CrossingIs0_DescendingIsNegative1 = [ -1 ];
 
To subsequently modify   BouncingBallGroundDetectionStop.m   to   BouncingBallGroundDetectionBounce.m
so the ball bounces with a coefficient of restitution of 0.7, simply change the MATLABŪ lines from:
eventTerminatesIntegration1Otherwise0ToContinue = [ 1 ];
%  Put code here to modify how integration continues.
to
eventTerminatesIntegration1Otherwise0ToContinue = [ 0 ];
yp = -0.7 * yp;