If we use CADisplayLink to set a timer, either at 60fps, 30fps, or 15fps, and we use the Accelerometer’s
acceleration.x
to handle acceleration, then 60fps will create twice the acceleration as 30fps, because now the object’s x position is updated twice as frequently. How can this be correctly handle for realistic acceleration? (probably to simulate a real gravity pull).
Update: I think I made a mistake: it is not the fps for CADisplayLink, but it is the frequency of the Accelerometer’s event handler. If I set it to 1 / 60.0 or 1 / 30.0, or 1 / 15.0, the object accelerates slower… due to the event handler does a
objVelocity.x += acceleration.x;
I thought the frequency should auto adjust to make the velocity increase slower, but it is not?
The concept is wrong. the acceleration will be exactly the same even if you could set it to 100 fps. It will not change because the acceleration is constant until motion manager changes it, and that happens based on the update frequency you set in it. Even if it changed you cant just “add” it because thats not how acceleration works. The device will give you the acceleration (gravity included unless you use device motion) at whatever moment you read it at.
if you want to get velocity you have the time factor which will compensate for the difference in fps. If you want position then things get much more complicated because there will be the noise from the double integration, as well as that you will have to take into consideration that the readings might not change as fast as you expect. According to my experiments you can safely set it to around 70 times a second, but it does change with what device you use (4s or 4 or so on)
So for reference:
The accelerometer gives you acceleration, this will always be a single reading which depends on the acceleration of the device at any given time.
If you integrate the acceleration you get velocity, this is what you seem to be trying to get, the equation is as follows:
v = u + a * t
Now here see carefully how to get your total velocity you add to the initial velocity (your previous velocity u) the new one which is the acceleration multiplied by the time (the acceleration is the reading from the accelerometer, and the time is the time interval between readings).
For example:
Your object starts from no movement, acceleratin should read 0 (unless you are using bare accelerometer and not motion manager in which case you will see gravity). now you make it move rapidly to the right and the device takes 5 readings with .2 secs of diff each.
so you get lets say 1 2 1 -1 -2 … that is because at first it accelerates but when it starts slowing down it is deaccelerating but still, just consider the first reading, your velocity at that time is 0 * 1*.2, now at time to is .2 + 2 * .2 etc etc. Ideally all the velocity from when it started moving to when it finished moving should sum up to 0 because of the laws of motion.
Now if you happen to want to get the displacement, THAT is super complicated to get accurately, because for it you have to integrate again, and a double integration is madness in terms of error.
there are several ways to get the displacement, In fact i remember a couple of posts here in stack overflow which discuss how you can get it using the device accelerometer.