Detecting rocking motion

I am trying to figure out how I can go about detecting a quick rocking motion of the device without too much luck. Basically, there are two motions I want to detect; a rocking of the device away from the user ( quick tilt down then back up) and rocking towards the user (quick tilt up and then back down).

I have tried using the event.yInstant property of an accelerometer runtime event, but it doesn’t seem to be giving me what I am looking for.

Any advice or direction is greatly appreciated.

All the best,
Alan

Have you tried the Gravity API? I used that to implement something similar to what you’re describing.

I have not.  Where can I find info about this?

Here’s the accelerometer api. The Gravity documentation is above the Instant entries.

Thanks for that.  

I was trying to do this with the yGravity at first.  The problem that I was running into was establishing the baseline from which to measure the changes.  For example, the user is holding their device at say a 25 degree angle when the game is started.  I would want to check for changes based on rocking from that angle.  I just couldn’t figure a way to get that start position and then measure based off of that.  Does that make sense?

You might be able to store the initial yGravity value and compare the values being collected against that. Something like this:

 local initGravY local function initGrav( event ) initGravY = event.yGravity Runtime:removeEventListener( "accelerometer", initGrav ) print( event.name, event.xGravity, event.yGravity, event.zGravity ) end local function onAccelerate( event )          if initGravY \> event.yGravity then ​ print("DO SOMETHING HERE") end print( event.name, event.xGravity, event.yGravity, event.zGravity ) end Runtime:addEventListener( "accelerometer", onAccelerate ) Runtime:addEventListener( "accelerometer", initGrav )

Not tested, YMMV, yadda yadda.

Looks good.  I’ll try that out.  Thanks!

Have you tried the Gravity API? I used that to implement something similar to what you’re describing.

I have not.  Where can I find info about this?

Here’s the accelerometer api. The Gravity documentation is above the Instant entries.

Thanks for that.  

I was trying to do this with the yGravity at first.  The problem that I was running into was establishing the baseline from which to measure the changes.  For example, the user is holding their device at say a 25 degree angle when the game is started.  I would want to check for changes based on rocking from that angle.  I just couldn’t figure a way to get that start position and then measure based off of that.  Does that make sense?

You might be able to store the initial yGravity value and compare the values being collected against that. Something like this:

 local initGravY local function initGrav( event ) initGravY = event.yGravity Runtime:removeEventListener( "accelerometer", initGrav ) print( event.name, event.xGravity, event.yGravity, event.zGravity ) end local function onAccelerate( event )          if initGravY \> event.yGravity then ​ print("DO SOMETHING HERE") end print( event.name, event.xGravity, event.yGravity, event.zGravity ) end Runtime:addEventListener( "accelerometer", onAccelerate ) Runtime:addEventListener( "accelerometer", initGrav )

Not tested, YMMV, yadda yadda.

Looks good.  I’ll try that out.  Thanks!