Skip to content

Instantly share code, notes, and snippets.

@telamon
Last active August 29, 2015 14:00
Show Gist options
  • Save telamon/11403910 to your computer and use it in GitHub Desktop.
Save telamon/11403910 to your computer and use it in GitHub Desktop.
DeltaTime dependend positioning.
float accumlatedTime=0;
Sprite cow;
float runSpeed = 1; # in pixels per second
float fallingAccelleration = 0.3; # how fast the fall should reach topspeed plummeting.
float maxFallVelocity = 3.0; # Should not be able to fall faster than 3px/s
onFrame(float deltaTime):
accumulatedTime + = deltaTime;
# Timestamp when a jump reached it's peak height and you're scheduled with a romantic date with gravity.
if(cow.maxJumpHeightReached()):
cow.fallingAt = accumulatedTime;
# X axis travel is the simple form as it does not involve accelleration.
cow.x + = deltaTime * speed; # use -speed if direction is left instead of right.
# Falling along the Y axis if cow is in falling state.
if(cow.isFalling()):
# Calculate amount of time the cow has been falling.
float fallTime = cow.fallingAt - accumulatedTime;
# Calculate current falling velocity capped to maxFallVelocity
float fallVelocity = Math.min(fallTime * fallingAccelleration, maxFallVelocity);
cow.y+= fallVelocity;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment