Author Topic: Vector dot product calculation - i need help  (Read 2401 times)

0 Members and 1 Guest are viewing this topic.

Offline Unseen Machine

  • Forum Regular
  • Posts: 158
  • Make the game not the engine!
    • View Profile
Vector dot product calculation - i need help
« on: October 30, 2020, 04:29:32 am »
Hi Folks,

As we know math is not my strong suit! I am working on my Quake 3 BSP loader (levels) and have hit a snag. All the examples i got to work from are in C++ and have their own methods of calculating the dot product. I'd rather not have to use GLM (glm::dot function) so i am hoping someone can help me out.

So i need to calculate the distance between the camera and the given vertices.

Code: QB64: [Select]
  1. TYPE Vector3
  2.   X AS SINGLE
  3.   Y AS SINGLE
  4.   Z AS SINGLE
  5.  
  6.  
  7. DIM Cam AS Vector3, Vertices AS Vector3
  8.  
  9.  
  10. DP = DotProduct(Cam, Vertices)
  11.  
  12.  
  13.  
  14.  
  15. FUNCTION DotProduct% (Vec1 AS Vector3, Vec2 AS Vector3)
  16.  
  17.  
  18.  
  19.  
  20.  

Thanks folks,

Unseen

Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
Re: Vector dot product calculation - i need help
« Reply #1 on: October 30, 2020, 06:22:32 am »
Standard dot product calculation is
Code: [Select]
FUNCTION DotProduct! (Vec1 AS Vector3, Vec2 AS Vector3)
    DotProduct! = Vec1.X * Vec2.X + Vec1.Y * Vec2.Y + Vec1.Z * Vec2.Z
END FUNCTION
I'm not sure what you mean exactly by distance - Euclidean distance in 3D between two points is sqrt((x2-x1)^2 + (y2-y1)^2 + (z2 - z1)^2) but you don't need a dot product to work that out.

Offline Unseen Machine

  • Forum Regular
  • Posts: 158
  • Make the game not the engine!
    • View Profile
Re: Vector dot product calculation - i need help
« Reply #2 on: October 30, 2020, 06:38:25 am »
Quote
I'm not sure what you mean exactly by distance - Euclidean distance in 3D between two points is sqrt((x2-x1)^2 + (y2-y1)^2 + (z2 - z1)^2) but you don't need a dot product to work that out.

Im not wuite sure either! I'm working from this....

Quote
const float distance = glm::dot(plane.normal, glm::vec3(pos)) - plane.distance;

and the glm thingy said

Quote
Returns the dot product of x and y, i.e., result = x * y.

So i think what youve hooked me up with should do the trick but i've got so much to do on this before i can see a result!

Thanks @luke

Unseen

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Vector dot product calculation - i need help
« Reply #3 on: October 30, 2020, 12:56:35 pm »
Here are my notes from STx paper introducing vectors, some functions are written in terms of others so you might need the whole group:

Code: QB64: [Select]
  1. _TITLE "Vector Math" ' B+ started 2019-10-20
  2. ' Based on notes provided to QB64 forum by William F Barnes, on 2019-10-19
  3. ' https://www.qb64.org/forum/index.php?topic=1782.0
  4. ' A vector's dimension is the number of components it has.
  5. ' Here is code for processing 2 and 3 dimension vectors.
  6.  
  7. TYPE xyType
  8.     x AS SINGLE
  9.     y AS SINGLE
  10.  
  11. TYPE xyzType
  12.     x AS SINGLE
  13.     y AS SINGLE
  14.     z AS SINGLE
  15.  
  16. ' notation 0 w/arrowHat  (no way of telling if 2, 3 or more dimensions)
  17. DIM SHARED v2zero AS xyType, v3zero AS xyzType
  18. v2zero.x = 0: v2zero.y = 0
  19. v3zero.x = 0: v3zero.y = 0: v3zero.z = 0
  20.  
  21. 'Basis Vectors, isolate components e sub x Dot V w/arrowHat = V sub x
  22. DIM SHARED v2e(1 TO 2) AS xyType, v3e(1 TO 3) AS xyzType
  23. v2e(1).x = 1: v2e(1).y = 0
  24. v2e(2).x = 0: v2e(2).y = 1
  25. v3e(1).x = 1: v3e(1).y = 0: v3e(1).z = 0
  26. v3e(2).x = 0: v3e(2).y = 1: v3e(2).z = 0
  27. v3e(3).x = 0: v3e(3).y = 0: v3e(3).z = 1
  28.  
  29.  
  30. '==================================================================================== test area
  31.  
  32. 'define a test vector
  33. DIM Test AS xyzType, A AS xyzType, B AS xyzType, Inv AS xyzType, sing AS SINGLE
  34. A.x = 1: A.y = 2: A.z = 3
  35. B.x = -10: B.y = -20: B.z = -30
  36. PRINT "A w/arrowHat = " + v3$(A)
  37. PRINT "B w/arrowHat = " + v3$(B)
  38. v3Add A, B, Test
  39. PRINT "A + B = " + v3$(Test)
  40. v3Subtr A, B, Test
  41. PRINT "A - B = " + v3(Test)
  42. v3Scale 10, A, Test
  43. PRINT "10A = " + v3$(Test)
  44. v3Inverse A, Inv
  45. PRINT "A Inverse = " + v3$(Inv)
  46. v3Add A, Inv, Test
  47. PRINT "Check Inverse: A v3Add Inv = " + v3$(Test)
  48. PRINT "A's magnitude is "; v3Magnitude(A)
  49. v3Unit A, Test
  50. PRINT "A's unit vector is "; v3$(Test)
  51. 'isolate y component of test
  52. Test.x = 10: Test.y = -101.11: Test.z = 22.37
  53. DIM yComponent
  54. PRINT "Test = " + v3$(Test)
  55. yComponent = v3DotProduct(v3e(2), Test)
  56. PRINT "Test isolate component y = v3DotProduct(v3e(2), Test) = " + ts$(yComponent) 'yeah it worked!"
  57. setV3 1, 0, 0, A
  58. setV3 0, 1, 0, B
  59. v3CrossProduct A, B, Test
  60. PRINT "A = " + v3$(A)
  61. PRINT "B = " + v3$(B)
  62. PRINT "A Cross B = " + v3$(Test) ' = [0, 0, 1] ?
  63.  
  64.  
  65. '================================================= subs and fuctions
  66. SUB setV3 (x, y, z, setMe AS xyzType)
  67.     setMe.x = x: setMe.y = y: setMe.z = z
  68.  
  69. FUNCTION v3$ (showMeInnards AS xyzType)
  70.     v3$ = "[" + ts$(showMeInnards.x) + ", " + ts$(showMeInnards.y) + ", " + ts$(showMeInnards.z) + "]"
  71.  
  72. FUNCTION ts$ (number)
  73.     ts$ = _TRIM$(STR$(number))
  74.  
  75. 'notation UppercaseLetter w/arrowhat + uppercase Letter w/arrowHat
  76. SUB v2Add (A AS xyType, B AS xyType, Sum AS xyType)
  77.     Sum.x = A.x + B.x
  78.     Sum.y = A.y + B.y
  79. SUB v3Add (A AS xyzType, B AS xyzType, Sum AS xyzType)
  80.     Sum.x = A.x + B.x
  81.     Sum.y = A.y + B.y
  82.     Sum.z = A.z + B.z
  83.  
  84. 'notation UppercaseLetter w/arrowHat - UppercaseLetter w/arrowHat
  85. SUB v2Subtr (A AS xyType, B AS xyType, Sum AS xyType)
  86.     Sum.x = A.x - B.x
  87.     Sum.y = A.y - B.y
  88. SUB v3Subtr (A AS xyzType, B AS xyzType, Sum AS xyzType)
  89.     Sum.x = A.x - B.x
  90.     Sum.y = A.y - B.y
  91.     Sum.z = A.z - B.z
  92.  
  93. 'notation lowercaseletter (for a number next to (times)) UppercaseLetter w/arrowHat
  94. SUB v2Scale (mult AS SINGLE, A AS xyType, Scale AS xyType) 'parallels
  95.     Scale.x = mult * A.x
  96.     Scale.y = mult * A.y
  97. SUB v3Scale (mult AS SINGLE, A AS xyzType, Scale AS xyzType) 'parallels
  98.     Scale.x = mult * A.x
  99.     Scale.y = mult * A.y
  100.     Scale.z = mult * A.z
  101.  
  102. 'notation the inverse of A w/arrowHat is -A w/arrowHat
  103. SUB v2Inverse (A AS xyType, Inverse AS xyType) ' A + InverseOfA = 0
  104.     Inverse.x = -A.x
  105.     Inverse.y = -A.y
  106. SUB v3Inverse (A AS xyzType, Inverse AS xyzType) ' A + InverseOfA = 0
  107.     Inverse.x = -A.x
  108.     Inverse.y = -A.y
  109.     Inverse.z = -A.z
  110.  
  111. 'notation: A w/arrowHat Dot B w/arrowHat v2 Dot Product is a number, v3 Dot Product is a vector
  112. FUNCTION v2DotProduct (A AS xyType, B AS xyType) 'shadow or projection  if A Dot B = 0 then A , B are perpendicular
  113.     v2DotProduct = A.x * B.x + A.y * B.y
  114. FUNCTION v3DotProduct (A AS xyzType, B AS xyzType) 'shadow or projection  if A Dot B = 0 then A , B are perpendicular
  115.     v3DotProduct = A.x * B.x + A.y * B.y + A.z * B.z
  116.  
  117. 'notation absolute value bars about A w/arrowHat OR just an UppercaseLetter (with no hat), its just a number
  118. FUNCTION v2Magnitude (A AS xyType) 'hypotenuse of right triangle
  119.     v2Magnitude = SQR(v2DotProduct(A, A))
  120. FUNCTION v3Magnitude (A AS xyzType) 'hypotenuse of cube
  121.     v3Magnitude = SQR(v3DotProduct(A, A))
  122.  
  123. 'notation: A w/arrowHat X B w/arrowHat, X is a Cross get it?
  124. FUNCTION v2CrossProduct (A AS xyType, B AS xyType) ' a vector perpendicular to both A and B, v2 is a magnitude
  125.     v2CrossProduct = A.x * B.y - A.y * B.x
  126. SUB v3CrossProduct (A AS xyzType, B AS xyzType, Cross AS xyzType) ' v3 cross product is a 3d vector perpendicular to A and B
  127.     'notice x has no x components, y no y componets, z no z components
  128.     Cross.x = A.y * B.z - A.z * B.y
  129.     Cross.y = A.z * B.x - A.x * B.z
  130.     Cross.z = A.x * B.y - A.y * B.x
  131.  
  132. 'notation: A w/caratHat = A w/arrowHat divided by A (UppercaseLetter) or scaled by 1/A magnitude (no hats)
  133. SUB v2Unit (A AS xyType, Unit AS xyType)
  134.     DIM m AS SINGLE
  135.     m = v2Magnitude(A)
  136.     v2Scale 1 / m, A, Unit
  137. SUB v3Unit (A AS xyzType, Unit AS xyzType)
  138.     DIM m AS SINGLE
  139.     m = v3Magnitude(A)
  140.     v3Scale 1 / m, A, Unit
  141.  

Doesn't look too bad (math wise):
Code: QB64: [Select]
  1. FUNCTION v3DotProduct (A AS xyzType, B AS xyzType) 'shadow or projection  if A Dot B = 0 then A , B are perpendicular
  2.     v3DotProduct = A.x * B.x + A.y * B.y + A.z * B.z
  3.