# Linear Interpolation

This topic is 4813 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

im trying to build a way to change colors and/or materials in my particle system based on their life. i have the age of the particle, the max lifetime for a particle, starting color and ending color. i feel sooo close to getting the right formula. this is what i have for one color channel:
r = (colorDeath.r-colorBirth.r)/(pArray[x].age/lifetime);

obviously it doesnt work. what do i need to change?

##### Share on other sites
The clearest statement of linear interpolation:

f(x0, x1, t) = x0*(1-t) + x1*t

Calculate that out with t=0 and with t=1, and see what the values are.

A crank of the algebra machine turns that into

f(x0, x1, t) = (x1 - x0) * t + x0

which is the same function, but expressed as a vector scaling. It's a bit more performant in a lot of cases. The first formulation is easier to understand, though, especially if you're not that into linear algebra.

##### Share on other sites
i implemented and tried Anonymous Poster's formula. all i end up with is a flash of the final color. colorBirth and colorDeath are float, but age and lifetime are int. that shouldnt cause a problem with ints should it?

im assuming if i set the birth color to white and the death color to black i should end up with a steady gradient from white to black with a gray middle right?

##### Share on other sites
Integer division is integer division. Remainder is truncated. Thus, (1 / 2) = 0, and (1.0 / 2.0) = 0.5. Cast to double BEFORE you divide.

##### Share on other sites

"pArray[x].age / lifetime" sets up a float function ranging from 0.0 to 1.0. If they are both ints, then the division will be truncated to 0 until age is equal to lifetime, only then will it be equal to 1. In theory this would cause your color to remain the same as the starting color for the duration of its life and then flash to the final color for a quick moment at the end.

You should set a break point on the line, step over it a couple of times, and make sure the math is doing what you expect it to do.

Learning to debug code efficiently and effectively is the best skill you can develop as a computer programmer.

Cheers mate, and good luck!

##### Share on other sites
HELLZ YEAH!!!! it was like 4 in the morning when i was working on that. it was an easy fix, just change the types from int to float.

1. 1
2. 2
3. 3
4. 4
JoeJ
12
5. 5
Rutin
11

• 12
• 16
• 13
• 20
• 12
• ### Forum Statistics

• Total Topics
632178
• Total Posts
3004598

×