Accelrometer Question

So I’m struggling a bit. In a mini-game I’m using the accelerometer to control the character in a landscape orientation. Obviously depending on whether it’s landscapeLeft or landscapeRight I need to toggle between adding yGravity positively or negatively to move the character in the correct direction.

I set a variable (orienVal) when the stage is opened and use this to modify the corresponding character motion:

if event.type == "landscapeRight" then  
 orienVal = -1  
 elseif event.type == "landscapeLeft" then  
 orienVal = 1  
 end  
if \_G.box2["profile"].environment == "simulator" then  
 print("SIMULATOR")  
 aclrVal = event.xGravity  
 else  
 print("DEVICE")  
 aclrVal = event.yGravity  
 end  
 motionX = 20 \* (orienVal\*aclrVal)  

However I’ve noticed on numerous occasions this fails because the orientation is being recognised as faceUp. Not entirely sure how to handle this, as a simple else in the above condition will have to make the assumption that it’s either landscapeLeft or landscapeRight - which obviously can be a wrong assumption to make.

The only solution I can think is to restrict the orientation to one landscape setting, but I don’t believe this is ideal.

Many thanks for any opinions. [import]uid: 33275 topic_id: 36076 reply_id: 336076[/import]

Hi segaboy
I have been struggling with gravity vs. orientation myself.

If you are using this code to handle proper orientation change:
http://developer.coronalabs.com/code/proper-orientation-rotation-animation

then read this thread #6 by ingemar:
https://developer.coronalabs.com/forum/2013/01/10/tableview-widget-inverted-scrolling-when-device-rotated#comment-138437

I guess you are locking your content orientation in the build.settings with content = “landscapeRight”?
the trick is not to lock your content orientation but instead add stage.rotation = 180 before the orientation animation in the rotationfix module above.

If you do it this way you don’t need to flip orienVal and you don’t need to worry about the “faceUp” orientation.

Hope this helps? [import]uid: 13632 topic_id: 36076 reply_id: 143301[/import]

Hey ojnab - thanks for the response.

I have followed your advice, but still having some issues.

I don’t have a ‘content=…’ in my build.settings, just default and supported.

I’m using the rotationFix module listed above and entered the stage.rotation = 180 before animation.

This all works nicely, however I noticed that the accelerometer is now working incorrectly - it works in one orientation, but when I flip the device it reverses the direction - left goes right, right goes left.

I completely removed any indication of orienVal, was that correct?

Thanks for your help… [import]uid: 33275 topic_id: 36076 reply_id: 143348[/import]

Oh sorry. Guess you still need to flip the gravity directions.
Since the rotationfix module has a global variable called isRotated the
easiest way to do it is something like this:

[lua]
local function onAccelerate( event )
local gx, gy = -event.yGravity*50, -event.xGravity*50
if isRotated then
gx, gy = gx*-1, gy*-1
end
physics.setGravity(gx, gy)
end

Runtime:addEventListener (“accelerometer”, onAccelerate);
[/lua]

It ignores the faceUp orientation. So this should work just fine. [import]uid: 13632 topic_id: 36076 reply_id: 143360[/import]

Cheers ojnab - working perfectly. [import]uid: 33275 topic_id: 36076 reply_id: 143451[/import]

Hi segaboy
I have been struggling with gravity vs. orientation myself.

If you are using this code to handle proper orientation change:
http://developer.coronalabs.com/code/proper-orientation-rotation-animation

then read this thread #6 by ingemar:
https://developer.coronalabs.com/forum/2013/01/10/tableview-widget-inverted-scrolling-when-device-rotated#comment-138437

I guess you are locking your content orientation in the build.settings with content = “landscapeRight”?
the trick is not to lock your content orientation but instead add stage.rotation = 180 before the orientation animation in the rotationfix module above.

If you do it this way you don’t need to flip orienVal and you don’t need to worry about the “faceUp” orientation.

Hope this helps? [import]uid: 13632 topic_id: 36076 reply_id: 143301[/import]

Hey ojnab - thanks for the response.

I have followed your advice, but still having some issues.

I don’t have a ‘content=…’ in my build.settings, just default and supported.

I’m using the rotationFix module listed above and entered the stage.rotation = 180 before animation.

This all works nicely, however I noticed that the accelerometer is now working incorrectly - it works in one orientation, but when I flip the device it reverses the direction - left goes right, right goes left.

I completely removed any indication of orienVal, was that correct?

Thanks for your help… [import]uid: 33275 topic_id: 36076 reply_id: 143348[/import]

Oh sorry. Guess you still need to flip the gravity directions.
Since the rotationfix module has a global variable called isRotated the
easiest way to do it is something like this:

[lua]
local function onAccelerate( event )
local gx, gy = -event.yGravity*50, -event.xGravity*50
if isRotated then
gx, gy = gx*-1, gy*-1
end
physics.setGravity(gx, gy)
end

Runtime:addEventListener (“accelerometer”, onAccelerate);
[/lua]

It ignores the faceUp orientation. So this should work just fine. [import]uid: 13632 topic_id: 36076 reply_id: 143360[/import]

Cheers ojnab - working perfectly. [import]uid: 33275 topic_id: 36076 reply_id: 143451[/import]

Hi segaboy
I have been struggling with gravity vs. orientation myself.

If you are using this code to handle proper orientation change:
http://developer.coronalabs.com/code/proper-orientation-rotation-animation

then read this thread #6 by ingemar:
https://developer.coronalabs.com/forum/2013/01/10/tableview-widget-inverted-scrolling-when-device-rotated#comment-138437

I guess you are locking your content orientation in the build.settings with content = “landscapeRight”?
the trick is not to lock your content orientation but instead add stage.rotation = 180 before the orientation animation in the rotationfix module above.

If you do it this way you don’t need to flip orienVal and you don’t need to worry about the “faceUp” orientation.

Hope this helps? [import]uid: 13632 topic_id: 36076 reply_id: 143301[/import]

Hey ojnab - thanks for the response.

I have followed your advice, but still having some issues.

I don’t have a ‘content=…’ in my build.settings, just default and supported.

I’m using the rotationFix module listed above and entered the stage.rotation = 180 before animation.

This all works nicely, however I noticed that the accelerometer is now working incorrectly - it works in one orientation, but when I flip the device it reverses the direction - left goes right, right goes left.

I completely removed any indication of orienVal, was that correct?

Thanks for your help… [import]uid: 33275 topic_id: 36076 reply_id: 143348[/import]

Oh sorry. Guess you still need to flip the gravity directions.
Since the rotationfix module has a global variable called isRotated the
easiest way to do it is something like this:

[lua]
local function onAccelerate( event )
local gx, gy = -event.yGravity*50, -event.xGravity*50
if isRotated then
gx, gy = gx*-1, gy*-1
end
physics.setGravity(gx, gy)
end

Runtime:addEventListener (“accelerometer”, onAccelerate);
[/lua]

It ignores the faceUp orientation. So this should work just fine. [import]uid: 13632 topic_id: 36076 reply_id: 143360[/import]

Cheers ojnab - working perfectly. [import]uid: 33275 topic_id: 36076 reply_id: 143451[/import]

Hi segaboy
I have been struggling with gravity vs. orientation myself.

If you are using this code to handle proper orientation change:
http://developer.coronalabs.com/code/proper-orientation-rotation-animation

then read this thread #6 by ingemar:
https://developer.coronalabs.com/forum/2013/01/10/tableview-widget-inverted-scrolling-when-device-rotated#comment-138437

I guess you are locking your content orientation in the build.settings with content = “landscapeRight”?
the trick is not to lock your content orientation but instead add stage.rotation = 180 before the orientation animation in the rotationfix module above.

If you do it this way you don’t need to flip orienVal and you don’t need to worry about the “faceUp” orientation.

Hope this helps? [import]uid: 13632 topic_id: 36076 reply_id: 143301[/import]

Hey ojnab - thanks for the response.

I have followed your advice, but still having some issues.

I don’t have a ‘content=…’ in my build.settings, just default and supported.

I’m using the rotationFix module listed above and entered the stage.rotation = 180 before animation.

This all works nicely, however I noticed that the accelerometer is now working incorrectly - it works in one orientation, but when I flip the device it reverses the direction - left goes right, right goes left.

I completely removed any indication of orienVal, was that correct?

Thanks for your help… [import]uid: 33275 topic_id: 36076 reply_id: 143348[/import]

Oh sorry. Guess you still need to flip the gravity directions.
Since the rotationfix module has a global variable called isRotated the
easiest way to do it is something like this:

[lua]
local function onAccelerate( event )
local gx, gy = -event.yGravity*50, -event.xGravity*50
if isRotated then
gx, gy = gx*-1, gy*-1
end
physics.setGravity(gx, gy)
end

Runtime:addEventListener (“accelerometer”, onAccelerate);
[/lua]

It ignores the faceUp orientation. So this should work just fine. [import]uid: 13632 topic_id: 36076 reply_id: 143360[/import]

Cheers ojnab - working perfectly. [import]uid: 33275 topic_id: 36076 reply_id: 143451[/import]