Skip to content

Instantly share code, notes, and snippets.

@nothke
Last active October 20, 2018 08:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nothke/c833e3e719572bd24171a6f1fc4825aa to your computer and use it in GitHub Desktop.
Save nothke/c833e3e719572bd24171a6f1fc4825aa to your computer and use it in GitHub Desktop.
-- variables
suspheight = 10 -- height of suspension
springrate = 10 -- spring stiffness
damprate = 1 -- damping amount
bouncemult = 0 -- bounce when we bottom out, keep between 0-1
velo = 0 -- verticla velocity
y = 100 -- distance from bottom of vehicle to ground
deltatime = 1/30 -- depending on framerate
-- ground is assumed to be at 0
-- in game loop
if y < suspheight then
target = y + suspheight
diff = target - y
spring = diff * springrate
damp = -velo * damprate
velo += (spring + damp) * deltatime
end
-- velocity update
velo -= 1 -- gravity
y += velo * deltatime
-- clamp so we don't go through ground
if y < 0 then
velo = -velo * bouncemult
value = 0;
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment