Angle interpolation is broken when rotated on y axis

Hello! I am currently working on a placement system that is fully customizable for the user. The problem I am having is with a angle lerp feature. If you’ve used tunicus’s module you’ll know what I mean. If you haven’t, here is an example:


That clip was from my module as the problem presents when you rotate the model. As soon as the model is rotating on the y axis, it messes stuff up:


Currently I am using this function to calculate the angle:

local function calculateAngle(a, b) 
    -- a is the the distance the mouse is away from the model
    -- b is the models position
    -- the function is called twice to handle both x and z rotation
	local x1, z1 = cos(a), sin(a)
	local x2, z2 = cos(b), sin(b)
	
	return atan2(z2 - z1, x2 - x1)*angleInterpolationAmount
end

My question is, is there a way to make it work even when rotation the y axis. I am really not good with math so I have no idea how any of this works.

1 Like

I’m so confused why you would use the distance as the radian its the ratio between the side lengths which is passed in. For cosine that’d be the adjacent and hypotenuse relative to the angle and if we just follow soh cah toa its the same for the rest. Try applying this and see if it helps

See you actually understand this. I just tried to copy the way another script did it and got this. I’ll see what I can do though.

I know the last reply was last year, but for any future readers, I’ve solved this issue. The function that controls the angle calculation is this:

-- Calculates the "tilt" angle
local function calcAngle(last, current) : CFrame
	if angleTilt then
		-- Calculates and clamps the proper angle amount
		local tiltX = (math.clamp((last.X - current.X), -10, 10)*pi/180)*amplitude
		local tiltZ = (math.clamp((last.Z - current.Z), -10, 10)*pi/180)*amplitude

		-- Returns the proper angle based on rotation
		return (anglesXYZ(dirZ*tiltZ, 0, dirX*tiltX):Inverse()*anglesXYZ(0, rot*pi/180, 0)):Inverse()*anglesXYZ(0, rot*pi/180, 0)
	else
		return anglesXYZ(0, 0, 0)
	end
end

You can see how it’s used in the module.

1 Like