Looking for Data Structure suggestions
I am working on an Asteroids clone and am having trouble deciding how to store the information about my asteroids. This is not really an OGL question, more pogramming theory really... , but I am getting frustrated. I need to keep track of say at most 50 BIG asteroids when they blow up they release 2 medium sized asteroids in random directions with random velocities. When a medium asteroid gets destroyed we need two little asteroid chunks to shoot out in a random fashion as well... How can I keep track of this dynamic environment easilly?
Any help is greatly appreciated.
--------------------------
-=ZP|9r8AjAx=-
"If you code it, they will come..."
---------------------------=ZP|9r8AjAx=-"If you code it, they will come..."
make a class for an asteroid.
instantiate 2 asteroids in one asteroid1 object,
then instantiate these 2 asteroid2 objects into another asteroid object, (the biggest one).
so, you really only need one class, and just have the same instance in itself when creating the bigger, new one.
a2k
instantiate 2 asteroids in one asteroid1 object,
then instantiate these 2 asteroid2 objects into another asteroid object, (the biggest one).
so, you really only need one class, and just have the same instance in itself when creating the bigger, new one.
a2k
------------------General Equation, this is Private Function reporting for duty, sir!a2k
I''m going to suggest one way right off the top of my head, so it probably isn''t full proof but should give you some ideas.
Create an asteroid class, with some member functions like spawn touch and die (no, don''t make the spawn/die functions the constructur/destructor).
Now when your big asteroid gets shot, call this die method which contains code to create two medium sized asteroids, and these medium sized asteroids create small ones when they die.
You could do some nifty stuff with inheritance and virtual functions to get the effects set up correctly, but I''m not going into detail on that.
-Mezz
Create an asteroid class, with some member functions like spawn touch and die (no, don''t make the spawn/die functions the constructur/destructor).
Now when your big asteroid gets shot, call this die method which contains code to create two medium sized asteroids, and these medium sized asteroids create small ones when they die.
You could do some nifty stuff with inheritance and virtual functions to get the effects set up correctly, but I''m not going into detail on that.
-Mezz
Have an asteroid struct/class and have a flag in it determining whether the asteriod is big, medium or small. Then have a function/method which deletes one asteriod and makes two in its place with a random velocity one size smaller than the asteroid. At the top of the function check if it is a small asteroid, in which case just get rid of it.
------------------------------
#pragma twice
------------------------------
#pragma twice
9r8AjAx, this seems like a stupid post, but if you check my website in a few days - there are a bunch of new tutorials...
I think it might be worth a try
I think it might be worth a try
Hey guys thanks for the posts...
I have declared my Asteroid struct which has xpos,ypos,angle,velocity, and size... thats is what I had so far.
I like the idea of having each big asteroid keep track of the smaller ones...
But my question still stands... What kind of data structure would you guys use to keep track of the whole asteroid field...?
It would have to be easy to travers through for when I render the scene...Maybe some kind of linked list? Or what about a tree structure? hrmm interesting thought there...
Aha... I got it...
A linked list of linked lists... =)
Three links in the first list to represent B, M , and S asteroids... Then off of those nodes the lists of the actual asteroid objects... It seems that that wouls be a little easier to maintain... Maybe I am an idiot though... Let me know what you guys think...
--------------------------
-=ZP|9r8AjAx=-
"If you code it, they will come..."
I have declared my Asteroid struct which has xpos,ypos,angle,velocity, and size... thats is what I had so far.
I like the idea of having each big asteroid keep track of the smaller ones...
But my question still stands... What kind of data structure would you guys use to keep track of the whole asteroid field...?
It would have to be easy to travers through for when I render the scene...Maybe some kind of linked list? Or what about a tree structure? hrmm interesting thought there...
Aha... I got it...
A linked list of linked lists... =)
Three links in the first list to represent B, M , and S asteroids... Then off of those nodes the lists of the actual asteroid objects... It seems that that wouls be a little easier to maintain... Maybe I am an idiot though... Let me know what you guys think...
--------------------------
-=ZP|9r8AjAx=-
"If you code it, they will come..."
---------------------------=ZP|9r8AjAx=-"If you code it, they will come..."
If you just want to keep track of the whole asteroid field (not too sure what this means... supposing that you want to know if the asteroid exists or not) then have a boolean array that just keeps track of them.
Otherwise, use your class to handle the coords of each size, just destroy the bigger asteroid and create a new one. If you wanted to get reeeeeeeeeally crazy, you could have a class called AsteroidField, then inside each of those have a declaration for 50 asteroids (different declared class) and set the size as you declare it...
ie, your constructor looks like
class Asteroid()
{
public:
Asteroid(int SizeOfAsteroids);
...
};
This number would specify large, medium, and small... That would be a way around it, then just declare a number of asteroids that are alive determined by the size... etc.
That''s a long hard way to do it.
S.
Otherwise, use your class to handle the coords of each size, just destroy the bigger asteroid and create a new one. If you wanted to get reeeeeeeeeally crazy, you could have a class called AsteroidField, then inside each of those have a declaration for 50 asteroids (different declared class) and set the size as you declare it...
ie, your constructor looks like
class Asteroid()
{
public:
Asteroid(int SizeOfAsteroids);
...
};
This number would specify large, medium, and small... That would be a way around it, then just declare a number of asteroids that are alive determined by the size... etc.
That''s a long hard way to do it.
S.
i done a asteroid clone awhile ago with source http://members.xoom.com/myBollux
Hi, a really easy way of managing multiple data elements which change over time or whatever is definately linked lists.
You can easily have linked lists within other linked lists, and that way, you only store data for objects which are active (therefore not wasting space on inactive objects).
As long as you get the functions pretty much right for adding/deleting list nodes, the rest is easy...
I use linked lists for heaps of things, and though people claim they are slower than arrays etc, I haven''t noticed any significant performance hits.
I cant honestly imagine how you would manage vast numbers of objects WITHOUT linked lists (eg large particle systems etc)
anywayz thats my little bit of dodgy opinion
Cel
You can easily have linked lists within other linked lists, and that way, you only store data for objects which are active (therefore not wasting space on inactive objects).
As long as you get the functions pretty much right for adding/deleting list nodes, the rest is easy...
I use linked lists for heaps of things, and though people claim they are slower than arrays etc, I haven''t noticed any significant performance hits.
I cant honestly imagine how you would manage vast numbers of objects WITHOUT linked lists (eg large particle systems etc)
anywayz thats my little bit of dodgy opinion
Cel
Cel aka Razehttp://chopper2k.qgl.org
September 26, 2000 02:17 AM
that''s actually a very good question.
linked lists or trees are very fast and efficient, but even a boolean array of 50 objects is not a big deal. (your hit tests will be)
as for the structure of the asteroid, easiest is just an asteroid class that creates / destroys asteroids of 3 different sizes. you could still inherit values from the parent asteroid by passing parameters to the destroy method like xspeed, xdir, whatever.
don''t make it to complicated though, there''s no need to.
linked lists or trees are very fast and efficient, but even a boolean array of 50 objects is not a big deal. (your hit tests will be)
as for the structure of the asteroid, easiest is just an asteroid class that creates / destroys asteroids of 3 different sizes. you could still inherit values from the parent asteroid by passing parameters to the destroy method like xspeed, xdir, whatever.
don''t make it to complicated though, there''s no need to.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement