Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 15 additions & 15 deletions crates-ptx/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 24 additions & 19 deletions src_kernels/cuda/grid_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -125,25 +126,29 @@ fn update_single_cell(

if apply_friction {
Copy link
Collaborator

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

Suggested change
if apply_friction {
if apply_friction_and_cancel_penetration {

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);
Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This amounts to changing the line above to let dist_with_margin = (dist - cell_width * 1.5).max(0.0);.

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;
}
}
}
Expand Down