SHARE
TWEET

collision velocity fixing

a guest Jan 17th, 2020 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local function resolveDynamically(b1, b2)
  2.    
  3.     -- Distance between balls
  4.     --local fDistance = um.distance(b1.px, b1.py, b2.px, b2.py)
  5.     local fDistance = math.sqrt((b1.px - b2.px)*(b1.px - b2.px) + (b1.py - b2.py)*(b1.py - b2.py));
  6.  
  7.     -- Normal
  8.     local nx = (b2.px - b1.px) / fDistance;
  9.     local ny = (b2.py - b1.py) / fDistance;
  10.  
  11.     -- Tangent
  12.     local tx = -ny;
  13.     local ty = nx;
  14.  
  15.     -- Dot Product Tangent
  16.     local dpTan1 = b1.vx * tx + b1.vy * ty;
  17.     local dpTan2 = b2.vx * tx + b2.vy * ty;
  18.  
  19.     -- Dot Product Normal
  20.     local dpNorm1 = b1.vx * nx + b1.vy * ny;
  21.     local dpNorm2 = b2.vx * nx + b2.vy * ny;
  22.  
  23.     -- Conservation of momentum in 1D
  24.     local m1 = (dpNorm1 * (b1.weight - b2.weight) + 2.0 * b2.weight * dpNorm2) / (b1.weight + b2.weight);
  25.     local m2 = (dpNorm2 * (b2.weight - b1.weight) + 2.0 * b1.weight * dpNorm1) / (b1.weight + b2.weight);
  26.  
  27.     local b1Speed = math.sqrt(b1.vx ^ 2 + b1.vy ^ 2)
  28.     local b2Speed = math.sqrt(b2.vx ^ 2 + b2.vy ^ 2)
  29.     local precol = b1Speed + b2Speed
  30.     print("total speed: " .. precol)
  31.     -- Update ball velocities
  32.     b1.vx = tx * dpTan1 + nx * m1;
  33.     b1.vy = ty * dpTan1 + ny * m1;
  34.     b2.vx = tx * dpTan2 + nx * m2;
  35.     b2.vy = ty * dpTan2 + ny * m2;
  36.     b1Speed = math.sqrt(b1.vx ^ 2 + b1.vy ^ 2)
  37.     b2Speed = math.sqrt(b2.vx ^ 2 + b2.vy ^ 2)
  38.     local postcol = b1Speed + b2Speed
  39.     print("total speed: " .. postcol)
  40. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top