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

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

0 Members and 2 Guests 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: 11110
    • 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!