From 083b89abd3e8d95be923ac9b5a54e1f3a84cba19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Sun, 29 Aug 2021 17:56:58 +0200 Subject: [PATCH] further physics improvements --- code/game/src/debug_ui_actions.c | 2 +- code/game/src/renderer_v0.c | 6 ++--- code/game/src/utils/raylib_helpers.h | 36 ++++++++++++++++++++++++++++ code/game/src/world/blocks_list.c | 2 +- code/modules/source/system_demo.c | 8 ++++--- code/modules/source/system_vehicle.c | 6 +++++ 6 files changed, 52 insertions(+), 8 deletions(-) diff --git a/code/game/src/debug_ui_actions.c b/code/game/src/debug_ui_actions.c index 12eec5f..985933e 100644 --- a/code/game/src/debug_ui_actions.c +++ b/code/game/src/debug_ui_actions.c @@ -42,8 +42,8 @@ ActSpawnCirclingDriver(void) { *dest = *origin; Input *input = ecs_get_mut(world_ecs(), e, Input, NULL); + zpl_zero_item(input); input->x = input->y = 1.0f; - input->use = false; Vehicle *veh = ecs_get_mut(world_ecs(), ve, Vehicle, NULL); veh->seats[0] = e; diff --git a/code/game/src/renderer_v0.c b/code/game/src/renderer_v0.c index 5c953aa..e781bd2 100644 --- a/code/game/src/renderer_v0.c +++ b/code/game/src/renderer_v0.c @@ -147,13 +147,13 @@ void renderer_debug_draw_v0(void) { float y = e->a.y; float x2 = e->b.x; float y2 = e->b.y; - DrawLine(x, y, x2, y2, color); + DrawLineV((Vector2){x, y}, (Vector2){x2, y2}, color); }break; case DDRAW_CIRCLE:{ float x = e->a.x; float y = e->a.y; - DrawCircleLines(x, y, e->radius, color); + DrawCircleLinesEco(x, y, e->radius, color); }break; case DDRAW_RECT:{ @@ -161,7 +161,7 @@ void renderer_debug_draw_v0(void) { float y = e->bmin.y; float w = e->bmax.x - e->bmin.x; float h = e->bmax.y - e->bmin.y; - DrawRectangleLines(x, y, w, h, color); + DrawRectangleLinesEco(x, y, w, h, color); }break; default: { diff --git a/code/game/src/utils/raylib_helpers.h b/code/game/src/utils/raylib_helpers.h index a4cbfec..e730dc3 100644 --- a/code/game/src/utils/raylib_helpers.h +++ b/code/game/src/utils/raylib_helpers.h @@ -330,3 +330,39 @@ Color GenerateRandomColor(float s, float v) { h = fmodf((h + h*Phi), 360.0f); return ColorFromHSV(h, s, v); } + + +// Draw circle outline +void DrawCircleLinesEco(float centerX, float centerY, float radius, Color color) +{ + rlCheckRenderBatchLimit(2*36); + + rlBegin(RL_LINES); + rlColor4ub(color.r, color.g, color.b, color.a); + + // NOTE: Circle outline is drawn pixel by pixel every degree (0 to 360) + for (int i = 0; i < 360; i += 10) + { + rlVertex2f(centerX + sinf(DEG2RAD*i)*radius, centerY + cosf(DEG2RAD*i)*radius); + rlVertex2f(centerX + sinf(DEG2RAD*(i + 10))*radius, centerY + cosf(DEG2RAD*(i + 10))*radius); + } + rlEnd(); +} + +void DrawRectangleLinesEco(float posX, float posY, float width, float height, Color color) +{ + rlBegin(RL_LINES); + rlColor4ub(color.r, color.g, color.b, color.a); + rlVertex2f(posX + 1, posY + 1); + rlVertex2f(posX + width, posY + 1); + + rlVertex2f(posX + width, posY + 1); + rlVertex2f(posX + width, posY + height); + + rlVertex2f(posX + width, posY + height); + rlVertex2f(posX + 1, posY + height); + + rlVertex2f(posX + 1, posY + height); + rlVertex2f(posX + 1, posY + 1); + rlEnd(); +} diff --git a/code/game/src/world/blocks_list.c b/code/game/src/world/blocks_list.c index 25d74cf..aad7518 100644 --- a/code/game/src/world/blocks_list.c +++ b/code/game/src/world/blocks_list.c @@ -3,7 +3,7 @@ static block blocks[] = { {.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, .bounce = 2.0f }, + {.name = "base-wall", .flags = BLOCK_FLAG_COLLISION, .kind = BLOCK_KIND_WALL, .biome = 0, .symbol = '#', .drag = 1.0f , .friction = 1.0f, .bounce = 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 }, diff --git a/code/modules/source/system_demo.c b/code/modules/source/system_demo.c index cec55e7..bb0eb38 100644 --- a/code/modules/source/system_demo.c +++ b/code/modules/source/system_demo.c @@ -1,11 +1,13 @@ -#define DEMO_NPC_MOVE_SPEED 150 +#define DEMO_NPC_MOVE_SPEED 50 +#define DEMO_NPC_STEER_SPEED 30 void DemoNPCMoveAround(ecs_iter_t *it) { Velocity *v = ecs_column(it, Velocity, 1); for (int i = 0; i < it->count; i++) { - v[i].x += (rand()%3-1)*DEMO_NPC_MOVE_SPEED; - v[i].y += (rand()%3-1)*DEMO_NPC_MOVE_SPEED; + float d = zpl_quake_rsqrt(v[i].x*v[i].x + v[i].y*v[i].y); + v[i].x += (v[i].x*d*DEMO_NPC_MOVE_SPEED + zpl_cos(zpl_to_radians(rand()%360))*DEMO_NPC_STEER_SPEED); + v[i].y += (v[i].y*d*DEMO_NPC_MOVE_SPEED + zpl_sin(zpl_to_radians(rand()%360))*DEMO_NPC_STEER_SPEED); } } diff --git a/code/modules/source/system_vehicle.c b/code/modules/source/system_vehicle.c index ea31caa..78a194e 100644 --- a/code/modules/source/system_vehicle.c +++ b/code/modules/source/system_vehicle.c @@ -135,6 +135,12 @@ void VehicleHandling(ecs_iter_t *it) { v[i].y += (fr_y + bk_y) / 2.0f - p[i].y; car->heading = zpl_arctan2(fr_y - bk_y, fr_x - bk_x); + world_block_lookup lookahead = world_block_from_realpos(p[i].x+PHY_LOOKAHEAD(v[i].x), p[i].y+PHY_LOOKAHEAD(v[i].y)); + uint32_t flags = blocks_get_flags(lookahead.block_id); + if (flags & BLOCK_FLAG_COLLISION) { + car->force = 0.0f; + } + for (int j = 0; j < 4; j++) { if (!world_entity_valid(veh[i].seats[j])) continue; ecs_entity_t pe = veh[i].seats[j];