2017-10-11 62 views
0

夥計!簡化lua「LookAt」功能

我有一個用Lua語言編寫的名爲「LookAt」的工作函數。

該函數的代碼和邏輯沒有錯誤。

但我相信我們可以簡化數學邏輯。

function LookAt(target) 
    local origin = Vec3.New(Engine.ClientData.Origin) 
    local direction = origin - target 

    Engine.Pitch = math.deg(math.atan(direction.Z, math.sqrt((direction.X^2) + (direction.Y^2)))) 
    Engine.Yaw = math.deg(math.atan(direction.Y, direction.X)) - 180.0 
end 
+0

'Engine.Yaw = math.deg(math.atan(-direction.Y,-direction.X))' –

回答

0

我不認爲有什麼可以做,以簡化數學邏輯。這裏有很少的冗餘,你可以利用。但是你可以因素成這樣的片段:

function atan_deg(y, x) 
    return (math.deg(math.atan(y, x))) 
end 

function hypotenuse(x, y) 
    return (math.sqrt(x^2 + y^2)) 
end 

function LookAt(target) 
    local origin = Vec3.New(Engine.ClientData.Origin) 
    local direction = origin - target 
    local X, Y, Z = direction.X, direction.Y, direction.Z 

    Engine.Pitch = atan_deg(Z, hypotenuse(X, Y)) 
    Engine.Yaw = atan_deg(Y, X) - 180.0 
end