Rotate a handle using the mouse to drive it.(player to move mouse in circle)

I’m having some issues getting a handle to rotate with the player mouse acting as the driving force. ( think Amnesia) when interacting with the handle the player need to move the mouse in a circle which drives the rotation and rotates the handle. ( as if they are turning it).

I currently am creating a circle when you interact with the handle abs then normalising the output of where the mouse is but this is not working. Any ideas would be much appreciated!

What I would try:

When first clicking:

  1. Project the center of the knob to the screen (from world to screen coordinates)
  2. Get the mouse position in screen coordinates, and subtract the projected center position
  3. Use the Atan2() function to calculate a rotation of the mouse position compared to the knob.

Each tick when the action is still going:

  1. Get the mouse position in screen coordinates, and subtract the projected center position
  2. Use the Atah2() function to calculate a new rotation of the mouse position
  3. Subtract the previous/old position from the new position
  4. If the movement is smaller than some delta (say, smaller than 15 degrees or 0.3 radians or whatever) then apply that movement to the knob
  5. The new “previous position” value should be set to whatever you calculated in step 2, to be ready for next tick

Like this?

I’ve never used Atah2() so this is new to me. I did try and use physics and drive it with a constraint but that did not work. Appreciate the help.

I think “viewport” and “screen” coordinates may actually be different, at least when using “cinematic camera” or letterboxing, so that might be something to watch out for.

Atan2() looks like this:

It takes a position in X and Y, in math-style coordinates (X to the right, Y upwards on a piece of paper) and returns the angle from the origin to that point. (Obviously it doesn’t work when the X and Y values are exactly 0,0 !)
(Also, oops, in the picture I wired X to Y and Y to X, which will return the degrees rotating the other way … feel free to wire it up correctly when you use it :smiley: )

A reasonable description can be found here:

The wikipedia page on the function is … complete, but not beginner friendly…

1 Like

I’m also trying to do this, but i haven’t found a satisfying answer online.
I haven’t tried it in action yet, but i came up with this:

It finds the look at rotation from the mouse position to the center of the screen (or any Vector2).
Like i said, i haven’t tried it in action, but it gets a 0 to 180 and -180 to -0 rotation. So it seems like it can be used.

1 Like