Moving paddle using gravity from accelerometer

Hi all,

Can anybody help me in accelerometer based movement.
I am trying to build brick breaker example myself.

I am tying X coordinate of paddle accoding to X component of gravity vector and using accelerometer event to change the position of the paddle but the paddle motion is not smooth. It’s kind of jerky.
Wondering if there is better way to do this?

Things that I think can be causing this are that accelerometer event is not fired as frequently as it should to get smooth motion, but I don’t know how to check that.

A solution I am thinking of is instead of tying X coordinate of the paddle to the gravity vector’s X component, I give velocity to paddle using gravity vector, but I am work right now so can’t test it until tonight when I go home.

Following is my code to change paddle position

[lua]local _w = display.contentWidth
local _h = display.contentHeight

local wBar = 80
local hBar = 15

local bar = display.newImage(‘h_paddle.png’,(_w-wBar)*0.5,_h-hBar)
physics.addBody(bar, “static”, { density=1, friction=0, bounce=1 } )

local function updateAccelerometer(e)

local xGravity = e.xGravity

local x = _w*0.5 + (_w*0.5 * (xGravity*4))

if ((x - wBar*0.5)<0) then
x = wBar*0.5
elseif ((x + wBar*0.5)>_w) then
x = _w - wBar*0.5
end
bar.x = x

end

Runtime:addEventListener( “accelerometer” , updateAccelerometer )[/lua] [import]uid: 48521 topic_id: 8870 reply_id: 308870[/import]

If it’s that your accelerometer event is not firing as frequently as you’d like, try playing around with this function:

[blockcode]
system.setAccelerometerInterval( 50 )
[/blockcode]

Change 50 to different values until you get it right (the higher it is, the more sensitive). I’ve made quite a few accelerometer games, i know, the testing process can be a pain.

Thankfully there’s the iPhone Configuration Utility that makes testing and installing apps on device a fast and painless process. [import]uid: 52430 topic_id: 8870 reply_id: 32409[/import]

Thanks Jonathan! I would try that out. Certainly shows how helpful bunch of people around here are.

Can tap into your expertise then and confirm whether above way of implementation is the best way to move paddle based on tilt of device or is there a better way to implement the functionality.

After playing Tilt Monster, I think you definitely must have implemented this. How was the experience like? [import]uid: 48521 topic_id: 8870 reply_id: 32414[/import]

This suggestion worked beautifully :slight_smile:

Thanks!!! [import]uid: 48521 topic_id: 8870 reply_id: 32623[/import]