Skip to content

Instantly share code, notes, and snippets.

@zabirauf
Created November 2, 2011 19:27
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 zabirauf/1334627 to your computer and use it in GitHub Desktop.
Save zabirauf/1334627 to your computer and use it in GitHub Desktop.
Codify Particle script. Use single touch to make particles go away from you. Use double touch to attract particles.
p={}
ps=800
touches={}
total=0
-- Use this function to perform your initial setup
function setup()
print("Hello World!")
for i=0,ps do
p[i]= {x=math.random(WIDTH*10)/10,
y=math.random(HEIGHT*10)/10, ox=0.0, oy=0.0, vx=math.random(20)-10,
vy=math.random(20)-10, prevX=0, prevY=0}
p[i].prevX=p[i].vx
p[i].prevY=p[i].vy
end
end
-- This function gets called once every frame
function draw()
--print(ElapsedTime)
noSmooth()
background(10,10,20)
fill(255,0,0)
stroke(255, 255, 255, 255)
strokeWidth(4)
--t={x=CurrentTouch.x , y=CurrentTouch.y}
--if t.x ~= CurrentTouch.prevX and t.y ~= CurrentTouch.prevY then
-- touchy(t,0)
--end
-- print(total)
-- for i=0,ps do
-- p[i].vx=p[i].prevX
-- p[i].vy=p[i].prevY
-- end
if total == 1 then
for k,touch in pairs(touches) do
touchy(touch,1)
end
elseif total == 2 then
t={}
i=0
for k,touch in pairs(touches) do
--touchy(touch,1)
t[i]=touch
i = i + 1
end
endTouch={x=(t[0].x+t[1].x)/2,y=(t[0].y+t[1].y)/2}
touchy(endTouch,0)
--elseif total==3 then
-- for i=0,ps do
-- p[i].prevX=p[i].vx
-- p[i].prevY=p[i].vy
-- p[i].vx=0
-- p[i].vy=0
--end
end
for k,touch in pairs(touches) do
-- Use the touch id as the random seed
math.randomseed(touch.id)
-- This ensures the same fill color is used for the same id
fill(math.random(255),math.random(255),math.random(255))
-- Draw ellipse at touch position
ellipse(touch.x, touch.y, 100, 100)
end
for i=0,ps do
p[i].ox= p[i].x
p[i].oy= p[i].y
p[i].x = p[i].x + p[i].vx
p[i].y = p[i].y + p[i].vy
if p[i].x<0 then
p[i].x=0
p[i].vx= -p[i].vx
end
if p[i].y<0 then
p[i].y=0
p[i].vy= -p[i].vy
end
if p[i].x>WIDTH then
p[i].x=WIDTH
p[i].vx= -p[i].vx
end
if p[i].y>HEIGHT then
p[i].y=HEIGHT
p[i].vy= -p[i].vy
end
p[i].vx = p[i].vx*0.97
p[i].vy = p[i].vy*0.97
stroke(math.random(255),math.random(255),math.random(255),255)
line(p[i].ox, p[i].oy, p[i].x, p[i].y)
end
end
function touchy(t,g)
a=5
for i=0,ps do
d= (p[i].x-t.x)*(p[i].x-t.x) + (p[i].y-t.y)*(p[i].y-t.y)
d= math.sqrt(d)
if g==0 then
p[i].vx = p[i].vx - a/d*(p[i].x-t.x)
p[i].vy = p[i].vy - a/d*(p[i].y-t.y)
else
p[i].vx = p[i].vx + a/d*(p[i].x-t.x)
p[i].vy = p[i].vy + a/d*(p[i].y-t.y)
end
end
end
function touched(touch)
if touch.state == ENDED then
table.remove(touches,touch.id)
touches[touch.id]=nil
if total ~= 0 then
total = total - 1
end
else
if touches[touch.id]== nil then
total = total + 1
end
touches[touch.id] = touch
end
if touch.state == BEGAN then
sound("jump",math.random(255))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment