January 23, 2021, 05:04:56 AM

### Author Topic: [bb] Vector Example by Jeppe Nielsen [ 1+ years ago ]  (Read 770 times)

#### BlitzBot

• Jr. Member
• Posts: 1
##### [bb] Vector Example by Jeppe Nielsen [ 1+ years ago ]
« on: June 29, 2017, 12:28:41 AM »
Title : Vector Example
Author : Jeppe Nielsen
Posted : 1+ years ago

Description : Graphically shows the use of aligntovector, dot and cross products

Code :
Code: BlitzBasic
1. ;small vector example by Jeppe Nielsen 2003
2. ;nielsen_jeppe@hotmail.com
3. ;
4. ;showing dot and cross products in action!
5. ;and aligntovector.
6.
7. Graphics3D 800,600,16,2
8.
9. SeedRnd MilliSecs()
10.
11. p1=CreatePivot()
12. p2=CreatePivot()
13. p3=CreatePivot()
14.
15. RotateEntity p1,Rnd(360),Rnd(360),Rnd(360)
16. RotateEntity p2,Rnd(360),Rnd(360),Rnd(360)
17.
18. cube=CreateCube()
19. ScaleEntity cube,0.5,0.5,0.5
20. EntityAlpha cube,0.5
21.
22. campivot=CreatePivot()
23. camera=CreateCamera(campivot)
24.
25. PositionEntity camera,0,2,-3
26. PointEntity camera,campivot
27.
28. Repeat
29.
30. TurnEntity campivot,0,2,0
31.
32. TFormVector 0,0,1,p1,0
33.
34. If KeyHit(57)=True
35.
36. align=1-align
37.
38. turnangle1#=Rnd(-2,2)
39. turnangle2#=Rnd(-2,2)
40.
41. EndIf
42.
43.
44. If align=1
45.
46. ;change the last parameter for varied speeds
47. AlignToVector p2,TFormedX(),TFormedY(),TFormedZ(),3,0.04
48.
49. Else
50.
51. TurnEntity p2,turnangle1#,turnangle2#,0
52.
53. EndIf
54.
55.
56. TFormNormal 0,0,1,p1,0
57. nx1#=TFormedX()
58. ny1#=TFormedY()
59. nz1#=TFormedZ()
60.
61. TFormNormal 0,0,1,p2,0
62. nx2#=TFormedX()
63. ny2#=TFormedY()
64. nz2#=TFormedZ()
65.
66. dot#=nx1*nx2+ny1*ny2+nz1*nz2
67.
68. angle#=ACos(dot#)
69.
70. ;Make a third vector perpendicular to two other vectors
71. cx# = ( ny1 * nz2 ) - ( nz1 * ny2 )
72. cy# = ( nz1 * nx2 ) - ( nx1 * nz2 )
73. cz# = ( nx1 * ny2 ) - ( ny1 * nx2 )
74.
75. AlignToVector p3,cx,cy,cz,3
76.
77.
78. RenderWorld()
79.
80. drawworldaxes(camera,1)
81. drawvector(p1,camera,1.5,255,255,255)
82. drawvector(p2,camera,1.5,0,0,255)
83. drawvector(p3,camera,1.5,255,0,0)
84.
85. Color 0,255,0
86. Text 10,10,"Angle between blue and white vectors: "+angle
87. Text 10,30,"Red vector is perpendicular to the two others: "
88. Text 10,50,"Space to toggle between align/rotate the blue vector"
89. Text 10,70,"White vector coordinates:"
90. Text 10,80,"X: "+nx1#
91. Text 10,90,"Y: "+ny1#
92. Text 10,100,"Z: "+nz1#
93.
94. Flip
95. Until KeyDown(1)
96. End
97.
98. Function drawvector(e,camera,length,r,g,b)
99.
100. ;Get initial position
101. CameraProject camera,EntityX(e,1),EntityY(e,1),EntityZ(e,1)
102. x=ProjectedX()
103. y=ProjectedY()
104.
105. ;Draw X axis
106. ;Color 255,0,0
107. ;MoveEntity e,length,0,0
108. ;CameraProject camera,EntityX(e,1),EntityY(e,1),EntityZ(e,1)
109. ;Line x,y,ProjectedX(),ProjectedY()
110. ;MoveEntity e,-length,0,0
111.
112. ;Draw Y axis
113. ;Color 0,255,0
114. ;MoveEntity e,0,length,0
115. ;CameraProject camera,EntityX(e,1),EntityY(e,1),EntityZ(e,1)
116. ;Line x,y,ProjectedX(),ProjectedY()
117. ;MoveEntity e,0,-length,0
118.
119. ;Draw Z axis
120. Color r,g,b ;,0,255
121. MoveEntity e,0,0,length
122. CameraProject camera,EntityX(e,1),EntityY(e,1),EntityZ(e,1)
123. Line x,y,ProjectedX(),ProjectedY()
124. MoveEntity e,0,0,-length
125. Oval ProjectedX()-4,ProjectedY()-4,8,8,1
126.
127. End Function
128.
129. Function drawworldaxes(camera,length)
130.
131. ;Get initial position
132. CameraProject camera,0,0,0
133. x=ProjectedX()
134. y=ProjectedY()
135.
136. ;Draw X axis
137. Color 255,0,0
138. CameraProject camera,length,0,0
139. Line x,y,ProjectedX(),ProjectedY()
140. CameraProject camera,length*1.1,0,0
141. Oval ProjectedX()-12,ProjectedY()-12,24,24,0
142. Text ProjectedX(),ProjectedY(),"X",1,1
143.
144. ;Draw Y axis
145. Color 0,255,0
146. CameraProject camera,0,length,0
147. Line x,y,ProjectedX(),ProjectedY()
148. CameraProject camera,0,length*1.1,0
149. Oval ProjectedX()-12,ProjectedY()-12,24,24,0
150. Text ProjectedX(),ProjectedY(),"Y",1,1
151.
152. ;Draw Z axis
153. Color 0,0,255
154. CameraProject camera,0,0,length
155. Line x,y,ProjectedX(),ProjectedY()
156. CameraProject camera,0,0,length*1.1
157. Oval ProjectedX()-12,ProjectedY()-12,24,24,0
158. Text ProjectedX(),ProjectedY(),"Z",1,1
159.
160. End Function