Frame Transitions

about 1 month

2024 11

Have you ever wondered how games transition from one frame to another?

When you have two scenes and you want to pass from one to another, what is the best way to do it?

For example, imagine that you have a menu, and you want to transition to the settings screen.
You can do something sweet and interpolate both images to create a smooth transition.
This can be implemented for anything you want, for example, if you die in the game, you can transition from the current scene to a dead screen menu. 

Linear Interpolation `lerp()`

The answer is "Linear Interpolation"
This allows us to interpolate two vectors to generate a new vector in the same -inf+ vector span of the two given vectors.

So we can considerate each pixel of the frame as a vector (Color RGBA) in this case you can use a Vector3 or Vector4 depending if you want to considerate the opacity.

Then all you need to do is to interpolate each corresponding vector/color from each frame. This can also be done with two different positions, to translate from one position to another by interpolating between the start and end.

In this case I implemented a super simple example using mlx.

See the Examples bellow.

Note:
There are two versions on the "Python/Ruby" transition, one does not consider the opacity (Vector3) and the other does consider the opacity (Vector4)

Here is the example if you want to check the code out (it's very small): https://github.com/pulgamecanica/42Course/tree/main/42Documentation/transitions