3D Starfield - Need Help

BlitzMax Forums/BlitzMax Programming/3D Starfield - Need Help

Rico(Posted 2012) [#1]
I have made a lot of 2D starfields but never a 3D one. I wanted to do a little intro for my game, and the idea was to have a starfield that started scrolling like a normal 2D starfield and then after a little time, the scroll direction would change into the screen. Which i thought would look cool. I also wanted to spin the viewpoint and general move around within the 3D starfield.

I found this really nice routine on the BlitzMax forum which makes a 3D starfield. Is it possible someone could modify it so i can move around in the 3D starfield using cursor keys and also rotate viewpoint? I have no idea about doing 3D stuff i am afraid

3D Starfield by forum member Tesuji



Last edited 2012

Last edited 2012


Muttley(Posted 2012) [#2]
How will you learn 3D if you get other people to write the code for you?

Anyway, saying that here's a few hints:

1. That example already translates 3D positions into 2D screen positions, so you don't need to do anything extra there.
2. The Star3D objects already contain 3D positions (xPos, yPos, zPos)
3. Positions are updated with this code: s.zPos = s.zPos - (s.zVel*speed)
4. Try updating the star positions in more than just the Z direction (into and out of the screen) and see what happens.


Rico(Posted 2012) [#3]
well i just wanted to make a little intro for a 2D game, this would just be a small part of the intro but sure i will have a go at learning a little. Thanks for the tips, i will try and see what i can get working tomorrow :)


Rico(Posted 2012) [#4]
Hi ok i have modified the programme so it scrolls horizontally and then if you press F1 it starts to scroll into the screen. Press F2 to make it scroll horizontally again

I also changed the look a little cos i wanted a bit more of a 16-bit looking starfield so i made the stars scale a little.



I need some help because the way it was distrubuting the stars before was like in a long tunnel from the screen into the distance, so the stars at the back were just in a small rectangle compared to the big square at the front of the screen.

I tried to modify this by shoving these lines in ( i dont know exactly what i am doing but i rearranged the equations used to find the screen coordinates) I figured this should spread the stars out accross the screen at all depths. Well it works better but i'm not sure its quite right. Can someone please help on this?. I am using these same equations to find the limits of the edges of the screen for the 2D starfield and it means that stars are reappearing in strange positions when its scrolling horizontally, so need to fix that.

Please help! thanks :)

llimit=(zPos*(0.0-(Float(GraphicsWidth())*0.5)))/100.0
rlimit=(zPos*(1023.0-(Float(GraphicsWidth())*0.5)))/100.0
ulimit=(zPos*(0.0-(Float(GraphicsHeight())*0.5)))/100.0
blimit=(zPos*(1023.0-(Float(GraphicsHeight())*0.5)))/100.0

xPos = Rnd (llimit, rlimit) 
yPos = Rnd (ulimit, blimit)


Last edited 2012


Rico(Posted 2012) [#5]
oops made a silly mistake, ok it appears to be working fine. Could someone check my star-distribution formulas are ok though. Thanks *thumbs up* Also the starfield thins out a bit when i scroll forward into it. i dont like that.

Working code



Last edited 2012


zambani(Posted 2012) [#6]
Star distribution look fine and even.


Rico(Posted 2012) [#7]
Thansk Zambani. I think i figured out why it is less dense when it goes into the screen, its because i am drawing the stars all spread out over the screen so many of the stars near the back don't even get anywhere near the front of the screen as they move forwards.

I tried to fix that a little and i have added rotation (use cursor keys left and right to rotate, preferably after pressing F1 to make starfield go 'into the screen')



I would really like to know now how to give the appearance of flying around in a spaceship within the starfield (from a first person view), so basically the camera can move at any angle?

Last edited 2012

Last edited 2012


John G(Posted 2012) [#8]
Rico, good job. Looks neat here.


ImaginaryHuman(Posted 2012) [#9]
You could use OpenGL, create a perspective projection and just set a bunch of vertices with 3D coords. Then moving the model or camera flies you through it.


Rico(Posted 2012) [#10]
Thanks very much John, its pretty much looking how i hoped now

Thanks ImaginaryHuman, is it possible to 'hack' it so i just change the angle of the stars and then when they go off the screen replace them in whatever direction they are coming from?.because I was thinking i would need a lot of stars if i am flying through a proper 3D space with them in to get the same kind of star-density i have now.


Muttley(Posted 2012) [#11]
@Rico: When a star is no longer visible how about repositioning it in the opposite direction, so if it's moving left and towards the screen when it becomes no longer visible, reposition it far-right and back, etc.


Rico(Posted 2012) [#12]
yes that's what i was thinking of doing to avoid doing a proper big area full of stars. i dont really know how to do the rotation of the stars in 3D space, i only know the 2D rotation formulas but i will look it up and see if i can make it work. thank you :)


Rico(Posted 2012) [#13]
You can see my little intro here now

http://www.youtube.com/watch?v=OyzUrVe5l_8

(YouTube doesnt really like starfields but it's all smooth in real-life)

hope you like it. Heavily inspired by old Amiga demos but quite simple too. I am trying to recreate that slightly cheesey 90s brashness. The game is going to be like a 16-bit style shoot-em up so will suit it down to the ground.

i decided i didn't really need to fly around in 3D space, esp since it was just for my intro!

Last edited 2012