Jump to content
• What is your GameDev Story?

• Advertisement

Linear Interpolation

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

If you intended to correct an error in the post then please contact us.

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 this post

Share on other sites
Advertisement
r = colorBirth.r + (colorDeath.r-colorBirth.r)*(pArray[x].age/lifetime);

Share this post

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 this post

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 this post

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 this post

Share on other sites
Adam17,

"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 this post

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.

Share this post

Share on other sites

• Advertisement
• Advertisement
• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• Popular Now

• 9
• 10
• 11
• 13
• 9
• Advertisement
• Forum Statistics

• Total Topics
634094
• Total Posts
3015469
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!