Skip to content

Commit

Permalink
physics test
Browse files Browse the repository at this point in the history
  • Loading branch information
julesgrc0 committed Feb 8, 2024
1 parent c931f1e commit 9f9c726
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 64 deletions.
68 changes: 55 additions & 13 deletions src/core/bridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ w_bridge *create_bridge() {
td->player->src = PLAYER_SRC_RECT;
td->player->dst = (Rectangle){.x = (RENDER_W - CUBE_W) / 2.f,
.y = (RENDER_H - CUBE_H * 2) / 2.f,
.width = CUBE_W,
.width = CUBE_W * 0.7f, // adjust player size
.height = CUBE_H * 2};
td->player->position = (Vector2){
.x = (td->chunk_group->position + CHUNK_GROUP_MID_LEN) * CHUNK_W * CUBE_W,
Expand Down Expand Up @@ -95,37 +95,79 @@ void destroy_bridge(w_bridge *td) {
}

void physics_update(w_bridge *td) {
if (td->keyboard->up) {
td->player->velocity.y -= 1;
} else if (td->keyboard->down) {
td->player->velocity.y += 1;
}

if (td->keyboard->left) {
if (td->player->src.width > 0) {
td->player->src.width = -td->player->src.width;
}
td->player->velocity.x -= 1;
} else if (td->keyboard->right) {
if (td->player->src.width < 0) {
td->player->src.width = -td->player->src.width;
}
td->player->velocity.x += 1;
}

if (td->keyboard->space) {
td->camera->zoom += 0.1f;
} else if (td->keyboard->shift) {
td->camera->zoom -= 0.1f;
}
if (td->keyboard->left || td->keyboard->right || !td->player->is_onground) {

if (!td->player->is_onground) {
td->player->velocity.y += 1;
} else {
td->player->velocity.y = 1;
td->player->is_onground = false;
}

if (td->keyboard->key != 0) {
td->player->velocity = Vector2Normalize(td->player->velocity);
td->player->position = Vector2Add(
td->player->position,
Vector2Scale(td->player->velocity, PLAYER_SPEED * PHYSICS_TICK));

if (!td->player->is_onground) {
Rectangle player_rect = {td->player->position.x, td->player->position.y,
td->player->dst.width, td->player->dst.height};
for (size_t i = 0; i < td->chunk_view->len; i++) {

Rectangle block_rect = td->chunk_view->blocks[i].dst;
if (CheckCollisionRecs(player_rect, block_rect)) {
/*
td->player->position = Vector2Subtract(
td->player->position,
Vector2Scale(td->player->velocity, PLAYER_SPEED * PHYSICS_TICK));
*/

if (td->player->position.y + td->player->dst.height >= block_rect.y) {
td->player->is_onground = true;
td->player->position.y = block_rect.y - td->player->dst.height;
td->player->velocity.y = 0;
}
if (td->player->position.x + td->player->dst.width >= block_rect.x &&
td->player->position.x <= block_rect.x + block_rect.width) {
td->player->velocity.x = 0;
}
break;
}
}
}

td->camera_target = center_camera_on_object(
td->camera, (Rectangle){.x = td->player->position.x,
.y = td->player->position.y,
.width = td->player->dst.width,
.height = td->player->dst.height});
}

td->player->animation += PHYSICS_TICK;
td->player->velocity = Vector2Scale(td->player->velocity, 0.9f);
if (abs(td->player->velocity.x) > 0.1f || td->keyboard->key != 0) {
if (td->player->animation > PHYSICS_TICK * 2) {
td->player->animation = 0;
td->player->state = (td->player->state == P_WALK_1) ? P_WALK_2 : P_WALK_1;
}
} else {
if (td->player->animation > PHYSICS_TICK * 12) {
td->player->animation = 0;
td->player->state = (td->player->state == P_IDLE_1) ? P_IDLE_2 : P_IDLE_1;
}
}
clear_keyboard(td->keyboard);
}

Expand Down
2 changes: 0 additions & 2 deletions src/core/keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
void update_keyboard(w_keyboard *kb) {
kb->left = IsKeyDown(KEY_LEFT);
kb->right = IsKeyDown(KEY_RIGHT);
kb->up = IsKeyDown(KEY_UP);
kb->down = IsKeyDown(KEY_DOWN);
kb->space = IsKeyDown(KEY_SPACE);
kb->shift = IsKeyDown(KEY_LEFT_SHIFT);
}
Expand Down
6 changes: 3 additions & 3 deletions src/core/keyboard.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#pragma once
#include "../stdafx.h"

#pragma pack(push, 1)
typedef union w_keyboard {
struct {
unsigned int space : 1;
unsigned int shift : 1;
unsigned int left : 1;
unsigned int right : 1;
unsigned int up : 1;
unsigned int down : 1;
};
unsigned int key : 6;
unsigned int key : 4;
} w_keyboard;
#pragma pack(pop)

void update_keyboard(w_keyboard *kb);
void clear_keyboard(w_keyboard *kb);
32 changes: 0 additions & 32 deletions src/entities/player.c

This file was deleted.

8 changes: 3 additions & 5 deletions src/entities/player.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once
#include "../core/state.h"
#include "../stdafx.h"
#include "../terrain/chunk.h"
#include "../terrain/chunk_view.h"

#define PLAYER_SPEED 1000.f
#define PLAYER_SRC_RECT \
Expand All @@ -24,9 +24,7 @@ typedef struct w_player {
float delay;
float animation;

unsigned int onground : 1;
unsigned int direction : 1;
unsigned int nearest_y;
bool is_onground;
w_playerstate state;
} w_player;

void update_player(w_player *player, float velocity, float dt);
32 changes: 24 additions & 8 deletions src/screen/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void game_screen(w_state *state) {
while (!WindowShouldClose() && td->is_active) {
update_keyboard(td->keyboard);

float speed = GetFrameTime() * PLAYER_SPEED * 0.8;
float speed = GetFrameTime() * PLAYER_SPEED * 0.95f;

BeginTextureMode(state->render);
ClearBackground(BLACK);
Expand All @@ -40,25 +40,38 @@ void game_screen(w_state *state) {
BeginMode2D(*(td->camera));
smooth_vec(&td->camera->target, td->camera_target, speed);
for (unsigned int i = 0; i < td->chunk_view->len; i++) {

bool is_current = round(td->player->position.x / FULL_CHUNK_W) ==
round(td->chunk_view->blocks[i].dst.x / FULL_CHUNK_W);
bool is_hover = round(td->player->position.x / CUBE_W) ==
round(td->chunk_view->blocks[i].dst.x / CUBE_W);

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, WHITE);
td->chunk_view->blocks[i].dst, VEC_ZERO, 0,
// WHITE);
is_current ? (is_hover ? BLUE : GREEN)
: (is_hover ? GREEN : WHITE));
}
/*
DrawRectangleLines(td->player->position.x, td->player->position.y,
td->player->dst.width, td->player->dst.height, RED);
*/
EndMode2D();
DrawTexturePro(player_textures[0], td->player->src, td->player->dst,
VEC_ZERO, 0, WHITE);
DrawTexturePro(player_textures[td->player->state], td->player->src,
td->player->dst, VEC_ZERO, 0, WHITE);
/*
DrawText(
DrawText(
TextFormat(
"Block %d\n\nCamera %f %f\n\Player %f %f\n\nChunk:\n "
"Block %d\n\nCamera %f %f\n\Player %d\n\nChunk:\n "
"Current: %d\n Next: %d\n Group: %d",
td->chunk_view->len, td->camera->target.x, td->camera->target.y,
td->player->position.x, td->player->position.y,
round(td->player->position.x / FULL_CHUNK_W),
td->chunk_view->target->position,
td->chunk_view->next == NULL ? -1 : td->chunk_view->next->position,
td->chunk_group->position),
0, 20, 20, WHITE);
*/
*/
#ifdef _WIN32
ReleaseMutex(td->chunk_view->mutex);
#else
Expand All @@ -71,9 +84,12 @@ void game_screen(w_state *state) {
ClearBackground(BLACK);
DrawTexturePro(state->render.texture, state->src_rnd, state->dest_rnd,
VEC_ZERO, 0.0f, WHITE);
DrawFPS(0, 0);
/*
DrawText(
TextFormat("dt: %f\nfps: %f", GetFrameTime(), 1.f / GetFrameTime()), 0,
0, 20, WHITE);
*/
EndDrawing();
}
destroy_bridge(td);
Expand Down
1 change: 1 addition & 0 deletions src/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <math.h>
#include <memory.h>
#include <signal.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
Expand Down
2 changes: 2 additions & 0 deletions src/terrain/chunk_view.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ bool update_chunkview(w_chunkview *chunk_view, w_chunkgroup *grp,

void filter_chunkview_blocks(w_chunk *chunk, Rectangle view,
w_renderblock *blocks, size_t *rendercount) {
// TODO: optimize this

unsigned int start_x = 0; // abs(viewx - floor(view.x / CUBE_W));
unsigned int end_x =
CHUNK_W; // abs(viewx - round((view.x + view.width) / CUBE_W));
Expand Down
7 changes: 6 additions & 1 deletion wispy-c.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@
<DisableLanguageExtensions>false</DisableLanguageExtensions>
<LanguageStandard_C>stdc17</LanguageStandard_C>
<Optimization>Full</Optimization>
<StringPooling>true</StringPooling>
<ExceptionHandling>false</ExceptionHandling>
<ErrorReporting>None</ErrorReporting>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand Down Expand Up @@ -267,6 +270,9 @@
<DisableLanguageExtensions>false</DisableLanguageExtensions>
<LanguageStandard_C>stdc17</LanguageStandard_C>
<Optimization>Full</Optimization>
<StringPooling>true</StringPooling>
<ExceptionHandling>false</ExceptionHandling>
<ErrorReporting>None</ErrorReporting>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand Down Expand Up @@ -325,7 +331,6 @@
<ClCompile Include="src\core\bridge.c" />
<ClCompile Include="src\core\keyboard.c" />
<ClCompile Include="src\core\view.c" />
<ClCompile Include="src\entities\player.c" />
<ClCompile Include="src\screen\game.c" />
<ClCompile Include="src\screen\loading.c" />
<ClCompile Include="src\screen\menu.c" />
Expand Down

0 comments on commit 9f9c726

Please sign in to comment.