Page 1 of 1

Help with math please

Posted: 21 Aug 2018, 22:36
by tomoe_uehara
Hello, here's what I'm trying to do:
  1. Press F1 to start the script
  2. It will move the mouse cursor to center of monitor screen
  3. Then the mouse cursor will move away from the center, toward the edge of the screen
  4. The angle should be randomized (imagine an exploding firework)
  5. When the mouse cursor hits the edge, it will return back to the center again
I can't seem to write the math of this kind of thing :oops:
Thank you in advance

Re: Help with math please

Posted: 22 Aug 2018, 03:48
by wolf_II
Hi, tomoe_uehara! :wave:

Try this:

Code: Select all

#NoEnv
#SingleInstance, Force
CoordMode, Mouse, Screen

Pi := 4 * ATan(1)
cX := A_ScreenWidth // 2
cY := A_ScreenHeight // 2

Esc:: ExitApp ; emergency exit



;-------------------------------------------------------------------------------
F1:: ; hotkey to start the animation
;-------------------------------------------------------------------------------
    Loop {
        MouseMove, cX, cY
        Random, Angle, 0, 2 * Pi
        MouseMove
            , cX + 500 * Cos(Angle)
            , cY - 500 * Sin(Angle)
            , 20
    }

Return
I hope you like. Please ask for refinements if needed.
I kept the trigonometry simple, "explosion range" is hardcoded to be 500 pixels. (for now, mouse does not hit the edges of a 1920x1080 screen)

Re: Help with math please

Posted: 22 Aug 2018, 06:23
by garry
@wolf_II, great ,thank you
hope you like. Please ask for refinements if needed.
I kept the trigonometry simple, "explosion range" is hardcoded to be 500 pixels. (for now, mouse does not hit the edges of a 1920x1080 screen)
( maybe can use like this , which is correct for vertical / horizontal , for diagonal move ( which is longer ) needs new calculation )

Code: Select all

 
 ;...
            , cX + cx * Cos(Angle)
            , cY - cy * Sin(Angle)
;...

Re: Help with math please

Posted: 22 Aug 2018, 07:28
by Noo0B
Maybe this will give a more homogeneous spread:

Code: Select all

#SingleInstance, Force
CoordMode, Mouse
SetFormat, Float, 0.6
SetDefaultMouseSpeed, 0
ExplosionSpeed:= 2
Esc:: ExitApp
F1::
	Loop {
		Mousemove, A_ScreenWidth/2, A_ScreenHeight/2
		Random, SpreadX, -1.00000, 1.00000
		Random, SpreadY, -1.00000, 1.00000
		Mousemove, SpreadX * A_ScreenWidth/2, SpreadY * A_ScreenHeight/2, %ExplosionSpeed%, R
	}
Return
Regards.

Re: Help with math please

Posted: 22 Aug 2018, 09:11
by swagfag
there are probably cleverer ways of doing this:

Code: Select all

; https://autohotkey.com/boards/viewtopic.php?p=234882#p234882
#NoEnv
#SingleInstance Force
; SendMode Input
SetBatchLines -1
CoordMode Mouse, Screen

F1::move(rand(0.0, 360.0))
Esc::ExitApp

rand(min, max) {
	Random rand, min, max
	return rand
}

toRad(deg) {
	static PI := 4 * ATan(1)
	return deg * PI / 180
}

toDeg(rad) {
	static PI := 4 * ATan(1)
	return rad * 180 / PI
}

move(deg, speed := 5) {
	static xMid := A_ScreenWidth // 2
	, yMid := A_ScreenHeight // 2
	, thresh := toDeg(ATan(yMid / xMid))

	deg := Mod(deg, 360)

	if (deg < 0)
		deg += 360

	alpha := Tan(toRad(deg))
	beta := Tan(toRad(90 - deg))

	if ((deg > 360 - thresh || deg >= 0) && deg <= thresh)
		x := A_ScreenWidth, y := yMid - (xMid * alpha)
	else if (deg > thresh && deg <= 180 - thresh)
		x := xMid + (yMid * beta), y := 0
	else if (deg > 180 - thresh && deg <= 180 + thresh)
		x := 0, y := yMid + (xMid * alpha)
	else if (deg > 180 + thresh && deg <= 360 - thresh)
		x := xMid - (yMid * beta), y := A_ScreenHeight

	MouseMove % xMid, % yMid, % speed
	MouseMove % x, % y, % speed
	MouseMove % xMid, % yMid, % speed
}

Re: Help with math please

Posted: 22 Aug 2018, 10:57
by tomoe_uehara
wolf_II wrote:Hi, tomoe_uehara! :wave:
Try this...
Hello wolf_II & garry, the script works great, Thanks!

Noo0B wrote:Maybe this will give a more homogeneous spread
Sorry but I need the math, by using only the MouseMove command, AHK calculates the coordinates by itself, thus I can't accept this as accepted answer, sorry ;)

swagfag wrote:there are probably cleverer ways of doing this
Your script seem doesn't work as intended, it only moves the cursor to the center of the screen but doesn't move the cursor anywhere else, maybe something is missing?

Re: Help with math please

Posted: 22 Aug 2018, 11:50
by AlphaBravo
this should do it using simple math ;)

Code: Select all

CoordMode, Mouse, Screen
cx := A_ScreenWidth // 2, cy := A_ScreenHeight // 2
return

F1::
MouseMove, cx, cy
Random, side, 1, 4				; 1=top, 2=bottom, 3=left, 4=right
Random, x, 0, A_ScreenWidth
Random, y, 0, A_ScreenHeight
if (side =1)
	y := 0
if (side =2)
	y := A_ScreenHeight
if (side =3)
	x := 0
if (side =4)
	x := A_ScreenWidth
MouseMove, x, y, 8
MouseMove, cx, cy, 8
return

Re: Help with math please

Posted: 22 Aug 2018, 12:33
by swagfag
tomoe_uehara wrote:Your script seem doesn't work as intended, it only moves the cursor to the center of the screen but doesn't move the cursor anywhere else, maybe something is missing?
i guess u cant call user-defined functions to initialize static variables. it was an issue with PI being uninitialized, while the static initializers were depending on it. its fixed now

Re: Help with math please

Posted: 22 Aug 2018, 14:29
by garry
thank you all for the interessant solutions , I think I never found them all
AlphaBravo , tricky, seems so easy ....

Re: Help with math please

Posted: 23 Aug 2018, 01:04
by Noo0B
tomoe_uehara wrote:Sorry but I need the math, by using only the MouseMove command, AHK calculates the coordinates by itself, thus I can't accept this as accepted answer, sorry ;)
I was wondering, if in case you need math help, StackOverflow or so might be a better bet for you.
Either way, you will need to formulate better what you are asking for, I'm not sure what you mean with "I need the math".
I can't also really guess what kind of coordinates you need as well.
In case you need circular coordinate values for a script, you can randomize for a full circle and use sin and cosin for the move factors on the code I showed you.
Regards.