Jump to content
  • Advertisement
Sign in to follow this  
feedayeen

[java] Managing Thousands of Objects for a Planetarium

This topic is 3752 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to write a software rendered planetarium. I have a Star.class that contains methods that will transform their coordinates from world space into view space, but I need to find a simpler method to create thousands of instances of this class and control them. Right now I am doing this to create the stars.
Star Polaris = new Star();
Star AlphaCentari = new Star();
Polaris.setLocation(430Ly);
AlphaCentari.setLocation(4Ly);
With this system I would have to enter thousands of lines just to set up all the instances of my Star.class. Is there a way that I can create the instances of these classes and control their methods using an array? This would allow me to store all the stars in a file and load it during runtime.

Share this post


Link to post
Share on other sites
Advertisement
Feed,

Just add the name of the star as a string to your star class. Then you can initialize an array of however many stars you'd like and read in the name and location from a file.

I'm not particularly familiar with Java so I'm not sure the exact syntax or Object/method names. In general however, you're going to do something like the following:

Open the File
Ask the file how many stars you have or compute it from the size of the file
Allocate memory for your array of stars
Iterate through the file, reading one star at a time
- Take the data from the file and set the values of the star at the current index
- increment the index
Close the file

If you've serialized your stars as in-memory objects, of if there's a class that allows you to do so, you could also read in the entire array of stars in one swoop and assign it to your pre-allocated array. Again, not being as familiar with Java I'm not sure if it has the functionality/library support to do this, however you could do this easily with any of the .NET languages.

Cheers and Good Luck!

Share this post


Link to post
Share on other sites

public class Star {
Star(string name, double distance)
{
this.name = name;
this.distance = distance;
}

void print()
{
System.out.println(name + " - " + distance);
}

string name;
double distance;
};

...

List<Star> stars = new List<Star>();

stars.add(new Star("Polaris", 430);
stars.add(new Star("Alpha Centauri", 4);


foreach (Star s : stars) {
s.print();
}[/source]


Note that the above applies to Java 6 (perhaps 5) due to use of generics.

This makes it trivial to implement file I/O.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!