Skip to content

Instantly share code, notes, and snippets.

@Breefield
Created December 10, 2011 02:20
Show Gist options
  • Save Breefield/1454316 to your computer and use it in GitHub Desktop.
Save Breefield/1454316 to your computer and use it in GitHub Desktop.
Bounce
box = {x: 10, y: 10, width: 40, height: 50, vX: 5, vY: 5};
bounds = {left: 0, right: 800, top: 0, bottom: 400};
frame() {
box.nX = box.x + box.vX;
box.nY = box.y + box.vY;
if(box.nX < bounds.left) box.vX *= -1;
if(box.nX + box.width > bounds.right) box.vX *= -1;
if(box.nY < bounds.top) box.vY *= -1;
if(box.nY + box.height > bounds.bottom) box.vY *= -1;
box.x += box.vX;
box.y += box.vY;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment