Orbital motion in Python and TurtleArt
Intended to demonstrate two things,
a) that programmable simulations are good ways for kids to learn physics and maths
b) that the programmable block provides a way for kids to move from simple drag and drop programming to more complicated text based programming
As at V18 of experimental TurtleArt, Rainbow is preventing the loading of the file tamyblock.py from the journal. See previous post. The following hack gets round the problem.
go to /home/olpc/Activities/TurtleArtPortfolio.activity
Save your edited tamyblock.py in the Journal immediately before typing
copy-from-journal tamyblock.py(as at v19 this is not required, neither are the 2 imports)
mv tamyblock..py tamyblock.py
# Orbital motion with inverse square law
# Tony Forster, March 2009
# For each iteration, acceleration is calculated
# as inversely proportional to orbital distance.
# The horizontal and vertical components of the
# acceleration are added to the horizontal and
# vertical components of the velocity.
# The horizontal and vertical velocity components are
# added to the x and y coordinates.
# A line is drawn from the old to the new coordinates.
# The turtle is moved.
from taturtle import *
from math import *
def myblock(lc,x): # x is not used
hspeed=lc.heap.pop(-1) # pop hor speed from the heap
vspeed=lc.heap.pop(-1) # pop ver speed from the heap
hspeed -= lc.tw.turtle.xcor*800/(point_distance2*point_distance)
vspeed -= lc.tw.turtle.ycor*800/(point_distance2*point_distance)
ox=lc.tw.turtle.xcor #old x coordinate
oy=lc.tw.turtle.ycor #old y coordinate
nx=ox + hspeed #new x coordinate
ny=oy + vspeed #new y coordinate
setlayer(lc.tw.turtle.spr,630) #make turtle visible
setxy(lc.tw.turtle, nx, ny) #jump to new coord.
lc.heap.append(vspeed) #push vert speed onto heap
lc.heap.append(hspeed) #push hor speed onto heap
What was difficult:
Any syntax error in tamyblock.py will prevent TurtleArt from loading.
The Rainbow problem makes it more complicated, also copy-from-journal is unreliable and poorly documented, I obviously do not understand the syntax.
The error log shows where your error was in the source listing but if you haven't reloaded TurtleArt, you can be running one version of the code and seeing errors in a different version.
The TurtleArt wait block is necessary to see the turtle but it slows execution too much, it could be shortened, i used a hack setlayer setlayer(lc.tw.turtle.spr,630) but don't fully understand.