Created
February 28, 2012 23:46
-
-
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...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Btw: still need to add an extra fall velocity when position vector reaches top of arc and starts descending again.