Search code examples
iosobjective-caccelerometer

Find Gravity in objective c


I wants Find gravity in objective c, I am using Accelerometer in my project. but I not got any good tutorial on Google for find gravity, So i need help

I wants to move small red circle inside Gravity Meter, shown in image attached.

I am currently using accelerometer can i find gravity using accelerometer.

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {

 ...............

  labelX.text = [NSString stringWithFormat:@"%@%f", @"X: ", acceleration.x];
  labelY.text = [NSString stringWithFormat:@"%@%f", @"Y: ", acceleration.y];
  labelZ.text = [NSString stringWithFormat:@"%@%f", @"Z: ", acceleration.z];  
![enter image description here][1]}

Thanks

enter image description herem.png


Solution

  • Looking at that app (videometer) it seems you're looking for G-force being exerted. G-force is NOT gravity - it is an outside mechanical force (user swinging the device, etc) specifically defined as NOT being from earth's gravity. This is important because on an iPhone or any other accelerometer-enabled device, it reports all 0 when there's no motion. If it was reporting gravity, any readings would constantly be reporting motion in the Y/downward axis (earth pulling you down).

    You're looking for G-force which means you need:

    1. The direction of the motion
    2. The force of the motion

    Check out Apple's documentation of the UIAcceleration object - you will get the direction based on which variables are changing - x, y, z. The value of those variables gives you a force (with 1.0 approximating g which is 9.8m/s2 - earth gravity). How you display that data on a graph depends on you - you can store a history of the acceleration events and then animate them, and reset the point to the middle when acceleration stops (the UIAccelerometer shared object will start reporting 0 values when acceleration stops).