# How do I determine parallax scroll factor?

Implementing parallax scrolling is easy enough. This question is about improving its presentation.

Hand-tuning some parallax scroll factors on multiple layers works, but parallax scrolling is meant to mimic a property of linear perspective--it has an underlying trigonometric relationship that I'm sure could benefit from the proper math.

Let's suppose my playfield has a parallax scroll factor 1.0, and a camera distance of 1.0. Given any other layer, how can we calculate its scroll factor from its camera distance, or its camera distance from its scroll factor?

There's no universal answer to this. The factor is essentially a function between your camera lens and the actual distance to something being observed.

However, assuming your typical projection would be linear this can be simplified to a rule of three:

factor = movement / distance


This leads to the conclusion that something at focus distance would have a factor of 1.0. Something at half that distance would result in a factor of 2.0, twice the distance a factor of 0.5, etc.

However, considering how this is always an extreme simplification (e.g. typically not using perspective projection/scaling), you should always pick numbers that feel good, not necessarily strict math. For example, more correct values might result in pixel art jumping irregularly due to precision/rounding errors, which might look worse than something moving too fast (by always whole pixels).

• I fully agree with the unknown in your answer. "rule of three", needs a reference, or better explanation.
– user122973
Feb 6, 2023 at 0:40

Parallax is an attempt to project prospective movement onto an orthographic surface. In other words, it attempts to place a perspective vanishing point that moves with the center of the screen.

True projective accuracy is not possible given the differences between orthographic and perspective views. As shown in green in the following image:

Note that the coordinates are relative meaning (0 to 1).

The projection error is zero at the the center of the screen, and increases outward.

It should be apparent from the graph that a setting sinusoidal parallax factor would minimize the projection error. Try arccos.

The same concept applies to the the vertical offset.

The speed becomes the derivative of the new factor's horizontal component + fudge_factor * the projection error.

As stated in another answer; There is no single perfect solution. Pick the values that appear best.

The distance to the vanishing point is completely arbitrary, and must be normalized prior to its use in arccos().

The application of similar triangles and other identities are required for formal geometrical proof, which I feel are unnecessary given the variance of the solutions possible. In this case an example image is worth one proof.

• I welcome any corrections, omissions, and the like.
– user122973
Feb 6, 2023 at 0:15