0

in my game I get the acceleration from the accelerometer. Computing my calculation, I have to apply a coefficient to turn unit of measurementin pixel unit.

I apply the coefficient founded for an Android app (in a sample):

 DisplayMetrics metrics = new DisplayMetrics();
 getWindowManager().getDefaultDisplay().getMetrics(metrics);
 mXDpi = metrics.xdpi;
 mYDpi = metrics.ydpi;
 mMetersToPixelsX = mXDpi / 0.0254f;
 mMetersToPixelsY = mYDpi / 0.0254f;

to my acceleration, getting pixels/s^2. in this way i can use pixel everywhere in my code instead of thinking all in meters.

It is right?

thebestneo
  • 137
  • 1
  • 6

1 Answers1

0

It's going to depend on what sort of physics you want to impose. (This assumes you want Newtonian mechanics.) If you want to track the motion of the device, then you need to integrate the acceleration to get velocity and then integrate the velocity to get position. Or I suppose, you could skip the intermediate step and translate from 'acceleration' to change in position by using 0.5*acceleration^2 (and then multiply that result by an appropriate scaling factor that you will probably need to determine by experiment). (That second method may not properly handle constant motion.) For each independent dimension, velocity and position would be a cumulative sum with these recurrence relations:

velocity[t] = acceleration[t] *(t -(t-1) ) + velocity[t-1]
position[t] = position[t-1] + velocity[t]*(t -(t-1) )
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • Yes I use Newtonian Mechanics. I know how to integrate (verlet o eulerian integration), but I'm not sure about unit of measurement. Is it a correct way multiply the acceleration (from accelerometer) and that constant and get an acceleration in pixel/s^2?? – thebestneo Apr 17 '12 at 17:28
  • You should be able to scale acceleration using the same scaling factor as one would use for distance. – IRTFM Apr 17 '12 at 17:47
  • The acceleration is the only thing I scale. I use pixels everywhere, because, like your relations above, the only units of measurement are seconds and meters. Meters are only in acceleration and if I convert it in pixels/s^2 I obtain pixels also for distance. Do you think it is correct? – thebestneo Apr 17 '12 at 19:01
  • That sounds correct physically. How does it work when loaded and tested? – IRTFM Apr 17 '12 at 19:13
  • I don't know, it seems ok but I have some other things in my function, like friction. The ball seems to move with too much constant velocity. But this is another thing. Without friction is too fast. – thebestneo Apr 17 '12 at 22:20
  • Why here (http://physics2d.com/content/verlet-integration) there is a 2 factor on current position? – thebestneo Apr 18 '12 at 19:03
  • If you are going from acceleration to distance in one step the formula is x=(1/2)*a*t^2. It is a double integral going from acceleration to distance and the 1/2 factor comes from the integration. – IRTFM Apr 18 '12 at 19:42
  • In other sites the 2 factor is not present in the formula, for example: (http://www.gamedev.net/page/resources/_/technical/math-and-physics/a-simple-time-corrected-verlet-integration-method-r2200). I don't understand... – thebestneo Apr 18 '12 at 19:50
  • If you scroll down you will see the 0.5 factor being used. – IRTFM Apr 18 '12 at 19:54
  • He says: _Plugging all of this in yields my final form of the Time-Corrected Verlet integration method:_ ' (7) xi+1 = xi + (xi - xi-1) * (dti / dti-1) + a * dti * dti' Is this the final formula of time corrected verlet, I don't see that factor. I'm a little confused... – thebestneo Apr 18 '12 at 21:22
  • You should look at his eqn (1) and eqn(6). Eqn(6) involves three successive times and has two terms of a*dt*dt. – IRTFM Apr 18 '12 at 22:01
  • this is a sample of Android documentation: ` final float x = mPosX + mOneMinusFriction * dTC * (mPosX - mLastPosX) + mAccelX * dTdT; final float y = mPosY + mOneMinusFriction * dTC * (mPosY - mLastPosY) + mAccelY* dTdT; ` A verlet integration, but here they don't use the _2_ factor. – thebestneo Apr 21 '12 at 08:43
  • I think you should go with what works. When you asked where the 1/2 factor came from I was just telling you the mathematical origin. I doubt it is going to make much difference in this instance. – IRTFM Apr 21 '12 at 12:17
  • Ok, thank you. I'm not satisfied from my physics engine and my questions are for trying an improvement. Thank you very much for your support. – thebestneo Apr 21 '12 at 22:54