1

I wish to make a turret in a 2d game, which should rotate smoothly to the angle of the mouse. I came up with two solutions but neither are satifactory. The first:

currentRot = targetRot; // Produces snapping + doesn't look realistic

The second, inspired from Smoothly rotate turret and retain accuracy :

if (currentRot < targetRot)
{
    currentRot += 2;
    if(currentRot > targetRot)
    {
        currentRot = targetRot;
    }
}
if (currentRot > targetRot)
{
    currentRot -= 2;
    if(currentRot < targetRot)
    {
        currentRot = targetRot;
    }
}

However, the second approach doesn't rotate in the optimal direction all the time. The code doesn't "know" which way to rotate is shorter. I cannot use libraries, and I think quaternions are overkill, so I'm unsure how to solve this problem. Also, is there a third approach that is simpler/better?

Other info: targetRot is from 0°-360°

Jomity
  • 56
  • 8
  • You tagged your question with `processing`, does that mean you have access to and can use the `lerp` function? – Justiniscoding Feb 16 '22 at 23:02
  • Justiniscoding, yes I tried that but it still has the wrapping/jumping issue. – Jomity Feb 16 '22 at 23:09
  • Could you take a screen recording of the jumping issue? – Justiniscoding Feb 16 '22 at 23:17
  • Heres the link the project I'm working on, just move your mouse over the canvas https://www.khanacademy.org/computer-programming/wip-12/5128306941804544 (if live demo link isn't allowed on stack, i can get a video) – Jomity Feb 16 '22 at 23:28
  • It seems like your problem is that when the angle goes past 360, it goes back to 0 and it jumps. A possible solution is to make `targetRot` go past 360, and not reset it to 0 after reaching 360 – Justiniscoding Feb 16 '22 at 23:32
  • Not sure how to do that, as targetRot is user input, but I can try. Thanks for your help! – Jomity Feb 16 '22 at 23:48
  • Justiniscoding, I found a solution that works (finally :D) – Jomity Feb 17 '22 at 02:43

1 Answers1

-1

I found a cheesed solution, after a long time! This was oddly challenging. I still feel like my solution was sub-optimal though

It just checks which way is shorter

var dp = /*world distance from currentRot to targetRot+5 on unitcircle*/
var dp = /*world distance from currentRot to targetRot-5 on unitcircle*/

if (currentRot < targetRot) {
    currentRot += rate * (dp < dm ? -1 : 1);
    if (currentRot > targetRot) {
        currentRot = targetRot;
    }
}
if (currentRot  > targetRot) {
    currentRot  -= rate * (dp < dm ? 1 : -1);
    if(currentRot  < targetRot) {
        currentRot  = targetRot;
    }
}

and also this line fixed some bugs

if (currentRot   < 0) {currentRot  += 360;}
Jomity
  • 56
  • 8