Hi,
I am trying to implement 'dragging' the object on the screen.
My draw function looks like this:
void GraphSprite::draw(sf::RenderWindow* win, vec2 offset)
{
sf::Transform finalt = transform; // transform is a data member of my object
finalt.translate(offset.x, offset.y);
sf::RenderStates state(transform);
win->draw(edgeArray.data(),edgeArray.size(), sf::Lines, state);
}
And I handle the respective inputs in the following way:
void GraphSprite::handleClick(float x,float y)
{
// std::cout <<"PRESSED: "<< x<<' '<< y<<std::endl;
fixInput(x,y);
if(x<=sizex && y<=sizey)
{
pressed=true; // this is made false when mouse is released
initial = {x,y};
}
}
void GraphSprite::handleMoved(float x, float y)
{
// std::cout <<"MOVED: "<< x<<' '<< y<<std::endl;
fixInput(x,y);
if (pressed == true && x<=sizex && x>=0 && y<=sizey && y>=0)
{
transform.translate(x - initial.x, y-initial.y);
initial = {x,y};
}
}
This works fine, but the 'translation' take place in the wrong frame of reference.
(i.e when the renderstate has a rotation of 90 degrees, dragging horizontally moves the object vertically)
To fix this, I created the following function:
void fixInput(float& x, float& y)
{
sf::Transform inv = transform.getInverse();
auto result = inv.transformPoint(x,y);
x = result.x;
y = result.y;
}
The result is that it seems very jerky when the dragging takes place (the axis issue is fixed though !)
I'll try to include a video/gif if anyone want a more visual description of the problem.