Smooth foreground/background scaling (a la Downhill Supreme)

General discussion about Box2D tutorials
Post Reply
therealcos
Posts: 16
Joined: Tue Aug 20, 2013 7:30 am

Smooth foreground/background scaling (a la Downhill Supreme)

Post by therealcos »

How did you create the smooth, dynamic scaling of both the foreground and background layers in Downhill Supreme?

I've already tried to do something similar with a project of my own by setting the scale of the each layer equal to the speed of a given body times a constant, but this has produced a really jittery result (i.e. the frame, which should be following the body, jumps around with every step). How did you achieve the effect of zooming in and out on both the foreground and background so smoothly in Downhill Supreme and how can I go about doing the same in my game?

P.S. - Also, I'm wondering (I can't really tell) if Downhill Supreme makes use of an effect that simulates a "slow camera" that appears to smoothly catch up to the position of the bike, but isn't constantly fixed perfectly on it. If so, how did you achieve this? I feel like the logic behind this sort of smooth movement might be related to my original question...
iforce2d
Site Admin
Posts: 861
Joined: Sat Dec 22, 2012 7:20 pm

Re: Smooth foreground/background scaling (a la Downhill Supr

Post by iforce2d »

Yes, it's a 'slow camera' type of method.

First a focus point is found. This is a point ahead of the rider that the player will need to see, in order to be have enough time to see what's coming up. This is done by casting ten rays down from 'heaven', to sample the area in front of the bike, as shown by the red lines in the screenshot here: viewtopic.php?f=4&t=200&p=783#p797 This sample area is moved forward and expanded a little depending on the current speed of the bike. The focus point is then restricted to a maximum distance away from the rider.

A layer scale is calculated such that when the view is centered on the focus point the rider will still be visible, and this new scale value is clamped between a min/max value. The layer scale is set to smoothly catch up to this new scale value.

The clamping may cause the rider to now be off-screen, so finally, the view is moved so that the rider is visible, also done by the smoothly catch up.

The smoothly catch up thing I usually do like this:

Code: Select all

float uptake = 0.05;
value = uptake * newValue + (1 - uptake) * currentValue;
... where 'value' can be the layer scale, or the view position etc. When 'uptake' is 1 the new value will be fully applied immediately, and smaller values apply it slower.

As for the background, I have no idea what's going on there but it looks ok so I left it that way. I didn't really like having no idea about it though, and as it happens just today I have been reworking this part of the game for DHS2. It will have a more typical parallax scrolling background this time.
therealcos
Posts: 16
Joined: Tue Aug 20, 2013 7:30 am

Re: Smooth foreground/background scaling (a la Downhill Supr

Post by therealcos »

Ahhh ray casting - genius!

I tried the 'uptake' equation for smooth zooming and it worked great! Thanks so much!
Post Reply