Angles outputs half circle instead of full circle angle

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I want angles go full 360° instead of half circle

  2. What is the issue? It prints -179° to -1° instead of full circle

  3. What solutions have you tried so far? I haven’t found any solutions

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local part1 = workspace.Part1
local part2 = workspace.Part2

local angle = 0


local function circle(angle,magnitude)
	local x = -math.cos(math.rad(angle)) * 7
	local z = math.sin(math.rad(angle)) * 7
	
	return x,z
end

game:GetService("RunService").Heartbeat:Connect(function(dt)
	angle += 1
	local relativity = part2.CFrame:ToObjectSpace(part1.CFrame)
	local angledebug = math.deg(math.atan2(relativity.X,relativity.Z))
	
	local x,z = circle(angle)
	
	part2.Position = part1.Position + Vector3.new(x,0,z)
	
	print("Angle between two parts is: "..math.floor(angledebug).."°")
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

Your script works just fine and everything is mathematically correct.

The reason you’re getting this error is related to the floating point imprecision. Because you’re flooring the result, the step is occasionally going to show as 0, as the difference will be almost 1 but slightly less, such as 0.998.

See this small modification

Please ignore the initial difference of 90 deg. It’s there because debug angle values are still 0 in the first cycle.

local part1 = workspace.Part1
local part2 = workspace.Part2

local angle = 0
local debugAngle = 0
local prevDebugAngle = 0

local function circle(angle, magnitude)
	local x = -math.cos(math.rad(angle)) * 7
	local z = math.sin(math.rad(angle)) * 7

	return x, z
end

part2.Position = part1.Position + Vector3.new(-7,0,0)

game:GetService("RunService").Heartbeat:Connect(function(dt)
	angle += 1
	angle = (angle == 361) and 0 or angle
	
	local relativity = part2.CFrame:ToObjectSpace(part1.CFrame)
	prevDebugAngle = debugAngle
	debugAngle = math.deg(math.atan2(relativity.X,relativity.Z))

	local x,z = circle(angle)

	part2.Position = part1.Position + Vector3.new(x,0,z)

	if debugAngle - prevDebugAngle < .95 or debugAngle - prevDebugAngle > 1.05 then
		print("Difference: "..debugAngle - prevDebugAngle)
	end
end)
0 - 359 = -359

Why need a difference between a previous angle and current angle? I am trying to make the angle outputs above 181°

The difference was meant for alternative illustration that the script works as expected, and to show that each angle step is proper, which was not an issue and an overlook on my part.

Can you please elaborate on your use case? I can’t reproduce the issue. Here’s your original script in action.

What I mean is that it outputs:

 06:14:48.522  Angle between two parts is: 180°  -  Server - Script:23
  06:14:48.539   ▶ Angle between two parts is: -179° (x2)  -  Server - Script:23

instead of going to >181°

It’s just how things are, not sure why exactly … It’s easy to convert it to 0-359°.

(angle + 360) % 360

Or something close.

2 Likes

Perfection :100:

This is it. Sorry if I caused any confusion, idk what I was thinking.

It’s not uncommon for angles to be represented in range from -180 to 180 instead of from 0 to 360. It’s a supported design decision (though normal 360-degree representation works in Roblox too). A range from -180 to 180 means 0 would be a straight angle in relation with the 360-degree system, and -180 as well as 180 represent the same angle. But in game development it’s normally interpreted as no rotation. Comparing positive against negative angles is often more practical. FOV 140 would mean -70 to the left and 70 to the right. Or imagine a gyro/attitude horizon instrument in airplanes.

1 Like

Exactly this is what I am looking for

1 Like

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