Angle betwen two vector Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
DrMckay
Posts: 7
Joined: 03 Jul 2019, 17:50

Angle betwen two vector

03 Jul 2019, 18:08

Hi everyone,

i want help for find one angle between two vector
anglexy.png
anglexy.png (22.94 KiB) Viewed 2738 times
i have make search on this forum and on the web but i have not found help for solve that problems

i have try different snippets but without good result

Code: Select all

	atan2(x,y) {    ; 4-quadrant atan
   		Return dllcall("msvcrt\atan2","Double",y, "Double",x, "CDECL Double")
	}
	; Try 1 ( but not work )
	; That coord want angle
	x1 := 50.35
	y1 := 56.47

	; That coord is my destination
	x2 := 43.33
	y2 := 52.20
	
	AngleCaculated := atan2(y2 - y1, x2 - x1)
	; returned angle =~ 2.1455...
	; wanted angle ~ 1.14
	
Sorry for my bad english
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Angle betwen two vector

03 Jul 2019, 18:15

try

Code: Select all

atan2(y,x) {    ; 4-quadrant atan
notice the flipped order of parameters
DrMckay
Posts: 7
Joined: 03 Jul 2019, 17:50

Re: Angle betwen two vector

03 Jul 2019, 18:19

Thx for that fast reply , i have try but now the new result its -2.566874 :(
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Angle betwen two vector

03 Jul 2019, 18:26

Not confimed. The correct result is -2.595120. (rather, My results are)
checked by two different versions. second version is home-made AHK and slower.

Code: Select all

	atan2(y,x) {    ; 4-quadrant atan
   		Return dllcall("msvcrt\atan2","Double",y, "Double",x, "CDECL Double")
	}
	; Try 1 ( but not work )
	; That coord want angle
	x1 := 50.35
	y1 := 56.47

	; That coord is my destination
	x2 := 43.33
	y2 := 52.20

	MsgBox, % AngleCaculated := atan2(y2 - y1, x2 - x1)
	; returned angle =~ 2.1455...
	; wanted angle ~ 1.14

	MsgBox, % AngleCaculated := Math.atan2(y2 - y1, x2 - x1)



;-------------------------------------------------------------------------------
class Math { ; CONSTANTS, functions
;-------------------------------------------------------------------------------
    static E := Exp(1)       ; Euler
    static PI := ACos(-1)    ; cos(π) = -1
    static HalfPI := ASin(1) ; sin(π/2) = 1
    static TwoPI := ACos(-1) * 2

    Rad(Angle) { ; convert angle in degrees => angle in radians
        return Angle * this.PI / 180
    }

    Deg(Angle) { ; convert angle in radians => angle in degrees
        return Angle * 180 / this.PI
    }

    ATan2(y, x) { ; ATan() with full range
        If (x > 0)
            return ATan(y/x)
        If (x < 0)
            return ATan(y/x) + (y >= 0 ? Math.PI : -Math.PI)
        ; else x = 0
        return (y > 0) ? Math.PI/2 : (y < 0) ? -Math.PI/2 : ""
    }

    Sign(Number) { ; signum of number
        return (Number = 0) ? 0 : (Number > 0) ? 1 : -1
    }

    Fraction(a, b) { ; return the reduced fraction {Num: a, Den: b}
        local Gcd := Abs(this.Gcd(a, b))
        return {Num: a // Gcd, Den: b // Gcd}
    }

    Gcd(a, b) { ; return the greatest common divisor of two integers
        while b
            b := Mod(a | 0, a := b)
        return a
    }

    Lcm(a, b) { ; return the lowest common multiple of two integers
       return b // this.Gcd(a, b) * a
    }

} ; end of class
DrMckay
Posts: 7
Joined: 03 Jul 2019, 17:50

Re: Angle betwen two vector

03 Jul 2019, 18:32

For a negative radian just multiply radian * -1 for get a good radian no ?

Because for me good angle its aproxymativly 1.13

I dont undersant :(
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Angle betwen two vector

03 Jul 2019, 18:37

DrMckay wrote:
03 Jul 2019, 18:32
For a negative radian just multiply radian * -1 for get a good radian no ?

Because for me good angle its aproxymativly 1.13

I dont undersant :(
I don't understand either, sorry, please try again to say why is 1.13 good for you?
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Angle betwen two vector

03 Jul 2019, 18:42

I try this, not sure, if I'm off-topic:
When Atan1() gives wrong result, we don't want to angle *= -1. we want angle += Pi or angle -= Pi.
DrMckay
Posts: 7
Joined: 03 Jul 2019, 17:50

Re: Angle betwen two vector

03 Jul 2019, 18:51

For me 1.13 its aproximativly a good because in my game when i looking at 1.13 i view a better angle for go to my destination

On 2.59 is very so far at left
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Angle betwen two vector

03 Jul 2019, 18:58

Your game could be skewed coordinates. Y lines and X lines are different distances in picture.
Are you sure, you have correct coordinates?
DrMckay
Posts: 7
Joined: 03 Jul 2019, 17:50

Re: Angle betwen two vector

03 Jul 2019, 19:05

My picture its not realy that, its just for a global idee of my problems

Game bad coordinate maybe i dont know i have just this information

north = 0
south ~3.15
west ~1.55
est ~ 4.7
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Angle betwen two vector

03 Jul 2019, 19:11

in mathematics, This is true:
East = 0
North = PI/2
West = PI
South = 3*PI/2

Screen coordinates are Y inverted. (often used by games)
East = 0
South = Pi/2
West = Pi
North = 3*Pi/2

Your game does not use either system. It may be rotated by 90 degrees, with regards to cartesian coordinates (maths),
or it may be rotated by -90 degrees, with respect to screen coordinates.
Either way, try to add Pi/2, then subtract Pi/2, and see what gets you there.
DrMckay
Posts: 7
Joined: 03 Jul 2019, 17:50

Re: Angle betwen two vector

03 Jul 2019, 20:42

Ok i have a better result but that result not work for all postion

On my top left its work
On my bottom left its not work
On my top right its not work
On my bottom right its not work

I dont know why only top left work perfeclty

Code: Select all

	atan2(y,x) {
		Return dllcall("msvcrt\atan2","Double",y, "Double",x, "CDECL Double")
	}
	Pi(){
		return 3.14159265359
	}
	
	x1 := 56.66
	y1 := 50.28
	
	; at my top left Needed ~ 1.13 ( Work )

	x2 := 52.22
	y2 := 48.23
	
	
	AngleCaculated := atan2(y2 - y1, x2 - x1) + (Pi() / 2) ; Return : -1.13 * -1 ( Good )
	
	; at my top left Needed ~ 0.61
	x2 := 55.25
	y2 := 48.26
		
	AngleCaculated := atan2(y2 - y1, x2 - x1) + (Pi() / 2) ; Return : -1.13 * -1 ( Good )
	
	; at my top left Needed ~ 1.33
	x2 := 53.41
	y2 := 49.51

	AngleCaculated := atan2(y2 - y1, x2 - x1) + (Pi() / 2) ; Return : -0.60 * -1 ( Good )

	; at my top right Needed ~ 5.46
	x2 := 59.62 
	y2 := 47.61
	
	AngleCaculated := atan2(y2 - y1, x2 - x1) ; Return : -0.73 ( False )
	AngleCaculated := atan2(y2 - y1, x2 - x1) + (Pi() / 2) ; Return : 0.83 ( False )	
	
	; at my bottom left Needed ~ 2.23
	x2 := 54.47 
	y2 := 52.28
	
	;AngleCaculated := atan2(y2 - y1, x2 - x1) ; Return : 2.40 ( False )
	AngleCaculated := atan2(y2 - y1, x2 - x1) + (Pi() / 2) ; Return : 3.97 ( False )
	
	; at my bottom right Needed ~ 3.54
	x2 := 57.68 
	y2 := 52.26
	
	AngleCaculated := atan2(y2 - y1, x2 - x1) ; Return : 1.095 ( False )
	;AngleCaculated := atan2(y2 - y1, x2 - x1) + (Pi() / 2) ; Return : 2.66 ( False )
	

	Msgbox % AngleCaculated

topleft.png
topleft.png (5.81 KiB) Viewed 2608 times
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Angle betwen two vector

03 Jul 2019, 21:53

Your picture with helping lines, and numbers:
topleft.png
topleft.png (8 KiB) Viewed 2550 times

Code: Select all

#NoEnv
#SingleInstance Force
CoordMode, Mouse, Client

    Gui, Margin, 0, 0
    Gui, Add, Picture,, topleft.png
    Gui, Show

	global Pi := 3.14159265359
    global CenterX := 210 ; by eye-ball measurement
    global CenterY := 210 ; by eye-ball measurement

    OnMessage(0x200, "onMouseMove") ; WM_MOUSEMOVE

return ; end of auto-execute section

GuiClose:
ExitApp



;-------------------------------------------------------------------------------
onMouseMove() { ; WM_MOUSEMOVE event
;-------------------------------------------------------------------------------
    MouseGetPos, mX, mY
    dx := mX - CenterX
    dy := mY - CenterY
    ToolTip, % Formula(dy, dx)
}



;-------------------------------------------------------------------------------
Formula(dy, dx) { ; testing
;-------------------------------------------------------------------------------
    ; need to invert Y (screen coords, not math coords)
    Angle := atan2(-dy, dx)

    ; need to rotate 90 degrees (game chooses North = 0, we follow)
    Angle -= Pi/2

    ; need to avoid negative result (we want range only from 0 .. 6.28)
    if (Angle < 0)
        Angle += 2 * Pi

    return Angle
}



;-------------------------------------------------------------------------------
atan2(y, x) { ; 4-quadrant atan
;-------------------------------------------------------------------------------
    Return DllCall("msvcrt\atan2", "Double", y, "Double", x, "CDECL Double")
}
All 4 quadrants work with new formula.
I hope that helps!
DrMckay
Posts: 7
Joined: 03 Jul 2019, 17:50

Re: Angle betwen two vector  Topic is solved

04 Jul 2019, 11:53

Thx for your help
i have solved my problems

Code: Select all


	AngleCaculated := Formula(y2 - y1, x2 - x1) 
	if(AngleCaculated < 0){
		AngleCaculated := Formula(y2 - y1, x2 - x1) + (Pi() * 2)
	}


Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Mateusz53, MrDoge, peter_ahk and 358 guests