Problems simulating turbulence. Is there a better way?

Hey guys,

Having a few problems with a couple of functions that I wrote that should simulate a bit of turbulence/mild shaking on an object periodically.  Basically, every second it will apply a linear impulse to the object.  The problem that I am having is that after about 10 seconds the game will literally grind to a halt! :frowning: Severe drop in frame-rate.  The object in question also has some Particle Candy FX attached to it, however, removing these effects only delays halting of the game by a further 10 seconds or so.

I’ve also tried literally shifting the object’s x/y coords by a randomised small number and had this on a repeating timer too.  Unfortunately though, I need to also be able to control the object via touch and so altering the position of the object manually just makes the object then become impossible to control.  I also tried removing the random number generation and just using a static value.

I’ve attached the code below.  Can you please help to shed some light on what the issue might be? and, even better, suggest an alternative way to do this.  I’m hoping it’s just something silly that I have overlooked…

Cheers,

Rich

[lua]

local initTurbulence – forward declaration

        

local function turbulence(xForce, yForce)          

    self.Player.turbulence = timer.performWithDelay(            

    1000, function() 

        self.Player:applyLinearImpulse( xForce, yForce, self.Player.x, self.Player.y); 

        initTurbulence(); 

    end, 0)            

end       

        

function initTurbulence()            

    local randXForce = mRandom(-2,2)            

    local randYForce = mRandom(-2,2)            

    turbulence(randXForce*0.001, randYForce*0.001) – tiny force         

end

        

initTurbulence() – invoked inside willEnterScene

[/lua]

On line 8 your have the iterations of the timer.performWithDelay set to 0 which means loop forever.  So every second you call initTurbulence.  But the function inside the timer calls another function that adds another function with the timer again so I believe every second you are doubling the number of functions.  After a few seconds you will have hundreds of functions running each applying Linear impulse and calculating turbulence!  

Try setting the iterations to 1 which means do the function in the timer only once.

Yep! That is exactly it!

Thanks a lot mate :slight_smile:

On line 8 your have the iterations of the timer.performWithDelay set to 0 which means loop forever.  So every second you call initTurbulence.  But the function inside the timer calls another function that adds another function with the timer again so I believe every second you are doubling the number of functions.  After a few seconds you will have hundreds of functions running each applying Linear impulse and calculating turbulence!  

Try setting the iterations to 1 which means do the function in the timer only once.

Yep! That is exactly it!

Thanks a lot mate :slight_smile: