phys: improve movement

isolation_bkp/dynres
Dominik Madarász 2021-08-11 18:19:10 +02:00
parent c5849da051
commit 76e4e81281
10 changed files with 38 additions and 27 deletions

View File

@ -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_setup_pkt_handlers(pkt_reader, sp_pkt_writer);
world_init(seed, chunk_size, chunk_amount); world_init(seed, chunk_size, chunk_amount);
if (is_dash_enabled) flecs_dash_init(); 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++) { for (uint32_t i = 0; i < num_viewers; i++) {

View File

@ -21,10 +21,9 @@ static bool request_shutdown;
#include "renderer_v0.c" #include "renderer_v0.c"
void platform_init() { void platform_init() {
InitWindow(screenWidth, screenHeight, "eco2d - client"); InitWindow(screenWidth, screenHeight, "eco2d");
SetWindowState(FLAG_WINDOW_UNDECORATED|FLAG_WINDOW_MAXIMIZED|FLAG_WINDOW_RESIZABLE); SetWindowState(FLAG_WINDOW_UNDECORATED|FLAG_WINDOW_MAXIMIZED|FLAG_WINDOW_RESIZABLE);
SetTargetFPS(60);
SetTargetFPS(0);
screenWidth = GetScreenWidth(); screenWidth = GetScreenWidth();
screenHeight = GetScreenHeight(); screenHeight = GetScreenHeight();

View File

@ -24,6 +24,7 @@ typedef struct {
uint32_t biome; uint32_t biome;
char symbol; char symbol;
float drag; float drag;
float friction;
// NOTE(zaklaus): viewer data // NOTE(zaklaus): viewer data
Texture2D img; Texture2D img;
@ -82,6 +83,10 @@ float blocks_get_drag(uint8_t id) {
return blocks[id].drag; return blocks[id].drag;
} }
float blocks_get_friction(uint8_t id) {
return blocks[id].friction;
}
void *blocks_get_img(uint8_t id) { void *blocks_get_img(uint8_t id) {
return (void*)&blocks[id].img; return (void*)&blocks[id].img;
} }

View File

@ -21,6 +21,7 @@ uint32_t blocks_get_flags(uint8_t id);
uint32_t blocks_get_biome(uint8_t id); uint32_t blocks_get_biome(uint8_t id);
uint32_t blocks_get_kind(uint8_t id); uint32_t blocks_get_kind(uint8_t id);
float blocks_get_drag(uint8_t id); float blocks_get_drag(uint8_t id);
float blocks_get_friction(uint8_t id);
// NOTE(zaklaus): viewer-related functions // NOTE(zaklaus): viewer-related functions
void *blocks_get_img(uint8_t id); void *blocks_get_img(uint8_t id);

View File

@ -1,11 +1,11 @@
#include "world/blocks.h" #include "world/blocks.h"
static block blocks[] = { static block blocks[] = {
{.name = "base-ground", .flags = 0, .kind = BLOCK_KIND_GROUND, .biome = 0, .symbol = '.', .drag = 1.0f}, {.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 }, {.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 }, {.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 }, {.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 }, {.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 }, {.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 }, {.name = "base-lava", .flags = BLOCK_FLAG_HAZARD, .kind = BLOCK_KIND_LAVA, .biome = 0, .symbol = '!', .drag = 6.2f , .friction = 4.0f },
}; };

View File

@ -17,6 +17,7 @@ ECS_TYPE_DECLARE(Player);
ECS_TYPE_DECLARE(Movement); ECS_TYPE_DECLARE(Movement);
ECS_TYPE_DECLARE(Walking); ECS_TYPE_DECLARE(Walking);
ECS_TYPE_DECLARE(Flying); ECS_TYPE_DECLARE(Flying);
// NOTE(zaklaus): @1 DECLARE
void ComponentsImport(ecs_world_t *ecs) { void ComponentsImport(ecs_world_t *ecs) {
ECS_MODULE(ecs, Components); ECS_MODULE(ecs, Components);
@ -60,4 +61,5 @@ void ComponentsImport(ecs_world_t *ecs) {
ECS_SET_ENTITY(EcsActor); ECS_SET_ENTITY(EcsActor);
ECS_SET_ENTITY(EcsDemoNPC); ECS_SET_ENTITY(EcsDemoNPC);
ECS_SET_TYPE(Movement); ECS_SET_TYPE(Movement);
// NOTE(zaklaus): @2 SET
} }

View File

@ -92,6 +92,7 @@ ECS_TYPE_EXTERN(Movement);
ECS_TYPE_EXTERN(Walking); ECS_TYPE_EXTERN(Walking);
ECS_TYPE_EXTERN(Flying); ECS_TYPE_EXTERN(Flying);
ECS_TYPE_EXTERN(EcsClient); ECS_TYPE_EXTERN(EcsClient);
// NOTE(zaklaus): @1 EXTERN
typedef struct { typedef struct {
ECS_DECLARE_COMPONENT(Chunk); ECS_DECLARE_COMPONENT(Chunk);
@ -112,6 +113,7 @@ typedef struct {
ECS_DECLARE_TYPE(Movement); ECS_DECLARE_TYPE(Movement);
ECS_DECLARE_ENTITY(Walking); ECS_DECLARE_ENTITY(Walking);
ECS_DECLARE_ENTITY(Flying); ECS_DECLARE_ENTITY(Flying);
// NOTE(zaklaus): @2 DECLARE
} Components; } Components;
#define ComponentsImportHandles(handles)\ #define ComponentsImportHandles(handles)\
@ -133,5 +135,6 @@ ECS_IMPORT_ENTITY(handles, EcsActor);\
ECS_IMPORT_ENTITY(handles, EcsDemoNPC);\ ECS_IMPORT_ENTITY(handles, EcsDemoNPC);\
ECS_IMPORT_ENTITY(handles, Walking);\ ECS_IMPORT_ENTITY(handles, Walking);\
ECS_IMPORT_ENTITY(handles, Flying);\ ECS_IMPORT_ENTITY(handles, Flying);\
// NOTE(zaklaus): @3 IMPORT
void ComponentsImport(ecs_world_t *ecs); void ComponentsImport(ecs_world_t *ecs);

View File

@ -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) { void SystemsImport(ecs_world_t *ecs) {
ECS_MODULE(ecs, Systems); 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, LeaveVehicle, EcsOnLoad, components.Input, components.IsInVehicle);
ECS_SYSTEM(ecs, DemoPlaceIceBlock, EcsOnLoad, components.Input, components.Position, !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, HurtOnHazardBlock, EcsOnUpdate, components.Position, components.Health);
ECS_SYSTEM(ecs, RegenerateHP, EcsOnUpdate, components.Health); ECS_SYSTEM(ecs, RegenerateHP, EcsOnUpdate, components.Health);
ECS_SYSTEM(ecs, VehicleHandling, EcsOnUpdate, components.Vehicle, components.Position, components.Velocity); ECS_SYSTEM(ecs, VehicleHandling, EcsOnUpdate, components.Vehicle, components.Position, components.Velocity);

View File

@ -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 50.0
#define PLR_MOVE_SPEED_MULT 4.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++) { for (int i = 0; i < it->count; i++) {
world_block_lookup lookup = world_block_from_realpos(p[i].x, p[i].y); 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); double speed = PLR_MOVE_SPEED * (in[i].sprint ? PLR_MOVE_SPEED_MULT : 1.0);
if (zpl_abs(v[i].x) < speed && in[i].x) if (zpl_abs(v[i].x) < speed && in[i].x)
v[i].x += in[i].x*speed*drag; v[i].x += in[i].x*speed*drag;

View File

@ -101,8 +101,9 @@ void VehicleHandling(ecs_iter_t *it) {
if (j == 0) { if (j == 0) {
Input const* in = ecs_get(it->world, pe, Input); Input const* in = ecs_get(it->world, pe, Input);
world_block_lookup lookup = world_block_from_realpos(p[i].x, p[i].y); 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 += in->x * -VEHICLE_STEER;
car->steer = zpl_clamp(car->steer, -40.0f, 40.0f); car->steer = zpl_clamp(car->steer, -40.0f, 40.0f);
} }