From 6ce19277e54e0c56be105d8edac118aa3f41ba67 Mon Sep 17 00:00:00 2001 From: Ulysse Cura Date: Tue, 1 Sep 2026 17:29:56 +0200 Subject: [PATCH] Added linear interpolation with time. --- src/Makefile | 2 +- src/headers/vector2d.h | 20 +++++++++++++++++++- src/vector2d.c | 9 +++++++-- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/Makefile b/src/Makefile index d362217..67f7bcb 100644 --- a/src/Makefile +++ b/src/Makefile @@ -34,7 +34,7 @@ CORE_AR = ar CORE_ARFLAGS = # Used in final linking. -CORE_DEPS_FLAGS = $(shell pkg-config --libs sdl3) +CORE_DEPS_FLAGS = $(shell pkg-config --libs sdl3) -lm # Deduce objects CORE_OBJECTS = $(CORE_SOURCES:%.c=$(BUILD_DIR)/$(CORE_DIR)/%.o) diff --git a/src/headers/vector2d.h b/src/headers/vector2d.h index bfb45b4..ab511b8 100644 --- a/src/headers/vector2d.h +++ b/src/headers/vector2d.h @@ -130,6 +130,24 @@ void frect_to_rect(const frect_t *frect, rect_t *rect); */ #define intersect_frect(A, B, result) SDL_GetRectIntersectionFloat(A, B, result) -float lerp(float a, float b, float t); +/** + * @brief Lerp a to b with smoothing factor s. + * + * @param a Current value + * @param b Target value + * @param s Smoothing factor + */ +float lerp(float a, float b, float s); + +/** + * @brief Lerp a to b with smoothing factor s and time basis t. + * Smoothing factor is for 60 fps call time. + * + * @param a Current value + * @param b Target value + * @param s Smoothing factor + * @param t Delta time + */ +float lerp_time(float a, float b, float s, float t); #endif // VECTOR2D_H \ No newline at end of file diff --git a/src/vector2d.c b/src/vector2d.c index 71d8be0..d52ae03 100644 --- a/src/vector2d.c +++ b/src/vector2d.c @@ -16,7 +16,12 @@ void frect_to_rect(const frect_t *frect, rect_t *rect) rect->h = (int)frect->h; } -float lerp(float a, float b, float t) +inline float lerp(float a, float b, float s) { - return a * (1 - t) + b * t; + return a * (1 - s) + b * s; } + +inline float lerp_time(float a, float b, float s, float t) +{ + return lerp(a, b, 1 - powf(1 - s, t * 60.0f)); +} \ No newline at end of file