[bb] Vector Math library by Beeps [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:42

Previous topic - Next topic

BlitzBot

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) Select
;vector maths lib by Beeps

;used to normalize vector
Const vector_tol#=0.0001

Type vector
Field x#
Field y#
Field z#
Field magnitude#
End Type

Function vector_createVector.vector(vx#,vy#,vz#)
;create a populated vector and return
vec.vector=New vector
vecx=vx
vecy=vy
vecz=vz
Return vec
End Function

Function vector_calcMagnitude(vec.vector)
;calc the magnitude of a vector and stor in vector
vecmagnitude=Sqr(vecx*vecx + vecy*vecy + vecz*vecz)
End Function

Function vector_normalize(vec.vector)
;normalize a vector so it's length = 1 and apply tolerance based on our constant tolerance value
m#=Sqr(Sqr(vecx*vecx + vecy*vecy + vecz*vecz) )
If m<= vector_tol Then m=1
vecx=vecx/m
vecy=vecy/m
vecz=vecz/m
If Abs(vecx)<vector_tol Then vecx=0
If Abs(vecy)<vector_tol Then vecy=0
If Abs(vecz)<vector_tol Then vecz=0
End Function

Function vector_reverse(vec.vector)
;reverse a vector
vecx=-vecx
vecy=-vecy
vecz=-vecz
End Function

Function vector_add.vector(vec1.vector,vec2.vector)
;add two vectors together and return the resulting vector
result.vector=New vector

resultx=vec1x+vec2x
resulty=vec1y+vec2y
resultz=vec1z+vec2z

Return result
End Function

Function vector_subtract.vector(vec1.vector,vec2.vector)
;subtract vec1 from vec2 and return the resulting vector
result.vector=New vector

resultx=vec1x-vec2x
resulty=vec1y-vec2y
resultz=vec1z-vec2z

Return result
End Function

Function vector_scalarMultiply.vector(vec1.vector,scale#)
;scalar multiplication of a vector with result returned as new vector
;used to scale a vector by 'scale'
result.vector=New vector
resultx=vec1x*scale
resulty=vec1y*scale
resultz=vec1z*scale
Return result
End Function

Function vector_scalarDivision.vector(vec1.vector,scale#)
;scalar division of a vector with result returned as new vector
;used to scale a vector
result.vector=New vector
resultx=vec1x/scale
resulty=vec1y/scale
resultz=vec1z/scale
Return result
End Function

Function vector_conjugate.vector(vec1.vector)
;conjugate operator takes the negative of each vector component
;can be used when subtracting one vector from another or for
;reversing the direction of a vector.
;applying conjugate is the same as reversing a vector
;returns a new vector
result.vector = New vector
resultx=-vec1x
resulty=-vec1y
resultz=-vec1z
Return result
End Function

Function vector_crossProduct.vector(vec1.vector,vec2.vector)
;takes vec1 and vec2 and returns the cross product vec1 X vec2
;the cross product is a vector perpendicular to both vec1 and vec2
;this is the normal of 2 vectors
result.vector=New vector

resultx=(vec1y*vec2z) - (vec1z*vec2y)
resulty=(vec1z*vec2x) - (vec1x*vec2z)
resultz=(vec1x*vec2y) - (vec1y*vec2x)

Return result
End Function

Function vector_dotProduct#(vec1.vector,vec2.vector)
;calculate and return the dot product of 2 vectors (distance)
result#=(vec1x*vec2x)+(vec1y*vec2y)+(vec1z*vec2z)
Return result
End Function

Function vector_tripleScalarProduct#(vec1.vector,vec2.vector,vec3.vector)
;calculate the triple scalar function and return it
result#=          vec1x * ( (vec2y*vec3z ) - (vec2z * vec3y) )
result=result + ( vec1y * ( (-vec2x*vec3z) + (vec2z * vec3x) ) )
result=result + ( vec1z * ( ( vec2x*vec3y) + (vec2y * vec3x) ) )
Return result
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?