Been trying to get this figured out for over an hour. I’m pretty sure I have Vector -> Angle done but probably not the best way:[lua]

– OLD

[/lua]

I also “started” writing the Angle -> Vector part but do not know how to finish it:[lua]

– OLD

[/lua]

I’m mainly doing this to learn exactly what is going on with these conversions; they are a small piece to a side project.

If anyone has ideas on how to make this better or how to finish it, it would be great!

**EDIT: **Fixed dumb mistakes, added MakeR’s line [sp](Don’t know why I didn’t think about that)[/sp]

This is what I have so far:[lua]

function VerifyAngle( a )

while a > 360 do a = a - 360 end

while a < 0 do a = a + 360 end

return a

end

function VectorToAngle( self )

local p, y = 0, 0

```
-- Pitch by angle between with acos( a dot b )
local norm = self:Normalize()
local norm2 = Vector( self.x, self.y, 0 ):Normalize()
p = math.deg( math.acos( norm:Dot(norm2) ) )
-- Yaw with atan( y / x )
if self.x == 0 then
if self.y > 0 then
y = 90
elseif self.y < 0 then
y = 180
end
else
y = math.deg( math.atan( self.y / self.x ) )
end
p = VerifyAngle( -p )
y = VerifyAngle( y )
return Angle( p, y, 0 )
```

end

function AngleToVector( self )

local x = math.cos( math.rad(self.y) )

local y = math.sin( math.rad(self.y) )

local z = -math.sin( math.rad(self.p) )

return Vector( x, y, z )

end

[/lua]

```
// V -> A
> VectorToAngle(Vector(2355,26217,63))...
359.860 84.867 0.000
> Vector(2355,26217,63):Angle()...
359.863 84.867 0.000
// A -> V
> AngleToVector(Angle(359.863,84.867,0.000))*Vector(2355,26217,63):Length()...
2355.0339 26217.0723 62.9370
> Angle(359.863,84.867,0.000):Forward()*Vector(2355,26217,63):Length()...
2355.0261 26216.9980 62.9417
```

Seems like there is some precision loss in the pitch calculation for V->A and an all around loss in A->V, or it could be Lua itself. Anyone have an idea?