Skip to content

Instantly share code, notes, and snippets.

@MyGodIsHe
Last active December 20, 2015 06:48
Show Gist options
  • Save MyGodIsHe/6088295 to your computer and use it in GitHub Desktop.
Save MyGodIsHe/6088295 to your computer and use it in GitHub Desktop.
"""
Predicting the collision of two cubes.
"""
def find_terms(a, b, c):
if not a:
return [(None, None)] if abs(b) <= c else None
down, up = sorted(((c - b) / a, (-c - b) / a))
if abs(a * (down - 1) + b) <= c:
return [(None, down), (up, None)]
return [(down, up)]
def predict(V1, P1, V2, P2, D):
dVx = V1[0] - V2[0]
dVy = V1[1] - V2[1]
dPx = P1[0] - P2[0]
dPy = P1[1] - P2[1]
terms = []
for dV, dP in [(dVx, dPx), (dVy, dPy)]:
_terms = find_terms(dV, dP, D)
if _terms is None:
return
terms.extend(_terms)
down, up = None, None
for td, tu in terms:
if td > down:
down = td
if up is None or tu is not None and tu < up:
up = tu
if down <= up:
return down, up
print predict((0, 0), (0, 0), (-1, 0), (10, 0), 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment