Accelerometer control

How can one get the accelerometer to control an object on screen. For example, let’s say I have a ball in the middle of the screen, how do I use the accelerometer to move it around?

[code]
local ball = display.newImage(“ball.png”)

–Centre the ball on the screen
ball.x = display.contentWidth/2
ball.y = display.contentHeight/2

–Now how do I use the accelerometer to get events?

[/code] [import]uid: 3826 topic_id: 3040 reply_id: 303040[/import]

if I use the onTilt event, how do I determine how much to move, I do not want to use physics in this movement, as there is no gravity involved, think of this as a cross hair that you can move on the screen by tilting the device. [import]uid: 3826 topic_id: 3040 reply_id: 8872[/import]

While there may be an “official” answer, what I did was build the Accelerometer1 sample (in the Sample Code/Hardware folder) and put it on my iPhone, then tilted this way and that and looked at the values that were returned.

Not sure if that will work for you, but it’s one option.

Jay
[import]uid: 9440 topic_id: 3040 reply_id: 8991[/import]

I do not have the Hardware folder in my Corona Game Edition samples folder. Any ideas where I can get that from?

thanks and cheers,

Jayant C Varma [import]uid: 3826 topic_id: 3040 reply_id: 8993[/import]

It looks like there is different Sample Code in Corona SDK and Corona Game Edition (weird). Whichever one you have, download the other one and see if it’s in there.

Jay
[import]uid: 9440 topic_id: 3040 reply_id: 8998[/import]

Here’s the meat of the Accelerometer sample.

Tim

[code]

– Create Text and Display Objects

– Text parameters
local labelx = 50
local x = 220
local y = 95
local fontSize = 24

local frameUpdate = false – used to update our Text Color (once per frame)

local xglabel = display.newText( "gravity x = ", labelx, y, native.systemFont, fontSize )
xglabel:setTextColor(255,255,255)
local xg = display.newText( “0.0”, x, y, native.systemFont, fontSize )
xg:setTextColor(255,255,255)
y = y + 25
local yglabel = display.newText( "gravity y = ", labelx, y, native.systemFont, fontSize )
local yg = display.newText( “0.0”, x, y, native.systemFont, fontSize )
yglabel:setTextColor(255,255,255)
yg:setTextColor(255,255,255)
y = y + 25
local zglabel = display.newText( "gravity z = ", labelx, y, native.systemFont, fontSize )
local zg = display.newText( “0.0”, x, y, native.systemFont, fontSize )
zglabel:setTextColor(255,255,255)
zg:setTextColor(255,255,255)
y = y + 50
local xilabel = display.newText( "instant x = ", labelx, y, native.systemFont, fontSize )
local xi = display.newText( “0.0”, x, y, native.systemFont, fontSize )
xilabel:setTextColor(255,255,255)
xi:setTextColor(255,255,255)
y = y + 25
local yilabel = display.newText( "instant y = ", labelx, y, native.systemFont, fontSize )
local yi = display.newText( “0.0”, x, y, native.systemFont, fontSize )
yilabel:setTextColor(255,255,255)
yi:setTextColor(255,255,255)
y = y + 25
local zilabel = display.newText( "instant z = ", labelx, y, native.systemFont, fontSize )
local zi = display.newText( “0.0”, x, y, native.systemFont, fontSize )
zilabel:setTextColor(255,255,255)
zi:setTextColor(255,255,255)

– Create a circle that moves with Accelerator events (for visual effects)

local centerX = display.contentWidth / 2
local centerY = display.contentHeight / 2

Circle = display.newCircle(0, 0, 20)
Circle.x = centerX
Circle.y = centerY
Circle:setFillColor( 0, 0, 255 ) – blue


– textMessage() –

– v1.0

– Create a message that is displayed for a few seconds.
– Text is centered horizontally on the screen.

– Enter: str = text string
– scrTime = time (in seconds) message stays on screen (0 = forever) – defaults to 3 seconds
– location = placement on the screen: “Top”, “Middle”, “Bottom” or number (y)
– size = font size (defaults to 24)
– color = font color (table) (defaults to white)

– Returns: text object (for removing or hiding later)

local textMessage = function( str, location, scrTime, size, color, font )

local x, t

size = tonumber(size) or 24
color = color or {255, 255, 255}
font = font or “Helvetica”

– Determine where to position the text on the screen
if “string” == type(location) then
if “Top” == location then
x = display.contentHeight/4
elseif “Bottom” == location then
x = (display.contentHeight/4)*3
else
– Assume middle location
x = display.contentHeight/2
end
else
– Assume it’s a number – default to Middle if not
x = tonumber(location) or display.contentHeight/2
end

scrTime = (tonumber(scrTime) or 3) * 1000 – default to 3 seconds (3000) if no time given

t = display.newText(str, 0, 0, font, size )
t.x = display.contentWidth/2
t.y = x
t:setTextColor( color[1], color[2], color[3] )

– Time of 0 = keeps on screen forever (unless removed by calling routine)

if scrTime ~= 0 then

– Function called after screen delay to fade out and remove text message object
local textMsgTimerEnd = function()
transition.to( t, {time = 500, alpha = 0},
function() t.removeSelf() end )
end

– Keep the message on the screen for the specified time delay
timer.performWithDelay( scrTime, textMsgTimerEnd )
end

return t – return our text object in case it’s needed

end – textMessage()


– Hardware Events

– Display the Accerator Values

– Update the text color once a frame based on sign of the value

local function xyzFormat( obj, value)

obj.text = string.format( “%1.3f”, value )

– Exit if not time to update text color
if not frameUpdate then return end

if value < 0.0 then
– Only update the text color if the value has changed
if obj.positive ~= false then
obj:setTextColor( 255, 0, 0 ) – red if negative
obj.positive = false
print("[—]")
end
else

if obj.positive ~= true then
obj:setTextColor( 255, 255, 255) – white if postive
obj.positive = true
print("+++")
end

end
end
– event.xGravity is the acceleration due to gravity in the x-direction
– event.yGravity
– event.yGravity is the acceleration due to gravity in the y-direction
– event.zGravity
– event.zGravity is the acceleration due to gravity in the z-direction
– event.xInstant
– event.xInstant is the instantaneous acceleration in the x-direction
– event.yInstant
– event.yInstant is the instantaneous acceleration in the y-direction
– event.zInstant
– event.zInstant is the instantaneous acceleration in the z-direction
– event.isShake
– event.isShake is true when the user shakes the device

– Called for Accelerator events

– Update the display with new values
– If shake detected, make sound and display message for a few seconds

local function onAccelerate( event )

– Format and display the Accelerator values

xyzFormat( xg, event.xGravity)
xyzFormat( yg, event.yGravity)
xyzFormat( zg, event.zGravity)
xyzFormat( xi, event.xInstant)
xyzFormat( yi, event.yInstant)
xyzFormat( zi, event.zInstant)

frameUpdate = false – update done

– Move our object based on the accelerator values

Circle.x = centerX + (centerX * event.xGravity)
Circle.y = centerY + (centerY * event.yGravity * -1)

end

– Function called every frame
– Sets update flag to time our color changes

local function onFrame()
frameUpdate = true

–[[
if xg.positive == true then
xg:setTextColor( 255, 255, 255) – white if postive
else
xg:setTextColor( 255, 0, 0) – red if negative
end
–]]
end

– Add runtime listeners

Runtime:addEventListener (“accelerometer”, onAccelerate);
Runtime:addEventListener (“enterFrame”, onFrame);
[/code] [import]uid: 8196 topic_id: 3040 reply_id: 9016[/import]