Getting half an angle

I need to get the angle that is half of the angle between an entity and a player’s view. You would get the angle like:

[lua](someVector - LocalPlayer():GetShootPos()):Angle()[/lua]

But how would I get half of the angle? I tried just dividing all three keys of the angle by to and multiplying them by two for the lulz, but it didn’t produce the desired effect.

[lua]
– using AngToEntity and PlyEye for the angles you want the average of
local HalfBetween = Angle(0,0,0) --placeholder value for the average
–YAW
local yawdif = PlyEye.y - AngToEntity.y --difference in yaw
if yawdif > 180 then yawdif = yawdif - 360 end --i think angles in source are 0 to 360 so we’re going to convert it to between -180 and 180
if yawdif < -180 then yawdif = yawdif + 360 end
HalfBetween.y = AngToEntity.y + (yawdif/2) --add on half the difference
–same sort of thing for pitch
[/lua]
I’m not absolutely sure that’s right but give it a go. Basically I’m adding on half the difference.

I would go about it by getting the dot product of the two vectors, then arccos that to find the angle between them (assuming they are both normalised). Then divide that angle by two. Then rotate the first vector around the cross product of the two vectors by that angle.

That may sound confusing, but it is actually really simple. Here is some code:
[lua]local dot = vec1:Dot(vec2);
local cross = vec1:Cross(vec2);
local ang = math.deg(math.acos(dot))/2;
local vec1ang = vec1:Angle();

vec1ang:RotateAroundAxis(cross, ang);

local newvec = vec1ang:Forward();
local halfangle = (vec1-newvec):Angle(); --This is the angle you are looking for (pitch, yaw and roll)[/lua]

Obviously this may be totally wrong, try it. :smug:

I wish I knew what the dot product of a vector was.

Assuming the vectors v1 and v2 are normalised (have a unit length of 1), the dot product of them is equal to the length of the adjacent side of the right-angle triangle formed by the two:

[editline]05:12PM[/editline]

That picture looks like it was done by a 9 year old :smile:

That’s pretty interesting.

So why is having the length of the adjacent side of the right triangle formed by the two angles helpful? I don’t doubt that the code works or anything (haven’t tried it yet though) but that seems like some random calculation that isn’t helpful. Obviously that’s wrong, so why is the dot product helpful?

The arccos of the dot product gives the angle between the two vectors.

[editline]05:33PM[/editline]

Basically, the dot product is equal to sin(angle) when both vectors are normalised.

[lua]
local Vect1 = LocalPlayer():GetShootPos():Normalize()
local Vect2 = toPoint:Normalize()

	local dot = Vect1:Dot(Vect2)
	local cross = Vect1:Cross(Vect2)
	local ang = math.deg(math.acos(dot)) / 2
	local Vect1ang = Vect1:Angle()

	Vect1ang:RotateAroundAxis(cross, ang)

	local newvec = Vect1ang:Forward()
	local halfangle = (Vect1 - newvec):Angle()

[/lua]

This code gives extremely weird results. It’s like the angle avoids looking at the point.

What are you trying to do?