From 161e615aa71e241e6f6cdc651d12fef4cd0e29d3 Mon Sep 17 00:00:00 2001 From: jacob Date: Mon, 28 Oct 2024 15:58:00 -0500 Subject: [PATCH] specify control torque in turns / second --- src/config.h | 2 +- src/entity.c | 2 +- src/entity.h | 4 ++-- src/game.c | 47 ++++++++++++++++++++++------------------------- src/user.c | 2 +- 5 files changed, 27 insertions(+), 30 deletions(-) diff --git a/src/config.h b/src/config.h index 2d638874..ad473c5a 100644 --- a/src/config.h +++ b/src/config.h @@ -45,7 +45,7 @@ #define GAME_PLAYER_AIM 1 #define GAME_MAX_LINEAR_VELOCITY 100 -#define GAME_MAX_ANGULAR_VELOCITY ((2 * PI) * 20) +#define GAME_MAX_ANGULAR_VELOCITY (TAU * 20) /* How many ticks back in time should the user blend between? * = * diff --git a/src/entity.c b/src/entity.c index be7cd695..d6f8ed94 100644 --- a/src/entity.c +++ b/src/entity.c @@ -305,7 +305,7 @@ void entity_set_local_xform(struct entity *ent, struct xform xf) } /* ========================== * - * Impulse + * Movement * ========================== */ void entity_apply_linear_impulse(struct entity *ent, struct v2 impulse, struct v2 point) diff --git a/src/entity.h b/src/entity.h index f29de786..d7e59043 100644 --- a/src/entity.h +++ b/src/entity.h @@ -222,7 +222,7 @@ struct entity { /* Control */ f32 control_force; /* How much force is applied to achieve desired control movement */ - f32 control_torque; /* How much torque is applied to achieve desired control focus */ + f32 control_turn_speed; /* Turn speed in turns / second */ struct { struct v2 move; struct v2 focus; @@ -404,7 +404,7 @@ struct xform entity_get_local_xform(struct entity *ent); void entity_set_xform(struct entity *ent, struct xform xf); void entity_set_local_xform(struct entity *ent, struct xform xf); -/* Impulse */ +/* Movement */ void entity_apply_linear_impulse(struct entity *ent, struct v2 impulse, struct v2 world_point); void entity_apply_force(struct entity *ent, struct v2 impulse, struct v2 world_point); void entity_apply_linear_impulse_to_center(struct entity *ent, struct v2 impulse); diff --git a/src/game.c b/src/game.c index c0a913b4..c353e71d 100644 --- a/src/game.c +++ b/src/game.c @@ -150,10 +150,10 @@ INTERNAL void spawn_test_entities(f32 offset) pos = v2_add(pos, V2(0, offset_all)); //struct v2 size = V2(1, 1); - struct v2 size = V2(0.25, 0.25); + //struct v2 size = V2(0.25, 0.25); //struct v2 size = V2(0.5, 0.5); //struct v2 size = V2(0.5, 0.25); - //struct v2 size = V2(1.0, 1.0); + struct v2 size = V2(1.0, 1.0); //struct v2 size = V2(1.5, 1.5); //f32 r = PI; f32 r = PI / 4; @@ -168,8 +168,8 @@ INTERNAL void spawn_test_entities(f32 offset) entity_set_xform(e, xf); - //e->sprite = sprite_tag_from_path(STR("res/graphics/tim.ase")); - e->sprite = sprite_tag_from_path(STR("res/graphics/box.ase")); + e->sprite = sprite_tag_from_path(STR("res/graphics/tim.ase")); + //e->sprite = sprite_tag_from_path(STR("res/graphics/box.ase")); //e->sprite = sprite_tag_from_path(STR("res/graphics/box_rounded.ase")); //e->sprite_span_name = STR("idle.unarmed"); //e->sprite_span_name = STR("idle.one_handed"); @@ -184,13 +184,13 @@ INTERNAL void spawn_test_entities(f32 offset) //e->control_force = 5000; e->control_force = 250; #endif - e->control_torque = 500; + e->control_turn_speed = 10; e->control.focus = V2(0, -1); entity_enable_prop(e, ENTITY_PROP_PHYSICAL); e->mass_unscaled = 50; //e->inertia_unscaled = F32_INFINITY; - e->inertia_unscaled = 25; + e->inertia_unscaled = 100; e->linear_ground_friction = 200; e->angular_ground_friction = 100; @@ -1487,20 +1487,6 @@ INTERNAL void game_update(struct game_cmd_array game_cmds) * Create forces from control move * ========================== */ -#if 0 - for (u64 entity_index = 0; entity_index < store->reserved; ++entity_index) { - struct entity *ent = &store->entities[entity_index]; - if (!entity_is_valid_and_active(ent)) continue; - - struct v2 move = ent->control.move; - if (entity_has_prop(ent, ENTITY_PROP_PLAYER_CONTROLLED)) { - struct entity *f = entity_alloc(ent); - entity_enable_prop(f, ENTITY_PROP_FORCE); - f->force = v2_mul(move, ent->control_force); - activate_now(f); - } - } -#else for (u64 entity_index = 0; entity_index < store->reserved; ++entity_index) { struct entity *ent = &store->entities[entity_index]; if (!entity_is_valid_and_active(ent)) continue; @@ -1511,7 +1497,6 @@ INTERNAL void game_update(struct game_cmd_array game_cmds) entity_apply_force_to_center(ent, force); } } -#endif /* ========================== * * Create forces from control focus (aim) @@ -1537,11 +1522,22 @@ INTERNAL void game_update(struct game_cmd_array game_cmds) struct motor_joint_def def = ZI; def.e0 = joint_ent->handle; /* Re-using joint entity as e0 */ def.e1 = ent->handle; - def.correction_rate = 1.0; + def.correction_rate = 0.2; def.max_force = 0; - def.max_torque = ent->control_torque; + + //def.max_torque = ent->control_torque; + joint_ent->motor_joint_data = motor_joint_from_def(def); ent->aim_joint = joint_ent->handle; + + joint_ent->mass_unscaled = F32_INFINITY; + joint_ent->inertia_unscaled = F32_INFINITY; + } + + { + /* TODO: Why is 25 working here? */ + f32 inertia_scaled = ent->inertia_unscaled * math_fabs(xform_get_determinant(xf)); + joint_ent->motor_joint_data.max_torque = (25 * PI * inertia_scaled) * ent->control_turn_speed; } /* Solve for final angle using law of sines */ @@ -1587,15 +1583,16 @@ INTERNAL void game_update(struct game_cmd_array game_cmds) new_angle = math_unwind_angle(v2_angle_from_dirs(V2(0, -1), v2_sub(focus_pos, ent_pos)) + final_ent_angle_btw_focus_and_hold - forward_hold_angle_offset); } - joint_ent->angular_velocity = 0; + f32 new_vel = 0; if (!F32_IS_NAN(new_angle)) { const f32 angle_error_allowed = 0.001; struct xform joint_xf = entity_get_xform(joint_ent); f32 diff = math_unwind_angle(new_angle - xform_get_rotation(joint_xf)); if (math_fabs(diff) > angle_error_allowed) { - joint_ent->angular_velocity = diff / dt; + new_vel = diff / dt; } } + joint_ent->angular_velocity = new_vel; } } #endif diff --git a/src/user.c b/src/user.c index ff27f412..136da616 100644 --- a/src/user.c +++ b/src/user.c @@ -496,7 +496,7 @@ INTERNAL void user_update(void) } e->control_force = math_lerp(e0->control_force, e1->control_force, tick_blend); - e->control_torque = math_lerp(e0->control_torque, e1->control_torque, tick_blend); + e->control_turn_speed = math_lerp(e0->control_turn_speed, e1->control_turn_speed, tick_blend); e->linear_velocity = v2_lerp(e0->linear_velocity, e1->linear_velocity, tick_blend); e->angular_velocity = math_lerp(e0->angular_velocity, e1->angular_velocity, tick_blend);