[camera] lerping and miscelanious

This commit is contained in:
KikooDX 2020-09-14 10:33:01 +02:00
parent 7a760e67bb
commit f58a212652
5 changed files with 22 additions and 7 deletions

View File

@ -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;

View File

@ -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 */

View File

@ -1,5 +1,7 @@
#include "camera.h"
#include "vec.h"
void camera_step(Camera *camera)
{
vec_lerp(&camera->position, *camera->target, camera->speed);
}

View File

@ -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);

View File

@ -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);
}