Skip to content

Instantly share code, notes, and snippets.

@dermotbalson
Created September 13, 2013 12:03
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/6549795 to your computer and use it in GitHub Desktop.
Save dermotbalson/6549795 to your computer and use it in GitHub Desktop.
Physics test
--# Main2
--Deleting dead objects
displayMode(FULLSCREEN)
function setup()
parameter.integer("BallsPerSecond",10,200,100)
balls={}
timer=0
BallsCreated=0
FPS=60
s=20
physics.gravity(0,-50)
end
function draw()
background(40, 40, 50)
FPS=FPS*.9+0.1/DeltaTime
pushStyle()
timer=timer+DeltaTime
CreateBalls(math.floor(timer*BallsPerSecond)-BallsCreated)
for i,b in pairs(balls) do
fill(255,255,0,100)
ellipse(b.x,b.y,s)
if b.y<0 or b.x<0 or b.x>WIDTH then
b:destroy()
table.remove(balls,i)
end
end
fill(255)
text("Live "..#balls,50,HEIGHT-50)
text("FPS "..string.format("%d",FPS),50,HEIGHT-90)
popStyle()
end
function CreateBalls(n)
for i=1,n do
b=physics.body(CIRCLE,s/2)
b.restitution=.6
b.x=math.random(0,WIDTH)
b.y=HEIGHT+math.random(50,150)
b.linearVelocity=vec2(math.random(-200,200),0)
table.insert(balls,b)
end
BallsCreated = BallsCreated + n
end
--# Main
--Using table to hold dead objects and re-using them
displayMode(FULLSCREEN)
function setup()
parameter.integer("BallsPerSecond",10,200,100)
balls={}
deadBalls={}
timer=0
BallsCreated=0
FPS=60
s=20
physics.gravity(0,-50)
end
function draw()
background(40, 40, 50)
FPS=FPS*.9+0.1/DeltaTime
pushStyle()
timer=timer+DeltaTime
CreateBalls(math.floor(timer*BallsPerSecond)-BallsCreated)
for i,b in pairs(balls) do
fill(255,255,0,100)
ellipse(b.x,b.y,s)
if b.y<0 or b.x<0 or b.x>WIDTH then
table.insert(deadBalls,b)
table.remove(balls,i)
end
end
fill(255)
text("Live "..#balls,50,HEIGHT-50)
text("Dead "..#deadBalls,50,HEIGHT-70)
text("FPS "..string.format("%d",FPS),50,HEIGHT-90)
popStyle()
end
function CreateBalls(n)
for i=1,n do
if deadBalls[1] then
b=deadBalls[1]
table.remove(deadBalls,1)
else
b=physics.body(CIRCLE,s/2)
end
b.restitution=.6
b.x=math.random(0,WIDTH)
b.y=HEIGHT+math.random(50,150)
b.linearVelocity=vec2(math.random(-200,200),0)
table.insert(balls,b)
end
BallsCreated = BallsCreated + n
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment