Physics Engine Dragging

Greetings! I have a fairly large project that has been experiencing “mysterious” bouts of a laggy physics engine, causeing choppy movement. There are no more than 3 physics bodies at one time and I’m conviced that they are being created and destroyed correctly. It comes and goes suddenly and without noticlable pattern. The FPS stays at dead 60 and I have physics.countinous set to true, which helped the problem.

What kinds of things can make the physiscs engine run slower besides sheer nunber of bodies?

Look in the reference under Physics, then physics.body. Interpolate is explained there.

@dave1707 Ahh, ok. So there is a down side, but I dont think its to major.

@dave1707 interpolate helps a lot if not removes the problem. It does’nt seem to be documented, what does it do?

I’ve seen that occur on simple physics examples myself. I think it started with version 2.0 and I might have mentioned it back then. I’ll see if I can find it.

My project “fake hacked ipad” is using physics with ~30 objects, i have noticed it is choppy sometimes, but i though it was because 30 objects… but maybe some other reason?

@Goatboy76 I’m pretty sure Box2D’s solver runs on O(n^2) so 4 objects all colliding with multiple (3 collisions) means you have 12 collisions overall not including the ones with the floor. If you have 4 objects colliding with a singular collision (one colliding with the next) you only have 3 collisions overall not including the ground collisions. This is probably not the reason but is an explanation why lag increases so much when you make a ball pit. I’ve noticed the drawing of Codea can stay at 60fps while box2D lags behind creating a stuttering effect, but this is a guess.

@Goatboy76 Is this like what you’re talking about. Hit the replay icon to keep restarting the program when the ball goes off the screen. On my iPad Air, some times the ball moves up and down in a smooth motion. Other times it’s jumpy going up, and smooth coming down, or both jumpy going up and jumpy coming down. I think maybe the 3 display screens are not quite in sync.


function setup()
    physics.continuous=true
    b=physics.body(CIRCLE,20)
    b.x=300
    b.y=300
    b.linearVelocity=vec2(0,600)
end

function draw()
    background(40, 40, 50)
    fill(255)
    ellipse(b.x,b.y,40)
end

I would try using the collectgarbage command after deleting physics objects, to make sure they are destroyed promptly

@Ignatz In my example I only have the one physics object, but it still runs jumpy every now and then.

@dave1707 That example demonstrates my problem.

@Ignatz I’ll try that.

@Luatee Is Box2D the physics engine that codea uses? I forgot about the wall colisions but I agree that its probely not the problem.

yes, Box2D

@Goatboy76 Try adding the line of code " .interpolate=true" on each object that is jumpy. Try my example below several times with and without the interpolate line.


function setup()
    physics.continuous=true
    b=physics.body(CIRCLE,20)
    b.x=300
    b.y=300
    b.interpolate=true     -- run with and without this line
    b.linearVelocity=vec2(0,600)
end

function draw()
    background(40, 40, 50)
    fill(255)
    ellipse(b.x,b.y,40)
end