Moving the game world
I've made 2D game world for diablo2 like game and for moving the
camera around I'm using two different methods:
The 1st method is activating when the mouse position is near the
borders of the screen, the 2nd method is activating when the middle
mouse button is down and the mouse is moving.
They look something like this:
I was surprized to notice that the second method is performing much more faster and
smoother than the first method that is placed inside the game loop.
Why both methods are performing so different. My primary goal is to move the camera
only when the cursor is near the screen borders - do you have any ideas how to make the
first method to perform as fast as the second one? Thank you.
I've also made a little video clip to illustrate the problem:
http://youtu.be/NcWNENEUgfI
If you need the source files I'll upload them too.
I've made 2D game world for diablo2 like game and for moving the
camera around I'm using two different methods:
The 1st method is activating when the mouse position is near the
borders of the screen, the 2nd method is activating when the middle
mouse button is down and the mouse is moving.
They look something like this:
'1st method:
Private Sub GameLoop()
Do While Me.Disposing = False And Me.IsDisposed = False And Me.Visible = True
'keep app responsive:
Application.DoEvents()
'change camera position:
If Control.MousePosition.X < 5 Then SetCameraPosX(False)
If Control.MousePosition.X > (Me.Width - 5) Then SetCameraPosX(True)
If Control.MousePosition.Y < 5 Then SetCameraPosY(False)
If Control.MousePosition.Y > (Me.Height - 5) Then SetCameraPosY(True)
'draw graphics:
DrawBackBuffer()
Loop
End Sub
Public Sub SetCameraPosX(ByVal booGrow As Boolean)
Select Case booGrow
Case True 'increase x position for camera
If (ptrCameraPos.X + My.Computer.Screen.Bounds.Width) < (szeWorldSize.Width - intWrldLimit) Then ptrCameraPos.X += intWrldSpeed
Case False 'decrease x position for camera
If ptrCameraPos.X > intWrldLimit Then ptrCameraPos.X -= intWrldSpeed
End Select
End Sub
Public Sub SetCameraPosY(ByVal booGrow As Boolean)
Select Case booGrow
Case True 'increase y position for camera
If (ptrCameraPos.Y + My.Computer.Screen.Bounds.Height) < (szeWorldSize.Height - 150) Then ptrCameraPos.Y += intWrldSpeed
Case False 'decrease y position for camera
If ptrCameraPos.Y > intWrldLimit Then ptrCameraPos.Y -= intWrldSpeed
End Select
End Sub
'2nd method:
If e.Button = Windows.Forms.MouseButtons.Middle Then
ptrCameraPos -= (ptrMousePos - ptrP)'move camera by holding the mouse down
End If
Private Sub frmGraphicsDevice_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
If e.Button = Windows.Forms.MouseButtons.Middle then ptrP = ptrMousePos
End Sub
I was surprized to notice that the second method is performing much more faster and
smoother than the first method that is placed inside the game loop.
Why both methods are performing so different. My primary goal is to move the camera
only when the cursor is near the screen borders - do you have any ideas how to make the
first method to perform as fast as the second one? Thank you.
I've also made a little video clip to illustrate the problem:
http://youtu.be/NcWNENEUgfI
If you need the source files I'll upload them too.