SFML community forums

Help => General => Topic started by: Xrey274 on March 18, 2019, 09:50:16 pm

Title: Accurate globalBounds of circle.
Post by: Xrey274 on March 18, 2019, 09:50:16 pm
What I want is for the global bounds to be the same size and shape as the circle. Currently the globalBounds is basically a rectangle. Current code literally just simulates the code of getGlobalBounds function.

#include <iostream>
#include <SFML/Graphics.hpp>

bool calculate(sf::Vector2f pos, sf::Vector2f mouse, int r)
{
    std::cout<<pos.x - mouse.x<<" "<<pos.y - mouse.y <<std::endl;

    if(pos.x - mouse.x >= 0 && pos.x - mouse.x <= r)
    {
        if(pos.y - mouse.y >= 0 && pos.y - mouse.y <= r)
        {
            return true;
        }
        else if(pos.y - mouse.y <= 0 && pos.y - mouse.y >= -r)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    else if(pos.x - mouse.x <= 0 && pos.x - mouse.x >= -r)
    {
        if(pos.y - mouse.y <= 0 && pos.y - mouse.y >= -r)
        {
            return true;
        }
        else if(pos.y - mouse.y >= 0 && pos.y - mouse.y <= r)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    else
    {
        return 0;
    }
}

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Test");

    sf::Event event;
    sf::Vector2f mouseCoords = window.mapPixelToCoords(sf::Vector2i(sf::Mouse::getPosition(window)));

    sf::CircleShape circle;
    circle.setRadius(150);
    circle.setPosition(sf::Vector2f(400, 300));
    circle.setFillColor(sf::Color::Blue);
    circle.setOrigin(circle.getRadius(), circle.getRadius());

    while(window.isOpen())
    {
        mouseCoords = window.mapPixelToCoords(sf::Vector2i(sf::Mouse::getPosition(window)));

        while(window.pollEvent(event))
        {
            if(event.type == sf::Event::Closed)
            {
                window.close();
            }

            if(event.type == sf::Event::MouseMoved)
            {
                if(calculate(circle.getPosition(), mouseCoords, circle.getRadius()) == true)
                {
                    circle.setFillColor(sf::Color::Red);
                }
                else
                {
                    circle.setFillColor(sf::Color::Blue);
                }
            }
        }

        window.clear(sf::Color::White);

        window.draw(circle);

        window.display();
    }
}
 
Title: Re: Accurate globalBounds of circle.
Post by: eXpl0it3r on March 19, 2019, 12:40:20 am
You can calculate the distance between the center point of the circle and the mouse position. If the distance is larger than the radius, it's outside the circle. If it's smaller (or equal) it's inside.

For how to calculate the distance of two points, I suggest to just google that, as it's basic vector math and better explained elsewhere. :)
Title: Re: Accurate globalBounds of circle.
Post by: Xrey274 on March 19, 2019, 05:19:33 pm
I'm already doing that - calculating the distance between the center of the circle and the mouse.
Title: Re: Accurate globalBounds of circle.
Post by: Xrey274 on March 19, 2019, 05:44:34 pm
So basically I had to just use a formula for calculating the distance between 2 points in euclidean space and just square it. Thanks for the reply.