GLBasic forum

Main forum => GLBasic - en => Topic started by: gregbug on 2009-Dec-27

Title: Iphone acceleration sensor... - determine orientation
Post by: gregbug on 2009-Dec-27
hi!

i'm trying to make a game for my iPhone...  ;)

which it is the better method in order to understand if the home button (iphone home button)
is on the left, right, up or down...

i coded this and it works discreetly...  :-[

Code (glbasic) Select

CONSTANT iPhone_HomeDown%  = 1
CONSTANT iPhone_HomeUp%    = 2
CONSTANT iPhone_HomeLeft%  = 3
CONSTANT iPhone_HomeRight% = 4

// ********************************************************** //
// iPhone_Get_CurrentPosition                                 //
// Rilascia la posizione corrente dell'iPhone                 //
// 1 iPhone_HomeDown%  iPhone con il pulsante Home in basso   //
// 2 iPhone_HomeUp%    iPhone con il pulsante Home in alto    //
// 3 iPhone_HomeLeft%  iPhone con il pulsante Home a sinistra //
// 4 iPhone_HomeRight% iPhone con il pulsante Home a destra   //
// ********************************************************** //
FUNCTION iPhone_Get_CurrentPosition:
STATIC LastPosition%
LOCAL XForce# = GETJOYX(0)
LOCAL YForce# = GETJOYY(0)
LOCAL ZForce# = GETJOYZ(0)
IF (XForce# >= -75) AND (XForce# < 75) AND (YForce# < 45) AND (ZForce# > -75)
LastPosition% = iPhone_HomeDown%
RETURN iPhone_HomeDown%
ENDIF
IF (XForce# >= -75) AND (XForce# < 75) AND (YForce# > 45) AND (ZForce# > -75)
LastPosition% = iPhone_HomeUp%
RETURN iPhone_HomeUp%
ENDIF
IF (XForce# > 75) AND (YForce# > -30) AND (ZForce# > -75)
LastPosition% = iPhone_HomeLeft%
RETURN iPhone_HomeLeft%
ENDIF
IF (XForce# <-75) AND (YForce# > -30) AND (ZForce# > -75)
LastPosition% = iPhone_HomeRight%
RETURN iPhone_HomeRight%
ENDIF
RETURN LastPosition%
ENDFUNCTION



some suggestion/optimization/other methods?

...and it is possible to get the current angle of the iPhone ?

thanks!
Title: Re: Iphone acceleration sensor...
Post by: matchy on 2009-Dec-27
Code (glbasic) Select
IF (XForce# >= -0.75) AND (XForce# < 0.75) AND (YForce# < 0.45) AND (ZForce# > -0.75)

GETJOYX

x# = GETJOYX(n%)
For Joystick number n% it returns the amount of movement in the X direction. The valid range for n% is 0-9 (maximum 10 joysticks), each with up to 32 buttons (m%). The value returned is in the range -1.0 to +1.0.

Title: Re: Iphone acceleration sensor...
Post by: gregbug on 2009-Dec-27
thanks matchy!

new code now works better!

as always... if someone has suggestion/optimizations... are welcome! :)

Code (glbasic) Select

CONSTANT iPhone_HomeDown%  = 1
CONSTANT iPhone_HomeUp%    = 2
CONSTANT iPhone_HomeLeft%  = 3
CONSTANT iPhone_HomeRight% = 4

// *************************************************** //
// iPhone_Get_XAcceleration: filter#                   //
// Rilascia la forza di gravità sull'asse X            //
// filter# filtro movimento (valore minimo variazione) //
// il valore va da -100 a 100 a seconda dell'intensità //
// *************************************************** //
FUNCTION iPhone_Get_XAcceleration: filter#
STATIC lastAngle# = 0
LOCAL xAngle# = GETJOYX(0)
IF (lastAngle# > xAngle#-filter#) AND (lastAngle# < xAngle#+filter#)
RETURN lastAngle#
ELSE
lastAngle# = xAngle#
RETURN xAngle#
ENDIF
ENDFUNCTION

// *************************************************** //
// iPhone_Get_YAcceleration: filter#                   //
// Rilascia la forza di gravità sull'asse Y            //
// filter# filtro movimento (valore minimo variazione) //
// il valore va da -100 a 100 a seconda dell'intensità //
// *************************************************** //
FUNCTION iPhone_Get_YAcceleration: filter#
STATIC lastAngle# = 0
LOCAL yAngle# = GETJOYY(0)
IF (lastAngle# > yAngle#-filter#) AND (lastAngle# < yAngle#+filter#)
RETURN lastAngle#
ELSE
lastAngle# = yAngle#
RETURN yAngle#
ENDIF
ENDFUNCTION

// *************************************************** //
// iPhone_Get_ZAcceleration: filter#                   //
// Rilascia la forza di gravità sull'asse Z            //
// filter# filtro movimento (valore minimo variazione) //
// il valore va da -100 a 100 a seconda dell'intensità //
// *************************************************** //
FUNCTION iPhone_Get_ZAcceleration: filter#
STATIC lastAngle# = 0
LOCAL zAngle# = GETJOYZ(0)
IF (lastAngle# > zAngle#-filter#) AND (lastAngle# < zAngle#+filter#)
RETURN lastAngle#
ELSE
lastAngle# = zAngle#
RETURN zAngle#
ENDIF
ENDFUNCTION


// ********************************************************** //
// iPhone_Get_CurrentPosition                                 //
// Rilascia la posizione corrente dell'iPhone                 //
// 1 iPhone_HomeDown%  iPhone con il pulsante Home in basso   //
// 2 iPhone_HomeUp%    iPhone con il pulsante Home in alto    //
// 3 iPhone_HomeLeft%  iPhone con il pulsante Home a sinistra //
// 4 iPhone_HomeRight% iPhone con il pulsante Home a destra   //
// ********************************************************** //
FUNCTION iPhone_Get_CurrentPosition:
STATIC LastPosition% = iPhone_HomeDown%
LOCAL XForce# = iPhone_Get_XAcceleration(0.03)
LOCAL YForce# = iPhone_Get_YAcceleration(0.03)
LOCAL ZForce# = iPhone_Get_ZAcceleration(0.03)

// *** Up Donw detect ***
IF (YForce# < -0.35)
LastPosition% = iPhone_HomeDown%
RETURN iPhone_HomeDown%
ELSEIF (YForce# > 0.35)
LastPosition% = iPhone_HomeUp%
RETURN iPhone_HomeUp%
ENDIF

// *** Left Right Detect ***
IF (XForce# > 0.35)
LastPosition% = iPhone_HomeLeft%
RETURN iPhone_HomeLeft%
ELSEIF (XForce# <-0.35)
LastPosition% = iPhone_HomeRight%
RETURN iPhone_HomeRight%
ENDIF

RETURN LastPosition%
ENDFUNCTION



thanks!