How to find the angle of a point on a circle

Im scripting a system where a speedometer is automatically generated and Im working on a script that generates the pointers. The positioning works fine however they do not rotate how I want them to.
image
Example: I want to find angle x

Each grey box is a pointer that needs to be angled relative to it’s position on the circle
image

Script:

local Player = game.Players.LocalPlayer
local Character = Player.Character

local TextLabel = script.Parent
local Needle = script.Parent.Parent.Parent.Right.SpeedWheel.Needle

-- Needle
local maxSpeed = 170
local minAngle,maxAngle = 45,315

local totalDegrees = maxAngle-minAngle
local speedPerDegree = totalDegrees/maxSpeed

local function calculateDegree(speed)
	return math.min(minAngle+speed*speedPerDegree,maxAngle)
end


-- Speedometer
local radius = 0.5 -- half of a thing in ui terms

local function calculatePointerDegree(x, y)
	return math.deg(math.atan2(x, y))
end

local function getXAndYPositions(r, angle)
	local x = math.sin(math.rad(angle)) * r
	local y = math.cos(math.rad(angle)) * r
	
	return x, y
end

local function newPointer(angle, speed)
	local BigPointer = Instance.new("Frame", Needle.Parent)
	BigPointer.AnchorPoint = Vector2.new(0.5, 0.5)
	BigPointer.Size = UDim2.new(0.05, 0,0.02, 0)
	BigPointer.Name = "MajorPointer"
	BigPointer.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
	BigPointer.BackgroundTransparency = 0.2
	BigPointer.BorderSizePixel = 0
	
	local x, y = getXAndYPositions(radius, angle)
	BigPointer.Position = Needle.Parent.Position + UDim2.new(x, 0, y, 0)
	BigPointer.Rotation = calculatePointerDegree(x, y) -- PROBLEM
end

task.spawn(function()
	while task.wait(0.01) do
		local Speed = math.floor(Character.HumanoidRootPart.AssemblyLinearVelocity.Magnitude/1609.344/3.571*3600)
		local deg = calculateDegree(Speed)

		TextLabel.Text = Speed
		Needle.Rotation = deg
	end
end)


for i = 10, maxSpeed, 10 do
	local degree = calculateDegree(i)
	newPointer(degree, i)
end

You don’t have to use atan to find the angle since you already have the angle

Try adding offsets of multiples of 90 if it’s still off

That didn’t really help it just put them in random rotations again.

I think it looks like its turning the wrong direction as it moves along the circle

Maybe try multiplying the rotation by -1

I tried math.deg(math.atan2(-x, -y)) but it gave this result
image

I fixxed it myself I didn’t need to do this complex math in the first place I just had to make my UI in a clever way so I just had to modify the rotation to achieve this effect

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.