Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update physics #2

Merged
merged 1 commit into from
Feb 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 35 additions & 77 deletions src/core/bridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@ w_bridge *create_bridge() {
destroy_bridge(td);
return NULL;
}

td->chunk_view = create_chunkview(td->chunk_group->chunks[0]);
if (td->chunk_view == NULL) {
LOG("failed to allocate memory for chunk view");
destroy_bridge(td);
return NULL;
}

td->next_view = create_chunkview(td->chunk_group->chunks[0]);
if (td->next_view == NULL) {
destroy_bridge(td);
return NULL;
}
Expand Down Expand Up @@ -73,6 +79,7 @@ void destroy_bridge(w_bridge *td) {

destroy_chunkgroup(td->chunk_group);
destroy_chunkview(td->chunk_view);
destroy_chunkview(td->next_view);
destroy_player(td->player);

sfree(td->camera);
Expand All @@ -81,93 +88,44 @@ void destroy_bridge(w_bridge *td) {
}

void physics_update(w_bridge *td) {
// update_player_input(td->player, td->keyboard);
/*
if (td->keyboard->key == 0 && td->player->velocity.x == 0 &&
td->player->velocity.y == 0 && td->player->collision.bottom) {

LOG("static");
animate_player(td->player, PHYSICS_TICK, td->keyboard->key != 0);
return;
}
update_player_input(td->player, td->keyboard);
update_player_velocity(td->player);

td->player->velocity.y += 1;
Vector2 next_position = Vector2Add(
td->player->position,
Vector2Scale(td->player->velocity, PLAYER_SPEED * PHYSICS_TICK));

update_player_velocity(td->player, PHYSICS_TICK);
Rectangle player_rect = {.x = next_position.x,
.y = next_position.y,
.width = td->player->dst.width,
.height = td->player->dst.height};

Rectangle player_rect = (Rectangle){.x = td->player->position.x,
.y = td->player->position.y,
.width = td->player->dst.width,
.height = td->player->dst.height};
update_chunkview(td->next_view, td->chunk_group,
(Rectangle){.x = next_position.x,
.y = next_position.y,
.width = RENDER_W,
.height = RENDER_H});

td->player->collision.all = 0;
Rectangle collisions[4] = {0};
size_t len = 0;
Rectangle *overlaps =
get_player_collision_overlap(player_rect, td->next_view, &len);

for (size_t i = 0; i < td->chunk_view->len; i++) {
Rectangle block_rect = td->chunk_view->blocks[i].dst;
bool done_y = td->player->velocity.y == 0;
for (size_t i = 0; i < len; i++) {

if (CheckCollisionRecs(player_rect, block_rect)) {
collisions[len] = block_rect;
len++;
if (len >= 4) {
break;
}
}
}
if (len > 0) {
td->player->velocity = VEC_ZERO;
} else {
td->player->position = Vector2Add(
td->player->position,
Vector2Scale(td->player->velocity, PLAYER_SPEED * PHYSICS_TICK));
}
if (!done_y && overlaps[i].y - player_rect.y < CUBE_H) {
player_rect.y =
(int)floor((overlaps[i].y - player_rect.height) / CUBE_H) * CUBE_H;
td->player->velocity.y = 0;
td->player->is_onground = true;

if (td->keyboard->left) {
td->player->body->velocity.x = 1;
} else if (td->keyboard->right) {
td->player->body->velocity.x = -1;
}

if (td->keyboard->jump) {
td->player->body->velocity.y = -4;
}

unsigned int around_count = 0;
for (size_t i = 0; i < td->chunk_view->len; i++) {
Vector2 block_center =
(Vector2){.x = td->chunk_view->blocks[i].dst.x + CUBE_W / 2,
.y = td->chunk_view->blocks[i].dst.y + CUBE_H / 2};
if (abs(Vector2Distance(td->player->body->position, block_center)) <
CUBE_W * 2) {

around_count++;
if (around_count >= 12) {
break;
}
done_y = true;
}
}
*/

Vector2 player_center = {td->player->position.x + td->player->dst.width / 2.f,
td->player->position.y +
td->player->dst.height / 2.f};
Vector2 block_center = {0};
Rectangle blocks[12] = {0};
size_t len = 0;
for (size_t i = 0; i < td->chunk_view->len; i++) {

block_center.x = td->chunk_view->blocks[i].dst.x + CUBE_W / 2;
block_center.y = td->chunk_view->blocks[i].dst.y + CUBE_H / 2;
free(overlaps);

if (abs(Vector2Distance(player_center, block_center)) < CUBE_W * 2) {
blocks[len] = td->chunk_view->blocks[i].dst;
len++;
if (len >= 12) {
break;
}
}
}
td->player->position.x = player_rect.x;
td->player->position.y = player_rect.y;

td->camera_target = get_camera_target_player(td->player, td->camera);
animate_player(td->player, PHYSICS_TICK, td->keyboard->key != 0);
Expand Down
1 change: 1 addition & 0 deletions src/core/bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ typedef struct w_bridge {

w_chunkgroup *chunk_group;
w_chunkview *chunk_view;
w_chunkview *next_view;
w_keyboard *keyboard;

w_player *player;
Expand Down
50 changes: 43 additions & 7 deletions src/entities/player.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,46 @@ void animate_player(w_player *player, float dt, bool should_walk) {

void update_player_input(w_player *player, w_keyboard *keyboard) {

if (!player->is_onground) {
player->velocity.y += 1;
} else if (keyboard->jump && player->is_onground) {
player->is_onground = false;
player->velocity.y -= 10;
}

if (keyboard->left) {
player->is_onground = false;

if (player->src.width > 0) {
player->src.width = -player->src.width;
}
player->velocity.x -= 1;
} else if (keyboard->right) {
player->is_onground = false;

if (player->src.width < 0) {
player->src.width = -player->src.width;
}
player->velocity.x += 1;
}
/*
}

void update_player_velocity(w_player *player) {
player->velocity.x =
Clamp(player->velocity.x, -MAX_PLAYER_VELOCITY_X, MAX_PLAYER_VELOCITY_X);

if (keyboard->jump && player->collision.bottom) {
player->velocity.y -= 10;
} else if (!player->collision.bottom) {
player->velocity.y += 1;
}
*/
player->velocity.y =
Clamp(player->velocity.y, -MAX_PLAYER_VELOCITY_Y, MAX_PLAYER_VELOCITY_Y);

player->velocity = Vector2Scale(player->velocity, PLAYER_FRICTION);

if (fabs(player->velocity.x) < 0.1f) {
player->velocity.x = 0;
}

if (fabs(player->velocity.y) < 0.1f) {
player->velocity.y = 0;
}
}

Vector2 get_camera_target_player(w_player *player, Camera2D *camera) {
Expand All @@ -73,3 +93,19 @@ Vector2 get_camera_target_player(w_player *player, Camera2D *camera) {
.width = player->dst.width,
.height = player->dst.height});
}

Rectangle *get_player_collision_overlap(Rectangle player, w_chunkview *view,
size_t *len) {
Rectangle *overlaps = malloc(sizeof(Rectangle) * MAX_OVERLAP_LEN);
for (size_t i = 0; i < view->len; i++) {
Rectangle block = view->blocks[i].dst;
if (CheckCollisionRecs(block, player)) {
overlaps[*len] = GetCollisionRec(block, player);
(*len)++;
if ((*len) >= MAX_OVERLAP_LEN) {
break;
}
}
}
return realloc(overlaps, sizeof(Rectangle) * (*len));
}
12 changes: 9 additions & 3 deletions src/entities/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
#include "../stdafx.h"
#include "../terrain/chunk_view.h"

#define PLAYER_SPEED 100.f
#define PLAYER_SPEED 1000.f
#define PLAYER_JUMP 10.f
#define PLAYER_FRICTION 0.8f
#define MAX_PLAYER_VELOCITY_X 1.f
#define MAX_PLAYER_VELOCITY_Y 10.f
#define MIN_PLAYER_VELOCITY 0.1f

#define MAX_OVERLAP_LEN 12
#define PLAYER_SRC_RECT \
(Rectangle) { 0, 0, 8, 16 }

Expand All @@ -32,6 +32,10 @@ typedef struct w_player {
Vector2 position;
Vector2 velocity;

bool is_onground;
bool left_stop;
bool right_stop;

w_playerstate state;
} w_player;

Expand All @@ -40,6 +44,8 @@ void destroy_player(w_player *player);

void animate_player(w_player *player, float dt, bool should_walk);
void update_player_input(w_player *player, w_keyboard *keyboard);
// void update_player_velocity(w_player *player, float dt);
void update_player_velocity(w_player *player);

Vector2 get_camera_target_player(w_player *player, Camera2D *camera);
Rectangle *get_player_collision_overlap(Rectangle player, w_chunkview *view,
size_t *len);
18 changes: 11 additions & 7 deletions src/screen/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,24 @@ void game_screen(w_state *state) {
BeginMode2D(*(td->camera));
smooth_vec(&td->camera->target, td->camera_target, speed);

/*
Vector2 player_center = {
td->player->position.x + td->player->dst.width / 2.f,
td->player->position.y + td->player->dst.height / 2.f};
Vector2 block_center = {0};
*/
for (unsigned int i = 0; i < td->chunk_view->len; i++) {
block_center.x = td->chunk_view->blocks[i].dst.x + CUBE_W / 2;
block_center.y = td->chunk_view->blocks[i].dst.y + CUBE_H / 2;
// block_center.x = td->chunk_view->blocks[i].dst.x + CUBE_W / 2;
// block_center.y = td->chunk_view->blocks[i].dst.y + CUBE_H / 2;
DrawTexturePro(block_textures[td->chunk_view->blocks[i].block.type - 1],
td->chunk_view->blocks[i].src,
td->chunk_view->blocks[i].dst, VEC_ZERO, 0,
abs(Vector2Distance(player_center, block_center)) <
CUBE_W * 2
? BLUE
: WHITE);
td->chunk_view->blocks[i].dst, VEC_ZERO, 0, WHITE);
/*
abs(Vector2Distance(player_center, block_center)) <
CUBE_W * 2
? BLUE
: WHITE);
*/
}
EndMode2D();
DrawTexturePro(player_textures[td->player->state], td->player->src,
Expand Down
Loading