views:

125

answers:

1

Hi !

I'm trying to use the accelerometer to move a UIImage. I works well but my problem is that with my code

self.character.center = CGPointMake(160+acceleration.x*175, 230-acceleration.y*175);

my picture moves even on a stable surface because of the precision of the acceleration.x value. So I decided to use a workaround by multiplying it with a value, casting it to an INT and then dividing it and cast it to a float (i.e i just remove some numbers after the coma)

self.character.center = CGPointMake(160+(float)((int)((acceleration.x*100000))/100000)*175, 230-(float)((int)((acceleration.y*100000))/100000)*175);

But after i use this code, my little picture isn't moving anymore.

So my question is : do you know why it doesn't work anymore ? Is there a proper way to remove numbers after the coma in a float ?

Thanks a lot Fred.

+2  A: 

Instead of trying to remove decimals after the comma, you should better use a low pass filter. A lowpass filter will let only pass changes to your acceleration that happen below a certain cutoff frequency. Therefore, it will keep steady changes to the acceleration but remove fluctations and jitter with very high frequencies.

Wikipedia has a good explanation how a simple RC Lowpass filter works and shows a possible implementation. Apple shows a similar implementation in the AccelerometerGraph sample code.

Johannes Rudolph
Thanks i used it and i've had sucess ! I minimised the cutoff frequency to make it even less sensitive !Thank you very much !
Fred Dpn