@jrohanian Try running this and see what your frame rate is. The frame rate is at the left side and its height will decrease as the frame rate decreases. Each time the red ball collides with a blue ball, another blue ball is created. The ball count is in the center. Let this run and see what the frame rate is and the ball count. How does this compare to the speed of you’re program. My frame rate drops to about 30 when the ball count reaches 500. With several hundred balls, the collision rate is extreamly high.
displayMode(FULLSCREEN)
function setup()
physics.continuous=true
tab={} -- table for balls
for x=1,25 do -- start with 5 balls
create()
end
line1 = physics.body(EDGE,vec2(5,5),vec2(5,HEIGHT-5))
line2 = physics.body(EDGE,vec2(WIDTH-5,5),vec2(WIDTH-5,HEIGHT-5))
line3 = physics.body(EDGE,vec2(5,5),vec2(WIDTH-5,5))
line4 = physics.body(EDGE,vec2(5,HEIGHT-5),vec2(WIDTH-5,HEIGHT-5))
end
function create() -- create a new ball in the table
local a=#tab+1
tab[a] = physics.body(CIRCLE,10)
tab[a].x=math.random(30,WIDTH-30)
tab[a].y=math.random(30,HEIGHT-30)
tab[a].friction=0
tab[a].gravityScale=0
tab[a].restitution=1
tab[a].linearVelocity=vec2(math.random(-400,400),math.random(-400,400))
end
function collide(contact)
if contact.state == BEGAN then
-- if the red ball is hit, create another ball and sound
if contact.bodyA.x == tab[1].x and
contact.bodyA.y == tab[1].y then
create()
end
end
end
function draw()
background(50, 50, 50)
fill(255)
text(#tab,WIDTH/2,HEIGHT/2+20)
strokeWidth(0)
for z=1,#tab do
fill(0, 47, 200, 255) -- color of the blue balls
if z == 1 then -- color of the red ball
fill(255,0,0)
end
ellipse(tab[z].x,tab[z].y,20,20)
end
stroke(255)
strokeWidth(5)
line(5,5,5,HEIGHT-5)
line(WIDTH-5,5,WIDTH-5,HEIGHT-5)
line(5,5,WIDTH-5,5)
line(5,HEIGHT-5,WIDTH-5,HEIGHT-5)
fill(255)
text(string.format("%d",1/DeltaTime),50,(1/DeltaTime)*15)
end