Skip to content

Instantly share code, notes, and snippets.

@CodeDraken
Created December 12, 2019 20:08
Show Gist options
  • Save CodeDraken/d6ddf00c31c6948d9aa4fb99f235a4ef to your computer and use it in GitHub Desktop.
Save CodeDraken/d6ddf00c31c6948d9aa4fb99f235a4ef to your computer and use it in GitHub Desktop.
update () {
const { props, sceneProps } = this
// bottom bound / floor
if (this.y + props.radius >= sceneProps.height) {
this.velY *= -props.bounce
this.y = sceneProps.height - props.radius
this.velX *= sceneProps.friction
}
// top bound / ceiling
if (this.y - props.radius <= 0) {
this.velY *= -props.bounce
this.y = props.radius
this.velX *= sceneProps.friction
}
// left bound
if (this.x - props.radius <= 0) {
this.velX *= -props.bounce
this.x = props.radius
}
// right bound
if (this.x + props.radius >= sceneProps.width) {
this.velX *= -props.bounce
this.x = sceneProps.width - props.radius
}
// reset insignificant amounts to 0
if (this.velX < 0.01 && this.velX > -0.01) {
this.velX = 0
}
if (this.velY < 0.01 && this.velY > -0.01) {
this.velY = 0
}
// update position
this.velY += sceneProps.gravity
this.x += this.velX
this.y += this.velY
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment