From fbb4fa4eb4494c88f6915cfe9aa53d27458af35f Mon Sep 17 00:00:00 2001 From: alistair Date: Mon, 28 Dec 2020 18:42:45 +1000 Subject: [PATCH] work on better camera --- src/draw.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++- src/game.c | 2 +- src/main.c | 8 ++++---- 3 files changed, 51 insertions(+), 6 deletions(-) diff --git a/src/draw.c b/src/draw.c index afd0434..cc3326a 100644 --- a/src/draw.c +++ b/src/draw.c @@ -394,6 +394,51 @@ void update_viewport(Body *pl) { } } +void accel_update_viewport(Body *pl) { + const double accel = 0.0001; + const float xmargin = 0.5; + const float ymargin = 0.2; + + static Vect target_pos = (Vect) {.x=0, .y=0}; + static Vect vel = (Vect) {.x=0.0, .y=0.0}; + + + if (pl->position.x - viewport_pos.x > (1-xmargin) * width) { + target_pos.x = - ((1-xmargin) * width - pl->position.x); + } + + if (pl->position.x - target_pos.x < xmargin * width) { + target_pos.x = -(xmargin * width - pl->position.x); + } + + if (pl->position.y - target_pos.y > (1-ymargin) * height) { + target_pos.y = -((1-ymargin) * height - pl->position.y); + } + + if (pl->position.y - target_pos.y < ymargin * height) { + target_pos.y = -(ymargin * height - pl->position.y); + } + + viewport_pos = target_pos; + return; + + Vect delta = vect_add(target_pos, vect_scalar(viewport_pos, -1)); + printf("d %f %f\n", delta.x, delta.y); + printf("d %f\n", vect_mag(delta)); + + if ((vect_mag(delta) < 0.001 )) { + return; + } + + Vect dir = vect_scalar(delta, 1/vect_mag(delta)); + Vect vel_delta = vect_scalar(dir, accel * time_delta); + vel = vect_add(vel, vel_delta); + + Vect pos_delta = vect_scalar(vel, time_delta/2); + + viewport_pos = vect_add(viewport_pos, pos_delta); +} + double interpolate_h(Vect left, Vect right, double x) { static Vect v; @@ -719,7 +764,7 @@ void redraw_buffer(SDL_Renderer * ren) { if (SDL_LockMutex(player.physics->lock) == 0){ lplayer = *player.physics; - update_viewport(&lplayer); + accel_update_viewport(&lplayer); } else { return; } diff --git a/src/game.c b/src/game.c index 0720884..ebdfbd6 100644 --- a/src/game.c +++ b/src/game.c @@ -12,7 +12,7 @@ #define MAX_ROPE_GRAB_LEN 80000 #define MIN_PHYSICS_STEP 2.0 -#define TIMESTEP_LENGTH 2.0 +#define TIMESTEP_LENGTH 5.0 #define BREAKPOINT *(int *)0 = 1; diff --git a/src/main.c b/src/main.c index 3438b92..0b2af88 100644 --- a/src/main.c +++ b/src/main.c @@ -75,15 +75,15 @@ int physics_loop(void *ptr) { } int game(void) { - LOGLEVEL = DEBUG; - STDOUTLEVEL = DEBUG; + LOGLEVEL = SILENT; + STDOUTLEVEL = SILENT; //SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "2" ); logwrite(INFO, "Starting\n"); SDL_Window * win = make_window(); - SDL_Renderer * ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED - | SDL_RENDERER_PRESENTVSYNC); + SDL_Renderer * ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED); +// | SDL_RENDERER_PRESENTVSYNC); SDL_SetRenderDrawBlendMode(ren, SDL_BLENDMODE_BLEND); queue_for_cleanup(win, WINDOW);