Similar to your algorithm, this is the algorithm I would suggest.
Set your variables. Choose armCount
arms, radius
radius, spinFactor
for spin factor, armR
for arm radius, starCount
for star count and point
center point.
- Place the
armCount
arms pointing straight away from a central point point
. These should be roughly equidistant around the circumference of the circle (with a radius of radius
entered on point
) that makes up the galaxy.
- Divide
starCount
into roughly equal armCount
portions.
- For each arm place
starCount/armCount
stars randomly inside the cylinder that follows the arm with a radius of armR
. Star placement should be weighted in the following way:
- More likely to be placed towards the core than towards the arm tip.
- More likely to be placed closer to the center of the cylinder than the outside of the cylinder.
- Now you have a galaxy shaped like a star with
armCount
arms. With more stars at the core and the arms somewhat distinct. Time to rotate. For each star, take its distance from the center and multiply that by spinFactor
to get a rotationAmount
.
- Rotate each star by the rotation amount clockwise or counter clockwise around the center point.
- Marvel at the resulting spiral galaxy.
You could probably combine the rotation and placement steps and just place the start initially at their rotated position. I'd do it in separate steps to start with, just so you can see how things form up without being all twisted up.
Bonus answer: Humans haven't really had the star viewing technology long enough to watch a galaxy rotate, so we don't know exactly how it looks. However, from viewing simulations, it looks like it's spinning at higher speed in the center than the outside. You can try applying the same rotation to each star as you do in step 5. Or you can just slowly rotate the entire galaxy and see how it looks.