I am moving a simple character all around the screen with the accelerometer. If I don’t have the device flat the character falls to the bottom of the screen. can I set the default tilt angle of the device to 45 degrees-so basically it thinks 45 degrees is 0? I want the user to be able to hold the device at 45 degrees. I tried reviewing the compass tutorial but am not sure that is what I need. Any help would be appreciated. [import]uid: 39370 topic_id: 11281 reply_id: 311281[/import]
Can you post your movement code so that we can suggest solution.
[import]uid: 48521 topic_id: 11281 reply_id: 40944[/import]
Sure:
local motionx = 0
local motiony = 0
local function onAccelerate(event)
motionx = 50 * event.yGravity
motiony = 50 * event.xGravity
end
Runtime:addEventListener(“accelerometer”, onAccelerate)
local function moveCharacter(event)
character.x = character.x - motionx
character.y = character.y - motiony
end
Runtime:addEventListener(“enterFrame”, moveCharacter) [import]uid: 39370 topic_id: 11281 reply_id: 40950[/import]
always post your code in lua tags.
now, let’s try to understand how your code works.
whenever your device tilt changes, accelerometer event is triggered which essentially changes motionx and motiony values.
motionx and motiony are the increments in position of your character object’s x and y coordinates. During each frame event, your character’s position gets updated by motionx and motiony.
Now what you want is, motionx and/or motiony to be zero, when your phone is already tilted to certain angle (in your case you want 45). Let’s call this position as neutral position.
So you need to note what is value of motionx and motiony when phone is at neutral position. once you note that, you change your code to subtract those values from motionx and motiony respectively in every accelerometer event.
For 45 degrees tilt the values should be 25.
Now if you only want tilt along x axis to be then you subtract only from motionx and do nothing for motiony and vice versa. [import]uid: 48521 topic_id: 11281 reply_id: 40952[/import]
Thanks for the reply. I understand what needs to happen now, I just can’t get it to work. I tried defining a neutral position by recording the accelerometer data when the app starts. here is my start code and the code I tried.
[lua]display.setStatusBar( display.HiddenStatusBar )
_W = display.contentWidth
_H = display.contentHeight
system.setAccelerometerInterval(50)
local hero = display.newRect(_W/2, _H/2, 30, 30)
rectangleTop:setFillColor(140, 140, 140)
local motionx = 0
local motiony = 0
local function onAccelerate( event )
motionx = 15 * (event.xGravity)
motiony = 15 * (event.yGravity)
end
Runtime:addEventListener (“accelerometer”, onAccelerate)
local function movehero (event)
hero.x = hero.x + motionx
hero.y = hero.y - motiony
end
Runtime:addEventListener(“enterFrame”, movehero)[/lua]
Then I tried to use:
[lua]xOffset = event.xGravity
yOffset = event.yGravity[/lua]
and
[lua]motionx = 15 * (event.xGravity - xOffset)
motiony = 15 * (event.yGravity - yOffset)[/lua]
I just can’t see the solution to this one [import]uid: 39370 topic_id: 11281 reply_id: 41246[/import]
Try this
[lua]display.setStatusBar( display.HiddenStatusBar )
_W = display.contentWidth
_H = display.contentHeight
system.setAccelerometerInterval(50)
local hero = display.newRect(_W/2, _H/2, 30, 30)
hero:setFillColor(140, 140, 140)
local motionx = 0
local motiony = 0
local function onAccelerate( event )
motionx = 15 * (event.xGravity-0.5)
motiony = 15 * (event.yGravity+0.5)
end
Runtime:addEventListener (“accelerometer”, onAccelerate)
local function movehero (event)
hero.x = hero.x + motionx
hero.y = hero.y - motiony
end
Runtime:addEventListener(“enterFrame”, movehero)[/lua] [import]uid: 48521 topic_id: 11281 reply_id: 41359[/import]
Thanks so much for your help. This was a great help. Sorry it took me so long to thank you. [import]uid: 39370 topic_id: 11281 reply_id: 45339[/import]
moved [import]uid: 102017 topic_id: 11281 reply_id: 71688[/import]