Accelerometer in Two Orientations

App SurgeApp Surge Member Posts: 651
edited February 2012 in Working with GS (Mac)
Hey guys,
The game I'm working on is designed for working on the iPhone in portrait mode and portrait upside down. However, I'm using accelerometer to control the actor's left and right movement. But there's a problem with the accelerometer:

In portrait mode the game.Accelerometer.X works fine, but in portrait upside down mode, it's backwards because the device is oriented different. So the player has to tilt right to go left and tilt left to go right

So how does should I set this up so no matter which orientation you're in, you can still tilt the iPhone left to go left and tilt right to go right?

Thanks,
Henry

P.S. My rule is accelerometer rules are:
when game.accelerometer is less than -0.01
-- constrain self.motion.linear velocity.x to (( game.Accelerometer.X + game.calibrate )*150)

when game.accelerometer is more than 0.01
-- constrain self.motion.linear velocity.x to (( game.Accelerometer.X + game.calibrate )*150)

Comments

  • thilanwijthilanwij Member Posts: 20
    I have the same problem, hopefully someone will help with it!
  • POMPOM Member Posts: 2,599
    edited February 2012
    There is an attribute called screen orientation ..
    Make 2set of rules, 1 for portrait, and another one for portrait upside down (which all you have to change is add a minus)

    Roy
  • App SurgeApp Surge Member Posts: 651
    @P-O-M
    I knew the screen orientation attribute was there, but where should I put the minus?
  • POMPOM Member Posts: 2,599
    edited February 2012
    @redsky
    Ok ,
    You basically have 2 set of rules right..

    ---------------------------------------------
    1) when game.Screen.Device Orientation IS Portrait :
    when game.accelerometer is less than -0.01
    Constrain self.motion.linear velocity.x to (( game.Accelerometer.X + game.calibrate )*150)

    when game.accelerometer is more than 0.01
    Constrain self.motion.linear velocity.x to (( game.Accelerometer.X + game.calibrate )*150)
    ---------------------------------------------

    ---------------------------------------------
    2) when game.Screen.Device Orientation IS Portrait UPSIDEDOWN :
    when game.accelerometer is less than 0.01 (WE DROPPED THE MINUS HERE)
    Constrain self.motion.linear velocity.x to (( game.Accelerometer.X + game.calibrate )*150)

    when game.accelerometer is more than -0.01 (WE ADDED THE MINUS HERE)
    Constrain self.motion.linear velocity.x to (( game.Accelerometer.X + game.calibrate )*150)
    ---------------------------------------------

    I think this should work for ya ;)

    Roy.
  • App SurgeApp Surge Member Posts: 651
    edited February 2012
    Hey Roy,
    Thanks for the comment, however that didn't work... I checked the code about a million times and it still was inverted.

    Thanks,
    Henry
  • POMPOM Member Posts: 2,599
    edited February 2012
    Thats weird

    Hey , can you try this :
    ---------------------------------------------
    2) when game.Screen.Device Orientation IS Portrait UPSIDEDOWN :
    when game.accelerometer is less than -0.01 ←SEE HERE
    Constrain self.motion.linear velocity.x to (( game.Accelerometer.X + game.calibrate )*150)

    when game.accelerometer is more than 0.01 ←SEE HERE
    Constrain self.motion.linear velocity.x to (( game.Accelerometer.X + game.calibrate )*150)
    ---------------------------------------------

    If this will not work either let me know and ill make you a quick template..

    EDIT :
    SEE THE RULES ABOVE AGAIN ,

    Roy.
  • App SurgeApp Surge Member Posts: 651

    image
    image
  • POMPOM Member Posts: 2,599
    Hey mate , I'm making a template for ya ,

    Roy.
  • App SurgeApp Surge Member Posts: 651
    Thanks Roy!
  • POMPOM Member Posts: 2,599
    edited February 2012
    Here you go :
    Preview this onto your device , and see that it work , then look at the rules :
    http://www.mediafire.com/?c1cmxom6wo7t5lb

    EDIT:
    I can clearly see that GS is very laggy when it comes to detect device orientation..
    I made you a template to detect device orientation with the Y value of the accelerometer , i think its MUCH more accurate then using the game.Screen.Device Orientation Method , see my comment below .

    Roy.
  • App SurgeApp Surge Member Posts: 651
    Hm... I copied the code exactly and it still didn't work...
    I'll have to triple check this and maybe use the Project Merger on it just to be sure. Well it's half past 4 a.m. here so I'm going to be getting some sleep now, thanks Roy!
  • POMPOM Member Posts: 2,599
    just made it :
    http://www.mediafire.com/?1jy5us5fhg3279q

    In this the "portrait" and the "portrait upside-down" detection is made by the Y value of the accelerometer .
    This is MUCH more accurate then using the normal game.Screen.Device Orientation Method !

    Hope it helps you..
    Roy.
  • App SurgeApp Surge Member Posts: 651
    Ah! It still doesn't work... I'll investigate this more tomorrow, thanks Roy.
  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,881
    @P-0-M -- Thanks for sharing this! It's also a very useful demo of how to determine device orientation without using the slower "screen.device orientation" attribute.

    RThurman
  • App SurgeApp Surge Member Posts: 651
    @P-O-M
    I copied and pasted all of the rules and it still didn't work!
    Do you think it has anything to do with the fact I'm using game.calibrate in my constrain attribute?

    Thanks,
    Henry
  • POMPOM Member Posts: 2,599
    edited February 2012
    @RThurman
    Glad it helps you !

    @resdsky
    Good morning man ;)
    Maybe its your game.calibrate fault , give me some info about what it is and what values you put in it..and ill try to figure it out .
    Anyway , the template i made you should work flawlessly , so i guess something else in the code is interrupting ..

    Roy.
  • JohnPapiomitisJohnPapiomitis Member Posts: 6,256
    if you dont press the game calibrate button, the calibrate attribute is set to 0. So if it doesnt work even when you dont press the calibrate button, it should have nothing to do with it cause the attribute has no value.
  • App SurgeApp Surge Member Posts: 651
    edited February 2012
    @P-O-M
    Thanks so much for all the help! Well I have a calibrate button on my main menu and all it says is when touch is pressed change attribute game.calibrate to game.accelerometer.x and then save attribute game.accelerometer.x and it loads the attribute on the main screen. Also, I have an extra condition where it says y is > or < 0, which says game.ingum is false and I have another set of accelerometer rules for when game.ingum is true, that makes the tilt less sensitive. I don't think it could be anything besides that though, because those are the only things I changed when I copied the code!

    EDIT: @JohnPapiomitis that's a good point, and I haven't been pressing calibrate.
  • App SurgeApp Surge Member Posts: 651
    Just a little bump for anyone who's got an idea how to fix this...
  • POMPOM Member Posts: 2,599
    redsky ,
    Im terribly sorry but I'm not at home right now thats why i can help much right now (I'm from my iPhone) , its friday night after all :)
    So if you want you can send over the project (or just a small part of it that demonstrates the issue) so i can take a look at once ill get home.
    Roysadaka (at) gmail (dot) com

    Roy.
  • App SurgeApp Surge Member Posts: 651
    Thanks Roy!
    Message sent :)
  • POMPOM Member Posts: 2,599
    @redsky ..
    I just sent you the fixed file .
    Good luck continuing your project, if you have any more questions don't hesitate to ask !

    Roy.
  • App SurgeApp Surge Member Posts: 651
    Thanks so much Roy!
  • bajashbajash Member, PRO Posts: 215
    Hi P-O-M,
    I am running into the same problem. Would you mind posting the solution to this problem?

    Thanks
  • POMPOM Member Posts: 2,599
    edited March 2012
    @bajash
    I believe reading this thread and downloading the demo I made and see the 2 simple rules in it should give you the solution you seek ;)

    Photobucket Roy
Sign In or Register to comment.