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\$
4
  • \$\begingroup\$ there are always only and only 3 points known? \$\endgroup\$ Aug 27, 2015 at 10:39
  • \$\begingroup\$ no, the Array is different for every particle \$\endgroup\$
    – Raildex
    Aug 27, 2015 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, 2015 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, 2015 at 16:32

2 Answers 2

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\$
0
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\$
1
  • \$\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, 2015 at 16:20

You must log in to answer this question.

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