diff --git a/src/collider.c b/src/collider.c index d402c03e..884295d9 100644 --- a/src/collider.c +++ b/src/collider.c @@ -86,6 +86,7 @@ struct collider_collision_points_result collider_collision_points(struct collide (UNUSED)radius1; /* TODO: Parameterize */ + //const f32 tolerance = 0.f; /* How close can shapes be before collision is considered */ const f32 tolerance = 0.005f; /* How close can shapes be before collision is considered */ //const f32 tolerance = 0.05f; /* How close can shapes be before collision is considered */ const f32 min_unique_pt_dist_sq = 0.0001f * 0.0001f; @@ -247,6 +248,8 @@ struct collider_collision_points_result collider_collision_points(struct collide * Epa (to find collision normal from inside shape) * ========================== */ + //const f32 epa_epsilon_sq = 0; + //const f32 epa_epsilon_sq = 0.01f * 0.01f; //const f32 epa_epsilon_sq = 0.001f * 0.001f; const f32 epa_epsilon_sq = F32_INFINITY; diff --git a/src/config.h b/src/config.h index 8fcea2c2..e80424eb 100644 --- a/src/config.h +++ b/src/config.h @@ -33,7 +33,12 @@ #define GAME_TIMESCALE 1.0 #define GAME_PHYSICS_SUBSTEPS 8 +#define GAME_PHYSICS_ENABLE_COLLISION 1 #define GAME_PHYSICS_ENABLE_WARM_STARTING 1 +#define GAME_PHYSICS_ENABLE_RELAXATION 1 +#define GAME_PHYSICS_ENABLE_GROUND_FRICTION 0 + +#define GAME_SPAWN_LOTS 0 #define GAME_MAX_LINEAR_VELOCITY 100 #define GAME_MAX_ANGULAR_VELOCITY ((2 * PI) * 20) diff --git a/src/game.c b/src/game.c index 72029cd8..28dc9353 100644 --- a/src/game.c +++ b/src/game.c @@ -20,7 +20,7 @@ GLOBAL struct { struct sprite_scope *sprite_frame_scope; /* TODO: Remove this (testing) */ - b32 first_spawn; + b32 extra_spawn; /* Game thread input */ struct sys_mutex game_cmds_mutex; @@ -128,12 +128,13 @@ INTERNAL void spawn_test_entities(f32 offset) const f32 offset_all = 0; /* Player */ - struct entity *player_ent; + struct entity *player_ent = entity_nil(); + //if (!G.extra_spawn) { { //struct v2 pos = V2(0.25, -10); //struct v2 pos = V2(0.25, -7); //struct v2 pos = V2(0.25, -5.27); - struct v2 pos = V2(0.25, -2); + struct v2 pos = V2(0.25, -1); //struct v2 pos = V2(1.1230469346046448864129274625156, -1); /* Touching right side of box */ //struct v2 pos = V2(1.1230469346046448864129274625156 - 0.0001, -1); /* Touching right side of box */ //struct v2 pos = V2(0.374142020941, -0.246118023992); /* Touching glitch spot */ @@ -189,7 +190,7 @@ INTERNAL void spawn_test_entities(f32 offset) //struct v2 pos = V2(0.25, -7); //struct v2 pos = V2(0.25, -5.27); //struct v2 pos = V2(0.85, -2); - struct v2 pos = V2(0.25, -2); + struct v2 pos = V2(0.25, -1); //struct v2 pos = V2(1.1230469346046448864129274625156, -1); /* Touching right side of box */ //struct v2 pos = V2(1.1230469346046448864129274625156 - 0.0001, -1); /* Touching right side of box */ //struct v2 pos = V2(0.374142020941, -0.246118023992); /* Touching glitch spot */ @@ -235,13 +236,11 @@ INTERNAL void spawn_test_entities(f32 offset) e->angular_ground_friction = 100; //entity_enable_prop(e, ENTITY_PROP_TEST); - - player_ent = e; } #endif /* Weapon */ - { + if (player_ent->valid) { #if 0 struct v2 pos = V2(1, 0); struct v2 size = V2(1, 1); @@ -264,8 +263,8 @@ INTERNAL void spawn_test_entities(f32 offset) /* Box */ #if 1 - if (!G.first_spawn) { - struct v2 pos = V2(0.5, -1); + if (!G.extra_spawn) { + struct v2 pos = V2(0.5, 0); //struct v2 pos = V2(0.5, 29); //struct v2 pos = V2(0.5, 24); //struct v2 pos = V2(1, -1); @@ -295,7 +294,7 @@ INTERNAL void spawn_test_entities(f32 offset) #endif /* Camera */ - if (!G.first_spawn) { + if (!G.extra_spawn) { struct entity *e = entity_alloc(root); entity_set_xform(e, XFORM_IDENT); @@ -308,7 +307,7 @@ INTERNAL void spawn_test_entities(f32 offset) e->camera_quad_xform = XFORM_TRS(.s = V2(width, height)); } - G.first_spawn = true; + G.extra_spawn = true; } @@ -649,7 +648,6 @@ INTERNAL struct soft_result make_soft(f32 hertz, f32 zeta, f32 h) INTERNAL void solve_collisions(f32 dt, b32 apply_bias) { -#if 1 struct entity_store *store = G.tick.entity_store; for (u64 entity_index = 0; entity_index < store->reserved; ++entity_index) { struct entity *manifold = &store->entities[entity_index]; @@ -765,8 +763,8 @@ INTERNAL void solve_collisions(f32 dt, b32 apply_bias) f32 vt = v2_dot(vrel, tangent); f32 j = vt * k; - f32 friction = 0.6f; - //f32 friction = 1.0f; + //f32 friction = 0.6f; + f32 friction = 1.0f; //f32 friction = F32_INFINITY; f32 max_friction = friction * contact->normal_impulse; f32 old_impulse = contact->tangent_impulse; @@ -787,11 +785,6 @@ INTERNAL void solve_collisions(f32 dt, b32 apply_bias) e1->angular_velocity = w1; } } -#else - (UNUSED)make_soft; - (UNUSED)dt; - (UNUSED)apply_bias; -#endif } @@ -942,14 +935,14 @@ INTERNAL void game_update(struct game_cmd_array game_cmds) { logf_info("Clearing level"); entity_store_release_all_entities(store); - G.first_spawn = false; + G.extra_spawn = false; } break; /* Spawn test */ case GAME_CMD_KIND_SPAWN_TEST: { logf_info("Spawning (test)"); -#if 0 +#if GAME_SPAWN_LOTS for (u32 i = 0; i < 50; ++i) { spawn_test_entities(-(f32)i); } @@ -1054,7 +1047,7 @@ INTERNAL void game_update(struct game_cmd_array game_cmds) #if 1 if (entity_has_prop(ent, ENTITY_PROP_PLAYER_CONTROLLED)) { //if ((true)) { -#if 0 +#if 1 ent->local_collider.points[0] = V2(0, 0); ent->local_collider.count = 1; ent->local_collider.radius = 0.5; @@ -1471,7 +1464,7 @@ INTERNAL void game_update(struct game_cmd_array game_cmds) * Create ground friction force (gravity) * ========================== */ -#if 0 +#if GAME_PHYSICS_ENABLE_GROUND_FRICTION /* TODO: Do this globally rather than creating entities for constant forces */ for (u64 entity_index = 0; entity_index < store->reserved; ++entity_index) { struct entity *ent = &store->entities[entity_index]; @@ -1533,6 +1526,12 @@ INTERNAL void game_update(struct game_cmd_array game_cmds) * Physics * ========================== */ + + (UNUSED)create_contact_manifolds; + (UNUSED)solve_collisions; + (UNUSED)integrate_velocities_from_forces; + (UNUSED)integrate_positions_from_velocities; + (UNUSED)warm_start_contacts; #if 1 { integrate_velocities_from_forces(dt); @@ -1545,17 +1544,15 @@ INTERNAL void game_update(struct game_cmd_array game_cmds) #else (UNUSED)warm_start_contacts; #endif +#if GAME_PHYSICS_ENABLE_COLLISION solve_collisions(substep_dt, true); +#endif integrate_positions_from_velocities(substep_dt); +#if GAME_PHYSICS_ENABLE_COLLISION && GAME_PHYSICS_ENABLE_RELAXATION solve_collisions(substep_dt, false); /* Relaxation */ +#endif } } -#else - (UNUSED)create_contact_manifolds; - (UNUSED)solve_collisions; - (UNUSED)integrate_velocities_from_forces; - (UNUSED)integrate_positions_from_velocities; - (UNUSED)warm_start_contacts; #endif /* ========================== * diff --git a/src/user.c b/src/user.c index e895b8f6..33ab0b04 100644 --- a/src/user.c +++ b/src/user.c @@ -888,7 +888,7 @@ INTERNAL void user_update(void) } } - /* Debug draw info */ + /* Debug draw entity info */ if (G.debug_draw && !skip_debug_draw) { struct temp_arena temp = arena_temp_begin(scratch.arena); @@ -1145,7 +1145,7 @@ INTERNAL void user_update(void) draw_solid_arrow_line(G.viewport_canvas, start, end, arrow_thickness, arrow_height, color); } #if 1 - /* Draw info text */ + /* Draw contact info */ { struct font *disp_font = font_load_async(STR("res/fonts/fixedsys.ttf"), 12.0f); if (disp_font) {