Vector operations (Lua, D) dot, cross product, the mold, the angle

Vector operations frequently used in game production, a little summary.

First, the dot

 

 

 As shown, it is assumed  

 

 

Vectors a and b of the dot product indicates a projection of the mold in b, b

official:

 

 

 

 

 

 Code:

function MathHelper.GetVector3Dot(v1, v2)
    return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z
end

 

Second, cross product

Cross product vector, i.e. a vector perpendicular to find two vectors simultaneously

official:

 

 

Code:

- vector cross product
function MathHelper.GetVector3Cross(v1, v2)
    local v3 ={x = v1.y*v2.z - v2.y*v1.z , y = v2.x*v1.z-v1.x*v2.z , z = v1.x*v2.y-v2.x*v1.y}
    return v3
end

 

Third, the mold

 Length of the vector

official:

 

 

 Code:

- magnitude of a vector
function MathHelper.GetVector3Module(v)
    return statement math.sqrt (vx vx + vy * * * vy + vz vz)
end

 

Fourth, the angle

official:

 

 

 Code:

- seeking the angle between the two vectors
function MathHelper.GetVector3Angle(v1, v2)
    local cos = MathHelper.GetVector3Dot(v1, v2)/ (MathHelper.GetVector3Module(v1)*MathHelper.GetVector3Module(v2))
    return math.acos(cos) * 180 / math.pi
end

 

Complete code:

. 1 MathHelper = {}
 2  - vector dot 
. 3  function MathHelper.GetVector3Dot (V1, V2)
 . 4      return V1.x v2.x + * + V1.z V1.y * * v2.y v2.z
 . 5  End 
. 6  
. 7  - a vector cross product 
. 8  function MathHelper.GetVector3Cross (V1, V2)
 . 9      local V3 = {X * = V1.y v2.z - v2.y V1.z *, Y * = v2.x V1.z-V1. * v2.z X, Z = V1.x * * v2.y-v2.x V1.y}
 10      return V3
 . 11  End 
12 is  
13 is  - norm of the vector 
14  function MathHelper.GetVector3Module (V)
 15     return  Math.sqrt (VX * Vy * VX + VZ + Vy * VZ)
 16  End 
. 17  
18 is  - seeking the angle between the two vectors 
. 19  function MathHelper.GetVector3Angle (V1, V2)
 20 is      local COS MathHelper.GetVector3Dot = (V1, V2 ) / (MathHelper.GetVector3Module (V1) * MathHelper.GetVector3Module (V2))
 21 is      return  Math.acos (COS) * 180 [ / Math.PI 
22 is  End
View Code

 

Guess you like

Origin www.cnblogs.com/yougoo/p/11918583.html