The last part is getting a little more complicated. Since smoothed movement isn't limited to one single frame we have to update the position frequently. I made another function called UpdatePosition which is called from the Main loop of the engine. The next problem is to slow-down movement. Our Main loop isn't timed and probably called 400 times per second, this would of course kill our smoothing. To prevent this I added a static variable that holds the time of the next allowed movement.
Finally the function calculates the distance between current and target position and calculates movement out of this. It also clips this speed component to a maximum. Since we only have the direction (angle) to the target we need to multiplicate the speed with Cos() and Sin() to get back the X and Y components of the direction.
Public Sub UpdatePosition() Dim Dist as Long Static NextMove as Long With Camera 'Timing If NextMove > GetTickCount Then: Exit Sub NextMove = GetTickCount + 20 'Update at 50 FPS 'Get distance Dist = Distance( CLng( .X ), CLng( .Y ), .DestX, .DestY ) If Dist = 0 Then: Exit Sub 'Limit speed If Dist > 128 Then: Dist = 128 'Move camera .X = .X + Cos( .Angle ) * ( Dist * 0.2 ) .Y = .Y + Sin( .Angle ) * ( Dist * 0.2 ) Clip End With End Sub
In this example I used 0.2 as the speed factor, this means in each call the camera moves 1/5 (which is 1 / 0.2) of the target distance towards the target. Note that one line before we clip the Dist variable to 128, well this should be called Speed here but I didn't want to make a extra variable for this. I'm telling you this because of course the distance can't be changed, but the speed can.
Smoothed camera movement surely is necessary in serious projects, however this was only a basic introduction to smoothing. Now that you know how the speed is controlled you could simply add some kind of acceleration, I recommend you to add a Speed variable to the camera Type to do this. However there's still loads of situations where this method fails, eg. when changing the direction while the camera is still moving would immediately change the direction instead of making a smooth curve. And here again you could just slow down or add Bezier curves to make it look even nicer. Anyways, the technique I showed you should suffice in most cases.
For those who are still interested in camera acceleration and soft target-changes I posted some example code on the next page. Without any explanation though.