Tilt-based Gravity not working

When I build my app and install onto my phone, the tilt=based gravity does not work. I am trying to use it to roll a ball left and right, but it just starts floating all over the place, whichever way I tilt it. I have the orientation set to landscape, and this is the code:

local function onTilt( event )
physics.setGravity( 10 * event.xGravity, -10 * event.yGravity )
end
Runtime:addEventListener( “accelerometer”, onTilt ) [import]uid: 116264 topic_id: 20430 reply_id: 320430[/import]

All over the place whichever way you tilt, as in it moves a truly random direction? It doesn’t move left when you tilt left, or left when you tilt right, but just randomly?

Please clarify and I may have some advice.

Peach :slight_smile: [import]uid: 52491 topic_id: 20430 reply_id: 80042[/import]

Ok, I will try and be more clear. When I tilt the device right, the ball floats upwards, and when I tilt it left, it gowas down again. If I lean the device away from me the ball goes right, towards me it goes left. [import]uid: 116264 topic_id: 20430 reply_id: 80061[/import]

OK, it sounds like there’s some issue relating to orientation; did you get the code from a tutorial? If so, was the tutorial for a portrait or landScape app? What orientation is your app?

I suspect that’s where the issue lies.

Peach :slight_smile: [import]uid: 52491 topic_id: 20430 reply_id: 80130[/import]

I got the code from a tutorial for a portrait app, and my app is in landscape [import]uid: 116264 topic_id: 20430 reply_id: 80167[/import]

I’m getting the exact same problem. I downloaded the Shape Tumbler template and everything runs fine initially. But if I change the orientation from “portrait” to “landscape” in build.settings, instead of the objects falling downwards, they fall sideways to the left. Is this a bug that needs to be reported? [import]uid: 82194 topic_id: 20430 reply_id: 80188[/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);  
  

Later on 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.

PS you have doubled posted thats why this answer is here too.
[import]uid: 81718 topic_id: 20430 reply_id: 80195[/import]

Nyah, double post? I haven’t seen the other but please don’t do that.

Pete’s suggestion is worth running; please post again to update on whether or not this resolved this issue.

Another thing to keep in mind is that math may have to be changed to get desired results using your original method - it could still be done but Pete’s suggestion is better as it just manipulates the one object.

Peach :slight_smile: [import]uid: 52491 topic_id: 20430 reply_id: 80209[/import]