Recording and playback mouse movement in polar coordinates script

Post gaming related scripts
User863
Posts: 5
Joined: 04 Feb 2021, 15:48

Recording and playback mouse movement in polar coordinates script

16 Oct 2021, 05:45

I found a very interesting script that allows you to record the movement of the mouse and play it in any direction and at any distance (between any two points):

Code: Select all

; Natural Mouse Movement
  ; --- Begin Demo Section ---
  CoordMode, ToolTip, Screen 
  CoordMode, Mouse, Screen
  MsgBox, start recording Mousemoves.`nClick the left mouse-button to stop.
  record := NMM_RecordUntilClick()
  MsgBox Movings recorded.`nPress Up/Down/Left/Right to `nplayback the movement in different directions.
  startx := A_ScreenWidth / 2
  starty := A_ScreenHeight / 2
  return

  ~Up::
  MouseMove, %startx%, %starty%
  NMM_PlaybackMouseMovement(startx, starty, startx, 0, record)
  return
  ~Down::
  MouseMove, %startx%, %starty%
  NMM_PlaybackMouseMovement(startx, starty, startx, A_ScreenHeight, record)
  return
  ~Left::
  MouseMove, %startx%, %starty%
  NMM_PlaybackMouseMovement(startx, starty, 0, starty, record)
  return
  ~Right::
  MouseMove, %startx%, %starty%
  NMM_PlaybackMouseMovement(startx, starty, A_ScreenWidth, starty, record)
  return
	~s::
	MsgBox, %record%
	return
  ; --- End Demo Section ---
   

  ; Natural Mouse Movement
  ; Simulate natural mouse-movement with movement-Templates
  ; Save your actual mouse-movement to a template with:
  ; NMM_RecordUntilClick()
  ; and use this template to move the mouse
  ; from any point on the screen to any other point with:
  ; NMM_PlaybackMouseMovement(BaseX, BaseY, TargetX, TargetY, Template)
  ;

  NMM_MouseMove(x, y, scanrate)
  {
     ; adjust this function to your needs (e.g. SendMode)
     if (scanrate < 20)
        MouseMove, %x%, %y%, 0
     else if (scanrate < 60)
     {
        StartTime := A_TickCount
        MouseMove, %x%, %y% , 1
        ElapsedTime := A_TickCount - StartTime
        if ((scanrate - ElapsedTime) > 15)
           Sleep, % scanrate - ElapsedTime
     }
     else if (scanrate < 120)
     {
        StartTime := A_TickCount
        MouseMove, %x%, %y% , 2
        ElapsedTime := A_TickCount - StartTime
        if ((scanrate - ElapsedTime) > 15)
           Sleep, % scanrate - ElapsedTime
     }
     else
     {
        StartTime := A_TickCount
        MouseMove, %x%, %y% , 3
        ElapsedTime := A_TickCount - StartTime
        if ((scanrate - ElapsedTime) > 15)
           Sleep, % scanrate - ElapsedTime
     }
     return 0
  }


  NMM_PlaybackMouseMovement(BaseX, BaseY, TargetX, TargetY, Template)
  {
     
     StringSplit, record_, Template , #
     StringSplit, meta_, record_1 , :
     Target_r := NMM_Kart2Polar_r( TargetX - BaseX, TargetY - BaseY)
     Target_Phi := NMM_Kart2Polar_Phi( TargetX - BaseX, TargetY - BaseY, Target_r)
     scanrate := meta_2 * Target_r
     loop, parse, record_2, |
     {
        StringSplit, pos_, A_Loopfield , :
        x := Round( NMM_Polar2Kart_X(pos_2 * Target_r, pos_1 + Target_Phi) + BaseX )
        y := Round( NMM_Polar2Kart_Y(pos_2 * Target_r, pos_1 + Target_Phi) + BaseY )
        NMM_MouseMove(x, y, scanrate)
     }
     return 0
  }

  NMM_RecordUntilClick(scanrate = 25)
  {
     MouseGetPos, BaseX, BaseY
     Soundbeep
     ; record raw mousemovement in polar-coord
     loop
     {
        MouseGetPos, X, Y
        Current_r := NMM_Kart2Polar_r( X - BaseX, Y - BaseY)
        Current_Phi := NMM_Kart2Polar_Phi( X - BaseX, Y - BaseY, Current_r)
        record .= "|" . Current_Phi . ":" . Current_r
        if GetKeyState("LButton")
        {
           soundbeep
           break
        }
        sleep, %scanrate%
     }
     Target_r := NMM_Kart2Polar_r( X - BaseX, Y - BaseY)
     Target_Phi := NMM_Kart2Polar_Phi( X - BaseX, Y - BaseY, Target_r)
     ; normalize the record
     loop, parse, record, |
     {
        StringSplit, pos_, A_Loopfield , :
        normrecord .= "|" . (pos_1 - Target_Phi) . ":" . ( pos_2 / Target_r )
        Steps := A_Index
     }
     normscanrate := scanrate / Target_r
     outrecord := Steps . ":" . normscanrate . "#" . normrecord
     return outrecord
  }


  NMM_Polar2Kart_X(r, Phi)
  {
     x := r * Cos(Phi)
     return x
  }

  NMM_Polar2Kart_Y(r, Phi)
  {
     y := r * Sin(Phi)
     return y
  }

  NMM_Kart2Polar_r( x, y)
  {
     r := Sqrt( (x*x) + (y*y) )
     return r
  }

  NMM_Kart2Polar_Phi(x, y, r)
  {
     if (y < 0)
        Phi := -1 * ACos(x/r)
     else
        Phi := ACos(x/r)
     return Phi
  }
Also here is my slightly modified version that allows you to record the movement manually according to any template shown on the screen point by point:

Code: Select all

; Natural Mouse Movement
  CoordMode, ToolTip, Screen 
  CoordMode, Mouse, Screen
  MsgBox, Start recording coordinates:`nPress N at each point.`nThe last point determines the rotation of the template and is not played (think of the last point as the tip of the arrow and the first point as the base of the arrow). `nPress S to stop recording.
  firstTime = 1
  record := NMM_RecordUntilClick()
  toDelete = |:|
  StringReplace, NewStr, record, %toDelete%
  Clipboard := NewStr
  MsgBox Movings recorded. `nTemplate is copied to clipboard.`n%NewStr%
  return

  ~*p::
  ;Number_of_Steps:normalized_Scanrate#Point1_Phy:Point1_R|Point2_Phy:Point2_R...
  template1 := "7:0.018797#-0.651210:0.357655|0.564751:0.583010|-0.476990:0.756071|0.414260:0.930869|-0.403523:1.081880|0.000000:1.000000"
  MouseGetPos, StartX, StartY
  KeyWait, p, U
  MouseGetPos, EndX, EndY
  MouseMove, StartX, StartY, 0
  ;star X coordinate, start Y coordinate, end X coordinate, end Y coordinate, template variable, speed
  NMM_PlaybackMouseMovement(StartX, StartY, EndX, EndY, template1, 60)
  return 
  
  NMM_MouseMove(x, y, scanrate)
  {
     ; adjust this function to your needs (e.g. SendMode)
     if (scanrate < 20)
        MouseMove, %x%, %y%, 0
     else if (scanrate < 60)
     {
        StartTime := A_TickCount
        MouseMove, %x%, %y% , 1
        ElapsedTime := A_TickCount - StartTime
        if ((scanrate - ElapsedTime) > 15)
           Sleep, % scanrate - ElapsedTime
     }
     else if (scanrate < 120)
     {
        StartTime := A_TickCount
        MouseMove, %x%, %y% , 2
        ElapsedTime := A_TickCount - StartTime
        if ((scanrate - ElapsedTime) > 15)
           Sleep, % scanrate - ElapsedTime
     }
     else
     {
        StartTime := A_TickCount
        MouseMove, %x%, %y% , 3
        ElapsedTime := A_TickCount - StartTime
        if ((scanrate - ElapsedTime) > 15)
           Sleep, % scanrate - ElapsedTime
     }
     return 0
  }


  NMM_PlaybackMouseMovement(BaseX, BaseY, TargetX, TargetY, Template, speed)
  {
     StringSplit, record_, Template , #
     StringSplit, meta_, record_1 , :
     Target_r := NMM_Kart2Polar_r( TargetX - BaseX, TargetY - BaseY)
     Target_Phi := NMM_Kart2Polar_Phi( TargetX - BaseX, TargetY - BaseY, Target_r)
     scanrate := speed
	 Str = %Template%
	 StringReplace Str,Str,|,|,UseErrorLevel
	 loopCount = %ErrorLevel%
	 loopCount += 1
     loop, parse, record_2, |
     {
			if A_Index = %loopCount%
			break
			
			StringSplit, pos_, A_Loopfield , :
			x := Round( NMM_Polar2Kart_X(pos_2 * Target_r, pos_1 + Target_Phi) + BaseX )
			y := Round( NMM_Polar2Kart_Y(pos_2 * Target_r, pos_1 + Target_Phi) + BaseY )
			NMM_MouseMove(x, y, speed)
			
			;-----------------------------------------------------------------------
			;Action in each point
			;MouseClick, right
			;-----------------------------------------------------------------------
     }
     return 0
  }

  NMM_RecordUntilClick(scanrate = 15)
  {
	Loop,
	{
			GetKeyState, state, n , P
			If State=d
			Break
	}
  
     MouseGetPos, BaseX, BaseY
     Soundbeep
     ; record raw mousemovement in polar-coord
     loop
     {
		Loop,
		{
				GetKeyState, state, n , P
				If State=d
				Break
				
				GetKeyState, state, s , P
				If State=d
				GoTo, end
		}
		soundbeep
		
        MouseGetPos, X, Y
        Current_r := NMM_Kart2Polar_r( X - BaseX, Y - BaseY)
        Current_Phi := NMM_Kart2Polar_Phi( X - BaseX, Y - BaseY, Current_r)
		record .= "|" . Current_Phi . ":" . Current_r
		
        sleep, %scanrate%
     }
	 end:
     Target_r := NMM_Kart2Polar_r( X - BaseX, Y - BaseY)
     Target_Phi := NMM_Kart2Polar_Phi( X - BaseX, Y - BaseY, Target_r)
     ; normalize the record
     loop, parse, record, |
     {
        StringSplit, pos_, A_Loopfield , :
        normrecord .= "|" . (pos_1 - Target_Phi) . ":" . ( pos_2 / Target_r )
        Steps := A_Index
     }
     normscanrate := scanrate / Target_r
     outrecord := Steps . ":" . normscanrate . "#" . normrecord
     return outrecord
  }


  NMM_Polar2Kart_X(r, Phi)
  {
     x := r * Cos(Phi)
     return x
  }

  NMM_Polar2Kart_Y(r, Phi)
  {
     y := r * Sin(Phi)
     return y
  }

  NMM_Kart2Polar_r( x, y)
  {
     r := Sqrt( (x*x) + (y*y) )
     return r
  }

  NMM_Kart2Polar_Phi(x, y, r)
  {
     if (y < 0)
        Phi := -1 * ACos(x/r)
     else
        Phi := ACos(x/r)
     return Phi
  }
Steps for my version:
Run the script click OK
Hover the cursor over the first point and press n
Move the cursor to the next point and press n
After the last point, place another point that will determine the rotation of the template (think of the last point as the tip of the arrow and the first point as the base of the arrow), this point is not played, press n
Press s to stop recording, the template will be copied to the clipboard.

You can play the template, for example in this way (just press p move the mouse in any direction and release p):

Code: Select all

~*p::
;Number_of_Steps:normalized_Scanrate#Point1_Phy:Point1_R|Point2_Phy:Point2_R...
template1 := "7:0.018797#-0.651210:0.357655|0.564751:0.583010|-0.476990:0.756071|0.414260:0.930869|-0.403523:1.081880|0.000000:1.000000"
MouseGetPos, StartX, StartY
KeyWait, p, U
MouseGetPos, EndX, EndY
MouseMove, StartX, StartY, 0
;star X coordinate, start Y coordinate, end X coordinate, end Y coordinate, template variable, speed
NMM_PlaybackMouseMovement(StartX, StartY, EndX, EndY, template1, 60) 
return

Return to “Gaming Scripts (v1)”

Who is online

Users browsing this forum: No registered users and 30 guests