Results 1 to 12 of 12

Thread: how to plot 3d

  1. #1

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755

    Arrow how to plot 3d

    Hi!

    I have the coordinates for a box and i want to plot it in 3d.

    If the camera is still and just a bit over the box so i can see the front and top, how do i calculate the positions of the coordinates?

    I just want to use some simple methods for plotting, like setpixel or line, not directx.
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  2. #2
    Addicted Member
    Join Date
    Aug 2002
    Posts
    192
    zip now contains 2 graphics dma 'bare-bones' projects
    Attached Files Attached Files
    Last edited by dafhi; Feb 28th, 2003 at 03:44 PM.

  3. #3

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    wow....good work! this was both fast and easy! but it wasnt the answer to this question



    in a 3d box when you look at the corners in the back of it....they seem to be closer to each other than they really are...the further back they are, the closer and smaller they appear to be! how do i calculate this?
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  4. #4
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    Truly simple:

    ScreenX = (X / Z) and
    ScreenY = (Y / Z)

    Example:

    VB Code:
    1. 'This is your point
    2. private Type tPoint3D
    3.     x as single
    4.     y as single
    5.     z as single
    6. end Type
    7.  
    8. 'This line type connects 2 points
    9. private Type tLine
    10.     p1 as Long
    11.     p2 as Long
    12. end Type
    13.  
    14. 'This will become a cube
    15. dim p(7) as tPoint3D
    16. dim l(11) as tLine
    17.  
    18. Private Sub Form_Load()
    19.     'Define the edge points
    20.  
    21.     'Top rect
    22.     with p(0): .x = -1: .y = -1: .z = 1: end with
    23.     with p(1): .x = 1: .y = -1: .z = 1: end with
    24.     with p(2): .x = 1: .y = 1: .z = 1: end with
    25.     with p(3): .x = -1: .y = 1: .z = 1: end with
    26.     'Bottom rect
    27.     with p(4): .x = -1: .y = -1: .z = -1: end with
    28.     with p(5): .x = 1: .y = -1: .z = -1: end with
    29.     with p(6): .x = 1: .y = 1: .z = -1: end with
    30.     with p(7): .x = -1: .y = 1: .z = -1: end with
    31.  
    32.     'Define the lines
    33.  
    34.     'Top rect
    35.     with l(0): .p1 = 0: .p2 = 1: end With
    36.     with l(1): .p1 = 1: .p2 = 2: end With
    37.     with l(2): .p1 = 2: .p2 = 3: end With
    38.     with l(3): .p1 = 3: .p2 = 0: end With
    39.     'Bottom rect
    40.     with l(4): .p1 = 4: .p2 = 5: end With
    41.     with l(5): .p1 = 5: .p2 = 6: end With
    42.     with l(6): .p1 = 6: .p2 = 7: end With
    43.     with l(7): .p1 = 7: .p2 = 4: end With
    44.     'Connectors
    45.     with l(8): .p1 = 0: .p2 = 4: end With
    46.     with l(9): .p1 = 1: .p2 = 5: end With
    47.     with l(10): .p1 = 2: .p2 = 6: end With
    48.     with l(11): .p1 = 3: .p2 = 7: end With
    49.  
    50.     'Setup the form for drawing
    51.     scaleMode = vbPixels
    52. End Sub
    53.  
    54. Public Sub Form_MouseDown(b As integer, s As integer, x As single, y As single)
    55.     'Draw the lines
    56.     dim A as Long
    57.  
    58.     'This will be screen coordinates
    59.     dim x1 as long
    60.     dim x2 as long
    61.  
    62.     dim y1 as long
    63.     dim y2 as long
    64.  
    65.     'This is the screen middle
    66.     dim tX as long
    67.     dim tY as long
    68.  
    69.     'Get screen middle
    70.     tX = (scaleWidth / 2)
    71.     tX = (scaleHeight / 2)
    72.  
    73.     'Since 3d positions are relative we need a size
    74.     Dim size As Single
    75.     size = 100
    76.  
    77.     'The camera says where to draw the cube
    78.     dim cam As tPoint3D
    79.  
    80.     'Move camera with mouse
    81.     cam.x = (x - tX) / size
    82.     cam.y = (y - tY) / size
    83.     cam.z = 5
    84.  
    85.     'FOV (field-of-view) corrects the stretching
    86.     dim fov as single
    87.     fov = 3
    88.  
    89.     me.Cls
    90.     for a = 0 to uBound(l)
    91.         'Get screen coordinates of both points
    92.         with p(l(a).p1)
    93.             x1 = (.x + cam.x) / (.z + cam.z) * size * fov
    94.             y1 = (.y + cam.y) / (.z + cam.z) * size * fov
    95.         end with
    96.         with p(l(a).p2)
    97.             x2 = (.x + cam.x) / (.z + cam.z) * size * fov
    98.             y2 = (.y + cam.y) / (.z + cam.z) * size * fov
    99.         end with
    100.  
    101.         'Draw line (origin at screen center)
    102.         Line (x1+tX, y1+tY)-(x2+tX, y2+tY)
    103.     next
    104. End Sub
    Last edited by Fox; Feb 26th, 2003 at 01:36 PM.

  5. #5

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    thanks alot!
    that was great!

    do you also know how to rotate the camera/box around different axis?
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  6. #6
    Fanatic Member petrus's Avatar
    Join Date
    May 2002
    Location
    pBytes[sizeof(pBytes)/2]
    Posts
    553
    Originally posted by cyborg
    thanks alot!
    that was great!

    do you also know how to rotate the camera/box around different axis?
    Snart vill du ha texturer också!
    ICQ: 128716725

  7. #7

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    hehe...nej då =)
    i just want to be able to draw some objects (stored in maps files(i can do the loading function by myself)) and then move around them how i want to....turn the mouse to look around and press the arrow keys to move around......no light, no textures or anything special...
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

  8. #8
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    Sure.. this is the formula for a z-rotation:

    newX = (x * Sin( angle )) + (y * Cos( angle ))
    newY = (x * Cos( angle )) - (y * Sin( angle ))

    And here's the modified draw function (get rest from first example) - changed parts are red:

    Code:
    Public Sub Form_MouseMove(b As Integer, s As Integer, x As Single, y As Single)
        'Draw the lines
        Dim A As Long
    
        'This will be screen coordinates
        Dim x1 As Long
        Dim x2 As Long
    
        Dim y1 As Long
        Dim y2 As Long
        
        'Temporary rotated points
        Dim p1 As tPoint3D
        Dim p2 As tPoint3D
        
        Dim r1 As tPoint3D
        Dim r2 As tPoint3D
        
        'This is the screen middle
        Dim tX As Long
        Dim tY As Long
    
        'Get screen middle
        tX = (ScaleWidth / 2)
        tX = (ScaleHeight / 2)
    
        'Since 3d positions are relative we need a size
        Dim size As Single
        size = 100
    
        'The camera says where to draw the cube
        Dim cam As tPoint3D
    
        'Move camera with mouse
        cam.x = (200 - tX) / size
        cam.y = (150 - tY) / size
        cam.z = 5
            
        'We also want a rotation
        Dim rot As Single
        
        'Rotate by mouse
        rot = x / 100
        
        'FOV (field-of-view) corrects the stretching
        Dim fov As Single
        fov = 3
    
        Me.Cls
        For A = 0 To UBound(l)
        
            'Get points
            p1 = p(l(A).p1)
            p2 = p(l(A).p2)
            
            'Rotate the points
            With p1
                r1.x = (.x * Sin(rot)) + (.y * Cos(rot))
                r1.y = (.x * Cos(rot)) - (.y * Sin(rot))
                r1.z = p1.z
            End With
            
            With p2
                r2.x = (.x * Sin(rot)) + (.y * Cos(rot))
                r2.y = (.x * Cos(rot)) - (.y * Sin(rot))
                r2.z = p2.z
            End With
             
            'Get screen coordinates of both points
            With r1 
                x1 = (.x + cam.x) / (.z + cam.z) * size * fov
                y1 = (.y + cam.y) / (.z + cam.z) * size * fov
            End With
            With r2 
                x2 = (.x + cam.x) / (.z + cam.z) * size * fov
                y2 = (.y + cam.y) / (.z + cam.z) * size * fov
            End With
               
            'Draw line (origin at screen center)
            Line (x1 + tX, y1 + tY)-(x2 + tX, y2 + tY)
        Next
    End Sub
    Last edited by Fox; Feb 28th, 2003 at 11:29 AM.

  9. #9
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    *reads question again* umm for different axes? Don't know this by hard, let my find a older project..


    here we are, i'm sure you can work out this snippet:

    VB Code:
    1. 'Types
    2.     Public Type tPosition
    3.         'Position info
    4.         x As Double
    5.         y As Double
    6.         z As Double
    7.     End Type
    8.    
    9.     Public Type tRotation
    10.         'Rotation info
    11.         rX As Double
    12.         rY As Double
    13.         rZ As Double
    14.        
    15.         'Lookup values
    16.         SinX As Double
    17.         CosX As Double
    18.        
    19.         SinY As Double
    20.         CosY As Double
    21.        
    22.         SinZ As Double
    23.         CosZ As Double
    24.     End Type
    25.  
    26. Public Function RotatePosition(iPosition As tPosition, iRotation As tRotation) As tPosition
    27.     Dim TempX As Double
    28.     Dim TempY As Double
    29.     Dim TempZ As Double
    30.    
    31.     With iRotation
    32.         'Calculate coordinates
    33.         TempX = .CosZ * iPosition.x
    34.         TempY = .SinZ * iPosition.y
    35.         TempZ = .CosY * iPosition.z
    36.        
    37.         Dim Temp As Double
    38.         Temp = iPosition.y * .CosZ + iPosition.x * .SinZ
    39.        
    40.         'Get position
    41.         RotatePosition.x = (.CosY * (TempX - TempY) + .SinY * iPosition.z)
    42.         RotatePosition.y = (.SinX * (.SinY * TempX - .SinY * TempY - TempZ) + .CosX * Temp)
    43.         RotatePosition.z = (.CosX * (-.SinY * TempX + .SinY * TempY + TempZ) + .SinX * Temp)
    44.     End With
    45. End Function

  10. #10
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    Re: how to plot 3d

    You have to rotate around each axis one after the other. The order is VERY important for this!!

    I use 'pitch' and 'rotation' when I do mine. I rotate by pitch, then by rotation. (Look up/down then turn around)
    Don't pay attention to this signature, it's contradictory.

  11. #11
    Fanatic Member damasterjo's Avatar
    Join Date
    Nov 2005
    Location
    In front of my Comp DirectX7 EXpert
    Posts
    827

    Re: how to plot 3d

    this thread was asked in 2003
    Software languages known:
    Qbasic - TI-Basic - Liberty Basic - Visual Basic 6
    Software API's known:
    Directx 7 and 8
    Internet languages, in the process of learning:
    HTML - JAVASCRIPT - PHP - CSS - MYSQL - AJAX

  12. #12
    Fanatic Member alkatran's Avatar
    Join Date
    Apr 2002
    Location
    Canada
    Posts
    860

    Re: how to plot 3d

    Quote Originally Posted by damasterjo
    this thread was asked in 2003
    Bah, I hate it when I forget I'm inside a search box! There needs to be a warning for stupid people like me.
    Don't pay attention to this signature, it's contradictory.

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