-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: apply ground friction on predictive velocity correction too #7
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,11 +108,12 @@ fn update_single_cell( | |
} | ||
} | ||
BoundaryHandling::Friction | BoundaryHandling::FrictionZUp => { | ||
if let Some((mut normal, dist)) = | ||
if let Some((mut normal, mut dist)) = | ||
Unit::try_new_and_get(cell.projection_scaled_dir, 1.0e-5) | ||
{ | ||
if is_inside { | ||
normal = -normal; | ||
dist = 0.0; | ||
} | ||
|
||
#[cfg(feature = "dim2")] | ||
|
@@ -125,25 +126,29 @@ fn update_single_cell( | |
|
||
if apply_friction { | ||
let normal_vel = cell_velocity.dot(&normal); | ||
let dist_with_margin = (dist - cell_width * 0.5).max(0.0); | ||
|
||
if normal_vel < 0.0 { | ||
let dist_with_margin = dist - cell_width; | ||
if is_inside || dist_with_margin <= 0.0 { | ||
let tangent_vel = | ||
cell_velocity - normal_vel * normal.into_inner(); | ||
let tangent_vel_norm = tangent_vel.norm(); | ||
|
||
cell_velocity = tangent_vel; | ||
|
||
if tangent_vel_norm > 1.0e-10 { | ||
let friction = collider.friction; | ||
cell_velocity = tangent_vel / tangent_vel_norm | ||
* (tangent_vel_norm + normal_vel * friction).max(0.0); | ||
} | ||
} else if -normal_vel * dt > dist_with_margin { | ||
cell_velocity -= | ||
(dist_with_margin / dt + normal_vel) * normal.into_inner(); | ||
} | ||
if -normal_vel * dt > dist_with_margin { | ||
// NOTE: if we enter this code, normal_vel is negative. | ||
let tangent_vel = cell_velocity - normal_vel * normal.into_inner(); | ||
let tangent_vel_norm = tangent_vel.norm(); | ||
|
||
let new_normal_vel = normal_vel.max(-dist_with_margin / dt); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even if we zero the normal velocity out completely, particles can still approach the boundary as their non-zero-kernel space goes further. We may want to extend the margin s.t. we cover the whole range of an approaching particle. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This amounts to changing the line above to This also restores the previous behavior in the fluid example btw. |
||
|
||
// NOTE: removed_normal_vel is always positive. | ||
let removed_normal_vel = new_normal_vel - normal_vel; | ||
|
||
let new_tangent_vel = if tangent_vel_norm > 1.0e-6 { | ||
tangent_vel / tangent_vel_norm | ||
* (tangent_vel_norm | ||
- removed_normal_vel * collider.friction) | ||
.max(0.0) | ||
} else { | ||
Vector::zeros() | ||
}; | ||
|
||
cell_velocity = | ||
new_normal_vel * normal.into_inner() + new_tangent_vel; | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be a better name