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/hXEHnbrThanks for any help.