Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate # move mouse using length + angle Best Answer Masonjar13 , 16 August 2014 - 05:32 AM

6 replies to this topic • Members
• 279 posts
• Last active: Dec 22 2015 03:07 AM
• Joined: 29 Jun 2011

I'm trying to figure out how I can make my mouse move to a position defined by length + angle

Example 1: Length = 950, angle = 90 that would be all the way to the right of my computer screen

Example 1: Length = 1000, angle = 225 that would be near my start button

Problem is I'm not sure how to move it according to the angle.

Any help would be great! =) • Members
• 1517 posts
• Last active:
• Joined: 16 Sep 2012

Math can and will solve that. However, you didn't specify where the angle starts. Is this from the center of the screen?
Here's the algorithm I found: (x2,y2)=(x1+l⋅cos(a),y1+l⋅sin(a))

```x1 := A_ScreenWidth / 2 ; Starting pos (x)
y1 := A_ScreenHeight / 2 ; Starting pos (y)

len := 1000 ; Length
ang := 225 ; Angle

x2 := x1 + len * cos(ang)
y2 := y1 + len * sin(ang)

MouseMove,% x2,% y2
```

And for convenience, a function:

```GetPosFromAngle(ByRef x2,ByRef y2,x1,x2,len,ang){
x2 := x1 + len * cos(ang)
y2 := y1 + len * sin(ang)
}```

PLEASE NOTE: I am not a mathematician. I did not write this formula, only the code. Sorry if it doesn't work.

OS: Windows 7 Ultimate / Windows 8.1 Pro | Editor: Notepad++ • Members
• 279 posts
• Last active: Dec 22 2015 03:07 AM
• Joined: 29 Jun 2011

I appreciate the help, I've been digging around for a while and found a similar piece of code to the one you linked, I've tried both and can't quite get it to work for me.

Here's what I got so far.

```f3::
txx = -9.365444  ;static target x coordinate
tyy = 581.791077 ;static target y coordinate

X:=txx-tx
Y:=tyy-ty
dist:= sqrt((X*X) + (Y*Y)) ;distance
angle:= Atan2(X,Y) * (180 / Pi)
if angle < 0
angle := 360 + angle
if angle > 360
angle -= 360
sleep 1000

newX := BaseX + dist * cos(angle)
newY := BaseY + dist * sin(angle)

;newX := BaseX + 100 * sin(degree2rad(angle)) ;previus code similar results
;newY := BaseY + 100 * cos(degree2rad(angle))

MouseMove, %newX%, %newY%
return
```

I forgot to mention the mouse base, it's located at the center of a windowed game x800 y400.

I've also tried both coordmodes, screen and relative. makes a small difference but nothing else.

I've done debugging and my distance and degrees are spot on always returning exactly where they should,, perhaps I'm not doing the other math right, if so let me know please, I can't really understand this math like I'd want to but I am trying. • Members
• 129 posts
• Last active: Sep 24 2016 08:15 AM
• Joined: 21 Jun 2012
```deg = 225
lenght=1000
1::
MouseGetPos, mx, my
MouseMove, %mx2%, %my2%, 100, R
return``` • Members
• 1517 posts
• Last active:
• Joined: 16 Sep 2012

Oh, duh, it has to be in radians.. Thanks ahcahc. Modified my code and tested:

```GetPosFromAngle(mx,my,960,540,1000,225)
MouseMove,% mx,% my
return

GetPosFromAngle(ByRef x2,ByRef y2,x1,y1,len,ang){
ang := ang * 0.0174532925
x2 := x1 + len * cos(ang)
y2 := y1 + len * -sin(ang)
}```

OS: Windows 7 Ultimate / Windows 8.1 Pro | Editor: Notepad++ • Members
• 279 posts
• Last active: Dec 22 2015 03:07 AM
• Joined: 29 Jun 2011

That looks promising! Is there anyway to flip the radian so it's going clockwise from 12:

0

270         90

180

right now it appears to be going counterclockwise from 3:

90

180              0

270

Really appreciate the work so far though it seems to be working the way I need it just in a different order. =)

I believe I got it now, Thanks you so much for the help everything is working now! =D • Members
• 1 posts
• Last active: Nov 04 2015 08:53 AM
• Joined: 22 Oct 2015

Thank you for this position it allowed me to move forward on my project. However I have a little problem: I have coordinates (x and y memoryread) but they are in Game positions. I wonder how determine how many pixels should move my mouse in comparison to the InGame position. I do not know if I make myself understood. For example my destination point is -80 300 (X and Y of games) but on my screen when I move to those pixels that does not match. How to calculate the ratio between the two? (I'm working on the hack Slash Marvel Heroes)
In advance thank you if you have a little help 