From 76e4e81281e923b0c416a5070f619cb6541b20c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Wed, 11 Aug 2021 18:19:10 +0200 Subject: [PATCH] phys: improve movement --- code/game/src/game.c | 2 +- code/game/src/platform_raylib.c | 5 ++--- code/game/src/world/blocks.c | 5 +++++ code/game/src/world/blocks.h | 1 + code/game/src/world/blocks_list.c | 14 +++++++------- code/modules/modules/components.c | 2 ++ code/modules/modules/components.h | 3 +++ code/modules/modules/systems.c | 15 ++++++++++++++- code/modules/source/system_onfoot.c | 15 +-------------- code/modules/source/system_vehicle.c | 3 ++- 10 files changed, 38 insertions(+), 27 deletions(-) diff --git a/code/game/src/game.c b/code/game/src/game.c index 5360d17..1d55a07 100644 --- a/code/game/src/game.c +++ b/code/game/src/game.c @@ -126,7 +126,7 @@ void game_init(game_kind play_mode, uint32_t num_viewers, int32_t seed, uint16_t world_setup_pkt_handlers(pkt_reader, sp_pkt_writer); world_init(seed, chunk_size, chunk_amount); if (is_dash_enabled) flecs_dash_init(); - ecs_set_target_fps(world_ecs(), 60); + //ecs_set_target_fps(world_ecs(), 60); } for (uint32_t i = 0; i < num_viewers; i++) { diff --git a/code/game/src/platform_raylib.c b/code/game/src/platform_raylib.c index a42e07d..a0f9158 100644 --- a/code/game/src/platform_raylib.c +++ b/code/game/src/platform_raylib.c @@ -21,10 +21,9 @@ static bool request_shutdown; #include "renderer_v0.c" void platform_init() { - InitWindow(screenWidth, screenHeight, "eco2d - client"); + InitWindow(screenWidth, screenHeight, "eco2d"); SetWindowState(FLAG_WINDOW_UNDECORATED|FLAG_WINDOW_MAXIMIZED|FLAG_WINDOW_RESIZABLE); - - SetTargetFPS(0); + SetTargetFPS(60); screenWidth = GetScreenWidth(); screenHeight = GetScreenHeight(); diff --git a/code/game/src/world/blocks.c b/code/game/src/world/blocks.c index bef2095..b109081 100644 --- a/code/game/src/world/blocks.c +++ b/code/game/src/world/blocks.c @@ -24,6 +24,7 @@ typedef struct { uint32_t biome; char symbol; float drag; + float friction; // NOTE(zaklaus): viewer data Texture2D img; @@ -82,6 +83,10 @@ float blocks_get_drag(uint8_t id) { return blocks[id].drag; } +float blocks_get_friction(uint8_t id) { + return blocks[id].friction; +} + void *blocks_get_img(uint8_t id) { return (void*)&blocks[id].img; } diff --git a/code/game/src/world/blocks.h b/code/game/src/world/blocks.h index fe7dafa..730539f 100644 --- a/code/game/src/world/blocks.h +++ b/code/game/src/world/blocks.h @@ -21,6 +21,7 @@ uint32_t blocks_get_flags(uint8_t id); uint32_t blocks_get_biome(uint8_t id); uint32_t blocks_get_kind(uint8_t id); float blocks_get_drag(uint8_t id); +float blocks_get_friction(uint8_t id); // NOTE(zaklaus): viewer-related functions void *blocks_get_img(uint8_t id); diff --git a/code/game/src/world/blocks_list.c b/code/game/src/world/blocks_list.c index 97ef0bc..ee65f0d 100644 --- a/code/game/src/world/blocks_list.c +++ b/code/game/src/world/blocks_list.c @@ -1,11 +1,11 @@ #include "world/blocks.h" static block blocks[] = { - {.name = "base-ground", .flags = 0, .kind = BLOCK_KIND_GROUND, .biome = 0, .symbol = '.', .drag = 1.0f}, - {.name = "base-dirt", .flags = 0, .kind = BLOCK_KIND_DIRT, .biome = 0, .symbol = ',', .drag = 2.1f }, - {.name = "base-wall", .flags = BLOCK_FLAG_COLLISION, .kind = BLOCK_KIND_WALL, .biome = 0, .symbol = '#', .drag = 1.0f }, - {.name = "base-hill", .flags = BLOCK_FLAG_COLLISION, .kind = BLOCK_KIND_HILL, .biome = 0, .symbol = '^', .drag = 1.0f }, - {.name = "base-hill-snow", .flags = BLOCK_FLAG_COLLISION, .kind = BLOCK_KIND_HILL_SNOW, .biome = 0, .symbol = '*', .drag = 1.0f }, - {.name = "base-water", .flags = 0, .kind = BLOCK_KIND_WATER, .biome = 0, .symbol = '~', .drag = 0.11f }, - {.name = "base-lava", .flags = BLOCK_FLAG_HAZARD, .kind = BLOCK_KIND_LAVA, .biome = 0, .symbol = '!', .drag = 6.2f }, + {.name = "base-ground", .flags = 0, .kind = BLOCK_KIND_GROUND, .biome = 0, .symbol = '.', .drag = 1.0f, .friction = 1.0f }, + {.name = "base-dirt", .flags = 0, .kind = BLOCK_KIND_DIRT, .biome = 0, .symbol = ',', .drag = 2.1f , .friction = 1.0f }, + {.name = "base-wall", .flags = BLOCK_FLAG_COLLISION, .kind = BLOCK_KIND_WALL, .biome = 0, .symbol = '#', .drag = 1.0f , .friction = 1.0f }, + {.name = "base-hill", .flags = BLOCK_FLAG_COLLISION, .kind = BLOCK_KIND_HILL, .biome = 0, .symbol = '^', .drag = 1.0f , .friction = 1.0f }, + {.name = "base-hill-snow", .flags = BLOCK_FLAG_COLLISION, .kind = BLOCK_KIND_HILL_SNOW, .biome = 0, .symbol = '*', .drag = 1.0f , .friction = 1.0f }, + {.name = "base-water", .flags = 0, .kind = BLOCK_KIND_WATER, .biome = 0, .symbol = '~', .drag = 0.11f , .friction = 1.0f }, + {.name = "base-lava", .flags = BLOCK_FLAG_HAZARD, .kind = BLOCK_KIND_LAVA, .biome = 0, .symbol = '!', .drag = 6.2f , .friction = 4.0f }, }; diff --git a/code/modules/modules/components.c b/code/modules/modules/components.c index e9e20a1..998a6fc 100644 --- a/code/modules/modules/components.c +++ b/code/modules/modules/components.c @@ -17,6 +17,7 @@ ECS_TYPE_DECLARE(Player); ECS_TYPE_DECLARE(Movement); ECS_TYPE_DECLARE(Walking); ECS_TYPE_DECLARE(Flying); +// NOTE(zaklaus): @1 DECLARE void ComponentsImport(ecs_world_t *ecs) { ECS_MODULE(ecs, Components); @@ -60,4 +61,5 @@ void ComponentsImport(ecs_world_t *ecs) { ECS_SET_ENTITY(EcsActor); ECS_SET_ENTITY(EcsDemoNPC); ECS_SET_TYPE(Movement); + // NOTE(zaklaus): @2 SET } diff --git a/code/modules/modules/components.h b/code/modules/modules/components.h index b06202b..dfcc72c 100644 --- a/code/modules/modules/components.h +++ b/code/modules/modules/components.h @@ -92,6 +92,7 @@ ECS_TYPE_EXTERN(Movement); ECS_TYPE_EXTERN(Walking); ECS_TYPE_EXTERN(Flying); ECS_TYPE_EXTERN(EcsClient); +// NOTE(zaklaus): @1 EXTERN typedef struct { ECS_DECLARE_COMPONENT(Chunk); @@ -112,6 +113,7 @@ typedef struct { ECS_DECLARE_TYPE(Movement); ECS_DECLARE_ENTITY(Walking); ECS_DECLARE_ENTITY(Flying); + // NOTE(zaklaus): @2 DECLARE } Components; #define ComponentsImportHandles(handles)\ @@ -133,5 +135,6 @@ ECS_IMPORT_ENTITY(handles, EcsActor);\ ECS_IMPORT_ENTITY(handles, EcsDemoNPC);\ ECS_IMPORT_ENTITY(handles, Walking);\ ECS_IMPORT_ENTITY(handles, Flying);\ +// NOTE(zaklaus): @3 IMPORT void ComponentsImport(ecs_world_t *ecs); diff --git a/code/modules/modules/systems.c b/code/modules/modules/systems.c index 89cc014..57f7486 100644 --- a/code/modules/modules/systems.c +++ b/code/modules/modules/systems.c @@ -99,6 +99,19 @@ void RegenerateHP(ecs_iter_t *it) { } } +void ApplyWorldDragOnVelocity(ecs_iter_t *it) { + Position *p = ecs_column(it, Position, 1); + Velocity *v = ecs_column(it, Velocity, 2); + + for (int i = 0; i < it->count; i++) { + world_block_lookup lookup = world_block_from_realpos(p[i].x, p[i].y); + float drag = zpl_clamp(blocks_get_drag(lookup.block_id), 0.0f, 1.0f); + float friction = blocks_get_friction(lookup.block_id); + v[i].x = zpl_lerp(v[i].x, 0.0f, PHY_WALK_DRAG*drag*friction); + v[i].y = zpl_lerp(v[i].y, 0.0f, PHY_WALK_DRAG*drag*friction); + } +} + void SystemsImport(ecs_world_t *ecs) { ECS_MODULE(ecs, Systems); @@ -108,7 +121,7 @@ void SystemsImport(ecs_world_t *ecs) { ECS_SYSTEM(ecs, LeaveVehicle, EcsOnLoad, components.Input, components.IsInVehicle); ECS_SYSTEM(ecs, DemoPlaceIceBlock, EcsOnLoad, components.Input, components.Position, !components.IsInVehicle); - ECS_SYSTEM(ecs, MoveWalk, EcsOnUpdate, components.Position, components.Velocity); + ECS_SYSTEM(ecs, ApplyWorldDragOnVelocity, EcsOnUpdate, components.Position, components.Velocity); ECS_SYSTEM(ecs, HurtOnHazardBlock, EcsOnUpdate, components.Position, components.Health); ECS_SYSTEM(ecs, RegenerateHP, EcsOnUpdate, components.Health); ECS_SYSTEM(ecs, VehicleHandling, EcsOnUpdate, components.Vehicle, components.Position, components.Velocity); diff --git a/code/modules/source/system_onfoot.c b/code/modules/source/system_onfoot.c index 5904e2d..ceb3ce9 100644 --- a/code/modules/source/system_onfoot.c +++ b/code/modules/source/system_onfoot.c @@ -1,16 +1,3 @@ - -void MoveWalk(ecs_iter_t *it) { - Position *p = ecs_column(it, Position, 1); - Velocity *v = ecs_column(it, Velocity, 2); - - for (int i = 0; i < it->count; i++) { - world_block_lookup lookup = world_block_from_realpos(p[i].x, p[i].y); - float drag = blocks_get_drag(lookup.block_id); - v[i].x = zpl_lerp(v[i].x, 0.0f, PHY_WALK_DRAG*drag); - v[i].y = zpl_lerp(v[i].y, 0.0f, PHY_WALK_DRAG*drag); - } -} - #define PLR_MOVE_SPEED 50.0 #define PLR_MOVE_SPEED_MULT 4.0 @@ -21,7 +8,7 @@ void MovementImpulse(ecs_iter_t *it) { for (int i = 0; i < it->count; i++) { world_block_lookup lookup = world_block_from_realpos(p[i].x, p[i].y); - float drag = blocks_get_drag(lookup.block_id); + float drag = zpl_clamp(blocks_get_drag(lookup.block_id), 0.0f, 1.0f); double speed = PLR_MOVE_SPEED * (in[i].sprint ? PLR_MOVE_SPEED_MULT : 1.0); if (zpl_abs(v[i].x) < speed && in[i].x) v[i].x += in[i].x*speed*drag; diff --git a/code/modules/source/system_vehicle.c b/code/modules/source/system_vehicle.c index 437a037..8e4ad22 100644 --- a/code/modules/source/system_vehicle.c +++ b/code/modules/source/system_vehicle.c @@ -101,8 +101,9 @@ void VehicleHandling(ecs_iter_t *it) { if (j == 0) { Input const* in = ecs_get(it->world, pe, Input); world_block_lookup lookup = world_block_from_realpos(p[i].x, p[i].y); + float drag = zpl_clamp(blocks_get_drag(lookup.block_id), 0.0f, 1.0f); - car->force += zpl_lerp(0.0f, in->y * VEHICLE_FORCE, VEHICLE_ACCEL) * blocks_get_drag(lookup.block_id); + car->force += zpl_lerp(0.0f, in->y * VEHICLE_FORCE, VEHICLE_ACCEL) * drag; car->steer += in->x * -VEHICLE_STEER; car->steer = zpl_clamp(car->steer, -40.0f, 40.0f); }