Physics cpu overload problem

Hi,

when I call my scene with the code 

local physics = require(“physics”)

physics.start()

physics.setGravity( gravityX,gravityY )

physics.setScale( 60 )

physics.setPositionIterations( 50 )

physics.stop()

physics = nil

for many times the app slows down

I remove all body objects when i destroy the scene

Do you know why this happens?

Thanks
Marco
 

Are you removing the scene with the removeScene() method?

https://docs.coronalabs.com/api/library/composer/removeScene.html

I use a structure with main.as that loads with
content:insert(<< PAGE >>)
to have custom transition

PAGE CODE------------------------------

module(…, package.seeall) --gioco

local clip

function creaPage()

  clip = display.newGroup()

  local physics = require(“physics”)

  physics.start()

  physics.setGravity( gravityX,gravityY ) 

  physics.setScale( 60 )

  physics.setPositionIterations( 50 )

 

 

  <<PAGE CODE>>

 

  function destroyAll()

    <<DESTROY ALL ELEMENT>>

    physics.stop()

    physics = nil

 

  end

end

destroyAll() is called when the page is destroied
content:remove(<< PAGE >>)

First of all, you should know that module(…, package.seeall) is deprecated and not recommended since it can cause memory leak.

Are you using composer API? - https://docs.coronalabs.com/api/library/composer/index.html

If not, is there a reason for it? Also, what does content:remove() do?

Thanks

yes for module(…, package.seeall) but I used this because the project is old
 

sorry, content:remove(<< PAGE >>) this is a my write error
when I destroy set MYPAGE as nil

What are you using for scene management? Are you using Director?

To call and destroy the page I use this function in main.lua:

local pageView

local destroyPage = function()

  --Destroy my old page

  pageView:destroyAll()

  pageView = nil

end

local callPage= function()

  --call and open the page 

  if pageView~=nil then destroyPage() end

  pageView = require(_G.myActualPage)

  pageView.creaPage()

end

with this code in page.lua:

module(…, package.seeall) --gioco

local clip

function creaPage()

  clip = display.newGroup()

  function destroyAll()

  end

end

I see. Without any scene management API(Composer, Storyboard or even Director) in place, I don’t think I can help. That’s beyond my skills. I’m sorry.

I replaced the page code with:

local PAGENAME = {}

 

function PAGENAME.creaPage()

end

 

function PAGENAME.destroyAll()

end

 

return PAGENAME

 

and when destroy the page call

package.loaded.PAGENAME= nil

 

But the cpu have again overload problem

physics.setPositionIterations( 50 )

… is way too high.  That is over 16 x the default, and I would expect that to crush a mobile device and even perhaps a desktop.

the higher that number is, the more physics iterations occur per frame.

Why do you feel the need to use such a high number?  i.e. What problem were you encountering with the default that you are you trying to solve with that high value?

@marcella,

Another thing I advise is: don’t use “physics.stop()” unless you absolutely want to destroy your entire physics world. If you intend to “resume” or use physics again sometime after, it’s almost always better to use “physics.pause()” instead. Basically, “physics.stop()” is a very powerful action, and often it’s too powerful, so use it with caution or with very intentional purpose.

Brent

Thanks for supports
I set physics.setPositionIterations(3) ad I use physics.pause() instead of physics.stop() but I still have the same problem.
It seems that _G.physics.start() add every times new operation on cpu.
Marco

Hi @marcella,

Why are you assigning physics to the “_G” space and then starting it multiple times? Just require physics in the module you need it and call “physics.start()” ( not"_G.physics.start()") and that will start the physics engine.

Take care,

Brent

Are you removing the scene with the removeScene() method?

https://docs.coronalabs.com/api/library/composer/removeScene.html

I use a structure with main.as that loads with
content:insert(<< PAGE >>)
to have custom transition

PAGE CODE------------------------------

module(…, package.seeall) --gioco

local clip

function creaPage()

  clip = display.newGroup()

  local physics = require(“physics”)

  physics.start()

  physics.setGravity( gravityX,gravityY ) 

  physics.setScale( 60 )

  physics.setPositionIterations( 50 )

 

 

  <<PAGE CODE>>

 

  function destroyAll()

    <<DESTROY ALL ELEMENT>>

    physics.stop()

    physics = nil

 

  end

end

destroyAll() is called when the page is destroied
content:remove(<< PAGE >>)

First of all, you should know that module(…, package.seeall) is deprecated and not recommended since it can cause memory leak.

Are you using composer API? - https://docs.coronalabs.com/api/library/composer/index.html

If not, is there a reason for it? Also, what does content:remove() do?

Thanks

yes for module(…, package.seeall) but I used this because the project is old
 

sorry, content:remove(<< PAGE >>) this is a my write error
when I destroy set MYPAGE as nil

What are you using for scene management? Are you using Director?

To call and destroy the page I use this function in main.lua:

local pageView

local destroyPage = function()

  --Destroy my old page

  pageView:destroyAll()

  pageView = nil

end

local callPage= function()

  --call and open the page 

  if pageView~=nil then destroyPage() end

  pageView = require(_G.myActualPage)

  pageView.creaPage()

end

with this code in page.lua:

module(…, package.seeall) --gioco

local clip

function creaPage()

  clip = display.newGroup()

  function destroyAll()

  end

end

I see. Without any scene management API(Composer, Storyboard or even Director) in place, I don’t think I can help. That’s beyond my skills. I’m sorry.