Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Last revision Both sides next revision | ||
2020:genesee_friction_activity_snippets [2020/02/12 06:44] wellerd |
2020:genesee_friction_activity_snippets [2020/02/12 13:39] wellerd |
||
---|---|---|---|
Line 3: | Line 3: | ||
Here is a link to the google doc where you can find more details on the activity: [[https://docs.google.com/document/d/1yGWHwJ3tRL8yq5J3BD2bIPrBk04dghvQxNl029No9xY/edit?usp=sharing | GISD Workday Activity]] | Here is a link to the google doc where you can find more details on the activity: [[https://docs.google.com/document/d/1yGWHwJ3tRL8yq5J3BD2bIPrBk04dghvQxNl029No9xY/edit?usp=sharing | GISD Workday Activity]] | ||
- | Below we have provided some snippets of code you can use by copying into your own glowscript window, modifying the code, and running the program. Any line of code with a # in front of it is simply a comment (or an editor's note) that defines what the line will make the computer do. | + | Below we have provided some snippets of code you can use by copying into your own Glowscript program, modifying the code, and running the program. Any code with a # in front of it is simply a comment (i.e., an editor's note) that defines what the line will make the computer do. The first line of a Glowscript code must always be... |
- | ==== Creating a box ==== | + | <code> |
+ | GlowScript 2.9 VPython | ||
+ | </code> | ||
+ | |||
+ | ==== Objects ==== | ||
Here is some code you can use and modify to create objects like blocks and platforms. | Here is some code you can use and modify to create objects like blocks and platforms. | ||
Line 22: | Line 26: | ||
v = vector(3,0,0) | v = vector(3,0,0) | ||
- | #Define the time variables (initial time, time-step, final time) for the while loop | + | #Define the time variables (initial time, time-step, final time) |
- | t = 0 | + | t = 0 |
dt = 0.01 | dt = 0.01 | ||
tf = 2 | tf = 2 | ||
</code> | </code> | ||
- | Here is some code you can repurpose to add animations to your code. | + | Here is some code you can repurpose to add animations to your code (notice the indentation of lines below the while line). |
<code> | <code> | ||
#While loop to iterate over time until we reach the final time | #While loop to iterate over time until we reach the final time | ||
while t < tf: | while t < tf: | ||
+ | | ||
#Rate keeps animation slow enough to view | #Rate keeps animation slow enough to view | ||
- | rate(100) | + | rate(100) |
- | + | ||
- | #Position update step to predict location of box after a time-step dt base on MyBox.velocity | + | #Position update step to predict location of box after a time-step dt base on v |
MyBox.pos = MyBox.pos + v*dt | MyBox.pos = MyBox.pos + v*dt | ||
| | ||
Line 42: | Line 47: | ||
t = t + dt | t = t + dt | ||
</code> | </code> | ||
- | |||
- | |||
- | ==== Physics ==== | ||
- | |||
- | Here is some code you can repurpose to make objects slow down using friction after defining some variables and calculations. | ||
- | |||
- | <code> | ||
- | #While loop to iterate over time | ||
- | while t < tf: | ||
- | rate(100) #Keeps animation slow enough to view | ||
- | | ||
- | #Velocity update step to predict velocity of box after a time-step dt | ||
- | MyBox.velocity = MyBox.velocity + a*dt | ||
- | | ||
- | #Position update step to predict location of box after a time-step dt | ||
- | MyBox.pos = MyBox.pos + MyBox.velocity*dt | ||
- | | ||
- | t = t + dt #Update the time to go on to the next time-step after the position update | ||
- | </code> | ||
- | |||
==== Graphing ==== | ==== Graphing ==== | ||
- | Here is some code you can use to graph physical quantities in real-time with the animation, such as kinetic energy. | + | Here is some code you can use to graph physical quantities in real-time with the animation, such as position or kinetic energy. |
<code> | <code> | ||
- | GlowScript 2.9 VPython | + | #Create graph object for position |
- | #This snippet of code will plot kinetic energy in a while loop | + | PositionGraph = gcurve(color=color.red) |
- | + | ||
- | #Assign MyBox a velocity, acceleration, and mass | + | |
- | Velocity = vector(5,0,0) | + | |
- | Acceleration = vector(0.5,0,0) | + | |
- | Mass = 50 | + | |
- | + | ||
- | #Set up the time variables for the while loop | + | |
- | t = 0 | + | |
- | dt = 0.01 | + | |
- | tf = 2 | + | |
- | + | ||
- | #Create graph object | + | |
- | Kgraph = gcurve(color=color.red) | + | |
#While loop to iterate over time | #While loop to iterate over time | ||
while t < tf: | while t < tf: | ||
- | rate(100) #Keeps animation slow enough to view | + | |
+ | #Keeps animation slow enough to view | ||
+ | rate(100) | ||
| | ||
#Position update step to predict location of box after a time-step dt | #Position update step to predict location of box after a time-step dt | ||
- | Velocity = Velocity + Acceleration*dt | + | MyBox.pos = MyBox.pos + v*dt |
| | ||
- | #Calculate kinetic energy at this time | + | #Add a data point to our graph for with the position at time t |
- | K = 0.5*Mass*mag(Velocity)**2 | + | PositionGraph.plot(t,MyBox.pos.x) |
| | ||
- | #Add a data point to our graph for the kinetic energy at time t | + | #Update the time to go on to the next time-step after the position update |
- | Kgraph.plot(t,K) | + | t = t + dt |
- | + | ||
- | t = t + dt #Update the time to go on to the next time-step after the position update | + | |
</code> | </code> |