Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Using accel. as a trigger to move an object.
Started by Abdou23 Oct 15 2016 10:10 PM

3 replies to this topic
accelerometer
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

Abdou23

[GLOBAL: userInfoPane.html]
Abdou23
  • Enthusiast

  • 80 posts
  • Corona SDK

I have an object and I want the accelerometer to just act as a trigger to move that object on the X axis instead of using the value of "xGravity".
 
local move = ""

movePlayer = function ( dt )
    if (move == "still") then
        player.x = player.x      -- notice here
    elseif (move == "right") then
        player.x = player.x + 5 * dt
    elseif (move == "left") then
        player.x = player.x - 5 * dt
    end
end


onAccel = function ( event )
    local x = event.xGravity
    if (x == 0) then      -- notice here
        move = "still"
    elseif (x > 0.1) then
        move = "right"
    elseif (x < -0.1) then
        move = "left"
    end
end


enterFrame = function (  )
    local dt = getDT() -- A function to calculate delta time, not relevant here.
    movePlayer(dt)
end
Runtime:addEventListener( "enterFrame", enterFrame )
Runtime:addEventListener("accelerometer", onAccel)

It works as expected except for one thing, even though I created a condition for when the "xGravity = 0" (the device is exactly 90 degrees) the object still keeps moving in its original direction.

To be clearer, when I tilt the device to the right, the object moves to the right, but when I tilt it back, it keeps moving to the right until I tilt it more to the left and then the object starts moving to the left. It never stands still if it was already moving.



[TOPIC: post.html]
#2

Alex@Panc

[GLOBAL: userInfoPane.html]
Alex@Panc
  • Corona Geek

  • 1,733 posts
  • Corona SDK

You should print out the xGravity values onscreen so that you can confirm the values that are being received by the device.

In my experience, it's extremely difficult to get the device to register a valid pure zero reading, without the assistance of a carpenter level.

[TOPIC: post.html]
#3

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,392 posts
  • Enterprise

I agree with Alex. The device is so sensitive that it's really hard to get a 0 value. You usually have to ignore small amounts of movement. You experience this frequently with the analog inputs on game controllers. They don't like to settle back out at 0, and you're always getting events from them.

 

Rob



[TOPIC: post.html]
#4

Abdou23

[GLOBAL: userInfoPane.html]
Abdou23
  • Enthusiast

  • 80 posts
  • Corona SDK

Thanks, guys, but I managed to fix this problem by using a bool to check if the device is moving or not. It now works perfectly. 




[topic_controls]
[/topic_controls]