# Bouncing Ball Animation – Using Events with ODE2

Posted by Jiro Doke,

Jiro's pick this week is "Bouncing Ball: Tutorial for ode45 events" by Matthew Kelly.

Using animation is a great way to convey information. By animating something physical, it has an added effect of tying concepts with experience or intuition. This tutorial by Matthew is about using events with ordinary differential equation solver ode45, or any other ODE solver for that matter. He uses a bouncing ball example to show the effect of events, represented as the act of the ball coming into contact with the ground. Since a ball typically bounces over and over again, he repeatedly calls ode45 with each calculation separated by an event (contact). The simulation takes into account the energy loss when the ball bounces, defined by the coefficient of restitution. Add to that a nice terrain defined by a few sinusoids, then you get a nice animation of the ball bouncing all over the place.

Matthew has documented the code very well, so it's easy to pick up and understand. He's modularized the code so that you can customize it to your needs. For example, he has a single function Set_Parameters that, well... sets your parameters. The function groundHeight defines the terrain, and the function Animate creates the animation.

I added a bit of interactivity to the terrain creation, and here are some interesting terrains I tested.

If you found this entry interesting, Matthew has contributed quite a bit to the File Exchange. Be sure to check it out.

Let us know what you think here or leave a comment for Matthew.

Get the MATLAB code

Published with MATLAB® R2015b

### Note

Allen Li replied on : 1 of 2

This is so cool, I’m sure my daughter will love to explore physics with it when she gets a bit older.

In the meantime, can you share how did you record your terrain creation process? Is there a function in matlab that you can use to record the mouse movement in the window and the window’s responses to it? I want to create some tutorial for using a GUI interface. Many thanks.

Jiro Doke replied on : 2 of 2

Hi Allen,

Your comment slipped through the radar. Sorry about that. Here is the code I used for terrain creation. It’s just raw code with no comments…
For recording mouse clicks, you can use ginput. For something that is more sophisticated, you can use the figure callback function WindowButtonMotionFcn.

function createTerrain
figure
x = [];
y = [];
hPlot = plot(NaN,NaN,'o');
hold on
hPlot2 = plot(NaN,NaN,'k','LineWidth',2);
hold off
axis equal
axis([0 5 0 3])
title('Click points to create terrain. Press ENTER to finish.')
while true
[xP,yP] = ginput(1);
if isempty(xP)
break
end
x = [x;xP];
y = [y;yP];
hPlot.XData = x;
hPlot.YData = y;

if length(x) > 1
x2 = linspace(min(x),max(x),100);
pp = interp1(x,y,'pchip','pp');
y2 = ppval(pp,x2);
else
x2 = x;
y2 = y;
end
hPlot2.XData = x2;
hPlot2.YData = y2;
end
[x,sI] = sort(x);
y = y(sI);
x = [x(1)-1;x;x(end)+1];
y = [y(1);y;y(end)];
x2 = linspace(x(1),x(end),100);
pp = interp1(x,y,'pchip','pp');
y2 = ppval(pp,x2);
hPlot2.XData = x2;
hPlot2.YData = y2;

% Calculate derivative
coeff = arrayfun(@(x)polyder(pp.coefs(x,:)),2:size(pp.coefs,1)-1,'UniformOutput',false);
coeff = vertcat(coeff{:});
ppD = mkpp(pp.breaks,[0 0 0;coeff;0 0 0]);

save ppData pp ppD