From f58a21265296668ac239e1b2315bf4fc1767aa7b Mon Sep 17 00:00:00 2001 From: KikooDX Date: Mon, 14 Sep 2020 10:33:01 +0200 Subject: [PATCH] [camera] lerping and miscelanious --- include/camera.h | 2 +- include/vec.h | 6 +++++- src/camera.c | 2 ++ src/main.c | 4 ++-- src/vec.c | 15 ++++++++++++--- 5 files changed, 22 insertions(+), 7 deletions(-) diff --git a/include/camera.h b/include/camera.h index 517e04b..ee8a373 100644 --- a/include/camera.h +++ b/include/camera.h @@ -6,7 +6,7 @@ typedef struct Camera { Vec position; - Vec *follow; /* the target position to lerp on */ + Vec *target; /* the target position to lerp on */ float speed; /* camera lerp speed with 0 < speed <= 1 */ } Camera; diff --git a/include/vec.h b/include/vec.h index b7f63f9..6b93224 100644 --- a/include/vec.h +++ b/include/vec.h @@ -8,7 +8,7 @@ typedef struct Vec } Vec; /* like memcpy but for vectors */ -void vec_cpy(Vec *destination, Vec *source); +void vec_cpy(Vec *destination, Vec source); /* apply a force on a vector */ void vec_add(Vec *vector, Vec force); @@ -22,4 +22,8 @@ void vec_mul(Vec *vector, float scale); /* divise a vector by a scale */ void vec_div(Vec *vector, float scale); +/* Linear interpolation between two vectors. + * Require a scale ranging from 0 to 1 (0 is instant, 1 is completly still). */ +void vec_lerp(Vec *from, Vec to, float scale); + #endif /* _DEF_VEC */ diff --git a/src/camera.c b/src/camera.c index 4589d27..90daef3 100644 --- a/src/camera.c +++ b/src/camera.c @@ -1,5 +1,7 @@ #include "camera.h" +#include "vec.h" void camera_step(Camera *camera) { + vec_lerp(&camera->position, *camera->target, camera->speed); } diff --git a/src/main.c b/src/main.c index c8ffd3d..b8959cb 100644 --- a/src/main.c +++ b/src/main.c @@ -26,9 +26,9 @@ int main(void) /* create camera */ Camera camera = { - .follow = &player.position + .target = &player.position }; - vec_cpy(&camera.position, &player.position); + vec_cpy(&camera.position, player.position); /* main game loop */ step_event(&player, &level, &camera); diff --git a/src/vec.c b/src/vec.c index af64294..a5a555c 100644 --- a/src/vec.c +++ b/src/vec.c @@ -1,9 +1,9 @@ #include "vec.h" -void vec_cpy(Vec *destination, Vec *source) +void vec_cpy(Vec *destination, Vec source) { - destination->x = source->x; - destination->y = source->y; + destination->x = source.x; + destination->y = source.y; } void vec_add(Vec *vector, Vec force) @@ -29,3 +29,12 @@ void vec_div(Vec *vector, float scale) vector->x /= scale; vector->y /= scale; } + +void vec_lerp(Vec *from, Vec to, float scale) +{ + Vec temp = {}; + vec_cpy(&temp, to); + vec_sub(&temp, *from); + vec_mul(&temp, scale); + vec_add(from, temp); +}