Jump to content
  • Advertisement
Sign in to follow this  
Elit3d

[SFML] Distance between random placed & random spawn number each time

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

So I currently have code that every time I enter a room, the blocks are placed randomly.

sf::Sprite leftBlock[6];

for (int i = 0; i < 6; i++)
	{
		y = rand() % 400 + 60;
		x = rand() % 400 + 60;

		leftBlock[i].setTexture(BLOCK);

		leftBlock[i].setPosition(x,y);
	}


but. my issue is, sometimes they overlap and are really clumped together. I also would like to not just have 6 spawn in each room so is there a way to randomize the number? I tried int block; and tried putting the int into sf::Sprite leftBlock[block]  but that didnt really do the trick. 

Share this post


Link to post
Share on other sites
Advertisement

1.) Randomizing the amount of items.   

static const int MinNumBlocks = 4;
static const int MaxNumBlocks = 6; // must be greater than MinNumBlocks

sf::Sprite leftBlock[MaxNumBlocks];

int numBlocks = MinNumBlocks + rand() % ( MaxNumBlocks - MinNumBlocks );

for (int idxBlock = 0; idxBlock < numBlocks; idxBlock++) {
    ...
}

2.) Ensuring a minimal distance between items by relocating if the minimum distance to any already existing item is fallen below a threshold.

static const float SquaredMinDistance = 20.0f;
static const int MaxNumTrials = 10;

for (int idxBlock = 0; idxBlock < numBlocks; idxBlock++) {
    for (int trial = 0; trial < MaxNumTrials; trial++) {
        x = rand() % 400 + 60;
        y = rand() % 400 + 60;
        bool okay = true;
        for (int idxCheck = 0; idxCheck < idxBlock; idxCheck++) {
            float xDist = x - leftBlock[ idxCheck ].getPosition().x;
            float yDist = y - leftBlock[ idxCheck ].getPosition().y;
            okay = okay && (( xDist * xDist + yDist * yDist ) >= SquaredMinDistance );
        }
        if( okay ) {
            break;
        }
    }
    leftBlock[ idxBlock ].setTexture( BLOCK );
    leftBlock[ idxBlock ].setPosition( x, y );
}

(Its all untested code, but it should show the idea.)

Edited by haegarr

Share this post


Link to post
Share on other sites

1.) Randomizing the amount of items.   

static const int MinNumBlocks = 4;
static const int MaxNumBlocks = 6; // must be greater than MinNumBlocks

sf::Sprite leftBlock[MaxNumBlocks];

int numBlocks = MinNumBlocks + rand() % ( MaxNumBlocks - MinNumBlocks );

for (int idxBlock = 0; idxBlock < numBlocks; idxBlock++) {
    ...
}

2.) Ensuring a minimal distance between items by relocating if the minimum distance to any already existing item is fallen below a threshold.

static const float SquaredMinDistance = 20.0f;
static const int MaxNumTrials = 10;

for (int idxBlock = 0; idxBlock < numBlocks; idxBlock++) {
    for (int trial = 0; trial < MaxNumTrials; trial++) {
        x = rand() % 400 + 60;
        y = rand() % 400 + 60;
        bool okay = true;
        for (int idxCheck = 0; idxCheck < idxBlock; idxCheck++) {
            float xDist = x - leftBlock[ idxCheck ].getPosition().x;
            float yDist = y - leftBlock[ idxCheck ].getPosition().y;
            okay = okay && (( xDist * xDist + yDist * yDist ) >= SquaredMinDistance );
        }
        if( okay ) {
            break;
        }
    }
    leftBlock[ idxBlock ].setTexture( BLOCK );
    leftBlock[ idxBlock ].setPosition( x, y );
}

(Its all untested code, but it should show the idea.)

Many thanks! Will try this now!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!