1 2 3 4 5 6 | z = rand(-1,1); // random number between -1 and 1 theta = rand(0,2*pi); // random number between 0 and 2*pi r = sqrt(1 - z*z); x = r * sin(theta); y = r * cos(theta); // The point (x,y,z) is what you want. |
So I do not have to keep track of their position
1 | StarPosition = GetStarPosition(int index, double time); |
Pseudonym73
One thing that's worth mentioning about starfields is that if you're not moving through the star field (i.e. if you just want a celestial sphere), the easiest way to generate the stars is to generate them on the smallest enclosing cylinder (not including the end caps) and then project them back onto the sphere:
1 2 3 4 5 6 z = rand(-1,1); // random number between -1 and 1 theta = rand(0,2*pi); // random number between 0 and 2*pi r = sqrt(1 - z*z); x = r * sin(theta); y = r * cos(theta); // The point (x,y,z) is what you want.
This, believe it or not, generates random points uniformly on the surface of the sphere. The theory behind the technique was discovered by Archimedes. According to legend, this was his favourite proof, and the image of the sphere and the enclosing cylinder was on his tomb.
midnight_mero
If I understand your question right, then yes. Using functions is a common method for generating procedural content whether that's textures (like a starfield) or heightmaps (like terrain) or 3D models, or procedural animation, or... well anything, I guess.
Sounds like you have the right idea, you can combine different transformations of a function to generate a more interesting output.
midnight_mero
More common is to use what's called a noise function. Perlin noise is the canonical example, but there's just no end to the different things you can use. Sin is a 1D function but you can use 2D, 3D, nD functions as well. One reason you might want to use a 4D function, for example, is if you have a 3D field of vectors that change over time (like wind). One thing I recently learned about from Casey is the concept of "blue noise" which is pretty durn neat... It's like white noise but less "clumpy."
midnight_mero
I have thought about how you could do this to generate like a whole city of npcs, for example, but I didn't get very far down the planning route.
d7samuraiSo I do not have to keep track of their position
I think perhaps the OP wants a persistent, moving starfield. Something like
1 StarPosition = GetStarPosition(int index, double time);
where he can call a function, passing in a star index and a time, and have it generate that star's position for the given time procedurally on demand.
midnight_mero
My first thought is if you use a periodic function like sin your content will be periodic eventually, meaning if you zoomed out enough you'd see a repeating pattern in your starfield or terrain. But I think if you change the transformations randomly over time or distance, then you should be able to avoid this?
Kladdehelvete
I even find it to be easy to grasp, yet I would never come up with it, [...]
Can't wait to implement it.Yeah, about that. Square root and the trigonometric functions are two of the most expensive operations you can do, so you may want to consider some approximations. Implementing trig functions via table lookup with linear interpolation (for example) is probably good enough.
Kladdehelvete
yes, but thats what i got too, unless I misinterpret. thanks for answers.
Pseudonym73
Hollasch's Law states that computer graphics is the only area of science where if it looks right, it is right. And Jim Blinn (of Voyager 2 encounter animation fame) always advised his students to follow the ancient and noble art of Chi' Ting.