-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtute-bounce.html
More file actions
178 lines (158 loc) · 14.3 KB
/
Copy pathtute-bounce.html
File metadata and controls
178 lines (158 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<h1 id="using-vpython-to-simulate-a-ball-bouncing-in-a-box">Using VPython to Simulate a Ball Bouncing in a Box</h1>
<p><strong>Sally Lloyd and Stephen Roberts</strong></p>
<h2 id="overview">Overview</h2>
<p>This tutorial will take you through a 'toy' problem in computational science. You will develop a computer program that simulates the motion of a ball in a box . This program will be written in visual python - a language designed to make the development of this type of physical simulation simple.</p>
<h2 id="what-is-computational-science">What is Computational Science?</h2>
<p>Computational science involves using mathematical models and computer programs to improve our understanding of the way the world works. There are three parts to the process:</p>
<ul>
<li><p>Find a complete mathematical description of the system that will include all the behaviour you are interested in.</p></li>
<li><p>Construct an efficient computer code that solves these equations</p></li>
<li><p>Extract results and predictions that can be compared with real world, with experiments or with other scientific theories.</p></li>
</ul>
<p>These steps are repeated to refine both the computer program and our understanding of the problem.</p>
<h2 id="what-is-vpython">What is VPython?</h2>
<p>VPython is a programming language that is easy to learn and is well suited to creating simple 3D models and visualisations of physical systems. VPython has three components that you will deal with directly:</p>
<ul>
<li><p><a href="http://www.python.org">Python</a>, a programming language invented in 1990 by Guido van Rossem, a Dutch computer scientist. Python is a modern, object-oriented language which is easy to learn.</p></li>
<li><p><a href="http://www.vpython.org">VPython</a>, a 3D graphics module for Python created by David Scherer while he was a student at Carnegie Mellon University. VPython allows you to create and animate 3D objects, and to navigate around in a 3D scene by spinning and zooming, using the mouse.</p></li>
<li><p>IDLE, an interactive editing environment, written by van Rossem and modified by Scherer, which allows you to enter computer code, try your program, and get information about your program.</p></li>
</ul>
<p>This tutorial starts with a cut down version of the 'standard' VPython tutorial. The tutorial assumes that Python and Visual are installed on the computer you are using. If you need to install any of these, follow this link to the <a href="http://www.vpython.org/">VPython web page</a></p>
<h1 id="introduction-to-visual-python-simple-motion">Introduction to Visual python: Simple motion</h1>
<p>The first section of the tutorial introduces you to programming in visual python by stepping through a program to display a ball bouncing around inside a box. In the next tutorial this program will be modified to become a simulation of an ideal gas.</p>
<h2 id="your-first-program">Your First Program</h2>
<p>Start VPython or IDLE (an editor for creating Python computer code).</p>
<p>If you are using a windows machine this will probably be possible via the start menu.</p>
<p>If you are working on a Unix (Linux) computer, then type 'vpython' at a prompt on a shell window. This will bring up an interactive Python window. You then need to select New Window from the File menu of this first Python window.</p>
<p>Vpython will demand that you save a copy of your working file somewhere before it will execute the program. Save it in your present working directory which will probably be /student/<username></p>
<p>A window labelled 'Untitled' should appear. This is the window in which you will type your program. Type the following statements (You can select the text below and then paste it into IDLE.):</p>
<pre><code>#########################################
# Import the visual library
#########################################
from visual import *
##########################################
# Create Wall(s)
##########################################
wallR = box(pos=vector(6,0,0), length=0.2, height=4, width=4, color = color.red)
##########################################
# Create Ball(s)
##########################################
ball = sphere(pos=vector(-5,0,0), radius=1.0, color=color.green)</code></pre>
<p>IDLE will colour different sections of the program to make it easier to read. For example comment lines, which begin with the # symbol are red. The functions sphere and box are ’constructors’ for 3D objects. The position of an object’s centre is specified in three dimensions using a vector.</p>
<p>Of course there are only 3 lines of actual code. I have added the comments for two reason,</p>
<ol style="list-style-type: decimal">
<li><p>to tell you what the code does,</p></li>
<li><p>to designate regions of your code, so that as the code grows it will be easier to describe where to act new features.</p></li>
</ol>
<h2 id="running-the-program">Running the Program</h2>
<p>Now run your program by choosing 'Run program' from the 'Run' menu. When you run the program, two new windows appear. There is a window titled 'VPython,' in which you should see a green sphere and a red rectangle (wall), and another window titled 'Output'.</p>
<p>In the VPython window, hold down the middle mouse button and move the mouse. You should see that you are able to zoom into and out of the scene. Now try holding down the right mouse button . You should find that you are able to rotate your view of the scene.</p>
<p>You can stop the program by choosing 'Stop program' from the run menu (or closing the graphics window).</p>
<p>You might want to play with changing the radius of the ball, the colour of the ball etc. Make the changes in the code and run the updated program using run command.</p>
<h2 id="objects-and-attributes">Objects and attributes</h2>
<p>Sphere and box are types of objects that visual python recognises and displays. They have some attributes associated with them (such as position(pos), colour(color) and radius) which can be set when you first define and name the object, otherwise the default values will be used.</p>
<p>You can change the radius attribute of ball after it has been constructed with the statement:</p>
<pre><code>ball.radius=0.4</code></pre>
<p>You can also add any new attributes you want to associate with the object, eg:</p>
<pre><code>ball.velocity=vector(2,.1,0)</code></pre>
<h2 id="moving-objects">Moving Objects</h2>
<p>To move objects you will need a program loop that repeatedly updates the position. This can be done with a while loop that repeats the indented lines of code following the while statement, as long as the while condition remains true.</p>
<p>Add the following assignments and while loop to the bottom of your program. The setting of the ball velocity should be part of the 'Create Ball(s)' part of the code. The ball should be able to move according to the velocity specified.</p>
<pre><code>ball.velocity=vector(2,.1,0)
##########################################
# Time loop for moving Ball(s)
###########################################
timestep=0.05
while (1==1):
# Set number of times loop is repeated per second
rate(100)
# Move ball(s)
ball.pos=ball.pos + ball.velocity*timestep</code></pre>
<p>The symbol <code>=</code> is used for assignment, to set a new value for a variable. In Python <code>==</code> stands for 'is equal to' so the statement <code>1==1</code> always has a value of true, and code inside the while loop will repeat forever. Loops are a little strange in Python. Most other languages use some code for the end of a loop, say a bracket or endwhile. Not in Python, the indentation defines the extent of the loop. So you are forced into writing indented code. Other control commands like <code>for</code> and <code>if</code> also work using indentation.</p>
<p>In the above example the value of <code>ball.pos + ball.velocity*timestep</code> is calculated and then assigned as the new value for ball.pos. Python knows how to add vectors and how to multiply them by a scalar so you don’t have to specify this element by element.</p>
<p>The rate statement specifies the number of times the loop will be executed per second. It allows you to control the animation speed so that the ball doesn't move too fast on fast computers.</p>
<p>Run your program by choosing 'Run program' from the 'Run' menu. You should observe that the ball moves to the right. You can change how fast either by changing the rate or the timestep.</p>
<p>Note that VPython tries to scale the view so that all the objects can be seen. So as the ball moves to the right, the camera has to pan back. This makes the wall look like it is moving backwards.</p>
<h2 id="making-the-ball-bounce-logical-tests">Making the ball bounce: Logical tests</h2>
<p>The ball goes straight through the wall. To make the ball bounce off the wall, we need to detect a collision between the ball and the wall. A simple approach is to compare the x coordinate of the ball to the x coordinate of the wall, and reverse the x component of the ball’s velocity if the ball has moved too far to the right. The components of vectors are attributes which can be referred to individually as .x, .y and .z. The logical test we would use to detect a collision and reverse the velocity might look like:</p>
<pre><code> #check for collisions with the wall(s)
#right wall
if ball.x > wallR.x:
ball.velocity.x = -ball.velocity.x</code></pre>
<p>The indented line after the if statement will be executed only if the logical test in the previous line gives 'true' for the comparison. If the result of the logical test is false (that is, if the x coordinate of the ball is not greater than the x coordinate of the wall), the indented line will be skipped. Since we want this logical test to be performed every time the ball is moved, we need put these lines in the while loop. We add these lines to the end of the code and indent them both so they become part of the while loop. Your program should now look like this:</p>
<pre><code>#########################################
# Import the visual library
#########################################
from visual import *
##########################################
# Create Wall(s)
##########################################
wallR = box(pos=vector(6,0,0), length=0.2, height=4, width=4, color=color.red)
##########################################
# Create Ball(s)
##########################################
ball = sphere(pos=vector(-5,0,0), radius=1.0, color=color.green)
ball.velocity=vector(2,.1,0)
##########################################
# Time loop for moving Ball(s)
###########################################
timestep=0.05
while (1==1):
# Set number of times loop is repeated per second
rate(100)
# Move ball(s)
ball.pos=ball.pos + ball.velocity*timestep
#check for collisions with the wall(s)
#right wall
if ball.x > wallR.x:
ball.velocity.x = -ball.velocity.x</code></pre>
<p>Run your program. You should observe that the ball moves to the right, bounces off the wall, and then moves to the left, continuing off into space. Note that our test is not very sophisticated. <code>ball.x</code> is at the centre of the ball and <code>wallR.x</code> is at the centre of the wall so the ball penetrates the wall before it bounces.</p>
<p>Adjust the collision test so that the ball bounces when the edge of the ball reaches the edge of the wall (ie when <code>ball.x > wallR.x-wallR.size.x/2- ball.radius</code>). Replace the test in the if statement with this new statement.</p>
<p>For a more realistic bounce you should reflect the position of the ball as well as its velocity.</p>
<p>You can add another wall at the left side of the display, and make the ball bounce off that wall also.</p>
<p>We have hard coded in a number of parameters such as the radius of the ball, the thickness and position of the walls. It is often better to define variables to hold the value of these types of parameters. In the following I have set a number of parameters early in the code and then used them throughout.</p>
<p>With these new variables, <code>side</code>, <code>thk</code>, <code>ball_radius</code>, <code>maxpos</code>, <code>maxv</code>, your program should now look something like the following:</p>
<pre><code>#########################################
# Import the library(s)
#########################################
from visual import *
##########################################
# Create Wall(s)
##########################################
side=4.0
thk=0.3
wallR = box (pos=vector( side, 0, 0), length=thk,
height=2*side, width=2*side, color = color.red)
wallL = box (pos=vector(-side, 0, 0), length=thk,
height=2*side, width=2*side, color = color.red)
##########################################
# Create Ball(s)
##########################################
ball_radius=1.0
maxpos=side-thk/2-ball_radius
maxv=2.0
ball = sphere(color = color.green, radius = ball_radius)
ball.velocity = vector(2,.1,0)
##########################################
# Time loop for moving Ball(s)
###########################################
timestep = 0.05
while (1==1):
# Set number of times loop is repeated per second
rate(100)
# Move ball(s)
ball.pos = ball.pos + ball.velocity*timestep
#check for collisions with the walls
#right wall
if ball.pos.x > maxpos:
ball.velocity.x = -ball.velocity.x #reflect velocity
ball.pos.x=2*maxpos-ball.pos.x #reflect position
#left wall
if ball.pos.x < -maxpos:
ball.velocity.x = -ball.velocity.x
ball.pos.x=-2*maxpos-ball.pos.x</code></pre>
<p>This program makes a ball bounce backward and forward between two parallel walls. Notice that the ball has a little upward (y) velocity (the z direction is out of the screen). The ball continues to bounce even when it has passed the top of the walls.</p>
<p>You should add extra walls now so that the ball bounces inside a box. You also need to extend the walls so they touch so that the simulation looks nice. You will want to have an invisible front wall so that you can see inside. Play around with different starting positions and velocities for the ball, and sizes for the ball and box.</p>
<h1 id="conclusion">Conclusion</h1>
<p>If you have had trouble, or have run out of time, <a href="bounce2.py">here is the code for the bouncing ball in a box</a></p>
<p>Using this bouncing ball program as a base, it is possible to create a simulation of a gas. Have a look at our <a href="tute-gas.html">gas simulation tutorial</a></p>