Mouse Movement Help

Ask gaming related questions (AHK v1.1 and older)
prototype_zero
Posts: 17
Joined: 19 Mar 2024, 13:01

Mouse Movement Help

Post by prototype_zero » 22 Apr 2024, 09:55

Hello,

I'm making this object tracking script using autohotkey. I was able to get it to work in game, where the mouse follows the object. However, the mouse tracking movement is slow, choppy, and has a delayed reaction time to the object movement. I'm trying to achieve as close to a 1:1 tracking effect, if that makes sense. Any help would be appreciated.

Code: Select all

ScaleFactor := 20  ; Set the ScaleFactor to 1

#Include mousedelta.ahk

; Instantiate the MouseDelta class
Mouse := new MouseDelta(Func("MouseMoveCallback"))

; Flag to track whether DPI script is active
DPIActive := false

; Function to move the mouse cursor smoothly to a specific position on the screen
SmoothMoveMouseTo(targetX, targetY, duration, speedFactor) {
    ; Activate DPI script to increase mouse speed
    DPIActive := true
    
    ; Get the current mouse position
    MouseGetPos, mouseX, mouseY
    
    ; Calculate the distance to move
    distanceX := targetX - mouseX
    distanceY := targetY - mouseY
    
    ; Calculate the total distance
    totalDistance := Sqrt(distanceX ** 2 + distanceY ** 2)
    
    ; Calculate step increments
    steps := Max(Abs(distanceX), Abs(distanceY))
    stepX := distanceX / steps
    stepY := -1 * (distanceY / steps)  ; Invert y-axis movement
    
    ; Calculate step duration based on total distance and desired duration
    stepDuration := duration / totalDistance
    
    ; Adjust step duration based on speed factor
    stepDuration := stepDuration / speedFactor
    
    ; Perform smooth mouse movement
    Loop, % steps {
        mouseX += stepX
        mouseY += stepY
        DllCall("mouse_event", UInt, 1, Int, Round(stepX), Int, Round(stepY), UInt, 0, UInt, 0)
        ,stepDuration
    }
    
    ; Deactivate DPI script after mouse movement
    DPIActive := false
}

; Function to move the mouse cursor smoothly towards the center of the target
SmoothMoveToCenter(targetX, targetY, centerX, centerY, duration, speedFactor) {
    ; Set higher mouse speed temporarily
    SetDefaultMouseSpeed, 20
    
    ; Move the mouse cursor smoothly to the center of the target
    SmoothMoveMouseTo(targetX, targetY, duration, speedFactor)
    
    ; Restore default mouse speed
    SetDefaultMouseSpeed, 2  ; Adjust to your default mouse speed value
}

; Mouse move callback function for DPI script
MouseMoveCallback(ThisMouse, x, y) {
    ; Check if DPI script is active
    if (DPIActive) {
        ; Increase mouse speed here (implement DPI script logic)
        ; For example, you can adjust mouse speed using SetDefaultMouseSpeed
        SetDefaultMouseSpeed, 20
    }
}

; Function to read coordinates from the named pipe
ReadCoordinatesFromPipe() {
    ; Create named pipe handle
    pipe := DllCall("CreateFile", "Str", "\\.\pipe\SamplePipe", "UInt", 0xC0000000, "UInt", 3, "Ptr", 0, "UInt", 3, "UInt", 0, "Ptr", 0)
    
    ; Check if the pipe was opened successfully
    if (pipe = -1) {
        MsgBox, Failed to connect to named pipe.
        ExitApp
    }
    
    ; Infinite loop to read coordinates from the pipe
    Loop {
        VarSetCapacity(data, 8)
        
        ; Check if there is data available in the pipe
        DllCall("PeekNamedPipe", "Ptr", pipe, "Ptr", 0, "UInt", 0, "Ptr", 0, "PtrP", bytesAvailable, "Ptr", 0)
        
        ; Read data from the pipe if available
        if (bytesAvailable > 0) {
            ; Read the data from the pipe
            bytesRead := DllCall("ReadFile", "Ptr", pipe, "Ptr", &data, "UInt", 8, "Ptr", 0, "UInt", 0)
            
            ; Check if data was read successfully
            if (bytesRead > 0) {
                ; Extract x and y coordinates from the data
                x := NumGet(data, 0, "Int")
                y := NumGet(data, 4, "Int")
                
                ; Set the center coordinates of the screen (replace these with your screen's center coordinates)
                centerX := A_ScreenWidth / 2
                centerY := A_ScreenHeight / 2
                
                ; Move the mouse cursor smoothly towards the center of the target
                SmoothMoveToCenter(x, y, centerX, centerY, 5, 2000)  ; Adjust duration and speed factor as needed
                
            } else if (bytesRead < 0) {
                MsgBox, Error reading from named pipe.
                ExitApp
            }
        }
        

    }
}

; Start reading coordinates from the named pipe
ReadCoordinatesFromPipe()

; Hotkeys for Control
Pause:: Pause
^Esc:: ExitApp

; Gets called when mouse moves or stops
; x and y are DELTA moves (Amount moved since last message), NOT coordinates.
MouseEvent(MouseID, x := 0, y := 0){
    global ScaleFactor

    if (MouseID){
        x *= ScaleFactor, y *= ScaleFactor
        DllCall("mouse_event",uint,1,int, x ,int, y,uint,0,int,0)
    }
}


peter_ahk
Posts: 117
Joined: 13 Feb 2024, 14:49

Re: Mouse Movement Help

Post by peter_ahk » 22 Apr 2024, 10:52

setdefaultmouse speed is 2 and 0 is instantly maybe you did a typo putting 20 there?

prototype_zero
Posts: 17
Joined: 19 Mar 2024, 13:01

Re: Mouse Movement Help

Post by prototype_zero » 23 Apr 2024, 09:04

peter_ahk wrote:
22 Apr 2024, 10:52
setdefaultmouse speed is 2 and 0 is instantly maybe you did a typo putting 20 there?
I've tried playing with the numbers but it doesn't seem to make a difference at all. I feel like there must be something fundamental I'm missing.

peter_ahk
Posts: 117
Joined: 13 Feb 2024, 14:49

Re: Mouse Movement Help

Post by peter_ahk » 24 Apr 2024, 02:46

i cant say i tried your script or fully understand it but to me from looking at the docs setting it to 20 does slow it down. i think there is a difference in mouse dpi and setdefaultmousespeed so i would try to set it to 0 where you want speed and higher where not

The speed to move the mouse in the range 0 (fastest) to 100 (slowest).
https://www.autohotkey.com/docs/v1/lib/SetDefaultMouseSpeed.htm

prototype_zero
Posts: 17
Joined: 19 Mar 2024, 13:01

Re: Mouse Movement Help

Post by prototype_zero » 03 May 2024, 08:54

peter_ahk wrote:
24 Apr 2024, 02:46
i cant say i tried your script or fully understand it but to me from looking at the docs setting it to 20 does slow it down. i think there is a difference in mouse dpi and setdefaultmousespeed so i would try to set it to 0 where you want speed and higher where not

The speed to move the mouse in the range 0 (fastest) to 100 (slowest).
https://www.autohotkey.com/docs/v1/lib/SetDefaultMouseSpeed.htm
I tried adjusting the number, doesn't seem to make a difference. Do you know if there's a way to improve the speed and reaction time by any chance? There's a discernible lag in the mouse moving towards the target.

peter_ahk
Posts: 117
Joined: 13 Feb 2024, 14:49

Re: Mouse Movement Help

Post by peter_ahk » 03 May 2024, 12:22

i am no expert. but i cant say i understand why changing it does not matter coz why is it there then perhaps the condition is never met that it actualy changes i dunno only thing i can come yup with is try running the script itself at setbatchlines -1 see if that makes a difference

Post Reply

Return to “Gaming Help (v1)”