Skip to content

Instantly share code, notes, and snippets.

@dermotbalson
Last active December 22, 2015 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dermotbalson/6539612 to your computer and use it in GitHub Desktop.
Save dermotbalson/6539612 to your computer and use it in GitHub Desktop.
Orbit
displayMode(FULLSCREEN)
supportedOrientations(PORTRAIT)
function setup()
D=100 --diameter of planet
S=120 --speed of planet in pixels per second
R=math.random(300,1000)--radius of orbit
a=0 --starting angle for planet
--set position of centre of sun at bottom
X=WIDTH/2
Y=-100
--calculate planet speed in degrees per redraw
da=6 * S / (2 * math.pi * R)
end
function draw()
background(0)
local x=X+R*math.sin(math.rad(a)) --turn a into radians for use with math.sin
local y=Y+R*math.cos(math.rad(a))
pushStyle()
--draw the sun
fill(255,255,0)
ellipse(X,Y,X+50) -- make the sun peek over the bottom of the screen
--draw the planet
fill(209, 168, 92, 255)
ellipse(x,y,D)
--reverse direction if we hit the sides
if x<D/2 or x>WIDTH-D/2 or y<D/2 then da=-da end
--update the planet angle
a=a+da
fontSize(24)
fill(255)
text("Restart (circular icon at bottom) to see different orbits",WIDTH/2,HEIGHT-100)
popStyle()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment