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

Author Topic: Jerky dragging when converting input coordinates using inverse transformation.  (Read 879 times)

0 Members and 1 Guest are viewing this topic.

manasij7479

  • Newbie
  • *
  • Posts: 7
    • View Profile
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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10823
    • View Profile
    • development blog
    • Email
This thread most likely hasn't received an answer yet, because you didn't present the problem properly. Don't just throw code at us and assume we'll compile it in our minds and then magically figure out what the issue was.
Describe your problem with simple sentences, tell us what you have, what you want to achieve, what you've done so far and what issues you're currently experiencing.

If you just want the most position in the game world, then you can simply use window.mapPixelToCoords().
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/