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

Author Topic: Detect if sf::LineStrip primitive overlaps sf::RectangleShape  (Read 1708 times)

0 Members and 1 Guest are viewing this topic.

tyranid12

  • Newbie
  • *
  • Posts: 2
    • View Profile
Detect if sf::LineStrip primitive overlaps sf::RectangleShape
« on: January 29, 2025, 11:03:02 pm »
I have an sf::VertexArray, with two points, and a primitive type of sf::LineStrip. I want to detect if an object (which will be an sf::RectangleShape) is overlapping on this line.
Reading the documentation, I was drawn towards sf::VertexArray::getBounds(), however it appears that returns a rectangle containing both points. This is not what I want. I would like it so that I can calculate whether or not an object is actually touching the rendered line, which spans between the two vertexes.
I have looked at the documentation ,however I cannot appear to find a solution with my desired effect. I cannot find it on the forums either. How would I be able to do this?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11175
    • View Profile
    • development blog
    • Email
Re: Detect if sf::LineStrip primitive overlaps sf::RectangleShape
« Reply #1 on: January 30, 2025, 08:42:08 am »
SFML doesn't offer anything to check a line vs rectangle intersection.

You'll have to apply a bit of math on your own.
There are multiple methods to check such an intersection, see for example these StackOverflow answers: https://stackoverflow.com/questions/16203760/how-to-check-if-line-segment-intersects-a-rectangle
Official FAQ: https://www.sfml-dev.org/faq/
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

tyranid12

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Detect if sf::LineStrip primitive overlaps sf::RectangleShape
« Reply #2 on: January 30, 2025, 09:15:26 am »
Thanks, this worked perfectly!

Hapax

  • Hero Member
  • *****
  • Posts: 3415
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Detect if sf::LineStrip primitive overlaps sf::RectangleShape
« Reply #3 on: February 04, 2025, 05:22:53 am »
Checking if the line intersects with all of the lines of the rectangle is a great method but remember that another possibility of "overlap" is that the line is fully inside the rectangle, depending on how strict your movement/physics/timestep is.

This is simple to check though; just check if each point of the line is inside the rectangle:
sf::Vector2f linePoint1{};
sf::Vector2f linePoint2{};
sf::FloatRect rect{}; // the rectangle shape bounds should do here

if ((rect.contains(linePoint1) && (rect.contains(linePoint2))
    lineIsInsideRect = true;

You could also create a rect from the line points and check to see if that rect overlaps with the rectangle bounds rect.

NOTE: this is in addition to checking if the line intersects with the rectangles edges.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*