2D space game. How to have infinite starfield?

Member
Posts: 90
Joined: 2006.11
Post: #1
I am making a game, that involves the player controlling a ship, moving around and shooting. There are stars and planets in the background. The planets are stars are individually placed. )planets coordinates are placed manually, whilst 500 stars are randomised between + or - 1000x from the ships' starting point. where x, is the 'z distance' from the player's 2D plane. This implements parallax star movement.The ship is placed in the middle (always), pressing left and right rotates, press up accelerates.

This means I only have a starfield of size 2000*2000 (for the front stars, but the back stars go across a lot longer). I could make the starfield bigger, but it'd mean more stars (game runs slower), and in the end the starfield is still finite in size.

So, how to make an infinite star field? I don't want to have to limit the size of the map.
Quote this message in a reply
Nibbie
Posts: 1
Joined: 2010.11
Post: #2
you might want to check this code out:
init:
Code:
float []rLines=new float[1024];
int worldSize=1024;
...
  for(int i=0;i<1021;i+=3)
{
   rLines[i]=random(-worldSize*2,worldSize*2); //x
   rLines[i+1]=random(2,worldSize*2);  //y
   rLines[i+2]=random(-worldSize*2,worldSize*2); //z
}

draw:
(ax is the player's angle)
Code:
stroke(255);   //white
    for(int i=0;i<1021;i+=3){
    if(kt>0) line(rLines[i],rLines[i+1],rLines[i+2],rLines[i]+2,rLines[i+1],rLines[i+2]);
    else line(rLines[i],rLines[i+1],rLines[i+2],rLines[i]-20*cos(ax+HALF_PI),rLines[i+1],rLines[i+2]-20*sin(ax+HALF_PI));
    if(rLines[i]<ship.x-worldSize) rLines[i]+=worldSize*2; //wrap x
    if(rLines[i]>ship.x+worldSize) rLines[i]-=worldSize*2; //wrap x
    if(rLines[i+2]<ship.z-worldSize) rLines[i+2]+=worldSize*2; //wrap z
    if(rLines[i+2]>ship.z+worldSize) rLines[i+2]-=worldSize*2; //wrap z
    if(rLines[i+1]<ship.y-worldSize) rLines[i+1]+=worldSize*2; //wrap y
    if(rLines[i+1]>ship.y+worldSize) rLines[i+1]-=worldSize*2; //wrap y
  }

A lot about this is unoptomized for 2d (this is for a 3D game), and this is not my finest example of code (it was for 3DU and I was in a hurry), but it works. (it is Java, but I presume that you can see what I'm doing)

If you want the full source it's here: (warning, my server takes 2-3 seconds to respond sometimes)
http://games.duodec.net/NOT%20ASTEROIDS.src.zip (compiled with Processing, freee, http://www.processing.org)
and to see the game:
http://games.duodec.net/NOT%20ASTEROIDS.zip

Also of interest might be a C version of basically the same thing, but for OpenGL and 2D from MadTak.
http://code.duodec.net/c/madtak/
Quote this message in a reply
Member
Posts: 90
Joined: 2006.11
Post: #3
I couldn't get your code working, but I wrote new code based on yours. (because my stars are implemented differently to yours) I finally got them to "wrap", but there is one problem, the stars "flicks" to the position relative to the ship instead, not wrapping.

Code:
//stars&wrap%parallax.
    for (int i=0;i<500;i++)
    {

      stroke(starcl[i]);
      ViewY=int(unit[player[thisplayer.thisplayeris].ship].y);
      ViewX=int(unit[player[thisplayer.thisplayeris].ship].x);


        if(i%3==0)
        {
          float ox=(ViewX/3)/(screen.width);
          float oy=(ViewY/3)/(screen.height);
          int offsetx=int(ox*screen.width);
          int offsety=-int(oy*screen.height);
          line (starsx[i]-ViewX/3+offsetx,starsy[i]+ViewY/3+offsety,starsx[i]-ViewX/3+offsetx+1,starsy[i]+ViewY/3+offsety+1);
        }
        if (i%4==0 && i%3!=0)
        {
          int offsetx=(round((ViewX/2)/screen.width)*screen.width+screen.width/2);
          int offsety=-(round((ViewY/2)/screen.height)*screen.height+screen.height/2);
          line (starsx[i]-ViewX/2+offsetx,starsy[i]+ViewY/2+offsety,starsx[i]-ViewX/2+1+offsetx,starsy[i]+ViewY/2+offsety+1);

        }
        if (i%5==0 && i%4!=0 && i%3!=0)
        {
          int offsetx=(round((ViewX/1.5)/screen.width)*screen.width+screen.width/2);
          int offsety=-(round((ViewY/1.5)/screen.height)*screen.height+screen.height/2);
          rect (starsx[i]-ViewX/1.5+offsetx,starsy[i]+ViewY/1.5+offsety,1,1);
        }
    }
Quote this message in a reply
Member
Posts: 90
Joined: 2006.11
Post: #4
Got it... thanks very much... (I don't get why the worldsize has to multiply by 2, however..., although it is frequent that I write code I don't understand, but works.)
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #5
use else if

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Member
Posts: 90
Joined: 2006.11
Post: #6
for anyone that comes to look:

Code:
for (int i=0;i<500;i++)
    {

      stroke(starcl[i]);
      ViewY=int(unit[player[thisplayer.thisplayeris].ship].y);
      ViewX=int(unit[player[thisplayer.thisplayeris].ship].x);


      if(i%2==0)
      {
        int scaleis=3;
        if (starsx[i]-ViewX/scaleis<-screen.width)
        {
          starsx[i]+=screen.width*2;
        }
        if (starsx[i]-ViewX/scaleis>+screen.width*2)
        {
          starsx[i]-=screen.width*2;
        }
        if (starsy[i]+ViewY/scaleis<-screen.height)
        {
          starsy[i]+=screen.height*2;
        }
        if  (starsy[i]+ViewY/scaleis>+screen.height*2)
        {
          starsy[i]-=screen.height*2;
        }

        line (starsx[i]-ViewX/scaleis,starsy[i]+ViewY/scaleis,starsx[i]-ViewX/scaleis+1,starsy[i]+ViewY/scaleis+1);
        line (starsx[i]-ViewX/scaleis,starsy[i]+ViewY/scaleis,starsx[i]-ViewX/scaleis+unit[player[thisplayer.thisplayeris].ship].vx/(scaleis*scaleis),starsy[i]+ViewY/scaleis-unit[player[thisplayer.thisplayeris].ship].vy/(scaleis*scaleis));
      }
      else if (i%3==0 || i%5==0)
      {
        int scaleis=2;
        if (starsx[i]-ViewX/scaleis<-screen.width)
        {
          starsx[i]+=screen.width*2;
        }
        if (starsx[i]-ViewX/scaleis>+screen.width*2)
        {
          starsx[i]-=screen.width*2;
        }
        if (starsy[i]+ViewY/scaleis<-screen.height)
        {
          starsy[i]+=screen.height*2;
        }
        if  (starsy[i]+ViewY/scaleis>+screen.height*2)
        {
          starsy[i]-=screen.height*2;
        }

        line (starsx[i]-ViewX/scaleis,starsy[i]+ViewY/scaleis,starsx[i]-ViewX/scaleis+1,starsy[i]+ViewY/scaleis+1);
        line (starsx[i]-ViewX/scaleis,starsy[i]+ViewY/scaleis,starsx[i]-ViewX/scaleis+unit[player[thisplayer.thisplayeris].ship].vx/(scaleis*scaleis),starsy[i]+ViewY/scaleis-unit[player[thisplayer.thisplayeris].ship].vy/(scaleis*scaleis));

      }
      else
      {
        float scaleis=1.5;
        if (starsx[i]-ViewX/scaleis<-screen.width)
        {
          starsx[i]+=screen.width*2;
        }
        if (starsx[i]-ViewX/scaleis>+screen.width*2)
        {
          starsx[i]-=screen.width*2;
        }
        if (starsy[i]+ViewY/scaleis<-screen.height)
        {
          starsy[i]+=screen.height*2;
        }
        if  (starsy[i]+ViewY/scaleis>+screen.height*2)
        {
          starsy[i]-=screen.height*2;
        }
        line (starsx[i]-ViewX/scaleis,starsy[i]+ViewY/scaleis,starsx[i]-ViewX/scaleis+1,starsy[i]+ViewY/scaleis+1);
        line (starsx[i]-ViewX/scaleis,starsy[i]+ViewY/scaleis,starsx[i]-ViewX/scaleis+unit[player[thisplayer.thisplayeris].ship].vx/(scaleis*scaleis),starsy[i]+ViewY/scaleis-unit[player[thisplayer.thisplayeris].ship].vy/(scaleis*scaleis));
      }
    }
Quote this message in a reply
Post Reply