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

Author Topic: Getting nearest sf::RectangleShape  (Read 3223 times)

0 Members and 1 Guest are viewing this topic.

steven_g

  • Newbie
  • *
  • Posts: 7
    • View Profile
Getting nearest sf::RectangleShape
« on: April 03, 2016, 12:15:40 am »
Hi, I'm trying to make a function that returns the nearest sf::RectangleShape, but I can't really grasp the concept yet. If I'm right in saying you can store the minimum X and Y value required to fit in all the points of a shape in a sf::FloatRect, how can you use this to determine the closest sf::RectangleShape? Can you use the x value with the point you're measuring from's x value and so on? I came up with the following code and compiled it but it doesn't work correctly. I may have the completely wrong idea though so feel free to correct me.     
 

std::vector<myGameClass> levelGenerator(50)                               // contains the sf::RectangleShapes
...

std::vector<sf::FloatRect> floatRectVec(50);                                           //vector to store the floatRects's
std::vector<int> floatRectDistances(50);                                               //vector to store the y distance
sf::FloatRect playerGlobalBounds = player.getGlobalBounds();             // player is the point measuring from
for (int vecObjects = 0; vecObjects < levelGenerator.size(); vecObjects++){
   sf::FloatRect tmpFloatRect = levelGenerator[vecObjects].rectangleShape.getGlobalBounds();
   floatRectVec[vecObjects] = tmpFloatRect;
   floatRectDistances[vecObjects] = (tmpFloatRect.height  -  playerGlobalBounds.height);
   std::cout<<floatRectDistances[vecObjects];                           //for debugging
 
 // ...using floatRectDistances[vecObjects] get the nearest sf::RectangleShape
}
 
 


 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10818
    • View Profile
    • development blog
    • Email
AW: Getting nearest sf::RectangleShape
« Reply #1 on: April 03, 2016, 07:23:11 pm »
Do I understand you correctly, that you have a point (x, y) and want to figure out which rect is the closest?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

steven_g

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Getting nearest sf::RectangleShape
« Reply #2 on: April 03, 2016, 07:39:23 pm »
Yes, but with x, y (the point from which you get the nearest rect) being a sf::RectangleShape. Also just to clear up the rectangles are stored in the vector levelGenerator.
 

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Getting nearest sf::RectangleShape
« Reply #3 on: April 04, 2016, 12:28:52 am »
You could test all of the points of the shape for distance to all of the rectangles you wish to test against and the shortest distance from any shape point to any rectangle is to the closest rectangle. The points in a rectangle shape's case would be its corners.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10818
    • View Profile
    • development blog
    • Email
AW: Getting nearest sf::RectangleShape
« Reply #4 on: April 04, 2016, 02:54:05 pm »
I'm still not certain that I understood your problem, but for closest rect to point I've found multiple StackOverflow and similar answers. Have you searched there already?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

steven_g

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: AW: Getting nearest sf::RectangleShape
« Reply #5 on: April 04, 2016, 05:18:32 pm »
I'm still not certain that I understood your problem, but for closest rect to point I've found multiple StackOverflow and similar answers. Have you searched there already?

I've probably described it badly but basically yes, I am trying to get the closest rect to a point. I did have a brief look but couldn't find any. Any links please?

Rhimlock

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: Getting nearest sf::RectangleShape
« Reply #6 on: April 04, 2016, 06:08:10 pm »
AABB describe a Rectangle which is aligned to X and Y axis.

Google has some usefull links:

https://www.google.de/?gws_rd=ssl#q=distance+aabb+point

AlexxanderX

  • Full Member
  • ***
  • Posts: 128
    • View Profile
    • AlexanderX
Re: Getting nearest sf::RectangleShape
« Reply #7 on: April 04, 2016, 08:20:13 pm »
If you have a lot of rectangles to test against, and the rectangles are all over the screen you could use a quad tree.
Here you can find my blog and tutorials about SFML - http://alexanderx.net/ (died...) - http://web.archive.org/web/20160110002847/http://alexanderx.net/