I’m trying to add some features into my editor that allow to the user things like dragging a selected item to a given direction
although i’ve ran into a damn issue.
This is my code :
//Origin
double objectx = selection->getX();
double objecty = selection->getY();
//Point
double pointerx = input->getMouseX();
double pointery = input->getMouseY();
//Displacement
double displacementx = fabs(pointerx - objectx);
double displacementy = fabs(pointery - objecty);
//Angle
double angle = atan2(displacementy,displacementx);
//Point
double pointx = displacementx * cosf(angle) + displacementy * sinf(angle);
double pointy = displacementy * cosf(angle) - displacementx * sinf(angle);
//Final position
double fx = objectx + pointx;
double fy = objecty + pointy;
//Save alpha
const bool alpha = graphics->getAlpha();
//Draw selection
graphics->setAlpha(false);
graphics->color(selection->getColor());
graphics->renderQd(selection->getBitmap(),
CRect(objectx,
objecty,
selection->getWidth(),
selection->getHeight()));
//Draw pointer around selection
graphics->setAlpha(true);
graphics->color(editor::ssImg[0]->getColor());
graphics->renderQd(editor::ssImg[0]->getBitmap(),
CRect(objectx + pointx,
objecty + pointy,
editor::ssImg[0]->getWidth(),
editor::ssImg[0]->getHeight()));
//Restore alpha
graphics->setAlpha(alpha);
The exact issue is that the selection pointer doesn’t follow mouse’s rotation only but its actually at mouse’s position(!).
The wanted behaviour is a pointer locked at selection’s offset but pointing to the mouse’s angle.
Anyone good at math sees anything wrong here ?
As I understand it, your wanted behavior presumes the existence of three points: an origin around which you’re rotating, a “mouse” to provide the direction relative to the origin, and a “selection” to provide the distance from the origin. (Somewhat confusingly, the result of your code is the “selection pointer”. I take it that “selection” means the original position of the selected object, and “selection pointer” means the position it’s been dragged to so far?)
Your code, however, only actually refers to two of those points: (
objectx,objecty), which I assume is the origin, and (pointerx,pointery), which I assume is the “mouse”. Your code never refers to the “selection”; so, naturally, the “selection” has no effect on the result of your code.There are a few other problems — Oli Charlesworth points out, in a comment above, that you’re wrongly dividing your angle by π/180, which means that you apply a very small rotation (which is why it looks like you end up with selection pointer = mouse; in fact, they can be up to a few degrees apart relative to the origin of rotation, but that’s not instantly noticeable) — but rather than fix those problems, I’d recommend you change your approach. Instead of generating “selection pointer” by rotating “selection” to match the angle of “mouse”, I recommend that you generate it by scaling “mouse” to match the magnitude of “selection”. The math for this is more straightforward, IMHO.
If you do want to stick with the approach of generating “selection pointer” by rotating “selection” to match the angle of “mouse”, then you have two main things to fix. Your current code rotates “mouse” by the current angle of “mouse”. Part of the fix is to rotate “selection” instead; the other part of the fix is to rotate by the difference between the current angles of “mouse” and “selection”.