Skip to content

Instantly share code, notes, and snippets.

@tito
Created March 26, 2018 16:01
Show Gist options
  • Save tito/a4b9a7a0016ad3b596b6881c753bfd28 to your computer and use it in GitHub Desktop.
Save tito/a4b9a7a0016ad3b596b6881c753bfd28 to your computer and use it in GitHub Desktop.
Distance approximation from velocity, friction and fps
def get_distance_from_velocity_loop(self, vel, fri, dt=1 / 60.):
# original algo from tito
dist = 0
it = 0
while abs(vel) > .5:
it += 1
vel -= vel * fri
dist += vel * dt
return dist
def get_distance_from_velocity_math(self, vel, fri, dt=1 / 60.):
# adapted from inclement (https://gist.github.com/inclement/4e004c4efb5c154fc4790ca11290a68f)
direction = -1 if vel < 0 else 1
vel = abs(vel)
fri = fri / dt
time_taken = -1 * log(0.5 / vel) / fri
distance_travelled = (
-1 * vel / fri * exp(-1 * fri * time_taken)
+ vel / fri)
return distance_travelled * direction
def get_distance_from_velocity_geom(self, vel, fri, dt=1 / 60.):
# from arndt (https://stackoverflow.com/questions/49494658/mathematically-sum-a-sequence)
k = 1 - fri
r = k / (1 - k)
return r * vel * (1 / dt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment