Accelerometer ontilt does not work properly in lanscaperight setting ? please advise

Hi experts,

i am testing the simple onTiltevent that is similar to the one on the sample “shape Tumber” with simple circles.

i am using the follwoing funciton :

[lua]

function onTilt( event )

    physics.setGravity( ( 9.8 * event.xGravity ), ( -9.8 *event.yGravity  ) )

end

Runtime:addEventListener( “accelerometer”, onTilt )

[/lua]

The function works as expected when using the following build settings (portrait) :

[lua]

– cpmgen build.settings

settings =

{

    orientation =

    {

        default =“portrait”,

        supported =

        {

            “portrait”

        },

    },

}

[/lua]

but when i use landscapeRight as shown below i got strange behavior :

[lua]

settings =

{

    orientation =

    {

        default =“landscapeRight”,

        content = “landscapeRight”,

        supported =

        {

            “landscapeRight”

        },

    },

}

[/lua]

do I need to correct something in my code ?

Regards

Abdul

any advice ?

Can you provide more information?  What is the strange behavior?  Is this one of the Corona sample apps?

Rob,
The ball for example does not roll in the direction of the tilt. If i tilt down right it goes up. Also it becomes light like in the sky flying and goes in diff direction.
The app is from the sample but i have modified my walls and add balls pictures that i made. Basically i used the tilt function…
I hope you can provide some hints
Regards
Abdul

Hi Abdul,

Can you show your “onTilt” function to us? I assume you copied that from the sample code (or used it as a model).

Thanks,

Brent

Hi Brent,

here is the onTilt function 

[lua]

function onTilt( event )
    physics.setGravity( ( 9.8 * event.xGravity ), ( -9.8 *event.yGravity  ) )
end
 
Runtime:addEventListener( “accelerometer”, onTilt )

[/lua]

i am not sure what is wrong in this function… do i need to change some values based on build settings.

My aim is to make three balls at a time( each has a number drawn on it) and based on the number shown , the player should move the ball using tilt capabilities until the correct ball is out of the screen, so the player can go to the next number and so on.

Regards

Abdulaziz

any idea team in this question, 

Regards

Abdul

Brent really needs to chime in on this since he’s more Physics that me, but I would think the physics simulation would not like having gravity changed that often.  And I’m not sure that’s going to give you the effect you want anyway.

You’re only going to get the balls to stop moving when the device is perfectly flat.  You have to tilt it away from  you to move things up.  This would be pretty uncomfortable for a player to do.  You need to take the delta numbers from that and apply the results as some impulse or force to your balls.

Thanks Rob for your comments, 

Do you have similar example or tutorials that might help in this.

I will wait for Brent as well to comment in this. I am wondering why the behavior is different between portrait and landscape layouts.

Regards

Abdul

Hi Abdul,

I confirmed with the engineers, and essentially, the accelerometer values are always relative to portrait on both iOS and Android. So, if your app is landscape, you’ll need to compensate by 90 degrees.

See the documentation here:

http://docs.coronalabs.com/daily/api/event/accelerometer/xGravity.html

Best regards,

Brent

Hi Brent,

When you say " you’ll need to compensate by 90 degrees", Do you mean to add 90 to both x and y gravity as follow

[lua]

physics.setGravity( ( 9.8 * event.xGravity +90  ), ( -9.8 *event.yGravity +90 ) )

[/lua]

Regards

Abdul

Hi Abdul,

You’ll probably need to experiment. It may be +90 or -90 depending on if you’re using LandscapeLeft or LandscapeRight orientation.

Hi Brent ,

Actually I tried all the combination but no luck… i am using landscapeRight orientation … here are what I tried

[lua]
physics.setGravity( ( 9.8 * event.xGravity -90), ( -9.8 *event.yGravity ) )

physics.setGravity( ( 9.8 * event.xGravity +90), ( -9.8 *event.yGravity ) )

physics.setGravity( ( 9.8 * event.xGravity ), ( -9.8 *event.yGravity - 90) )

physics.setGravity( ( 9.8 * event.xGravity ), ( -9.8 *event.yGravity +90) )

physics.setGravity( ( 9.8 * event.xGravity -90), ( -9.8 *event.yGravity -90) )

physics.setGravity( ( 9.8 * event.xGravity +90), ( -9.8 *event.yGravity +90) )

physics.setGravity( ( 9.8 * event.xGravity -90), ( -9.8 *event.yGravity +90) )

physics.setGravity( ( 9.8 * event.xGravity +90), ( -9.8 *event.yGravity -90) )

[/lua]

Hi Abdul,

It’s not so much a direct “90” or “-90” application to the value. Instead, you need to effectively make the Y gravity be the X gravity, and possibly negate the value too. For example, if you had -9.8 m/s² being applied from the top (in portrait), and you rotate the device, that value is now being applied to the left (or right) of that object in regards to the accelerometer. So, you need to swap those values so that it’s being applied to the “new top” of the object in landscape.

Hi Brent ,

Finally I managed to get working with following line, 

[lua]

physics.setGravity( ( -9.8 * event.yGravity ), ( -9.8 *event.xGravity ) )

[/lua]

i swapped event.yGravity with event.xGravity and negate the event.xGravity… I am not sure I understand the concept behind it but it works… also i changed the number from 9.8 to 15 for both side just to make it move faster … I think Corona SDK should understand the landscape changes without changing the code since corona can understand the landscape and portrait orientation. This is my personal opinion. I might be wrong but I really struggled getting it to work because every change must do a build and test it in a real device as simulator cant show you the result of tilt event,

Regards

Abdulaziz

any advice ?

Can you provide more information?  What is the strange behavior?  Is this one of the Corona sample apps?

Rob,
The ball for example does not roll in the direction of the tilt. If i tilt down right it goes up. Also it becomes light like in the sky flying and goes in diff direction.
The app is from the sample but i have modified my walls and add balls pictures that i made. Basically i used the tilt function…
I hope you can provide some hints
Regards
Abdul

Hi Abdul,

Can you show your “onTilt” function to us? I assume you copied that from the sample code (or used it as a model).

Thanks,

Brent

Hi Brent,

here is the onTilt function 

[lua]

function onTilt( event )
    physics.setGravity( ( 9.8 * event.xGravity ), ( -9.8 *event.yGravity  ) )
end
 
Runtime:addEventListener( “accelerometer”, onTilt )

[/lua]

i am not sure what is wrong in this function… do i need to change some values based on build settings.

My aim is to make three balls at a time( each has a number drawn on it) and based on the number shown , the player should move the ball using tilt capabilities until the correct ball is out of the screen, so the player can go to the next number and so on.

Regards

Abdulaziz