Corona – Accelerometer

Accelerometer

The Accelerometer is built into all IOS and other handheld devices, it detects motion of the device, tilting or shaking. Corona handles this with the Accelerometer event.

Remote Test

The accelerometer is difficult to test with, since the Accelerometer events are not available in the simulator. This means that you need to build your project each time you want to test. Which tedious and slow. There’s a solution!

There are several apps that can be used to send accelerometer and other events from a real device to the simulator.

All of these apps are very similar. You install them on a device: iPhone, iPad, iPod Touch etc. Launch and Configure the app. Set up your Corona project to receive data from the device. This consists of adding a few lines of code to your main.lua file.

Try it out for yourself

You will need an IOS device to perform this test. You will also need to install one of the apps listed above for testing. Your desktop or laptop computer will also need to have a WIFI connection. For the example I used rCorona Lite, because it is free.

First, install rCorona Lite on your IOS device.

Launch rCorona on your IOS device. Set the IP address to the IP address of your desktop computer. Open System Preferences on your Desktop or laptop computer. Click on Network. Find your IP address in the network panel.

Open rCorona on your mobile device. In the settings area, add your IP address to the field at the top of the screen. Below this is a smaller field. this should show the port number 8181. Tap save.

Open Corona, and make a new project. Save your new project to a folder where you will be working.

Navigate to the rCorona web site: http://rcorona.wongcharles.com/. Find the link to the file rcorona.lua. Download this file. Open it up. Inside is a file named rcorona.lua. Copy the rcorona.lua file into the folder with the main.lua of the project you created in the earlier step.

Open main.lua in your project and add the following code:

if system.getInfo("environment") == "simulator" then
	local rcorona = require("rcorona")
	rcorona.startServer(8181)
end

Now you’re ready! For a simple first test add the following code to your main.lua file. Here we’ll create a text field and display values from the accelerometer events on the screen.

 

--------------------------------------------------------------------------------
-- set up rcorona
--------------------------------------------------------------------------------
if system.getInfo("environment") == "simulator" then
	local rcorona = require("rcorona")
	rcorona.startServer(8181)
end
--------------------------------------------------------------------------------

--------------------------------------------------------------------------
-- Make some text
--------------------------------------------------------------------------
local display_text = display.newText( "v1", 0, 0, native.systemFont, 16)
display_text.x = 160
display_text.y = 100
--------------------------------------------------------------------------

---------------------------------------------------------------------------
-- Listen for Accelerometer events
--------------------------------------------------------------------------
local function onAccelerate( event )
	local xGravity = event.xGravity
	local yGravity = event.yGravity
	display_text.text = "x gravity:"..xGravity.." y gravity:"..yGravity
end

Runtime:addEventListener ("accelerometer", onAccelerate)

Testing

Open your project in the Corona Simulator. Then open your IOS device and launch the rCorona app. When the app launches the simulator should start displaying information from the accelerometer in your device in the simulator on the computer.

I tested on a second generation iPod Touch. I saw a range of -1 to +1 for xGravity. This corresponded to tilting my device from flat to 90 degrees. I got -1 tilting my device to the left and +1 tilting to the right. You may not see exactly 1.0. I saw ranges of numbers from 0.0044 to 0.995 which are close enough to 1 and 0 for our purposes.

Notice that yGravity is positive when the device is tilted forward, and negative when the device is tilted toward you.

xGravity is negative when the device is tilted to the left and positive when the device tilts to the right.

Leave a Reply

Your email address will not be published. Required fields are marked *