For a 2d sprite simulation I have a grouping of different sized planets. Their size represents their mass. The each have a randomly generated color. When they collide I would like to mix the colors based on their mass, ie a small black + a big white should be slightly gray. How can I do this in a RGB system?
Not sure if I got the question right but… maybe add the colors and divide by two?
color.r = (color1.r + color2.r)/2
color.g = (color1.g + color2.g)/2
color.b = (color1.b+ color2.b)/2
int totalMass = a.mass + b.mass;
int r = ((a.r * a.mass) + (b.r * b.mass)) / totalMass;
int g = ((a.g * a.mass) + (b.g * b.mass)) / totalMass;
int b = ((a.b * a.mass) + (b.b * b.mass)) / totalMass;
It is just a weighted average:
(color1 * diameter1 * diameter1) + (color2 * diameter2 * diameter2) / (diameter1 * diameter1 + diameter2 * diameter2)
Er, yes, as per riven’s post, my method is 2D which can be considered wrong. Of course the color derives from the surface layer so considering diameter squared is arguably better than considering mass.
Planets are spheres, not circles.
When planets collide, their original surfaces won’t be very important
But they aren’t the same color the whole way through… And the light crust will rise to the top regardless. If the small black planet is pure iron, and the white planet is mostly quartz, the new planet is going to be white.
thanks this is what I needed. I was near doing this, but was confusing the area/ mass with the diameter.
Actually, if the planets are anywhere near equal in size, the color of the new planet will be glowing red lava! (assuming rocky planets) ;D
Maybe make the planets go red on impact, and then let them cool down over time to the weighted average color?
Unless the orbits are almost identical, the two planets wouldn’t coalesce anyway