Skip to content

Instantly share code, notes, and snippets.

@squarefeet
Created February 28, 2012 23:46
Show Gist options
  • Save squarefeet/1936265 to your computer and use it in GitHub Desktop.
Save squarefeet/1936265 to your computer and use it in GitHub Desktop.
Sine arc jumping. This chunk of code is called on each update tick, and with each tick, the position vector is moved along the sine curve...
// If jump key is pressed, toggle the isJumping property, reset the
// jumpSinPos to zero, and set the y position.
if(keyHandler.isKeyPressed('space') && !this.isJumping) {
this.vel.y = 0;
this.jumpSinPos = 0;
this.isJumping = true;
this.jumpStartPos = this.pos.y;
}
// If we're jumping, move along the sine wave.
else if(this.isJumping) {
// Increment the position on the sine wave
this.jumpSinPos += dt / this.jumpTime;
var sinePos = Math.sin(this.jumpSinPos);
// Alter position vector directly since we're working with a Sine
// arc instead of velocity for the jump action.
this.pos.y = this.jumpStartPos - Math.sin(this.jumpSinPos);
// Apply the jump height
this.pos.y -= sinePos * this.jumpHeight;
}
@squarefeet
Copy link
Author

Btw: still need to add an extra fall velocity when position vector reaches top of arc and starts descending again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment