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

Author Topic: Problem with clicking and dragging rotated rectangles.  (Read 1337 times)

0 Members and 1 Guest are viewing this topic.

SwiftWombat

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Problem with clicking and dragging rotated rectangles.
« 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

Thanks for any help.
« Last Edit: January 21, 2023, 03:48:07 am by SwiftWombat »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Problem with clicking and dragging rotated rectangles.
« Reply #1 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Problem with clicking and dragging rotated rectangles.
« Reply #2 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...
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*