Skip to content

Instantly share code, notes, and snippets.

@dermotbalson
Last active December 17, 2015 03:48
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/5545886 to your computer and use it in GitHub Desktop.
Save dermotbalson/5545886 to your computer and use it in GitHub Desktop.
nav2
-- Navigator
function setup()
x,y=200,200 --starting position
d,a=0,0 --starting velocity / angle
sd,sa=10,5 --speed/angle change per tap
rad=math.pi/180 --constant to convert degrees to radians
end
function draw()
background(220)
pushStyle()
fill(255,255,0,100)
local dx=d*DeltaTime*math.sin(a*rad)
local dy=d*DeltaTime*math.cos(a*rad)
x,y=x+dx,y+dy
pushStyle()
translate(x,y)
rotate(-a)
ellipse(0,0,50,100)
popStyle()
popStyle()
end
function touched(touch)
local t=false --to tell us if touch has been located yet
--change y speed if in outer 1/4 of screen
if touch.y<HEIGHT/4 then d=d-sd t=true
elseif touch.y>HEIGHT*.75 then d=d+sd t=true
end
--change x speed if in top/bottom 1/4 of screen
if touch.x<WIDTH/4 then a=a-sa t=true
elseif touch.x>WIDTH*.75 then a=a+sa t=true
end
--stop moving otherwise
if t==false then d=0 end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment