# Determing an object's position along a curve over time

I have some objects in my game which are "thrown". At the moment I am trying to implement this by having these objects follow a parabolic curve. I know the start point, the end point, the vertex and the speed of the object.

1. How can I determine at any given time or frame what the x & y co-ordinates are?
2. Is a parabolic curve even the right curve to be using?
-
Your inputs are ambiguous. I assume vertex means the staring position. And end point means the ending position. What does speed mean? How far the object can travel in a second? Is speed how long in time the object should take to get from start to finish? – deft_code Oct 23 '10 at 1:58
Apologies for being unclear. I will try to make it simple - I would like to make a ball move from one side of the screen (x=0) to the other side (x=480), starting from y=0, and reaching a maximum of y=320 (at halfway, x=240). I want it to do this over 2 seconds. From this information, can I determine from a formula my x and y coordinates for each frame? – Ben Williams Oct 24 '10 at 22:02

What your looking for a parametric plot of the parabolic function. It's easiest to make the parametric function use a range of p ∈ [0,1].

The canonical form for a parametric parabola is

k := some constant
f_x(p) = 2kp
f_y(p) = kp²

Using this formula and some basic algebra for function morphing and I got

p ∈ [0,1] → x,y ∈ [0,1]
or in other words keep p between 0 and 1 and x,y will be between 0 and 1 as well.
x = p
y = 4p - 4p²

So to get these functions will produce the numbers you're looking for.

``````float total_time = 2;
float x_min = 0;
float x_max = 480;
float y_min = 0;
float y_max = 320;

float f_x( float time )
{
float p = time/total_time;
return x_min + (x_max-x_min)*p;
}

float f_y( float time )
{
float p = time/total_time;
return y_min + (y_max-y_min)*(4*p-4*p*p);
}
``````
-
your notation is a bit confusing. your `f_x` uses `f_y` s acceleration `a` as its half velocity? You should make that `x(t) = x0 + vx0*t` and `y(t) = y0 + vy0*t + .5*ay*t*t` – Tobias Kienzler Oct 26 '10 at 13:18
I purposely didn't use the euler motion formulas. `a` and `t` were just poorly chosen names. You should notice that there isn't a velocity component in the formulas. Euler motion and parametric parabolas are not the same thing, but they are very similar as ballistic flight traces a parabolic path. – deft_code Oct 26 '10 at 14:23
Thanks! This is just what I was after. – Ben Williams Oct 27 '10 at 8:03

Finding the equation of a curve that you want your object to move along is one way to accomplish what you want, but probably not the best.

Instead, one usually keeps track of local properties of an object (velocity, acceleration) and then uses these values to update the object's position every frame.

Since you mentioned a parabola I am assuming that you are throwing a ball in 2D and you want it to fall down along the y-axis. So, your object has constant acceleration in the y-direction (let's call that `g`) and no acceleration in the x-direction. When the object is thrown it is given some velocity, let's call that `vx` and `vy`.

Then, every frame of your application you would add the object's acceleration to its velocity, and then add it's velocity to it's position. Something like:

``````vy += g;
x += vx;
y += vy;
``````

Do this every frame and your ball will start to move. There is a lot more to know about this, but it's a start.

-
I am throwing a ball in 2D, but the ball does have acceleration in the x-direction. I want it to be thrown from one side of the screen to the other (see comment on original question). I understand how to update based on vx and vy, but am not sure how to update those values themselves. – Ben Williams Oct 24 '10 at 22:04
@Ben Williams You only need to set the vx and the vy at the start. And add gravity to the vy each frame. You also could have a friction by multiplying both the vx and vy by a number below 1(something like .95 might work depending on your frame rate). Search on google "bouncing ball YourProgrammingLanguageHere" and you will probably get some basic, but helpful tutorials. – AttackingHobo Oct 25 '10 at 2:22
Actually, if you know that your object is following a parabolic trajectory, it's much, much better to implement it as a curve function, rather than discrete physical steps. Yes, it can be 'harder' to code initially, but the payoff is that the movement of your object becomes decoupled from frame-rate concerns. – Blair Holloway Oct 26 '10 at 6:43
I second @Blair, unless you separate the physics-framerate from the video framerate this may cause horrible effects like clipping errors – Tobias Kienzler Oct 26 '10 at 13:23

Brandon's answer is pretty good, but if you're looking for something more advanced, you might want to check out linear interpolation: http://en.wikipedia.org/wiki/Linear_interpolation

Also, if your curve is a function, you can know the x and y (and z) coords at a given time. This might help too: http://www.ucl.ac.uk/Mathematics/geomath/level2/fvec/fv8.html#l1

-

In console games we often use Bicubic Interpolation to solve this problem. First, sample an object's position at regular intervals of time t. For a projectile, add gravitational acceleration [0,dy/dt/dt] to its velocity [dx/dt,dy/dt] at each interval. Record all the [x,y] coordinates so generated in an array.

Later, to reconstruct the object's position [x,y] for a given t, read the four samples closest to that t from the buffer you recorded: [t-1,t,t+1,t+2]. Blend the four samples according to the coefficients in the linked wikipedia article to get smooth motion in space.

This is not as physically accurate as performing physics calculations on-the-fly but it permits artistic license and economy of scale to assist your simulation.

-