Calculating unit doesn't result in values that add up to 1 (trigonometry fail)

Alright, so lately i’ve decided to make a game that’s a direct copy of Celeste (a platformer that revolves around dashing and some other stuff).

If you’ve ever played it, you know that theres 8 directions that you can dash in, those Up, Right, Left, Down and the diagonals.

However… Bringing this to 3D is pretty tough, as you get a new dimension that you have to do trigonometry in (i’ve got zero experience in this).

I understand the baaaasics of trigonometry (as i go to school) and know that cos is the X axis in a 2d space, and sin is the Y axis in a 2d space. Anyway, i’ve made a rather simple script that… doesn’t work.

When i add up the math.abs of the X, Y and Z vectors they a lot of times result in a value that’s bigger than 1, which should not happen, it should ALWAYS equal 1 as i want my dashes to be consistent.

Here’s the script!! Please, help me with this and explain how you fixed my script, as i think i will have to use trigonometry a LOT.

local pressingS = keybinder.AskForKeybind("S") -- backward
	local pressingW = keybinder.AskForKeybind("W") -- forward
	local pressingR = keybinder.AskForKeybind("R") -- upper
	local pressingQ = keybinder.AskForKeybind("Q") -- downer
	local pressingD = keybinder.AskForKeybind("D")
	local pressingA = keybinder.AskForKeybind("A")
	
	local camCF:CFrame = cameraasking:Invoke()
	
	local _, y, _ = camCF:ToEulerAnglesYXZ()
	
	local yangle = math.deg(y)
	
	local right = not checkifnil(pressingD)
	local left = not checkifnil(pressingA)
	local forward = not checkifnil(pressingW)
	local backward = not checkifnil(pressingS)
	local upward = not checkifnil(pressingR)
	local downward = not checkifnil(pressingQ)
	
	local negativeYOffset = (backward and not forward) and true or false
	
	local Ynumber = 0
	local Xnumber = 0
	-- getting the x offset
		local Xangletable = {}
		if forward then table.insert(Xangletable, 0) end
		if backward and not forward then table.insert(Xangletable, -180) end
		if upward then table.insert(Xangletable, 90) end
		if downward then table.insert(Xangletable, -90) end
		if #Xangletable > 0 then
			print(Xangletable)
			for i, v in Xangletable do
				Xnumber += v
			end
			Xnumber /= #Xangletable
		end
	
	
	
		local Yangletable = {}
		if forward or backward then table.insert(Yangletable, 0) end
		if left then table.insert(Yangletable, 90) end
		if right then table.insert(Yangletable, -90) end
		if #Yangletable > 0 then
			print(Yangletable)
			for i, v in Yangletable do
				Ynumber += v
			end
			Ynumber /= #Yangletable
			if negativeYOffset then Ynumber = -Ynumber end
			print(Ynumber)
		end
	
	yangle += Ynumber
	
	
	
	
	local Yvector = math.sin(Xnumber)
	local Xvector = math.cos(Xnumber) * math.sin(yangle)
	local Zvector = math.cos(Xnumber) * math.cos(yangle)
	print(math.abs(Yvector)+math.abs(Xvector)+math.abs(Zvector)) --prints a value that's NOT 1 (bad)

some clarifications: the xangletable and the yangletable i use to get the arithmetic mean of the player’s inputs. the angle offsets work decent… i think? im not sure if my problems are because my angles go over 180, that may actually be the case. if it is, please tell me how to fix it as i’ve got no idea.

2 Likes

Didnt read all of it, seems like you have trouble unitizing your vector.

Usually you dont just add the xyz components, in 3d you have to use pythagoras theorem and unitize your vector.

1742570532719169663774209235620

4 Likes

Try putting it in a while loop without any waits and your done :smile:

hmm, thanks. gonna be honest, this is my first time hearing of 3d pythagoras so ill see rn if its gonna give me the result i want (sorry for some illiteracy, im on my phone, as if im not always illeterate)

Oh my GOD it actually gives 1 when i do math.sqrt(Yvector^2 + Xvector^2 + Zvector^2)!! Thanks a bunch, pal.

This isn’t the process of normalization. What you’re doing is calculating the magnitude/hypotenuse of a vector. A normalized vector will produce a magnitude of 1 because it is characterized by components which have had the vector’s magnitude factored out:

local function normalize(vector3: Vector3): Vector3
    return vector3 / vector3.Magnitude
end
1 Like

Wait, so i’m confused here. Does this mean that my vector’s magnitude isn’t one?

No. All unit vectors have a magnitude of 1. Each component of the vector had the vector’s magnitude factored out, which is why your components were decimals in the range of [0,1]; the vector now solely represents direction. Calculating the magnitude of a normalized/unit vector will show a magnitude of 1

But now it always returns 1 both when i do 3d pythagros and when i combine the individual coordinate numbers into a vector3 and then do .Magnitude so… the magnitude should be one? or was your no answer implying that “yes it’s one”?

Yes. Your vector’s magnitude is 1

1 Like

Phew, thanks. I was afraid for a second because that woulda made stuff a lot more complicated

also if anyone is going to read this post in the far far future please remember to use math.deg when doing math.cos

1 Like