Compartilhar via


Large Star Field in Silverlight - Taste of Game, Part 2

Edit2: After posting this, I made a significant improvement on the star field... will post it tonight. In the new version there's more sense of depth not just "dots" like the one below. The star speed and brightness now is calculated better (based on square of the distance and correct view angles). See the next update tonight...will look a lot better :)

A good space game can't go without stars :)

Source code: https://nokola.com/sources/StarFieldOld.zip (the new one is in newer post)

Check out the Silverlight star field below, its intentionally large-size so that I can finally appreciate the night sky :)

Edit: I'm stupid...Brightness should be in a range 0..1 (or 0..2 or something like that)...please add / 100.0 to the Brightness (star.Brightness = Globals.Random.Next(256) / 100.0;) when you download the source. I'll update the effect when I get back home tonight.

It looks 1000 times cooler with the correct opacity...

Notice the dimmer, slow moving stars in the background. Notice the CPU usage. It's 2-3% 1080P here , if it is higher on your side, please ping me!

Also, since I made the background, you are free to use it for your purposes (credit me or not - I don't care)!

Below the Star field read a few notes on how its built.

this is the Old version of the star field.

Stars are represented with Ellipses on a Canvas. Each star has a X, Y, and Z (distance) position.

The X and Y are just normal screen coords (e.g. 0..1024 for X), depending on the control/screen size. The Z is in the range 0...1.

1 means "farthest". Z determines how fast the star moves and the size of the Ellipse.

Each star has a Brightness as well - approx 0..2. You can probably see why I chose Z to be from 0..1 now - because Z * Brightness = Star control's Opacity. It just makes things easier...

 On every frame I move the ellipses - see the code.

Comments

  • Anonymous
    August 10, 2009
    Very cool! However, instead of using DateTime.Now to track the speed, I think you should be using RenderingEventArgs.RenderingTime to get the exact time of the frame: private void CompositionTarget_Rendering(object sender, EventArgs e) {    RenderingEventArgs args = (RenderingEventArgs)e;    TimeSpan now = args.RenderingTime;    if (!_lastUpdate.HasValue) //First assignment    {        _lastUpdate = now; return;    }    double msec = (now - _lastUpdate.Value).TotalMilliseconds;    ... } http://msdn.microsoft.com/en-us/library/system.windows.media.renderingeventargs(VS.95).aspx

  • Anonymous
    August 10, 2009
    Hi Morten! Thank you...this is really interesting. The Silverlight IntelliSense gives me "do not use" for the property RenderingTime...although it seems to be working. I'll check with the Silverlight team tomorrow to see if this is an issue with the documentation or they had a valid reason (speed improvement most likely) for this. Until them I'm using DateTime.Now just in case :) Thank you for your suggestion! btw, the new version of the StarField is going out in ~15 minutes...please let me know which one you like better (the new or the old one..) Thanks, Nikola

  • Anonymous
    August 11, 2009
    I already asked about this, and it's apparently a documention error. They are working on fixing it, but MSDN takes months to update : http://silverlight.net/forums/t/115994.aspx

  • Anonymous
    August 11, 2009
    oh ic...interesting I wonder if there's any perf implication by using it, anyway. Maybe there was in the beginning and there isn't one anymore (this is a speculation - I'll ask)

  • Anonymous
    August 12, 2009
    OK please post what you find. I would be interested in knowing those details as well.