-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrate.html
More file actions
165 lines (125 loc) · 11.5 KB
/
rate.html
File metadata and controls
165 lines (125 loc) · 11.5 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.18.1: http://docutils.sourceforge.net/" />
<title>Animations — VPython 3.2 documentation</title>
<link rel="stylesheet" type="text/css" href="_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="_static/alabaster.css" />
<script data-url_root="./" id="documentation_options" src="_static/documentation_options.js"></script>
<script src="_static/jquery.js"></script>
<script src="_static/underscore.js"></script>
<script src="_static/_sphinx_javascript_frameworks_compat.js"></script>
<script src="_static/doctools.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="canvas" href="canvas.html" />
<link rel="prev" title="text" href="text.html" />
<link rel="stylesheet" href="_static/custom.css" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
</head><body>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="animations">
<h1>Animations<a class="headerlink" href="#animations" title="Permalink to this heading">¶</a></h1>
<section id="rate">
<h2>rate()<a class="headerlink" href="#rate" title="Permalink to this heading">¶</a></h2>
<dl class="py function">
<dt class="sig sig-object py" id="id0">
<span class="sig-name descname"><span class="pre">rate</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">60</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#id0" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>
<p>The rate(n) statement halts computations, if necessary, long enough to make sure that at least 1/n second has elapsed.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">rate()</span></code> function is an <em>essential</em> part of any animation loop in a VPython program. <code class="docutils literal notranslate"><span class="pre">rate(n)</span></code> does four important things:</p>
<ol class="arabic simple">
<li><p>First, <code class="docutils literal notranslate"><span class="pre">rate(n)</span></code> determines the real-world execution speed of a loop. <code class="docutils literal notranslate"><span class="pre">rate(1)</span></code> will allow the loop to execute no more than once per real-time second. <code class="docutils literal notranslate"><span class="pre">rate(500)</span></code> allows the loop to execute up to 500 times per real-time second. A loop with <code class="docutils literal notranslate"><span class="pre">rate(30)</span></code> will look the same on every computer, regardless of the intrinsic speed of the CPU.</p></li>
<li><p>Second, <code class="docutils literal notranslate"><span class="pre">rate(n)</span></code> makes animations work. It allows VPython time to redraw the display, so objects can move, animations can occur, and graphs can be plotted. When possible, VPython redraws each canvas 60 times per second, but this will not happen at all without a <code class="docutils literal notranslate"><span class="pre">rate()</span></code> statement in a computational loop. Without a <code class="docutils literal notranslate"><span class="pre">rate()</span></code> statement, the loop may update the positions of objects but this won’t be visible because the screen never gets redrawn.</p></li>
<li><p>Third, <code class="docutils literal notranslate"><span class="pre">rate(n)</span></code> allows the processing of mouse and keyboard events. A loop without a <code class="docutils literal notranslate"><span class="pre">rate()</span></code> statement locks these out until it completes.</p></li>
<li><p>Fourth, <code class="docutils literal notranslate"><span class="pre">rate(n)</span></code> makes an infinite loop interruptable. With a <code class="docutils literal notranslate"><span class="pre">rate()</span></code> statement inside a loop, you can break out of an infinite loop by clicking “Edit this program”. Without a <code class="docutils literal notranslate"><span class="pre">rate()</span></code> statement, the program will hang, and the only option is to kill the browser tab.</p></li>
</ol>
<p>A simple animation using <code class="docutils literal notranslate"><span class="pre">rate()</span></code>. If the <code class="docutils literal notranslate"><span class="pre">rate()</span></code> statement is omitted, no motion is seen.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">ball</span> <span class="o">=</span> <span class="n">sphere</span><span class="p">(</span><span class="n">pos</span><span class="o">=</span><span class="n">vec</span><span class="p">(</span><span class="o">-</span><span class="mi">3</span><span class="p">,</span><span class="o">-</span><span class="mi">3</span><span class="p">,</span><span class="mi">0</span><span class="p">),</span> <span class="n">radius</span><span class="o">=</span><span class="mf">0.3</span><span class="p">,</span> <span class="n">color</span><span class="o">=</span><span class="n">color</span><span class="o">.</span><span class="n">yellow</span><span class="p">,</span> <span class="n">make_trail</span> <span class="o">=</span> <span class="kc">True</span><span class="p">)</span>
<span class="n">velocity</span> <span class="o">=</span> <span class="n">vec</span><span class="p">(</span><span class="mf">0.5</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="n">dt</span> <span class="o">=</span> <span class="mf">0.1</span>
<span class="k">while</span> <span class="n">ball</span><span class="o">.</span><span class="n">pos</span><span class="o">.</span><span class="n">y</span> <span class="o"><</span> <span class="mi">3</span><span class="p">:</span>
<span class="n">rate</span><span class="p">(</span><span class="mi">30</span><span class="p">)</span>
<span class="n">ball</span><span class="o">.</span><span class="n">pos</span> <span class="o">=</span> <span class="n">ball</span><span class="o">.</span><span class="n">pos</span> <span class="o">+</span> <span class="n">velocity</span> <span class="o">*</span> <span class="n">dt</span>
</pre></div>
</div>
<section id="make-trail-and-rate">
<h3>make_trail and rate()<a class="headerlink" href="#make-trail-and-rate" title="Permalink to this heading">¶</a></h3>
<p>If a trail’s points are far apart, do not use a rate greater than rate(60). VPython attempts to render the 3D scene about 60 times per second. Each time it renders the scene it looks through a list of all those objects that have specified “make_trail=True”, and if an object’s pos attribute has changed, the render function extends the trail. If you specify rate(300), and you update an object’s position once in each pass through the loop, only every 5th position of the object will be added to the trail, because the scene is rendered only about 60 times per second. In contrast, if you specify rate(10) and you update an object’s position once in each pass through the loop, the trail will be extended 10 times per second.</p>
</section>
</section>
<section id="animation-speed">
<h2>Animation Speed<a class="headerlink" href="#animation-speed" title="Permalink to this heading">¶</a></h2>
<p>Web VPython is intrinsically faster than Python, because it is transpiled to JavaScript, and JavaScript loops are significantly faster than Python loops. However, the addition of Python dictionaries to Web VPython in version 3.2 has led to a slowdown of some Web VPython loops. In most situations this will not be noticeable. However, if your code is loop intensive (for example, if you are animating a large number of particles at once), you may wish to disable dictionaries by changing the first line of your program to:</p>
<p><strong>Web VPython 3.2 nodictionary</strong></p>
</section>
</section>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h1 class="logo"><a href="index.html">VPython</a></h1>
<h3>Navigation</h3>
<p class="caption" role="heading"><span class="caption-text">Contents:</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="objects.html">3D Objects</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">Animations</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#rate">rate()</a></li>
<li class="toctree-l2"><a class="reference internal" href="#animation-speed">Animation Speed</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="canvas.html">Canvases</a></li>
<li class="toctree-l1"><a class="reference internal" href="cameraAndLights.html">Camera and Lighting</a></li>
<li class="toctree-l1"><a class="reference internal" href="colorOpacityGlow.html">Color/Opacity/Glow</a></li>
<li class="toctree-l1"><a class="reference internal" href="makeshapes.html">Design New Objects</a></li>
<li class="toctree-l1"><a class="reference internal" href="file.html">Files/Libraries</a></li>
<li class="toctree-l1"><a class="reference internal" href="graph.html">Graphs</a></li>
<li class="toctree-l1"><a class="reference internal" href="math.html">Math Functions</a></li>
<li class="toctree-l1"><a class="reference internal" href="workwithobjects.html">Object Attachments</a></li>
<li class="toctree-l1"><a class="reference internal" href="rotation.html">Rotation</a></li>
<li class="toctree-l1"><a class="reference internal" href="textoutput.html">Text output</a></li>
<li class="toctree-l1"><a class="reference internal" href="texture.html">Textures</a></li>
<li class="toctree-l1"><a class="reference internal" href="userinput.html">User Input</a></li>
<li class="toctree-l1"><a class="reference internal" href="webvs7.html">VPython/WebVpython</a></li>
<li class="toctree-l1"><a class="reference internal" href="license.html">VPython license</a></li>
<li class="toctree-l1"><a class="reference internal" href="resources.html">Additional Resources</a></li>
</ul>
<div class="relations">
<h3>Related Topics</h3>
<ul>
<li><a href="index.html">Documentation overview</a><ul>
<li>Previous: <a href="text.html" title="previous chapter">text</a></li>
<li>Next: <a href="canvas.html" title="next chapter">canvas</a></li>
</ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>document.getElementById('searchbox').style.display = "block"</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
©2023, Ruth Chabay and Bruce Sherwood.
|
Powered by <a href="http://sphinx-doc.org/">Sphinx 5.0.2</a>
& <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.12</a>
|
<a href="_sources/rate.rst.txt"
rel="nofollow">Page source</a>
</div>
</body>
</html>