# Manually converting angles and vectors back and forth

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 &gt; 0 then
y = 90
elseif self.y &lt; 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?

[lua]z = math.sin(math.rad(self.p))[/lua]

Wouldn’t this work?

[editline]11:02AM[/editline]

Why are you doing math.Rad2Deg on the return value from math.cos and math.sin?

• SNIP -

Gah, old copy pasta, I’m not sure why I have that there >.<

Sine and cosine don’t return angles, they take angles.

Updated OP, man I feel dumb.

why would you want to do this manually ?

Also converting angles in to vectors is useless as long you dont know the length isn’t it ?

Would this be of any help?

[lua]
function meta:Forward()
local pitch, yaw, roll = rad(self.x), rad(self.y), rad(self.z)

``````local x = cos(roll) * cos(yaw)
local y = sin(roll) * cos(yaw)
local z = -sin(yaw)

local mag = sqrt(x*x + y*y + z*z)

x = x/mag
y = y/mag
z = z/mag

return Vector(x,y,z)
``````

end

function meta:Up()
local pitch, yaw, roll = rad(self.x), rad(self.y), rad(self.z)

``````local x = -sin(roll) * cos(pitch) + cos(roll) * sin(yaw) * sin(pitch)
local y = cos(roll) * cos(pitch) + sin(roll) * sin(yaw) * sin(pitch)
local z = cos(yaw) * sin(pitch)

local mag = math.sqrt(x*x + y*y + z*z)
x = x/mag
y = y/mag
z = z/mag

return Vector(x,y,z)
``````

end

function meta:Right()
local pitch, yaw, roll = rad(self.x), rad(self.y), rad(self.z)

``````local x = sin(roll) * sin(pitch) + cos(roll) * sin(yaw) * cos(pitch)
local y = -cos(roll) * sin(pitch) + sin(roll) * sin(yaw) * cos(pitch)
local z = cos(yaw) * cos(pitch)

local mag = math.sqrt(x*x + y*y + z*z)
x = x/mag
y = y/mag
z = z/mag

return Vector(x,y,z)
``````

end
[/lua]

I asked how to do it some time ago and I got the answer from a friend of mine. I did this for Crysis lua.

I’m not sure if the sqrt is needed.

This is actually something for LOVE2D, making a framework for it so it is similar to GMod Lua in a few ways and I knew there would be some people understanding Lua who would definitely know how to do it.

Also, say I want to move something according to its angle:

Position = Position + 100 * Angle:Forward()

So this takes an angle and gets it’s forward / right / up vectors? Awesome this helps a lot! (Was my next question)

Well those are for 3D vectors. You really should mention that this is for 2D vectors and not 3D.

I’m doing it on 3D vector objects

Oh I see.

Are you making a 3d library for it or something?

Well, I’m trying to wrap my head around how world positions are translated into screen positions. Mainly because I’m bored and I’ve been using Love2D to test concepts for a DX9 rendering thing I’m writing in C++ and I want to know what DX9 is doing with my matrices lol.