PuzzledGreatly wrote: ↑27 Sep 2021, 00:54
Sorry, I don't under stand the vector class idea at all.
That's ok. In that example it is just a object with a
X and a
Y key.
Other than that it had one math function which didn't require you to pass two values to it because the object that calls up the math function automatically gets used. So
Distance := Obj1.Dist( Obj2 ) is the pixel distance between obj1 and obj2
There are a number of other math functions you can add to the class, but in your case it was all that was needed.
Anyways, here is a simple example of the whole vectors and distance thing.
This creates a red square that will jump to a new spot if the distance from your cursor to the center of the red square gets less than 60px
Code: Select all
#SingleInstance, Force
CoordMode, Mouse, Screen
Width := 100 , Height := 100
x := Random( 0 , A_ScreenWidth - Width ) ;Set a random x and y for the gui
y := Random( 0 , A_ScreenHeight - Height )
GuiPosition := New Vector( x , y ) ;Create a vector object ( a x/y pair ) for the gui.
Gui, 1:+AlwaysOnTop -Caption
Gui, 1:Color, RED
Gui, 1:Show, % "x" GuiPosition.X " y" GuiPosition.Y " w" 100 " h" 100 " NA"
SetTimer, WatchCursor, 30
return
WatchCursor:
MouseGetPos, x, y ;Get the cursor position
CursorPosition := New Vector( x , y ) ;Create a vector object ( a x/y pair ) for the cursor.
GuiCenter := New Vector( GuiPosition.X + Width / 2 , GuiPosition.Y + Height / 2 ) ;Create a vector for the center of the gui.
if( CursorPosition.Dist( GuiCenter ) < 60 ){ ; Check to see if the cursor is within 60px of the center of the gui.
GuiPosition := New Vector( Random( 0 , A_ScreenWidth - Width ) , Random( 0 , A_ScreenHeight - Height ) ) ;Create a new vector object ( a x/y pair ) for the gui.
Gui, 1:Show, % "x" GuiPosition.X " y" GuiPosition.Y " w" 100 " h" 100 " NA" ;Move the window to its new position
}
GuiCenter := , CursorPosition := "" ;clean up
return
Random( Min := 1 , Max := 100 ){ ;Function returns a random number
local out
Random, out, Min, Max
return out
}
Class Vector { ;This is just a object with a x and y key and a function to get the distance between two points.
__New( x := 0 , y := 0 ){
This.X := x , This.Y := y
}
dist( in1 ){
return Sqrt( ( ( This.X - In1.X ) ** 2 ) + ( ( This.Y - In1.Y ) ** 2 ) ) ; Pythagorean Theorem
}
}
*ESC::ExitApp
This one has 500 red squares that teleport away from your cursor.
Code: Select all
#SingleInstance, Force
CoordMode, Mouse, Screen
SetBatchLines, -1
SetControlDelay, -1
TargetCount := 500
Width := 20 , Height := 20
GuiPosition := []
Handles := []
Loop, % TargetCount {
GuiPosition[ A_Index ] := New Vector( Random( 10 , A_ScreenWidth - ( Width + 10 ) ) , Random( 10 , A_ScreenHeight - ( Height + 10 ) ) )
Gui, New, +AlwaysOnTop -Caption +HwndHwnd +ToolWindow
Gui, Color, RED
Gui, Show, % "x" GuiPosition[ A_Index ].X " y" GuiPosition[ A_Index ].Y " w" Width " h" Height " NA"
Handles[ A_index ] := hwnd
}
SetTimer, WatchCursor, 30
return
WatchCursor:
MouseGetPos, x, y
CursorPosition := New Vector( x , y )
Loop, % TargetCount {
if( CursorPosition.Dist( GuiPosition[ A_Index ] ) < 20 ){
GuiPosition[ A_Index ] := New Vector( Random( 10 , A_ScreenWidth - ( Width + 10 ) ) , Random( 10 , A_ScreenHeight - ( Height + 10 ) ) )
Gui, % Handles[ A_Index ] ":Show", % "x" GuiPosition[ A_Index ].X " y" GuiPosition[ A_Index ].Y " w" Width " h" Height " NA"
Gui, % Handles[ A_Index ] ":Color", Blue
}
}
return
Random( Min := 1 , Max := 100 ){
local out
Random, out, Min, Max
return out
}
Class Vector {
__New( x := 0 , y := 0 ){
This.X := x , This.Y := y
}
dist( in1 ){
return Sqrt( ( ( This.X - In1.X ) ** 2 ) + ( ( This.Y - In1.Y ) ** 2 ) )
}
}
*ESC::ExitApp