Interpolation: Linear interpolation, Inverse Lerp, Remap, Slerp - Gr8-Tools/game-developer-roadmap-doc GitHub Wiki
8 hours.
Interpolation is a mathematical technique used in game development to estimate values between two known points, based on the assumption that the data between those points is smooth and continuous. It is commonly used to create smooth transitions and animations, as well as to calculate values based on limited input data. One of the most commonly used interpolation methods is linear interpolation, also known as lerp. In addition to linear interpolation, inverse lerp and remap are also important concepts used in game development.
Subtopics and Definitions
-
Linear Interpolation (LERP): a method that calculates a value between two known points based on a linear function. It assumes that the data between the two points forms a straight line. Lerp takes two input values and a weight value between 0 and 1, and returns a value that is linearly interpolated between the two input values. It is expressed as
lerp(a, b, t) = a + (b - a) * t
, wheret
is the weight value. -
Inverse LERP (also known as "normalized linear interpolation"): the opposite of linear interpolation. Instead of calculating a value between two known points, it calculates the weight value needed to achieve a given value between two known points. Inverse lerp takes three input values: the start point, the end point, and the value to be interpolated. It returns the weight value between 0 and 1 that represents the position of the given value between the start and end points.
-
Remap: a technique used to convert a value from one range to another. It is commonly used in game development for mapping input values (such as mouse position, controller input, or audio levels) from one range to another for more convenient or meaningful usage. Remap takes an input value, the input range (minimum and maximum), and the output range (minimum and maximum), and returns the remapped value. It can be expressed as
remap(value, inputMin, inputMax, outputMin, outputMax) = (value - inputMin) * (outputMax - outputMin) / (inputMax - inputMin) + outputMin
. -
Spherical Linear Interpolation (SLERP): a type of interpolation commonly used for rotations in 3D space. Unlike linear interpolation, which moves along a straight line, slerp ensures a smooth rotation path by moving along the shortest arc on the surface of a sphere. This is particularly useful when interpolating between two orientations, as it maintains a constant angular velocity and avoids undesirable artifacts like gimbal lock.
- Quaternion Slerp is a commonly used implementation of SLERP in game development. It takes two quaternions as input and returns a new quaternion representing the intermediate rotation between them.
Interpolation techniques, such as linear interpolation, inverse LERP, remap, and SLERP, provide powerful tools for creating smooth animations, transitions, and realistic movements in game development. By understanding these concepts and using them effectively, developers can enhance the visual and interactive experience of their games.
Additional Materials
Articles
- Linear Interpolation - This article describes tasks that can be resolved with this technique, provides examples and explanations.
15 minutes
- Inverse Lerp & Remap - Explanation of usage Lerp, InvLerp and Remap functions in shader programming.
15 minutes
Videos
- Linear Interpolations: Lerp, InvLerp, Remap: (from 01:33:18 - to 02:33:48): Explain basic concepts of interpolations. mostly linear interpolation and how it can be used.
2 hours
- Lerping Fundamentals: Explain the difference between
Lerp
andSlerp
for Quaternions.
20 minutes
Tasks
Math Lib
The following tasks should be realised in special project "Math Lib". Write functions that resolve the tasks issues in one class and demonstrate the functionality in any way on "Scene"- or "Editor"-windows.
- Linear Interpolation: Given two points A and B, write a program that takes a value t (between 0 and 1) as input and performs linear interpolation between A and B. The program should calculate the interpolated point and output it.
20 minutes
- Inverse Lerp: Given three points A, B, and P, write a program that takes P as input and performs inverse lerp. The program should calculate the t value (between 0 and 1) that represents the position of P between A and B, and output it.
20 minutes
- Remap: Given a value x and two ranges (A1 to A2 and B1 to B2), write a program that remaps x from the range A1 to A2 to the range B1 to B2. The program should calculate the remapped value and output it.
30 minutes
- Slerp (Spherical Linear Interpolation): Given two unit quaternions Q1 and Q2, write a program that takes a value t (between 0 and 1) as input and performs spherical linear interpolation between Q1 and Q2. The program should calculate the interpolated quaternion and output it.
40 minutes
- Visualise each program above
Cube-game
The following tasks extend current game experience in our "Cube"-competitor game (where we trying achieve as more goals as we can)
- Imitation of the game “hot - cold”: depending on the angle between the direction of the “character” (cube) and the target and the distance to the target, we will change the color of the material of this character (or any other parameter). The maximum and minimum values of the direction angle are 180 and 0 degrees, respectively. The maximum distance to the target is the length of the diagonal of the site. When changing the direction or distance to the target, you need to linearly change the color (or any other parameter) of the character's material.
Use Unity-functions mostly.
Self task time: 2 hours.
Spend some time to refactor your code: make attempts to build your own code architecture with splitting responsibilities to different classes. Refactor time: 1 hour 20 minutes.