MotionGenesis: F=ma Software, textbooks, training, consulting. Advanced tutorial: Coin flip with bounce
Dual event handling in MATLABŪ
 

The MotionGenesis file   MGCoinFlip2D.txt   writes
an event-aware MATLABŪ file for a free-flying coin (2D).

With minor modifications (shown below), it detects the instant
one of the two edges of the coin first hits the ground.

MotionGenesis: Coin flip simulation with event handling  
Or try a simpler   soccer-ball bounce
(single event handling in MATLABŪ)
MotionGenesis Bouncing Ball Simulation with event handling
 
MGCoinFlip2D.m
(as produced by MotionGenesis)
MotionGenesis: Coin flip projectile MGCoinFlip2D.jpg
Click to enlarge image
MGCoinFlip2DGroundDetectionStop.m
(minor modification)
MotionGenesis: Coin flip MGCoinFlip2DGroundDetectionStop.jpg
Click to enlarge image
MGCoinFlip2DGroundDetectionBounce.m
(minor modification)
MotionGenesis: Bouncing Ball Simulation results .jpg
Click to enlarge image
 
To modify   MGCoinFlip2D.m   to   MGCoinFlip2DGroundDetectionStop.m
to detect the first instant at which one of the coin's two edges 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 = [  y1,  y2  ];
eventTerminatesIntegration1Otherwise0ToContinue = [  1,  1  ];
eventDirection_AscendingIs1_CrossingIs0_DescendingIsNegative1 = [  -1,  -1  ];

To subsequently modify   MGCoinFlip2DGroundDetectionStop.m   to   MGCoinFlip2DGroundDetectionBounce.m
so the coin bounces with a coefficient of restitution of 0.8, simply change the MATLABŪ lines from:
eventTerminatesIntegration1Otherwise0ToContinue = [  1,  1  ];

%  Put code here to modify how integration continues.
to
eventTerminatesIntegration1Otherwise0ToContinue = [  0,  0  ];

e = 0.8;
if( nIndexOfEvents == 1 )
   ypPostImpact = (r*I*cos(q)*qp+m*r^2*cos(q)^2*yp-e*I*(yp-r*cos(q)*qp))/(I+m*r^2*cos(q)^2);
   qpPostImpact = (I*qp+m*r*cos(q)*yp+m*e*r*cos(q)*(yp-r*cos(q)*qp))/(I+m*r^2*cos(q)^2);
else
   ypPostImpact = (m*r^2*cos(q)^2*yp-r*I*cos(q)*qp-e*I*(yp+r*cos(q)*qp))/(I+m*r^2*cos(q)^2);
   qpPostImpact = (I*qp-m*r*cos(q)*yp-m*e*r*cos(q)*(yp+r*cos(q)*qp))/(I+m*r^2*cos(q)^2);
end
yp = ypPostImpact;
qp = qpPostImpact;