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

Author Topic: Checking 2 shapes Position  (Read 1636 times)

0 Members and 1 Guest are viewing this topic.

Made Y

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Checking 2 shapes Position
« on: May 25, 2017, 08:39:49 am »
I want to check if two shapes are in the same position.

if(sFigures.getPosition() == Triangle.getPosition()){
         break;
      }

I want to check if two shapes are in the same position.


here is the main

int main(){
   srand(time(NULL));

   RenderWindow window(VideoMode(800, 600), "ROBOT !");
   Texture figures;

   float dx = 0.5, dy = 0.5;

   if(!figures.loadFromFile("figures2.png")){
      cout << "FAILED" << endl;
      return -1;
   }

   CircleShape Triangle(32, 3);
   Triangle.setFillColor(Color::Black);
   int x1 = rand() % 768, y1 = rand() % 568;
   Triangle.setPosition(x1, y1);

   Sprite sFigures;
   sFigures.setTexture(figures);
   sFigures.setTextureRect(IntRect(0, 0, 32, 35));
   float x = 400;
   float y = 300;
   sFigures.setPosition(x, y);

   while(window.isOpen()){
      Event e;
      while(window.pollEvent(e)){
         if(e.type == Event::Closed){
            window.close();
         }if(e.type == Event::LostFocus){
            
         }
         
      }

      /////// MOVE CHARACTER ///////
      if(Keyboard::isKeyPressed(Keyboard::Down) && sFigures.getPosition().y < 568){
         sFigures.move(0,0.5);
         sFigures.setTextureRect(IntRect(0, 0, 32, 35));
      }else if(Keyboard::isKeyPressed(Keyboard::Up) && sFigures.getPosition().y > 0 ){
         sFigures.move(0,-0.5);
         sFigures.setTextureRect(IntRect(64, 71, 32, 35));
      }else if(Keyboard::isKeyPressed(Keyboard::Left) && sFigures.getPosition().x > 0 ){
         sFigures.move(-0.5,0);
         sFigures.setTextureRect(IntRect(96, 106, 32, 35));
      }else if(Keyboard::isKeyPressed(Keyboard::Right) && sFigures.getPosition().x < 768 ){
         sFigures.move(0.5,0);
         sFigures.setTextureRect(IntRect(32, 36, 32, 35));
      }
      
      ///// ENEMY MOVE /////
      if(Triangle.getPosition().x == 0){
         dx = 0.5;
      }else if(Triangle.getPosition().x == 768){
         dx = -0.5;
      }
      if(Triangle.getPosition().y == 0){
         dy = 0.5;
      }else if (Triangle.getPosition().y == 568){
         dy = -0.5;
      }
      Triangle.move(dx, dy);
      
      ///// CHECK //////
      if(sFigures.getPosition() == Triangle.getPosition()){
         break;
         
      }

      window.clear(Color(255, 0, 255));
      window.draw(Triangle);
      window.draw(sFigures);
      window.display();

   }

   return 0;   

}

Is there a better way to do this?
somethings like, checking with the player/enemy radius
sFigures = Player
Triangle = Enemy

ThankYou  :)

GameBear

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
    • Email
Re: Checking 2 shapes Position
« Reply #1 on: May 25, 2017, 10:21:52 am »
That depends.
Right now, you check if they are in the absolute same location.
That is, if their coordinates are the same. This will rarely happen outside of grid based programs.
Other options could be:

Check if distance is less than their combined radius.
(useing pythagarus to get the distance, also, since your circles are triangles, it may give some collision where there is none)

A bounding box collision test.
(easy to implement, not so precise unless you only use squares)

Line intersection test.
(a bit harder to implement but perfect for geometric shapes as it can allow you to find any intersection perfectly)

Lastly pixel perfect collision.
(this is a trade off. It's easy enough to implement by following a tutorial, and it will give perfect results with any bitmap images, but it consumes a lot more power than most want for moat applications. Use this only of needed)

I guess there are more out there, but if you learn these and what you want to use, you are a good part of the way,and it will teach you a lot.

A bonus info, I suggest that in this code:
///// ENEMY MOVE /////
      if(Triangle.getPosition().x == 0){
         dx = 0.5;
      }else if(Triangle.getPosition().x == 768){
         dx = -0.5;
      }
      if(Triangle.getPosition().y == 0){
         dy = 0.5;
      }else if (Triangle.getPosition().y == 568){
         dy = -0.5;
      }
      Triangle.move(dx, dy);
 

Instead of == you use >= and <= (greater than or equal to, and less than or equal to)
The reason is that == is absolute, so of by some means your enemy moves from say 767.5 to 768.1 it will just keep moving.
If you instead had said >= it would still change direction.
Of course, if you are 100% sure there will never be a case where this happens, than == is just fine :)
« Last Edit: May 25, 2017, 10:32:54 am by GameBear »
string message = "some random dude";
cout << "I'm just " << message;

Made Y

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: Checking 2 shapes Position
« Reply #2 on: May 25, 2017, 01:20:23 pm »
Oh, that's right,
It would be better to use> = or <= instead of ==
Heheh. Thank you sir; D
I just found how to find the intersects

If (sFigures.getGlobalBounds (). Intersects (Triangle.getGlobalBounds ())) {
Break;
}

And it works,

I apologize,
I really should search for it first before asking it.
Sorry for my bad english; D
« Last Edit: May 25, 2017, 02:05:25 pm by Made Y »

 

anything