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

Author Topic: know if an shape is completly inside of an other  (Read 1251 times)

0 Members and 1 Guest are viewing this topic.

EIRBLAST

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
know if an shape is completly inside of an other
« on: October 06, 2018, 10:31:23 pm »
Hello everyone,

I tried to made a way to know if an rectangle shape is completly inside of an other rectangle shape
this is all i can come up with:
bool Map::Limit(const sf::RectangleShape body)
{
    sf::RectangleShape MapBody;
    MapBody.setPosition(MapStartPos.x,MapStartPos.y);
    MapBody.setSize(sf::Vector2f(MapStartPos.x+MapSize.x*TileSize.x,MapStartPos.y+MapSize.y*TileSize.y));

    if(MapBody.getGlobalBounds().intersects(body.getGlobalBounds()))
    {
        return true;
    }
    else
    {
        return false;
    }
}
 
obviously this only work when the "body" is completly outside of the map
Can someone help me please ? thanks !

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10822
    • View Profile
    • development blog
    • Email
Re: know if an shape is completly inside of an other
« Reply #1 on: October 07, 2018, 12:14:59 am »
A simple solution is to check that all for corners of the rect are contained within the other rect (rect.contains()).
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

EIRBLAST

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: know if an shape is completly inside of an other
« Reply #2 on: October 07, 2018, 01:02:52 am »
I think of that !
But i don't know how to pull it out :/

what do i have to modify it ? should i do it like this ?
    if(MapBody.getGlobalBounds().intersects(sf::Rect.contains(/*What do i put here ?*/))
    {
        return true;
    }
    else
    {
        return false;
 
Because this return a boolean to an other bool, i don't think this a thing x).

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: know if an shape is completly inside of an other
« Reply #3 on: October 15, 2018, 10:40:03 pm »
First, get the points of each of the four corners of the "inner" rectangle. You will then have four sf::Vectors containing those corner positions.
Then, test to see if those positions are contained within the "outer" rectangle.
If, and only if, all four corner positions are contained within the "outer" rectangle is that "inner" rectangle fully inside the "outer" rectangle.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything