VBfx / Tile tutorial / Step 2

Type changes

Remember that we want smooth scrolling which means the camera position does not match the target position while moving. This simply brings us to new variables in the tCamera Type that hold the requested target position while X and Y still represent the current position. For speed reasons I also put the angle between the original and requested position in the Type, we'll see later why. Now here's the updated Type:

Public Type tCamera
    'Current position
    X as Single
    Y as Single
    
    'Requested position
    DestX as Long
    DestY as Long
    
    'Movement angle
    Angle as Single
    
    'Screen size
    w as Long 'W means width
    h as Long 'H means height
    
    'Number of tiles that fit on screen
    TilesX as Long
    TilesY as Long
    
    'Front buffer
    FrontDC as Long
End Type

Note that I also changed X and Y to Single values. This is because smooth movement may require to set positions between pixels (you won't notive that but it's really necessary).

LookAt again

First thing we have to do now is updating the LookAt function. I decided to add another parameter that tells the function wheter to use smooth moving or not. There are always situations where you want to move the camera without smoothing, especially when switching scenes. If smooth movement is requested the function now pre-calculates the angle between the current and target position. We can do this because the angle won't change in straight movement. As I said this is only for speed reasons, you could calculate the angle in each frame again of course.

Public Sub LookAt( iX as Long, iY as Long, iSmooth as Boolean )
    With Camera
        'Set requested position
        .DestX = iX
        .DestY = iY
        
        If iSmooth Then
            'Calculate new angle
            .Angle = GetAngle( .X, .Y, CSng( .DestX ), CSng( .DestY ) )
        
        Else
            'Set current position
            .X = iX
            .Y = iY
            
            'Check position
            Clip
        End If
    End With
End Sub

Note that I moved the boundaries-checks to the newly created Clip function. This was necessary because we will soon need clipping in other functions, too.

Navigation