Continue rotating a circular image with mouseDragged where it was left off

Hi, I’m trying to rotate a circular image on mouseDragged and I got the image to rotate when dragging the mouse but when I stop dragging and start dragging again from a different position of the image it doesn’t continue the drag where it was left off. How can I start dragging again from its end position. This is some of the code I have:

float rotAngle = 0;
void draw(){ 
    pushMatrix();
    translate(width/2, height/2);
    rotate(rotAngle);
    imageMode(CENTER);
    image(abc, 0, 0, ABC_SIZE, ABC_SIZE);  //abc is the image and ABC_SIZE the size of the image
    popMatrix();  
}

void mouseDragged(){
    rotAngle = atan2(mouseY - height/2, mouseX - width/2);
}
1 Like

I was able to kinda do it with this code:

float rotAngle, originalRotAng, newRot, originalTouchAng = 0;
void draw() { 
    if (mousePressed == true) {
       originalTouchAng = atan2(mouseY - height/2, mouseX - width/2);   //original angle at which the image was tapped
    }
    pushMatrix();
    translate(width/2, height/2);
    rotate(rotAngle);
    imageMode(CENTER);
    image(abc, 0, 0, ABC_SIZE, ABC_SIZE);  //abc is the image and ABC_SIZE the size of the image
    popMatrix();  
}

void mouseDragged() {
    originalRotAng = rotAngle;                              // original angle of the image
    newRot = atan2(mouseY - height/2, mouseX - width/2);    // 
    rotAngle = originalRotAng + newRot - originalTouchAng;  // new angle for the rotation of the image
}

It does continue the rotation where it was left off but now it rotates way faster than the mouse drag the farther it is rotated from the center. Any ideas?

If you want it to “smoothly” rotate, maybe base the newRot off of the change of the mouse’s position (Something like mousex - pmousex).

Otherwise, just try different things! There’s no right way to do this haha

Edit: Welcome to the forums!

2 Likes

See related: