Ooops
October 28, 2021, 11:21:08

Author Topic: [bb] Vector Math library by Beeps [ 1+ years ago ]  (Read 731 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Vector Math library by Beeps [ 1+ years ago ]
« on: June 29, 2017, 00:28:42 »
Title : Vector Math library
Author : Beeps
Posted : 1+ years ago

Description : A simple blitz include that gives you most of the vector maths functions. (Not fully tested)

Code :
Code: BlitzBasic
  1. ;vector maths lib by Beeps
  2.  
  3. ;used to normalize vector
  4. Const vector_tol#=0.0001
  5.  
  6. Type vector
  7.         Field x#
  8.         Field y#
  9.         Field z#
  10.         Field magnitude#
  11. End Type
  12.  
  13. Function vector_createVector.vector(vx#,vy#,vz#)
  14.         ;create a populated vector and return
  15.         vec.vector=New vector
  16.         vecx=vx
  17.         vecy=vy
  18.         vecz=vz
  19.         Return vec
  20. End Function
  21.  
  22. Function vector_calcMagnitude(vec.vector)
  23.         ;calc the magnitude of a vector and stor in vector
  24.         vecmagnitude=Sqr(vecx*vecx + vecy*vecy + vecz*vecz)
  25. End Function
  26.  
  27. Function vector_normalize(vec.vector)
  28.         ;normalize a vector so it's length = 1 and apply tolerance based on our constant tolerance value
  29.         m#=Sqr(Sqr(vecx*vecx + vecy*vecy + vecz*vecz) )
  30.         If m<= vector_tol Then m=1
  31.         vecx=vecx/m
  32.         vecy=vecy/m
  33.         vecz=vecz/m
  34.         If Abs(vecx)<vector_tol Then vecx=0
  35.         If Abs(vecy)<vector_tol Then vecy=0
  36.         If Abs(vecz)<vector_tol Then vecz=0
  37. End Function   
  38.  
  39. Function vector_reverse(vec.vector)
  40.         ;reverse a vector
  41.         vecx=-vecx
  42.         vecy=-vecy
  43.         vecz=-vecz
  44. End Function
  45.  
  46. Function vector_add.vector(vec1.vector,vec2.vector)
  47.         ;add two vectors together and return the resulting vector
  48.         result.vector=New vector
  49.        
  50.         resultx=vec1x+vec2x
  51.         resulty=vec1y+vec2y
  52.         resultz=vec1z+vec2z
  53.        
  54.         Return result
  55. End Function
  56.  
  57. Function vector_subtract.vector(vec1.vector,vec2.vector)
  58.         ;subtract vec1 from vec2 and return the resulting vector
  59.         result.vector=New vector
  60.        
  61.         resultx=vec1x-vec2x
  62.         resulty=vec1y-vec2y
  63.         resultz=vec1z-vec2z
  64.        
  65.         Return result
  66. End Function
  67.  
  68. Function vector_scalarMultiply.vector(vec1.vector,scale#)
  69.         ;scalar multiplication of a vector with result returned as new vector
  70.         ;used to scale a vector by 'scale'
  71.         result.vector=New vector
  72.         resultx=vec1x*scale
  73.         resulty=vec1y*scale
  74.         resultz=vec1z*scale
  75.         Return result
  76. End Function
  77.  
  78. Function vector_scalarDivision.vector(vec1.vector,scale#)
  79.         ;scalar division of a vector with result returned as new vector
  80.         ;used to scale a vector
  81.         result.vector=New vector
  82.         resultx=vec1x/scale
  83.         resulty=vec1y/scale
  84.         resultz=vec1z/scale
  85.         Return result
  86. End Function
  87.  
  88. Function vector_conjugate.vector(vec1.vector)
  89.         ;conjugate operator takes the negative of each vector component
  90.         ;can be used when subtracting one vector from another or for
  91.         ;reversing the direction of a vector.
  92.         ;applying conjugate is the same as reversing a vector
  93.         ;returns a new vector
  94.         result.vector = New vector
  95.         resultx=-vec1x
  96.         resulty=-vec1y
  97.         resultz=-vec1z
  98.         Return result
  99. End Function
  100.  
  101. Function vector_crossProduct.vector(vec1.vector,vec2.vector)
  102.         ;takes vec1 and vec2 and returns the cross product vec1 X vec2
  103.         ;the cross product is a vector perpendicular to both vec1 and vec2
  104.         ;this is the normal of 2 vectors
  105.         result.vector=New vector
  106.        
  107.         resultx=(vec1y*vec2z) - (vec1z*vec2y)
  108.         resulty=(vec1z*vec2x) - (vec1x*vec2z)
  109.         resultz=(vec1x*vec2y) - (vec1y*vec2x)
  110.  
  111.         Return result
  112. End Function
  113.  
  114. Function vector_dotProduct#(vec1.vector,vec2.vector)
  115.         ;calculate and return the dot product of 2 vectors (distance)
  116.         result#=(vec1x*vec2x)+(vec1y*vec2y)+(vec1z*vec2z)
  117.         Return result
  118. End Function
  119.  
  120. Function vector_tripleScalarProduct#(vec1.vector,vec2.vector,vec3.vector)
  121.         ;calculate the triple scalar function and return it
  122.         result#=          vec1x * ( (vec2y*vec3z ) - (vec2z * vec3y) )
  123.         result=result + ( vec1y * ( (-vec2x*vec3z) + (vec2z * vec3x) ) )
  124.         result=result + ( vec1z * ( ( vec2x*vec3y) + (vec2y * vec3x) ) )
  125.         Return result
  126. End Function


Comments :


Dreamora(Posted 1+ years ago)

 The crossproduct is wrongresulty = (vec1z*vec2x) - (vec1x*vec2z)


Beaker(Posted 1+ years ago)

 Replace..Sqr(vecx^2 + vecy^2 + vecz^2)..with..Sqr(vecx*vecx + vecy*vecy + vecz*vecz)..for speed.What I do is keep a global called result.vector or vec_result.vector to store the result in.  Speeds things up and reduces risk of memory leaks.  You can even return it as well if you want to store it elsewhere.


Beeps(Posted 1+ years ago)

 Done. Thanks guys!


Chroma(Posted 1+ years ago)

 <div class="quote"> What I do is keep a global called result.vector or vec_result.vector to store the result in. Speeds things up and reduces risk of memory leaks. </div>Ah ha!  Pure genious Beaker.  I shall implement this into my Vector Lib this very instant!


_Skully(Posted 1+ years ago)

 So with this are the x,y,z values normals and the magnitude a multiplier?


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal