Ensure physics aren't tied to framerate

isolation_bkp/dynres
Dominik Madarász 2021-10-27 00:22:54 +02:00
parent 16da0ad158
commit 6f57fb178a
10 changed files with 1261 additions and 1258 deletions

View File

@ -28,7 +28,7 @@ void platform_init() {
InitWindow(screenWidth, screenHeight, "eco2d"); InitWindow(screenWidth, screenHeight, "eco2d");
SetWindowState(FLAG_WINDOW_UNDECORATED|FLAG_WINDOW_MAXIMIZED|FLAG_WINDOW_RESIZABLE|FLAG_MSAA_4X_HINT); SetWindowState(FLAG_WINDOW_UNDECORATED|FLAG_WINDOW_MAXIMIZED|FLAG_WINDOW_RESIZABLE|FLAG_MSAA_4X_HINT);
SetTargetFPS(60); //SetTargetFPS(144);
screenWidth = GetScreenWidth(); screenWidth = GetScreenWidth();
screenHeight = GetScreenHeight(); screenHeight = GetScreenHeight();
renderer_init(); renderer_init();

View File

@ -9,11 +9,12 @@ uint64_t vehicle_spawn(void) {
ecs_entity_t e = entity_spawn(EKIND_VEHICLE); ecs_entity_t e = entity_spawn(EKIND_VEHICLE);
Vehicle *veh = ecs_get_mut(world_ecs(), e, Vehicle, NULL); Vehicle *veh = ecs_get_mut(world_ecs(), e, Vehicle, NULL);
zpl_zero_item(veh); *veh = (Vehicle){
veh->wheel_base = 50.0f; .wheel_base = 50.0f,
veh->speed = 50.0f; .speed = 50.0f,
veh->reverse_speed = -20.0f; .reverse_speed = -20.0f,
veh->force = 0.0f; .force = 0.0f,
};
return (uint64_t)e; return (uint64_t)e;
} }

View File

@ -8,7 +8,7 @@
#include "game.h" #include "game.h"
#define PHY_BLOCK_COLLISION 1 #define PHY_BLOCK_COLLISION 1
#define PHY_WALK_DRAG 0.12 #define PHY_WALK_DRAG 4.23f
#define PHY_LOOKAHEAD(x) (zpl_sign(x)*16.0f) #define PHY_LOOKAHEAD(x) (zpl_sign(x)*16.0f)
#include "source/system_onfoot.c" #include "source/system_onfoot.c"
@ -29,7 +29,7 @@ void IntegratePositions(ecs_iter_t *it) {
for (int i = 0; i < it->count; i++) { for (int i = 0; i < it->count; i++) {
// NOTE(zaklaus): world bounds // NOTE(zaklaus): world bounds
{ {
double w = (double)world_dim(); float w = (float)world_dim();
p[i].x = zpl_clamp(p[i].x, 0, w-1); p[i].x = zpl_clamp(p[i].x, 0, w-1);
p[i].y = zpl_clamp(p[i].y, 0, w-1); p[i].y = zpl_clamp(p[i].y, 0, w-1);
} }
@ -127,8 +127,8 @@ void ApplyWorldDragOnVelocity(ecs_iter_t *it) {
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); float drag = zpl_clamp(blocks_get_drag(lookup.block_id), 0.0f, 1.0f);
float friction = blocks_get_friction(lookup.block_id); 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].x = zpl_lerp(v[i].x, 0.0f, PHY_WALK_DRAG*drag*friction*it->delta_time);
v[i].y = zpl_lerp(v[i].y, 0.0f, PHY_WALK_DRAG*drag*friction); v[i].y = zpl_lerp(v[i].y, 0.0f, PHY_WALK_DRAG*drag*friction*it->delta_time);
} }
} }

View File

@ -3,6 +3,7 @@
typedef struct { typedef struct {
// NOTE(zaklaus): Public systems are exposed here // NOTE(zaklaus): Public systems are exposed here
int32_t _unused;
} Systems; } Systems;
#define SystemsImportHandles(handles) (void)(handles) #define SystemsImportHandles(handles) (void)(handles)

View File

@ -1,12 +1,12 @@
#define DEMO_NPC_MOVE_SPEED 50 #define DEMO_NPC_MOVE_SPEED 500
#define DEMO_NPC_STEER_SPEED 30 #define DEMO_NPC_STEER_SPEED 300
void DemoNPCMoveAround(ecs_iter_t *it) { void DemoNPCMoveAround(ecs_iter_t *it) {
Velocity *v = ecs_column(it, Velocity, 1); Velocity *v = ecs_column(it, Velocity, 1);
for (int i = 0; i < it->count; i++) { for (int i = 0; i < it->count; i++) {
float d = zpl_quake_rsqrt(v[i].x*v[i].x + v[i].y*v[i].y); 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].x += (v[i].x*d*DEMO_NPC_MOVE_SPEED*it->delta_time + zpl_cos(zpl_to_radians(rand()%360))*DEMO_NPC_STEER_SPEED*it->delta_time);
v[i].y += (v[i].y*d*DEMO_NPC_MOVE_SPEED + zpl_sin(zpl_to_radians(rand()%360))*DEMO_NPC_STEER_SPEED); v[i].y += (v[i].y*d*DEMO_NPC_MOVE_SPEED*it->delta_time + zpl_sin(zpl_to_radians(rand()%360))*DEMO_NPC_STEER_SPEED*it->delta_time);
} }
} }

View File

@ -1,4 +1,4 @@
#define PLR_MOVE_SPEED 30.0 #define PLR_MOVE_SPEED 800.0
#define PLR_MOVE_SPEED_MULT 1.5 #define PLR_MOVE_SPEED_MULT 1.5
void MovementImpulse(ecs_iter_t *it) { void MovementImpulse(ecs_iter_t *it) {
@ -10,7 +10,7 @@ void MovementImpulse(ecs_iter_t *it) {
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); 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);
v[i].x += in[i].x*speed*drag; v[i].x += in[i].x*speed*drag*it->delta_time;
v[i].y -= in[i].y*speed*drag; v[i].y -= in[i].y*speed*drag*it->delta_time;
} }
} }

View File

@ -77,10 +77,11 @@ void EnterVehicle(ecs_iter_t *it) {
} }
} }
#define VEHICLE_FORCE 34.8f #define VEHICLE_FORCE 340.8f
#define VEHICLE_ACCEL 0.42f #define VEHICLE_ACCEL 0.12f
#define VEHICLE_DECEL 0.28f #define VEHICLE_DECEL 0.28f
#define VEHICLE_STEER 3.89f #define VEHICLE_STEER 3.89f
#define VEHICLE_POWER 34.89f
#define VEHICLE_BRAKE_FORCE 0.84f #define VEHICLE_BRAKE_FORCE 0.84f
void VehicleHandling(ecs_iter_t *it) { void VehicleHandling(ecs_iter_t *it) {
@ -134,8 +135,8 @@ void VehicleHandling(ecs_iter_t *it) {
fr_x += car->force * drag * zpl_cos(car->heading + zpl_to_radians(car->steer)); fr_x += car->force * drag * zpl_cos(car->heading + zpl_to_radians(car->steer));
fr_y += car->force * drag * zpl_sin(car->heading + zpl_to_radians(car->steer)); fr_y += car->force * drag * zpl_sin(car->heading + zpl_to_radians(car->steer));
v[i].x += (fr_x + bk_x) / 2.0f - p[i].x; v[i].x += ((fr_x + bk_x) / 2.0f - p[i].x)*it->delta_time*VEHICLE_POWER;
v[i].y += (fr_y + bk_y) / 2.0f - p[i].y; v[i].y += ((fr_y + bk_y) / 2.0f - p[i].y)*it->delta_time*VEHICLE_POWER;
car->heading = zpl_arctan2(fr_y - bk_y, fr_x - bk_x); 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)); world_block_lookup lookahead = world_block_from_realpos(p[i].x+PHY_LOOKAHEAD(v[i].x), p[i].y+PHY_LOOKAHEAD(v[i].y));