Sphere elastic collision position and velocity calculation algorithms

# My original, a lot of effort calculation formula is derived by verifying perfect

# Two goals position and velocity, R is the radius, mass as provided herein, together with different quality and easy radius
DEF Collide (loc1, LOC2, SP1, SP2):
  X, Y = loc1
  X2, Y2 = LOC2
  DLX, DLY X2-X =, Y Y2-

  DX, Dy = SP1
  DX2, Dy2 = SP2
  DVX, DVY = DX-DX2, Dy-Dy2

  # preceding If the current "excessive collisions" and then goes back to the position request tangential position

  if ** ** 2 + DLY DLX 2 <R & lt ** 2:
    X, X-Y = DX, Dy Y-
    X2, Y2-DX2 = X2, Y2-Dy2
    DLX, DLY = X-X2, Y2-Y
    B = ** 2 * R & lt (DVX DVY ** ** 2 + 2) - (* DLX DVY - DVX DLY *) ** 2
    # calculates the time to collision
    p = -dlx * dvx -dly * dvy + math.sqrt (b)
    p2 = -dlx * dvx -dly * dvy - math.sqrt (b)

    if dvx**2 + dvy**2==0:
      t=1

    # Because it will be tangent to two has two solutions, a smaller take
    the else:
      TEMP = min ((ABS (P), ABS (P2)))
      T = (TEMP) / (2 + DVY DVX ** ** 2)


    # collision position
    loc1 * DX = X + T, Y + T * Dy
    LOC2 = T * X2 + DX2, T * Y2 + Dy2


    ## print(math.sqrt((loc1[1]-loc2[1])**2+(loc1[0]-loc2[0])**2))

    # Changed after the collision velocity, momentum exchange request
    IF DLY == 0:
      EY = 0
      EX = DVX
     the else:
      K = DLX / DLY
    EY = (K * + DVY DVX) / (K * K +. 1)
    EX = K * EY
    SP1 = -ex DX, Dy-EY
    SP2 = DX2 + EX, EY + Dy2
  return [loc1, LOC2, SP1, SP2]

Guess you like

Origin www.cnblogs.com/chgq/p/11105978.html