How do I know if the mouse is moving clockwise or counterclockwise? What algorithm should I apply?
Tobitobiah Do you know where the center of the clock is?
Skew No, the center of the clock is not clear. What I'm trying to do is to make a rotation with a gizmo in the editor, I'm trying to make a rotation movement like that.
Tobitobiah You must have a reference point i.e. rotation center, otherwise the winding direction cannot be determined with certainty. A gizmo should have the centerpoint, shouldn't it?
Skew I tried a lot, but I couldn't get the result I wanted, where can I find a code example that moves a meshinstance in a 3D scene clockwise or counterclockwise with mouse dragging movements?
Tobitobiah clockwise or counterclockwise
Relative to what? You must go around something in order for cw/ccw distinction to make sense. I don't know if there's an example because it's not clear what you're looking for.
Is this 3d or 2d ? Can you show a prints of your scene ? I think what you need is the look_at function so your object will point rotate to your mouse pos.
Tarragona I am trying to move the object with mouse movements in 3D scene.
Tobitobiah I am trying to move the object
Move or rotate? Those are two different things. So you want to rotate a 3D object around its pivot with rotation axis perpendicular to screen plane? Or is it something else?
In things concerning coding, you need to learn to communicate with precision and clarity. Otherwise you're wasting yours and everyone else's time on deciphering your thoughts. Also stating the problem unambiguously in writing (and/or drawing) may help you yourself figure out a solution.
Skew I meant the action of rotation.
Skew I need to know whether I am moving clockwise or anti-clockwise with mouse movements, how do I know this, I need this algorithm.
Tobitobiah Here it is:
- (un)project object's pivot into 2D screen plane. Let's call this point C (for center)
- if mouse is dragging, get the mouse position in this and previous frame. Let's call these points M1 and M2 (for mouse)
- construct vectors CM1 and CM2.
- get the sign of the signed angle between them - that's your direction
The following code block does not work for me. How do I distinguish whether the x-axis moves in the - or + direction when drawing a circle shape with the mouse.
Vector3 MouseRelative;
Vector3 StartingMousePosition;
Vector3 CurrentPosition;
if (@event is InputEventMouseButton mouseMotionEvent)
{
if (mouseMotionEvent.ButtonIndex == MouseButton.Left && mouseMotionEvent.IsPressed())
{
StartingMousePosition = mouseMotionEvent.Position;
}
}
if (@event is InputEventMouseMotion mouseSlideEvent)
{
MouseRelative = mouseSlideEvent.Relative;
Vector2 CurrentPosition = mouseSlideEvent.Position;
float DirX = CurrentPosition.X - StartingMousePosition.X;
float DirY = CurrentPosition.Y - StartingMousePosition.Y;
GD.Print(Math.Sign(Mathf.Atan2(DirY, DirX)));
StartingMousePosition = CurrentPosition;
}
Tobitobiah Try to implement the algorithm from my previous post.
Skew The y-axis can be irregular as in the blue line, but I can tell with the x-axis whether it is clockwise or counterclockwise, but how do I make this distinction because the x-axis can be negative clockwise or counterclockwise?
Skew Can you share a code that contains the algorithm? It would be fine in gdscript, I'll do it in c#🙂 I wonder if I'm applying the algorithm incorrectly? First it rotates clockwise and then counterclockwise, even though I move the mouse clockwise.
Vector2 center;
public override void _Input(InputEvent e){
if (e is InputEventMouseButton button ){
center = button.Position;
}
if (e is InputEventMouseMotion motion){
if (motion.ButtonMask != 0){
GD.Print((motion.Position - motion.Relative - center).AngleTo(motion.Position - center)>0 ? "CW" : "CCW");
}
}
}
Skew unfortunately the situation is the same, the mouse movement is correct when I move clockwise, but when I move counterclockwise it turns clockwise again.
Tobitobiah No it doesn't if you move around the center. It works precisely as it should. CCW vs CW only makes sense when going around some fixed point. For an actual clock it's the center of the clock. In this example it's your click point.
© 2022 - 2025 — McMap. All rights reserved.