SFML community forums

Help => Graphics => Topic started by: SwiftWombat on January 21, 2023, 03:45:24 am

Title: Problem with clicking and dragging rotated rectangles.
Post by: SwiftWombat on January 21, 2023, 03:45:24 am
Hey guys, just got into SFML a couple days ago and am wanting to create an app where you can drag rectangles shapes. I got that part all working fine, except in the case where the rectangles are rotated. When they are rotated, clicking and dragging a rectangle will cause the shape to snap a certain distance from where the mouse was.

Every time I click inside a rectangle (I call them "projections" in code), I calculate the mouse offset such that the shape doesn't get it's origin snapped to the mouse (i.e., the shape will be dragged from wherever I clicked in the shape). The dragging logic is as follows:

void AppState::update(const sf::Vector2f& mpos)
{
    if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) // moving projection
    {
        if (!this->m_dragging)
        {
            for (auto proj : this->m_projections)
            {
                if (!proj->contains(mpos)) { continue; }                                        
                this->m_offset.x = mpos.x - proj->getGlobalBounds().left - proj->getOrigin().x;
                this->m_offset.y = mpos.y - proj->getGlobalBounds().top - proj->getOrigin().x;
                this->m_current = proj;
                this->m_dragging = true;
            }
        }
    }
    else
    {
        this->m_current = nullptr;
        this->m_dragging = false;
    }

    if (sf::Mouse::isButtonPressed(sf::Mouse::Right)) // delete projection
    {
        auto i = this->m_projections.begin();
        while (i != this->m_projections.end())
        {
            auto proj = *i;
            if (proj->contains(mpos)) { i = this->m_projections.erase(i); }
            else                      { ++i; }
        }
    }

    if(this->m_current && this->m_dragging)
    {
        this->m_current->setPosition(mpos.x - m_offset.x, mpos.y - m_offset.y);  
    }
}
 

I assume I need to account for the rotation in some way when calculating the offset, I just don't know how. I have linked a couple videos I posted to Imgur of both cases, the un-rotated rectangles are behaving as expected.
https://imgur.com/a/hXEHnbr (https://imgur.com/a/hXEHnbr)

Thanks for any help.
Title: Re: Problem with clicking and dragging rotated rectangles.
Post by: eXpl0it3r on January 21, 2023, 10:36:53 am
You don't need to calculate in the global bounds, instead just consider the origin.

The global bounds will always remain axis aligned, so if you rotate the shape, the global bounds will be drawn has a box around the whole shape with the axis aligned to x and y.
You're really just interested in the relative position of the mouse towards the origin of the shape.
Title: Re: Problem with clicking and dragging rotated rectangles.
Post by: Hapax on February 01, 2023, 04:48:05 pm
And, of course, the location of the origin of the shape is just the shape's position ;D

It's worth noting, also, that you are using "proj->getOrigin().x" for the y offset...