Bouncing Balls: Handle Events and Discontinuities

Introduction

Model and simulate balls bouncing on the ground or in a box, taking advantage of powerful event handling in Wolfram System Modeler. Two models are used, one where a ball bounces on the ground, and one where multiple balls are constrained in a two-dimensional box.

Explore the available scenarios by evaluating each cell in order. Evaluation is done by selecting a cell and pressing Shift+Enter.

In order to get the full experience of this example, you need the following:

Wolfram System Modeler; a free trial download is available at www.wolfram.com/system-modeler

The model that came in the same bundle as this notebook

For more information about Wolfram System Modeler, visit www.wolfram.com/system-modeler.

Initialize

Define the model names:

mmodel="EducationExamples.Physics.BouncingBalls.BouncingBalls1D";
mmodel2D="EducationExamples.Physics.BouncingBalls.BouncingBalls2D";

1D Model

The one-dimensional model of a bouncing ball illustrates the event handling supported by System Modeler. Below are simulations over ranges of values for the initial height from which the ball is dropped, the gravity of the environment, and the bounciness of the ball.

Define some options for plotting used in all the plots below:

plotOptions={PlotRange->All,PlotStyle->ColorData[3,"ColorList"],ImageSize->Large};

Initial Height

Vary the height the ball is dropped from with the InitialValues option. The parameter for the bounciness of the ball is also set with ParameterValues:

ℳh= SystemModelSimulate[mmodel, {0,8}, <|"InitialValues" -> {"h" -> Range[0.5, 5, 0.5]}, "ParameterValues" -> {"e" -> 0.8}|>] ;

Plot the height of the ball in all the simulated scenarios with a single command:

SystemModelPlot[ℳh,{"h"},plotOptions]

Gravity

Varying the parameter specifying the gravity of the environment is just as simple:

ℳg = SystemModelSimulate[mmodel,{0,8},<|"InitialValues" ->{"h"->5},"ParameterValues" ->{"e"->0.8,"g"->Range[8,13,0.5]}|>];

SystemModelPlot[ℳg,{"h"},plotOptions]

Restitution

Finally, compare how the restitution coefficient (the bounciness) of the ball affects the result:

ℳe=SystemModelSimulate[mmodel,{0,8}, <|"InitialValues" ->{"h"->5},"ParameterValues" ->{"e"->Range[0.5,0.9,0.04]}|>];

SystemModelPlot[ℳe,{"h"},plotOptions]

2D Model

Simulate balls bouncing in a rectangular room, colliding with each other and against the walls. Illustrate the result in an animation using Graphics and Animate.

Set the number of balls you want to place in a room:

nballs=5;

newModel2D = SystemModel[mmodel2D,<|"ParameterValues" ->{"nr"->nballs}|>];

Create a function for distributing the balls nicely at the start of the simulation:

initpos[n_]:=Flatten[Table[{"p["<>ToString[i]<>",1]"->2i-nballs-1,"p["<>ToString[i]<>",2]"->0},{i,1,n}]]

Simulate the bouncing balls with some initial velocities and a given size of the room. Increase the number of interpolation points to improve collision detection:

ℳ2d=SystemModelSimulate[newModel2D,{0,3000},<|"InitialValues"->Join[initpos[nballs],{"v[1,1]"->0.1,"v[1,2]"->0.05,"v[5,1]"->-0.15}],"ParameterValues"->{"wallx"->6,"wally"->10}|>];

Create helper functions to fetch ball positions:

ClearAll[traj,wallx,wally];
traj[n_,t_]:=traj[n,t]=ℳ2d[{"p["<>ToString[n]<>",1]","p["<>ToString[n]<>",2]"},t]
{wallx,wally}=ℳ2d[{"wallx","wally"}];

Create an animation of the balls bouncing in the two-dimensional room:

Animate[
Graphics[{
(* The rectangular room *)
FaceForm[White],Rectangle[{-wallx,-wally},{wallx,wally}],
(* All the balls *)
PointSize[0.5],Sequence@@Flatten[Table[{Hue[i/nballs],Disk[Evaluate@traj[i,\[FormalT]],0.5]},{i,nballs}]/.\[FormalT]->t]
},
(* Size of the plotted area *)PlotRange->{{-wallx-1,wallx+1},{-wally-1,wally+1}}
,Background->Gray]
,{t,0,3000},AnimationRunning->False,AnimationRate->100,SaveDefinitions->True]

Show the start of each trajectory in a parametric plot:

Table[ParametricPlot[Evaluate@traj[i,t],{t,0,2000},PlotRange->{{-wallx-1,wallx+1},{-wally-1,wally+1}},PlotStyle->Hue[i/nballs]],{i,nballs}]

Terms and Conditions of Use

This domain example is an informational resource made freely available by Wolfram Research.

Use of This Example

• You may not use this example for any purpose that is unlawful or dangerous.
• You assume total responsibility and risk for your use of this example.
• You may not present this example with any alteration to its trade dress, trademarks, or design.

License

All content in this bundle is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. By accessing the content or using it in any way, you accept and agree to be bound by the terms of this license. If you do not agree to these Terms of Use, you may not use this content. Wolfram Research reserves the right to change, modify, add to, or remove portions of these Terms of Use at any time without notice. Please refer back to www.wolfram.com for the latest Terms of Use.

A summary of the licensing terms can be found at:
http://creativecommons.org/licenses/by-nc-sa/3.0

The full legal code can be found at: 
http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode