September 28, 2021, 18:24:47

Author Topic: [bb] Align an Object to a Triangle. by Richard Betson [ 1+ years ago ]  (Read 969 times)

BlitzBot

• Jr. Member
• Posts: 1
[bb] Align an Object to a Triangle. by Richard Betson [ 1+ years ago ]
« on: June 29, 2017, 00:28:39 »
Title : Align an Object to a Triangle.
Author : Richard Betson
Posted : 1+ years ago

Description : This code shows how to Align an Object to a triangle.
Thanks go out to:
Halo, BlitzSupport, Floyed and Ross C

<a href="../Community/posts224c.html?topic=28662" target="_blank">http://www.blitzbasic.com/Community/posts.php?topic=28662[/url]

Have Fun!,
Rich

Code :
Code: BlitzBasic
1. ;Alighn object to triangle - By Richard Betson
2. ;www.redeyeware.50megs.com
3. ;vidiot@getgoin.net
4. ;
5. ;Special thanks to Halo, BlitzSupport, Floyed and Ross C
6. ;
7. ;This code shows how to Align an Object to a triangle.
8. ;Use as you see fit :)
9. ;
10.
11.
12.
13. Global vectorx#
14. Global vectory#
15. Global vectorz#
16. Global vectorw#
17.
18.
19.
20.
21. Graphics3D 640, 480, 0, 2
22. AppTitle "Use Arrow Keys to turn"
23.
24. cam = CreateCamera ()
25. MoveEntity cam, 0, 0, -10
26.
27.
28. Global cone = CreateCone ()
29. EntityColor cone, 0, 0, 255
30.
31. light = CreateLight ()
32. MoveEntity light, -50, -5, 10
33.
34.
35.
36. ;Mesh<<<<<
37. Global mesh=CreateMesh()
38.
39. Global surf=CreateSurface(mesh)
40.
44.
46.
47. ScaleMesh mesh,3,3,3
48. PositionEntity mesh,0,-1,0
49.
50. ;-----------------------
51.
52.
53. Repeat
54.
55.
56.         x()
57.
58.         If KeyDown (203) TurnEntity mesh, 0, -1, 0, 1
59.         If KeyDown (205) TurnEntity mesh, 0, 1, 0, 1
60.         If KeyDown (200) TurnEntity mesh, 1, 0, 0
61.         If KeyDown (208) TurnEntity mesh, -1, 0, 0
62.
63.         RenderWorld
64.         Flip
65.
66. Until KeyHit (1)
67.
68. End
69.
70.
71. Function x()
72.
73.         v0 = TriangleVertex (surf, tri, 0)
74.         v1 = TriangleVertex (surf, tri, 1)
75.         v2 = TriangleVertex (surf, tri, 2)
76.
77.         ; ---------------------------------------------------------
78.         ; Mark's code for backface culling...
79.         ; ---------------------------------------------------------
80.
81.     ; Vertex positions...
82.
83.         x0# = VertexX (surf, v0)
84.         y0# = VertexY (surf, v0)
85.         z0# = VertexZ (surf, v0)
86.
87.         x1# = VertexX (surf, v1)
88.         y1# = VertexY (surf, v1)
89.         z1# = VertexZ (surf, v1)
90.
91.         x2# = VertexX (surf, v2)
92.         y2# = VertexY (surf, v2)
93.         z2# = VertexZ (surf, v2)
94.
95.
96. d#=TriangleNormal#(x0#,y0#,z0#,x1#,y1#,z1#,x2#,y2#,z2#)
97.         TFormVector vectorx, vectory, vectorz,mesh,0
98.         AlignToVector cone, TFormedX (), TFormedY (), TFormedZ (), 2,1
99. End Function
100.
101.
102. Function TriangleNormal#(Ax#,Ay#,Az#,Bx#,By#,Bz#,Cx#,Cy#,Cz#)
103.         SubVector Bx#,By#,Bz#,Ax#,Ay#,Az#
104.         ux#=VectorX()
105.         uy#=VectorY()
106.         uz#=VectorZ()
107.         SubVector Cx#,Cy#,Cz#,Bx#,By#,Bz#
108.         vx#=VectorX()
109.         vy#=VectorY()
110.         vz#=VectorZ()
111.         CrossProduct vx#,vy#,vz#,ux#,uy#,uz#
112.         Normalize vectorx,vectory,vectorz
113.         Return Ax#*vectorx+Ay#*vectory+Az#*vectorz
114. End Function
115.
116. Function VectorX#()
117.         Return vectorx
118. End Function
119.
120. Function VectorY#()
121.         Return vectory
122. End Function
123.
124. Function VectorZ#()
125.         Return vectorz
126. End Function
127.
128. Function VectorW#()
129.         Return vectorw
130. End Function
131.
132. Function SubVector(Ax#,Ay#,Az#,Bx#,By#,Bz#)
133.         vectorx#=ax#-bx#
134.         vectory#=ay#-by#
135.         vectorz#=az#-bz#
136. End Function
137.
138. Function Normalize(nx#,ny#,nz#)
139.         If nx=0 And ny=0 And nz=0 Return
140.         m#=Magnitude(nx#,ny#,nz#)
141.         vectorx#=nx#/m#
142.         vectory#=ny#/m#
143.         vectorz#=nz#/m#
144. End Function
145.
146. Function CrossProduct(Ax#,Ay#,Az#,Bx#,By#,Bz#)
147.         vectorx#=Ay#*Bz#-Az#*By#
148.         vectory#=Az#*Bx#-Ax#*Bz#
149.         vectorz#=Ax#*By#-Ay#*Bx#
150. End Function
151.
152.
153. Function Magnitude(nx#,ny#,nz#)
154.         m#= Sqr( (nx*nx) + (ny*ny) + (nz*nz) )
155.         Return m#
156. End Function