Sign in to follow this  
raptorstrike

random block placment switch

Recommended Posts

ok i have this pice of code here
            for (int l = 0; l <30; l++)
              { 
                blocks[l]->SetPosition(rand()%500, rand()%450);
                      for(int s=0; s < 30; s++)
                         {
                             if( blocks[l]->TestCollision(blocks[s]) == TRUE)
                             {
                              if (blocks[s] != blocks[l])
                               blocks[l]->SetPosition(rand()%500, rand()%450);
                             };    
                          };
             };        
          for (int y = 0; y <30; y++)
          {
              blocks[y]->SetHidden(false);
          }; 
             
                      
       };
       totalblocks=29; 
   };


in my GameStart function to load the blocks randomly but when i do this
 switch (totalblocks)
    {
        case  28: 
        {
        
            for (int l = 0; l <30; l++)
              { 
                blocks[l]->SetPosition(rand()%500, rand()%450);
                      for(int s=0; s < 30; s++)
                         {
                             if( blocks[l]->TestCollision(blocks[s]) == TRUE)
                             {
                              if (blocks[s] != blocks[l])
                               blocks[l]->SetPosition(rand()%500, rand()%450);
                             };    
                          };
             };        
          for (int y = 0; y <30; y++)
          {
              blocks[y]->SetHidden(false);
          }; 
             
                      
       };
       totalblocks=29; 
   };

in my GamePaint function it dosnt work sure most of them are seprate but some of them are on top of each other and that screws up my collision detection. i cant figure out what wrong here maybe you guys can thx in advance. [smile] windows API dev-cpp 4.9.9.0

Share this post


Link to post
Share on other sites
You test for a collison. If there is you try again. But you dont then test to see if your second attempt failed. It might have collided again. You need a while loop to retest for collison everytime you try to place the block.

Share this post


Link to post
Share on other sites
i tryed that i put this line

while ( blocks[l]->TestCollision(blocks[s]) == TRUE)
if (blocks[s] != blocks[l])
blocks[l]->SetPosition(rand()%500, rand()%450);


but when it got time to exicute the statement the whole thing just froze up

Share this post


Link to post
Share on other sites
           for (int l = 0; l <30; l++)
{
blocks[l]->SetPosition(rand()%500, rand()%450);
bool IsNotSet=true;
while (1)
{
bool Failed=false;
for(int s=0; s < 30; s++)
{
if( blocks[l]->TestCollision(blocks[s]) == TRUE)
{
if (blocks[s] != blocks[l])
blocks[l]->SetPosition(rand()%500, rand()%450);
Failed=true;
}
}
if (!Failed) break;
}
}
for (int y = 0; y <30; y++)
{
blocks[y]->SetHidden(false);
}


}
totalblocks=29;
}



Try that.

Share this post


Link to post
Share on other sites
yey it works thanks alot you guys

[edit] guss i got ahead of my self well it works even better then it did before but im still getting 1 or 2 thinks overlapped i dont know why

do you think i can just ignore it. it isnt very common it must be an exception or somthing thats throwing it off

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