2017-10-11 60 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