Page 4 of 4 FirstFirst 1234
Results 121 to 125 of 125

Thread: 3D Math: how draw a 3D cube?

  1. #121

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,352

    Re: 3D Math: how draw a 3D cube?

    SeabrookStan's: i have 2 questions:
    1 - how you get\calculate the line coordinates when the vector it's outside the view\camera?
    2 - isn't normal draw the plane using triangles?
    VB6 2D Sprite control

    To live is difficult, but we do it.

  2. #122

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,352

    Re: 3D Math: how draw a 3D cube?

    passel: the DrawPlane is almost done:
    Code:
    Private Sub DrawPlane(Position As position3D, Size As Size3D, Rotation As Angle3D, WorldSize As Size3D)
        Dim Points(4) As POINTAPI
        
        Dim NewPoint As POINTAPI
        
        Dim NewPosition3D(4) As position3D
        Dim RotatedPosition As position3D
        Dim NewPosition3D2 As position3D
        
        'FillPosition3D RotatedPosition, Position.X + Size.Width / 2, Position.Y + Size.Height / 2, Position.Z + Size.ZDepth / 2
        With Player1.Position
           FillPosition3D RotatedPosition, .x, .Y, .Z  'Camera Position
        End With
        
        If (IsCollision3D(Player1.Position, WorldSize, Position, Size) = False) Then Exit Sub
        
        'Floor:
        'Vector1
        FillPosition3D NewPosition3D(0), Position.x, Position.Y, Position.Z
        NewPosition3D(0) = Rotate(NewPosition3D(0), Rotation, RotatedPosition)
        
        If (IsOnCamera(NewPosition3D(0), camera1.Position, camera1.Size) = False) Then
            
            NewPosition3D(0) = GetInCamVector(NewPosition3D(0), NewPosition3D2)
        End If
        
        NewPoint = ConvertPositon3DTo2D(NewPosition3D(0), WorldSize)
        Points(0) = NewPoint
        
        'Vector2
        FillPosition3D NewPosition3D(1), Position.x, Position.Y, Position.Z + Size.ZDepth
        NewPosition3D(1) = Rotate(NewPosition3D(1), Rotation, RotatedPosition)
        
        NewPoint = ConvertPositon3DTo2D(NewPosition3D(1), WorldSize)
        Points(1) = NewPoint
        
        'Vector3
        FillPosition3D NewPosition3D(2), Position.x + Size.Width, Position.Y, Position.Z + Size.ZDepth
        NewPosition3D(2) = Rotate(NewPosition3D(2), Rotation, RotatedPosition)
        NewPosition3D2 = NewPosition3D(2)
        NewPoint = ConvertPositon3DTo2D(NewPosition3D(2), WorldSize)
        Points(2) = NewPoint
        
        'Vector4:
        'getting the wrong vector X :(
        NewPosition3D(3) = NewPosition3D(0)
        NewPosition3D(3).x = NewPosition3D(2).x
        NewPoint = ConvertPositon3DTo2D(NewPosition3D(3), WorldSize)
        Points(3) = NewPoint
        
        
        FillStyle = vbFSSolid
        
        FillColor = vbRed
        Polygon Me.hDC, Points(0), 4
    End Sub
    but the vector 4, the position 'X', seems wrong... can you advice me more?
    VB6 2D Sprite control

    To live is difficult, but we do it.

  3. #123
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,984

    Re: 3D Math: how draw a 3D cube?

    Position, Size, Rotation and WorldSize values are unknown.

    Rotate function doesn't exist.
    ConvertPosition3DTo2D doesn't exist.
    IsCollision3D function doesn't exist.
    IsOnCamera function doesn't exist.
    GetInCamVector function doesn't exist.

    Nothing compiles. Nothing is testable.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  4. #124

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,352

    Re: 3D Math: how draw a 3D cube?

    i'm using a timer for use the keyboard
    arrow\direction keys and the 'W', 'S', 'A' and 'S'
    Code:
    Option Explicit
    
    'Get Key state:
    Const KEY_DOWN As Integer = &H8000
    Private Declare Function GetKeyState Lib "user32.dll" (ByVal nVirtKey As KeyCodeConstants) As Integer
    
    'GDI pens and brushes:
    Private Declare Function SelectObject Lib "GDI32.dll" ( _
    ByVal hDC As Long, ByVal hObject As Long) As Long
    Private Declare Function GetDCBrushColor Lib "GDI32.dll" ( _
    ByVal hDC As Long) As Long
    Private Declare Function SetDCBrushColor Lib "GDI32.dll" ( _
    ByVal hDC As Long, ByVal crColor As Long) As Long
    Private Declare Function CreateSolidBrush Lib "GDI32.dll" (ByVal crColor As Long) As Long
    
    'Draw array Vertices:
    Private Type POINTAPI
        x As Long
        Y As Long
    End Type
    Private Declare Function Polygon Lib "gdi32" (ByVal hDC As Long, lpPoint As POINTAPI, ByVal nCount As Long) As Long
    
    Private Enum Coordenates
        None
        Z
        x
        Y
    End Enum
    
    Private Type position3D
        x As Double
        Y As Double
        Z As Double
    End Type
    
    Private Type Angle3D
        x As Double
        Y As Double
        Z As Double
    End Type
    
    Private Type Size3D
        Width As Double
        Height As Double
        ZDepth As Double
        distance As Double
    End Type
    
    Private Type Player
        Position As position3D
        Movement As position3D
        Size As Size3D
        Angle As Angle3D
        MoveCoordenates As Coordenates
    End Type
    
    Private Type Camera
        Position As position3D
        Size As Size3D
    End Type
    
    Private Const Pi As Double = 3.14159265358979
    Dim Angletest As Angle3D
    
    Dim Player1 As Player
    Dim camera1 As Camera
    
    Private Function IsKeyDown(ByVal nVirtKey As KeyCodeConstants) As Boolean
        If GetKeyState(nVirtKey) And KEY_DOWN Then
            IsKeyDown = True
        Else
            IsKeyDown = False
        End If
    End Function
    
    Private Sub LetPoint3dArray(la() As position3D, ParamArray va() As Variant)
        ' No error trapping, so use with caution.
        Dim i       As Long
        Dim j       As Long
        Dim k      As Long
    
        k = (UBound(va) + 1) \ 3 - 1
        ReDim la(k)
        For i = 0& To k
            la(i).x = va(j): j = j + 1
            la(i).Y = va(j): j = j + 1
            la(i).Z = va(j): j = j + 1
        Next
    End Sub
    
    Private Sub FillPosition2D(Position As POINTAPI, x As Long, Y As Long)
        Position.x = x
        Position.Y = Y
    End Sub
    
    Private Sub FillPosition3D(Position As position3D, x As Double, Y As Double, Z As Double)
        Position.x = x
        Position.Y = Y
        Position.Z = Z
    End Sub
    
    Private Sub FillSize3D(Size As Size3D, Width As Double, Height As Double, ZDepth As Double)
        Size.Width = Width
        Size.Height = Height
        Size.ZDepth = ZDepth
    End Sub
    
    Private Function GetLinePosition(Origin As position3D, Destiny As position3D, ActualPosition As Double, Optional WhatCoordenate As Coordenates = Z) As position3D
        
        'Getting the AB vector(B-A or Destiny-Origin):
        Dim AB As position3D
        AB.x = Destiny.x - Origin.x
        AB.Y = Destiny.Y - Origin.Y
        AB.Z = Destiny.Z - Origin.Z
        
        Dim NewPosition As position3D
        Dim T As Double
        'depending on player movement(for get 1 coordenate and the T), we get the point coordenate:
        If (WhatCoordenate = x) Then
            'P.x = O.x + t*AB.x
            't = (P.x - O.x) / AB.x
            If (AB.x = 0) Then
                T = 0
            Else
                T = (ActualPosition - Origin.x) / AB.x
            End If
            NewPosition.x = ActualPosition
            NewPosition.Y = Origin.Y + AB.Y * T
            NewPosition.Z = Origin.Z + AB.Z * T
            
        ElseIf (WhatCoordenate = Y) Then
            'P.y = O.y + t*AB.y
            't = (P.y - O.y) / AB.y
            
            If (AB.Y = 0) Then
                T = 0
            Else
                T = (ActualPosition - Origin.Y) / AB.Y
            End If
            NewPosition.Y = ActualPosition
            NewPosition.x = Origin.x + AB.x * T
            NewPosition.Z = Origin.Z + AB.Z * T
        ElseIf (WhatCoordenate = Z) Then
            'P.z = O.z + t*AB.z
            't = (P.z - O.z) / AB.z
            
            If (AB.Z = 0) Then
                T = 0
            Else
                T = (ActualPosition - Origin.Z) / AB.Z
            End If
            NewPosition.Z = ActualPosition
            NewPosition.x = Origin.x + AB.x * T
            NewPosition.Y = Origin.Y + AB.Y * T
        End If
        GetLinePosition = NewPosition
    End Function
    
    
    Private Function ConvertPositon3DTo2D(Position As position3D, World3DSize As Size3D) As POINTAPI
        Dim ConvertedPosition As POINTAPI
        Dim PosZZDepth As Long
        
        PosZZDepth = Position.Z + World3DSize.distance
        
        If (PosZZDepth = 0) Then PosZZDepth = 1 'avoiding division by zero
        Dim Width As Double
        Dim Height As Double
        Width = World3DSize.Width / 2
        If (Width = 0) Then Width = 0
        Height = World3DSize.Height / 2
        If (Height = 0) Then Height = 0
        ConvertedPosition.x = (Position.x * World3DSize.distance / PosZZDepth) + Width
        ConvertedPosition.Y = (Position.Y * World3DSize.distance / PosZZDepth) + Height
        ConvertPositon3DTo2D = ConvertedPosition
    End Function
    
    Private Function IsOnCamera(VerticePosition As position3D, CameraPosition As position3D, CameraSize As Size3D) As Boolean
        If (((VerticePosition.Z) >= CameraPosition.Z) And ((VerticePosition.Z) <= (CameraPosition.Z + CameraSize.ZDepth)) _
        And (((VerticePosition.Y) >= CameraPosition.Y) And ((VerticePosition.Y) <= (CameraPosition.Y + CameraSize.Height))) _
        And (((VerticePosition.x) >= CameraPosition.x) And ((VerticePosition.x) <= (CameraPosition.x + CameraSize.Width)))) Then
            IsOnCamera = True
        Else
            IsOnCamera = False
        End If
    End Function
        
    Private Function IsCollision3D(ObjectPosition1 As position3D, ObjectSize1 As Size3D, ObjectPosition2 As position3D, ObjectSize2 As Size3D) As Boolean
      IsCollision3D = ((ObjectPosition1.x <= (ObjectPosition2.x + ObjectSize2.Width)) And ((ObjectPosition1.x + ObjectSize1.Width) >= ObjectPosition2.x)) And ((ObjectPosition1.Y <= (ObjectPosition2.Y + ObjectSize2.Height)) And ((ObjectPosition1.Y + ObjectSize1.Height) >= ObjectPosition2.Y)) And ((ObjectPosition1.Z <= (ObjectPosition2.Z + ObjectSize2.ZDepth)) And ((ObjectPosition1.Z + ObjectSize1.ZDepth) >= ObjectPosition2.Z))
    End Function
    
    Private Function ConvertDegreesToRadians(Rotation As Angle3D) As Angle3D
        Dim deg2Rad As Double
        deg2Rad = Pi / 180
        
        ConvertDegreesToRadians.x = Rotation.x * deg2Rad
        ConvertDegreesToRadians.Y = Rotation.Y * deg2Rad
        ConvertDegreesToRadians.Z = Rotation.Z * deg2Rad
    End Function
    
    Private Function Rotate(Position As position3D, Rotation As Angle3D, PositionRotated As position3D) As position3D
        Dim ConvertedPosition As position3D
        Dim RotationInRads As Angle3D
        
        'Convert degrees to Radians
        'Rotation works on Radians
        RotationInRads = ConvertDegreesToRadians(Rotation)
        
        'Convert the actual position to zero, using the rotation position:
        ConvertedPosition = Position
        ConvertedPosition.x = Position.x - PositionRotated.x
        ConvertedPosition.Y = -(PositionRotated.Y - Position.Y)  'reversed because Y increments down
        ConvertedPosition.Z = Position.Z - PositionRotated.Z
        
        
        'Rotate the position, but the Z axis must be the 1st:
        Dim T As position3D
        
        'Z axis  (Roll)
        T = ConvertedPosition
            ConvertedPosition.x = T.x * Cos(RotationInRads.Z) - T.Y * Sin(RotationInRads.Z)
            ConvertedPosition.Y = T.x * Sin(RotationInRads.Z) + T.Y * Cos(RotationInRads.Z)
        
        'X axis  (Pitch)
        T = ConvertedPosition
            ConvertedPosition.Y = T.Y * Cos(RotationInRads.x) - T.Z * Sin(RotationInRads.x)
            ConvertedPosition.Z = T.Y * Sin(RotationInRads.x) + T.Z * Cos(RotationInRads.x)
      
        'Y axis  (Yaw)
        T = ConvertedPosition
            ConvertedPosition.x = T.Z * Sin(RotationInRads.Y) + T.x * Cos(RotationInRads.Y)
            ConvertedPosition.Z = T.Z * Cos(RotationInRads.Y) - T.x * Sin(RotationInRads.Y)
            
            'Convert to the rotated original position:
            ConvertedPosition.x = ConvertedPosition.x + PositionRotated.x
            ConvertedPosition.Y = ConvertedPosition.Y + PositionRotated.Y
            ConvertedPosition.Z = ConvertedPosition.Z + PositionRotated.Z
            
            'Convert to Camera Coordinates\Position:
            'ConvertedPosition.X = ConvertedPosition.X - Player1.Position.X
            'ConvertedPosition.Y = ConvertedPosition.Y - Player1.Position.Y
            'ConvertedPosition.Z = ConvertedPosition.Z - Player1.Position.Z
            
        Rotate = ConvertedPosition
    End Function
    
    Private Sub DrawCube(Position As position3D, Size As Size3D, Rotation As Angle3D, WorldSize As Size3D)
        Dim Vertices(11) As POINTAPI
        
        Dim NewPoint As POINTAPI
        
        Dim NewPosition3D As position3D
        Dim sized As Size3D
        Dim RotatedPosition As position3D
        Static FixedPoint0(4) As position3D
        
        'FillPosition3D RotatedPosition, Position.X + Size.Width / 2, Position.Y + Size.Height / 2, Position.Z + Size.ZDepth / 2
        With Player1.Position
           FillPosition3D RotatedPosition, .x, .Y, .Z  'Camera Position
        End With
        If (IsOnCamera(NewPosition3D, camera1.Position, camera1.Size) = False) Then Exit Sub
        
        'Floor:
        
        
        'Vector1
        FillPosition3D NewPosition3D, Position.x, Position.Y, Position.Z
        NewPosition3D = Rotate(NewPosition3D, Rotation, RotatedPosition)
        NewPoint = ConvertPositon3DTo2D(NewPosition3D, WorldSize)
        Vertices(0) = NewPoint
        
        'Vector2
        FillPosition3D NewPosition3D, Position.x, Position.Y, Position.Z + Size.ZDepth
        NewPosition3D = Rotate(NewPosition3D, Rotation, RotatedPosition)
        NewPoint = ConvertPositon3DTo2D(NewPosition3D, WorldSize)
        Vertices(1) = NewPoint
        
        'Vector3
        'FillPosition3D NewPosition3D, 105000, 0, 224000
        FillPosition3D NewPosition3D, Position.x + Size.Width, Position.Y, Position.Z + Size.ZDepth
        NewPosition3D = Rotate(NewPosition3D, Rotation, RotatedPosition)
        NewPoint = ConvertPositon3DTo2D(NewPosition3D, WorldSize)
        Vertices(2) = NewPoint
        
        'Vector4
        'FillPosition3D NewPosition3D, 105000, 0, 204000
        FillPosition3D NewPosition3D, Position.x + Size.Width, Position.Y, Position.Z
        NewPosition3D = Rotate(NewPosition3D, Rotation, RotatedPosition)
        NewPoint = ConvertPositon3DTo2D(NewPosition3D, WorldSize)
        Vertices(3) = NewPoint
        
        
        'Roof:
        'Vector1
        'FillPosition3D NewPosition3D, 95000, -6000, 204000
        FillPosition3D NewPosition3D, Position.x, Position.Y + Size.Height, Position.Z
        NewPosition3D = Rotate(NewPosition3D, Rotation, RotatedPosition)
        NewPoint = ConvertPositon3DTo2D(NewPosition3D, WorldSize)
        Vertices(4) = NewPoint
        
        'Vector2
        'FillPosition3D NewPosition3D, 95000, -6000, 224000
        FillPosition3D NewPosition3D, Position.x, Position.Y + Size.Height, Position.Z + Size.ZDepth
        NewPosition3D = Rotate(NewPosition3D, Rotation, RotatedPosition)
        NewPoint = ConvertPositon3DTo2D(NewPosition3D, WorldSize)
        Vertices(5) = NewPoint
        
        'Vector3
        'FillPosition3D NewPosition3D, 105000, -6000, 224000
        FillPosition3D NewPosition3D, Position.x + Size.Width, Position.Y + Size.Height, Position.Z + Size.ZDepth
        NewPosition3D = Rotate(NewPosition3D, Rotation, RotatedPosition)
        NewPoint = ConvertPositon3DTo2D(NewPosition3D, WorldSize)
        Vertices(6) = NewPoint
        
        'Vector4
        'FillPosition3D NewPosition3D, 105000, -6000, 204000
        FillPosition3D NewPosition3D, Position.x + Size.Width, Position.Y + Size.Height, Position.Z
        NewPosition3D = Rotate(NewPosition3D, Rotation, RotatedPosition)
        NewPoint = ConvertPositon3DTo2D(NewPosition3D, WorldSize)
        Vertices(7) = NewPoint
        
        'Left:
        'Vector1
        'FillPosition3D NewPosition3D, 95000, 0, 204000
        FillPosition3D NewPosition3D, Position.x, Position.Y, Position.Z
        NewPosition3D = Rotate(NewPosition3D, Rotation, RotatedPosition)
        NewPoint = ConvertPositon3DTo2D(NewPosition3D, WorldSize)
        Vertices(8) = NewPoint
        
        'Vector2
        'FillPosition3D NewPosition3D, 95000, -6000, 204000
        FillPosition3D NewPosition3D, Position.x, Position.Y + Size.Height, Position.Z
        NewPosition3D = Rotate(NewPosition3D, Rotation, RotatedPosition)
        NewPoint = ConvertPositon3DTo2D(NewPosition3D, WorldSize)
        Vertices(9) = NewPoint
        
        'Vector3
        'FillPosition3D NewPosition3D, 95000, -6000, 224000
        FillPosition3D NewPosition3D, Position.x, Position.Y + Size.Height, Position.Z + Size.ZDepth
        NewPosition3D = Rotate(NewPosition3D, Rotation, RotatedPosition)
        NewPoint = ConvertPositon3DTo2D(NewPosition3D, WorldSize)
        Vertices(10) = NewPoint
        
        'Vector4
        'FillPosition3D NewPosition3D, 95000, 0, 224000
        FillPosition3D NewPosition3D, Position.x, Position.Y, Position.Z + Size.ZDepth
        NewPosition3D = Rotate(NewPosition3D, Rotation, RotatedPosition)
        NewPoint = ConvertPositon3DTo2D(NewPosition3D, WorldSize)
        Vertices(11) = NewPoint
        
        'FillStyle = vbFSSolid
        
        'FillColor = vbRed
        'If (IsCollisionVertice3D(NewPosition3D, Player1.Position, World3D) = True) Then
            Polygon Me.hDC, Vertices(0), 4
        'End If
          
        FillColor = vbBlue
        'If (IsCollisionVertice3D(NewPosition3D, Player1.Position, World3D) = True) Then
            Polygon Me.hDC, Vertices(4), 4
        'End If
    
        FillColor = vbGreen
        'If (IsCollisionVertice3D(NewPosition3D, Player1.Position, World3D) = True) Then
            Polygon Me.hDC, Vertices(8), 4
        'End If
    End Sub
    
    Private Sub DrawLine(Origin As position3D, Destiny As position3D, Rotation As Angle3D, WorldSize As Size3D)
        Dim Points(2) As POINTAPI
        Dim NewPosition3D(2) As position3D
        Dim RotatedPosition As position3D
    
        With Player1.Position
           FillPosition3D RotatedPosition, .x, .Y, .Z  'rotate using camera Position
        End With
        
        'Line:
        'Calculate Vector1:
        FillPosition3D NewPosition3D(0), Origin.x, Origin.Y, Origin.Z
        NewPosition3D(0) = Rotate(NewPosition3D(0), Rotation, RotatedPosition)
        
        'Calculate Vector2:
        FillPosition3D NewPosition3D(1), Destiny.x, Destiny.Y, Destiny.Z
        NewPosition3D(1) = Rotate(NewPosition3D(1), Rotation, RotatedPosition)
        
        'Testing If Vector0 and Vector1 are inside of camera:
        
        
        
        If (IsOnCamera(NewPosition3D(0), camera1.Position, camera1.Size) = False And _
            IsOnCamera(NewPosition3D(1), camera1.Position, camera1.Size) = False) Then
            Exit Sub
        End If
        
        If (IsOnCamera(NewPosition3D(0), camera1.Position, camera1.Size) = False) Then
            NewPosition3D(0) = GetInCamVector(NewPosition3D(0), NewPosition3D(1))
        End If
        
       If (IsOnCamera(NewPosition3D(1), camera1.Position, camera1.Size) = False) Then
            NewPosition3D(1) = GetInCamVector(NewPosition3D(1), NewPosition3D(0))
        End If
        
        'Convert the Vector1 to 2D:
        Points(0) = ConvertPositon3DTo2D(NewPosition3D(0), camera1.Size)
        
        'Convert the Vector2 to 2D:
        Points(1) = ConvertPositon3DTo2D(NewPosition3D(1), camera1.Size)
        Polygon Me.hDC, Points(0), 2
        
    End Sub
    
    
    Private Sub DrawPlane(Position As position3D, Size As Size3D, Rotation As Angle3D, WorldSize As Size3D)
        Dim Points(4) As POINTAPI
        
        Dim NewPoint As POINTAPI
        
        Dim NewPosition3D(4) As position3D
        Dim RotatedPosition As position3D
        Dim NewPosition3D2 As position3D
        
        'FillPosition3D RotatedPosition, Position.X + Size.Width / 2, Position.Y + Size.Height / 2, Position.Z + Size.ZDepth / 2
        With Player1.Position
           FillPosition3D RotatedPosition, .x, .Y, .Z  'Camera Position
        End With
        
        If (IsCollision3D(Player1.Position, WorldSize, Position, Size) = False) Then Exit Sub
        
        'Floor:
        'Vector1
        FillPosition3D NewPosition3D(0), Position.x, Position.Y, Position.Z
        NewPosition3D(0) = Rotate(NewPosition3D(0), Rotation, RotatedPosition)
        
        If (IsOnCamera(NewPosition3D(0), camera1.Position, camera1.Size) = False) Then
            
            NewPosition3D(0) = GetInCamVector(NewPosition3D(0), NewPosition3D2)
        End If
        
        NewPoint = ConvertPositon3DTo2D(NewPosition3D(0), WorldSize)
        Points(0) = NewPoint
        
        'Vector2
        FillPosition3D NewPosition3D(1), Position.x, Position.Y, Position.Z + Size.ZDepth
        NewPosition3D(1) = Rotate(NewPosition3D(1), Rotation, RotatedPosition)
        
        NewPoint = ConvertPositon3DTo2D(NewPosition3D(1), WorldSize)
        Points(1) = NewPoint
        
        'Vector3
        FillPosition3D NewPosition3D(2), Position.x + Size.Width, Position.Y, Position.Z + Size.ZDepth
        NewPosition3D(2) = Rotate(NewPosition3D(2), Rotation, RotatedPosition)
        NewPosition3D2 = NewPosition3D(2)
        NewPoint = ConvertPositon3DTo2D(NewPosition3D(2), WorldSize)
        Points(2) = NewPoint
        
        'Vector4:
        'getting the wrong vector X :(
        NewPosition3D(3) = NewPosition3D(0)
        NewPosition3D(3).x = NewPosition3D(2).x
        NewPoint = ConvertPositon3DTo2D(NewPosition3D(3), WorldSize)
        Points(3) = NewPoint
        
        
        FillStyle = vbFSSolid
        
        FillColor = vbRed
        Polygon Me.hDC, Points(0), 4
    End Sub
    
    Private Function Ceil(dInput As Double) As Double
        ' Ceiling, defined as: the smallest integer that is not smaller than x.
        ' Works for both positive and negative.
        Ceil = Int(dInput): If Ceil <> dInput Then Ceil = Ceil + 1#
    End Function
    
    Private Function GetInCamVector(Origin As position3D, Destiny As position3D) As position3D
        Dim Steps As Double
        Steps = Math.Sqr(Math.Abs(Destiny.x - Origin.x) ^ 2 + Math.Abs(Destiny.Y - Origin.Y) ^ 2 + Math.Abs(Destiny.Z - Origin.Z) ^ 2)
        Steps = Ceil(Steps)
        
        Dim increment As position3D
        increment.x = (Destiny.x - Origin.x) / Steps
        increment.Y = (Destiny.Y - Origin.Y) / Steps
        increment.Z = (Destiny.Z - Origin.Z) / Steps
        
        Dim nextpoint As position3D
        nextpoint = Origin
        Dim i As Integer
        
        Dim inter As position3D
        Dim Size As Size3D
        For i = 1 To Steps
            nextpoint.x = nextpoint.x + increment.x
            nextpoint.Y = nextpoint.Y + increment.Y
            nextpoint.Z = nextpoint.Z + increment.Z
            inter.x = Math.Round(nextpoint.x)
            inter.Y = Math.Round(nextpoint.Y)
            inter.Z = Math.Round(nextpoint.Z)
            If (IsOnCamera(inter, camera1.Position, camera1.Size) = True) Then
                GetInCamVector = inter
                Exit For
            End If
        Next i
    End Function
    
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
        If (KeyCode = vbKeyEscape) Then End
    End Sub
    
    Private Sub Form_Load()
        ScaleMode = vbPixels
        AutoRedraw = True
        FillPosition3D Player1.Position, Me.ScaleWidth / 2, Me.ScaleHeight / 2, 0 'Camera Position
        camera1.Size.Height = Me.ScaleHeight
        camera1.Size.Width = Me.ScaleWidth
        camera1.Position.x = -Me.ScaleWidth / 2 - 100
        camera1.Position.Y = -Me.ScaleHeight / 2 - 100
        camera1.Position.Z = 100
        camera1.Size.ZDepth = 20000
        camera1.Size.distance = 100
        ScrollChanged
    End Sub
    
    Sub ScrollChanged()
        Me.Cls
        'Angletest.X = HScroll1.Value
        'Angletest.Y = VScroll1.Value
        'Angletest.Z = HScroll2.Value
        
        Label1.Caption = Angletest.x & ", " & Angletest.Y & ", " & Angletest.Z
        Label1.Caption = Label1.Caption & vbNewLine & Player1.Position.x & ", " & Player1.Position.Y & ", " & Player1.Position.Z
        
        Dim RecPosition As position3D
        Dim RecPositiond As position3D
        Dim RecSize As Size3D
        FillPosition3D RecPosition, Player1.Position.x - 775, Player1.Position.Y, Player1.Position.Z
        FillSize3D RecSize, 800, 100, 2000
        DrawPlane RecPosition, RecSize, Angletest, camera1.Size
        
        'Line (camera1.Position.x, camera1.Position.Y)-Step(camera1.size.Width, camera1.size.Height), vbBlue, B
        Me.Refresh
    End Sub
    
    Private Sub Form_Resize()
        'ScaleMode = vbPixels
        'AutoRedraw = True
        FillPosition3D Player1.Position, Me.ScaleWidth / 2, Me.ScaleHeight / 2, 0 'Camera Position
        camera1.Size.Height = Me.ScaleHeight + 200
        camera1.Size.Width = Me.ScaleWidth
        camera1.Position.x = -Me.ScaleWidth / 2
        camera1.Position.Y = -Me.ScaleHeight / 2
        camera1.Position.Z = -100
        camera1.Size.ZDepth = 20000
        camera1.Size.distance = 100
        ScrollChanged
    End Sub
    
    Private Sub Timer1_Timer()
         Const Speed As Integer = 10
         Player1.MoveCoordenates = None
         If IsKeyDown(vbKeyEscape) = True Then
            End
         End If
         
         If IsKeyDown(vbKeyA) = True Then
            Angletest.Y = Angletest.Y + 1
            If (Angletest.Y >= 360) Then Angletest.Y = 0
            Player1.MoveCoordenates = Y
            ScrollChanged
        End If
        
        If IsKeyDown(vbKeyD) And KEY_DOWN Then
            Angletest.Y = Angletest.Y - 1
            If (Angletest.Y <= -360) Then Angletest.Y = 0
            Player1.MoveCoordenates = Y
            ScrollChanged
        End If
        
        If IsKeyDown(vbKeyLeft) And KEY_DOWN Then
            Player1.Position.x = Player1.Position.x + Speed
            Player1.MoveCoordenates = x
            ScrollChanged
        End If
        
        If IsKeyDown(vbKeyRight) And KEY_DOWN Then
            Player1.Position.x = Player1.Position.x - Speed
            Player1.MoveCoordenates = x
            ScrollChanged
        End If
        
        If IsKeyDown(vbKeyW) And KEY_DOWN Then
            Player1.Position.Y = Player1.Position.Y - Speed
            Player1.MoveCoordenates = Y
            ScrollChanged
        End If
        
        If IsKeyDown(vbKeyS) And KEY_DOWN Then
            Player1.Position.Y = Player1.Position.Y + Speed
            Player1.MoveCoordenates = Y
            ScrollChanged
        End If
        
        If IsKeyDown(vbKeyUp) And KEY_DOWN Then
            Player1.Position.Z = Player1.Position.Z - Speed
            Player1.Movement.Z = Player1.Movement.Z - Speed
            Player1.MoveCoordenates = Z
            ScrollChanged
        End If
        
        If IsKeyDown(vbKeyDown) And KEY_DOWN Then
            Player1.Position.Z = Player1.Position.Z + Speed
            Player1.Movement.Z = Player1.Movement.Z + Speed
            Player1.MoveCoordenates = Z
            ScrollChanged
        End If
    End Sub
    Last edited by joaquim; Jun 25th, 2020 at 03:26 PM.
    VB6 2D Sprite control

    To live is difficult, but we do it.

  5. #125

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,352

    Re: 3D Math: how draw a 3D cube?

    finally i fixed how i draw the plane:
    1 - get all four vectors and rotate them;
    2 - i test if the plane is drawed on camera, if not then exit the sub;
    3 - else i test the bottoms vectors for drawed inside the camera.
    4 - i draw them(here we can use the brush).
    Code:
    Private Sub DrawPlane(Position As Position3D, Size As Size3D, Rotation As Angle3D, WorldSize As Size3D)
        Dim Points(4) As POINTAPI
        Dim NewPoint As POINTAPI
        Dim NewPosition3D(4) As Position3D
        Dim RotatedPosition As Position3D
        
        'Rotate from player position:
        With Player1.Position
           FillPosition3D RotatedPosition, .X, .Y, .Z  'Camera Position
        End With
        
        'Get the four vectors
        
        'Floor:
        'Vector1
        FillPosition3D NewPosition3D(0), Position.X, Position.Y, Position.Z
        NewPosition3D(0) = Rotate(NewPosition3D(0), Rotation, RotatedPosition)
        
        
        'Vector2
        FillPosition3D NewPosition3D(1), Position.X, Position.Y, Position.Z + Size.ZDepth
        NewPosition3D(1) = Rotate(NewPosition3D(1), Rotation, RotatedPosition)
        
        'Vector3
        FillPosition3D NewPosition3D(2), Position.X + Size.Width, Position.Y, Position.Z + Size.ZDepth
        NewPosition3D(2) = Rotate(NewPosition3D(2), Rotation, RotatedPosition)
        
        
        'Vector4:
        'getting the wrong vector X :(
        FillPosition3D NewPosition3D(3), Position.X + Size.Width, Position.Y, Position.Z
        NewPosition3D(3) = Rotate(NewPosition3D(3), Rotation, RotatedPosition)
        
        'Testing if the plane is on screen:
            
        If ((IsOnCamera(NewPosition3D(0), camera1.Position, camera1.Size) = False) _
            And (IsOnCamera(NewPosition3D(1), camera1.Position, camera1.Size) = False) _
            And (IsOnCamera(NewPosition3D(2), camera1.Position, camera1.Size) = False) _
            And (IsOnCamera(NewPosition3D(3), camera1.Position, camera1.Size) = False)) Then
            Exit Sub
        End If
        
        If (IsOnCamera(NewPosition3D(0), camera1.Position, camera1.Size) = False) Then
            NewPosition3D(0) = GetInCamVector(NewPosition3D(0), NewPosition3D(1))
        End If
        
        If (IsOnCamera(NewPosition3D(3), camera1.Position, camera1.Size) = False) Then
            NewPosition3D(3) = NewPosition3D(0)
            NewPosition3D(3).X = NewPosition3D(2).X
        End If
        
        'Convert all 3D vectors to 2D vectors(for screen):
        Dim i As Integer
        For i = 0 To 3
            Points(i) = ConvertPositon3DTo2D(NewPosition3D(i), WorldSize)
        Next i
        
        'change the brush and pen(i belive the pen can be transparent):
        FillStyle = vbFSSolid
        FillColor = vbRed
        Polygon Me.hDC, Points(0), 4
    End Sub
    i will update these sub for get the brush and pen and then the angle3d will accept the rotation position.
    heres the only problem is the camera size and position...
    passel: please can you see my last topic for see the camera size and position?
    http://www.vbforums.com/showthread.p...he-camera-size
    if i click on max window button, the form will fill the screen area and the plane is drawed.. that's why i know that i have problems with camera size and position.
    thanks for all
    VB6 2D Sprite control

    To live is difficult, but we do it.

Page 4 of 4 FirstFirst 1234

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width