Tools Links Login

Small 3D Wireframe Cube Example

Creates a 3D cube on the form which can be manipulated using the left, right, up and down arrow keys.

Original Author: Matthew Pearce

Assumptions

This code uses Sine and Cosine to spin the cube around.

Code

' Name: 3D Cube Program
' Author: Matthew Pearce
' Date: Tuesday, 20th January 2004
' Purpose: To display an interactive 3D cube on the screen using Sin and Cos
Dim i1 As Integer, i2 As Integer, i3 As Integer, i4 As Integer, pitch As Double
Private Sub Form_Activate()
  i1 = degtorad(0)
  i2 = degtorad(90)
  i3 = degtorad(180)
  i4 = degtorad(270)
  pitch = 0.5
  UpdateCube
End Sub
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  Select Case KeyCode
  Case vbKeyLeft:
    i1 = i1 - 10
    i2 = i2 - 10
    i3 = i3 - 10
    i4 = i4 - 10
  Case vbKeyRight:
    i1 = i1 + 10
    i2 = i2 + 10
    i3 = i3 + 10
    i4 = i4 + 10
  Case vbKeyUp:
    If pitch > -1 Then
      pitch = pitch - 0.1
    End If
  Case vbKeyDown:
    If pitch < 1 Then
      pitch = pitch + 0.1
    End If
  End Select
  UpdateCube
End Sub
Function degtorad(deg As Integer)
  deg = deg / 360 * 3.14 * 200
  degtorad = deg
End Function
Function LineCalc(inum1, inum2, inum3, inum4, add1, add2)
  ' Calculate verteces
  LX1 = LineCalcX(inum1)
  LX2 = LineCalcX(inum2)
  LY1 = LineCalcY(inum3, add1)
  LY2 = LineCalcY(inum4, add2)
  ' Draw lines between verteces
  Line (LX1, LY1)-(LX2, LY2)
End Function
Function LineCalcX(inum)
  LineCalcX = Sin(inum / 100) * 1000 + 2000
End Function
Function LineCalcY(inum, a1)
  LineCalcY = Cos(inum / 100) * 400 * pitch + a1
End Function
Function UpdateCube()
  Refresh
  ' Draw Top Face
  LineCalc i1, i2, i1, i2, 1000, 1000
  LineCalc i2, i3, i2, i3, 1000, 1000
  LineCalc i3, i4, i3, i4, 1000, 1000
  LineCalc i4, i1, i4, i1, 1000, 1000
  ' Draw Sides
  LineCalc i1, i2, i1, i2, 2000, 2000
  LineCalc i2, i3, i2, i3, 2000, 2000
  LineCalc i3, i4, i3, i4, 2000, 2000
  LineCalc i4, i1, i4, i1, 2000, 2000
  ' Draw Bottom Face
  LineCalc i1, i1, i1, i1, 1000, 2000
  LineCalc i2, i2, i2, i2, 1000, 2000
  LineCalc i3, i3, i3, i3, 1000, 2000
  LineCalc i4, i4, i4, i4, 1000, 2000
End Function

About this post

Posted: 2003-06-01
By: ArchiveBot
Viewed: 96 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.