momentumExchange¶
-
taichi_glsl.lagrangian.
momentumExchange
(v1, v2, disp, m1=1, m2=1, gamma=1)¶ Exchange momentum (bounce) between two objects.
momentumExchange should be invocated when a bounce occurred. It takes the velocity of two objects before bounce, and returns the velocity of two objects after bounce.
This function is most useful in rigid-body simulation with collision. For example:
if distance(pos[i], pos[j]) < radius[i] + radius[j]: # Collision detected! Perform a momentum exchange: vel[i], vel[j] = momentumExchange( vel[i], vel[j], mass[i], mass[j], pos[i] - pos[j], 0.8)
- Parameters
v1 – (Vector) The velocity vector of the first object to bounce. Or, the velocity vector at the collision point in first object.
v2 – (Vector) The velocity vector of the second object to bounce. Or, the velocity vector at the collision point in second object.
disp – (Vector) The displacement vector from between two object. Or, the normal vector of collision surface. Specifically, for balls or circles, disp is pos1 - pos2.
m1 – (scalar) The mass of the first object to bounce.
m2 – (scalar) The mass of the second object to bounce.
gamma – (scalar) The decrease factor of bounce, in range [0, 1], determines how much energy is conserved after the bounce process. If 1, then no energy is loss; if 0, then the collided objects will stops immediately.
- Returns
(tuple of Vector) The return value is a tuple of velocity of two objects after bounce. Specifically the first element is for the velocity of first object (previously to be v1), and same to the second element.
- Note
For usage example, check out this: https://github.com/taichi-dev/taichi_three/blob/master/examples/many_balls.py