1
\$\begingroup\$

I want to make a particle that interpolates in Alpha and velocity.

public class Particle extends GameObject implements Updateable
{

    int time;

    int lifetime;

    int Alpha;

    double dx;

    double dy;

    int[][] alphaEnvelope;

    double[][] speedEnvelope;


    @Override
    public void update()
    {
        time++;

    }


}

this is my particle class.

alphaEnvelope[x][y] changes the Alpha over time where X is the time and Y is the Alpha Value.

For example:

[1][100]

[60][70]

[70][0]

the Alpha is 100 at lifetime 1, 70 at lifetime 60 and 0 at lifetime 70.

How do I interpolate between these values fluently?

\$\endgroup\$
  • \$\begingroup\$ there are always only and only 3 points known? \$\endgroup\$ – dnk drone.vs.drones Aug 27 '15 at 10:39
  • \$\begingroup\$ no, the Array is different for every particle \$\endgroup\$ – Raildex Aug 27 '15 at 11:08
  • \$\begingroup\$ What kind of interpolation do you want/need? If you are not sure, you can show it with a picture. \$\endgroup\$ – wondra Aug 27 '15 at 16:17
  • \$\begingroup\$ A linear interpolation will do it for the moment. I have an array of X Values and an Array of Y values. my X0 increases over time and I want to get the Y values for X0. i.imgur.com/pgUP32w.png My code now looks like this: It does work a bit, but if the second Key is selected, it does not interpolate anymore but the alpha changes instantly. pastebin.com/mmnPUr8s \$\endgroup\$ – Raildex Aug 27 '15 at 16:32
1
\$\begingroup\$

Linear interpolation you can do with following code:

int l = 0;
for(int i = 0; i < keys.Length; i++)
{ 
if(time > keys[i]) l = i;
}
double interval_len = keys[l + 1] - keys[l]; //r = l + 1
double k2_a = (time - keys[l]) / interval_len; //k1_a = 1 - k2_a
return k2_a * values[l+1] + (1 - k2_a) * values[l];

It is very similar to your code - you were on the right track. Code explanation: first we find between which keys the time lies, then we find the interval length, after that we subtract left value from time to get "local time on interval". And lastly we find "how much close" it is to right boundary by dividing local time by interval length. We multiply right value by the result and finally we multiply the left value by complement and sum it for desired result.
If you wanted more smooth values you could for example interpolate by cubic spline solving system of linear equations (not hard to find, you will be likely looking for chord parameterization and natural spline bounding conditions).

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

You can try some polinomial interpolation : i.e lagrangian interpolation

In your example it becomes (if I calculated it right ):

L(x) = 100 * ((x-60)/(1-60)) * ((x-70)/(1-70)) +
       70 * ((x-1)/(60-1)) * ((x-70)/(60-70)) +
       0 * ((x-1)/(70-1)) * ((x-60)/(70-60)) +
\$\endgroup\$
  • \$\begingroup\$ A word of warning would be in order for this answer: lagrangian interpolation is a global interpolation and it is not very stable - it can give you really crazy results, including values over 100 or below 0 for your kind of inputs. \$\endgroup\$ – wondra Aug 27 '15 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.