Android Studio Gyroscope examples? -


i'm trying build application desired affect when device tilted degree.

i've taken at, , enabled, accelerometer, doesn't give me desired affect. said, wish device want to, when device has achieved degree, 90 degrees.

i've got following code, works when device tilted fast enough:

@override public void onsensorchanged(sensorevent sensorevent) {     sensor mysensor = sensorevent.sensor;     if (mysensor.gettype() == sensor.type_gyroscope) {         float x = sensorevent.values[0];         float y = sensorevent.values[1];         float z = sensorevent.values[2];          long currtime = system.currenttimemillis();          if ((currtime - lastupdate) > 100) {             long difftime = (currtime - lastupdate);             lastupdate = currtime;              float speed = math.abs(x + y + z - last_x - last_y - last_z) / difftime * 10000;              if (speed > shake_threshold && !sound.isplaying()) {                 sound.start();             }              last_x = x;             last_y = y;             last_z = z;         }     } } 

this code used accelerometer, changed sensor.type_accelerometer sensor.type_gyroscope, hoping reveal me do.

could possibly somethings this?

if (x > 90 || y > 90 || z > 90 && !sound.isplaying()) {     sound.start(); } 

in place of my

if (speed > shake_threshold && !sound.isplaying()) {     sound.start(); } 

i've tried looking examples of how this, unable find anything, on android developers...

i want simple allow me tell when device has reached degree or radian, speed not matter.

any wonderful.

thanks!

nathan

edit:

i've done this:

 if (z > 5 || z < -5 || x > 5 || x < -5 && !sound.isplaying()) {      sound.start();  } else if ( z == 4 || x == 4) {      sound.stop();      sound = mediaplayer.create(this, r.raw.sound);  } 

and works extent. once z , x values have crossed threshold of '5', sound play. but, if leave device in position, then, upon returning device starting position activates sound again, because still within '5' threshold.

this not looking for, figured try little different:

if (z == 5 || z == -5 || x == 5 || x == -5 && !sound.isplaying()) {     sound.start(); } else if ( z == 4 || x == 4) {     sound.stop();     sound = mediaplayer.create(this, r.raw.sound); } 

this doesn't work @ all.

i want device able detect when has crossed threshold of '5', not want able start sound again if moved , still inside of threshold.

basically, want device able play sound when line crossed, , not when point after line. figured having when number == 5 it, can't seem that.

the stop function doesn't work @ either. if use == @ all, doesn't work. there way can code java able recognize?

remember, want able know when point crossed, not when area entered.

cheers!

accelerometer: "measures acceleration force in m/s2 applied device on 3 physical axes (x, y, , z), including force of gravity."

orientation: "measures degrees of rotation device makes around 3 physical axes (x, y, z). of api level 3 can obtain inclination matrix , rotation matrix device using gravity sensor , geomagnetic field sensor in conjunction getrotationmatrix() method."


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -