Advertisement

stars for 2D space game

Started by September 19, 2004 09:06 PM
4 comments, last by markr 20 years, 2 months ago
I have already created a background of stars made up of GL_POINTS that have random x and y positions. The game is going to have a space ship always centered on the screen. As the ship moves forward the ship will stay centered and the stars will move off the screen. My problem is that I am not sure how to generate new stars as I move to new areas of space at the same time as keeping the old stars in the same formation until they have left the screen. I'm using the same seed for the random numbers before the stars are drawn, so they show up in the same place every frame. I tried using the sugestion in this post, which I think is saying to use the ships position as a seed. I could not get this to work, the stars completely changed everytime I moved. Maybe I didn't understand the meaning of the comment from that other thread. If this is confusing I will explain it better or show examples. Thanks
This won't be too hard for you to do.

Just keep an array of the stars positions, when a star leaves the screen place it on the edge of the opposite end.
Advertisement
I'm not sure but I think if I do that, after a little bit of playing the game you would notice the paterns of the stars repeating. Still that is a good idea of something to try. I would like to try for something that would make the stars always random, I also don't mind if when the ship returns to it's starting point the stars no longer have the same pattern as they originally did.

Thanks
Well you should know what way your ship is going so it's not that hard.
If star i is out of the screen and you're heading up then just do like:
if(star.x < 0 || star.x > SCREEN_X || star.y < 0 || star.y > SCREEN_Y) {
if(heading = UP)
{
star.x = rand()%>SCREEN_X;
star.y = 0;
}
//if your heading down to the left
if(heading = DOWNLEFT) {
if(rand()%2 == 0)
{
star.x = rand()%SCREEN_X;
star.y = SCREEN_Y;
}
else
{
star.x = 0;
star.y = rand()%SCREEN_Y;

}
}
well... hope some of that helps
Maybe you could have an array of stars, and when a star leavs the screen you generate a new one at the beginning of the screen? That way you won't have a pattern ...
-----------------SloGameDev.net | My homepageQ:What does a derived class in C# tell to it's parent?A:All your base are belong to us!
I made something exactly like this, it work by having a fixed small number of stars, and wrapping them around an area bigger than the viewport such that the user never sees them disappear and reappear (but they do)

(Difficult to explain, here is some code)

// Starfield - only one instance of this is required.class Starfield : public DisplayObject {        private :                 std::vector<Point> *stars;                int phase;        public:                Starfield();                virtual void draw(const View &v);                virtual void tick(float quantum);                virtual bool isDead();                virtual ~Starfield();};


Here is the .cpp

const int STARFIELD_NUM_STARS=40;const int STARFIELD_SIZE=480;const int STARFIELD_HALF=STARFIELD_SIZE / 2;const int STARFIELD_PERIOD=100;const int STARFIELD_HALF_PERIOD=STARFIELD_PERIOD/2;Starfield::Starfield() {        stars = new std::vector<Point>(STARFIELD_NUM_STARS);        for (int i=0; i<STARFIELD_NUM_STARS; i++) {                int rx, ry;                rx = rand() % STARFIELD_SIZE;                ry = rand() % STARFIELD_SIZE;                (*stars).x = rx;                (*stars).y = ry;        }        phase=0;}void Starfield::draw(const View &v) {        for (int i=0; i<STARFIELD_NUM_STARS; i++) {                float xoffset, yoffset;                float x,y;                x = (*stars).x;                y = (*stars).y;                xoffset = floor((v.centre.x - x +STARFIELD_HALF) / STARFIELD_SIZE);                yoffset = floor((v.centre.y - y +STARFIELD_HALF) / STARFIELD_SIZE);                xoffset *= STARFIELD_SIZE;                yoffset *= STARFIELD_SIZE;                // now add that to x and y                Point drawPoint;                drawPoint.x = x + xoffset;                drawPoint.y = y + yoffset;                // convert to screen coords                ScreenPoint sp = v.pointToScreen(drawPoint);                int colour = makecol(255,255,255);                putpixel(v.targetbm,sp.x,sp.y,colour);        }}


Mark

This topic is closed to new replies.

Advertisement