From 8eadda99311617e780cdab25d90a6800ac11d9b2 Mon Sep 17 00:00:00 2001 From: jacob Date: Tue, 29 Oct 2024 18:35:35 -0500 Subject: [PATCH] store & calculate friction --- src/entity.c | 2 ++ src/entity.h | 7 +++++++ src/game.c | 12 ++++++++---- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/entity.c b/src/entity.c index d6f8ed94..7a252062 100644 --- a/src/entity.c +++ b/src/entity.c @@ -16,6 +16,7 @@ READONLY struct entity _g_entity_nil = { .local_xform = XFORM_IDENT_NOCAST, .cached_global_xform = XFORM_IDENT_NOCAST, .cached_global_xform_dirty = false, + .friction = 0.5f, .mass_unscaled = 1, .inertia_unscaled = 1, .sprite_local_xform = XFORM_IDENT_NOCAST, @@ -27,6 +28,7 @@ GLOBAL READONLY struct entity g_entity_default = { .local_xform = XFORM_IDENT_NOCAST, .cached_global_xform = XFORM_IDENT_NOCAST, .cached_global_xform_dirty = true, + .friction = 0.5f, .mass_unscaled = 1, .inertia_unscaled = 1, .sprite_local_xform = XFORM_IDENT_NOCAST, diff --git a/src/entity.h b/src/entity.h index d489daae..697254f9 100644 --- a/src/entity.h +++ b/src/entity.h @@ -91,6 +91,8 @@ struct contact_constraint { struct contact_point points[2]; u32 num_points; + f32 friction; + struct math_spring_result softness; f32 pushout_velocity; @@ -275,8 +277,13 @@ struct entity { /* ENTITY_PROP_PHYSICAL */ + //f32 density; /* Density in kg/m^2 */ + + f32 friction; + f32 mass_unscaled; /* Mass of entity in kg before any transformations */ f32 inertia_unscaled; /* Inertia of entity in kg*m^2 before any transformations */ + f32 linear_ground_friction; f32 angular_ground_friction; diff --git a/src/game.c b/src/game.c index 0a3e7145..4ede1961 100644 --- a/src/game.c +++ b/src/game.c @@ -176,11 +176,13 @@ INTERNAL void spawn_test_entities(f32 offset) e->angular_ground_friction = 100; + //e->control_force = 500; e->control_force = 500; e->control_force_max_speed = 4; - e->control_torque = 5000; + e->control_torque = 500; entity_enable_prop(e, ENTITY_PROP_PHYSICAL); + e->mass_unscaled = 10; e->inertia_unscaled = 10; @@ -189,7 +191,7 @@ INTERNAL void spawn_test_entities(f32 offset) player_ent = e; -#if 0 +#if 1 { struct entity *joint_ent = entity_alloc(root); entity_enable_prop(joint_ent, ENTITY_PROP_PHYSICAL); @@ -487,6 +489,8 @@ INTERNAL void create_contacts(void) if (res.num_points > 0) { struct contact_constraint *constraint = &constraint_ent->contact_constraint_data; + constraint->friction = math_sqrt(e0->friction * e1->friction); + /* Delete old contacts that are no longer present */ for (u32 i = 0; i < constraint->num_points; ++i) { struct contact_point *old = &constraint->points[i]; @@ -769,10 +773,10 @@ INTERNAL void solve_contacts(f32 dt, b32 apply_bias) f32 vt = v2_dot(vrel, tangent); f32 j = vt * k; - f32 friction = 0.6f; + //f32 friction = 0.6f; //f32 friction = 1.0f; //f32 friction = F32_INFINITY; - f32 max_friction = friction * point->normal_impulse; + f32 max_friction = constraint->friction * point->normal_impulse; f32 old_impulse = point->tangent_impulse; f32 new_impulse = clamp_f32(old_impulse + j, -max_friction, max_friction); f32 delta = new_impulse - old_impulse;