fix mouse joint instability

This commit is contained in:
jacob 2025-05-20 00:26:38 -05:00
parent 60c17aac63
commit 079ae4c47c
4 changed files with 19 additions and 16 deletions

View File

@ -743,7 +743,7 @@ struct phys_mouse_joint phys_mouse_joint_from_def(struct phys_mouse_joint_def de
struct phys_mouse_joint res = ZI; struct phys_mouse_joint res = ZI;
res.target = def.target; res.target = def.target;
res.point_local_start = def.point_local_start; res.point_local_start = def.point_local_start;
res.point_local_end = def.point_local_end; res.point_end = def.point_end;
res.linear_spring_hz = def.linear_spring_hz; res.linear_spring_hz = def.linear_spring_hz;
res.linear_spring_damp = def.linear_spring_damp; res.linear_spring_damp = def.linear_spring_damp;
res.angular_spring_hz = def.angular_spring_hz; res.angular_spring_hz = def.angular_spring_hz;
@ -856,7 +856,7 @@ void phys_solve_mouse_joints(struct phys_step_ctx *ctx, f32 dt)
struct xform xf = sim_ent_get_xform(ent); struct xform xf = sim_ent_get_xform(ent);
struct v2 point_start = xform_mul_v2(xf, joint->point_local_start); struct v2 point_start = xform_mul_v2(xf, joint->point_local_start);
struct v2 point_end = xform_mul_v2(xf, joint->point_local_end); struct v2 point_end = joint->point_end;
struct v2 vcp = v2_sub(point_start, xf.og); struct v2 vcp = v2_sub(point_start, xf.og);
struct v2 separation = v2_sub(point_start, point_end); struct v2 separation = v2_sub(point_start, point_end);

View File

@ -139,7 +139,7 @@ void phys_solve_motor_joints(struct phys_step_ctx *ctx, f32 dt);
struct phys_mouse_joint_def { struct phys_mouse_joint_def {
struct sim_ent_id target; struct sim_ent_id target;
struct v2 point_local_start; struct v2 point_local_start;
struct v2 point_local_end; struct v2 point_end;
f32 linear_spring_hz; f32 linear_spring_hz;
f32 linear_spring_damp; f32 linear_spring_damp;
f32 angular_spring_hz; f32 angular_spring_hz;
@ -150,7 +150,7 @@ struct phys_mouse_joint_def {
struct phys_mouse_joint { struct phys_mouse_joint {
struct sim_ent_id target; struct sim_ent_id target;
struct v2 point_local_start; struct v2 point_local_start;
struct v2 point_local_end; struct v2 point_end;
f32 linear_spring_hz; f32 linear_spring_hz;
f32 linear_spring_damp; f32 linear_spring_damp;
f32 angular_spring_hz; f32 angular_spring_hz;

View File

@ -234,7 +234,8 @@ INTERNAL void spawn_test_entities2(struct sim_ent *parent, struct v2 pos)
/* Big box */ /* Big box */
#if 1 #if 1
{ {
struct sim_ent *e = sim_ent_alloc_local(parent); //struct sim_ent *e = sim_ent_alloc_local(parent);
struct sim_ent *e = sim_ent_alloc_sync_src(parent);
f32 r = 0; f32 r = 0;
struct v2 size = V2(0.5, 0.25); struct v2 size = V2(0.5, 0.25);
@ -1263,7 +1264,6 @@ void sim_step(struct sim_step_ctx *ctx)
sim_ent_enable_prop(joint_ent, SEPROP_ACTIVE); sim_ent_enable_prop(joint_ent, SEPROP_ACTIVE);
} }
struct xform xf = sim_ent_get_xform(target_ent); struct xform xf = sim_ent_get_xform(target_ent);
f32 mass = target_ent->mass_unscaled * math_fabs(xform_get_determinant(xf));
struct phys_mouse_joint_def def = phys_mouse_joint_def_init(); struct phys_mouse_joint_def def = phys_mouse_joint_def_init();
def.target = target_ent->id; def.target = target_ent->id;
@ -1272,8 +1272,12 @@ void sim_step(struct sim_step_ctx *ctx)
} else { } else {
def.point_local_start = xform_invert_mul_v2(xf, cursor); def.point_local_start = xform_invert_mul_v2(xf, cursor);
} }
def.point_local_end = xform_invert_mul_v2(xf, cursor); def.point_end = cursor;
def.max_force = mass * 1000; def.max_force = F32_INFINITY;
def.linear_spring_hz = 5;
def.linear_spring_damp = 0.7;
def.angular_spring_hz = 5;
def.angular_spring_damp = 0.1;
joint_ent->mouse_joint_data = phys_mouse_joint_from_def(def); joint_ent->mouse_joint_data = phys_mouse_joint_from_def(def);
} else if (sim_ent_is_valid_and_active(joint_ent)) { } else if (sim_ent_is_valid_and_active(joint_ent)) {
joint_ent->mouse_joint_data.target = target_ent->id; joint_ent->mouse_joint_data.target = target_ent->id;

View File

@ -1194,14 +1194,13 @@ INTERNAL void user_update(void)
if (sim_ent_has_prop(ent, SEPROP_MOUSE_JOINT)) { if (sim_ent_has_prop(ent, SEPROP_MOUSE_JOINT)) {
struct sim_ent *target = sim_ent_from_id(G.ss_blended, ent->mouse_joint_data.target); struct sim_ent *target = sim_ent_from_id(G.ss_blended, ent->mouse_joint_data.target);
struct xform target_xf = sim_ent_get_xform(target); struct xform target_xf = sim_ent_get_xform(target);
u32 color = COLOR_WHITE;
u32 color = COLOR_YELLOW; struct v2 point_start = xform_mul_v2(target_xf, ent->mouse_joint_data.point_local_start);
f32 radius = 3; struct v2 point_end = G.world_cursor;
struct v2 point = xform_mul_v2(target_xf, ent->mouse_joint_data.point_local_end); point_start = xform_mul_v2(G.world_to_ui_xf, point_start);
point = xform_mul_v2(G.world_to_ui_xf, point); point_end = xform_mul_v2(G.world_to_ui_xf, point_end);
draw_circle(G.ui_cmd_buffer, point, radius, color, 10); draw_arrow_line(G.ui_cmd_buffer, point_start, point_end, 3, 10, color);
draw_circle(G.ui_cmd_buffer, point_start, 4, color, 10);
DEBUGBREAKABLE;
} }
/* Draw collider */ /* Draw collider */