Advertisement
Guest User

Basic4Android 3D Object Rotate

a guest
Apr 25th, 2015
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. Sub Process_Globals
  2. Type Node (X As Double, Y As Double, Z As Double)
  3. Type Face (Nodes As List)
  4. Type Obj (Nodes() As Node, Faces() As Face)
  5. Dim PI As Double = 3.14159
  6. End Sub
  7.  
  8. Sub Globals
  9. Dim DCube As Obj = CreateCube(-100dip,-100dip,-100dip,200dip,200dip,200dip)
  10. Dim XOffset As Int = 200dip
  11. Dim YOffset As Int = 200dip
  12.  
  13. Dim IsDown As Boolean
  14. Dim OldPoX As Int
  15. Dim OldPoY As Int
  16. Dim DragSensitivity As Int = 5
  17. End Sub
  18.  
  19. Sub InitNode(X As Double, Y As Double, Z As Double) As Node
  20.  
  21. Dim NewNode As Node
  22. NewNode.X = X
  23. NewNode.Y = Y
  24. NewNode.Z = Z
  25. Return NewNode
  26.  
  27. End Sub
  28.  
  29. Sub InitFace(Nodes As List) As Face
  30.  
  31. Dim NewFace As Face
  32. NewFace.Nodes = Nodes
  33. Return NewFace
  34.  
  35. End Sub
  36.  
  37. Sub CreateCube(X As Double, Y As Double, Z As Double, W As Double, H As Double, D As Double) As Obj
  38.  
  39. Dim Cube As Obj
  40.  
  41. Dim Nodes(8) As Node
  42. Nodes(0) = InitNode(X,Y,Z)
  43. Nodes(1) = InitNode(X,Y,Z+D)
  44. Nodes(2) = InitNode(X,Y+H,Z)
  45. Nodes(3) = InitNode(X,Y+H,Z+D)
  46. Nodes(4) = InitNode(X+W,Y,Z)
  47. Nodes(5) = InitNode(X+W,Y,Z+D)
  48. Nodes(6) = InitNode(X+W,Y+H,Z)
  49. Nodes(7) = InitNode(X+W,Y+H,Z+D)
  50.  
  51. Dim Faces(6) As Face
  52. Faces(0) = InitFace(Array As Int(1, 3, 7, 5))
  53. Faces(1) = InitFace(Array As Int(1, 0, 4, 5))
  54. Faces(2) = InitFace(Array As Int(1, 3, 2, 0))
  55. Faces(3) = InitFace(Array As Int(4, 6, 2, 0))
  56. Faces(4) = InitFace(Array As Int(4, 6, 7, 5))
  57. Faces(5) = InitFace(Array As Int(6, 7, 3, 2))
  58.  
  59. Cube.Nodes = Nodes
  60. Cube.Faces = Faces
  61.  
  62. Return Cube
  63.  
  64. End Sub
  65.  
  66. Sub RotateX(Theta As Double, DObject As Obj)
  67.  
  68. Dim Rad As Double = (2 * PI) * Theta / 360
  69.  
  70. Dim TSin As Double = Sin(Rad)
  71. Dim TCos As Double = Cos(Rad)
  72.  
  73. For N=0 To DObject.Nodes.Length - 1
  74.  
  75. Dim Y As Double = DObject.Nodes(N).Y
  76. Dim Z As Double = DObject.Nodes(N).Z
  77.  
  78. DObject.Nodes(N).Y = Y * TCos - Z * TSin
  79. DObject.Nodes(N).Z = Z * TCos + Y * TSin
  80.  
  81. Next
  82. End Sub
  83.  
  84. Sub RotateY(Theta As Double, DObject As Obj)
  85.  
  86. Dim Rad As Double = (2 * PI) * Theta / 360
  87.  
  88. Dim TSin As Double = Sin(Rad)
  89. Dim TCos As Double = Cos(Rad)
  90.  
  91. For N=0 To DObject.Nodes.Length - 1
  92.  
  93. Dim X As Double = DObject.Nodes(N).X
  94. Dim Z As Double = DObject.Nodes(N).Z
  95.  
  96. DObject.Nodes(N).X = X * TCos - Z * TSin
  97. DObject.Nodes(N).Z = Z * TCos + X * TSin
  98.  
  99. Next
  100. End Sub
  101.  
  102. Sub RotateZ(Theta As Double, DObject As Obj)
  103.  
  104. Dim Rad As Double = (2 * PI) * Theta / 360
  105.  
  106. Dim TSin As Double = Sin(Rad)
  107. Dim TCos As Double = Cos(Rad)
  108.  
  109. For N=0 To DObject.Nodes.Length - 1
  110.  
  111. Dim X As Double = DObject.Nodes(N).X
  112. Dim Y As Double = DObject.Nodes(N).Y
  113.  
  114. DObject.Nodes(N).Y = X * TCos - Y * TSin
  115. DObject.Nodes(N).Z = X * TSin + Y * TCos
  116.  
  117. Next
  118. End Sub
  119.  
  120. Sub DrawObject(DObj As Obj)
  121.  
  122. Dim Canvas As Canvas
  123. Canvas.Initialize(Activity)
  124.  
  125. Canvas.DrawColor(Colors.ARGB(255,0,0,0))
  126.  
  127. For F=0 To DObj.Faces.Length - 1
  128. Dim NodeList As List = DObj.Faces(F).Nodes
  129. Dim Str As String = NodeList.Get(0)
  130. Canvas.DrawLine(XOffset + DObj.Nodes(NodeList.Get(0)).x,YOffset + DObj.Nodes(NodeList.Get(0)).Y,XOffset + DObj.Nodes(NodeList.Get(1)).X,YOffset + DObj.Nodes(NodeList.Get(1)).y, Colors.Red, 3)
  131. Canvas.DrawLine(XOffset + DObj.Nodes(NodeList.Get(1)).x,YOffset + DObj.Nodes(NodeList.Get(1)).Y,XOffset + DObj.Nodes(NodeList.Get(2)).X,YOffset + DObj.Nodes(NodeList.Get(2)).y, Colors.Red, 3)
  132. Canvas.DrawLine(XOffset + DObj.Nodes(NodeList.Get(2)).x,YOffset + DObj.Nodes(NodeList.Get(2)).Y,XOffset + DObj.Nodes(NodeList.Get(3)).X,YOffset + DObj.Nodes(NodeList.Get(3)).y, Colors.Red, 3)
  133. Canvas.DrawLine(XOffset + DObj.Nodes(NodeList.Get(3)).x,YOffset + DObj.Nodes(NodeList.Get(3)).Y,XOffset + DObj.Nodes(NodeList.Get(0)).X,YOffset + DObj.Nodes(NodeList.Get(0)).y, Colors.Red, 3)
  134. Next
  135.  
  136. For N=0 To DObj.Nodes.Length - 1
  137. Dim Rect1 As Rect
  138. Rect1.Initialize(XOffset + DObj.Nodes(N).X - 4, YOffset + DObj.Nodes(N).Y - 4, XOffset + DObj.Nodes(N).X + 4, YOffset + DObj.Nodes(N).Y + 4)
  139. Canvas.DrawOval(Rect1, Colors.blue, True, 5)
  140. Canvas.DrawText("N:" & (N + 1) & " (X:" & Round(DObj.Nodes(N).X) & " Y:"& Round(DObj.Nodes(N).y) & " Z:" & Round(DObj.Nodes(N).z) & ")", XOffset + DObj.Nodes(N).X + 4, YOffset + DObj.Nodes(N).y + 4, Typeface.DEFAULT, 10, Colors.white, "LEFT")
  141. Next
  142. End Sub
  143.  
  144. Sub Activity_Create(FirstTime As Boolean)
  145. Activity.LoadLayout("main")
  146. DrawObject(DCube)
  147. End Sub
  148.  
  149. Sub Activity_Resume
  150.  
  151. End Sub
  152.  
  153. Sub Activity_Pause (UserClosed As Boolean)
  154.  
  155. End Sub
  156.  
  157. Sub Activity_Touch (Action As Int, X As Float, Y As Float)
  158. Select Action
  159. Case Activity.ACTION_DOWN
  160. IsDown = True
  161. OldPoX = X
  162. OldPoY = Y
  163. Case Activity.ACTION_MOVE
  164. If(IsDown = True) Then
  165. Dim NewPoX As Int = X - OldPoX
  166. Dim NewPoY As Int = Y - OldPoY
  167.  
  168. RotateX(NewPoY / DragSensitivity, DCube)
  169. RotateY(NewPoX / DragSensitivity, DCube)
  170.  
  171. OldPoX = X
  172. OldPoY = Y
  173.  
  174. DrawObject(DCube)
  175. End If
  176. Case Activity.ACTION_UP
  177. IsDown = False
  178. End Select
  179. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement