October 28, 2020, 06:17:11 AM

Author Topic: [bmx] 3D Math Functions by AndrewT [ 1+ years ago ]  (Read 1542 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] 3D Math Functions by AndrewT [ 1+ years ago ]
« on: June 29, 2017, 12:28:43 AM »
Title : 3D Math Functions
Author : AndrewT
Posted : 1+ years ago

Description : As of OpenGL 3.0, all matrix functions are deprecated. This means that you must handle all of your transformations by yourself. This library is designed to help with that.

Edited, 6/16/10, fixed some bugs and contained it within a GLMath UDT, because I plan on adding some vector functions soon. I also added an 'ExtractNormalMatrix()' to assist with GLSL.

Edited, 6/17/10, added PrintMatrix(), added most of the vector functions, will add the rest later, plus hopefully quaternions.


Code :
Code: BlitzMax
  1. Rem
  2. 3D Math Library
  3.  
  4. By Andrew Thorwall (AndrewT)
  5.  
  6. -----------------------------------------------------------------------
  7. INFORMATION
  8. -----------------------------------------------------------------------
  9.  
  10. As of OpenGL 3.0, the built-in matrix functions are deprecated. This means you must handle
  11. matrices yourself. This library is designed to help you do that.
  12.  
  13. Note that there is no Matrix UDT. Matrices must be defined as 16-element Float arrays, i.e. Local MyMatrix:Float[16].
  14.  
  15. The matrices are serialized in column-major format, i.e
  16.  
  17.    1   5   9   13
  18.  
  19.    2   6   10  14
  20.  
  21.    3   7   11  15
  22.  
  23.    4   8   12  16
  24.  
  25. -----------------------------------------------------------------------
  26. USING IT WITH OPENGL
  27. -----------------------------------------------------------------------
  28.  
  29. As previously mentioned, ALL of the matrix functinos are deprecated.
  30. This includes glLoadMatrix{fd}(). In addition to this, the fixed-function
  31. pipeline is also deprecated. This means you must use vertex shaders to transform your
  32. vertices. In order to let your shader have access to your transformation
  33. and projection matrices, you must declare appropriate uniforms in your shader
  34. and set them from your OpenGL program with glUniformMatrix4fv(). The first paremeter
  35. is the location of the uniform in your shader (get it with glGetUniformLocation),
  36. the second parameter is the number of matrices you're modifying (usually one,
  37. unless you're modifying an array of matrices), the third parameter specifies whether or not
  38. you want OpenGL to use the transpose of your matrix (only use this if you've used row-major matrices
  39. for some reason, set it to false if you're using this library), and a pointer to the first
  40. value of your matrix.
  41.  
  42. -----------------------------------------------------------------------
  43. FUNCTION LIST
  44. -----------------------------------------------------------------------
  45.  
  46. IdentityMatrix()
  47.  
  48. SetMatrix()
  49. GetMatrix()
  50.  
  51. CopyMatrix()
  52. AddMatrix()
  53. SubtractMatrix()
  54. MultiplyMatrix()
  55.  
  56. TranslationMatrix()
  57. TranslateMatrix()
  58.  
  59. XRotationMatrix()
  60. YRotationMatrix()
  61. ZRotationMatrix()
  62. XRotateMatrix()
  63. YRotateMatrix()
  64. ZRotateMatrix()
  65.  
  66. ScalingMatrix()
  67. XYZScalingMatrix()
  68. ScaleMatrix()
  69. XYZScaleMatrix()
  70.  
  71. RHPerspectiveProjectionMatrix()
  72.  
  73. ExtractNormalMatrix()
  74.  
  75. PrintMatrix()
  76.  
  77. NewVector2()
  78. NewVector3()
  79. NewVector4()
  80.  
  81. AddVector()
  82. AddVectorScalar()
  83. SubtractVector()
  84. SubtractVectorScalar()
  85. MultiplyVector()
  86. MultiplyVectorScalar()
  87. DivideVector()
  88. DivideVectorScalar()
  89.  
  90. VectorDotProduct()
  91. VectorCrossProduct()
  92.  
  93. VectorMagnitude()
  94. NormalizeVector()
  95.  
  96. ProjectVector()
  97.  
  98. MultiplyMatrixVector()
  99.  
  100.  
  101. ----------------------------------------------------------------------------------
  102. Diffference between MatTranslation/Rotation/Scaling and MatTranslate/Rotate/Scale
  103. ----------------------------------------------------------------------------------
  104.  
  105. Some of the functions are similarly named and it may be difficult To know which does what.
  106. MatTranslationMatrix(), Mat*RotationMatrix(), And MatScalingMatrix() build transformation matrices according
  107. To the parameters you provide. However, MatTranslateMatrix(), Mat*RotateMatrix(), And MatScaleMatrix() all operate
  108. on existing matrices--they first build the correct matrix, Then multiply the existing matrix by it. These functions are
  109. similar To what OpenGL does--you only have transformation matrix (ModelView matrix) And you just perform different operations
  110. on it, whereas with the former three functions you have a different matrix returned from each Function, And you have To multiply
  111. them all together.
  112.  
  113. EndRem
  114.  
  115.  
  116. Type GLMath
  117.  
  118.         '********************************************************************************************
  119.         ' MATRICES
  120.         '********************************************************************************************
  121.         Method IdentityMatrix:Float[]()
  122.                 Local Mat:Float[16]
  123.                 Mat[0] = 1.0
  124.                 Mat[5] = 1.0
  125.                 Mat[10] = 1.0
  126.                 Mat[15] = 1.0
  127.                 Return Mat
  128.         EndMethod
  129.         'Sets an element.
  130.         Method SetMatrix(Mat:Float[], Row:Int, Column:Int, Val:Float)
  131.                 Mat[Column * 4 + Row] = Val
  132.         EndMethod
  133.        
  134.         'Gets an element.
  135.        
  136.         Method GetMatrix:Float(Mat:Float[], Row:Int, Column:Int)
  137.                 Return Mat[Column * 4 + Row]
  138.         EndMethod
  139.        
  140.         'Copies a matrix.
  141.        
  142.         Method CopyMatrix:Float[](Mat:Float[])
  143.                 Local Mat2:Float[16]
  144.                 MemCopy(Mat2, Mat, SizeOf(Mat))
  145.                 Return Mat2
  146.         EndMethod
  147.        
  148.         'Returns Mat1 + Mat2.
  149.        
  150.         Method AddMatrix:Float[](Mat1:Float[], Mat2:Float[])
  151.                 Local MatResult:Float[16]
  152.                 For Local I:Int = 0 To 15
  153.                         MatResult[I] = Mat1[I] + Mat2[I]
  154.                 Next
  155.                 Return MatResult
  156.         EndMethod
  157.        
  158.         'Returns Mat1 - Mat2.
  159.        
  160.         Method SubtractMatrix:Float[](Mat1:Float[], Mat2:Float[])
  161.                 Local MatResult:Float[16]
  162.                 For Local I:Int = 0 To 15
  163.                         MatResult[I] = Mat1[I] - Mat2[I]
  164.                 Next
  165.                 Return MatResult
  166.         EndMethod
  167.        
  168.         'Returns Mat1 * Mat2.
  169.        
  170.         Method MultiplyMatrix:Float[](Mat1:Float[], Mat2:Float[])
  171.                 Local MatResult:Float[16]
  172.                 For Local X:Int = 0 To 3
  173.                         For Local Y:Int = 0 To 3
  174.                                 For Local I:Int = 0 To 3
  175.                                         MatResult[X * 4 + Y] :+ Mat1[I * 4 + Y] * Mat2[X * 4 + I]
  176.                                 Next
  177.                         Next
  178.                 Next
  179.                 Return MatResult
  180.         EndMethod
  181.        
  182.         'Builds a translation matrix.
  183.        
  184.         Method TranslationMatrix:Float[](X:Float, Y:Float, Z:Float)
  185.                 Local MatResult:Float[16]
  186.                 MatResult = IdentityMatrix()
  187.                 MatResult[12] = X
  188.                 MatResult[13] = Y
  189.                 MatResult[14] = Z
  190.                 Return MatResult
  191.         EndMethod
  192.        
  193.         'Translates the matrix.
  194.        
  195.         Method TranslateMatrix(Mat:Float[] Var, X:Float, Y:Float, Z:Float)
  196.                 Local Mat2:Float[16]
  197.                 Mat2 = TranslationMatrix(X, Y, Z)
  198.                 Mat = MultiplyMatrix(Mat, Mat2)
  199.         EndMethod
  200.        
  201.         'Builds an x-axis rotation matrix.
  202.        
  203.         Method XRotationMatrix:Float[](Ang:Float)
  204.                 Local MatResult:Float[16]
  205.                 MatResult = IdentityMatrix()
  206.                 SetMatrix(MatResult, 1, 1, Cos(Ang))
  207.                 SetMatrix(MatResult, 2, 2, Cos(Ang))
  208.                 SetMatrix(MatResult, 2, 1, -Sin(Ang))
  209.                 SetMatrix(MatResult, 1, 2, Sin(Ang))
  210.                 Return MatResult
  211.         EndMethod
  212.        
  213.         'Build a y-axis rotation matrix.
  214.        
  215.         Method YRotationMatrix:Float[](Ang:Float)
  216.                 Local MatResult:Float[16]
  217.                 MatResult = IdentityMatrix()
  218.                 SetMatrix(MatResult, 0, 0, Cos(Ang))
  219.                 SetMatrix(MatResult, 2, 2, Cos(Ang))
  220.                 SetMatrix(MatResult, 0, 2, -Sin(Ang))
  221.                 SetMatrix(MatResult, 2, 0, Sin(Ang))
  222.                 Return MatResult
  223.         EndMethod      
  224.        
  225.         'Builds a z-axis rotation matrix.
  226.        
  227.         Method ZRotationMatrix:Float[](Ang:Float)
  228.                 Local MatResult:Float[16]
  229.                 MatResult = IdentityMatrix()
  230.                 SetMatrix(MatResult, 0, 0, Cos(Ang))
  231.                 SetMatrix(MatResult, 1, 1, Cos(Ang))
  232.                 SetMatrix(MatResult, 1, 0, -Sin(Ang))
  233.                 SetMatrix(MatResult, 0, 1, Sin(Ang))
  234.                 Return MatResult
  235.         EndMethod
  236.        
  237.         'Rotates the matrix on the x-axis.
  238.        
  239.         Method XRotateMatrix(Mat:Float[] Var, Ang:Float)
  240.                 Local Mat2:Float[16]
  241.                 Mat2 = XRotationMatrix(Ang)
  242.                 Mat = MultiplyMatrix(Mat, Mat2)
  243.         EndMethod
  244.        
  245.         'Rotates the matrix on the y-axis.
  246.        
  247.         Method YRotateMatrix(Mat:Float[] Var, Ang:Float)
  248.                 Local Rot:Float[16]
  249.                 Rot = YRotationMatrix(Ang)
  250.                 Mat = MultiplyMatrix(Mat, Rot)
  251.         EndMethod
  252.        
  253.         'Rotates the matrix on the z-axis.
  254.        
  255.         Method ZRotateMatrix(Mat:Float[] Var, Ang:Float)
  256.                 Local Mat2:Float[16]
  257.                 Mat2 = ZRotationMatrix(Ang)
  258.                 Mat = MultiplyMatrix(Mat, Mat2)
  259.         EndMethod
  260.        
  261.         'Builds a scaling matrix with the same scale for each axis.
  262.        
  263.         Method ScalingMatrix:Float[](S:Float)
  264.                 Local MatResult:Float[16]
  265.                 MatResult = IdentityMatrix()
  266.                 SetMatrix(MatResult, 0, 0, S)
  267.                 SetMatrix(MatResult, 1, 1, S)
  268.                 SetMatrix(MatResult, 2, 2, S)
  269.                 Return MatResult
  270.         EndMethod
  271.        
  272.         'Builds a scaling matrix, with a different scale for each axis.
  273.        
  274.         Method XYZScalingMatrix:Float[](SX:Float, SY:Float, SZ:Float)
  275.                 Local MatResult:Float[16]
  276.                 MatResult = IdentityMatrix()
  277.                 SetMatrix(MatResult, 0, 0, SX)
  278.                 SetMatrix(MatResult, 1, 1, SY)
  279.                 SetMatrix(MatResult, 2, 2, SZ)
  280.                 Return MatResult
  281.         EndMethod
  282.        
  283.         'Scales a matrix by the same amount on each axis.
  284.        
  285.         Method ScaleMatrix(Mat:Float[] Var, S:Float)
  286.                 Local Mat2:Float[16]
  287.                 Mat2 = ScalingMatrix(S)
  288.                 Mat = MultiplyMatrix(Mat, Mat2)
  289.         EndMethod
  290.        
  291.         'Scales a matrix, with the option of choosing different scales for different axes.
  292.        
  293.         Method XYZScaleMatrix(Mat:Float[] Var, SX:Float, SY:Float, SZ:Float)
  294.                 Local Mat2:Float[16]
  295.                 Mat2 = XYZScalingMatrix(SX, SY, SZ)
  296.                 Mat = MultiplyMatrix(Mat, Mat2)
  297.         EndMethod
  298.        
  299.         'Builds a right-handed perspective projection matrix.
  300.        
  301.         Method RHPerspectiveProjectionMatrix:Float[](FOV:Float, Aspect:Float, ZNear:Float, ZFar:Float)
  302.                 Local MatResult:Float[16]
  303.                
  304.                 Local Top:Float = ZNear * Tan(FOV / 2.0)
  305.                 Local Bottom:Float = -Top
  306.                 Local _Right:Float = Top * Aspect
  307.                 Local _Left:Float = -_Right
  308.        
  309.                 MatResult[0]  = (2 * ZNear) / (_Right - _Left)
  310.                 MatResult[1]  = 0
  311.                 MatResult[2]  = 0
  312.                 MatResult[3]  = 0
  313.                
  314.                 MatResult[4]  = 0
  315.                 MatResult[5]  = (2 * ZNear) / (Top - Bottom)
  316.                 MatResult[6]  = 0
  317.                 MatResult[7]  = 0
  318.                
  319.                 MatResult[8]  = (_Right + _Left) / (_Right - _Left)
  320.                 MatResult[9]  = (Top + Bottom) / (Top - Bottom)
  321.                 MatResult[10] = -(ZFar + ZNear) / (ZFar - ZNear)
  322.                 MatResult[11] = -1
  323.                
  324.                 MatResult[12] = 0
  325.                 MatResult[13] = 0
  326.                 MatResult[14] = -(2 * ZFar * ZNear) / (ZFar - ZNear)
  327.                 MatResult[15] = 0
  328.        
  329.                 Return MatResult
  330.         EndMethod
  331.        
  332.         'Extracts the normal matrix from a modelview matrix, i.e. the transpose of the inverse of the upper-left 3x3
  333.         'sub-matrix of our modelview matrix.
  334.        
  335.         Method ExtractNormalMatrix:Float[](Mat:Float[])
  336.                 'Our normal matrix. It's 3x3 instead of 4x4.
  337.                 Local NM:Float[9]
  338.                
  339.                 'Now we set its values to the upper-left corner of our modelview matrix.
  340.                 NM[0] = Mat[0]
  341.                 NM[1] = Mat[1]
  342.                 NM[2] = Mat[2]
  343.                
  344.                 NM[3] = Mat[4]
  345.                 NM[4] = Mat[5]
  346.                 NM[5] = Mat[6]
  347.                
  348.                 NM[6] = Mat[8]
  349.                 NM[7] = Mat[9]
  350.                 NM[8] = Mat[10]
  351.                
  352.                 'Now we find the determinant.
  353.                 Local Det:Float = ..
  354.                         NM[0] * (NM[4] * NM[8] - NM[5] * NM[7]) - ..
  355.                         NM[3] * (NM[1] * NM[8] - NM[2]* NM[7]) + ..
  356.                         NM[6] * (NM[1] * NM[5] - NM[2] * NM[4])
  357.                        
  358.                 'Recipricate.
  359.                 Local RecDet:Float = 1 / Det
  360.                
  361.                 'Now we're going to do two steps in one. We are going
  362.                 'calculate the adjugate matrix and multiply it by the reciprocal
  363.                 'of the determinant in the same step.
  364.                 Local Adj:Float[9]
  365.                 Adj[0] = (NM[4] * NM[8] - NM[5] * NM[7]) * RecDet
  366.                 Adj[1] = (NM[1] * NM[8] - NM[2] * NM[7]) * RecDet
  367.                 Adj[2] = (NM[1] * NM[5] - NM[2] * NM[4]) * RecDet
  368.                
  369.                 Adj[3] = (NM[3] * NM[8] - NM[5] * NM[6]) * RecDet
  370.                 Adj[4] = (NM[0] * NM[8] - NM[2] * NM[6]) * RecDet
  371.                 Adj[5] = (NM[0] * NM[5] - NM[2] * NM[3]) * RecDet
  372.                
  373.                 Adj[6] = (NM[3] * NM[7] - NM[4] * NM[6]) * RecDet
  374.                 Adj[7] = (NM[0] * NM[7] - NM[1] * NM[6]) * RecDet
  375.                 Adj[8] = (NM[0] * NM[4] - NM[1] * NM[3]) * RecDet
  376.                
  377.                 'Now we return it.
  378.                 Return Adj
  379.         EndMethod
  380.        
  381.         Method PrintMatrix(Mat:Float[])
  382.                 Local MatStr:String
  383.                 For Local I:Int = 0 To 15
  384.                         If I Mod 4 = 0
  385.                                 MatStr :+ Chr(10)
  386.                         EndIf
  387.                         MatStr :+ Mat[I]
  388.                         If I Mod 4 <> 3
  389.                                 MatStr :+ ", "
  390.                         EndIf
  391.                 Next
  392.                 Print MatStr
  393.         EndMethod
  394.         '----------------------------------------------------------------------------------------------------------------------------
  395.         'VECTORS
  396.         '----------------------------------------------------------------------------------------------------------------------------
  397.        
  398.         'Creates a 2-dimensional vector.
  399.        
  400.         Method NewVector2:Float[](X:Float, Y:Float)
  401.                 Local Vec:Float[2]
  402.                 Vec[0] = X
  403.                 Vec[1] = Y
  404.                 Return Vec
  405.         EndMethod
  406.        
  407.         'Creates a 3-dimensional vector.
  408.        
  409.         Method NewVector3:Float[](X:Float, Y:Float, Z:Float)
  410.                 Local Vec:Float[3]
  411.                 Vec[0] = X
  412.                 Vec[1] = Y
  413.                 Vec[2] = Z
  414.                 Return Vec
  415.         EndMethod
  416.        
  417.         'Creates a 4-dimensional vector.
  418.        
  419.         Method NewVector4:Float[](X:Float, Y:Float, Z:Float, W:Float)
  420.                 Local Vec:Float[4]
  421.                 Vec[0] = X
  422.                 Vec[1] = Y
  423.                 Vec[2] = Z
  424.                 Vec[3] = W
  425.                 Return Vec
  426.         EndMethod
  427.        
  428.         'Adds two vectors together.
  429.        
  430.         Method AddVector:Float[](V1:Float[], V2:Float[])
  431.                 Local VecResult:Float[Len(V1)]
  432.                 For Local I:Int = 0 To Len(V1) - 1
  433.                         VecResult[I] = V1[I] + V2[I]
  434.                 Next
  435.                 Return VecResult
  436.         EndMethod
  437.        
  438.         'Adds a scalar.
  439.        
  440.         Method AddVectorScalar:Float[](V:Float[], S:Float)
  441.                 Local VecResult:Float[Len(V)]
  442.                 For Local I:Int = 0 To Len(V) - 1
  443.                         VecResult[I] = V[I] + S
  444.                 Next
  445.                 Return VecResult
  446.         EndMethod
  447.        
  448.         'Subtracts two vectors.
  449.        
  450.         Method SubtractVector:Float[](V1:Float[], V2:Float[])
  451.                 Local VecResult:Float[Len(V1)]
  452.                 For Local I:Int = 0 To Len(V1) - 1
  453.                         VecResult[I] = V1[I] - V2[I]
  454.                 Next
  455.                 Return VecResult
  456.         EndMethod
  457.        
  458.         'Subtracts a scalar.
  459.        
  460.         Method SubtractVectorScalar:Float[](V:Float[], S:Float)
  461.                 Local VecResult:Float[Len(V)]
  462.                 For Local I:Int = 0 To Len(V) - 1
  463.                         VecResult[I] = V[I] - S
  464.                 Next
  465.                 Return VecResult
  466.         EndMethod
  467.        
  468.         'Multiplies by a vector. NOTE: This function just multiplies straight across,
  469.         'if you want to do something different look at VectorDotProduct/CrossProduct.
  470.        
  471.         Method MultiplyVector:Float[](V1:Float[], V2:Float[])
  472.                 Local VecResult:Float[Len(V1)]
  473.                 For Local I:Int = 0 To Len(V1) - 1
  474.                         VecResult[I] = V1[I] * V2[I]
  475.                 Next
  476.                 Return VecResult
  477.         EndMethod
  478.        
  479.         'Mutliplies by a scalar.
  480.        
  481.         Method MultiplyVectorScalar:Float[](V:Float[], S:Float)
  482.                 Local VecResult:Float[Len(V)]
  483.                 For Local I:Int = 0 To Len(V) - 1
  484.                         VecResult[I] = V[I] * S
  485.                 Next
  486.                 Return VecResult
  487.         EndMethod
  488.        
  489.         'Divides by a vector. Just divides straight across.
  490.        
  491.         Method DivideVector:Float[](V1:Float[], V2:Float[])
  492.                 Local VecResult:Float[Len(V1)]
  493.                 For Local I:Int = 0 To Len(V1) - 1
  494.                         VecResult[I] = V1[I] / V2[I]
  495.                 Next
  496.                 Return VecResult
  497.         EndMethod
  498.        
  499.         'Divides by a scalar.
  500.        
  501.         Method DivideVectorScalar:Float[](V:Float[], S:Float)
  502.                 Local VecResult:Float[Len(V)]
  503.                 For Local I:Int = 0 To Len(V) - 1
  504.                         VecResult[I] = V[I] / S
  505.                 Next
  506.                 Return VecResult
  507.         EndMethod
  508.        
  509.         'Returns the dot product.
  510.        
  511.         Method VectorDotProduct:Float(V1:Float[], V2:Float[])
  512.                 Local Result:Float
  513.                 For Local I:Int = 0 To Len(V1) - 1
  514.                         Result :+ V1[I] * V2[I]
  515.                 Next
  516.                 Return Result
  517.         EndMethod
  518.        
  519.         'Returns cross product. Vector must be 3D.
  520.        
  521.         Method VectorCrossProduct:Float[](V1:Float[], V2:Float[])
  522.                 Local VecResult:Float[3]
  523.                 VecResult[0] = V1[1] * V2[2] - V2[1] * V1[2]
  524.                 VecResult[1] = V1[2] * V2[0] - V1[0] * V2[2]
  525.                 VecResult[2] = V1[0] * V2[1] - V1[1] * V2[0]
  526.                 Return VecResult
  527.         EndMethod
  528.        
  529.         'Returns the magnitude of a vector.
  530.        
  531.         Method VectorMagnitude:Float(V:Float[])
  532.                 Local SumSq:Float
  533.                 For Local I:Int = 0 To Len(V) - 1
  534.                         SumSq :+ V[I] * V[I]
  535.                 Next
  536.                 Return Sqr(SumSq)
  537.         EndMethod
  538.        
  539.         'Returns a normalized vector.
  540.        
  541.         Method NormalizeVector:Float[](V:Float[])
  542.                 Local VecResult:Float[Len(V)]
  543.                 Local M:Float = VectorMagnitude(V)
  544.                 For Local I:Int = 0 To Len(V) - 1
  545.                         VecResult[I] = V[I] / M
  546.                 Next
  547.                 Return VecResult
  548.         EndMethod
  549.        
  550.         'Projects vector A onto vector B.
  551.        
  552.         Method ProjectVector:Float[](V1:Float[], V2:Float[])
  553.                 Local VecResult:Float[Len(V1)]
  554.                 VecResult = MultiplyVectorScalar(V2, VectorDotProduct(V1, V2) / (VectorMagnitude(V2) ^ 2))
  555.                 Return VecResult
  556.         EndMethod
  557.        
  558.         'Multiplies a matrix by a vector and returns the resulting vector.
  559.         'The vector must be 4D.
  560.        
  561.         Method MultiplyMatrixVector:Float[](Mat:Float[], Vec:Float[])
  562.                 Local VecResult:Float[4]
  563.                
  564.                 For Local E:Int = 0 To 3
  565.                         For Local I:Int = 0 To 3
  566.                                 VecResult[E] :+ Mat[E + I * 4] * Vec[I]
  567.                         Next
  568.                 Next
  569.                 Return VecResult
  570.         EndMethod
  571.  
  572. EndType


Comments :


Kryzon(Posted 1+ years ago)

 <a href="http://en.wikipedia.org/wiki/Vector_projection#Vector_projection_2" target="_blank">Wikipedia enlightens[/url] that the projection method...
Code: [Select]
Method ProjectVector:Float[](V1:Float[], V2:Float[])
Local VecResult:Float[Len(V1)]
VecResult = MultiplyVectorScalar(V2, VectorDotProduct(V1, V2) / (VectorMagnitude(V2) ^ 2))
Return VecResult
EndMethod
...can be optimized with the following:
Code: [Select]
Method ProjectVector:Float[](V1:Float[], V2:Float[])
Local VecResult:Float[Len(V1)]
VecResult = MultiplyVectorScalar(V2, VectorDotProduct(V1, V2) / VectorDotProduct(V2, V2))
Return VecResult
EndMethod
A vector's squared magnitude is equal to the Dot product of this vector with itself. [/i]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal