Ooops
November 28, 2020, 02:38:33 AM

Author Topic: [bmx] 3D Math Lib - BMax by Chroma [ 1+ years ago ]  (Read 751 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] 3D Math Lib - BMax by Chroma [ 1+ years ago ]
« on: June 29, 2017, 12:28:41 AM »
Title : 3D Math Lib - BMax
Author : Chroma
Posted : 1+ years ago

Description : Easy to use 3d math lib for BMax.  Can do complex equations on a single line.  Very handy for converting complex maths from C/C++.
Each command has a description of what it does and examples of exactly how to use it.

Updated 02Dec05: Added some Quaternions
Updated 04Dec05: Added Matrices


Code :
Code: BlitzMax
  1. ' 3D Math Library v2.0
  2. ' BlitzMAX Edition
  3. ' by Chroma
  4.  
  5. ' You can do some lengthy equations with this Math Lib
  6. ' Just remember it does the maths in order of parenthesis
  7. ' So you have to write the equation accordingly (in reverse?)
  8. ' For example the equation: v=v1+v2*v3+v4
  9. ' It doesn't automatically do the multiplication first
  10. ' You have to engineer the equation manually like so:
  11. ' v = v1.Add( v4.Add( v2.Mul( v3 ) ) )
  12. ' Just be aware of the order they are calc'ed in
  13.  
  14. ' Here's a physics example:
  15. ' vAcceleration = vForces.DivS( Mass )
  16. ' vVelocity.AddTimeStep( vAcceleration, dt )
  17. ' vPosition.AddTimeStep( vVelocity, dt )
  18.  
  19. 'Test
  20. 'Graphics 800,600,32,85
  21. 'a:Vector = Vector.Create()
  22. 'b:Vector = Vector.Create(1,2,3)
  23. 'While Not KeyHit(KEY_ESCAPE)
  24. 'Cls
  25. 'a.AddTimeStep( b, .015)
  26. 'a.Show(5,5,"Test")
  27. 'Flip
  28. 'Wend
  29. 'End
  30.  
  31.  
  32. ' Constants
  33. Const Tol:Float = 0.0001
  34.  
  35. '-------------------------------------------
  36. '---- Vector Type and Functions/Methods ----
  37. '-------------------------------------------
  38. Type Vector
  39.  
  40.         Field x:Float
  41.         Field y:Float
  42.         Field z:Float
  43.        
  44.         ' Purpose: Create a New Vector
  45.         ' Returns: Vector
  46.         ' Example1: v1:Vector = New Vector
  47.         ' Example2: v1:Vector = Vector.Create()
  48.         ' Example3: v1:Vector = Vector.Create(1,2,3)
  49.         Function Create:Vector( x:Float = 0, y:Float = 0, z:Float = 0 )
  50.                 Local v:Vector = New Vector
  51.                 v.x = x
  52.                 v.y = y
  53.                 v.z = z
  54.                 Return v
  55.         End Function
  56.        
  57.         ' Purpose: Set a Vector with New XYZ Components
  58.         ' Returns: Nothing
  59.         ' Example: v1.Set(1,2,3)
  60.         ' Example: v1.Set() will set a vector to (0,0,0)
  61.         Method Set( newx:Float = 0, newy:Float = 0, newz:Float = 0 )
  62.                 x = newx
  63.                 y = newy
  64.                 z = newz
  65.         End Method
  66.        
  67.         ' Purpose: Add Two Vectors
  68.         ' Returns: Vector
  69.         ' Example1: v1=v1+v2 would be written v1 = v1.Add( v2 )
  70.         ' Example2: v1=v2+v3 would be written v1 = v2.Add( v3 )
  71.         Method Add:Vector( v:Vector )
  72.                 Local res:Vector = New Vector
  73.                 res.x = x + v.x
  74.                 res.y = y + v.y
  75.                 res.z = z + v.z
  76.                 Return res
  77.         End Method
  78.  
  79.         ' Purpose: Subtract Two Vectors
  80.         ' Returns: Vector
  81.         ' Example1: v1=v1-v2 would be written v1 = v1.Sub( v2 )
  82.         ' Example2: v1=v2-v3 would be written v1 = v2.Sub( v3 )
  83.         Method Sub:Vector( v:Vector )
  84.                 Local res:Vector = New Vector
  85.                 res.x = x - v.x
  86.                 res.y = y - v.y
  87.                 res.z = z - v.z
  88.                 Return res
  89.         End Method
  90.        
  91.         ' Purpose: Multiply Two Vectors
  92.         ' Returns: Vector
  93.         ' Example1: v1=v1*v2 would be written v1 = v1.Mul( v2 )
  94.         ' Example2: v1=v2*v3 would be written v1 = v2.Mul( v3 )
  95.         Method Mul:Vector( v:Vector )
  96.                 Local res:Vector = New Vector
  97.                 res.x = x * v.x
  98.                 res.y = y * v.y
  99.                 res.z = z * v.z
  100.                 Return res
  101.         End Method
  102.        
  103.         ' Purpose: Divide One Vector By Another
  104.         ' Returns: Vector
  105.         ' Example1: v1=v1/v2 would be written v1 = v1.Div( v2 )
  106.         ' Example2: v1=v2/v3 would be written v1 = v2.Div( v3 )
  107.         Method Div:Vector( v:Vector )
  108.                 Local res:Vector = New Vector
  109.                 res.x = x / v.x
  110.                 res.y = y / v.y
  111.                 res.z = z / v.z
  112.                 Return res
  113.         End Method
  114.        
  115.         ' Purpose: Add a Scalar to a Vector
  116.         ' Returns: Vector
  117.         ' Example1: v1=v1+1 would be written v1 = v1.AddS( 1 )
  118.         ' Example2: v1=v2+1 would be written v1 = v2.AddS( 1 )
  119.         Method AddS:Vector( s:Float )
  120.                 Local res:Vector = New Vector
  121.                 res.x = x + s
  122.                 res.y = y + s
  123.                 res.z = z + s
  124.                 Return res
  125.         End Method
  126.        
  127.         ' Purpose: Subtract a Scalar from a Vector
  128.         ' Returns: Vector
  129.         ' Example1: v1=v1-1 would be written v1 = v1.SubS( 1 )
  130.         ' Example2: v1=v2-1 would be written v1 = v2.SubS( 1 )
  131.         Method SubS:Vector( s:Float )
  132.                 Local res:Vector = New Vector
  133.                 res.x = x - s
  134.                 res.y = y - s
  135.                 res.z = z - s
  136.                 Return res
  137.         End Method
  138.  
  139.         ' Purpose: Multiply a Vector by a Scalar
  140.         ' Returns: Vector
  141.         ' Example1: v1=v1*2 would be written v1 = v1.MulS( 2 )
  142.         ' Example2: v1=v2*2 would be written v1 = v2.MulS( 2 )
  143.         Method MulS:Vector( s:Float )
  144.                 Local res:Vector = New Vector
  145.                 res.x = x * s
  146.                 res.y = y * s
  147.                 res.z = z * s
  148.                 Return res
  149.         End Method
  150.        
  151.         ' Purpose: Divide a Vector by a Scalar
  152.         ' Returns: Vector
  153.         ' Example1: v1=v1/2 would be written v1 = v1.DivS( 2 )
  154.         ' Example2: v1=v2/2 would be written v1 = v2.DivS( 2 )
  155.         Method DivS:Vector( s:Float )
  156.                 Local res:Vector = New Vector
  157.                 res.x = x / s
  158.                 res.y = y / s
  159.                 res.z = z / s
  160.                 Return res
  161.         End Method
  162.        
  163.         ' Purpose: Multiply a Vector by a Quaternion
  164.         ' Returns: Quaternion
  165.         ' Example: q1=v1*q1 would be written q1 = v1.MulQ ( q1 )       
  166.         Method MulQ:Quaternion( q:Quaternion )
  167.                 Local res:Quaternion = New Quaternion
  168.                 res.n = -(q.v.x*x + q.v.y*y + q.v.z*z)
  169.                 res.v.x = q.n*x + q.v.z*y - q.v.y*z
  170.                 res.v.y = q.n*y + q.v.x*z - q.v.z*x
  171.                 res.v.z = q.n*z + q.v.y*x - q.v.x*y
  172.                 Return res
  173.         End Method
  174.  
  175.         ' Purpose: Multiply a Vector by a Matrix
  176.         ' Returns: Vector
  177.         ' Example: v1=v1*m1 would be written v1 = v1.MulM( m1 )
  178.         Method MulM:Vector ( m:Matrix )
  179.                 Local res:Vector = New Vector  
  180.                 res.x = x*m.e11 + y*m.e21 + z*m.e31
  181.                 res.y = x*m.e12 + y*m.e22 + z*m.e32
  182.                 res.z = x*m.e13 + y*m.e23 + z*m.e33
  183.                 Return res
  184.         End Method
  185.  
  186.         ' Purpose: Calculate the Magnitude of a Vector
  187.         ' Returns: Float
  188.         ' Example: Local a:Float = v1.Magnitude()
  189.         Method Magnitude:Float()
  190.                 Return Sqr(x * x + y * y + z * z)
  191.         End Method
  192.  
  193.         ' Purpose: Normalize a Vector
  194.         ' Returns: Nothing - Directly Normalizes Target Vector
  195.         ' Example: v1.Normalize()
  196.         Method Normalize()
  197.                 Local mag:Float = Magnitude()
  198.                 If mag = 0.0 Then Set();Return
  199.                 x :/ mag
  200.                 y :/ mag
  201.                 z :/ mag
  202.                 If Abs( x ) < Tol x = 0.0
  203.                 If Abs( y ) < Tol y = 0.0
  204.                 If Abs( z ) < Tol z = 0.0
  205.         End Method
  206.        
  207.         ' Purpose: Calculate the Cross Product of Two Vectors
  208.         ' Returns: Vector
  209.         ' Example1: v1 = v1.CrossP( v2 )
  210.         ' Example2: v1 = v2.CrossP( v3 )
  211.         Method CrossP:Vector( v:Vector )
  212.                 Local res:Vector = New Vector
  213.                 res.x =  y * v.z  -  z * v.y
  214.                 res.y = -x * v.z  +  z * v.x
  215.                 res.z =  x * v.y  -  y * v.x
  216.                 Return res
  217.         EndMethod
  218.        
  219.         ' Purpose: Calculate the Dot Product Between Two Vectors
  220.         ' Returns: Float
  221.         ' Example: Local mydot:float = v1.DotP( v2 )
  222.         Method DotP:Float( v:Vector )
  223.                 Return x * v.x + y * v.y + z * v.z
  224.         EndMethod
  225.        
  226.         Method Inverse:Vector()
  227.                 Return Vector.Create( -x, -y, -z )
  228.         End Method
  229.        
  230.         ' Purpose: Add Two Vectors and Multiply by a DeltaTime
  231.         ' Returns: Nothing - Directly Changes the Target Vector
  232.         ' Example: vPos=vPos+vVel*dt would be vPos.AddTimeStep( vVel, dt )
  233.         Method AddTimeStep( v:Vector, time_step:Float )
  234.                 x :+ v.x * time_step
  235.                 y :+ v.y * time_step
  236.                 z :+ v.z * time_step
  237.         End Method
  238.        
  239.         ' Purpose: Show Vector Values for Debug Purposes
  240.         ' Returns: Nothing
  241.         ' Note: Graphics mode must be set for this command to work
  242.         ' Example: v1.Show( 5, 5, "vPosition" )
  243.         Method Show( xpos:Int = 5, ypos:Int = 5, label:String = "", vspc:Int = 12 )
  244.                 DrawText label + "_X: " + x, xpos, ypos
  245.                 DrawText label + "_Y: " + y, xpos, ypos + vspc
  246.                 DrawText label + "_Z: " + z, xpos, ypos + vspc * 2
  247.         End Method
  248.        
  249. End Type
  250.  
  251. ' Degrees To Radians Conversion
  252. Function DegreesToRadians:Float(deg:Float)
  253.         Return deg * Pi / 180.0
  254. End Function
  255.  
  256. ' Radians To Degrees Conversion
  257. Function RadiansToDegrees:Float(rad:Float)
  258.         Return rad * 180.0 / Pi
  259. End Function
  260.  
  261.  
  262. '-------------------------------------------
  263. '-- Quaternion Type and Functions/Methods --
  264. '-------------------------------------------
  265. Type Quaternion
  266.        
  267.         Field n:Float
  268.         Field v:Vector = New Vector
  269.        
  270.         ' Purpose: Create a New Quaternion
  271.         ' Returns: Quaternion
  272.         ' Example1: q1:Quaternion = New Quaternion
  273.         ' Example2: q1:Quaternion = Quaternion.Create( 1, 2, 3, 4 )
  274.         ' Example3: q1:Quaternion = Quaternion.Create()
  275.         Function Create:Quaternion( n:Float = 0, x:Float = 0, y:Float = 0, z:Float = 0 )
  276.                 Local q:Quaternion = New Quaternion
  277.                 q.n = n
  278.                 q.v.x = x
  279.                 q.v.y = y
  280.                 q.v.z = z
  281.                 Return q
  282.         End Function
  283.        
  284.         ' Purpose: Add Two Quaternions
  285.         ' Returns: Quaternion
  286.         ' Example1: q1=q1+q2 would be q1 = q1.Add( q2 )
  287.         ' Example2: q1=q2+q3 would be q1 = q2.Add( q3 )
  288.         Method Add:Quaternion( q:Quaternion )
  289.                 Local res:Quaternion = New Quaternion
  290.                 res.n = n + q.n
  291.                 res.v.x = v.x + q.v.x
  292.                 res.v.y = v.y + q.v.y
  293.                 res.v.z = v.z + q.v.z
  294.                 Return res
  295.         End Method
  296.        
  297.         ' Purpose: Subtract Two Quaternions
  298.         ' Returns: Quaternion
  299.         ' Example1: q1=q1-q2 would be q1 = q1.Sub( q2 )
  300.         ' Example2: q1=q2-q3 would be q1 = q2.Sub( q3 )
  301.         Method Sub:Quaternion( q:Quaternion )
  302.                 Local res:Quaternion = New Quaternion
  303.                 res.n = n - q.n
  304.                 res.v.x = v.x - q.v.x
  305.                 res.v.y = v.y - q.v.y
  306.                 res.v.z = v.z - q.v.z
  307.                 Return res
  308.         End Method
  309.        
  310.         ' Purpose: Multiply Two Quaternions
  311.         ' Returns: Quaternion
  312.         ' Example1: q1=q1*q2 would be q1 = q1.Mul( q2 )
  313.         ' Example2: q1=q2*q3 would be q1 = q2.Mul( q3 )
  314.         Method Mul:Quaternion( q:Quaternion )
  315.                 Local res:Quaternion = New Quaternion
  316.                 res.n = n*q.n - v.x*q.v.x - v.y*q.v.y - v.z*q.v.z
  317.                 res.v.x = n*q.v.x + v.x*q.n + v.y*q.v.z - v.z*q.v.y
  318.                 res.v.y = n*q.v.y + v.y*q.n + v.z*q.v.x - v.x*q.v.z
  319.                 res.v.z = n*q.v.y + v.y*q.n + v.z*q.v.x - v.x*q.v.z
  320.                 Return res
  321.         End Method
  322.        
  323.         ' Purpose: Multiply a Quaternion by a Scalar
  324.         ' Returns: Quaternion
  325.         ' Example1: q1=q1*2 would be q1 = q1.MulS( 2 )
  326.         ' Example2: q1=q2*2 would be q1 = q2.MulS( 2 )
  327.         Method MulS:Quaternion( s:Float )
  328.                 Local res:Quaternion = New Quaternion
  329.                 res.n = n * s
  330.                 res.v.x = v.x * s
  331.                 res.v.y = v.y * s
  332.                 res.v.z = v.z * s
  333.                 Return res
  334.         End Method
  335.  
  336.         ' Purpose: Divide a Quaternion by a Scalar
  337.         ' Returns: Quaternion
  338.         ' Example1: q1=q1/2 would be q1 = q1.DivS( 2 )
  339.         ' Example2: q1=q2/2 would be q1 = q2.DivS( 2 )
  340.         Method DivS:Quaternion( s:Float )
  341.                 Local res:Quaternion = New Quaternion
  342.                 res.n = n / s
  343.                 res.v.x = v.x / s
  344.                 res.v.y = v.y / s
  345.                 res.v.z = v.z / s
  346.                 Return res
  347.         End Method
  348.        
  349.         ' Purpose: Multiply a Quaternion by a Vector
  350.         ' Returns: Quaternion
  351.         ' Example1: q1=q1*v1 would be written q1 = q1.MulV( v1 )
  352.         Method MulV:Quaternion( v:Vector )
  353.                 res:Quaternion = New Quaternion
  354.                 res.n = -(self.v.x*v.x + self.v.y*v.y + self.v.z*v.z)
  355.                 res.v.x = self.n*v.x + self.v.y*v.z - self.v.z*v.y
  356.                 res.v.y = self.n*v.y + self.v.z*v.x - self.v.x*v.z
  357.                 res.v.z = self.n*v.z + self.v.x*v.y - self.v.y*v.x
  358.                 Return res
  359.         End Method
  360.        
  361.         ' Purpose: Calculate the Magnitude of a Quaternion
  362.         ' Returns: Float
  363.         ' Example: Local qmag:float = q1.Magnitude()
  364.         Method Magnitude:Float( q:Quaternion )
  365.                 Return Float Sqr( n * n + v.x * v.x + v.y * v.y + v.z * v.z )
  366.         End Method
  367.  
  368.         ' Purpose: Gets the XYZ Component of a Quaternion
  369.         ' Returns: Vector
  370.         ' Example: v1 = q1.GetVector()
  371.         Method GetVector:Vector()
  372.                 Return Vector.Create(v.x, v.y, v.z)
  373.         End Method
  374.        
  375.         ' Purpose: Inverse a Quaternion
  376.         ' Returns: Quaternion
  377.         ' Example1: q1=~q1 would be q1 = q1.Inverse()
  378.         ' Example2: q1=~q2 would be q1 = q2.Inverse()
  379.         Method Inverse:Quaternion()
  380.                 Return Quaternion.Create(n, -v.x, -v.y, -v.z)
  381.         End Method
  382.        
  383.         ' Purpose: Show Quaternion Values for Debug Purposes
  384.         ' Returns: Nothing
  385.         ' Note: Graphics mode must be set for this command to work
  386.         ' Example: q1.Show( 5, 5, "qAngularVelocity" )
  387.         Method Show( xpos:Int, ypos:Int, label:String = "", vspc:Int = 12 )
  388.                 DrawText n, xpos, ypos
  389.                 DrawText v.x, xpos, ypos + vspc
  390.                 DrawText v.y, xpos, ypos + vspc * 2
  391.                 DrawText v.z, xpos, ypos + vspc * 3
  392.         End Method
  393.  
  394.         ' Purpose: Used in Physics
  395.         ' Returns: Quaternion
  396.         ' Example: q1 = q1.QRotate( q2 )
  397.         Method QRotate:Quaternion( q:Quaternion )
  398.                 ' q1*q2*(~q1)
  399.                 Local t:Quaternion = New Quaternion
  400.                 t = self.Mul( q )
  401.                 Return t.Mul( self.Inverse() )
  402.         End Method
  403.  
  404.         ' Purpose: Used in Physics
  405.         ' Returns: Vector
  406.         ' Example: v1 = q1.QVRotate( v2 )
  407.         Method QVRotate:Vector( v )
  408.                 Local t:Quaternion = New Quaternion
  409.                 ' t = q*v*(~q);
  410.                 t = self.MulV( v )
  411.                 t = t.Mul( self.Inverse() )
  412.                 Return t.GetVector()
  413.         End Method
  414.  
  415. End Type
  416.  
  417.  
  418. '-------------------------------------------
  419. '---- Matrix Type and Functions/Methods ----
  420. '-------------------------------------------
  421.  
  422. Type Matrix
  423.  
  424.         Field e11:Float = 0, e12:Float = 0, e13:Float = 0
  425.         Field e21:Float = 0, e22:Float = 0, e23:Float = 0
  426.         Field e31:Float = 0, e32:Float = 0, e33:Float = 0
  427.  
  428.         Function Create:Matrix(e11#=0,e12#=0,e13#=0,e21#=0,e22#=0,e23#=0,e31#=0,e32#=0,e33#=0)
  429.                 Local m:Matrix = New Matrix
  430.                 m.e11 = e11
  431.                 m.e12 = e12
  432.                 m.e13 = e13
  433.                 m.e21 = e21
  434.                 m.e22 = e22
  435.                 m.e23 = e23
  436.                 m.e31 = e31
  437.                 m.e32 = e32
  438.                 m.e33 = e33
  439.                 Return m
  440.         End Function
  441.  
  442.         Method Det:Float()
  443.                 Return e11*e22*e33-e11*e32*e23+e21*e32*e13-e21*e12*e33+e31*e12*e23-e31*e22*e13
  444.         End Method
  445.  
  446.         Method Transpose:Matrix()
  447.                 Return Matrix.Create(e11,e21,e31,e12,e22,e32,e13,e23,e33)
  448.         End Method
  449.                
  450.         Method  Inverse:Matrix()
  451.                 Local dd:Float = e11*e22*e33-e11*e32*e23+e21*e32*e13-e21*e12*e33+e31*e12*e23-e31*e22*e13
  452.                 If dd = 0 Then dd = 1
  453.                 Local a:Float = (e22*e33-e23*e32)/dd
  454.                 Local b:Float = -(e12*e33-e13*e32)/dd
  455.                 Local c:Float = (e12*e23-e13*e22)/dd
  456.                 Local d:Float = -(e21*e33-e23*e31)/dd
  457.                 Local e:Float = (e11*e33-e13*e31)/dd
  458.                 Local f:Float = -(e11*e23-e13*e21)/dd
  459.                 Local g:Float = (e21*e32-e22*e31)/dd
  460.                 Local h:Float = -(e11*e32-e12*e31)/dd
  461.                 Local i:Float = (e11*e22-e12*e21)/dd
  462.                 Return Matrix.Create(a,b,c,d,e,f,g,h,i)
  463.         End Method
  464.                
  465.         Method Add:Matrix( m:Matrix )
  466.                 Local res:Matrix = New Matrix
  467.                 res.e11 = e11 + m.e11
  468.                 res.e12 = e12 + m.e12
  469.                 res.e13 = e13 + m.e13
  470.                 res.e21 = e21 + m.e21
  471.                 res.e22 = e22 + m.e22
  472.                 res.e23 = e23 + m.e23
  473.                 res.e31 = e31 + m.e31
  474.                 res.e32 = e32 + m.e32
  475.                 res.e33 = e33 + m.e33
  476.                 Return res
  477.         End Method
  478.  
  479.         Method Sub:Matrix( m:Matrix )
  480.                 Local res:Matrix = New Matrix
  481.                 res.e11 = e11 - m.e11
  482.                 res.e12 = e12 - m.e12
  483.                 res.e13 = e13 - m.e13
  484.                 res.e21 = e21 - m.e21
  485.                 res.e22 = e22 - m.e22
  486.                 res.e23 = e23 - m.e23
  487.                 res.e31 = e31 - m.e31
  488.                 res.e32 = e32 - m.e32
  489.                 res.e33 = e33 - m.e33
  490.                 Return res
  491.         End Method
  492.  
  493.         Method Mul:Matrix( m:Matrix )
  494.                 Local res:Matrix = New Matrix
  495.                 res.e11 = e11*m.e11 + e12*m.e21 + e13*m.e31
  496.                 res.e12 = e11*m.e12 + e12*m.e22 + e13*m.e32
  497.                 res.e13 = e11*m.e13 + e12*m.e23 + e13*m.e33
  498.                 res.e21 = e21*m.e11 + e22*m.e21 + e23*m.e31
  499.                 res.e22 = e21*m.e12 + e22*m.e22 + e23*m.e32
  500.                 res.e23 = e21*m.e13 + e22*m.e23 + e23*m.e33
  501.                 res.e31 = e31*m.e11 + e32*m.e21 + e33*m.e31
  502.                 res.e32 = e31*m.e12 + e32*m.e22 + e33*m.e32
  503.                 res.e33 = e31*m.e13 + e32*m.e23 + e33*m.e33
  504.                 Return res
  505.         End Method
  506.  
  507.         Method MulS:Matrix( s:Float )
  508.                 Local res:Matrix = New Matrix
  509.                 res.e11 = e11 * s
  510.                 res.e12 = e12 * s
  511.                 res.e13 = e13 * s
  512.                 res.e21 = e21 * s
  513.                 res.e22 = e22 * s
  514.                 res.e23 = e23 * s
  515.                 res.e31 = e31 * s
  516.                 res.e32 = e32 * s
  517.                 res.e33 = e33 * s
  518.                 Return res
  519.         End Method
  520.  
  521.         Method DivS:Matrix( s:Float )
  522.                 Local res:Matrix = New Matrix
  523.                 res.e11 = e11 * s
  524.                 res.e12 = e12 * s
  525.                 res.e13 = e13 * s
  526.                 res.e21 = e21 * s
  527.                 res.e22 = e22 * s
  528.                 res.e23 = e23 * s
  529.                 res.e31 = e31 * s
  530.                 res.e32 = e32 * s
  531.                 res.e33 = e33 * s
  532.                 Return res
  533.         End Method
  534.  
  535.         ' Purpose: Multiply a Matrix by a Vector and Return a Vector Result
  536.         ' Example: v1=m1*v1 would be v1=m1.MulV( v1 )
  537.         Method MulV:Vector( v:Vector )
  538.                 Local res:Vector = New Vector
  539.                 res.x = e11*v.x + e12*v.y + e13*v.z
  540.                 res.y = e21*v.x + e22*v.y + e23*v.z
  541.                 res.z = e31*v.x + e32*v.y + e33*v.z
  542.                 Return res
  543.         End Method
  544.  
  545. End Type


Comments :


Arowx(Posted 1+ years ago)

 Just what I needed to move my project up to the next level. Thank You


Pineapple(Posted 1+ years ago)

 Very nice indeed! :)Great work me auld fruitDabz


Vertex(Posted 1+ years ago)

 Hmm I think, this isn't the best way.
Code: [Select]
Global A : Vector
Global B : Vector
Global R : Vector

A = New Vector
A.Set(1.0, 2.0, 3.0)

B = New Vector
B.Set(3.0, 2.0, 1.0)

R = A.Add(B)
R = R.MulS(3.0)
R is firstly created by A.Add(B) than is R deleted by R.MulS (3.0) and is new created. This slowing the speed because the internel ressourcehandling.Better:
Code: [Select]
Method Add( a:Vector, b:Vector )
Self.x = a.x + b.x
Self.y = a.y + b.y
Self.z = a.z + b.z
End Method
In practise:R.Add(A, B) ' R = A + BAlso I miss some methods like Slerp(Quanternion), this is a very important function to interpolate between to rotations and completly a plane type.Sorry, but there are certainly better libs :(cu olli


Chroma(Posted 1+ years ago)

 Hi Vertex...I'll check out the proposed changes. Thanks.


Gabriel(Posted 1+ years ago)

 Just checked this out because I was checking my maths on multiplying a vector by a quaternion. Your method returns a quaternion, which I don't understand. If you multiply a vector by a quaternion, surely you get a vector result? You are, after all, effectively rotating a vector. A rotated vector is still a vector. No?


Chroma(Posted 1+ years ago)

 No.


Bobysait(Posted 1+ years ago)

 
Code: [Select]
' Purpose: Multiply Two Quaternions
' Returns: Quaternion
' Example1: q1=q1*q2 would be q1 = q1.Mul( q2 )
' Example2: q1=q2*q3 would be q1 = q2.Mul( q3 )
Method Mul:Quaternion( q:Quaternion )
Local res:Quaternion = New Quaternion
res.n = n*q.n - v.x*q.v.x - v.y*q.v.y - v.z*q.v.z
res.v.x = n*q.v.x + v.x*q.n + v.y*q.v.z - v.z*q.v.y
res.v.y = n*q.v.y + v.y*q.n + v.z*q.v.x - v.x*q.v.z
res.v.z = n*q.v.y + v.y*q.n + v.z*q.v.x - v.x*q.v.z
Return res
End Method
Maybe I'm wrong, but it seems res.v.z is falsehere, v.y=v.z ...it should be something like
Code: [Select]
res.n   = n*q.n   - v.x*q.v.x - v.y*q.v.y - v.z*q.v.z
res.v.x = n*q.v.x + v.x*q.n   + v.y*q.v.z - v.z*q.v.y
res.v.y = n*q.v.y + v.x*q.v.z - v.y*q.n   + v.z*q.v.x
res.v.z = n*q.v.z - v.x*q.v.y + v.y*q.v.x + v.z*q.n
It's what I find when I developp Sibly's quaternion from its C++ lib
Code: [Select]
'+> Original TQuat.Create( w*q.w-v.dot(q.v) , (q.v.cross(v)).Add(q.v.Multiply(w)).Add(v.Multiply(q.w)) )
' According v:{ x,y,z }
QF:tquat=New tquat

v0:tvector=(q.v.cross(v))
v0.x = q.y*z-q.z*y;
v0.y = q.z*x-q.x*z;
v0.z = q.x*y-q.y*x;

v1:tvector=q.v.Multiply(w) v1 = q.x*w
q.y*w
q.z*w

v2:tvector=v0.Add(v1) v2 = q.y*z - q.z*y + q.x*w
q.z*x - q.x*z + q.y*w
q.x*y - q.y*x + q.z*w

v3:tvector=v.Multiply(q.w) v3 = x*q.w
y*q.w
z*q.w

v4:tvector=v2.Add(v3) v4 = q.y*z - q.z*y + q.x*w + x*q.w
q.z*x - q.x*z + q.y*w + y*q.w
q.x*y - q.y*x + q.z*w + z*q.w


QF.w = w*q.w - x*q.x - y*q.y - z*q.z
QF.x = w*q.x + x*q.w - y*q.z + z*q.y
QF.y = w*q.y + x*q.z + y*q.w - z*q.x
QF.z = w*q.z - x*q.y + y*q.x + z*q.w

Of course, I have maybe made a mistake...


Chroma(Posted 1+ years ago)

 Geez, you probably just nailed why I could never get my flight sim working 3 years ago... [/i]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal