Friday, February 5, 2016

physics - How can I calculate force and acceleration?


I have problem of calculating the movement of the object.


At the moment, I have a vectors calculated from the center of the screen minus the position of the object.


direction vector = centerScreen - positionObject

To calculate the new position of the object


position(new) = position(t) + directionVector * delta

The delta is the time frame rate.



Does anyone know how can I modify the above formula to calculate forces and acceleration? I want to have an object pulling other objects towards it.



Answer



While the equations



v(n+1) = v(n) + a
d(n+1) = d(n) + (v(n+1) + v(n)) / 2



with:



  • d(n) is position at time n;


  • v(n) is velocity at time n; and

  • a is acceleration in distance units per frame per frame


are arithmetically (and physically) correct, they are computationally problematic.


The faster your frame rate becomes the larger the computational error arising from floating point approximation becomes as a fraction of your values for velocity, acceleration and time. Then the further out you extrapolate these approximations from the last change in acceleration, the greater the accumulated error in position and velocity becomes.


The problems arise in attempting to calculate intersection of targeting vectors, or predict future positions.


A better computation is to directly calculate position and velocity from the initial values at the last acceleration change and the elapsed time as:



v(n) = v(0) + a * n
d(n) = d(0) + (a * n * n) / 2




where v(0) and d(0) are respectively the position and velocity at the time of the last acceleration change, and n is the number of frame time-units since the last acceleration change.


To compute the gravitational force between two objects one uses Newton's gravitational law:



F = m1 * m2 * G / (r * r)



and the gravitational acceleration experienced by each mass due to the other will be



g = M * G / (r * r)




where G is the Gravitational Constant


Note that the gravitational acceleration experienced by each mass is only proportional to the mass of the other object, not it's own.


Generalizing the homily above on computational accuracy and precision, with non-constant but calculable acceleration such as in this case, it would be better to use the appropriate Calculus formulae to calculate position and velocity directly at each frame than to calculate each indirectly from the immediately previous values.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...