[VB6] - scrooling and following an object-VBForums
Results 1 to 18 of 18

Thread: [VB6] - scrooling and following an object

  1. #1

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    2,916

    [VB6] - scrooling and following an object

    i need some advices on how scrooling and follow an object.
    like any games we have the level size. i can scroll the level very easy.
    but i need an automatic scrooling by fallowing an object. i belive, than on some games, we have a "rectangule" for control the object position and the scrool. but my ideia is only theory. but can anyone give me more information?
    VB6 2D Sprite control

    To live is difficult, but we do it.

  2. #2
    The DirectXpert Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,073

    Re: [VB6] - scrooling and following an object

    It seems vague what you are asking but the background and sprite you must bare in mind are totally independent of each other, just like in 3D where the camera is totally independent of the 3D object. So if you were to have the background "follow" the sprite, you gotta think "will the sprite be moving towards the center of the screen with the background moving as well?" If thats the case, it all just involves a little vector math and linear interpolation. Now if you mean the background moving with the sprite, then think of it this way. The sprite isn't even physically moving technically as the background moves. Maybe in world space but not local space. Like a spaceship shooter without the movement from the joystick / keyboard. The background scrolls but the sprite remains in the same place, which gives it that background "following" the object effect. Remember the camera and sprite are both independent, regardless in 2D or 3D.

  3. #3

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    2,916

    Re: [VB6] - scrooling and following an object

    Quote Originally Posted by Jacob Roman View Post
    It seems vague what you are asking but the background and sprite you must bare in mind are totally independent of each other, just like in 3D where the camera is totally independent of the 3D object. So if you were to have the background "follow" the sprite, you gotta think "will the sprite be moving towards the center of the screen with the background moving as well?" If thats the case, it all just involves a little vector math and linear interpolation. Now if you mean the background moving with the sprite, then think of it this way. The sprite isn't even physically moving technically as the background moves. Maybe in world space but not local space. Like a spaceship shooter without the movement from the joystick / keyboard. The background scrolls but the sprite remains in the same place, which gives it that background "following" the object effect. Remember the camera and sprite are both independent, regardless in 2D or 3D.
    sorry. but i'm loking for a basic information.
    i'm creating a Level Editor 2D. like the name says, these control is for create the levels. the level size can be more big than control size(that's normal).
    and i think that the best choice is testing if the player is on center of control and then testing is movement for scrool the level. right?
    (i just need some general information for complete my ideia)
    VB6 2D Sprite control

    To live is difficult, but we do it.

  4. #4
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,409

    Re: [VB6] - scrooling and following an object

    One of the most common ways to perform scrolling is having the player always on the center, when the user press left the background is scrolled right, when the user press right the background is scrolled left, the same for up and down, always the opposite, but the player is always in the center and just the background is moved. Racing games top view, Soccer games top view, platform games side view, they all use this approach... well with some exceptions like in Mario when Mario jumps.
    Last edited by jcis; Mar 1st, 2012 at 12:02 AM.

  5. #5
    The DirectXpert Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,073

    Re: [VB6] - scrooling and following an object

    I created a level editor for an rpg before that allows the level to be as huge as you want with zero slowdown, even if it had a million by million tiles!. And how I did it was very simple. As I display the graphics I only loop through what the user sees:

    Code:
    For Y = Y1 To Y2
    For X = X1 To X2
    ...
    Next X
    Next Y
    Where X1 and X2 are the left and right of whatever is displayed and visible and Y1 and Y2 are the the top and bottom of whatever is displayed and visible. Its similar to a rectangular magnifine glass over a gigantic map. You don't need to loop through the entire map. Just that little rectangle. Heres a small sample from my code using DX. The Screen tile width and heights are just however many tiles are visible on the screen at once with that row such as 32 x 24

    vb Code:
    1. Public Sub Draw_Background_Map(Map As Map_Type, Red_Light As Long, Green_Light As Long, Blue_Light As Long, Fade As Long)
    2.  
    3.     Dim X As Long, Y As Long
    4.     Dim I As Long
    5.     Dim X1 As Long, Y1 As Long
    6.     Dim X2 As Long, Y2 As Long
    7.     Dim Coordinates As Vector
    8.     Dim R As RECT
    9.     Dim Border_Size As Long
    10.     Dim Vertex_List(6) As TLVERTEX
    11.     Dim Color As Long
    12.    
    13.     Border_Size = 0
    14.    
    15.     R.Left = Border_Size
    16.     R.Top = Border_Size
    17.     R.Right = Fullscreen_Width - Border_Size
    18.     R.Bottom = Fullscreen_Height - Border_Size
    19.    
    20.     If Red_Light <= 0 Then Red_Light = 0
    21.     If Green_Light <= 0 Then Green_Light = 0
    22.     If Blue_Light <= 0 Then Blue_Light = 0
    23.     If Fade <= 0 Then Fade = 0
    24.     If Red_Light >= 255 Then Red_Light = 255
    25.     If Green_Light >= 255 Then Green_Light = 255
    26.     If Blue_Light >= 255 Then Blue_Light = 255
    27.     If Fade >= 255 Then Fade = 255
    28.    
    29.     Coordinates.X = Int(-(Map.Position.X) / TILE_WIDTH)
    30.     Coordinates.Y = Int(-(Map.Position.Y) / TILE_HEIGHT)
    31.    
    32.     X1 = Coordinates.X
    33.     Y1 = Coordinates.Y
    34.     X2 = Coordinates.X + Map.Screen_Tile_Width
    35.     Y2 = Coordinates.Y + Map.Screen_Tile_Height
    36.    
    37.     If X2 <= 0 Then X2 = 0
    38.     If Y2 <= 0 Then Y2 = 0
    39.     If Y2 >= Map.Height - 1 Then Y2 = Map.Height - 1
    40.     If X2 >= Map.Width - 1 Then X2 = Map.Width - 1
    41.     If X1 <= 0 Then X1 = 0
    42.     If Y1 <= 0 Then Y1 = 0
    43.     If X1 >= X2 Then X1 = X2
    44.     If Y1 >= Y2 Then Y1 = Y2
    45.    
    46.     For Y = Y1 To Y2
    47.         For X = X1 To X2
    48.            
    49.             If Map.Collision_Map.Response(X, Y) = EFFECT_LIGHTNING Then
    50.                 Color = D3DColorRGBA(Lightning, Lightning, Lightning, Fade)
    51.             Else
    52.                 Color = D3DColorRGBA(Red_Light, Green_Light, Blue_Light, Fade)
    53.             End If
    54.            
    55.             If Map.Collision_Map.Response(X, Y) = COLLISION_LAVA Then
    56.                 Color = D3DColorRGBA(255, 255, 255, Fade)
    57.             End If
    58.            
    59.             Vertex_List(0) = Create_TLVertex(Map.Position.X + ((TILE_WIDTH * X) + 0), Map.Position.Y + ((TILE_HEIGHT * Y) + 0), 0, 1, Color, 0, 0, 0)
    60.             Vertex_List(1) = Create_TLVertex(Map.Position.X + ((TILE_WIDTH * X) + TILE_WIDTH), Map.Position.Y + ((TILE_HEIGHT * Y) + 0), 0, 1, Color, 0, 1, 0)
    61.             Vertex_List(2) = Create_TLVertex(Map.Position.X + ((TILE_WIDTH * X) + 0), Map.Position.Y + ((TILE_HEIGHT * Y) + TILE_HEIGHT), 0, 1, Color, 0, 0, 1)
    62.             Vertex_List(3) = Create_TLVertex(Map.Position.X + ((TILE_WIDTH * X) + TILE_WIDTH), Map.Position.Y + ((TILE_HEIGHT * Y) + TILE_HEIGHT), 0, 1, Color, 0, 1, 1)
    63.  
    64.             If Not ((Vertex_List(4).X < R.Left) Or (Vertex_List(0).X > R.Right) Or _
    65.                (Vertex_List(4).Y < R.Top) Or (Vertex_List(0).Y > R.Bottom)) Then
    66.                
    67.                 Device.SetRenderState D3DRS_ALPHABLENDENABLE, True
    68.                 Device.SetTexture 0, Map.Texture_List(Map.Background_Tile(X, Y))
    69.                 Device.DrawPrimitiveUP D3DPT_TRIANGLESTRIP, 2, Vertex_List(0), Len(Vertex_List(0))
    70.             End If
    71.  
    72.         Next X
    73.     Next Y
    74.    
    75. End Sub

    Heres a sample in VB.Net using pure VB.Net. Its just as fast believe it or not using no DX:

    vb.net Code:
    1. Private Sub Draw_Map()
    2.  
    3.         Dim Coordinates As Vector 'Tile Coordinates of the position on map such as 5,10 or 24, 8
    4.         Dim X1 As Integer, Y1 As Integer, X2 As Integer, Y2 As Integer
    5.         Dim X As Integer, Y As Integer
    6.         Dim R As RECT
    7.  
    8.         R.Left = 0
    9.         R.Top = 0
    10.         R.Right = Me.Width
    11.         R.Bottom = Me.Height
    12.  
    13.         Coordinates.X = Int(-(Map.Position.X) / TILE_WIDTH)
    14.         Coordinates.Y = Int(-(Map.Position.Y) / TILE_HEIGHT) 'converts your position into tile coordinates
    15.  
    16.         'This here allows the world to be as gigantic as you want with zero slow down like 5000x5000 for example. Only draws what is on screen.
    17.  
    18.         X1 = Coordinates.X
    19.         Y1 = Coordinates.Y
    20.         X2 = Coordinates.X + Map.Screen_Tile_Width
    21.         Y2 = Coordinates.Y + Map.Screen_Tile_Height
    22.  
    23.         If X2 <= 0 Then X2 = 0
    24.         If Y2 <= 0 Then Y2 = 0
    25.         If Y2 >= Map.Height - 1 Then Y2 = Map.Height - 1
    26.         If X2 >= Map.Width - 1 Then X2 = Map.Width - 1
    27.         If X1 <= 0 Then X1 = 0
    28.         If Y1 <= 0 Then Y1 = 0
    29.         If X1 >= X2 Then X1 = X2
    30.         If Y1 >= Y2 Then Y1 = Y2
    31.  
    32.         For Y = Y1 To Y2
    33.             For X = X1 To X2
    34.  
    35.                 If Not ((Map.Position.X + ((TILE_WIDTH * X) + TILE_WIDTH) < R.Left) Or (Map.Position.X + ((TILE_WIDTH * X)) > R.Right) Or (Map.Position.Y + ((TILE_HEIGHT * Y) + TILE_HEIGHT) < R.Top) Or (Map.Position.Y + ((TILE_HEIGHT * Y)) > R.Bottom)) Then
    36.  
    37.                     Dim imageFile As Image = Image.FromFile(Map.Texture_List(Map.Tile(X, Y)))
    38.                     Dim g As Graphics = Me.CreateGraphics()
    39.                     Dim newGraphics As Graphics = Graphics.FromImage(imageFile)
    40.                     g.DrawImage(imageFile, New RectangleF(Map.Position.X + (TILE_WIDTH * X), Map.Position.Y + (TILE_HEIGHT * Y), TILE_WIDTH + 3, TILE_HEIGHT + 3))
    41.                     newGraphics.Dispose()
    42.                     g.Dispose()
    43.  
    44.                 End If
    45.             Next X
    46.         Next Y
    47.  
    48.     End Sub

    Only draw what you see.

  6. #6

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    2,916

    Re: [VB6] - scrooling and following an object

    Quote Originally Posted by jcis View Post
    One of the most common ways to perform scrolling is having the player always on the center, when the user press left the background is scrolled right, when the user press right the background is scrolled left, the same for up and down, always the opposite, but the player is always in the center and just the background is moved. Racing games top view, Soccer games top view, platform games side view, they all use this approach... well with some exceptions like in Mario when Mario jumps.
    thanks for that information, is the general information, that i need.
    and if the player moves too?
    (my level editor control can scroll, making all controls move on same time with a for..to..next)
    thanks
    jacob thanks for the help too. but what i needed was these information.
    sorry if i wasn't good on my english.
    VB6 2D Sprite control

    To live is difficult, but we do it.

  7. #7

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    2,916

    Re: [VB6] - scrooling and following an object

    now i'm putting these option on pratic
    Code:
    Private Sub tmrScroolObject_Timer()
        ScrollingVertical.Value = UserControl.Controls(strObjectScroll).Top / 2 - (UserControl.Height / 2)
        ScrollingHorizontal.Value = UserControl.Controls(strObjectScroll).Left / 2 - (UserControl.Width / 2)
    End Sub
    my problem is the error message:
    "Run-time '30': Invalid property value"
    my objective is putting the UserControl.Controls(strObjectScroll) on center of usercontrol. but if the scrollbars are in max isn't needed
    can anyone advice me about the error message?
    the values are negative
    VB6 2D Sprite control

    To live is difficult, but we do it.

  8. #8

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    2,916

    Re: [VB6] - scrooling and following an object

    and now i did the code:
    Code:
    Private Sub tmrScroolObject_Timer()
        Dim lngDiference As Long
        If Ambient.UserMode = False Then tmrScroolObject.Enabled = False
        If strObjectScroll = Empty Then
            tmrScroolObject.Enabled = False
            Exit Sub
        End If
        
        'when the control move to down
        If UserControl.Parent.Controls(strObjectScroll).Top > lngOldScrollObjectY Then
            lngDiference = (UserControl.Parent.Controls(strObjectScroll).Top - lngOldScrollObjectY) / 16
            If lngDiference >= 0 Then
                If ScrollingVertical.Value + lngDiference >= ScrollingVertical.Max Then
                    ScrollingVertical.Value = ScrollingVertical.Max
                Else
                    ScrollingVertical.Value = ScrollingVertical.Value + lngDiference
                End If
            End If
            lngOldScrollObjectY = UserControl.Parent.Controls(strObjectScroll).Top
            
        ElseIf UserControl.Parent.Controls(strObjectScroll).Top < lngOldScrollObjectY Then
            'when the control move to up
            lngDiference = (UserControl.Parent.Controls(strObjectScroll).Top + lngOldScrollObjectY) / 16
            If lngDiference >= 0 Then
                If ScrollingVertical.Value - lngDiference <= ScrollingVertical.Min Then
                    ScrollingVertical.Value = ScrollingVertical.Min
                Else
                    ScrollingVertical.Value = ScrollingVertical.Value - lngDiference
                End If
            End If
            lngOldScrollObjectY = UserControl.Parent.Controls(strObjectScroll).Top
            
        ElseIf UserControl.Parent.Controls(strObjectScroll).Left > lngOldScrollObjectX Then
            'when the control move to right
            lngDiference = (UserControl.Parent.Controls(strObjectScroll).Left - lngOldScrollObjectX) / 16
            If lngDiference >= 0 Then
                If ScrollingHorizontal.Value - lngDiference >= ScrollingHorizontal.Max Then
                    ScrollingHorizontal.Value = ScrollingHorizontal.Max
                Else
                    ScrollingHorizontal.Value = ScrollingHorizontal.Value + lngDiference
                End If
            End If
            lngOldScrollObjectX = UserControl.Parent.Controls(strObjectScroll).Left
            
        ElseIf UserControl.Parent.Controls(strObjectScroll).Left < lngOldScrollObjectX Then
            'when the control move to left
            lngDiference = (UserControl.Parent.Controls(strObjectScroll).Left + lngOldScrollObjectX) / 16
            If lngDiference >= 0 Then
                If ScrollingHorizontal.Value - lngDiference <= ScrollingHorizontal.Min Then
                    ScrollingHorizontal.Value = ScrollingHorizontal.Min
                Else
                    ScrollingHorizontal.Value = ScrollingHorizontal.Value - lngDiference
                End If
            End If
            lngOldScrollObjectX = UserControl.Parent.Controls(strObjectScroll).Left
        End If
    End Sub
    but i can see 1 logical bug because i see 2 movements the control and then the scrollbar making like a "vibration" style
    can anyone advice me?
    VB6 2D Sprite control

    To live is difficult, but we do it.

  9. #9
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,409

    Re: [VB6] - scrooling and following an object

    Can you upload your project? I can't find the problem just by looking at your code.

  10. #10

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    2,916

    Re: [VB6] - scrooling and following an object

    Quote Originally Posted by jcis View Post
    Can you upload your project? I can't find the problem just by looking at your code.
    can i use a shared page?(i don't have space on my attachment)
    VB6 2D Sprite control

    To live is difficult, but we do it.

  11. #11
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,409

    Re: [VB6] - scrooling and following an object

    www.mediafire.com works pretty well for temporary storage, i think you don't even need registration.

  12. #12

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    2,916

    Re: [VB6] - scrooling and following an object

    Quote Originally Posted by jcis View Post
    www.mediafire.com works pretty well for temporary storage, i think you don't even need registration.
    sorry i can't use it. but i can use these:
    http://www.2shared.com/file/iW0hxaMf...el_Editor.html
    enter on groupproject file. if you find any error message, please tell me. thanks
    -you can scroll on ide, but the values are saved, then put the scroll on zero(both);
    -in executed mode, use the directionals keys(for you test, use the down key for a while and then use the up key for you see the error
    thanks for the help
    VB6 2D Sprite control

    To live is difficult, but we do it.

  13. #13
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,409

    Re: [VB6] - scrooling and following an object

    You need a gameloop or a timer for your game, you shouldn't use Form events for this like Picture1_KeyDown, that's why you see that vibration. Also, you really need those scrollbars? Games doesn't have scrollbars.

  14. #14

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    2,916

    Re: [VB6] - scrooling and following an object

    Quote Originally Posted by jcis View Post
    You need a gameloop or a timer for your game, you shouldn't use Form events for this like Picture1_KeyDown, that's why you see that vibration. Also, you really need those scrollbars? Games doesn't have scrollbars.
    yes i need, in these case
    if you have advices for correct my calculation, please tell me my friend
    VB6 2D Sprite control

    To live is difficult, but we do it.

  15. #15
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,409

    Re: [VB6] - scrooling and following an object

    Here you can see a GameLoop, this is a pong style game i did some time ago:Arkanoid

  16. #16

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    2,916

    Re: [VB6] - scrooling and following an object

    i did 1 nice game with Form events way, it's very nice
    what is the diference from your game?
    1 - is more easy to complete\code
    2 - i did the 2D Sprite control(is for show the images, collision detection, the key down don't have that delay, mouse events(leave enter ang whell), joystick and some nice graphic effects and can read animated gif's too.... very cool
    i want finish my Level Editor. if you have some advice for correct my calculatio, please tell me... i will find the solution, i always do hehehe
    like using scrollbars on ide: it's impossible, but i did it hehehe
    thanks for your time
    VB6 2D Sprite control

    To live is difficult, but we do it.

  17. #17
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,409

    Re: [VB6] - scrooling and following an object

    Quote Originally Posted by joaquim View Post
    2 - i did the 2D Sprite control(is for show the images, collision detection, the key down don't have that delay, mouse events(leave enter ang whell), joystick and some nice graphic effects and can read animated gif's too.... very cool
    If you already made it before why you can't do it now?
    I don't see this working in the project you uploaded. I don't see any collision or anything like that, maybe you changed the picture you were using?
    Quote Originally Posted by joaquim View Post
    i want finish my Level Editor. if you have some advice for correct my calculatio, please tell me...
    The vibration you see is due to the key_down event, use a Timer in your Form to fix it. About your calculation what's not working? the error is because the scrollbar value goes beyond the max value (38).
    Quote Originally Posted by joaquim View Post
    like using scrollbars on ide: it's impossible, but i did it hehehe
    Well, that's normal behavior for UserControls.
    Last edited by jcis; Apr 26th, 2012 at 05:44 PM.

  18. #18

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    2,916

    Re: [VB6] - scrooling and following an object

    90% of code is working fine hehehe
    Code:
    Private Sub tmrScroolObject_Timer()
        Dim lngDiference As Long
        If Ambient.UserMode = False Then tmrScroolObject.Enabled = False
        If strObjectScroll = Empty Then
            tmrScroolObject.Enabled = False
            Exit Sub
        End If
        
        'when the control move to down
        If UserControl.Parent.Controls(strObjectScroll).Top > lngOldScrollObjectY Then
            lngDiference = (UserControl.Parent.Controls(strObjectScroll).Top - lngOldScrollObjectY) / 16
            If lngDiference >= 0 Then
                If ScrollingVertical.Value + lngDiference >= ScrollingVertical.Max Then
                    ScrollingVertical.Value = ScrollingVertical.Max
                Else
                    ScrollingVertical.Value = ScrollingVertical.Value + lngDiference
                End If
            End If
            lngOldScrollObjectY = UserControl.Parent.Controls(strObjectScroll).Top
            
        ElseIf UserControl.Parent.Controls(strObjectScroll).Top < lngOldScrollObjectY Then
            'when the control move to up
            lngDiference = (lngOldScrollObjectY - UserControl.Parent.Controls(strObjectScroll).Top) / 16
            If lngDiference >= 0 Then
                If ScrollingVertical.Value - lngDiference <= ScrollingVertical.Min Then
                    ScrollingVertical.Value = ScrollingVertical.Min
                Else
                    ScrollingVertical.Value = ScrollingVertical.Value - lngDiference
                End If
            End If
            lngOldScrollObjectY = UserControl.Parent.Controls(strObjectScroll).Top
            
        ElseIf UserControl.Parent.Controls(strObjectScroll).Left > lngOldScrollObjectX Then
            'when the control move to right
            lngDiference = (UserControl.Parent.Controls(strObjectScroll).Left - lngOldScrollObjectX) / 16
            If lngDiference >= 0 Then
                If ScrollingHorizontal.Value + lngDiference >= ScrollingHorizontal.Max Then
                    ScrollingHorizontal.Value = ScrollingHorizontal.Max
                Else
                    ScrollingHorizontal.Value = ScrollingHorizontal.Value + lngDiference
                End If
            End If
            lngOldScrollObjectX = UserControl.Parent.Controls(strObjectScroll).Left
            
        ElseIf UserControl.Parent.Controls(strObjectScroll).Left < lngOldScrollObjectX Then
            'when the control move to left
            lngDiference = (lngOldScrollObjectX - UserControl.Parent.Controls(strObjectScroll).Left) / 16
            If lngDiference >= 0 Then
                If ScrollingHorizontal.Value - lngDiference <= ScrollingHorizontal.Min Then
                    ScrollingHorizontal.Value = ScrollingHorizontal.Min
                Else
                    ScrollingHorizontal.Value = ScrollingHorizontal.Value - lngDiference
                End If
            End If
            lngOldScrollObjectX = UserControl.Parent.Controls(strObjectScroll).Left
        End If
    End Sub
    and where is other 10%? when in time i will make it better and show the control on middel of my usercontrol... very cool
    VB6 2D Sprite control

    To live is difficult, but we do it.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.