Accelerometer lag?

Hi i don’t know if this is a newbie question but i’ll go ahead and ask anyways  :wink: . Whenever I test my app on the simulator it runs VERY smoothly but when I run it on my iphone with the accelerometer, it really slows down and lags heavily. I have already tried setting accelerometer interval but that didn’t work to well. Does anyone have an idea on how I can smoothly use the accelerometer? thanks in advance!

The best practice is to set your accelerometer’s setting to match your app’s expected FPS settings.  Thus if your FPS is set to 30, set this to 30.  If you set the app to 60, set the accelerometer to 60.

http://docs.coronalabs.com/api/library/system/setAccelerometerInterval.html

Thanks :). I have one more thing if you don’t mind, Now the accelerometer seems very choppy. Here is my code:

[lua]

function onAccelerate(event)

    local dt = getDeltaTime()

    if gamestate == “running” then

            player.x = display.contentCenterX + ((event.xGravity * 3) * display.contentCenterX)

            player.x = math.min(player.x, display.contentWidth - 25)

            player.x = math.max(player.x, 25)

    end

end

Runtime:addEventListener (“accelerometer”, onAccelerate)

[/lua]

How can I fix that to make it smoother? Thanks!

Here is basically what I used in my space shooter:

local function onTilt(event) &nbsp;&nbsp;&nbsp; app.myRocket.x = (app.myRocket.x + event.xGravity \* ((app.sensitivity \* 15 )+ level)) &nbsp;&nbsp;&nbsp; if app.myRocket.x \< 64 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; app.myRocket.x = 64 &nbsp;&nbsp;&nbsp; elseif app.myRocket.x \> (display.contentWidth - 64) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; app.myRocket.x = display.contentWidth - 64 &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; return true end

The only real difference I see is I’m moving my player (app.myRocket) in a relative manner.  In other words, I’m adding the gravity amount to my current location, not calculating a new locations based on center. 

Also using math.min and math.max to protect your player from going off the edges.  I suspect that those calls are more CPU intensive than the simple if’s I’m doing.  I basically cut out my if statement to see if I’m running or not, but that shouldn’t be an issue.

Now this code is still a big choppy on  my Nook Color, but it was pretty smooth on the 1st Gen kindle fire and has always been smooth on iOS devices.

Rob

Rob

Thanks so much, and just to be clear, app.sensitivity is your variable not a built in one?

Yes, I have a settings screen where they player can pick between three settings for basically fast, medium or slow and the ship moves are adjusted by that setting.

Ok thanks a lot! You have helped so much :slight_smile:

Sorry, I just compiled on my phone and it still just doesn’t feel smooth with large accelerations. Would this be the result of lots of code in my enter frame functions? Because the rest of my app runs really smooth and then the player is choppy. Thanks and sorry I’m asking so much

It could be.

The best practice is to set your accelerometer’s setting to match your app’s expected FPS settings.  Thus if your FPS is set to 30, set this to 30.  If you set the app to 60, set the accelerometer to 60.

http://docs.coronalabs.com/api/library/system/setAccelerometerInterval.html

Thanks :). I have one more thing if you don’t mind, Now the accelerometer seems very choppy. Here is my code:

[lua]

function onAccelerate(event)

    local dt = getDeltaTime()

    if gamestate == “running” then

            player.x = display.contentCenterX + ((event.xGravity * 3) * display.contentCenterX)

            player.x = math.min(player.x, display.contentWidth - 25)

            player.x = math.max(player.x, 25)

    end

end

Runtime:addEventListener (“accelerometer”, onAccelerate)

[/lua]

How can I fix that to make it smoother? Thanks!

Here is basically what I used in my space shooter:

local function onTilt(event) &nbsp;&nbsp;&nbsp; app.myRocket.x = (app.myRocket.x + event.xGravity \* ((app.sensitivity \* 15 )+ level)) &nbsp;&nbsp;&nbsp; if app.myRocket.x \< 64 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; app.myRocket.x = 64 &nbsp;&nbsp;&nbsp; elseif app.myRocket.x \> (display.contentWidth - 64) then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; app.myRocket.x = display.contentWidth - 64 &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; return true end

The only real difference I see is I’m moving my player (app.myRocket) in a relative manner.  In other words, I’m adding the gravity amount to my current location, not calculating a new locations based on center. 

Also using math.min and math.max to protect your player from going off the edges.  I suspect that those calls are more CPU intensive than the simple if’s I’m doing.  I basically cut out my if statement to see if I’m running or not, but that shouldn’t be an issue.

Now this code is still a big choppy on  my Nook Color, but it was pretty smooth on the 1st Gen kindle fire and has always been smooth on iOS devices.

Rob

Rob

Thanks so much, and just to be clear, app.sensitivity is your variable not a built in one?

Yes, I have a settings screen where they player can pick between three settings for basically fast, medium or slow and the ship moves are adjusted by that setting.

Ok thanks a lot! You have helped so much :slight_smile:

Sorry, I just compiled on my phone and it still just doesn’t feel smooth with large accelerations. Would this be the result of lots of code in my enter frame functions? Because the rest of my app runs really smooth and then the player is choppy. Thanks and sorry I’m asking so much

It could be.