star fields
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?
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).
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).
Ok I got it working, looks like I didn''t need help after all
I''m gonna have to stop relying on these boards...
I''m gonna have to stop relying on these boards...
Thank you, you helped absolutely no one
"Love all, trust a few. Do wrong to none." - Shakespeare
Dirge - Aurelio Reis
www.CodeFortress.com
"Love all, trust a few. Do wrong to none." - Shakespeare
Dirge - Aurelio Reis
www.CodeFortress.com
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.
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:
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement