Accelerometer Question

osucowboy18osucowboy18 Member Posts: 1,307
edited November -1 in Working with GS (Mac)
Hey there everyone,

I've got a question regarding implementing the iOS accelerometer into one of my games. The game I want to incorporate this into is an iPhone game in Landscape mode. I want to control an Actor's vertical position by using the accelerometer. Can someone post the code for that, as the template I usually use is failing me :( Thanks in advance!

- Alex

Comments

  • JohnPapiomitisJohnPapiomitis Member Posts: 6,256
    when attribute device accelerometer x is greater then or equal to 0.2 move up

    when attribute device accelerometer x is less then or equal to -0.2 move down

    another way to do it thats smoother is :

    Constrain self position y to : self.Position.Y +((min(.5,max(-.5,( game.Accelerometer.X + 0 ))))*17)

    If you wanted to go one step further and have a calibrate button for your accelerometer if you use that constrain, make a attribute calibrate and in the constrain position towards the end where its +0 change that to +game.calibrate

    then in your calibrate button ahve when touch is pressed change attribute calibrate to: game.Accelerometer.X *-1

    cheers
  • osucowboy18osucowboy18 Member Posts: 1,307
    Thanks John, I'll give it a try!

    - Alex
  • osucowboy18osucowboy18 Member Posts: 1,307
    Hey John,

    One quick question regarding the constrain attribute above.

    I want it set up that when the actor is going up, it's rotation changes to 5, and when the actor is going down, its rotation changes to -5. While keeping the same constrain attribute behavior above, is there a rule I can add in to determine if the actor is moving up and down so I can change its rotation accordingly? Thanks again for your help on this.

    - Alex
  • osucowboy18osucowboy18 Member Posts: 1,307
    Sorry to bump this thread so quickly, but I'm just about finished with my game and this is the last thing I need to add.
  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,881
    Try:

    When game.Accelerometer.Y > .02
    ....Change Attribute: self.Rotation To: 5

    When game.Accelerometer.Y <.02
    ....Change Attribute: self.Rotation To: -5
  • osucowboy18osucowboy18 Member Posts: 1,307
    Thanks! I'll test it on my device here in a sec.
  • JohnPapiomitisJohnPapiomitis Member Posts: 6,256
    yes like RThurman said

    but it will be accelerometer.x not y
  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,881
    JohnPapiomitis said:
    but it will be accelerometer.x not y

    Oops... that's right. (I forgot about landscape mode.)
  • osucowboy18osucowboy18 Member Posts: 1,307
    Ok, I tried adding in a rule below the constrain attribute that said

    When Accelerometer.X > .02
    Change Rotation to 5

    When Accelerometer.X < .02
    Change Rotation to -5

    but when I tested this on my device, it didn't work. My rotation just stayed at 0. By the way, I am using the calibrate method you described earlier John, and it works great, but I don't know if it would effect this. Thanks again guys.

    - Alex
  • osucowboy18osucowboy18 Member Posts: 1,307
    Oh ya one other thing.

    I have it set up for the game to autorotate. However, if the user flips the device from one landscape mode to the other, the accelerometer controls don't work as intended. If you go back and re-calibrate with the new device orientation, the accelerometer controls are opposite. Can you provide some assistance with this issue also?
  • JohnPapiomitisJohnPapiomitis Member Posts: 6,256
    well the accelerometer axis don't autorotate as well. So im guessing you need 4 rules based on the autorotation, and have the constrain in each rule substituting the accelerometer axis for the correct one. If that makes sense ahah
  • osucowboy18osucowboy18 Member Posts: 1,307
    I'm afraid I don't quite follow you. If it's not to much to ask, could you make a quick template to demonstrate what you are talking about? Thanks :)
  • osucowboy18osucowboy18 Member Posts: 1,307
    Just to be clear, the constrain attribute and calibration method that was mentioned above are working fine. The only problem is that when the user changes the orientation of their device, the accelerometer controls do not work as intended. If anyone can provide some help on this issue I would really appreciate it!
  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,881
    About the rotation (+5 and -5):
    You will need to continually test for the accelerometer data (put them into a timer).

    About the orientation:
    My guess is that you will probably need four separate rule sets. One for each orientation.
    You will need to flip the rules for Accelerometer.X between both landscape modes.
    You will need to flip the rules for Accelerometer.Y between both portrait modes.
    (Negative becomes positive, and positive becomes negative.)
  • osucowboy18osucowboy18 Member Posts: 1,307
    So, have a rule that says...

    If game.Screen.Device Orientation is Landscape Left
    // Use constrain attribute from above //

    If game.Screen.Device Orientation is Landscape Right
    // swap all positive and negative game.Accelerometer.X values with the constrain above //

    Is this what you mean? Thanks again for your help!
  • BlackbirdStudiosBlackbirdStudios Member Posts: 493
    osucowboy18 said:
    So, have a rule that says...

    If game.Screen.Device Orientation is Landscape Left
    // Use constrain attribute from above //

    If game.Screen.Device Orientation is Landscape Right
    // swap all positive and negative game.Accelerometer.X values with the constrain above //

    Is this what you mean? Thanks again for your help!

    This would probably be A LOT easier if you don't support autorotation.

    Cheers

    -Matt
  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,881
    osucowboy18 said:
    So, have a rule that says...

    If game.Screen.Device Orientation is Landscape Left
    // Use constrain attribute from above //

    If game.Screen.Device Orientation is Landscape Right
    // swap all positive and negative game.Accelerometer.X values with the constrain above //

    Is this what you mean? Thanks again for your help!

    Yes, that is what I meant. But first, I would take BlackBird's advice and turn off other orientations for now. I'd just suggest getting it working in landscape first. Then later you can branch into the arcane mysteries of such things as upside down portrait orientation.
  • osucowboy18osucowboy18 Member Posts: 1,307
    BlackbirdStudios said:
    This would probably be A LOT easier if you don't support autorotation.

    Cheers

    -Matt

    Believe me, after trying this over and over and over, I am about to make that decision. But I'm about to test a new build so we'll see how it works this time :) Here's hoping...
Sign In or Register to comment.