Physics based game runs poorly on devices

Hi,

I have a really big problem -

I’ve created a physics based game that contantly adds and subtracts physical objects in and from a Table.

The project is barely 20mb, everything runs smoothly, but inside the scene with the physical objects, it kills even a nexus7 after some point…

I’m writing down  the main function that handles the physical objects inside the table…

Please, If you see anything wrong here that might indicate a memory problem please write it down - thank you :slight_smile:

function newCrate()

local crate=""

local isPopped=0

local check=math.random(1,4)

    if(check==1) then

    crate=display.newImage(“Photos/crate.png”)

    elseif (check==2) then

    crate=display.newImage(“Photos/crate2.png”)

    elseif(check==3) then

    crate=display.newImage(“Photos/crate3.png”)

    else

    crate=display.newImage(“Photos/crate4.png”)

    end

    

    table.insert(t,crate)

    crate.width = 50

    crate.height= 50

    crate.y=-70

    local px=math.random(50,900)

    crate.x= px

    local rotate=math.random(0,359)

    crate.rotation=rotate

    physics.addBody(crate, “dynamic”, {bounce=0.5})

    local pl=math.random(0.5,2.5)

    crate.linearDamping =pl

        

    local check2=(math.random(1,2))

    if(check2==1) then

    local moveTrans=transition.to(crate,{time=8000, rotation=rotate+360})

    else

    local moveTrans=transition.to(crate,{time=8000, rotation=rotate-360})

    end

        local function reventar(event)

        if(event.object2.class==“ball”) then

            event.object1.fastkill = true

        elseif(event.object1.class==“ball”) then

            event.object2.fastkill = true

        end

    end

    local function suck(obj)

        local good=display.newImage(“Photos/Spiral.png”)

        good.x=obj.x; good.y=obj.y

        ball.ballX(obj)

        local trans2=transition.to(good, {time=300, xScale=0.5,yScale=0.5,alpha=0,rotation=180})

        local trans=transition.to(obj, {time=250, width=0,height=0,x=obj.x, y=obj.y,alpha=0})

        timer.performWithDelay(300,function()

        if isPlaying then

        trans=nil;good:removeSelf(); good=nil;trans2=nil

        end

        end)

    end

local function loop(event)

         for i = #t, 1, -1 do

            local object = t[i]

            

                if(object~=nil) then

                            

                            

                            if(isPaused) then

                            object.isVisible=false

                            else

                            object.isVisible=true

                            end

                            

                            if(slowMo==false) then

                                object.linearDamping=pl;

                            else

                                object.linearDamping=5;

                            end

                            

                                if(object.y~=nil) and object.y>700 then

                                    score.setScore(9999)

                                    local child = table.remove(t, i) 

                                            if child ~= nil then

                                            child:removeSelf()

                                            child = nil

                                            check=nil

                                            isPopped=nil

                                            px=nil;pl=nil;rotate=nil

                                            crate=nil

                                            check2=nil

                                            Runtime:removeEventListener(“enterFrame”, loop)

                                            Runtime:removeEventListener(“collision”,reventar)

                                                if(moveTrans~=nil) then

                                                transition.cancel(moveTrans)

                                                moveTrans=nil

                                                end

                                            end

                    

                                 elseif object.fastkill then

                                        if isDouble then

                                            score.setScore(200)

                                        else

                                            score.setScore(100)

                                        end

                                    if(isPopped~=nil) then

                                    isPopped=isPopped+1

                                    timer.performWithDelay(isPopped*50,function()

                                            local popSound = audio.play( pop )

                                        isPopped=0

                                        end)

                                    end

                                    ball.addOne()

                                    suck(object)

                                    local child = table.remove(t, i) 

                                    timer.performWithDelay(300,function()

                                            if child ~= nil and isPlaying then

                                                child:removeSelf()

                                                child = nil

                                                check=nil

                                                check2=nil

                                                isPopped=nil

                                                crate=nil

                                                px=nil;pl=nil;rotate=nil

                                                popSound=nil

                                                Runtime:removeEventListener(“enterFrame”, loop)

                                                Runtime:removeEventListener(“collision”,reventar)

                                                    if(moveTrans~=nil) then

                                                    transition.cancel(moveTrans)

                                                    moveTrans=nil

                                                    end                                                

                                            end

                                        end)

                                end

                end

        end

end

    

for i=1,#t do

        t[i].id = i

    end

Runtime:addEventListener(“enterFrame”, loop)

Runtime:addEventListener(“collision”,reventar)

end

I’d suggest you first try the memory monitoring feature mentioned in this blog post so that you can determine for cerrtain whether the problem is a memory leak or not: http://www.coronalabs.com/blog/2011/08/15/corona-sdk-memory-leak-prevention-101/.  If it is, then we can go hunting for the root cause.  If it’s not, then it must be some other issue.

(As a separate point, I happened to notice you’re trying to implement a slow motion feature using linearDamping.  linearDamping may give the appearance of a slower motion, but it’s not actually.  What you probably want to use is physics.setTimeScale().)

  • Andrew

From what I tested, i dont seem to have a memory leak…

maybe it’s something to do with the physics objects? or the use of table.insert?

Can’t read the code easily on my mobile (kinda ironic CL :slight_smile: ), however are you creating lots of display objects and physics bodies at runtime?

Hi Dan,

At a quick glance, that’s a lot to be running/checking in a Runtime loop. Try to figure out a more “event based” system where you don’t need to check and configure all of this stuff in a runtime loop. Also, “table.remove” and “table.insert” are not the fastest Lua functions, and you should avoid them if possible (or if you must use them, don’t use them in Runtime, and pre-declare them as locals so you’re not constantly calling the global Lua reference to them).

Here’s a guide on some performance tricks:

http://docs.coronalabs.com/guide/basics/optimization/index.html

Hope this helps,

Brent

table.insert() is a very slow operation.  If you are just adding them at the end, doing:

myTable[#myTable+1] = something – is much faster.

I don’t think that’s even the fastest way.  We did a Tuesday Blog Tutorial on optimizing apps a few months ago.   See:

http://www.coronalabs.com/blog/2013/03/12/performance-optimizations/

thanks guys, i’ll give it a try :slight_smile:

I’d suggest you first try the memory monitoring feature mentioned in this blog post so that you can determine for cerrtain whether the problem is a memory leak or not: http://www.coronalabs.com/blog/2011/08/15/corona-sdk-memory-leak-prevention-101/.  If it is, then we can go hunting for the root cause.  If it’s not, then it must be some other issue.

(As a separate point, I happened to notice you’re trying to implement a slow motion feature using linearDamping.  linearDamping may give the appearance of a slower motion, but it’s not actually.  What you probably want to use is physics.setTimeScale().)

  • Andrew

From what I tested, i dont seem to have a memory leak…

maybe it’s something to do with the physics objects? or the use of table.insert?

Can’t read the code easily on my mobile (kinda ironic CL :slight_smile: ), however are you creating lots of display objects and physics bodies at runtime?

Hi Dan,

At a quick glance, that’s a lot to be running/checking in a Runtime loop. Try to figure out a more “event based” system where you don’t need to check and configure all of this stuff in a runtime loop. Also, “table.remove” and “table.insert” are not the fastest Lua functions, and you should avoid them if possible (or if you must use them, don’t use them in Runtime, and pre-declare them as locals so you’re not constantly calling the global Lua reference to them).

Here’s a guide on some performance tricks:

http://docs.coronalabs.com/guide/basics/optimization/index.html

Hope this helps,

Brent

table.insert() is a very slow operation.  If you are just adding them at the end, doing:

myTable[#myTable+1] = something – is much faster.

I don’t think that’s even the fastest way.  We did a Tuesday Blog Tutorial on optimizing apps a few months ago.   See:

http://www.coronalabs.com/blog/2013/03/12/performance-optimizations/

thanks guys, i’ll give it a try :slight_smile: