GLBasic forum

Main forum => GLBasic - en => Topic started by: bigtunacan on 2011-Oct-18

Title: iPhone GETJOYY, calibration, duplicate values?
Post by: bigtunacan on 2011-Oct-18
I'm trying to add in tilt controls to my game on iPhone and I noticed for GETJOYY() any given value returned by GETJOYY actually indicates to possible positions of the phone.  Essentially if I start with the phone laying face up I will have a y value of 0.  A quarter turn in one direction will flow from 0 to positive 1; a quarter turn the other direction will flow from 0 to negative 1.  Continuing past the quarter turn point will cause the values to reduce back towards 0.  The result is that every value represents 2 phone positions rather than a unique phone position.

I'm testing this on an iPhone 3G.  I verified this behavior by pulling GETJOYY(0) and printing it to the screen of my iPhone as I rotated the phone after experience some rather bizarre behavior in my game.

This is the code I used to verify.
Code (glbasic) Select

LOCAL y = GETJOYY(0)
PRINT "ybefore: " + y, 0, 60


Is this the expected behavior?
Title: Re: iPhone GETJOYY, calibration, duplicate values?
Post by: Kitty Hello on 2011-Oct-18
y value is gravity direction component in Y direction (bottom of phone=1, top of phone=-1).

If you hold your phone like you are talking to a call, the values should be +1, thus.
If you flip it upside down then, the value should be -1
If you lay it flat on the table, it should be 0
Title: Re: iPhone GETJOYY, calibration, duplicate values?
Post by: matchy on 2011-Oct-18
And if you hold the axis diagonally (45 or 180+45 degree angle) the result is +/- 0.25/0.75.

Quote from: bigtunacan on 2011-Oct-18...as I rotated the phone after experience some rather bizarre behavior in my game.

Anyhow, what's the bizarre behavior?
Title: Re: iPhone GETJOYY, calibration, duplicate values?
Post by: Moebius on 2011-Oct-18
QuoteThe result is that every value represents 2 phone positions rather than a unique phone position.
That's why there are 'GETJOYX()' and 'GETJOYZ()'  :good:
Title: Re: iPhone GETJOYY, calibration, duplicate values?
Post by: bigtunacan on 2011-Oct-18
Quote
If you hold your phone like you are talking to a call, the values should be +1, thus.
If you flip it upside down then, the value should be -1
If you lay it flat on the table, it should be 0

What you explain there is what I thought should happen.  But that is not what I'm seeing.  While phone is laying face up value is 0.  While phone is laying face down value again is 0.

@Serpent,

I'm not sure what you are trying to say there.  If I were able to rotate the phone perfectly about a single axis then the values related to the other 2 axis would not change so their values are not helpful.  Each axis is reports independently.  Or is there something I'm missing here?
Title: Re: iPhone GETJOYY, calibration, duplicate values?
Post by: Kitty Hello on 2011-Oct-18
face up and face down = 0. That's true. Because there's no gravity acting on the sensor that checks for the top-to-bottom axis. Do you understand?
Title: Re: iPhone GETJOYY, calibration, duplicate values?
Post by: bigtunacan on 2011-Oct-18
Ok, I will have to rethink how I am handling my tilt controls then
Title: Re: iPhone GETJOYY, calibration, duplicate values?
Post by: bigtunacan on 2011-Oct-19
I've been agonizing over this all day and have not been making progress.

Here is what I currently have
Code (glbasic) Select


        // At initialization I grab the current y accelleration
IF platform = "IPHONE" THEN  yoff = GETJOYY(0)


        // Then in my game loop I get accelleration
LOCAL y = GETJOYY(0)
        // and try to offset it by the initial value
        y = y-yoff

IF y > .1 AND (play1.ent.locy > play1.ent.ymin)
        DEC play1.ent.locy, 10.0*y
DEC play1.shadow.locy, 10.0*y
ELSEIF y < -.1 AND (play1.ent.locy < play1.ent.ymax)
DEC play1.ent.locy, 10.0*y
DEC play1.shadow.locy, 10.0*y
        ENDIF


This does not work. Once the user hits a certain point of rotation the controls will invert.  Also at certain positions of rotation for the starting calibration controls will then not work at all (this seems to happen if the phone is held at a 90 degree rotation up from laying face down or up.
Title: Re: iPhone GETJOYY, calibration, duplicate values?
Post by: bigtunacan on 2011-Oct-21
I've been struggling with this for a few days; Googled it a hundred times; searched the forums here; and still have no idea what to do :(

I found the following post on the boards where another guy was having the exact same problem 2 years ago, but didn't look like he figured it out either...

http://www.glbasic.com/forum/index.php?topic=3939.msg28886#msg28886 (http://www.glbasic.com/forum/index.php?topic=3939.msg28886#msg28886)

I saw the Gernot replied to the other post with

Quoteget the accelerations for the "still" state. Then subtract these from the actual acceleration on each run.

I think to make it really work, you need to make a rotation matrix for the "is" state, then invert that and multiply the current state with the invese matrix each time. Overkill IMO.

The first part of that statement is what I've been attempting to do in my code, but it doesn't work correctly, not sure what I'm doing wrong.  For the second part I really don't understand how to even begin to do that.

Someone has to have solved this problem already; I will gladly write up a tutorial and post it back here for future users if someone can help me get this working.
Title: Re: iPhone GETJOYY, calibration, duplicate values?
Post by: Kitty Hello on 2011-Oct-21
I made a code snippet / math for this. Might be of interest for others as well:

http://www.glbasic.com/forum/index.php?topic=7115.0 (http://www.glbasic.com/forum/index.php?topic=7115.0)
Title: Re: iPhone GETJOYY, calibration, duplicate values?
Post by: bigtunacan on 2011-Oct-22
Thanks for the code snippet.  I updated my controls based off of your advice.  It works much better than before.  Now I can calibrate off of quite a few positions and the controls will still work, but there are some angles that you can calibrate from that just won't work correctly.  I wonder if this is a limitation of the accelerometer? I tried playing my game while sitting (worked fine), laying on my back (worked fine), and various stages of laying on my side (some angles from a side angle worked, others didn't not...)  I also searched the web and downloaded a few app store apps that use the motion controls, and every one I found locks the user into using the phone in a limited position, so hopefully this won't be a big deal if I release my game this way as well :)