views:

947

answers:

3

I doing a sound application on accelerometer.Its play different sound for movement by calculating accelerometer value.But how can i find the accelerometer direction that the user move x-axis plus or minus and y-axis plus or minus.How can i find this value on accelerometer.

Please give some instruction or helping code or project.

+1  A: 

You need to perform a vector addition and calculate the Summation of 2 vectors to get the resultant vector. The above article explains all the common methods of calculating it. But doing it in Programmatically you just have to apply Pythagoras theorem and Tan theta = b/a

Chathuranga Chandrasekara
+1  A: 

I think you would need the magnetometer direction (to at least give you a bearing you could always compare against), as well as using the vector math mentioned above. This article does a better job of explaining how to add vectors (the first one glosses over the most likely case by just saying it's "hard")...

http://blog.dotphys.net/2008/09/basics-vectors-and-vector-addition/

Jeffrey Berthiaume
He's not asking about the magnometer, he's talking about the Accelerometer. In other words if the device moves, which direction did it move in, not which direction is it facing.
Brock Woolf
I know -- I'm just saying I think it would be easier to use the magnetometer to compare against. I have a hard time trusting that the info I get from the accelerometer alone (even if gathered fast enough) would give me enough to go on...
Jeffrey Berthiaume
+1  A: 

You have to represent it using vectors, there is a delegate method below which details what you need to do.

Now I haven't taken a look at the API too much yet, but it I believe your direction vector is returned to you from the accelerometer.

There is a delegate method which returns the values you will need. The following code may help from a tutorial you should take a look at here:

- (void)acceleratedInX:(float)xx Y:(float)yy Z:(float)zz
{
    // Create Status feedback string
    NSString *xstring = [NSString stringWithFormat:
    @"X (roll, %4.1f%%): %f\nY (pitch %4.1f%%): %f\nZ (%4.1f%%) : %f",
    100.0 - (xx + 1.0) * 100.0, xx,
    100.0 - (yy + 1.0) * 100.0, yy,
    100.0 - (zz + 1.0) * 100.0, zz
    ];
    self.textView.text = xstring;

    // Revert Arrow and then rotate to new coords
    float angle = atan2(xx, yy);
    angle += M_PI / 2.0;

    CGAffineTransform affineTransform = CGAffineTransformIdentity;
    affineTransform = CGAffineTransformConcat( affineTransform,       CGAffineTransformMakeRotation(angle));
    self.xarrow.transform = affineTransform;
}

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    [self acceleratedInX:acceleration.x Y:acceleration.y Z:acceleration.z];
}

There is also an easy to read article which explains it clearly here along with sample code.

Brock Woolf