Sign in to follow this  

stars for 2D space game

Recommended Posts

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

Share this post

Link to post
Share on other sites
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.


Share this post

Link to post
Share on other sites
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[i].x < 0 || star[i].x > SCREEN_X || star[i].y < 0 || star[i].y > SCREEN_Y) {
if(heading = UP)
star[i].x = rand()%>SCREEN_X;
star[i].y = 0;
//if your heading down to the left
if(heading = DOWNLEFT) {
if(rand()%2 == 0)
star[i].x = rand()%SCREEN_X;
star[i].y = SCREEN_Y;
star[i].x = 0;
star[i].y = rand()%SCREEN_Y;

well... hope some of that helps

Share this post

Link to post
Share on other sites
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;
virtual void draw(const View &v);
virtual void tick(float quantum);
virtual bool isDead();
virtual ~Starfield();

Here is the .cpp

const int STARFIELD_SIZE=480;
const int STARFIELD_PERIOD=100;

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)[i].x = rx;
(*stars)[i].y = ry;

void Starfield::draw(const View &v)
for (int i=0; i<STARFIELD_NUM_STARS; i++) {
float xoffset, yoffset;
float x,y;
x = (*stars)[i].x;
y = (*stars)[i].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);


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this