[VB6] - scrooling and following an object-VBForums

# Thread: [VB6] - scrooling and following an object

1. ## [VB6] - scrooling and following 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?

2. ## 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. ## Re: [VB6] - scrooling and following an object

Originally Posted by Jacob Roman
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)

4. ## 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.

5. ## 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:
`Public Sub Draw_Background_Map(Map As Map_Type, Red_Light As Long, Green_Light As Long, Blue_Light As Long, Fade As Long)     Dim X As Long, Y As Long    Dim I As Long    Dim X1 As Long, Y1 As Long    Dim X2 As Long, Y2 As Long    Dim Coordinates As Vector    Dim R As RECT    Dim Border_Size As Long    Dim Vertex_List(6) As TLVERTEX    Dim Color As Long        Border_Size = 0        R.Left = Border_Size    R.Top = Border_Size    R.Right = Fullscreen_Width - Border_Size    R.Bottom = Fullscreen_Height - Border_Size        If Red_Light <= 0 Then Red_Light = 0    If Green_Light <= 0 Then Green_Light = 0    If Blue_Light <= 0 Then Blue_Light = 0    If Fade <= 0 Then Fade = 0    If Red_Light >= 255 Then Red_Light = 255    If Green_Light >= 255 Then Green_Light = 255    If Blue_Light >= 255 Then Blue_Light = 255    If Fade >= 255 Then Fade = 255        Coordinates.X = Int(-(Map.Position.X) / TILE_WIDTH)    Coordinates.Y = Int(-(Map.Position.Y) / TILE_HEIGHT)        X1 = Coordinates.X    Y1 = Coordinates.Y    X2 = Coordinates.X + Map.Screen_Tile_Width    Y2 = Coordinates.Y + Map.Screen_Tile_Height        If X2 <= 0 Then X2 = 0    If Y2 <= 0 Then Y2 = 0    If Y2 >= Map.Height - 1 Then Y2 = Map.Height - 1    If X2 >= Map.Width - 1 Then X2 = Map.Width - 1    If X1 <= 0 Then X1 = 0    If Y1 <= 0 Then Y1 = 0    If X1 >= X2 Then X1 = X2    If Y1 >= Y2 Then Y1 = Y2        For Y = Y1 To Y2        For X = X1 To X2                        If Map.Collision_Map.Response(X, Y) = EFFECT_LIGHTNING Then                Color = D3DColorRGBA(Lightning, Lightning, Lightning, Fade)            Else                Color = D3DColorRGBA(Red_Light, Green_Light, Blue_Light, Fade)            End If                        If Map.Collision_Map.Response(X, Y) = COLLISION_LAVA Then                Color = D3DColorRGBA(255, 255, 255, Fade)            End If                        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)            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)            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)            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)             If Not ((Vertex_List(4).X < R.Left) Or (Vertex_List(0).X > R.Right) Or _               (Vertex_List(4).Y < R.Top) Or (Vertex_List(0).Y > R.Bottom)) Then                                Device.SetRenderState D3DRS_ALPHABLENDENABLE, True                Device.SetTexture 0, Map.Texture_List(Map.Background_Tile(X, Y))                Device.DrawPrimitiveUP D3DPT_TRIANGLESTRIP, 2, Vertex_List(0), Len(Vertex_List(0))            End If         Next X    Next Y    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:
`Private Sub Draw_Map()         Dim Coordinates As Vector 'Tile Coordinates of the position on map such as 5,10 or 24, 8        Dim X1 As Integer, Y1 As Integer, X2 As Integer, Y2 As Integer        Dim X As Integer, Y As Integer        Dim R As RECT         R.Left = 0        R.Top = 0        R.Right = Me.Width        R.Bottom = Me.Height         Coordinates.X = Int(-(Map.Position.X) / TILE_WIDTH)        Coordinates.Y = Int(-(Map.Position.Y) / TILE_HEIGHT) 'converts your position into tile coordinates         '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.         X1 = Coordinates.X        Y1 = Coordinates.Y        X2 = Coordinates.X + Map.Screen_Tile_Width        Y2 = Coordinates.Y + Map.Screen_Tile_Height         If X2 <= 0 Then X2 = 0        If Y2 <= 0 Then Y2 = 0        If Y2 >= Map.Height - 1 Then Y2 = Map.Height - 1        If X2 >= Map.Width - 1 Then X2 = Map.Width - 1        If X1 <= 0 Then X1 = 0        If Y1 <= 0 Then Y1 = 0        If X1 >= X2 Then X1 = X2        If Y1 >= Y2 Then Y1 = Y2         For Y = Y1 To Y2            For X = X1 To X2                 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                     Dim imageFile As Image = Image.FromFile(Map.Texture_List(Map.Tile(X, Y)))                    Dim g As Graphics = Me.CreateGraphics()                    Dim newGraphics As Graphics = Graphics.FromImage(imageFile)                    g.DrawImage(imageFile, New RectangleF(Map.Position.X + (TILE_WIDTH * X), Map.Position.Y + (TILE_HEIGHT * Y), TILE_WIDTH + 3, TILE_HEIGHT + 3))                    newGraphics.Dispose()                    g.Dispose()                 End If            Next X        Next Y     End Sub`

Only draw what you see.

6. ## Re: [VB6] - scrooling and following an object

Originally Posted by jcis
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.

7. ## 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
the values are negative

8. ## 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

10. ## Re: [VB6] - scrooling and following an object

Originally Posted by jcis
can i use a shared page?(i don't have space on my attachment)

11. ## 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. ## Re: [VB6] - scrooling and following an object

Originally Posted by jcis
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

13. ## 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. ## Re: [VB6] - scrooling and following an object

Originally Posted by jcis
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

15. ## 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. ## 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

17. ## Re: [VB6] - scrooling and following an object

Originally Posted by joaquim
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?
Originally Posted by joaquim
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).
Originally Posted by joaquim
like using scrollbars on ide: it's impossible, but i did it hehehe
Well, that's normal behavior for UserControls.

18. ## 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

#### 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