How do you create a 3D starfield within a 3D world that will regenerate the stars (loop them) and simulate turning as well. The 3D starfield is easy and I normally don't make the stars a part of the actual 3D world - they are in local space totally separate from the rest of the world. It's easy to regenerate them but it is not easy to get them to simulate the feel of turning. I know that the stars must be translated relative to the spacecraft so that the rotation looks correct, but I've not been able to do this since the stars are really not part of the main rendering loop. Translating the stars by certain amounts relative the speed of the craft would work because it would move the center of rotation of the field by that much. But my stars are already in local space and translating them would effectively change their original coordinates thus resulting in a huge mess due to rounding errors.
Making the stars part of the 3D world introduces a whole new plethora of problems since the stars must always fly towards or away from the spacecraft to simulate movement. Regenerating the stars at the correct angle and rotation and z depth from the spacecraft is not simple at all. The rotation around the craft works very well, but if the stars do not regenerate, then you effectively have a small block of stars in a huge 3D space - not good and not pretty. And to generate thousands of 3D points for one effect is not an intelligent use of the system resources and even with millions of points you could never simulate a good starfield.
Also, how do you avoid gimble lock with euler angles or should I switch to quaternions? Gimble lock is not a major problem in FPS games since you usually are on the ground - but gimble lock in a space game is a huge problem - check out Battlecruiser Millenium to find out why. Left in space should always turn left - but at 90 degrees pitch up, left would only rotate in place, not turn the craft left. This is fine for simulated planet-bound flight, but not in space. Any ideas?
For a good demo of the effect I'm after check out Freelancer and watch the starfield - the foreground starfield that flies all around you. Turn and pitch and watch the field. Then try to code it and you will see my problem.
I could code the whole deal with no starfield, but the movements in my 3D space are so small compared to the size of the world that it really does not look like you are moving at all. Boring.
To avoid Gimbal Lock you want to definitely use Quaternions. Its easy to find a free-ware quaternion library that will transform all your vector and matrix stuff to quaternions. I think I still have one floating around somewhere.
As for the starfield, are you using billboarded sprites or are you setting pixels or varying colors to simulate distance? I have a friend who made an exceptional starfield simulation. If I can contact him I will get some input on it.
You don't have to use quaternions to avoid gimbal lock. The only other reason for using quaternions is that they cut down the number of rotation calculations by about half. I've created my own 3D starfield using regular vector rotation equations and avoided gimbal lock by just always setting the 'up' vector to the cross product between the view and strafe vectors. Or, you can just rotate the up vector the same number of radians that you've rotated your view vector about the strafe axis.
EDIT: I have no clue what you meant by 'simulating turning'. To generate my starfield I did this for each billboarded bitmap position (all visible stars were drawn as glQuads)
All that code does is generates a sphere of stars all the same distance away (but each had a different size so it appeared as though some were closer/farther away). In order to keep them at that distance I added a displacement vector to each position when translating out (the player's position was the displacement vector).
Position.x = ((int)rand()%1200) - 600;
Position.y = ((int)rand()%1200) - 600;
Position.z = ((int)rand()%1200) - 600;
Position = Position * Player1->MaxViewDist;
When you turn your spacecraft the stars should seem to rotate around your craft. The center of rotation of the starfield is where the viewer is when the viewer is not moving. As the viewer begins to move, when the viewer turns, the center of rotation of the stars is offset to the left, right, below, and above the craft.
So if you turn left at a certain speed, the stars will start from the left of the screen and rotate into view towards the right side of the screen - if they simply just keep coming at you like a normal starfield, you cannot tell that the spacecraft is turning - unless you have a background bitmap for the sector of space that you are in. Normally the stars start at the center of the screen and move towards or away from the viewer - but when turning/pitching this center ought to change.
Hope this helps.
Thanks for the ideas on how to avoid gimbal lock - I will implement them ASAP. Probably will not use quaternions as I have little or no experience with them.
I did figure out how to get the stars to regenerate.
My thinking here is that no matter how you rotate the viewer - z should always be directly in front of the viewer - this would make it easier to determine where to regen the stars. So the view vector is rotated to align with the viewers current attitude in 3D space.
for (int i=0;i<NUMSTARS;i++)
....choose new random x
....choose new random y
if (Stars[i].Screen.x<LEFT || Stars[i].Screen.x>RIGHT || Stars[i].Screen.y<TOP || Stars[i].Screen.y>BOTTOM)
...Regenerate a new star (same as above code)
The stars are simply pixels right now - billboards seem like overkill just to simulate movement through space. Bitmap Tiling will be used for the backdrop for each system or I might place the viewer inside of a huge 3D cube and texture it like most games seem to have done.
Well, like I said I calculated each star's position in its constructor, and then their positions stayed constant except the player's position was added before translating out. This meant you were always inside a sphere. When you moved the mouse the spaceship (and the view) would change but the stars would stay in the same spot.
Download it from here
You can just extract it to your desktop (it automatically puts it in a folder named 'Game' just so you know) and then just run the .exe. Dont' change the location of any of the files because they are needed. It takes a while to load and only runs at <= 50 fps on my system (because I was using inefficient frustum culling).
here's a picture of what the starfield looks like (except normally you are exactly in the middle of it, and the entire sphere moves out with you but they don't rotate with you so it looks as if they are (as you said) 'turning')
Ok, but do the stars fly by you or are you using them as a backdrop for your universe?? I'm going to create a gigantic textured cube or sphere that you will always be inside of - this will create a nice backdrop for each star system.
That's exactly what mine does...it creates a sphere that you are always inside.