mirror of
https://github.com/markqvist/RNode_Firmware.git
synced 2026-04-27 14:30:33 +00:00
Fix bubble level: sub-step spring-damper for stability at 500ms GUI rate
The spring-damper (k=40, c=12) was unstable at dt=0.5s — position overshot max_r on every frame, got clamped, then overshot again, keeping the bubble stuck at the ring edge. Fix: sub-step the physics at 20ms intervals (25 steps per 500ms frame). The integration is now stable at any GUI update rate.
This commit is contained in:
parent
3848b97150
commit
fded16c19a
1 changed files with 10 additions and 5 deletions
15
Gui.h
15
Gui.h
|
|
@ -816,13 +816,18 @@ static void gui_update_data() {
|
|||
float target_y = -tilt_dir_y * mapped_r;
|
||||
|
||||
// Spring-damper: overdamped for viscous fluid feel
|
||||
// spring=40 damping=12 → settles in ~0.3s, no oscillation
|
||||
// Sub-step at 20ms to keep integration stable at any frame rate
|
||||
const float spring = 40.0f;
|
||||
const float damping = 12.0f;
|
||||
vel_x += (spring * (target_x - bub_x) - damping * vel_x) * dt;
|
||||
vel_y += (spring * (target_y - bub_y) - damping * vel_y) * dt;
|
||||
bub_x += vel_x * dt;
|
||||
bub_y += vel_y * dt;
|
||||
const float max_sub = 0.02f;
|
||||
int steps = (int)(dt / max_sub) + 1;
|
||||
float sdt = dt / steps;
|
||||
for (int si = 0; si < steps; si++) {
|
||||
vel_x += (spring * (target_x - bub_x) - damping * vel_x) * sdt;
|
||||
vel_y += (spring * (target_y - bub_y) - damping * vel_y) * sdt;
|
||||
bub_x += vel_x * sdt;
|
||||
bub_y += vel_y * sdt;
|
||||
}
|
||||
|
||||
// Clamp to ring boundary (bubble can't escape the fluid)
|
||||
float dist = sqrtf(bub_x * bub_x + bub_y * bub_y);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue