base sideview physics

This commit is contained in:
KikooDX 2021-11-10 08:22:24 +01:00
parent c0c4630095
commit 69fb783dc6
2 changed files with 32 additions and 5 deletions

View File

@ -1,6 +1,14 @@
#pragma once
#define TARGET_FPS 60
#define TILE_SIZE 16
#define PLAYER_WIDTH 12
#define PLAYER_HEIGHT 12
#define TARGET_FPS 60
#define TILE_SIZE 16
#define PLAYER_WIDTH 14
#define PLAYER_HEIGHT 14
#define GRAVITY 0.2f
#define AIR_RESISTANCE 0.02f
#define MAX_WALK_SPEED 2.0f
#define ACCELERATION_AIR 0.1f
#define ACCELERATION_GROUND 0.4f
#define FRICTION_AIR (ACCELERATION_AIR / MAX_WALK_SPEED)
#define FRICTION_GROUND (ACCELERATION_GROUND / MAX_WALK_SPEED)
#define FRICTION_BREAK (0.6f / MAX_WALK_SPEED)

View File

@ -1,5 +1,6 @@
#include "player.h"
#include "conf.h"
#include "input.h"
#include "level.h"
#include "util.h"
#include "vec.h"
@ -20,7 +21,25 @@ player_init(struct Vec pos)
void
player_update(void)
{
self.spd.x = 0.9f;
const int on_ground = collide_solid(self.pos.x, self.pos.y + 1);
const struct Vec dir = VEC(input_down(K_RIGHT) - input_down(K_LEFT),
input_down(K_DOWN) - input_down(K_UP));
/* acceleration & friction */
if (on_ground && !dir.x) {
self.spd.x *= 1.0f * FRICTION_BREAK;
} else {
self.spd.x *=
1.0f - (on_ground ? FRICTION_GROUND : FRICTION_AIR);
self.spd.x +=
(on_ground ? ACCELERATION_GROUND : ACCELERATION_AIR) *
dir.x;
}
/* resistance & gravity */
self.spd.y *= 1.0f - AIR_RESISTANCE;
self.spd.y += GRAVITY;
player_move(player_update_rem());
}