Jump to content

Get Mouse Angle


Recommended Posts

Hello dear community, today Im having a math issue.

Well, Im just trying to get the mouse angle with the center at the middle on the screen. The code is almost done, but it only works 180 degrees. It only detects the half part of the circle. top side with negatives value and top with positive values.

I know I can use conditions to make it work with 360 degrees, however Im here to know if there is a way to get a perfect angle without appealing to conditions

if isCursorShowing() then
	local px,py = getCursorPosition()
	local mx,my = sX*px,sY*py

	local distY = math.abs(my-centerY)
	local distX = math.abs(mx-centerX)
	local dist = math.sqrt((distY*distY)+(distX*distX))
	
	local val = distY/dist
	local aSine = math.atan2(my-centerY,mx-centerX);
	local degrees = aSine*(180/piVariable)
	
	--Draw degrees ...
end

Missing variables are already defined. Just to clear it. Center variables are the middle of the screen.

Thanks for reading, best regards.

Link to comment
  • Discord Moderators

Lua has math.deg to convert radians.

You can add 180 degrees to get 0-360. If that was your question?

local sX, sY = guiGetScreenSize()
local centerX = sX/2
local centerY = sY/2
addEventHandler("onClientRender", root,
	function()
		if isCursorShowing() then
			local px,py = getCursorPosition()
			local mx,my = sX*px,sY*py
			
			local aSine = math.atan2(my-centerY, mx-centerX)
			local degrees = math.deg(aSine) + 180
			dxDrawText(tostring(degrees), 400, 400)
		end
	end)

 

  • Like 1
Link to comment

@Zango

Thats it ! Well, I have the last question, how could I select the angle to start.

For example, the angle starts at the red line, but I want that it starts at green line.

I know that when I use the angle while drawing images or whatever i can use the angle argument, but, I just would like to know whats the right math application in this case

Spoiler

mta-screen_2020-05-01_12-00-22.png

Thanks for reading, best regards.

Link to comment
  • Discord Moderators
(math.deg(aSine) + 90) % 360

Add 90 to get 0 at the green line, and use modulus (% 360) so it counts from 0 when it reaches 360 (361 % 360 is 1)

  • Like 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...