# Does having a lot of physic collisions cause codea crashes?

I know that creating physic bodies in the draw function or functions called in the draw function crashes codea easily, and my code is proof of that, but what I do have is about 25 physic bodies, circles, and a few edges. My code seems to crash when there is too many collisions. Is this common? Will I have to tone down the collisions somehow or is it possible to avoid these crashes?

edit: i found the reason for the frequent crashes but it still runs really slow.

I think we are waiting on an update to fix the slowness. Update was submitted on the 10th.

@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
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

``````

@dave1707 wow that helps

Hi @dave1707, in my case fps drops to 30 when i have only 100 balls or so, moving on. Using meshes i got the same slow down with much fewer objects moving on the screen.
What should be, in normal condition, the limit you expect fps to drop at?

The FPS will depend on the device you’re running this on. On my iPad 1 running version 1.5 of Codea, my frame rate is about 30 with 150 balls. On my iPad Air with version 2.1 of Codea, my frame rate is about 30 with 500 balls. It’s also hard to tell exactly the frame rate and ball count. Maybe I’ll see if I can change the way I do the frame rate, ball count.

ok, thanks @dave1707.
My point, actually is : how do I set my game to run on any device? When shall I stop adding objects on screen? Maybe the game runs fine on Ipad 3 but not on Ipad 2 ( or 1 ).

@deactive Check the device type and set hard limits based on that or look at the frame rate as the game is running and limit the objects as the rate goes down. But from tests that I’ve run, the frame rate can be fine at a certain limit then a couple of seconds later at that limit the ipad completly bogs down and crashes.