Collision#
Collision detection and response
This module implements two collision handling approaches:
IPC (Incremental Potential Contact): Rigorous collision handling using barrier potentials with provable non-penetration guarantees. Based on Li et al. “Incremental Potential Contact” (2020).
Simplified: Fast approximate collision detection using distance-based repulsion forces for real-time simulation.
The IPC barrier function is:
where \(d\) is the distance between primitives and \(\hat{d}\) is the activation distance. This creates smooth repulsive forces that prevent penetration.
- class diffsim.collision.IPCCollisionHandler(barrier_stiffness=1000.0, dhat=0.001, friction_mu=0.3)[source]#
Bases:
objectIPC (Incremental Potential Contact) collision handler
Implements collision handling using smooth barrier potentials as described in: Li, M., Ferguson, Z., Schneider, T., Langlois, T. R., Zorin, D., Panozzo, D., … & Jiang, C. (2020). Incremental potential contact: intersection-and inversion-free, large-deformation dynamics. ACM Trans. Graph., 39(4), 49.
The barrier function creates smooth repulsive forces that activate when primitives approach within a threshold distance \(\hat{d}\), preventing penetration while maintaining continuity for gradient-based optimization.
- Parameters:
- __init__(barrier_stiffness=1000.0, dhat=0.001, friction_mu=0.3)[source]#
Initialize IPC collision handler
- Parameters:
barrier_stiffness – stiffness of barrier potential
dhat – activation distance for barrier (collision threshold)
friction_mu – friction coefficient
- barrier_function(d_squared)[source]#
IPC barrier function: b(d) for distance d
- Parameters:
d_squared – squared distance
- Returns:
barrier value
- barrier_gradient(d_squared)[source]#
Gradient of barrier function
- Parameters:
d_squared – squared distance
- Returns:
gradient magnitude
- point_triangle_distance(p, v0, v1, v2)[source]#
Compute squared distance from point p to triangle (v0, v1, v2)
- Parameters:
p – \((N, 3)\) points
v0 – \((M, 3)\) triangle vertices
v1 – \((M, 3)\) triangle vertices
v2 – \((M, 3)\) triangle vertices
- Returns:
\((N, M)\) squared distances closest_points: \((N, M, 3)\) closest points on triangles
- Return type:
distances
- compute_self_collision_forces(mesh, positions)[source]#
Compute self-collision forces using IPC barrier potentials
This is a simplified version that checks vertex-face distances
- Parameters:
mesh – TetrahedralMesh
positions – \((N, 3)\) current positions
- Returns:
\((N, 3)\) collision forces
- Return type:
forces
- class diffsim.collision.SimplifiedCollisionHandler(collision_distance=0.02, repulsion_stiffness=10000.0, max_checks_per_frame=1000)[source]#
Bases:
objectFast simplified collision detection for real-time simulation
Uses distance-based repulsion without full CCD