Tilt-based gravity for landscape mode

I am having trouble trying to get the tilt-based gravity to work in landscape mode. The ball in my game rolls the wrong way when I tilt the device (when I tilt left, the ball rolls upwards, when I tilt down, the ball rolls right and so on). Is there any way to fix this?
The code that I am currently using is:
local function onTilt( event )
physics.setGravity( 5 * event.xGravity, -5 * event.yGravity )
end
Runtime:addEventListener( “accelerometer”, onTilt )
[import]uid: 116264 topic_id: 20445 reply_id: 320445[/import]

Hey WIll,

why are you changing actual gravity? you could try something like this instead…

Leave physics.setGravity( 0,0) at the place where you set up physics to begin with, at the top of the programme, to avoid confusion, think of the ‘Y’ as being Earth style gravity, you see setGravity is really for the ‘physics world’ not individual objects.

Let’s say your ball object is called ‘ball’

  
local function onTilt(event)  
  
ball.x = 5\*event.xGravity  
ball.y = 5\*event.yGravity   
  
end  
  
Runtime:removeEventListener ("accelerometer", onTilt);  

Late you can try to set physics gravity to 9.84 or whatever and see how that affects your results.

Also check out your configure settings and pop in something to make sure you game rotates to the correct orientation when the user rotates their device, if that is actually what you want.

I hope this helps,

IPete2. [import]uid: 81718 topic_id: 20445 reply_id: 80187[/import]

I’ve added the code but yet the ball isnt moving when I move the device? [import]uid: 116264 topic_id: 20445 reply_id: 80220[/import]

Will,

You know you won’t see it on the simulator don’t you?

Generally you have to deploy to a device to get to see tilt and shake etc in action, or you can download ‘rCorona Lite’ (remote App which sends signals to the simulator from your device) from the App store (if you are on iOS). This clever App asks you for your IP address, and then allows you to see the results on the simulator from actions, like tilt and touch on the device connected.

Other quick thoughts:
Your ball has got a physics body added to it? That body is set to dynamic?

For example this code should set physics up load a ball and allow you to tilt it about:

  
local physics = require ("physics");  
system.setAccelerometerInterval( 60 )  
physics.start()  
physics.setGravity(0 , 0)  
  
ball = display.newImageRect("ball.png", 40,40)  
  
ball.x, ball.y = display.contentWidth/2,display.contentHeight/2   
ball:setReferencePoint( display.CenterReferencePoint )  
physics.addBody(ball, "dynamic", {friction =0.20, bounce =0.3 });  
  
local function onTilt(event)  
   
ball.x = 30\*event.xGravity  
ball.y = 30\*event.yGravity   
   
print(event.xGravity)  
  
end  
   
Runtime:removeEventListener ("accelerometer", onTilt);  
  

You could try altering the 30 to 60 or bigger or smaller. You can also use print() to help out in other places in you code, check the results in the corona terminal.

If none of this works for you then we probably need some more info about the devices, and to see the code, keep us in touch.

IPete2. [import]uid: 81718 topic_id: 20445 reply_id: 80260[/import]

IPete2 - I just tried your sample code above and it displays the ball in the center of my device but when I tilt my device the ball does not move. Does your code work for you? [import]uid: 82194 topic_id: 20445 reply_id: 80263[/import]

Doh!

Sorry about that - it was late and I cut and pasted the wrong section of code for you, off course you have to add the eventlistener not remove it - sorry about that :slight_smile:

Okay the following code will work ok. (it does in my simulator with rCorona running on the iOS device

-- rcorona -- only include this small rcorona section of code if you have the rCorona App from the App Store running on your iOS device... and the lua file from their website in your folder too.  
  
if system.getInfo("environment") == "simulator" then  
 local rcorona = require("rcorona")  
 rcorona.startServer(8181)  
end  
  
-- if you don't have rcorona cut and paste from here down...  
local physics = require ("physics");  
--system.setAccelerometerInterval( 60 )  
physics.start()  
physics.setGravity(0 , 0)  
   
local ball = display.newImageRect("ball.png",80,140)  
   
ball.x = display.contentWidth/2  
ball.y = display.contentHeight/2   
  
ball:setReferencePoint( display.CenterReferencePoint )  
physics.addBody(ball, "dynamic", {friction =0.20, bounce =0.3 });  
function onTilt(event)  
   
 ball.x = ball.x+30\*(event.xGravity)  
 ball.y = ball.y+30\*(-event.yGravity)   
 print(event.xGravity)  
   
end  
  
Runtime:addEventListener ("accelerometer", onTilt);  
  

cut and paste that into your screen and the ball should start in the middle and move around that point. We just need to get you started first then you can fiddle with settings as you wish.

IPete2. [import]uid: 81718 topic_id: 20445 reply_id: 80350[/import]

Thanks, I’ve made a new project just using the code you’ve given and the ball does move when I tilt the device, however, it only moves a certain distance and then stops, even if I hold the device tilted [import]uid: 116264 topic_id: 20445 reply_id: 80399[/import]

Hi Will,

Thats odd, using rcorona on both the devices I tested this code on (generation 2 iPod, and an iPad 2) it works as intended. As it works ok on the simulator I am assuming it is fine on the actual devices.

which device are you using? and which version of Corona are you using? Also how big is your ball graphic or are you using a circle generated by Corona?

IPete2.

[import]uid: 81718 topic_id: 20445 reply_id: 80404[/import]

With the help of this thread and some others, I finally figured out how to get tilt gravity working correctly in landscape mode. I purchased this awesome app called Corona Remote ($9.99) and it actually includes some sample code which you can download called “Corona Remote Demo”. Another thread mentioned that “Accelerometer movements are based on a portrait scale, so when you are using landscape, x becomes y and y becomes x.” So after flipping around some of the x and y values in the code, I was able to get it to work. [import]uid: 82194 topic_id: 20445 reply_id: 80426[/import]

Hey David,

That’s great news, rCorona is the same sort of thing, an App which allows in simulator control, the lite version is free, I bought it because I needed it to help me with my latest project.

IPete2.
[import]uid: 81718 topic_id: 20445 reply_id: 80464[/import]

Sorry I didnt reply to your last post david, I’ve not had internet acces for a few days. I’ve managed to sort the problem now, thanks for all your help [import]uid: 116264 topic_id: 20445 reply_id: 81385[/import]

Um, the code didn’t work for me [import]uid: 44110 topic_id: 20445 reply_id: 94201[/import]

Thought I’d just share it here.

After I modified the event.xGravity and the event.yGravity, it worked.

[code]
function onTilt( event )
physics.setGravity( ( -9.8 * event.yGravity ), ( -9.8 * event.xGravity ) )
end

Runtime:addEventListener( “accelerometer”, onTilt ) [import]uid: 44110 topic_id: 20445 reply_id: 111089[/import]