Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Overlapping Array not working?  (Read 1267 times)

0 Members and 1 Guest are viewing this topic.

LucasShadow

  • Jr. Member
  • **
  • Posts: 52
    • View Profile
Overlapping Array not working?
« on: March 06, 2012, 05:41:04 am »
I am not sure where I have gone wrong, but this program is supposed to first randomly position a bunch of rocks, and then check to see if each one is overlapping any of the others. The randomly distributing I got working, but the checking for overlappings is proving difficult.

Full Code:
Code: [Select]
#include <SFML/Graphics.hpp>

 int main()
 {
     sf::RenderWindow App(sf::VideoMode(800, 600), "SRock Test");

     ///Set the number of rocks, load picture, set sprite, etc
     int NumberOfRock1 = 500;
     sf::Image Rock1Img;
     if(!Rock1Img.LoadFromFile("rock1.png")) {}
     Rock1Img.SetSmooth(false);
     sf::Sprite Rock1[NumberOfRock1];

     for (int i = 1; i < NumberOfRock1; i++) Rock1[i].SetImage(Rock1Img);///Set rock images
     for (int i = 1; i < NumberOfRock1; i++) Rock1[i].SetPosition(sf::Randomizer::Random(0, 800), sf::Randomizer::Random(0,600)); ///Randomly allocate the rocks

     for (int i = 1; i < NumberOfRock1; i++) ///Now lets check for overlapping of all the rocks
     {
         ///Get the position and dimension of a rock
         float XPCurrent = Rock1[i].GetPosition().x;
         float YPCurrent = Rock1[i].GetPosition().y;

         ///Check against all the previous rocks in the sequence
         for (int j = 0; j < (i - 1); j++)
         {
             float XPPrevious = Rock1[j].GetPosition().x;
             float YPPrevious = Rock1[j].GetPosition().y;
             float XSPrevious = Rock1[j].GetSize().x;
             float YSPrevious = Rock1[j].GetSize().y;

             ///Check to see if they are overlapping
             if( (XPCurrent>=XPPrevious) && (XPCurrent<=(XPPrevious+XSPrevious)) && (YPCurrent>=YPPrevious) && (YPCurrent <= (YPPrevious + YSPrevious)) )
             {
                 Rock1[i].SetPosition(-100, -100); ///Remove the rock if it is overlapping another
             }
         }
         ///Now check against all the rocks afterwards in the sequence
         for (int k = (i + 1); k < NumberOfRock1; k++)
         {
             float XPAfter = Rock1[k].GetPosition().x;
             float YPAfter = Rock1[k].GetPosition().y;
             float XSAfter = Rock1[k].GetSize().x;
             float YSAfter = Rock1[k].GetSize().y;

             ///Check to see if they are overlapping
             if( (XPCurrent>=XPAfter) && (XPCurrent<=(XPAfter+XSAfter)) && (YPCurrent>=YPAfter) && (YPCurrent <= (YPAfter + YSAfter)) )
             {
                 Rock1[i].SetPosition(-100, -100); //Remove the rock if it is overlapping another
             }
         }
     }

     while (App.IsOpened())
     {
         sf::Event Event;
         while (App.GetEvent(Event))
         {
             if (Event.Type == sf::Event::Closed)
                 App.Close();
         }

         App.Clear();

         for(int i = 0; i < NumberOfRock1; i++) App.Draw(Rock1[i]);

         App.Display();
     }
     return EXIT_SUCCESS;
 }


Picture of current generation: http://puu.sh/jDCR
As you can see, they overlap one another...any suggestions much appreciated.
[/url]

dxt

  • Newbie
  • *
  • Posts: 1
    • View Profile
Overlapping Array not working?
« Reply #1 on: March 08, 2012, 05:00:26 pm »
I don't know whats wrong with your code ( I'm not good in reading someone elses code btw) but a an easy method to check if sprites are overlapping is using their intersect method.. i.e.

Code: [Select]

sprite1.GetGlobalBounds().Intersects( sprite2.GetGlobalBounds() )


which returns true if they are overlapping

 

anything