Outils pour utilisateurs

Outils du site


back2root:archives:denthor:part-13

PART 13 : 3D Starsfield

Hello again! Here I am, cooped up at home, recovering from my illness with nothing to do, so of course it is the perfect time to write another trainer! After the long delay between parts 11 and 12, two trainers in two days doesn't sound like a bad idea.

This trainer is on starfields, which is by request of more then one person. This is quite an easy effect, and you should have no trouble grasping the concept behind it. I will be doing a 3d starfield, a horizontal starfield is very easy with you merely incrementing a x-value for each star for each frame. I am not even going to bother doing code for that one (unless requested).

So I am off to go grab my antibiotics pills and I will be right back with the tutorial! ;-)

If you would like to contact me, or the team, there are many ways you can do it :

            1) Write a message to Grant Smith/Denthor/Asphyxia in private mail on the ASPHYXIA BBS.
            2) Write to Denthor, EzE, Goth, Fubar or Nobody on Connectix.
            3) Write to :  Grant Smith
                           P.O.Box 270 Kloof
                           3640
                           Natal
                           South Africa
            4) Call me (Grant Smith) at (031) 73 2129 (leave a message if you call during varsity). Call +27-31-73-2129 if you call from outside South Africa. (It's YOUR phone bill ;-))
            5) Write to smith9@batis.bis.und.ac.za in E-Mail.
            6) Write to asphyxia@beastie.cs.und.ac.za

NB : If you are a representative of a company or BBS, and want ASPHYXIA to do you a demo, leave mail to me; we can discuss it.

NNB : If you have done/attempted a demo, SEND IT TO ME! We are feeling quite lonely and want to meet/help out/exchange code with other demo groups. What do you have to lose? Leave a message here and we can work out how to transfer it. We really want to hear from you!

What is a 3d starfield?

I am not even sure if I should do this bit. Go watch any episode of Star Trek, the movies, Star Wars, or just about any sci-fi movie. Somewhere there will be a scene where you can see stars whizzing past the viewscreen, with the ones that are further away moving slower then the ones that are passed quite close to.

This is a 3d starfield. If you look closely, you will see that all the stars seem to originate from a point, the point you are travelling towards. This is an illusion which thankfully happens automatically, you don't have to code for it ;)

Starfields look very nice, and can make a big difference to an otherwise black background. It also makes a great screen saver ;-)

How do they work?

This is actually quite simple. Imagine if you will, each star in the heavens having an x,y and z coordinate, with you being at 0,0,0. Easy? Right. Now, if you were to say move forward, ie. increase your z value, to you you will still be at 0,0,0 , but all the stars z values would have appeared to decrease by the exact same amount.

In easier language, we decrease the z value of all the the stars so that they come closer to you, and eventually whizz past.

This solves all our problems. Stars that are close to us on the x and y scales will pass us by faster then those that are very far from us on the x and y scales. The only thing we must watch out for is that no star is at 0,0 , ie. exactly in front of us, otherwise there will be a collision which will not look good.

How do we code this?

The first thing to be done is to generate our starfield. This is quite easy, with us choosing x values between -160 and 160, and y values between -100 and 100 randomly. Each z is sequentially greater for each star so that we don't get large areas with no stars. We must remember to check that there are no stars at 0,0!

Okay, now we start the actual viewing section. Here are the steps :

1) Convert our 3-d coords into their 2-d versions. Have a look at tut 8 to see how this is done, but basically we divide by z.

2) Clear away all old stars that may be on the screen.

3) Draw all our stars according to our 2-d values we have calculated in 1)

4) Move all the stars either closer to us or further away from us by decreasing or increasing their z values respectively.

5) If a star's z value has passed into the negative, place it at the very back of our “queue” so that it will come around again

6) Jump back to 1) ad-infinitum.

That is, as they say, it. In our sample program the steps have been neatly placed into individual procedures for easy reading.

What next?

Okay, so now we have a cool looking starfield. What next? How about adding left and right motion? A menu or a scrolly in the foreground? How about figuring out how a star tunnel works? A cool 3d routine going in front of the stars?

A starfield can make just about any routine look just that much more professional, and can itself be improved to be a great effect all on it's own.

In closing

So, this was yet another effect in the series. Do you still want more effects, or what? Leave me mail with further ideas for trainers. I may not do it if you don't ask for it!

Oh, well, the medicine has been taken, it is time for me to go. Hello to all those people who have sent me mail, and those great guys on #coders in IRC (you know who you are). Wow. That is the first greets I have ever done in a trainer. Hmm. Maybe I'm just ill ;-)

Happy coding people!

  1. Denthor

19:28

      24-7-94

The following are official ASPHYXIA distribution sites :

╔══════════════════════════╦════════════════╦═════╗
║BBS Name                  ║Telephone No.   ║Open ║
╠══════════════════════════╬════════════════╬═════╣
║ASPHYXIA BBS #1           ║+27-31-765-5312 ║ALL  ║
║ASPHYXIA BBS #2           ║+27-31-765-6293 ║ALL  ║
║C-Spam BBS                ║410-531-5886    ║ALL  ║
║Connectix BBS             ║+27-31-266-9992 ║ALL  ║
║POP!                      ║+27-12-661-1257 ║ALL  ║
║Soul Asylum               ║+358-0-5055041  ║ALL  ║
║Wasted Image              ║407-838-4525    ║ALL  ║
╚══════════════════════════╩════════════════╩═════╝

Leave me mail if you want to become an official Asphyxia BBS distribution site.

back2root/archives/denthor/part-13.txt · Dernière modification : 2021/09/02 15:07 de frater