• Advertisement

Archived

This topic is now archived and is closed to further replies.

star fields

This topic is 5718 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

How would I do a star-field commonly seen in 2d space games? I know using multiple layers of pixels looks nice, but would''nt keeping track of all those pixels be rather slow, especially if it''s a large game world, and you wan''t those pixels to scroll?

Share this post


Link to post
Share on other sites
Advertisement
Are you talking about implementing the 2D right-to-left type of star-field used in side scrollers?

If so then there is no real need to look after each star independently as (say) five layers of parallax would be plenty and so you would only need to look after each layer (ie. each layer can be treated as a seperate entity).

Share this post


Link to post
Share on other sites
Ok I got it working, looks like I didn''t need help after all
I''m gonna have to stop relying on these boards...

Share this post


Link to post
Share on other sites
Thank you, you helped absolutely no one


"Love all, trust a few. Do wrong to none." - Shakespeare

Dirge - Aurelio Reis
www.CodeFortress.com

Share this post


Link to post
Share on other sites
Ok, Ill say how I did it now that I feel guilty about not saying sooner (I mooch quite a bit off other peoples answers)

I made a multi-dimensional array called StarArray (creatively) to hold all the stars. I have two constant intergers for the size, one is MAX_STARS and the other STAR_DISTANCE. The StarArray is in CManager, the class which handles all my resource management, such as bullets, particles, etc.

In the WinMain function, just before the message loop, I call pManager->InitializeStars(player_ptr player, sprite_ptr CloseStar, sprite_ptr FarStar). player_ptr is a typde_def for a boost::shared_ptr. Anyway, the function looks like this, to give you an idea.


  
void CManager::InitializeStars(sprite_ptr pSpriteClose, sprite_ptr pSpriteFar, player_ptr pPlayer)
{
for (int i = 0; i < STAR_DISTANCE; ++i)
{
for (int j = 0; j < MAX_STARS; ++j)
{
star_ptr pStar(new CStar());

if (i == 0)
{
pStar->SetClose(TRUE);
pStar->InitializeSelf(pGame, pSpriteClose);
}
if (i == 1)
{
pStar->SetClose(FALSE);
pStar->InitializeSelf(pGame, pSpriteFar);
}

pStar->ResetSelf();
pStar->SetPlayer(pPlayer);
pStar->SetLocalXPosition(rand() % pGame->GetScreenWidth());
pStar->SetLocalYPosition(rand() % pGame->GetScreenHeight());
StarArray[i][j] = pStar;
pGame->AddEntity(pStar);
pGame->AddVisibleEntity(pStar);
}
}
}


So, now every star object holds a pointer to the current player, and is randomly scattered across the screen. If the STAR_DISTANCE is 0, it loads the close picture, which is an ordinary star picture. If it''s 1, then it loads the far star pic, which is smaller.

Every frame the stars poll for the players velocity via their pointer to him, and set their own velocity to the inverse of the player. The players local (screen) X and Y coordinates never changes, just his World Coordinates. The closer stars match the speed of the player, and the farther ones have half the speed, makin it a paralax background.

Every frame the stars also check if they move out of the view-port using this bit of code:


  
void CStar::UpdateSelf(CGame *pGame)
{
LocalXPosition -= pPlayer->GetVelocityX();
LocalYPosition -= pPlayer->GetVelocityY();

if (bClose == FALSE)
{
LocalXPosition -= (pPlayer->GetVelocityX() / 2.0f);
LocalYPosition -= (pPlayer->GetVelocityY() / 2.0f);
}

if (LocalXPosition < 0 - GetBoxX())
{
LocalXPosition = pGame->GetScreenWidth();
}
if (LocalXPosition > pGame->GetScreenWidth())
{
LocalXPosition = 0 - GetBoxX() + 1;
}



if (LocalYPosition < 0 - GetBoxY())
{
LocalYPosition = pGame->GetScreenHeight();
}
if (LocalYPosition > pGame->GetScreenHeight())
{
LocalYPosition = 0 - GetBoxY() + 1;
}

if (bClose == FALSE)
{
SetRect(&rDestination, LocalXPosition,
LocalYPosition,
LocalXPosition + rBoundingBox.right,
LocalYPosition + rBoundingBox.bottom);
return;
}

SetRect(&rDestination, LocalXPosition,
LocalYPosition,
LocalXPosition + (rBoundingBox.right / 2),
LocalYPosition + (rBoundingBox.bottom / 2));

}


If the star moves out of the view-port, it sticks back in on the other side of the screen, so no matter how big the world is, you will ALWAYS need only MAX_STARS amount of stars.

Share this post


Link to post
Share on other sites

  • Advertisement