Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Accelerometer ontilt does not work properly in lanscaperight setting ? please advise
Started by alzaabi98 Oct 10 2013 03:44 AM

14 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

alzaabi98

[GLOBAL: userInfoPane.html]
alzaabi98
  • Contributor

  • 354 posts
  • Corona SDK

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 :

 

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

 

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

 

 

-- cpmgen build.settings
settings =
{
    orientation =
    {
        default ="portrait",
        supported =
        {
            "portrait"
        },
    },
 
}

 

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

 

settings =
{
    orientation =
    {
        default ="landscapeRight",
        content = "landscapeRight",
        supported =
        {
            "landscapeRight"
        },
    },
 
}
 
 

 

 

do I need to correct something in my code ?

 

 

Regards

Abdul



[TOPIC: post.html]
#2

alzaabi98

[GLOBAL: userInfoPane.html]
alzaabi98
  • Contributor

  • 354 posts
  • Corona SDK

any advice ?



[TOPIC: post.html]
#3

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,376 posts
  • Enterprise

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



[TOPIC: post.html]
#4

alzaabi98

[GLOBAL: userInfoPane.html]
alzaabi98
  • Contributor

  • 354 posts
  • Corona SDK

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

[TOPIC: post.html]
#5

bjsorrentino

[GLOBAL: userInfoPane.html]
bjsorrentino
  • Veteran

  • 8,506 posts
  • Corona SDK

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



[TOPIC: post.html]
#6

alzaabi98

[GLOBAL: userInfoPane.html]
alzaabi98
  • Contributor

  • 354 posts
  • Corona SDK

Hi Brent,

 

here is the onTilt function 

 

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

 

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



[TOPIC: post.html]
#7

alzaabi98

[GLOBAL: userInfoPane.html]
alzaabi98
  • Contributor

  • 354 posts
  • Corona SDK

any idea team in this question, 

 

Regards

Abdul



[TOPIC: post.html]
#8

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 25,376 posts
  • Enterprise

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.



[TOPIC: post.html]
#9

alzaabi98

[GLOBAL: userInfoPane.html]
alzaabi98
  • Contributor

  • 354 posts
  • Corona SDK

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



[TOPIC: post.html]
#10

bjsorrentino

[GLOBAL: userInfoPane.html]
bjsorrentino
  • Veteran

  • 8,506 posts
  • Corona SDK

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



[TOPIC: post.html]
#11

alzaabi98

[GLOBAL: userInfoPane.html]
alzaabi98
  • Contributor

  • 354 posts
  • Corona SDK

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

 

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

 

Regards

Abdul



[TOPIC: post.html]
#12

bjsorrentino

[GLOBAL: userInfoPane.html]
bjsorrentino
  • Veteran

  • 8,506 posts
  • Corona SDK

Hi Abdul,

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



[TOPIC: post.html]
#13

alzaabi98

[GLOBAL: userInfoPane.html]
alzaabi98
  • Contributor

  • 354 posts
  • Corona SDK

Hi Brent ,

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



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) )



[TOPIC: post.html]
#14

bjsorrentino

[GLOBAL: userInfoPane.html]
bjsorrentino
  • Veteran

  • 8,506 posts
  • Corona SDK

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.



[TOPIC: post.html]
#15

alzaabi98

[GLOBAL: userInfoPane.html]
alzaabi98
  • Contributor

  • 354 posts
  • Corona SDK

Hi Brent ,,

 

Finally I managed to get working with following line, 

 

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

 

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




[topic_controls]
[/topic_controls]