0
\$\begingroup\$

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.

\$\endgroup\$

3 Answers 3

2
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ Thanks for u answer. One question, is my directional vector can use as a velocity or is that a distance vector \$\endgroup\$ Mar 14, 2014 at 21:01
  • \$\begingroup\$ Whatever your coordinate origin, provided your coordinate axes are perpendicular to each other (the norm) you can compute and store the X-, Y-, and Z-coordinates of all vectors independently. All three properties (displacement, velocity and acceleration) are vector quantities. \$\endgroup\$ Mar 14, 2014 at 21:11
3
\$\begingroup\$

Every frame:

acceleration <-- from input

velocity = velocity + acceleration * t   
position = position + velocity * t  

with t= time passed since last frame

UPDATE:

Reading again the question I thought you would need also the direction. I didn't specified because I assumed that acceleration, velocity and position are vectors.
By the way if you have just the magnitude you need to take your direction vector, normalize it and multiply it times the magnitude.

\$\endgroup\$
1
\$\begingroup\$

You just have to add acceleration to your speed.

For every "frame" you have to do

v = u+at

where v is the new speed, u is the old one, a is the acceleration and t is the time

http://en.wikipedia.org/wiki/Linear_motion#Acceleration

\$\endgroup\$
1
  • 1
    \$\begingroup\$ I thought v should be v0 + a * t, not v0 * a + t \$\endgroup\$
    – Leggy7
    Mar 14, 2014 at 11:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .