Skip to content

Instantly share code, notes, and snippets.

@vgheri
Created December 24, 2012 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vgheri/4369350 to your computer and use it in GitHub Desktop.
Save vgheri/4369350 to your computer and use it in GitHub Desktop.
client physics loop
function updatePhysics() {
// 1: updates self position and direction
if (!pongR.updateTimestamp) { // The first time this loop runs, pongR.updateTimestamp will be undefined
pongR.updateTimestamp = new Date().getTime();
}
else {
var now = new Date().getTime();
pongR.deltaTime = (now - pongR.updateTimestamp) / 1000;
//console.log("Delta time: " + pongR.deltaTime + ". Now: " + now + ", previous update: " + pongR.updateTimestamp);
pongR.updateTimestamp = now;
}
pongR.me.topLeftVertex = computeNewClientPosition();
// 2: update ball position
var newPosition = updateBallPosition(pongR.game.ball.angle, pongR.game.ball.position, pongR.deltaTime, pongR.settings.BALL_FIXED_STEP);
pongR.game.ball.position = newPosition;
// 2: check collision
checkForCollisionsAndUpdateBallState();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment