Problem Rotating Objects in 3-Dimensions

Discussion in 'Computer Science & Culture' started by Success_Machine, May 30, 2003.

Thread Status:
Not open for further replies.
  1. Success_Machine Impossible? I can do that Registered Senior Member

    Messages:
    365
    I have a problem with my Visual Basic 6.0 program. It is a simple program: just drag & drop a picturebox into your object window, then copy and paste the code below into your code window. You may have to fix the code a bit to eliminate unwanted carriage returns (due to this message board). Then run the program.

    The program is supposed to orbit a point-object in a circular path around the origin. Instead it follows a figure-eight path! Can anyone fix this?

    ' Code to Orbit point-object through incremental angles in 3D space.
    Dim pi As Double

    ' Initialize window.
    Private Sub form_activate()
    pi = 4 * Atn(1)
    Form1.WindowState = vbMaximized
    Picture1.AutoRedraw = True
    Picture1.Width = 9855
    Picture1.Height = 6375
    Picture1.ScaleMode = vbPixels
    Picture1.ScaleWidth = 640
    Picture1.ScaleHeight = 480
    Form1.Refresh
    Call LineTest
    End Sub

    ' Defines point-object position, increments angles, calls the rotate function, and draws the result.
    Private Sub LineTest()
    Dim Vx(1) As Variant
    Dim Vy(1) As Variant
    Dim Vz(1) As Variant
    Dim Px(1) As Variant
    Dim Py(1) As Variant
    Dim Pz(1) As Variant

    ' Defines the original vector position of the point-object.
    Vx(1) = 0
    Vy(1) = 200
    Vz(1) = 0

    ' Loop to increment rotation angles.
    For i = 0 To 360
    anglex = i ' Angle for rotation around the x-axis.
    angley = 0 ' Angle for rotation around the y-axis.
    anglez = 0 ' Angle for rotation around the z-axis.
    Call Rotate(1, anglex, angley, anglez, Vx(), Vy(), Vz(), Px(), Py(), Pz())

    ' Draws line from origin to rotated point-object.
    Picture1.Cls
    Picture1.DrawWidth = 2
    If Pz(1) < 0 Then
    Picture1.Line (Picture1.ScaleWidth / 2, Picture1.ScaleHeight / 2)-(Pz(1) + Picture1.ScaleWidth / 2, Py(1) + Picture1.ScaleHeight / 2), RGB(255, 0, 0)
    Else
    Picture1.Line (Picture1.ScaleWidth / 2, Picture1.ScaleHeight / 2)-(Pz(1) + Picture1.ScaleWidth / 2, Py(1) + Picture1.ScaleHeight / 2), RGB(0, 0, 0)
    End If

    ' Prints text showing object coordinates.
    Picture1.CurrentX = Pz(1) + Picture1.ScaleWidth / 2 + 10
    Picture1.CurrentY = Py(1) + Picture1.ScaleHeight / 2
    Picture1.Print "P( " & Int(Px(1)) & " , " & Int(Py(1)) & " , " & Int(Pz(1)) & " )"
    Picture1.CurrentX = 0
    Picture1.CurrentY = 0
    Picture1.Print "Rotation angle around x-axis = " & anglex
    Picture1.Print "Rotation angle around y-axis = " & angley
    Picture1.Print "Rotation angle around z-axis = " & anglez
    Picture1.Print "P( " & Int(Px(1)) & " , " & Int(Py(1)) & " , " & Int(Pz(1)) & " )"
    Picture1.CurrentX = Picture1.ScaleWidth / 2 - 45
    Picture1.CurrentY = Picture1.ScaleHeight / 2 - 10
    Picture1.Print "( 0,0,0 )"

    ' Draws axes defining yz-plane.
    Picture1.Line (10, Picture1.ScaleHeight - 30)-(10, Picture1.ScaleHeight - 70), RGB(0, 0, 0)
    Picture1.CurrentY = Picture1.ScaleHeight - 85
    Picture1.Print "Y"
    Picture1.Line (10, Picture1.ScaleHeight - 30)-(50, Picture1.ScaleHeight - 30), RGB(0, 0, 0)
    Picture1.Print "Z"
    Picture1.Refresh
    Next i
    End Sub

    ' Black Box Subroutine that Rotates and Returns Duplicate Vertices (Avoids Modification Of Original).
    Public Sub Rotate(ByVal nodeCount As Single, ByVal anglex As Single, ByVal angley As Single, ByVal anglez As Single, ByRef Inputx() As Variant, ByRef Inputy() As Variant, ByRef Inputz() As Variant, ByRef Outputx() As Variant, ByRef Outputy() As Variant, ByRef Outputz() As Variant)
    Dim ThetaRadX As Double, ThetaRadY As Double, ThetaRadZ As Double 'rotation angles in radians
    Dim Rotatx(3, 3) As Double, Rotaty(3, 3) As Double, Rotatz(3, 3) As Double 'axial rotation matrices
    Dim Resultant(3, 3) As Double 'resultant rotation matrix
    ' Converts Angles to Radians.
    ThetaRadX = anglex * pi / 180
    ThetaRadY = angley * pi / 180
    ThetaRadZ = anglez * pi / 180
    ' Matrix for Rotation around the x-axis.
    Rotatx(1, 1) = 1: Rotatx(1, 2) = 0: Rotatx(1, 3) = 0
    Rotatx(2, 1) = 0: Rotatx(2, 2) = Cos(ThetaRadX): Rotatx(2, 3) = -Sin(ThetaRadX)
    Rotatx(3, 1) = 0: Rotatx(3, 2) = Sin(ThetaRadX): Rotatx(3, 3) = Cos(ThetaRadX)
    ' Matrix for Rotation around the y-axis.
    Rotaty(1, 1) = Cos(ThetaRadY): Rotaty(1, 2) = 0: Rotaty(1, 3) = Sin(ThetaRadY)
    Rotaty(2, 1) = 0: Rotaty(2, 2) = 1: Rotaty(2, 3) = 0
    Rotaty(3, 1) = -Sin(ThetaRadY): Rotaty(3, 2) = 0: Rotaty(3, 3) = Cos(ThetaRadY)
    ' Matrix for Rotation around the z-axis.
    Rotatz(1, 1) = Cos(ThetaRadZ): Rotatz(1, 2) = -Sin(ThetaRadZ): Rotatz(1, 3) = 0
    Rotatz(2, 1) = Sin(ThetaRadZ): Rotatz(2, 2) = Cos(ThetaRadZ): Rotatz(2, 3) = 0
    Rotatz(3, 1) = 0: Rotatz(3, 2) = 0: Rotatz(3, 3) = 1
    ' Resultant Matrix for Rotation Operation.
    For i = 1 To 3
    For j = 1 To 3
    Resultant(i, j) = Rotaty(i, 1) * Rotatz(1, j) + Rotaty(i, 2) * Rotatz(2, j) + Rotaty(i, 3) * Rotatz(3, j)
    Next j
    Next i
    For i = 1 To 3
    For j = 1 To 3
    Resultant(i, j) = Rotatx(i, 1) * Resultant(1, j) + Rotatx(i, 2) * Resultant(2, j) + Rotatx(i, 3) * Resultant(3, j)
    Next j
    Next i
    ' Rotates Duplicate Vertices (Avoids Modification Of Original).
    For t = 1 To nodeCount
    Outputx(t) = Inputx(t) * Resultant(1, 1) + Inputy(t) * Resultant(1, 2) + Inputz(t) * Resultant(1, 3)
    Outputy(t) = Inputx(t) * Resultant(2, 1) + Inputy(t) * Resultant(2, 2) + Inputz(t) * Resultant(2, 3)
    Outputz(t) = Inputx(t) * Resultant(3, 1) + Inputy(t) * Resultant(3, 2) + Inputz(t) * Resultant(3, 3)
    Next t
    End Sub
     
Thread Status:
Not open for further replies.

Share This Page